~adamzammit/quexf/main

« back to all changes in this revision

Viewing changes to functions/domxml-php4-to-php5.php

  • Committer: Adam Zammit
  • Date: 2023-08-09 00:55:58 UTC
  • Revision ID: adam.zammit@acspri.org.au-20230809005558-lqwoupkldm5mmkc7
Tags: quexf-1.21.0
1.21.0 release - add PHP 8.1 support

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 Requires PHP5, uses built-in DOM extension.
4
 
 To be used in PHP4 scripts using DOMXML extension: allows PHP4/DOMXML scripts to run on PHP5/DOM.
5
 
 (Optional: requires PHP5/XSL extension for domxml_xslt functions, PHP>=5.1 for XPath evaluation functions, and PHP>=5.1/libxml for DOMXML error reports)
6
 
 
7
 
 Typical use:
8
 
 {
9
 
  if (version_compare(PHP_VERSION,'5','>='))
10
 
   require_once('domxml-php4-to-php5.php');
11
 
 }
12
 
 
13
 
 Version 1.19, 2007-09-30, http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/
14
 
 
15
 
 ------------------------------------------------------------------
16
 
 Written by Alexandre Alapetite, http://alexandre.alapetite.net/cv/
17
 
 
18
 
 Copyright 2004-2007, Licence: Creative Commons "Attribution-ShareAlike 2.0 France" BY-SA (FR),
19
 
 http://creativecommons.org/licenses/by-sa/2.0/fr/
20
 
 http://alexandre.alapetite.net/divers/apropos/#by-sa
21
 
 - Attribution. You must give the original author credit
22
 
 - Share Alike. If you alter, transform, or build upon this work,
23
 
   you may distribute the resulting work only under a license identical to this one
24
 
   (Can be included in GPL/LGPL projects)
25
 
 - The French law is authoritative
26
 
 - Any of these conditions can be waived if you get permission from Alexandre Alapetite
27
 
 - Please send to Alexandre Alapetite the modifications you make,
28
 
   in order to improve this file for the benefit of everybody
29
 
 
30
 
 If you want to distribute this code, please do it as a link to:
31
 
 http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/
32
 
*/
33
 
 
34
 
define('DOMXML_LOAD_PARSING',0);
35
 
define('DOMXML_LOAD_VALIDATING',1);
36
 
define('DOMXML_LOAD_RECOVERING',2);
37
 
define('DOMXML_LOAD_SUBSTITUTE_ENTITIES',4);
38
 
//define('DOMXML_LOAD_COMPLETE_ATTRS',8);
39
 
define('DOMXML_LOAD_DONT_KEEP_BLANKS',16);
40
 
 
41
 
function domxml_new_doc($version) {return new php4DOMDocument();}
42
 
function domxml_new_xmldoc($version) {return new php4DOMDocument();}
43
 
function domxml_open_file($filename,$mode=DOMXML_LOAD_PARSING,&$error=null)
44
 
{
45
 
 $dom=new php4DOMDocument($mode);
46
 
 $errorMode=(func_num_args()>2)&&defined('LIBXML_VERSION');
47
 
 if ($errorMode) libxml_use_internal_errors(true);
48
 
 if (!$dom->myDOMNode->load($filename)) $dom=null;
49
 
 if ($errorMode)
50
 
 {
51
 
  $error=array_map('_error_report',libxml_get_errors());
52
 
  libxml_clear_errors();
53
 
 }
54
 
 return $dom;
55
 
}
56
 
function domxml_open_mem($str,$mode=DOMXML_LOAD_PARSING,&$error=null)
57
 
{
58
 
 $dom=new php4DOMDocument($mode);
59
 
 $errorMode=(func_num_args()>2)&&defined('LIBXML_VERSION');
60
 
 if ($errorMode) libxml_use_internal_errors(true);
61
 
 if (!$dom->myDOMNode->loadXML($str)) $dom=null;
62
 
 if ($errorMode)
63
 
 {
64
 
  $error=array_map('_error_report',libxml_get_errors());
65
 
  libxml_clear_errors();
66
 
 }
67
 
 return $dom;
68
 
}
69
 
