[ PHPXref.com ] [ Generated: Sun Jul 20 20:11:31 2008 ] [ SAPID CMS 1.2.3 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/kernel/ -> vdb.lib.php (source)

   1  <?PHP
   2  // vim: set expandtab tabstop=4 shiftwidth=4:

   3  // +----------------------------------------------------------------------+

   4  // | SAPID: XML Sapiens Engine Demonstrator                               |

   5  // +----------------------------------------------------------------------+

   6  // | Author:  Max Baryshnikov aka Mephius <mb@rg.by>                      |

   7  // | Copyright (c) 2004 Max Baryshnikov                                   |

   8  // | http://sapid.sourceforge.net                                          |

   9  // +----------------------------------------------------------------------+

  10  // | This source file is free software; you can redistribute it and/or    |

  11  // | modify it under the terms of the GNU Lesser General Public           |

  12  // | License as published by the Free Software Foundation; either         |

  13  // | version 2.1 of the License, or (at your option) any later version.   |

  14  // |                                                                      |

  15  // | This source file is distributed in the hope that it will be useful,  |

  16  // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |

  17  // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |

  18  // | Lesser General Public License for more details.                      |

  19  // +----------------------------------------------------------------------+

  20  // Release: 13.11.04 (dd/mm/yy)

  21  // $Id: vdb.lib.php,v 1.3 2005/09/01 11:00:41 mephius Exp $

  22  
  23  
  24  
  25  class sapi_vdb {
  26      var $parser;
  27      var $depth;
  28      var $filename;
  29      var $objdata;
  30      var $id_of_row;
  31      var $last_name;
  32      var $filter;
  33      var $new_id;
  34  
  35  
  36      /**

  37      * @return true

  38      * @desc sapi_vdb class constructor

  39      */
  40  	function sapi_vdb($filename=false){
  41  
  42          if($filename) {
  43              if($this->filename AND $filename!=$this->filename) $this->close();
  44              $this->filename=$filename;
  45          }
  46  
  47          $this->filename=$filename;
  48          $this->objdata=array();
  49          $this->new_id=time();
  50          return true;
  51      }
  52  
  53  	function close(){
  54          //$this->filename=false;

  55          $this->objdata=array();
  56          $this->new_id=0;
  57          $this->parser=0;
  58          $this->depth=0;
  59          $this->id_of_row=0;
  60          $this->last_name=0;
  61          $this->filter=0;
  62          return true;
  63      }
  64  
  65  	function set_filter($filter){
  66          $code="";
  67          if(is_array($filter)){
  68              foreach ($filter as $key=>$exp) {
  69                  if($key=="DATE_CREATE") $code='strpos(\$attrs["DATE_CREATE"], "'.$exp.'")!==false';
  70                  if($key=="ID") $code.=(($code!="" and $exp!="")?' AND \$attrs["ID"]=='.$exp."":"");
  71              }
  72              $this->filter="\$pass=(" . $code . "); ";
  73          }
  74          return true;
  75      }
  76  
  77      /**

  78      * @return array

  79      * @param string $filename

  80      * @desc get data from xml file

  81      */
  82  	function get_data($filename=false, $commands=false){
  83  
  84  
  85          if($filename) {
  86              if($this->filename AND $filename!=$this->filename) $this->close();
  87              $this->filename=$filename;
  88          }
  89  
  90          switch($this->filename) {
  91              case "users":
  92              $this->filename = $root_path."usr/xml/users.xml";
  93              break;
  94          }
  95  
  96  
  97          $this->depth=0;
  98          $this->id_of_row=0;
  99          $this->last_name="";
 100  
 101          if($this->objdata) return $this->objdata;
 102  
 103          //if( preg_match("/_GETCONTENT_/is", $commands) ) $get_content = true; else $get_content = false;

 104  
 105          $this->parser = xml_parser_create();
 106          xml_set_object($this->parser, $this);
 107          xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, true);
 108          xml_set_element_handler($this->parser, "startElement", "endElement");
 109          xml_set_character_data_handler($this->parser, "characterData");
 110  
 111          $this->objdata = array();
 112          $this->depth = $this->last_name = $this->id_of_row = false;
 113  
 114          if (!($fp = @fopen($this->filename, "r"))) {
 115              //message_die("Couldn't open <b>" . $this->filename . "</b> for reading!", "Fatal Error");

 116          }else{
 117              while ($data = fread($fp, 4096)) {
 118                  if (!xml_parse($this->parser, $data, feof($fp))) {
 119  
 120                      //message_die("XML error: ".$this->filename);

 121                      die(sprintf("XML error: %s at line %d column %d in file " . $this->filename,
 122                      xml_error_string(xml_get_error_code($this->parser)),
 123                      xml_get_current_line_number($this->parser)
 124                      ));
 125  
 126                  }
 127              }
 128              xml_parser_free($this->parser);
 129          }
 130  
 131          $ids_c = 0;
 132          $ids = array();
 133          foreach ($this->objdata as $key=>$entry) {
 134              foreach ($entry as $k=>$data) {
 135                  $this->objdata[$key][$k]=$data;
 136              }
 137              $ids[$ids_c++]=$this->objdata[$key]["ID"];
 138          }
 139          if($ids) {
 140              rsort($ids);
 141              $this->new_id = $ids[0]+1;
 142          }
 143          if( preg_match("/_ONEROW_/is", $commands) ) return $entry;
 144          return $this->objdata;
 145      }
 146  
 147  
 148  
 149      /**

 150      * @return void

 151      * @param XML Parser resource id $parser

 152      * @param string $name

 153      * @param array $attrs

 154      * @desc Start element handler

 155      */
 156  	function startElement($parser, $name, $attrs) {
 157          if($this->depth[$parser]==1 AND $name="ROW") {
 158              $this->id_of_row+=1;
 159              @eval($this->filter);
 160              if (($this->filter and $pass) or $this->filter=="") {
 161                  $this->pass=true;
 162                  foreach ($attrs as $key=>$value) {
 163                      $this->objdata[$this->id_of_row][$key]=$value;
 164                  }
 165              }else $this->pass=false;
 166          }
 167  
 168          if($this->depth[$parser]==2 AND isset($attrs["VALUE"])) $this->objdata[$this->id_of_row][$name] = $attrs["VALUE"];
 169          $this->last_name = $name;
 170          if(!isset($this->depth[$parser])) $this->depth[$parser]=0;
 171          $this->depth[$parser]++;
 172      }
 173      /**

 174      * @return void

 175      * @param XML Parser resource id $parser

 176      * @param string $name

 177      * @desc End element handler

 178      */
 179  	function endElement($parser, $name) {
 180          $this->depth[$parser]--;
 181      }
 182  
 183      /**

 184      * @return void

 185      * @param XML Parser resource id $parser

 186      * @param string $data

 187      * @desc CData handler

 188      */
 189  	function characterData($parser, $data) {
 190          if($this->depth[$parser]==3 AND $data) {
 191              if ($this->pass) {
 192                  if(!isset($this->objdata[$this->id_of_row][$this->last_name])) $this->objdata[$this->id_of_row][$this->last_name]="";
 193                  $this->objdata[$this->id_of_row][$this->last_name] .= $data;
 194              }
 195          }
 196      }
 197  
 198  
 199      /*

 200      Appropriate values of $data["post_action"]

 201      ADDNEWIPROW

 202      ADDROW

 203      CHANGEROW

 204      UPDATEROW

 205      Source array $data["ROW"][..]

 206      */
 207  
 208  	function save_data_row($filename=false, $data=false, $unique_ip=false){
 209  
 210          if($filename) $this->filename=$filename;
 211          if (!$data) $data=$_POST;
 212          if($data["ROW"]) $data["row"] = $data["ROW"];
 213          if($this->filename AND $data["row"]) {
 214  
 215              $objdata=$this->get_data();
 216  
 217  
 218              $IP = getenv("REMOTE_ADDR");
 219              $DATE_CREATE = date("Y-m-d H:i:s");
 220              if(!$data["row"]["IP"]) $data["row"]["IP"] = $IP; else $IP = $data["row"]["IP"];
 221              if(!$data["row"]["DATE_CREATE"]) $data["row"]["DATE_CREATE"] = $DATE_CREATE; else $DATE_CREATE = $data["row"]["DATE_CREATE"];
 222  
 223              if($objdata and ($unique_ip ||  $data["post_action"]=="ADDNEWIPROW")) {
 224                  foreach($objdata as $fetch) {
 225                      $fetch_date_create=strtotime($fetch['DATE_CREATE']);
 226                      $current_date=strtotime(date('Y-m-d H:i:s'));
 227                      if ($fetch["IP"]==$IP && ($current_date-$fetch_date_create<=(VDB_SAMEIP_WRITE_DELAY*60))) $flag_ip_false = true;
 228  
 229                  }
 230              }
 231  
 232              $stream = "<data>\n";
 233              if((!$flag_ip_false AND $data["post_action"]=="ADDNEWIPROW" ) OR $data["post_action"] =="ADDROW" OR $data["post_action"] == "CHANGEROW" ) {
 234                  // Adding new row

 235                  $stream .= "\t<row ip=\"".$IP."\" id=\"".$this->new_id."\" date_create=\"".$DATE_CREATE."\">\n";
 236  
 237                  if (is_array($data["row"])) {
 238                      $this->objdata = array_merge(array($data["row"]), $this->objdata);
 239  
 240                      foreach($data["row"] as $key => $value) {
 241                          if($key!="IP" AND $key!="DATE_CREATE"  AND $key!="ID") {
 242                              if( ini_get("magic_quotes_gpc")!= "Off" ) $value = stripslashes($value);
 243                              $stream .= "\t\t<".$key."><![CDATA[".str_replace("’", "'", $value)."]]></" . $key . ">\n";
 244                          }
 245                      }
 246                  }
 247                  $stream .= "\t</row>\n";
 248              }
 249  
 250              if($data["post_action"] != "CHANGEROW" ) {
 251                  // Restoring old rows

 252                  if (is_array($objdata))
 253                  foreach($objdata as $inx => $fetch) {
 254                      if($data["post_action"] == "UPDATEROW" AND $fetch["ID"]==$data["row"]["ID"]) {
 255                          $this->objdata[$inx] = $fetch = $data["row"];
 256                      }
 257                      $stream .= "\t<row ip=\"".$fetch["IP"]."\" id=\"".$fetch["ID"]."\" date_create=\"".$fetch["DATE_CREATE"]."\">\n";
 258                      foreach($fetch as $key => $value) {
 259                          if($key!="IP" AND $key!="DATE_CREATE"  AND $key!="ID")
 260                          $stream .= "\t\t<".strtolower($key)."><![CDATA[".str_replace("’", "'", $value)."]]></" . strtolower($key) . ">\n";
 261                      }
 262                      $stream .= "\t</row>\n";
 263                  }
 264              }
 265              $stream .= "</data>";
 266  
 267              if($fp = @fopen($this->filename, "w+")) {
 268                  @fwrite($fp, $stream);
 269                  @fclose($fp);
 270                  @chmod($filename, 0666);
 271              } else message_die("Can not rewrite file ".$this->filename);
 272  
 273          }
 274          return true;
 275      }
 276  
 277  	function save_objdata($objdata){
 278          $stream="<data>\n";
 279          $IP = getenv("REMOTE_ADDR");
 280          $DATE_CREATE = date("Y-m-d H:i:s");
 281          if (is_array($objdata))
 282          foreach($objdata as $fetch) {
 283              $stream .= "\t<row ip=\"".($fetch["IP"]?$fetch["IP"]:$IP)."\" id=\"".$fetch["ID"]."\" date_create=\"".($fetch["DATE_CREATE"]?$fetch["DATE_CREATE"]:$DATE_CREATE)."\">\n";
 284              foreach($fetch as $key => $value) {
 285                  if( get_magic_quotes_gpc() ) $value = stripslashes($value);
 286                  if(strtolower($key)!="ip" AND strtolower($key)!="date_create" AND strtolower($key)!="id")
 287                  $stream .= "\t\t<".strtolower($key)."><![CDATA[".str_replace("’", "'", $value)."]]></" . strtolower($key) . ">\n";
 288              }
 289              $stream .= "\t</row>\n";
 290          }
 291          $stream .= "</data>";
 292          $fp = fopen($this->filename, "w+");
 293          $result=@fwrite($fp, $stream);
 294          @fclose($fp);
 295          @chmod($filename, 0666);
 296          return $result;
 297      }
 298  
 299  
 300  	function delete_data_row($arg_value, $arg_key="DATE_CREATE"){
 301          $objdata=$this->get_data();
 302  
 303          foreach ($objdata as $key=>$entry) {
 304              if ($entry[$arg_key]==$arg_value) {
 305                  unset($objdata[$key]);
 306                  break;
 307              }
 308          }
 309  
 310          $result=$this->save_objdata($objdata);
 311          return $result;
 312      }
 313  
 314  	function change_data_row($date_create, $new_row){
 315          $objdata=$this->get_data();
 316          foreach ($objdata as $key=>$entry) {
 317              if ($entry["DATE_CREATE"]==$date_create) {
 318                  $new_row["DATE_CREATE"]=date("Y-m-d H:i:s");
 319                  $new_row["IP"] = getenv("REMOTE_ADDR");
 320                  $objdata[$key]=$new_row;
 321                  break;
 322              }
 323          }
 324  
 325          $result=$this->save_objdata($objdata);
 326          return $result;
 327      }
 328  
 329  	function get_userdata($login, $password) {
 330          $data = $this->get_data($root_path."usr/xml/users.xml");
 331          if($data) {
 332              foreach($data as $row) {
 333                  if($row["LOGIN"]==$login AND $row["PASSWORD"]==$password AND $password!=""  AND $login!="") return $row;
 334              }
 335  
 336          } else return false;
 337          return false;
 338      }
 339  
 340  
 341  }
 342  
 343  
 344  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]