ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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, bool $throw = false)
166  {
167  try {
168  $ok = $this->myDOMDocument->validate();
169  } catch (Exception $e) {
170  if ($throw) {
171  throw $e;
172  }
173  }
174  if (!$ok) {
175  $error = array(array("0", "Unknown Error"));
176 
177  if (function_exists("libxml_get_last_error")) {
178  $err = libxml_get_last_error();
179 
180  if (is_object($err)) {
181  $error = array(array($err->code, $err->message));
182  }
183  }
184  }
185  return $error;
186  }
187 
188  public function create_attribute($name, $value)
189  {
190  $myAttr = $this->myDOMDocument->createAttribute($name);
191  $myAttr->value = $value;
192 
193  return new php4DOMAttr($myAttr);
194  }
195 
196  public function create_cdata_section($content)
197  {
198  return new php4DOMCDATASection($this->myDOMDocument->createCDATASection($content));
199  }
200 
201  public function create_comment($data)
202  {
203  return new php4DOMElement($this->myDOMDocument->createComment($data));
204  }
205 
206  public function create_element($name)
207  {
208  return new php4DOMElement($this->myDOMDocument->createElement($name));
209  }
210 
211  public function create_text_node($content)
212  {
213  return new php4DOMNode($this->myDOMDocument->createTextNode($content));
214  }
215 
216  public function document_element()
217  {
218  return new php4DOMElement($this->myDOMDocument->documentElement);
219  }
220 
221  public function dump_file($filename, $compressionmode = false, $format = false)
222  {
223  return $this->myDOMDocument->save($filename);
224  }
225 
226  public function dump_mem($format = false, $encoding = false)
227  {
228  $r = $this->myDOMDocument->saveXML();
229  return $r;
230  }
231 
233  {
234  $myDOMNodeList = $this->myDOMDocument->getElementsByTagName($name);
235  $nodeSet = array();
236  $i = 0;
237  while ($node = $myDOMNodeList->item($i)) {
238  $nodeSet[] = new php4DOMElement($node);
239  $i++;
240  }
241 
242  return $nodeSet;
243  }
244 
245  public function html_dump_mem()
246  {
247  return $this->myDOMDocument->saveHTML();
248  }
249 }
250 
255 {
256  public function get_attribute($name)
257  {
258  return $this->myDOMNode->getAttribute($name);
259  }
260 
261  public function owner_document()
262  {
263  return new php4DOMDocument($this->myDOMNode->ownerDocument);
264  }
265 
267  {
268  $myDOMNodeList = $this->myDOMNode->getElementsByTagName($name);
269  $nodeSet = array();
270  $i = 0;
271  while ($node = $myDOMNodeList->item($i)) {
272  $nodeSet[] = new php4DOMElement($node);
273  $i++;
274  }
275 
276  return $nodeSet;
277  }
278 
279  public function has_attribute($name)
280  {
281  return $this->myDOMNode->hasAttribute($name);
282  }
283 
284  public function remove_attribute($name)
285  {
286  return $this->myDOMNode->removeAttribute($name);
287  }
288 
289  public function set_attribute($name, $value)
290  {
291  return $this->myDOMNode->setAttribute($name, $value);
292  }
293 
294  public function tagname()
295  {
296  return $this->myDOMNode->tagName;
297  }
298 
299  // ##added
300  public function set_content($text)
301  {
302  // the following replace has been added to conform with PHP4.
303  // A set_content("&amp;") brought a get_content() = "&" there,
304  // whereas PHP5 gives a get_content() = "&amp;"
305  $text = str_replace("&lt;", "<", $text);
306  $text = str_replace("&gt;", ">", $text);
307  $text = str_replace("&amp;", "&", $text);
308 
309  $text_node = new DOMText();
310  $text_node->appendData($text);
311  if (is_object($this->myDOMNode->firstChild)) {
312  $this->myDOMNode->replaceChild($text_node, $this->myDOMNode->firstChild);
313  } else {
314  $this->myDOMNode->appendChild($text_node);
315  }
316  }
317 
318  // ##added
319  public function get_content()
320  {
321  $text_node = $this->myDOMNode->firstChild;
322 
323  if (is_object($text_node)) {
324  return $text_node->textContent;
325  } else {
326  return "";
327  }
328  }
329 
330  // ## added
331  public function unlink($aDomNode)
332  {
333  parent::unlink_node($aDomNode);
334  }
335 }
336 
341 {
342  public $myDOMNode;
343 
344  public function __construct($aDomNode)
345  {
346  $this->myDOMNode = $aDomNode;
347  }
348 
349  public function append_child($newnode)
350  {
351  //echo "BH";
352  //if (strtolower(get_class($newnode)) != "php4domcdatasection")
353  //{
354  $doc = $this->myDOMNode->ownerDocument;
355  //echo "<br>BH1:".get_class($newnode).":";
356  $newnode->myDOMNode = $doc->importNode($newnode->myDOMNode, true);
357  //echo "BH2";
358  return new php4DOMElement($this->myDOMNode->appendChild($newnode->myDOMNode));
359  //}
360  //else
361  //{
362  //}
363  }
364 
365  public function replace_node($newnode)
366  {
367  return $this->set_content($newnode->myDOMNode->textContent);
368  }
369 
370  public function append_sibling($newnode)
371  {
372  return new php4DOMElement($this->myDOMNode->parentNode->appendChild($newnode->myDOMNode));
373  }
374 
375  public function attributes()
376  {
377  //echo "<br>node:".$this->myDOMNode->nodeName.":";
378  $myDOMNodeList = $this->myDOMNode->attributes;
379  $nodeSet = array();
380  $i = 0;
381  if (is_object($myDOMNodeList)) {
382  while ($node = $myDOMNodeList->item($i)) {
383  $nodeSet[] = new php4DOMAttr($node);
384  $i++;
385  }
386  }
387 
388  return $nodeSet;
389  }
390 
391  public function child_nodes()
392  {
393  $myDOMNodeList = $this->myDOMNode->childNodes;
394  $nodeSet = array();
395  $i = 0;
396  while ($node = $myDOMNodeList->item($i)) {
397  $nodeSet[] = new php4DOMElement($node);
398  $i++;
399  }
400  return $nodeSet;
401  }
402 
403  // ## added
404  public function children()
405  {
406  //echo "<br>php4DomNode::children"; flush();
407  return $this->child_nodes();
408  }
409 
410  // ## added
411  public function unlink_node($aDomNode = "")
412  {
413  // sometimes the node to unlink is passed
414  if (!is_object($aDomNode)) {
415  $aDomNode = $this;
416  //$aDomNode = $this;
417  }
418 
419  $parent = $aDomNode->myDOMNode->parentNode;
420  if (is_object($parent)) {
421  $parent->removeChild($aDomNode->myDOMNode);
422  }
423  }
424 
425  public function clone_node($deep = false)
426  {
427  return new php4DOMElement($this->myDOMNode->cloneNode($deep));
428  }
429 
430  public function first_child()
431  {
432  return new php4DOMElement($this->myDOMNode->firstChild);
433  }
434 
435  public function get_content()
436  {
437  return $this->myDOMNode->textContent;
438  }
439 
440  public function has_attributes()
441  {
442  return $this->myDOMNode->hasAttributes();
443  }
444 
445  public function has_child_nodes()
446  {
447  return $this->myDOMNode->hasChildNodes();
448  }
449 
450  // ## changed
451  public function insert_before($newnode, $refnode)
452  {
453  //echo "BH";
454  $doc = $this->myDOMNode->ownerDocument;
455  $newnode->myDOMNode = $doc->importNode($newnode->myDOMNode, true);
456 
457  $mydomnode = $this->myDOMNode;
458  $mynewnode = $newnode->myDOMNode;
459  $myrefnode = $refnode->myDOMNode;
460  try {
461  $domel = $mydomnode->insertBefore($mynewnode, $myrefnode);
462  } catch (DOMException $exception) {
463  // php 4 accepted $this == $refnode -> switch to parent of $this
464  $mydomnode = $this->myDOMNode->parentNode;
465  $domel = $mydomnode->insertBefore($mynewnode, $myrefnode);
466  }
467  $el = new php4DOMElement($domel);
468  return $el;
469  }
470 
471  // ## changed
472  public function last_child()
473  {
474  $last = $this->myDOMNode->lastChild;
475 
476  if (is_object($last)) {
477  return new php4DOMElement($last);
478  } else {
479  return false;
480  }
481  }
482 
483  // ## changed
484  public function next_sibling()
485  {
486  $next = $this->myDOMNode->nextSibling;
487 
488  if (is_object($next)) {
489  return new php4DOMElement($next);
490  } else {
491  return false;
492  }
493  }
494 
495  public function node_name($a_local = false)
496  {
497  if ($a_local) {
498  return $this->myDOMNode->localName;
499  } else {
500  return $this->myDOMNode->nodeName;
501  }
502  }
503 
504  public function node_type()
505  {
506  return $this->myDOMNode->nodeType;
507  }
508 
509  public function node_value()
510  {
511  return $this->myDOMNode->nodeValue;
512  }
513 
514  // ## changed
515  public function parent_node()
516  {
517  $parent = $this->myDOMNode->parentNode;
518 
519  if (is_object($parent)) {
520  return new php4DOMElement($parent);
521  } else {
522  return false;
523  }
524  }
525 
526  // ## changed
527  public function previous_sibling()
528  {
529  $prev = $this->myDOMNode->previousSibling;
530 
531  if (is_object($prev)) {
532  return new php4DOMElement($prev);
533  } else {
534  return false;
535  }
536  }
537 
538  public function remove_child($oldchild)
539  {
540  return new php4DOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode));
541  }
542 
543  public function replace_child($oldnode, $newnode)
544  {
545  return new php4DOMElement($this->myDOMNode->replaceChild($oldchild->myDOMNode, $newnode->myDOMNode));
546  }
547 
548  public function set_content($text)
549  {
550  $this->myDOMNode->textContent = $text;
551  return $this->myDOMNode->textContent;
552  }
553 }
554 
556 {
558  public $nodeset;
559 
560  public function __construct($aDOMNodelist)
561  {
562  $this->myDOMNodelist = $aDOMNodelist;
563  $this->nodeset = array();
564  $i = 0;
565  while ($node = $this->myDOMNodelist->item($i)) {
566  $this->nodeset[] = new php4DOMElement($node);
567  $i++;
568  }
569  }
570 }
571 
573 {
574  public $myDOMXPath;
575 
576  // ## added
577  public function xpath_eval($eval_str)
578  {
579  return xpath_eval($this, $eval_str);
580  }
581 
582  public function __construct($dom_document)
583  {
584  $this->myDOMXPath = new DOMXPath($dom_document->myDOMDocument);
585  }
586 
587  public function query($eval_str)
588  {
589  return new php4DOMNodelist($this->myDOMXPath->query($eval_str));
590  }
591 
592  public function xpath_register_ns($prefix, $namespaceURI)
593  {
594  return $this->myDOMXPath->registerNamespace($prefix, $namespaceURI);
595  }
596 }
php4DOMAttr($aDOMAttr)
xpath_register_ns($prefix, $namespaceURI)
append_child($newnode)
$data
Definition: storeScorm.php:23
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)
if($format !==null) $name
Definition: metadata.php:230
create_cdata_section($content)
php4DomElement
validate(&$error, bool $throw=false)
php4DOMNode($aDomNode)
__construct($aDOMNodelist)
$format
Definition: metadata.php:218
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)
append_sibling($newnode)
replace_child($oldnode, $newnode)
__construct($aDomNode)
$source
Definition: metadata.php:76
$i
Definition: metadata.php:24
xpath_eval($eval_str)
domxml_open_file($filename)