function html_doc($html_doc,$from_file=false)
70
 
{
71
 
 $dom=new php4DOMDocument();
72
 
 if ($from_file) $result=$dom->myDOMNode->loadHTMLFile($html_doc);
73
 
 else $result=$dom->myDOMNode->loadHTML($html_doc);
74
 
 return $result ? $dom : null;
75
 
}
76
 
function html_doc_file($filename) {return html_doc($filename,true);}
77
 
function xmldoc($str) {return domxml_open_mem($str);}
78
 
function xmldocfile($filename) {return domxml_open_file($filename);}
79
 
function xpath_eval($xpath_context,$eval_str,$contextnode=null) {return $xpath_context->xpath_eval($eval_str,$contextnode);}
80
 
function xpath_new_context($dom_document) {return new php4DOMXPath($dom_document);}
81
 
function xpath_register_ns($xpath_context,$prefix,$namespaceURI) {return $xpath_context->myDOMXPath->registerNamespace($prefix,$namespaceURI);}
82
 
function _entityDecode($text) {return html_entity_decode(strtr($text,array('&apos;'=>'\'')),ENT_QUOTES,'UTF-8');}
83
 
function _error_report($error) {return array('errormessage'=>$error->message,'nodename'=>'','line'=>$error->line,'col'=>$error->column)+($error->file==''?array():array('directory'=>dirname($error->file),'file'=>basename($error->file)));}
84
 
 
85
 
class php4DOMAttr extends php4DOMNode
86
 
{
87
 
 function name() {return $this->myDOMNode->name;}
88
 
 function set_content($text) {}
89
 
 //function set_value($content) {return $this->myDOMNode->value=htmlspecialchars($content,ENT_QUOTES);}
90
 
 function specified() {return $this->myDOMNode->specified;}
91
 
 function value() {return $this->myDOMNode->value;}
92
 
}
93
 
 
94
 
class php4DOMDocument extends php4DOMNode
95
 
