ILIAS  release_8 Revision v8.24
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
23function staticxmlerror(int $errno, string $errstr, ?string $errfile = null, ?int $errline = null, ?array $errcontext = null, bool $ret = false)
24{
25 static $errs = array();
26
27 $tag = 'DOMDocument::validate(): ';
28 $errs[] = str_replace($tag, '', $errstr);
29
30 if ($ret === true) {
31 return $errs;
32 }
33}
34
36{
37 return new php4DOMDocument($filename);
38}
39
40define('DOMXML_LOAD_PARSING', 0);
41
42/*
43* ##added
44*/
45function 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
58function xpath_eval(php4DOMXPath $xpath_context, string $eval_str, $contextnode = null)
59{
60 return $xpath_context->query($eval_str, $contextnode);
61}
62
63function xpath_new_context($dom_document): php4DOMXPath
64{
65 return new php4DOMXPath($dom_document);
66}
67
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{
106 public $success = null;
107 public string $error = "";
108 public DOMDocument $myDOMDocument;
109
110 // ##altered
111 public function __construct($source, $file = true, $a_mode = 0)
112 {
113 $this->myDOMDocument = new DOMDocument();
114 // temporary set error handler
115 set_error_handler('staticxmlerror');
116 $old = ini_set('html_errors', false);
117
118 if (is_object($source)) {
119 $this->myDOMDocument = $source;
120 $this->success = true;
121 } else {
122 if ($file) {
123 $this->success = @$this->myDOMDocument->load($source, $a_mode);
124 } else {
125 $this->success = $this->myDOMDocument->loadXML($source, $a_mode);
126 }
127 }
128
129 // Restore error handling
130 ini_set('html_errors', $old);
131 restore_error_handler();
132
133 if (!$this->success) {
134 $this->error_arr = staticxmlerror(0, "", "", 0, null, true);
135 foreach ($this->error_arr as $error) {
136 $error = str_replace("DOMDocument::loadXML():", "", $error);
137 $this->error .= $error . "<br />";
138 }
139 }
140 }
141
142 // ##added
143 public function xpath_init()
144 {
145 }
146
147 public function free()
148 {
149 unset($this->myDOMDocument);
150 }
151
152 // ##added
153 public function xpath_new_context()
154 {
155 return xpath_new_context($this);
156 }
157
158 // ##added
159 public function dump_node($node)
160 {
161 $str = $this->myDOMDocument->saveXML($node->myDOMNode);
162 return $str;
163 }
164
165 // ##added
166 public function validate(&$error, bool $throw = false)
167 {
168 $ok = false;
169 try {
170 $ok = $this->myDOMDocument->validate();
171 } catch (Exception $e) {
172 if ($throw) {
173 throw $e;
174 }
175 }
176 if (!$ok) {
177 $error = array(array("0", "Unknown Error"));
178
179 if (function_exists("libxml_get_last_error")) {
180 $err = libxml_get_last_error();
181
182 if (is_object($err)) {
183 $error = array(array($err->code, $err->message));
184 }
185 }
186 }
187 return $error;
188 }
189
190 public function create_attribute($name, $value)
191 {
192 $myAttr = $this->myDOMDocument->createAttribute($name);
193 $myAttr->value = $value;
194
195 return new php4DOMAttr($myAttr);
196 }
197
198 public function create_cdata_section($content)
199 {
200 return new php4DOMCDATASection($this->myDOMDocument->createCDATASection($content));
201 }
202
203 public function create_comment($data)
204 {
205 return new php4DOMElement($this->myDOMDocument->createComment($data));
206 }
207
208 public function create_element(string $name): php4DOMElement
209 {
210 return new php4DOMElement($this->myDOMDocument->createElement($name));
211 }
212
213 public function create_text_node($content)
214 {
215 return new php4DOMNode($this->myDOMDocument->createTextNode($content));
216 }
217
218 public function document_element()
219 {
220 return new php4DOMElement($this->myDOMDocument->documentElement);
221 }
222
223 public function dump_file($filename, $compressionmode = false, $format = false)
224 {
225 return $this->myDOMDocument->save($filename);
226 }
227
228 public function dump_mem($format = false, $encoding = false)
229 {
230 $r = $this->myDOMDocument->saveXML();
231 return $r;
232 }
233
235 {
236 $myDOMNodeList = $this->myDOMDocument->getElementsByTagName($name);
237 $nodeSet = array();
238 $i = 0;
239 while ($node = $myDOMNodeList->item($i)) {
240 $nodeSet[] = new php4DOMElement($node);
241 $i++;
242 }
243
244 return $nodeSet;
245 }
246
247 public function html_dump_mem()
248 {
249 return $this->myDOMDocument->saveHTML();
250 }
251}
252
257{
258 public function get_attribute($name)
259 {
260 return $this->myDOMNode->getAttribute($name);
261 }
262
263 public function owner_document()
264 {
265 return new php4DOMDocument($this->myDOMNode->ownerDocument);
266 }
267
269 {
270 $myDOMNodeList = $this->myDOMNode->getElementsByTagName($name);
271 $nodeSet = array();
272 $i = 0;
273 while ($node = $myDOMNodeList->item($i)) {
274 $nodeSet[] = new php4DOMElement($node);
275 $i++;
276 }
277
278 return $nodeSet;
279 }
280
281 public function has_attribute($name)
282 {
283 return $this->myDOMNode->hasAttribute($name);
284 }
285
286 public function remove_attribute($name)
287 {
288 return $this->myDOMNode->removeAttribute($name);
289 }
290
291 public function set_attribute($name, $value)
292 {
293 return $this->myDOMNode->setAttribute($name, $value);
294 }
295
296 public function tagname()
297 {
298 return $this->myDOMNode->tagName;
299 }
300
301 // ##added
302 public function set_content($text)
303 {
304 // the following replace has been added to conform with PHP4.
305 // A set_content("&amp;") brought a get_content() = "&" there,
306 // whereas PHP5 gives a get_content() = "&amp;"
307 $text = str_replace("&lt;", "<", $text);
308 $text = str_replace("&gt;", ">", $text);
309 $text = str_replace("&amp;", "&", $text);
310
311 $text_node = new DOMText();
312 $text_node->appendData($text);
313 if (is_object($this->myDOMNode->firstChild)) {
314 $this->myDOMNode->replaceChild($text_node, $this->myDOMNode->firstChild);
315 } else {
316 $this->myDOMNode->appendChild($text_node);
317 }
318 }
319
320 // ##added
321 public function get_content()
322 {
323 $text_node = $this->myDOMNode->firstChild;
324
325 if (is_object($text_node)) {
326 return $text_node->textContent;
327 } else {
328 return "";
329 }
330 }
331
332 // ## added
333 public function unlink($aDomNode)
334 {
335 parent::unlink_node($aDomNode);
336 }
337}
338
343{
345
346 public function __construct($aDomNode)
347 {
348 $this->myDOMNode = $aDomNode;
349 }
350
351 public function append_child($newnode)
352 {
353 //echo "BH";
354 //if (strtolower(get_class($newnode)) != "php4domcdatasection")
355 //{
356 $doc = $this->myDOMNode->ownerDocument;
357 //echo "<br>BH1:".get_class($newnode).":";
358 $newnode->myDOMNode = $doc->importNode($newnode->myDOMNode, true);
359 //echo "BH2";
360 return new php4DOMElement($this->myDOMNode->appendChild($newnode->myDOMNode));
361 //}
362 //else
363 //{
364 //}
365 }
366
367 public function replace_node($newnode)
368 {
369 return $this->set_content($newnode->myDOMNode->textContent);
370 }
371
372 public function append_sibling($newnode)
373 {
374 return new php4DOMElement($this->myDOMNode->parentNode->appendChild($newnode->myDOMNode));
375 }
376
377 public function attributes()
378 {
379 //echo "<br>node:".$this->myDOMNode->nodeName.":";
380 $myDOMNodeList = $this->myDOMNode->attributes;
381 $nodeSet = array();
382 $i = 0;
383 if (is_object($myDOMNodeList)) {
384 while ($node = $myDOMNodeList->item($i)) {
385 $nodeSet[] = new php4DOMAttr($node);
386 $i++;
387 }
388 }
389
390 return $nodeSet;
391 }
392
393 public function child_nodes()
394 {
395 $myDOMNodeList = $this->myDOMNode->childNodes;
396 $nodeSet = array();
397 $i = 0;
398 while ($node = $myDOMNodeList->item($i)) {
399 $nodeSet[] = new php4DOMElement($node);
400 $i++;
401 }
402 return $nodeSet;
403 }
404
405 // ## added
406 public function children()
407 {
408 //echo "<br>php4DomNode::children"; flush();
409 return $this->child_nodes();
410 }
411
412 // ## added
413 public function unlink_node($aDomNode = "")
414 {
415 // sometimes the node to unlink is passed
416 if (!is_object($aDomNode)) {
417 $aDomNode = $this;
418 //$aDomNode = $this;
419 }
420
421 $parent = $aDomNode->myDOMNode->parentNode;
422 if (is_object($parent)) {
423 $parent->removeChild($aDomNode->myDOMNode);
424 }
425 }
426
427 public function clone_node($deep = false)
428 {
429 return new php4DOMElement($this->myDOMNode->cloneNode($deep));
430 }
431
432 public function first_child()
433 {
434 return new php4DOMElement($this->myDOMNode->firstChild);
435 }
436
437 public function get_content()
438 {
439 return $this->myDOMNode->textContent;
440 }
441
442 public function has_attributes()
443 {
444 return $this->myDOMNode->hasAttributes();
445 }
446
447 public function has_child_nodes()
448 {
449 return $this->myDOMNode->hasChildNodes();
450 }
451
452 // ## changed
453 public function insert_before($newnode, $refnode)
454 {
455 //echo "BH";
456 $doc = $this->myDOMNode->ownerDocument;
457 $newnode->myDOMNode = $doc->importNode($newnode->myDOMNode, true);
458
459 $mydomnode = $this->myDOMNode;
460 $mynewnode = $newnode->myDOMNode;
461 $myrefnode = $refnode->myDOMNode;
462 try {
463 $domel = $mydomnode->insertBefore($mynewnode, $myrefnode);
464 } catch (DOMException $exception) {
465 // php 4 accepted $this == $refnode -> switch to parent of $this
466 $mydomnode = $this->myDOMNode->parentNode;
467 $domel = $mydomnode->insertBefore($mynewnode, $myrefnode);
468 }
469 $el = new php4DOMElement($domel);
470 return $el;
471 }
472
473 // ## changed
474 public function last_child()
475 {
476 $last = $this->myDOMNode->lastChild;
477
478 if (is_object($last)) {
479 return new php4DOMElement($last);
480 } else {
481 return false;
482 }
483 }
484
485 // ## changed
486 public function next_sibling()
487 {
488 $next = $this->myDOMNode->nextSibling;
489
490 if (is_object($next)) {
491 return new php4DOMElement($next);
492 } else {
493 return false;
494 }
495 }
496
497 public function node_name($a_local = false)
498 {
499 if ($a_local) {
500 return $this->myDOMNode->localName ?? "";
501 } else {
502 return $this->myDOMNode->nodeName ?? "";
503 }
504 }
505
506 public function node_type()
507 {
508 return $this->myDOMNode->nodeType;
509 }
510
511 public function node_value()
512 {
513 return $this->myDOMNode->nodeValue;
514 }
515
516 // ## changed
517 public function parent_node()
518 {
519 $parent = $this->myDOMNode->parentNode;
520
521 if (is_object($parent)) {
522 return new php4DOMElement($parent);
523 } else {
524 return false;
525 }
526 }
527
528 // ## changed
529 public function previous_sibling()
530 {
531 $prev = $this->myDOMNode->previousSibling;
532
533 if (is_object($prev)) {
534 return new php4DOMElement($prev);
535 } else {
536 return false;
537 }
538 }
539
540 public function remove_child($oldchild)
541 {
542 return new php4DOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode));
543 }
544
545 public function replace_child($oldnode, $newnode)
546 {
547 return new php4DOMElement($this->myDOMNode->replaceChild($oldchild->myDOMNode, $newnode->myDOMNode));
548 }
549
550 public function set_content($text)
551 {
552 $this->myDOMNode->textContent = $text;
553 return $this->myDOMNode->textContent;
554 }
555}
556
558{
560 public array $nodeset;
561
562 public function __construct($aDOMNodelist)
563 {
564 $this->myDOMNodelist = $aDOMNodelist;
565 $this->nodeset = array();
566 $i = 0;
567 while ($node = $this->myDOMNodelist->item($i)) {
568 $this->nodeset[] = new php4DOMElement($node);
569 $i++;
570 }
571 }
572}
573
575{
576 public DOMXPath $myDOMXPath;
577
578 // ## added
579 public function xpath_eval($eval_str)
580 {
581 return xpath_eval($this, $eval_str);
582 }
583
584 public function __construct($dom_document)
585 {
586 $this->myDOMXPath = new DOMXPath($dom_document->myDOMDocument);
587 }
588
589 public function query(string $eval_str): php4DOMNodelist
590 {
591 return new php4DOMNodelist($this->myDOMXPath->query($eval_str));
592 }
593
594 public function xpath_register_ns($prefix, $namespaceURI)
595 {
596 return $this->myDOMXPath->registerNamespace($prefix, $namespaceURI);
597 }
598}
$filename
Definition: buildRTE.php:78
error(string $a_errmsg)
__construct($aDOMAttr)
__construct($aDOMCDATASection)
create_cdata_section($content)
dump_mem($format=false, $encoding=false)
create_element(string $name)
DOMDocument $myDOMDocument
create_attribute($name, $value)
dump_file($filename, $compressionmode=false, $format=false)
__construct($source, $file=true, $a_mode=0)
validate(&$error, bool $throw=false)
set_attribute($name, $value)
clone_node($deep=false)
remove_child($oldchild)
node_name($a_local=false)
replace_node($newnode)
replace_child($oldnode, $newnode)
insert_before($newnode, $refnode)
__construct($aDomNode)
append_sibling($newnode)
unlink_node($aDomNode="")
append_child($newnode)
__construct($aDOMNodelist)
xpath_eval($eval_str)
__construct($dom_document)
query(string $eval_str)
xpath_register_ns($prefix, $namespaceURI)
domxml_open_file($filename)
staticxmlerror(int $errno, string $errstr, ?string $errfile=null, ?int $errline=null, ?array $errcontext=null, bool $ret=false)
domxml_open_mem($str, $mode=0, &$error=null)
xpath_new_context($dom_document)
xpath_eval(php4DOMXPath $xpath_context, string $eval_str, $contextnode=null)
if($format !==null) $name
Definition: metadata.php:247
$format
Definition: metadata.php:235
$i
Definition: metadata.php:41
$source
Definition: metadata.php:93