[ 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/ -> tree.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: 08.09.04 (dd/mm/yy)

  21  // $Id: tree.lib.php,v 1.2 2005/06/14 14:23:48 mephius Exp $

  22  
  23  
  24  
  25  
  26  class tree {
  27      var $pointer;
  28      var $prev_pointer;
  29      var $last_var;
  30      var $structure;
  31      var $xml_file;
  32      var $xml;
  33      var $element;
  34      var $element_count;
  35      var $default_pointer;
  36      var $map;
  37      var $depth;
  38      var $default_pointer_var;
  39      var $default_pointer_id;
  40  
  41  
  42      /**

  43      * @return tree

  44      * @param string $filename

  45      * @desc tree class constructor

  46      */
  47  	function tree($filename){
  48          $this->pointer="";
  49          $this->element="";
  50          $this->element_count=0;
  51          $this->structure=array();
  52          $this->map=array();
  53          $this->xml_file=$filename;
  54      }
  55  
  56  
  57      /**

  58      * @return void

  59      * @param XML Parser resourse ID $parser

  60      * @param string $name

  61      * @param string $attrs

  62      * @desc XML Parser start element handler

  63      */
  64  	function startElement($parser, $name, $attrs)
  65      {
  66          $this->depth++;
  67          $this->element=$name;
  68          if ($name=="TREEITEM") {
  69              $this->element_count++;
  70              $this->prev_pointer=$this->pointer;
  71              if ($attrs["VAR"]) {
  72                  $this->pointer.="['" . $attrs["VAR"] . "']";
  73                  $this->last_var=$attrs["VAR"];
  74              }else {
  75                  $this->pointer.="['" . $name . "']";
  76                  $this->last_var=$name;
  77              }
  78              $this->element_count=(str_replace("id_", "", $attrs["ID"])?str_replace("id_", "", $attrs["ID"]):0);
  79              $this->map[$this->element_count]["ID"]=$this->element_count;
  80              $tmp=str_replace(array("['root", "']['"), array("", "/"), $this->pointer);
  81              $this->map[$this->element_count]["POINTER"]=$this->pointer;
  82              $this->map[$this->element_count]["HREF"]=str_replace("']", "/", $tmp);
  83              $this->map[$this->element_count]["VAR"]=$attrs["VAR"];
  84              $this->map[$this->element_count]["OWNER"]=$attrs["OWNER"];
  85              $this->map[$this->element_count]["GROUP"]=$attrs["GROUP"];
  86              $this->map[$this->element_count]["PERMISSIONS"]=$attrs["PERMISSIONS"];
  87              @eval("\$this->structure" . $this->prev_pointer . "[\"CHILDREN\"][\"".$this->last_var."\"]=" . $this->map[$this->element_count]["ID"] . ";");
  88              @eval("\$this->structure" . $this->pointer . "[\"ID\"]=" . $this->map[$this->element_count]["ID"] . ";");
  89  
  90  
  91              $this->map[$this->element_count]["LEVEL"]=$this->depth-2;
  92              if ($this->map[$this->element_count]["LEVEL"]==1 and !$this->default_pointer) {
  93                  $this->default_pointer=$this->pointer;
  94                  $this->default_pointer_var=$attrs["VAR"];
  95                  $this->default_pointer_id=$this->element_count;
  96              }
  97          }
  98      }
  99  
 100      /**

 101      * @return void

 102      * @param XML Parser resourse ID $parser

 103      * @param string $name

 104      * @desc XML Parser end element handler

 105      */
 106  	function endElement($parser, $name)
 107      {
 108          $this->depth--;
 109          if ($name=="TREEITEM") {
 110              $this->pointer=preg_replace("/(.*)(\['(.*)'\])$/", "\\1", $this->pointer);
 111          }
 112      }
 113  
 114      /**

 115      * @return void

 116      * @param XML Parser resourse ID $parser

 117      * @param string $data

 118      * @desc XML Parser data handler

 119      */
 120  	function charData($parser, $data)
 121      {
 122          if ($this->element!="TREEITEM" and trim($data)!="") {
 123              $this->map[$this->element_count][$this->element]=iconv("UTF-8", $GLOBALS["default_charset"], $data);
 124              //            if($this->element=="ID") {

 125              //                @eval("\$this->structure" . $this->prev_pointer . "[\"CHILDREN\"][".$this->last_var."]=" . intval($data) . ";");

 126              //            }

 127          }
 128          if (addslashes(trim($data))!="") eval("\$this->structure".$this->pointer."[\"".$this->element."\"]=\"".addslashes(iconv("UTF-8", $GLOBALS["default_charset"], trim($data)))."\";");
 129      }
 130  
 131      /**

 132      * @return array

 133      * @desc Build tree from xml

 134      */
 135  	function build_structure(){
 136          $fp=@fopen($this->xml_file, "r+");
 137          if (!$fp) message_die("Couldn't open <b>" . $this->xml_file . "</b> for reading!");
 138          $size=filesize($this->xml_file);
 139          $this->xml=fread($fp, $size);
 140          fclose($fp);
 141  
 142          $xml_parser = xml_parser_create();
 143          xml_set_element_handler($xml_parser, array(&$this, "startElement"), array(&$this, "endElement"));
 144          xml_set_character_data_handler($xml_parser, array(&$this, "charData"));
 145          if (!xml_parse($xml_parser, $this->xml)) {
 146              message_die(sprintf("XML error: %s at line %d",
 147              xml_error_string(xml_get_error_code($xml_parser)),
 148              xml_get_current_line_number($xml_parser)));
 149          }
 150          xml_parser_free($xml_parser);
 151          return $this->structure;
 152      }
 153  
 154  	function array2xml($tree, $depth=1){
 155          $xml="";
 156          $spacer="";
 157          for ($i=0; $i<$depth; $i++) $spacer.="\t";
 158          foreach ($tree as $key=>$value) {
 159              if (gettype($value)=="array" and $key!="CHILDREN") {
 160                  $depth++;
 161                  $xml.=$spacer . "<treeitem var=\"".$key."\" id=\"id_".$value["ID"]."\">\n" . $this->array2xml($value, $depth) . $spacer . "</treeitem>\n";
 162                  $depth--;
 163              }
 164              elseif($key!="VAR" and $value!="" and $key!="ID") $xml.=$spacer . "<".strtolower($key).">".$value."</".strtolower($key).">\n";
 165          }
 166          return $xml;
 167  
 168      }
 169  
 170  }
 171  
 172  
 173  // If function iconv doesn't exist, provide a simple replacement using utf8.class

 174  if(!function_exists("iconv")){
 175      require_once (ROOT_PATH . "usr/system/utf/utf8.class.php");
 176  
 177  	function iconv($in_charset, $out_charset, $string){
 178          global $conv;
 179          $in_charset=preg_replace("/WINDOWS(-)?/i", "CP", strtoupper($in_charset));
 180          $out_charset=preg_replace("/WINDOWS(-)?/i", "CP", strtoupper($out_charset));
 181  
 182          if($in_charset!="UTF-8"){
 183              if(!$conv[$in_charset]) $conv[$in_charset] = new utf8($in_charset);
 184              return  $conv[$in_charset]->strToUtf8($string);
 185          }else{
 186              if(!$conv[$out_charset]) $conv[$out_charset] = new utf8($out_charset);
 187              return  $conv[$out_charset]->utf8ToStr($string);
 188          }
 189      }
 190  }
 191  
 192  
 193  ?>


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