{
96
 
 function php4DOMDocument($mode=DOMXML_LOAD_PARSING)
97
 
 {
98
 
  $this->myDOMNode=new DOMDocument();
99
 
  $this->myOwnerDocument=$this;
100
 
  if ($mode & DOMXML_LOAD_VALIDATING) $this->myDOMNode->validateOnParse=true;
101
 
  if ($mode & DOMXML_LOAD_RECOVERING) $this->myDOMNode->recover=true;
102
 
  if ($mode & DOMXML_LOAD_SUBSTITUTE_ENTITIES) $this->myDOMNode->substituteEntities=true;
103
 
  if ($mode & DOMXML_LOAD_DONT_KEEP_BLANKS) $this->myDOMNode->preserveWhiteSpace=false;
104
 
 }
105
 
 function add_root($name)
106
 
 {
107
 
  if ($this->myDOMNode->hasChildNodes()) $this->myDOMNode->removeChild($this->myDOMNode->firstChild);
108
 
  return new php4DOMElement($this->myDOMNode->appendChild($this->myDOMNode->createElement($name)),$this->myOwnerDocument);
109
 
 }
110
 
 function create_attribute($name,$value)
111
 
 {
112
 
  $myAttr=$this->myDOMNode->createAttribute($name);
113
 
  $myAttr->value=htmlspecialchars($value,ENT_QUOTES);
114
 
  return new php4DOMAttr($myAttr,$this);
115
 
 }
116
 
 function create_cdata_section($content) {return new php4DOMNode($this->myDOMNode->createCDATASection($content),$this);}
117
 
 function create_comment($data) {return new php4DOMNode($this->myDOMNode->createComment($data),$this);}
118
 
 function create_element($name) {return new php4DOMElement($this->myDOMNode->createElement($name),$this);}
119
 
 function create_element_ns($uri,$name,$prefix=null)
120
 
 {
121
 
  if ($prefix==null) $prefix=$this->myDOMNode->lookupPrefix($uri);
122
 
  if (($prefix==null)&&(($this->myDOMNode->documentElement==null)||(!$this->myDOMNode->documentElement->isDefaultNamespace($uri)))) $prefix='a'.sprintf('%u',crc32($uri));
123
 
  return new php4DOMElement($this->myDOMNode->createElementNS($uri,$prefix==null ? $name : $prefix.':'.$name),$this);
124
 
 }
125
 
 function create_entity_reference($content) {return new php4DOMNode($this->myDOMNode->createEntityReference($content),$this);} //By Walter Ebert 2007-01-22
126
 
 function create_processing_instruction($target,$data=''){return new php4DomProcessingInstruction($this->myDOMNode->createProcessingInstruction($target,$data),$this);}
127
 
 function create_text_node($content) {return new php4DOMText($this->myDOMNode->createTextNode($content),$this);}
128
 
 function document_element() {return parent::_newDOMElement($this->myDOMNode->documentElement,$this);}
129
 
 function dump_file($filename,$compressionmode=false,$format=false)
130
 
 {
131
 
  $format0=$this->myDOMNode->formatOutput;
132
 
  $this->myDOMNode->formatOutput=$format;
133
 
  $res=$this->myDOMNode->save($filename);
134
 
  $this->myDOMNode->formatOutput=$format0;
135
 
  return $res;
136
 
 }
137
 
 function dump_mem($format=false,$encoding=false)
138
 
 {
139
 
  $format0=$this->myDOMNode->formatOutput;
140
 
  $this->myDOMNode->formatOutput=$format;
141
 
  $encoding0=$this->myDOMNode->encoding;
142
 
  if ($encoding) $this->myDOMNode->encoding=$encoding;
143
 
  $dump=$this->myDOMNode->saveXML();
144
 
  $this->myDOMNode->formatOutput=$format0;
145
 
  if ($encoding) $this->myDOMNode->encoding= $encoding0=='' ? 'UTF-8' : $encoding0; //UTF-8 is XML default encoding
146
 
  return $dump;
147
 
 }
148
 
 function free()
149
 
 {
150
 
  if ($this->myDOMNode->hasChildNodes()) $this->myDOMNode->removeChild($this->myDOMNode->firstChild);
151
 
  $this->myDOMNode=null;
152
 
  $this->myOwnerDocument=null;
153
 
 }
154
 
 function get_element_by_id($id) {return parent::_newDOMElement($this->myDOMNode->getElementById($id),$this);}
155
 
 function get_elements_by_tagname($name)
156
 
 {
157
 
  $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
158
 
  $nodeSet=array();
159
 
  $i=0;
160
 
  if (isset($myDOMNodeList))
161
 
   while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMElement($node,$this);
162
 
  return $nodeSet;
163
 
 }
164
 
 function html_dump_mem() {return $this->myDOMNode->saveHTML();}
165
 
 function root() {return parent::_newDOMElement($this->myDOMNode->documentElement,$this);}
166
 
 function xpath_new_context() {return new php4DOMXPath($this);}
167
 
}
168
 
 
169
 
class php4DOMElement extends php4DOMNode
170
 
