| [ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /********************************************************************\ 3 * eGroupWare SiteMgr - Web Content Management * 4 * http://www.egroupware.org * 5 * -------------------------------------------- * 6 * eGroupWare API - XML tools * 7 * Written by Dan Kuykendall <seek3r@phpgroupware.org> * 8 * and Bettina Gille [ceb@phpgroupware.org] * 9 * and Ralf Becker <ralfbecker@outdoortraining.de> * 10 * Michael Totschnig: changed function export_var, so that 11 * Copyright (C) 2002 Dan Kuykendall, Bettina Gille, Ralf Becker * 12 * ----------------------------------------------------------------- * 13 * This library is part of the eGroupWare API * 14 * ----------------------------------------------------------------- * 15 * This library is free software; you can redistribute it and/or * 16 * modify it under the terms of the GNU General Public License as * 17 * published by the Free Software Foundation; either version 2 of * 18 * the License, or (at your option) any later version. * 19 * * 20 * This program is distributed in the hope that it will be useful, * 21 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 23 * General Public License for more details. * 24 * * 25 * You should have received a copy of the GNU General Public License * 26 * along with this program; if not, write to the Free Software * 27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * 28 \*******************************************************************/ 29 30 /* $Id: class.xmltool2.inc.php 20295 2006-02-15 12:31:25Z $ */ 31 32 // Michael Totschnig: this is a variant of xmltool where export_var returns subelements 33 // in a subarray, even if there is only one of them 34 // for example <dir><app>myapp</app></dir> is exported to 35 // array('dir' => array('app' => array('myapp')) 36 // instead of 37 // array('dir' => array('app' => 'myapp') 38 // this helps interpreting the data returned 39 40 function var2xml($name, $data) 41 { 42 $doc =& new xmltool2('root','',''); 43 return $doc->import_var($name,$data,True,True); 44 } 45 46 class xmltool2 47 { 48 /* for root nodes */ 49 var $xmlversion = '1.0'; 50 var $doctype = Array(); 51 /* shared */ 52 var $node_type = ''; 53 var $name = ''; 54 var $data_type; 55 var $data; 56 /* for nodes */ 57 var $attributes = Array(); 58 var $comments = Array(); 59 var $indentstring = "\t"; 60 61 /* start the class as either a root or a node */ 62 function xmltool2 ($node_type = 'root', $name='',$indentstring="\t") 63 { 64 $this->node_type = $node_type; 65 $this->indentstring = $indentstring; 66 if ($this->node_type == 'node') 67 { 68 if($name !== '') 69 { 70 $this->name = $name; 71 } 72 else 73 { 74 echo 'You must name node type objects<br>'; 75 exit; 76 } 77 } 78 } 79 80 function set_version ($version = '1.0') 81 { 82 $this->xmlversion = $version; 83 return True; 84 } 85 86 function set_doctype ($name, $uri = '') 87 { 88 if ($this->node_type == 'root') 89 { 90 $this->doctype[$name] = $uri; 91 return True; 92 } 93 else 94 { 95 return False; 96 } 97 } 98 99 function add_node ($node_object, $name = '') 100 { 101 switch ($this->node_type) 102 { 103 case 'root': 104 if (is_object($node_object)) 105 { 106 $this->data = $node_object; 107 } 108 else 109 { 110 $this->data = $this->import_var($name, $node_object); 111 } 112 break; 113 case 'node': 114 if(!is_array($this->data)) 115 { 116 $this->data = Array(); 117 $this->data_type = 'node'; 118 } 119 if (is_object($node_object)) 120 { 121 if ($name != '') 122 { 123 $this->data[$name] = $node_object; 124 } 125 else 126 { 127 $this->data[] = $node_object; 128 } 129 } 130 else 131 { 132 $this->data[$name] = $this->import_var($name, $node_object); 133 } 134 return True; 135 break; 136 } 137 } 138 139 function get_node ($name = '') // what is that function doing: NOTHING !!! 140 { 141 switch ($this->data_type) 142 { 143 case 'root': 144 break; 145 case 'node': 146 break; 147 case 'object': 148 break; 149 } 150 151 } 152 153 function set_value ($string) 154 { 155 $this->data = $string; 156 $this->data_type = 'value'; 157 return True; 158 } 159 160 function get_value () 161 { 162 if($this->data_type == 'value') 163 { 164 return $this->data; 165 } 166 else 167 { 168 return False; 169 } 170 } 171 172 function set_attribute ($name, $value = '') 173 { 174 $this->attributes[$name] = $value; 175 return True; 176 } 177 178 function get_attribute ($name) 179 { 180 return $this->attributes[$name]; 181 } 182 183 function get_attributes () 184 { 185 return $this->attributes; 186 } 187 188 function add_comment ($comment) 189 { 190 $this->comments[] = $comment; 191 return True; 192 } 193 194 function import_var($name, $value,$is_root=False,$export_xml=False) 195 { 196 //echo "<p>import_var: this->indentstring='$this->indentstring'</p>\n"; 197 $node =& new xmltool2('node',$name,$this->indentstring); 198 switch (gettype($value)) 199 { 200 case 'string': 201 case 'integer': 202 case 'double': 203 case 'NULL': 204 $node->set_value($value); 205 break; 206 case 'boolean': 207 if($value == True) 208 { 209 $node->set_value('1'); 210 } 211 else 212 { 213 $node->set_value('0'); 214 } 215 break; 216 case 'array': 217 $new_index = False; 218 while (list ($idxkey, $idxval) = each ($value)) 219 { 220 if(is_array($idxval)) 221 { 222 while (list ($k, $i) = each ($idxval)) 223 { 224 if (is_int($k)) 225 { 226 $new_index = True; 227 } 228 } 229 } 230 } 231 reset($value); 232 while (list ($key, $val) = each ($value)) 233 { 234 if($new_index) 235 { 236 $keyname = $name; 237 $nextkey = $key; 238 } 239 else 240 { 241 $keyname = $key; 242 $nextkey = $key; 243 } 244 switch (gettype($val)) 245 { 246 case 'string': 247 case 'integer': 248 case 'double': 249 case 'NULL': 250 $subnode =& new xmltool2('node', $nextkey,$this->indentstring); 251 $subnode->set_value($val); 252 $node->add_node($subnode); 253 break; 254 case 'boolean': 255 $subnode =& new xmltool2('node', $nextkey,$this->indentstring); 256 if($val == True) 257 { 258 $subnode->set_value('1'); 259 } 260 else 261 { 262 $subnode->set_value('0'); 263 } 264 $node->add_node($subnode); 265 break; 266 case 'array': 267 list($first_key) = each($val); reset($val); 268 if($new_index && is_int($first_key)) 269 { 270 while (list ($subkey, $subval) = each ($val)) 271 { 272 $node->add_node($this->import_var($nextkey, $subval)); 273 } 274 } 275 else 276 { 277 $subnode = $this->import_var($nextkey, $val); 278 $node->add_node($subnode); 279 } 280 break; 281 case 'object': 282 $subnode =& new xmltool2('node', $nextkey,$this->indentstring); 283 $subnode->set_value('PHP_SERIALIZED_OBJECT&:'.serialize($val)); 284 $node->add_node($subnode); 285 break; 286 case 'resource': 287 echo 'Halt: Cannot package PHP resource pointers into XML<br>'; 288 exit; 289 default: 290 echo 'Halt: Invalid or unknown data type<br>'; 291 exit; 292 } 293 294 } 295 break; 296 case 'object': 297 $node->set_value('PHP_SERIALIZED_OBJECT&:'.serialize($value)); 298 break; 299 case 'resource': 300 echo 'Halt: Cannot package PHP resource pointers into XML<br>'; 301 exit; 302 default: 303 echo 'Halt: Invalid or unknown data type<br>'; 304 exit; 305 } 306 307 if($is_root) 308 { 309 $this->add_node($node); 310 if($export_xml) 311 { 312 $xml = $this->export_xml(); 313 return $xml; 314 } 315 else 316 { 317 return True; 318 } 319 } 320 else 321 { 322 $this->add_node($node); 323 return $node; 324 } 325 } 326 327 function export_struct() 328 { 329 if($this->node_type == 'root') 330 { 331 return $this->data->export_struct(); 332 } 333 334 $retval['tag'] = $this->name; 335 $retval['attributes'] = $this->attributes; 336 if($this->data_type != 'node') 337 { 338 $found_at = strstr($this->data,'PHP_SERIALIZED_OBJECT&:'); 339 if($found_at != False) 340 { 341 $retval['value'] = unserialize(str_replace ('PHP_SERIALIZED_OBJECT&:', '', $this->data)); 342 } 343 else 344 { 345 $retval['value'] = $this->data; 346 } 347 return $retval; 348 } 349 else 350 { 351 reset($this->data); 352 while(list($key,$val) = each($this->data)) 353 { 354 $retval['children'][] = $val->export_struct(); 355 } 356 return $retval; 357 } 358 } 359 360 361 function import_xml_children($data, &$i, $parent_node) 362 { 363 while (++$i < count($data)) 364 { 365 switch ($data[$i]['type']) 366 { 367 case 'cdata': 368 case 'complete': 369 $node =& new xmltool2('node',$data[$i]['tag'],$this->indentstring); 370 if(is_array($data[$i]['attributes']) && count($data[$i]['attributes']) > 0) 371 { 372 while(list($k,$v)=each($data[$i]['attributes'])) 373 { 374 $node->set_attribute($k,$v); 375 } 376 } 377 $node->set_value($data[$i]['value']); 378 $parent_node->add_node($node); 379 break; 380 case 'open': 381 $node =& new xmltool2('node',$data[$i]['tag'],$this->indentstring); 382 if(is_array($data[$i]['attributes']) && count($data[$i]['attributes']) > 0) 383 { 384 while(list($k,$v)=each($data[$i]['attributes'])) 385 { 386 $node->set_attribute($k,$v); 387 } 388 } 389 390 $node = $this->import_xml_children($data, $i, $node); 391 $parent_node->add_node($node); 392 break; 393 case 'close': 394 return $parent_node; 395 } 396 } 397 } 398 399 function import_xml($xmldata) 400 { 401 $parser = xml_parser_create(); 402 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 403 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 404 xml_parse_into_struct($parser, $xmldata, $vals, $index); 405 xml_parser_free($parser); 406 unset($index); 407 $node =& new xmltool2('node',$vals[0]['tag'],$this->indentstring); 408 if(isset($vals[0]['attributes'])) 409 { 410 while(list($key,$value) = each($vals[0]['attributes'])) 411 { 412 $node->set_attribute($key, $value); 413 } 414 } 415 switch ($vals[0]['type']) 416 { 417 case 'complete': 418 $node->set_value($vals[0]['value']); 419 break; 420 case 'cdata': 421 $node->set_value($vals[0]['value']); 422 break; 423 case 'open': 424 $node = $this->import_xml_children($vals, $i = 0, $node); 425 break; 426 case 'closed': 427 exit; 428 } 429 $this->add_node($node); 430 } 431 432 433 function export_var() 434 { 435 if($this->node_type == 'root') 436 { 437 return $this->data->export_var(); 438 } 439 440 if($this->data_type != 'node') 441 { 442 $found_at = strstr($this->data,'PHP_SERIALIZED_OBJECT&:'); 443 if($found_at != False) 444 { 445 return unserialize(str_replace ('PHP_SERIALIZED_OBJECT&:', '', $this->data)); 446 } 447 return $this->data; 448 } 449 else 450 { 451 reset($this->data); 452 while(list($key,$val) = each($this->data)) 453 { 454 $return_array[$val->name][] = $val->export_var(); 455 } 456 return $return_array; 457 } 458 } 459 460 function export_xml($indent = 1) 461 { 462 if ($this->node_type == 'root') 463 { 464 $result = '<?xml version="'.$this->xmlversion.'" encoding="' . lang('charset') . '"?>'."\n"; 465 if(count($this->doctype) == 1) 466 { 467 list($doctype_name,$doctype_uri) = each($this->doctype); 468 $result .= '<!DOCTYPE '.$doctype_name.' SYSTEM "'.$doctype_uri.'">'."\n"; 469 } 470 if(count($this->comments) > 0 ) 471 { 472 //reset($this->comments); 473 while(list($key,$val) = each ($this->comments)) 474 { 475 $result .= "<!-- $val -->\n"; 476 } 477 } 478 if(is_object($this->data)) 479 { 480 $indent = 0; 481 $result .= $this->data->export_xml($indent); 482 } 483 return $result; 484 } 485 else /* For node objects */ 486 { 487 for ($i = 0; $i < $indent; $i++) 488 { 489 $indentstring .= $this->indentstring; 490 } 491 492 $result = $indentstring.'<'.$this->name; 493 if(count($this->attributes) > 0 ) 494 { 495 reset($this->attributes); 496 while(list($key,$val) = each ($this->attributes)) 497 { 498 $result .= ' '.$key.'="'.htmlspecialchars($val).'"'; 499 } 500 } 501 502 $endtag_indent = $indentstring; 503 if (empty($this->data_type)) 504 { 505 $result .= '/>'."\n"; 506 } 507 else 508 { 509 $result .= '>'; 510 511 switch ($this->data_type) 512 { 513 case 'value': 514 if(is_array($this->data)) 515 { 516 $type_error = True; 517 break; 518 } 519 520 /*if(preg_match("(&|<)", $this->data)) // this is unnecessary with htmlspecialchars($this->data) 521 { 522 $result .= '<![CDATA['.$this->data.']]>'; 523 $endtag_indent = ''; 524 } 525 else*/if(strlen($this->data) > 30 && !empty($this->indentstring)) 526 { 527 $result .= "\n".$indentstring.$this->indentstring.htmlspecialchars($this->data)."\n"; 528 $endtag_indent = $indentstring; 529 } 530 else 531 { 532 $result .= htmlspecialchars($this->data); 533 $endtag_indent = ''; 534 } 535 break; 536 case 'node': 537 $result .= "\n"; 538 if(!is_array($this->data)) 539 { 540 $type_error = True; 541 break; 542 } 543 544 $subindent = $indent+1; 545 reset($this->data); 546 while(list($key,$val) = each ($this->data)) 547 { 548 if(is_object($val)) 549 { 550 $result .= $val->export_xml($subindent); 551 } 552 } 553 break; 554 default: 555 if($this->data != '') 556 { 557 echo 'Invalid or unset data type ('.$this->data_type.'). This should not be possible if using the class as intended<br>'; 558 } 559 } 560 561 if ($type_error) 562 { 563 echo 'Invalid data type. Tagged as '.$this->data_type.' but data is '.gettype($this->data).'<br>'; 564 } 565 566 $result .= $endtag_indent.'</'.$this->name.'>'; 567 if($indent != 0) 568 { 569 $result .= "\n"; 570 } 571 } 572 if(count($this->comments) > 0 ) 573 { 574 reset($this->comments); 575 while(list($key,$val) = each ($this->comments)) 576 { 577 $result .= $endtag_indent."<!-- $val -->\n"; 578 } 579 } 580 return $result; 581 } 582 } 583 } 584 585 class xmlnode extends xmltool2 586 { 587 function xmlnode($name) 588 { 589 $this->xmltool2('node',$name); 590 } 591 } 592 593 class xmldoc extends xmltool2 594 { 595 function xmldoc($version = '1.0') 596 { 597 $this->xmltool2('root'); 598 $this->set_version($version); 599 } 600 601 function add_root($root_node) 602 { 603 return $this->add_node($root_node); 604 } 605 } 606 607 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |