PageRenderTime 27ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/domxml-php4-to-php5.php

https://github.com/foxluck/otdelstroy
PHP | 572 lines | 462 code | 110 blank | 0 comment | 30 complexity | 64f2cc891fe5308413190f80b597f6bb MD5 | raw file
  1. <?php
  2. @ini_set('zend.ze1_compatibility_mode',0);
  3. function domxml_new_doc( $version )
  4. {
  5. return new php4DOMDocument('');
  6. }
  7. function domxml_open_file( $filename )
  8. {
  9. return new php4DOMDocument($filename);
  10. }
  11. function domxml_open_mem( $str )
  12. {
  13. $dom=new php4DOMDocument('');
  14. if ($str)
  15. $dom->myDOMNode->loadXML($str);
  16. return $dom;
  17. }
  18. function xpath_eval( $xpath_context, $eval_str, $contextnode=null )
  19. {
  20. return $xpath_context->xpath_eval($eval_str,$contextnode);
  21. }
  22. function xpath_new_context( $dom_document )
  23. {
  24. return new php4DOMXPath( $dom_document );
  25. }
  26. function xpath_register_ns( $xpath_context, $prefix, $namespaceURI )
  27. {
  28. return $xpath_context->myDOMXPath->registerNamespace($prefix,$namespaceURI);
  29. }
  30. class php4DOMAttr extends php4DOMNode
  31. {
  32. function php4DOMAttr( $aDOMAttr )
  33. {
  34. $this->myDOMNode=$aDOMAttr;
  35. }
  36. function __get($name)
  37. {
  38. if ($name=='name')
  39. return $this->myDOMNode->name;
  40. elseif ($name=='value')
  41. return $this->myDOMNode->value;
  42. return parent::__get($name);
  43. }
  44. function name()
  45. {
  46. return $this->myDOMNode->name;
  47. }
  48. function set_value($content)
  49. {
  50. return $this->myDOMNode->value=$content;
  51. }
  52. function specified()
  53. {
  54. return $this->myDOMNode->specified;
  55. }
  56. function value()
  57. {
  58. return $this->myDOMNode->value;
  59. }
  60. }
  61. class php4DOMDocument extends php4DOMNode
  62. {
  63. function php4DOMDocument( $filename='' )
  64. {
  65. $this->myDOMNode=new DOMDocument();
  66. $this->myOwnerDocument=$this;
  67. if ($filename!='')
  68. $this->myDOMNode->load($filename);
  69. }
  70. function add_root( $name )
  71. {
  72. if ( $this->myDOMNode->hasChildNodes( ) )
  73. $this->myDOMNode->removeChild($this->myDOMNode->firstChild);
  74. return new php4DOMElement( $this->myDOMNode->appendChild( $this->myDOMNode->createElement( $name ) ), $this->myOwnerDocument );
  75. }
  76. function create_attribute( $name, $value )
  77. {
  78. $myAttr=$this->myDOMNode->createAttribute($name);
  79. $myAttr->value=$value;
  80. return new php4DOMAttr( $myAttr, $this );
  81. }
  82. function create_cdata_section( $content )
  83. {
  84. return new php4DOMNode( $this->myDOMNode->createCDATASection( $content ), $this );
  85. }
  86. function create_comment($data)
  87. {
  88. return new php4DOMNode( $this->myDOMNode->createComment( $data ), $this );
  89. }
  90. function create_element($name)
  91. {
  92. return new php4DOMElement( $this->myDOMNode->createElement( $name ), $this );
  93. }
  94. function create_text_node( $content )
  95. {
  96. return new php4DOMNode( $this->myDOMNode->createTextNode( $content ), $this );
  97. }
  98. function document_element()
  99. {
  100. return $this->_newDOMElement( $this->myDOMNode->documentElement, $this );
  101. }
  102. function dump_file( $filename, $compressionmode=false, $format=false )
  103. {
  104. $format0 = $this->myDOMNode->formatOutput;
  105. $this->myDOMNode->formatOutput = $format;
  106. $res = $this->myDOMNode->save($filename);
  107. $this->myDOMNode->formatOutput = $format0;
  108. return $res;
  109. }
  110. function dump_mem( $format=false, $encoding=false )
  111. {
  112. $format0=$this->myDOMNode->formatOutput;
  113. $this->myDOMNode->formatOutput=$format;
  114. $encoding0=$this->myDOMNode->encoding;
  115. if ($encoding)
  116. $this->myDOMNode->encoding=$encoding;
  117. $dump=$this->myDOMNode->saveXML();
  118. $this->myDOMNode->formatOutput=$format0;
  119. if ($encoding)
  120. $this->myDOMNode->encoding= $encoding0=='' ? 'UTF-8' : $encoding0; //UTF-8 est l'encodage XML par defaut
  121. return $dump;
  122. }
  123. function dump_node( $node = false )
  124. {
  125. return $this->myDOMNode->saveXML($node->myDOMNode);
  126. }
  127. function free()
  128. {
  129. if ($this->myDOMNode->hasChildNodes() )
  130. $this->myDOMNode->removeChild( $this->myDOMNode->firstChild );
  131. $this->myDOMNode=null;
  132. $this->myOwnerDocument=null;
  133. }
  134. function get_element_by_id($id)
  135. {
  136. return $this->_newDOMElement($this->myDOMNode->getElementById($id), $this );
  137. }
  138. function get_elements_by_tagname($name)
  139. {
  140. $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
  141. $nodeSet=array();
  142. $i=0;
  143. if ( isset( $myDOMNodeList ) && is_object( $myDOMNodeList ) )
  144. while ($node=$myDOMNodeList->item($i))
  145. {
  146. $nodeSet[]=new php4DOMElement($node,$this);
  147. $i++;
  148. }
  149. return $nodeSet;
  150. }
  151. function html_dump_mem()
  152. {
  153. return $this->myDOMNode->saveHTML();
  154. }
  155. function root()
  156. {
  157. return $this->_newDOMElement( $this->myDOMNode->documentElement, $this );
  158. }
  159. function xpath_new_context()
  160. {
  161. return new php4DOMXPath($this);
  162. }
  163. }
  164. class php4DOMElement extends php4DOMNode
  165. {
  166. function get_attribute( $name )
  167. {
  168. return $this->myDOMNode->getAttribute($name);
  169. }
  170. function get_elements_by_tagname($name)
  171. {
  172. $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
  173. $nodeSet=array();
  174. $i=0;
  175. if ( isset( $myDOMNodeList ) && is_object( $myDOMNodeList ) )
  176. while ($node=$myDOMNodeList->item($i))
  177. {
  178. $nodeSet[]=new php4DOMElement($node,$this->myOwnerDocument);
  179. $i++;
  180. }
  181. return $nodeSet;
  182. }
  183. function has_attribute( $name )
  184. {
  185. return $this->myDOMNode->hasAttribute($name);
  186. }
  187. function remove_attribute( $name )
  188. {
  189. return $this->myDOMNode->removeAttribute($name);
  190. }
  191. function set_attribute( $name, $value )
  192. {
  193. return $this->myDOMNode->setAttribute($name,$value);
  194. }
  195. function tagname()
  196. {
  197. return $this->myDOMNode->tagName;
  198. }
  199. }
  200. class php4DOMNode
  201. {
  202. var $myDOMNode;
  203. var $myOwnerDocument;
  204. function php4DOMNode( $aDomNode, $aOwnerDocument )
  205. {
  206. $this->myDOMNode=$aDomNode;
  207. $this->myOwnerDocument=$aOwnerDocument;
  208. }
  209. function __get($name)
  210. {
  211. if ( $name=='type' )
  212. return $this->myDOMNode->nodeType;
  213. elseif ( $name=='tagname' )
  214. return $this->myDOMNode->tagName;
  215. elseif ( $name=='content' )
  216. return $this->myDOMNode->textContent;
  217. else
  218. {
  219. $myErrors=debug_backtrace();
  220. trigger_error( 'Undefined property: '.get_class($this).'::$'.$name.' ['.$myErrors[0]['file'].':'.$myErrors[0]['line'].']',E_USER_NOTICE );
  221. return false;
  222. }
  223. }
  224. function append_child( $newnode )
  225. {
  226. return new php4DOMElement( $this->myDOMNode->appendChild( $this->_importNode( $newnode ) ), $this->myOwnerDocument );
  227. }
  228. function append_sibling( $newnode)
  229. {
  230. return new php4DOMElement( $this->myDOMNode->parentNode->appendChild( $this->_importNode( $newnode ) ), $this->myOwnerDocument );
  231. }
  232. function attributes()
  233. {
  234. $myDOMNodeList=$this->myDOMNode->attributes;
  235. $nodeSet=array();
  236. $i=0;
  237. if ( isset( $myDOMNodeList ) && is_object( $myDOMNodeList ) )
  238. while ( $node=$myDOMNodeList->item( $i ) )
  239. {
  240. $nodeSet[]=new php4DOMAttr($node,$this->myOwnerDocument);
  241. $i++;
  242. }
  243. return $nodeSet;
  244. }
  245. function child_nodes()
  246. {
  247. $myDOMNodeList=$this->myDOMNode->childNodes;
  248. $nodeSet=array();
  249. $i=0;
  250. if ( isset( $myDOMNodeList ) && is_object( $myDOMNodeList ) )
  251. while ( $node=$myDOMNodeList->item( $i ) )
  252. {
  253. $nodeSet[]=new php4DOMElement($node,$this->myOwnerDocument);
  254. $i++;
  255. }
  256. return $nodeSet;
  257. }
  258. function children()
  259. {
  260. return $this->child_nodes();
  261. }
  262. function clone_node( $deep=false )
  263. {
  264. return new php4DOMElement( $this->myDOMNode->cloneNode($deep), $this->myOwnerDocument );
  265. }
  266. function unlink_node( )
  267. {
  268. return $this->myDOMNode->parentNode->removeChild( $this->myDOMNode );
  269. }
  270. function dump_node( $node = false )
  271. {
  272. return $this->myOwnerDocument->myDOMNode->saveXML($this->myDOMNode);
  273. }
  274. function first_child()
  275. {
  276. return $this->_newDOMElement($this->myDOMNode->firstChild,$this->myOwnerDocument);
  277. }
  278. function get_content()
  279. {
  280. return $this->myDOMNode->textContent;
  281. }
  282. function has_attributes()
  283. {
  284. return $this->myDOMNode->hasAttributes();
  285. }
  286. function has_child_nodes()
  287. {
  288. return $this->myDOMNode->hasChildNodes();
  289. }
  290. function insert_before($newnode,$refnode)
  291. {
  292. return new php4DOMElement($this->myDOMNode->insertBefore($newnode->myDOMNode,$refnode->myDOMNode),$this->myOwnerDocument);
  293. }
  294. function is_blank_node()
  295. {
  296. return ($this->myDOMNode->nodeType==XML_TEXT_NODE)&&ereg('^([[:cntrl:]]|[[:space:]])*$',$this->myDOMNode->nodeValue);
  297. }
  298. function last_child()
  299. {
  300. return $this->_newDOMElement($this->myDOMNode->lastChild,$this->myOwnerDocument);
  301. }
  302. function new_child($name,$content)
  303. {
  304. $mySubNode=$this->myDOMNode->ownerDocument->createElement($name);
  305. $mySubNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(html_entity_decode($content,ENT_QUOTES)));
  306. $this->myDOMNode->appendChild($mySubNode);
  307. return new php4DOMElement($mySubNode,$this->myOwnerDocument);
  308. }
  309. function next_sibling()
  310. {
  311. return $this->_newDOMElement($this->myDOMNode->nextSibling,$this->myOwnerDocument);
  312. }
  313. function node_name()
  314. {
  315. if ( $this->myDOMNode->nodeType==XML_ELEMENT_NODE )
  316. return $this->myDOMNode->localName; //evite prefixe espace de nom
  317. else
  318. return $this->myDOMNode->nodeName;
  319. }
  320. function node_type()
  321. {
  322. return $this->myDOMNode->nodeType;
  323. }
  324. function node_value()
  325. {
  326. return $this->myDOMNode->nodeValue;
  327. }
  328. function owner_document()
  329. {
  330. return $this->myOwnerDocument;
  331. }
  332. function parent_node()
  333. {
  334. return $this->_newDOMElement($this->myDOMNode->parentNode,$this->myOwnerDocument);
  335. }
  336. function prefix()
  337. {
  338. return $this->myDOMNode->prefix;
  339. }
  340. function previous_sibling()
  341. {
  342. return $this->_newDOMElement($this->myDOMNode->previousSibling,$this->myOwnerDocument);
  343. }
  344. function remove_child( $oldchild )
  345. {
  346. return $this->_newDOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode),$this->myOwnerDocument);
  347. }
  348. function replace_child($oldnode,$newnode)
  349. {
  350. return $this->_newDOMElement($this->myDOMNode->replaceChild($oldnode->myDOMNode,$this->_importNode($newnode)),$this->myOwnerDocument);
  351. }
  352. function set_content( $text )
  353. {
  354. return $this->myDOMNode->appendChild($this->myDOMNode->ownerDocument->createTextNode($text));
  355. }
  356. function _importNode($newnode)
  357. {//Fonction privee pour importer un DOMNode d'un autre DOMDocument
  358. if ( $this->myOwnerDocument === $newnode->myOwnerDocument )
  359. return $newnode->myDOMNode;
  360. else
  361. return $this->myOwnerDocument->myDOMNode->importNode($newnode->myDOMNode,true);
  362. }
  363. function _newDOMElement($aDOMNode,$aOwnerDocument)
  364. {//Fonction privee pour verifier le DOMNode PHP5 avant d'y associer une enveloppe DOMNode PHP4
  365. if ($aDOMNode==null)
  366. return null;
  367. elseif ($aDOMNode->nodeType==XML_ELEMENT_NODE)
  368. return new php4DOMElement($aDOMNode,$aOwnerDocument);
  369. elseif ($aDOMNode->nodeType==XML_ATTRIBUTE_NODE)
  370. return new php4DOMAttr($aDOMNode,$aOwnerDocument);
  371. else return new php4DOMNode($aDOMNode,$aOwnerDocument);
  372. }
  373. }
  374. class php4DOMNodelist
  375. {
  376. var $myDOMNodelist;
  377. var $nodeset;
  378. function php4DOMNodelist($aDOMNodelist,$aOwnerDocument)
  379. {
  380. $this->myDOMNodelist=$aDOMNodelist;
  381. $this->nodeset=array();
  382. $i=0;
  383. if ( isset( $this->myDOMNodelist ) && is_object( $this->myDOMNodelist ) )
  384. while ($node=$this->myDOMNodelist->item($i))
  385. {
  386. switch($node->nodeType)
  387. {
  388. case XML_ATTRIBUTE_NODE:
  389. $this->nodeset[]=new php4DOMAttr($node,$aOwnerDocument);
  390. break;
  391. case XML_ELEMENT_NODE:
  392. default:
  393. $this->nodeset[]=new php4DOMElement($node,$aOwnerDocument);
  394. }
  395. $i++;
  396. }
  397. }
  398. }
  399. class php4DOMXPath
  400. {
  401. var $myDOMXPath;
  402. var $myOwnerDocument;
  403. function php4DOMXPath($dom_document)
  404. {
  405. $this->myOwnerDocument=$dom_document;
  406. $this->myDOMXPath=new DOMXPath($dom_document->myDOMNode);
  407. }
  408. function xpath_eval($eval_str,$contextnode=null)
  409. {
  410. if ( isset( $contextnode ) && is_object( $contextnode ) )
  411. return new php4DOMNodelist($this->myDOMXPath->query($eval_str,$contextnode->myDOMNode),$this->myOwnerDocument);
  412. else
  413. return new php4DOMNodelist($this->myDOMXPath->query($eval_str),$this->myOwnerDocument);
  414. }
  415. function xpath_register_ns($prefix,$namespaceURI)
  416. {
  417. return $this->myDOMXPath->registerNamespace( $prefix, $namespaceURI );
  418. }
  419. }
  420. if ( extension_loaded( 'xsl' ) )
  421. {
  422. function domxml_xslt_stylesheet($xslstring)
  423. {
  424. return new php4DomXsltStylesheet(DOMDocument::loadXML($xslstring));
  425. }
  426. function domxml_xslt_stylesheet_doc( $dom_document )
  427. {
  428. return new php4DomXsltStylesheet($dom_document);
  429. }
  430. function domxml_xslt_stylesheet_file( $xslfile )
  431. {
  432. return new php4DomXsltStylesheet(DOMDocument::load($xslfile));
  433. }
  434. class php4DomXsltStylesheet
  435. {
  436. var $myxsltProcessor;
  437. function php4DomXsltStylesheet($dom_document)
  438. {
  439. $this->myxsltProcessor=new xsltProcessor();
  440. $this->myxsltProcessor->importStyleSheet($dom_document);
  441. }
  442. function process($dom_document,$xslt_parameters=array(),$param_is_xpath=false)
  443. {
  444. foreach ( $xslt_parameters as $param=>$value )
  445. $this->myxsltProcessor->setParameter('',$param,$value);
  446. $myphp4DOMDocument=new php4DOMDocument();
  447. $myphp4DOMDocument->myDOMNode=$this->myxsltProcessor->transformToDoc($dom_document->myDOMNode);
  448. return $myphp4DOMDocument;
  449. }
  450. function result_dump_file($dom_document,$filename)
  451. {
  452. $html=$dom_document->myDOMNode->saveHTML();
  453. file_put_contents($filename,$html);
  454. return $html;
  455. }
  456. function result_dump_mem($dom_document)
  457. {
  458. return $dom_document->myDOMNode->saveHTML();
  459. }
  460. }
  461. }
  462. ?>