{
171
 
 function add_namespace($uri,$prefix)
172
 
 {
173
 
  if ($this->myDOMNode->hasAttributeNS('http://www.w3.org/2000/xmlns/',$prefix)) return false;
174
 
  else
175
 
  {
176
 
   $this->myDOMNode->setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns:'.$prefix,$uri); //By Daniel Walker 2006-09-08
177
 
   return true;
178
 
  }
179
 
 }
180
 
 function get_attribute($name) {return $this->myDOMNode->getAttribute($name);}
181
 
 function get_attribute_node($name) {return parent::_newDOMElement($this->myDOMNode->getAttributeNode($name),$this->myOwnerDocument);}
182
 
 function get_elements_by_tagname($name)
183
 
 {
184
 
  $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
185
 
  $nodeSet=array();
186
 
  $i=0;
187
 
  if (isset($myDOMNodeList))
188
 
   while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMElement($node,$this->myOwnerDocument);
189
 
  return $nodeSet;
190
 
 }
191
 
 function has_attribute($name) {return $this->myDOMNode->hasAttribute($name);}
192
 
 function remove_attribute($name) {return $this->myDOMNode->removeAttribute($name);}
193
 
 function set_attribute($name,$value)
194
 
 {
195
 
  //return $this->myDOMNode->setAttribute($name,$value); //Does not return a DomAttr
196
 
  $myAttr=$this->myDOMNode->ownerDocument->createAttribute($name);
197
 
  $myAttr->value=htmlspecialchars($value,ENT_QUOTES); //Entity problem reported by AL-DesignWorks 2007-09-07
198
 
  $this->myDOMNode->setAttributeNode($myAttr);
199
 
  return new php4DOMAttr($myAttr,$this->myOwnerDocument);
200
 
 }
201
 
 /*function set_attribute_node($attr)
202
 
 {
203
 
  $this->myDOMNode->setAttributeNode($this->_importNode($attr));
204
 
  return $attr;
205
 
 }*/
206
 
 function set_name($name)
207
 
 {
208
 
  if ($this->myDOMNode->prefix=='') $newNode=$this->myDOMNode->ownerDocument->createElement($name);
209
 
  else $newNode=$this->myDOMNode->ownerDocument->createElementNS($this->myDOMNode->namespaceURI,$this->myDOMNode->prefix.':'.$name);
210
 
  $myDOMNodeList=$this->myDOMNode->attributes;
211
 
  $i=0;
212
 
  if (isset($myDOMNodeList))
213
 
   while ($node=$myDOMNodeList->item($i++))
214
 
    if ($node->namespaceURI=='') $newNode->setAttribute($node->name,$node->value);
215
 
    else $newNode->setAttributeNS($node->namespaceURI,$node->nodeName,$node->value);
216
 
  $myDOMNodeList=$this->myDOMNode->childNodes;
217
 
  if (isset($myDOMNodeList))
218
 
   while ($node=$myDOMNodeList->item(0)) $newNode->appendChild($node);
219
 
  $this->myDOMNode->parentNode->replaceChild($newNode,$this->myDOMNode);
220
 
  $this->myDOMNode=$newNode;
221
 
  return true;
222
 
 }
223
 
 function tagname() {return $this->myDOMNode->tagName;}
224
 
}
225
 
 
226
 
class php4DOMNode
227
 
