ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
inc.xml5compliance.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | Copyright (c) by Alexandre Alapetite, |
5  | http://alexandre.alapetite.net/cv/alexandre-alapetite.en.html |
6  | http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/ |
7  | Modifications by Alex Killing, alex.killing@gmx.de (search for ##) |
8  |-----------------------------------------------------------------------------|
9  | Allows PHP4/DOMXML scripts to run on PHP5/DOM |
10  | |
11  | Typical use: |
12  | { |
13  | if (version_compare(PHP_VERSION,'5','>=')) |
14  | require_once('domxml-php4-to-php5.php'); |
15  | } |
16  |-----------------------------------------------------------------------------|
17  | This code is published under Creative Commons |
18  | Attribution-ShareAlike 2.0 "BY-SA" licence. |
19  | See http://creativecommons.org/licenses/by-sa/2.0/ for details. |
20  +-----------------------------------------------------------------------------+
21 */
22 
23 function staticxmlerror($errno, $errstr, $errfile, $errline, $errcontext, $ret = false)
24 {
25  static $errs = array();
26 
27  if ($ret === true) {
28  return $errs;
29  }
30 
31  $tag = 'DOMDocument::validate(): ';
32  $errs[] = str_replace($tag, '', $errstr);
33 }
34 
36 {
37  return new php4DOMDocument($filename);
38 }
39 
40 define('DOMXML_LOAD_PARSING', 0);
41 
42 /*
43 * ##added
44 */
45 function domxml_open_mem($str, $mode = 0, &$error = null)
46 {
47  if (!is_int($mode)) {
48  $mode = 0;
49  }
50  $doc = new php4DOMDocument($str, false, $mode);
51  if (!$doc->success) {
52  $error = $doc->error;
53  }
54 
55  return $doc;
56 }
57 
58 function xpath_eval($xpath_context, $eval_str, $contextnode = null)
59 {
60  return $xpath_context->query($eval_str, $contextnode);
61 }
62 
63 function xpath_new_context($dom_document)
64 {
65  return new php4DOMXPath($dom_document);
66 }
67 
68 class php4DOMAttr extends php4DOMNode
69 {
70  public $myDOMAttr;
71 
72  public function __construct($aDOMAttr)
73  {
74  $this->myDOMAttr = $aDOMAttr;
75  }
76 
77  public function Name()
78  {
79  return $this->myDOMAttr->name;
80  }
81 
82  public function Specified()
83  {
84  return $this->myDOMAttr->specified;
85  }
86 
87  public function Value()
88  {
89  return $this->myDOMAttr->value;
90  }
91 }
92 
94 {
96 
97  public function __construct($aDOMCDATASection)
98  {
99  parent::php4DOMNode($aDOMCDATASection); // #added
100  $this->myDOMCDATASection = $aDOMCDATASection;
101  }
102 }
103 
105 {
107 
108  // ##altered
109  public function __construct($source, $file = true, $a_mode = 0)
110  {
111  $this->myDOMDocument = new DOMDocument();
112  // temporary set error handler
113  set_error_handler('staticxmlerror');
114  $old = ini_set('html_errors', false);
115 
116  if (is_object($source)) {
117  $this->myDOMDocument = $source;
118  $this->success = true;
119  } else {
120  if ($file) {
121  $this->success = @$this->myDOMDocument->load($source, $a_mode);
122  $this->success = @$this->myDOMDocument->load($source, $a_mode);
123  } else {
124  $this->success = $this->myDOMDocument->loadXML($source, $a_mode);
125  }
126  }
127 
128  // Restore error handling
129  ini_set('html_errors', $old);
130  restore_error_handler();
131 
132  if (!$this->success) {
133  $this->error_arr = staticxmlerror(null, null, null, null, null, true);
134  foreach ($this->error_arr as $error) {
135  $error = str_replace("DOMDocument::loadXML():", "", $error);
136  $this->error .= $error . "<br />";
137  }
138  }
139  }
140 
141  // ##added
142  public function xpath_init()
143  {
144  }
145 
146  public function free()
147  {
148  unset($this->myDOMDocument);
149  }
150 
151  // ##added
152  public function xpath_new_context()
153  {
154  return xpath_new_context($this);
155  }
156 
157  // ##added
158  public function dump_node($node)
159  {
160  $str = $this->myDOMDocument->saveXML($node->myDOMNode);
161  return $str;
162  }
163 
164  // ##added
165  public function validate(&$error)
166  {
167  $ok = $this->myDOMDocument->validate();
168 
169  if (!$ok) {
170  $error = array(array("0", "Unknown Error"));
171 
172  if (function_exists("libxml_get_last_error")) {
173  $err = libxml_get_last_error();
174 
175  if (is_object($err)) {
176  $error = array(array($err->code, $err->message));
177  }
178  }
179  }
180  return $error;
181  }
182 
183  public function create_attribute($name, $value)
184  {
185  $myAttr = $this->myDOMDocument->createAttribute($name);
186  $myAttr->value = $value;
187 
188  return new php4DOMAttr($myAttr);
189  }
190 
191  public function create_cdata_section($content)
192  {
193  return new php4DOMCDATASection($this->myDOMDocument->createCDATASection($content));
194  }
195 
196  public function create_comment($data)
197  {
198  return new php4DOMElement($this->myDOMDocument->createComment($data));
199  }
200 
201  public function create_element($name)
202  {
203  return new php4DOMElement($this->myDOMDocument->createElement($name));
204  }
205 
206  public function create_text_node($content)
207  {
208  return new php4DOMNode($this->myDOMDocument->createTextNode($content));
209  }
210 
211  public function document_element()
212  {
213  return new php4DOMElement($this->myDOMDocument->documentElement);
214  }
215 
216  public function dump_file($filename, $compressionmode = false, $format = false)
217  {
218  return $this->myDOMDocument->save($filename);
219  }
220 
221  public function dump_mem($format = false, $encoding = false)
222  {
223  $r = $this->myDOMDocument->saveXML();
224  return $r;
225  }
226 
228  {
229  $myDOMNodeList = $this->myDOMDocument->getElementsByTagName($name);
230  $nodeSet = array();
231  $i = 0;
232  while ($node = $myDOMNodeList->item($i)) {
233  $nodeSet[] = new php4DOMElement($node);
234  $i++;
235  }
236 
237  return $nodeSet;
238  }
239 
240  public function html_dump_mem()
241  {
242  return $this->myDOMDocument->saveHTML();
243  }
244 }
245 
250 {
251  public function get_attribute($name)
252  {
253  return $this->myDOMNode->getAttribute($name);
254  }
255 
256  public function owner_document()
257  {
258  return new php4DOMDocument($this->myDOMNode->ownerDocument);
259  }
260 
262  {
263  $myDOMNodeList = $this->myDOMNode->getElementsByTagName($name);
264  $nodeSet = array();
265  $i = 0;
266  while ($node = $myDOMNodeList->item($i)) {
267  $nodeSet[] = new php4DOMElement($node);
268  $i++;
269  }
270 
271  return $nodeSet;
272  }
273 
274  public function has_attribute($name)
275  {
276  return $this->myDOMNode->hasAttribute($name);
277  }
278 
279  public function remove_attribute($name)
280  {
281  return $this->myDOMNode->removeAttribute($name);
282  }
283 
284  public function set_attribute($name, $value)
285  {
286  return $this->myDOMNode->setAttribute($name, $value);
287  }
288 
289  public function tagname()
290  {
291  return $this->myDOMNode->tagName;
292  }
293 
294  // ##added
295  public function set_content($text)
296  {
297  // the following replace has been added to conform with PHP4.
298  // A set_content("&amp;") brought a get_content() = "&" there,
299  // whereas PHP5 gives a get_content() = "&amp;"
300  $text = str_replace("&lt;", "<", $text);
301  $text = str_replace("&gt;", ">", $text);
302  $text = str_replace("&amp;", "&", $text);
303 
304  $text_node = new DOMText();
305  $text_node->appendData($text);
306  if (is_object($this->myDOMNode->firstChild)) {
307  $this->myDOMNode->replaceChild($text_node, $this->myDOMNode->firstChild);
308  } else {
309  $this->myDOMNode->appendChild($text_node);
310  }
311  }
312 
313  // ##added
314  public function get_content()
315  {
316  $text_node = $this->myDOMNode->firstChild;
317 
318  if (is_object($text_node)) {
319  return $text_node->textContent;
320  } else {
321  return "";
322  }
323  }
324 
325  // ## added
326  public function unlink($aDomNode)
327  {
328  parent::unlink_node($aDomNode);
329  }
330 }
331 
336 {
337  public $myDOMNode;
338 
339  public function __construct($aDomNode)
340  {
341  $this->myDOMNode = $aDomNode;
342  }
343 
344  public function append_child($newnode)
345  {
346  //echo "BH";
347  //if (strtolower(get_class($newnode)) != "php4domcdatasection")
348  //{
349  $doc = $this->myDOMNode->ownerDocument;
350  //echo "<br>BH1:".get_class($newnode).":";
351  $newnode->myDOMNode = $doc->importNode($newnode->myDOMNode, true);
352  //echo "BH2";
353  return new php4DOMElement($this->myDOMNode->appendChild($newnode->myDOMNode));
354  //}
355  //else
356  //{
357  //}
358  }
359 
360  public function replace_node($newnode)
361  {
362  return $this->set_content($newnode->myDOMNode->textContent);
363  }
364 
365  public function append_sibling($newnode)
366  {
367  return new php4DOMElement($this->myDOMNode->parentNode->appendChild($newnode->myDOMNode));
368  }
369 
370  public function attributes()
371  {
372  //echo "<br>node:".$this->myDOMNode->nodeName.":";
373  $myDOMNodeList = $this->myDOMNode->attributes;
374  $nodeSet = array();
375  $i = 0;
376  if (is_object($myDOMNodeList)) {
377  while ($node = $myDOMNodeList->item($i)) {
378  $nodeSet[] = new php4DOMAttr($node);
379  $i++;
380  }
381  }
382 
383  return $nodeSet;
384  }
385 
386  public function child_nodes()
387  {
388  $myDOMNodeList = $this->myDOMNode->childNodes;
389  $nodeSet = array();
390  $i = 0;
391  while ($node = $myDOMNodeList->item($i)) {
392  $nodeSet[] = new php4DOMElement($node);
393  $i++;
394  }
395  return $nodeSet;
396  }
397 
398  // ## added
399  public function children()
400  {
401  //echo "<br>php4DomNode::children"; flush();
402  return $this->child_nodes();
403  }
404 
405  // ## added
406  public function unlink_node($aDomNode = "")
407  {
408  // sometimes the node to unlink is passed
409  if (!is_object($aDomNode)) {
410  $aDomNode = $this;
411  //$aDomNode = $this;
412  }
413 
414  $parent = $aDomNode->myDOMNode->parentNode;
415  if (is_object($parent)) {
416  $parent->removeChild($aDomNode->myDOMNode);
417  }
418  }
419 
420  public function clone_node($deep = false)
421  {
422  return new php4DOMElement($this->myDOMNode->cloneNode($deep));
423  }
424 
425  public function first_child()
426  {
427  return new php4DOMElement($this->myDOMNode->firstChild);
428  }
429 
430  public function get_content()
431  {
432  return $this->myDOMNode->textContent;
433  }
434 
435  public function has_attributes()
436  {
437  return $this->myDOMNode->hasAttributes();
438  }
439 
440  public function has_child_nodes()
441  {
442  return $this->myDOMNode->hasChildNodes();
443  }
444 
445  // ## changed
446  public function insert_before($newnode, $refnode)
447  {
448  //echo "BH";
449  $doc = $this->myDOMNode->ownerDocument;
450  $newnode->myDOMNode = $doc->importNode($newnode->myDOMNode, true);
451 
452  $mydomnode = $this->myDOMNode;
453  $mynewnode = $newnode->myDOMNode;
454  $myrefnode = $refnode->myDOMNode;
455  try {
456  $domel = $mydomnode->insertBefore($mynewnode, $myrefnode);
457  } catch (DOMException $exception) {
458  // php 4 accepted $this == $refnode -> switch to parent of $this
459  $mydomnode = $this->myDOMNode->parentNode;
460  $domel = $mydomnode->insertBefore($mynewnode, $myrefnode);
461  }
462  $el = new php4DOMElement($domel);
463  return $el;
464  }
465 
466  // ## changed
467  public function last_child()
468  {
469  $last = $this->myDOMNode->lastChild;
470 
471  if (is_object($last)) {
472  return new php4DOMElement($last);
473  } else {
474  return false;
475  }
476  }
477 
478  // ## changed
479  public function next_sibling()
480  {
481  $next = $this->myDOMNode->nextSibling;
482 
483  if (is_object($next)) {
484  return new php4DOMElement($next);
485  } else {
486  return false;
487  }
488  }
489 
490  public function node_name($a_local = false)
491  {
492  if ($a_local) {
493  return $this->myDOMNode->localName;
494  } else {
495  return $this->myDOMNode->nodeName;
496  }
497  }
498 
499  public function node_type()
500  {
501  return $this->myDOMNode->nodeType;
502  }
503 
504  public function node_value()
505  {
506  return $this->myDOMNode->nodeValue;
507  }
508 
509  // ## changed
510  public function parent_node()
511  {
512  $parent = $this->myDOMNode->parentNode;
513 
514  if (is_object($parent)) {
515  return new php4DOMElement($parent);
516  } else {
517  return false;
518  }
519  }
520 
521  // ## changed
522  public function previous_sibling()
523  {
524  $prev = $this->myDOMNode->previousSibling;
525 
526  if (is_object($prev)) {
527  return new php4DOMElement($prev);
528  } else {
529  return false;
530  }
531  }
532 
533  public function remove_child($oldchild)
534  {
535  return new php4DOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode));
536  }
537 
538  public function replace_child($oldnode, $newnode)
539  {
540  return new php4DOMElement($this->myDOMNode->replaceChild($oldchild->myDOMNode, $newnode->myDOMNode));
541  }
542 
543  public function set_content($text)
544  {
545  $this->myDOMNode->textContent = $text;
546  return $this->myDOMNode->textContent;
547  }
548 }
549 
551 {
553  public $nodeset;
554 
555  public function __construct($aDOMNodelist)
556  {
557  $this->myDOMNodelist = $aDOMNodelist;
558  $this->nodeset = array();
559  $i = 0;
560  while ($node = $this->myDOMNodelist->item($i)) {
561  $this->nodeset[] = new php4DOMElement($node);
562  $i++;
563  }
564  }
565 }
566 
568 {
569  public $myDOMXPath;
570 
571  // ## added
572  public function xpath_eval($eval_str)
573  {
574  return xpath_eval($this, $eval_str);
575  }
576 
577  public function __construct($dom_document)
578  {
579  $this->myDOMXPath = new DOMXPath($dom_document->myDOMDocument);
580  }
581 
582  public function query($eval_str)
583  {
584  return new php4DOMNodelist($this->myDOMXPath->query($eval_str));
585  }
586 
587  public function xpath_register_ns($prefix, $namespaceURI)
588  {
589  return $this->myDOMXPath->registerNamespace($prefix, $namespaceURI);
590  }
591 }
php4DOMAttr($aDOMAttr)
xpath_register_ns($prefix, $namespaceURI)
append_child($newnode)
$format
Definition: metadata.php:141
set_attribute($name, $value)
xpath_new_context($dom_document)
__construct($aDOMCDATASection)
success()
Definition: success.php:2
domxml_open_mem($str, $mode=0, &$error=null)
xpath_eval($xpath_context, $eval_str, $contextnode=null)
dump_mem($format=false, $encoding=false)
__construct($dom_document)
staticxmlerror($errno, $errstr, $errfile, $errline, $errcontext, $ret=false)
dump_file($filename, $compressionmode=false, $format=false)
clone_node($deep=false)
__construct($aDOMAttr)
create_cdata_section($content)
$r
Definition: example_031.php:79
php4DomElement
$text
Definition: errorreport.php:18
php4DOMNode($aDomNode)
__construct($aDOMNodelist)
replace_node($newnode)
insert_before($newnode, $refnode)
unlink_node($aDomNode="")
create_attribute($name, $value)
$filename
Definition: buildRTE.php:89
node_name($a_local=false)
remove_child($oldchild)
$ret
Definition: parser.php:6
__construct($source, $file=true, $a_mode=0)
$i
Definition: disco.tpl.php:19
append_sibling($newnode)
$source
Definition: linkback.php:22
replace_child($oldnode, $newnode)
__construct($aDomNode)
if(function_exists('posix_getuid') &&posix_getuid()===0) if(!array_key_exists('t', $options)) $tag
Definition: cron.php:35
xpath_eval($eval_str)
$data
Definition: bench.php:6
domxml_open_file($filename)