{
228
 
 public $myDOMNode;
229
 
 public $myOwnerDocument;
230
 
 function php4DOMNode($aDomNode,$aOwnerDocument)
231
 
 {
232
 
  $this->myDOMNode=$aDomNode;
233
 
  $this->myOwnerDocument=$aOwnerDocument;
234
 
 }
235
 
 function __get($name)
236
 
 {
237
 
  switch ($name)
238
 
  {
239
 
   case 'type': return $this->myDOMNode->nodeType;
240
 
   case 'tagname': return $this->myDOMNode->tagName; //DomElement
241
 
   case 'content': return $this->myDOMNode->textContent;
242
 
   case 'name': return $this->myDOMNode->name; //DomAttribute
243
 
   case 'value': return $this->myDOMNode->value;
244
 
   default:
245
 
    $myErrors=debug_backtrace();
246
 
    trigger_error('Undefined property: '.get_class($this).'::$'.$name.' ['.$myErrors[0]['file'].':'.$myErrors[0]['line'].']',E_USER_NOTICE);
247
 
    return false;
248
 
  }
249
 
 }
250
 
 function add_child($newnode) {return append_child($newnode);}
251
 
 function add_namespace($uri,$prefix) {return false;}
252
 
 function append_child($newnode) {return self::_newDOMElement($this->myDOMNode->appendChild($this->_importNode($newnode)),$this->myOwnerDocument);}
253
 
 function append_sibling($newnode) {return self::_newDOMElement($this->myDOMNode->parentNode->appendChild($this->_importNode($newnode)),$this->myOwnerDocument);}
254
 
 function attributes()
255
 
 {
256
 
  $myDOMNodeList=$this->myDOMNode->attributes;
257
 
  $nodeSet=array();
258
 
  $i=0;
259
 
  if (isset($myDOMNodeList))
260
 
   while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMAttr($node,$this->myOwnerDocument);
261
 
  return $nodeSet;
262
 
 }
263
 
 function child_nodes()
264
 
 {
265
 
  $myDOMNodeList=$this->myDOMNode->childNodes;
266
 
  $nodeSet=array();
267
 
  $i=0;
268
 
  if (isset($myDOMNodeList))
269
 
   while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=self::_newDOMElement($node,$this->myOwnerDocument);
270
 
  return $nodeSet;
271
 
 }
272
 
 function children() {return $this->child_nodes();}
273
 
 function clone_node($deep=false) {return self::_newDOMElement($this->myDOMNode->cloneNode($deep),$this->myOwnerDocument);}
274
 
 //dump_node($node) should only be called on php4DOMDocument
275
 
 function dump_node($node=null) {return $node==null ? $this->myOwnerDocument->myDOMNode->saveXML($this->myDOMNode) : $this->myOwnerDocument->myDOMNode->saveXML($node->myDOMNode);}
276
 
 function first_child() {return self::_newDOMElement($this->myDOMNode->firstChild,$this->myOwnerDocument);}
277
 
 function get_content() {return $this->myDOMNode->textContent;}
278
 
 function has_attributes() {return $this->myDOMNode->hasAttributes();}
279
 
 function has_child_nodes() {return $this->myDOMNode->hasChildNodes();}
280
 
 function insert_before($newnode,$refnode) {return self::_newDOMElement($this->myDOMNode->insertBefore($this->_importNode($newnode),$refnode==null?null:$refnode->myDOMNode),$this->myOwnerDocument);}
281
 
 function is_blank_node() {return ($this->myDOMNode->nodeType===XML_TEXT_NODE)&&preg_match('%^\s*$%',$this->myDOMNode->nodeValue);}
282
 
 function last_child() {return self::_newDOMElement($this->myDOMNode->lastChild,$this->myOwnerDocument);}
283
 
 function new_child($name,$content)
284
 
 {
285
 
  $mySubNode=$this->myDOMNode->ownerDocument->createElement($name);
286
 
  $mySubNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(_entityDecode($content)));
287
 
  $this->myDOMNode->appendChild($mySubNode);
288
 
  return new php4DOMElement($mySubNode,$this->myOwnerDocument);
289
 
 }
290
 
 function next_sibling() {return self::_newDOMElement($this->myDOMNode->nextSibling,$this->myOwnerDocument);}
291
 
 function node_name() {return ($this->myDOMNode->nodeType===XML_ELEMENT_NODE) ? $this->myDOMNode->localName : $this->myDOMNode->nodeName;} //Avoid namespace prefix for DOMElement
292
 
 function node_type() {return $this->myDOMNode->nodeType;}
293
 
 function node_value() {return $this->myDOMNode->nodeValue;}
294
 
 function owner_document() {return $this->myOwnerDocument;}
295
 
 function parent_node() {return self::_newDOMElement($this->myDOMNode->parentNode,$this->myOwnerDocument);}
296
 
 function prefix() {return $this->myDOMNode->prefix;}
297
 
 function previous_sibling() {return self::_newDOMElement($this->myDOMNode->previousSibling,$this->myOwnerDocument);}
298
 
 function remove_child($oldchild) {return self::_newDOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode),$this->myOwnerDocument);}
299
 
 function replace_child($newnode,$oldnode) {return self::_newDOMElement($this->myDOMNode->replaceChild($this->_importNode($newnode),$oldnode->myDOMNode),$this->myOwnerDocument);}
300
 
 function replace_node($newnode) {return self::_newDOMElement($this->myDOMNode->parentNode->replaceChild($this->_importNode($newnode),$this->myDOMNode),$this->myOwnerDocument);}
301
 
 function set_content($text) {return $this->myDOMNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(_entityDecode($text)));} //Entity problem reported by AL-DesignWorks 2007-09-07
302
 
 //function set_name($name) {return $this->myOwnerDocument->renameNode($this->myDOMNode,$this->myDOMNode->namespaceURI,$name);}
303
 
 function set_namespace($uri,$prefix=null)
304
 
 {//Contributions by Daniel Walker 2006-09-08
305
 
  $nsprefix=$this->myDOMNode->lookupPrefix($uri);
306
 
  if ($nsprefix==null)
307
 
  {
308
 
   $nsprefix= $prefix==null ? $nsprefix='a'.sprintf('%u',crc32($uri)) : $prefix;
309
 
   if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE)
310
 
   {
311
 
    if (($prefix!=null)&&$this->myDOMNode->ownerElement->hasAttributeNS('http://www.w3.org/2000/xmlns/',$nsprefix)&&
312
 
        ($this->myDOMNode->ownerElement->getAttributeNS('http://www.w3.org/2000/xmlns/',$nsprefix)!=$uri))
313
 
    {//Remove namespace
314
 
     $parent=$this->myDOMNode->ownerElement;
315
 
     $parent->removeAttributeNode($this->myDOMNode);
316
 
     $parent->setAttribute($this->myDOMNode->localName,$this->myDOMNode->nodeValue);
317
 
     $this->myDOMNode=$parent->getAttributeNode($this->myDOMNode->localName);
318
 
     return;
319
 
    }
320
 
    $this->myDOMNode->ownerElement->setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns:'.$nsprefix,$uri);
321
 
   }
322
 
  }
323
 
  if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE)
324
 
  {
325
 
   $parent=$this->myDOMNode->ownerElement;
326
 
   $parent->removeAttributeNode($this->myDOMNode);
327
 
   $parent->setAttributeNS($uri,$nsprefix.':'.$this->myDOMNode->localName,$this->myDOMNode->nodeValue);
328
 
   $this->myDOMNode=$parent->getAttributeNodeNS($uri,$this->myDOMNode->localName);
329
 
  }
330
 
  elseif ($this->myDOMNode->nodeType===XML_ELEMENT_NODE)
331
 
  {
332
 
   $NewNode=$this->myDOMNode->ownerDocument->createElementNS($uri,$nsprefix.':'.$this->myDOMNode->localName);
333
 
   foreach ($this->myDOMNode->attributes as $n) $NewNode->appendChild($n->cloneNode(true));
334
 
   foreach ($this->myDOMNode->childNodes as $n) $NewNode->appendChild($n->cloneNode(true));
335
 
   $xpath=new DOMXPath($this->myDOMNode->ownerDocument);
336
 
   $myDOMNodeList=$xpath->query('namespace::*[name()!="xml"]',$this->myDOMNode); //Add old namespaces
337
 
   foreach ($myDOMNodeList as $n) $NewNode->setAttributeNS('http://www.w3.org/2000/xmlns/',$n->nodeName,$n->nodeValue); 
338
 
   $this->myDOMNode->parentNode->replaceChild($NewNode,$this->myDOMNode);
339
 
   $this->myDOMNode=$NewNode;
340
 
  }
341
 
 }
342
 
 function unlink_node()
343
 
 {
344
 
  if ($this->myDOMNode->parentNode!=null)
345
 
  {
346
 
   if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE) $this->myDOMNode->parentNode->removeAttributeNode($this->myDOMNode);
347
 
   else $this->myDOMNode->parentNode->removeChild($this->myDOMNode);
348
 
  }
349
 
 }
350
 
 protected function _importNode($newnode) {return $this->myOwnerDocument===$newnode->myOwnerDocument ? $newnode->myDOMNode : $this->myOwnerDocument->myDOMNode->importNode($newnode->myDOMNode,true);} //To import DOMNode from another DOMDocument
351
 
 static function _newDOMElement($aDOMNode,$aOwnerDocument)
352
 
 {//Check the PHP5 DOMNode before creating a new associated PHP4 DOMNode wrapper
353
 
  if ($aDOMNode==null) return null;
354
 
  switch ($aDOMNode->nodeType)
355
 
  {
356
 
   case XML_ELEMENT_NODE: return new php4DOMElement($aDOMNode,$aOwnerDocument);
357
 
   case XML_TEXT_NODE: return new php4DOMText($aDOMNode,$aOwnerDocument);
358
 
   case XML_ATTRIBUTE_NODE: return new php4DOMAttr($aDOMNode,$aOwnerDocument);
359
 
   case XML_PI_NODE: return new php4DomProcessingInstruction($aDOMNode,$aOwnerDocument);
360
 
   default: return new php4DOMNode($aDOMNode,$aOwnerDocument);
361
 
  }
362
 
 }
363
 
}
364
 
 
365
 
class php4DomProcessingInstruction extends php4DOMNode
366
 
{
367
 
 function data() {return $this->myDOMNode->data;}
368
 
 function target() {return $this->myDOMNode->target;}
369
 
}
370
 
 
371
 
class php4DOMText extends php4DOMNode
372
 
{
373
 
 function __get($name)
374
 
 {
375
 
  if ($name==='tagname') return '#text';
376
 
  else return parent::__get($name);
377
 
 }
378
 
 function tagname() {return '#text';}
379
 
 function set_content($text) {$this->myDOMNode->nodeValue=$text; return true;}
380
 
}
381
 
 
382
 
if (!defined('XPATH_NODESET'))
383
 
{
384
 
 define('XPATH_UNDEFINED',0);
385
 
 define('XPATH_NODESET',1);
386
 
 define('XPATH_BOOLEAN',2);
387
 
 define('XPATH_NUMBER',3);
388
 
 define('XPATH_STRING',4);
389
 
 /*define('XPATH_POINT',5);
390
 
 define('XPATH_RANGE',6);
391
 
 define('XPATH_LOCATIONSET',7);
392
 
 define('XPATH_USERS',8);
393
 
 define('XPATH_XSLT_TREE',9);*/
394
 
}
395
 
 
396
 
class php4DOMNodelist
397
 
{
398
 
 private $myDOMNodelist;
399
 
 public $nodeset;
400
 
 public $type;
401
 
 public $value;
402
 
 function php4DOMNodelist($aDOMNodelist,$aOwnerDocument)
403
 
 {
404
 
  if (is_object($aDOMNodelist)||is_array($aDOMNodelist))
405
 
  {
406
 
    $this->myDOMNodelist=$aDOMNodelist;
407
 
    $this->nodeset=array();
408
 
    if (isset($this->myDOMNodelist))
409
 
    {
410
 
     $this->type=XPATH_NODESET;
411
 
     $i=0;
412
 
     while ($node=$this->myDOMNodelist->item($i++)) $this->nodeset[]=php4DOMNode::_newDOMElement($node,$aOwnerDocument);
413
 
    }
414
 
    else $this->type=XPATH_UNDEFINED;
415
 
  }
416
 
  elseif (is_int($aDOMNodelist)||is_float($aDOMNodelist))
417
 
  {
418
 
    $this->type=XPATH_NUMBER;
419
 
    $this->value=$aDOMNodelist;
420
 
  }
421
 
  elseif (is_bool($aDOMNodelist))
422
 
  {
423
 
    $this->type=XPATH_BOOLEAN;
424
 
    $this->value=$aDOMNodelist;
425
 
  }
426
 
  elseif (is_string($aDOMNodelist))
427
 
  {
428
 
    $this->type=XPATH_STRING;
429
 
    $this->value=$aDOMNodelist;
430
 
  }
431
 
  else $this->type=XPATH_UNDEFINED;
432
 
 }
433
 
}
434
 
 
435
 
class php4DOMXPath
436
 
{
437
 
 public $myDOMXPath;
438
 
 private $myOwnerDocument;
439
 
 function php4DOMXPath($dom_document)
440
 
 {
441
 
  //TODO: If $dom_document is a DomElement, make that default $contextnode and modify XPath. Ex: '/test'
442
 
  $this->myOwnerDocument=$dom_document->myOwnerDocument;
443
 
  $this->myDOMXPath=new DOMXPath($this->myOwnerDocument->myDOMNode);
444
 
 }
445
 
 function xpath_eval($eval_str,$contextnode=null)
446
 
 {
447
 
  if (method_exists($this->myDOMXPath,'evaluate'))
448
 
   return isset($contextnode) ? new php4DOMNodelist($this->myDOMXPath->evaluate($eval_str,$contextnode->myDOMNode),$this->myOwnerDocument) : new php4DOMNodelist($this->myDOMXPath->evaluate($eval_str),$this->myOwnerDocument);
449
 
  else return isset($contextnode) ? new php4DOMNodelist($this->myDOMXPath->query($eval_str,$contextnode->myDOMNode),$this->myOwnerDocument) : new php4DOMNodelist($this->myDOMXPath->query($eval_str),$this->myOwnerDocument);
450
 
 }
451
 
 function xpath_register_ns($prefix,$namespaceURI) {return $this->myDOMXPath->registerNamespace($prefix,$namespaceURI);}
452
 
}
453
 
 
454
 
if (extension_loaded('xsl'))
455
 
{//See also: http://alexandre.alapetite.net/doc-alex/xslt-php4-php5/
456
 
 function domxml_xslt_stylesheet($xslstring) {return new php4DomXsltStylesheet(DOMDocument::loadXML($xslstring));}
457
 
 function domxml_xslt_stylesheet_doc($dom_document) {return new php4DomXsltStylesheet($dom_document);}
458
 
 function domxml_xslt_stylesheet_file($xslfile) {return new php4DomXsltStylesheet(DOMDocument::load($xslfile));}
459
 
 class php4DomXsltStylesheet
460
 
 {
461
 
  private $myxsltProcessor;
462
 
  function php4DomXsltStylesheet($dom_document)
463
 
  {
464
 
   $this->myxsltProcessor=new xsltProcessor();
465
 
   $this->myxsltProcessor->importStyleSheet($dom_document);
466
 
  }
467
 
  function process($dom_document,$xslt_parameters=array(),$param_is_xpath=false)
468
 
  {
469
 
   foreach ($xslt_parameters as $param=>$value) $this->myxsltProcessor->setParameter('',$param,$value);
470
 
   $myphp4DOMDocument=new php4DOMDocument();
471
 
   $myphp4DOMDocument->myDOMNode=$this->myxsltProcessor->transformToDoc($dom_document->myDOMNode);
472
 
   return $myphp4DOMDocument;
473
 
  }
474
 
  function result_dump_file($dom_document,$filename)
475
 
  {
476
 
   $html=$dom_document->myDOMNode->saveHTML();
477
 
   file_put_contents($filename,$html);
478
 
   return $html;
479
 
  }
480
 
  function result_dump_mem($dom_document) {return $dom_document->myDOMNode->saveHTML();}
481
 
 }
482
 
}
483
 
?>