]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/3rdparty/simple_html_dom.php
fix of issue #718: Error parsing file imported from Pocket #718
[github/wallabag/wallabag.git] / inc / 3rdparty / simple_html_dom.php
CommitLineData
0753bfef 1<?php\r
2/**\r
3 * Website: http://sourceforge.net/projects/simplehtmldom/\r
4 * Additional projects that may be used: http://sourceforge.net/projects/debugobject/\r
5 * Acknowledge: Jose Solorzano (https://sourceforge.net/projects/php-html/)\r
6 * Contributions by:\r
7 * Yousuke Kumakura (Attribute filters)\r
8 * Vadim Voituk (Negative indexes supports of "find" method)\r
9 * Antcs (Constructor with automatically load contents either text or file/url)\r
10 *\r
11 * all affected sections have comments starting with "PaperG"\r
12 *\r
13 * Paperg - Added case insensitive testing of the value of the selector.\r
14 * Paperg - Added tag_start for the starting index of tags - NOTE: This works but not accurately.\r
15 * This tag_start gets counted AFTER \r\n have been crushed out, and after the remove_noice calls so it will not reflect the REAL position of the tag in the source,\r
16 * it will almost always be smaller by some amount.\r
17 * We use this to determine how far into the file the tag in question is. This "percentage will never be accurate as the $dom->size is the "real" number of bytes the dom was created from.\r
18 * but for most purposes, it's a really good estimation.\r
19 * Paperg - Added the forceTagsClosed to the dom constructor. Forcing tags closed is great for malformed html, but it CAN lead to parsing errors.\r
20 * Allow the user to tell us how much they trust the html.\r
21 * Paperg add the text and plaintext to the selectors for the find syntax. plaintext implies text in the innertext of a node. text implies that the tag is a text node.\r
22 * This allows for us to find tags based on the text they contain.\r
23 * Create find_ancestor_tag to see if a tag is - at any level - inside of another specific tag.\r
24 * Paperg: added parse_charset so that we know about the character set of the source document.\r
25 * NOTE: If the user's system has a routine called get_last_retrieve_url_contents_content_type availalbe, we will assume it's returning the content-type header from the\r
26 * last transfer or curl_exec, and we will parse that and use it in preference to any other method of charset detection.\r
27 *\r
28 * Found infinite loop in the case of broken html in restore_noise. Rewrote to protect from that.\r
29 * PaperG (John Schlick) Added get_display_size for "IMG" tags.\r
30 *\r
31 * Licensed under The MIT License\r
32 * Redistributions of files must retain the above copyright notice.\r
33 *\r
34 * @author S.C. Chen <me578022@gmail.com>\r
35 * @author John Schlick\r
36 * @author Rus Carroll\r
aa126ba4 37 * @version 1.5 ($Rev: 210 $)\r
0753bfef 38 * @package PlaceLocalInclude\r
39 * @subpackage simple_html_dom\r
40 */\r
41\r
42/**\r
43 * All of the Defines for the classes below.\r
44 * @author S.C. Chen <me578022@gmail.com>\r
45 */\r
46define('HDOM_TYPE_ELEMENT', 1);\r
47define('HDOM_TYPE_COMMENT', 2);\r
48define('HDOM_TYPE_TEXT', 3);\r
49define('HDOM_TYPE_ENDTAG', 4);\r
50define('HDOM_TYPE_ROOT', 5);\r
51define('HDOM_TYPE_UNKNOWN', 6);\r
52define('HDOM_QUOTE_DOUBLE', 0);\r
53define('HDOM_QUOTE_SINGLE', 1);\r
54define('HDOM_QUOTE_NO', 3);\r
55define('HDOM_INFO_BEGIN', 0);\r
56define('HDOM_INFO_END', 1);\r
57define('HDOM_INFO_QUOTE', 2);\r
58define('HDOM_INFO_SPACE', 3);\r
59define('HDOM_INFO_TEXT', 4);\r
60define('HDOM_INFO_INNER', 5);\r
61define('HDOM_INFO_OUTER', 6);\r
62define('HDOM_INFO_ENDSPACE',7);\r
63define('DEFAULT_TARGET_CHARSET', 'UTF-8');\r
64define('DEFAULT_BR_TEXT', "\r\n");\r
65define('DEFAULT_SPAN_TEXT', " ");\r
66define('MAX_FILE_SIZE', 600000);\r
67// helper functions\r
68// -----------------------------------------------------------------------------\r
69// get html dom from file\r
70// $maxlen is defined in the code as PHP_STREAM_COPY_ALL which is defined as -1.\r
71function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)\r
72{\r
73 // We DO force the tags to be terminated.\r
74 $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);\r
75 // For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done.\r
76 $contents = file_get_contents($url, $use_include_path, $context, $offset);\r
77 // Paperg - use our own mechanism for getting the contents as we want to control the timeout.\r
78 //$contents = retrieve_url_contents($url);\r
79 if (empty($contents) || strlen($contents) > MAX_FILE_SIZE)\r
80 {\r
81 return false;\r
82 }\r
83 // The second parameter can force the selectors to all be lowercase.\r
84 $dom->load($contents, $lowercase, $stripRN);\r
85 return $dom;\r
86}\r
87\r
88// get html dom from string\r
89function str_get_html($str, $lowercase=true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)\r
90{\r
91 $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);\r
92 if (empty($str) || strlen($str) > MAX_FILE_SIZE)\r
93 {\r
94 $dom->clear();\r
95 return false;\r
96 }\r
97 $dom->load($str, $lowercase, $stripRN);\r
98 return $dom;\r
99}\r
100\r
101// dump html dom tree\r
102function dump_html_tree($node, $show_attr=true, $deep=0)\r
103{\r
104 $node->dump($node);\r
105}\r
106\r
107\r
108/**\r
109 * simple html dom node\r
110 * PaperG - added ability for "find" routine to lowercase the value of the selector.\r
111 * PaperG - added $tag_start to track the start position of the tag in the total byte index\r
112 *\r
113 * @package PlaceLocalInclude\r
114 */\r
115class simple_html_dom_node\r
116{\r
117 public $nodetype = HDOM_TYPE_TEXT;\r
118 public $tag = 'text';\r
119 public $attr = array();\r
120 public $children = array();\r
121 public $nodes = array();\r
122 public $parent = null;\r
123 // The "info" array - see HDOM_INFO_... for what each element contains.\r
124 public $_ = array();\r
125 public $tag_start = 0;\r
126 private $dom = null;\r
127\r
128 function __construct($dom)\r
129 {\r
130 $this->dom = $dom;\r
131 $dom->nodes[] = $this;\r
132 }\r
133\r
134 function __destruct()\r
135 {\r
136 $this->clear();\r
137 }\r
138\r
139 function __toString()\r
140 {\r
141 return $this->outertext();\r
142 }\r
143\r
144 // clean up memory due to php5 circular references memory leak...\r
145 function clear()\r
146 {\r
147 $this->dom = null;\r
148 $this->nodes = null;\r
149 $this->parent = null;\r
150 $this->children = null;\r
151 }\r
152\r
153 // dump node's tree\r
154 function dump($show_attr=true, $deep=0)\r
155 {\r
156 $lead = str_repeat(' ', $deep);\r
157\r
158 echo $lead.$this->tag;\r
159 if ($show_attr && count($this->attr)>0)\r
160 {\r
161 echo '(';\r
162 foreach ($this->attr as $k=>$v)\r
163 echo "[$k]=>\"".$this->$k.'", ';\r
164 echo ')';\r
165 }\r
166 echo "\n";\r
167\r
168 if ($this->nodes)\r
169 {\r
170 foreach ($this->nodes as $c)\r
171 {\r
172 $c->dump($show_attr, $deep+1);\r
173 }\r
174 }\r
175 }\r
176\r
177\r
178 // Debugging function to dump a single dom node with a bunch of information about it.\r
179 function dump_node($echo=true)\r
180 {\r
181\r
182 $string = $this->tag;\r
183 if (count($this->attr)>0)\r
184 {\r
185 $string .= '(';\r
186 foreach ($this->attr as $k=>$v)\r
187 {\r
188 $string .= "[$k]=>\"".$this->$k.'", ';\r
189 }\r
190 $string .= ')';\r
191 }\r
192 if (count($this->_)>0)\r
193 {\r
194 $string .= ' $_ (';\r
195 foreach ($this->_ as $k=>$v)\r
196 {\r
197 if (is_array($v))\r
198 {\r
199 $string .= "[$k]=>(";\r
200 foreach ($v as $k2=>$v2)\r
201 {\r
202 $string .= "[$k2]=>\"".$v2.'", ';\r
203 }\r
204 $string .= ")";\r
205 } else {\r
206 $string .= "[$k]=>\"".$v.'", ';\r
207 }\r
208 }\r
209 $string .= ")";\r
210 }\r
211\r
212 if (isset($this->text))\r
213 {\r
214 $string .= " text: (" . $this->text . ")";\r
215 }\r
216\r
217 $string .= " HDOM_INNER_INFO: '";\r
218 if (isset($node->_[HDOM_INFO_INNER]))\r
219 {\r
220 $string .= $node->_[HDOM_INFO_INNER] . "'";\r
221 }\r
222 else\r
223 {\r
224 $string .= ' NULL ';\r
225 }\r
226\r
227 $string .= " children: " . count($this->children);\r
228 $string .= " nodes: " . count($this->nodes);\r
229 $string .= " tag_start: " . $this->tag_start;\r
230 $string .= "\n";\r
231\r
232 if ($echo)\r
233 {\r
234 echo $string;\r
235 return;\r
236 }\r
237 else\r
238 {\r
239 return $string;\r
240 }\r
241 }\r
242\r
243 // returns the parent of node\r
244 // If a node is passed in, it will reset the parent of the current node to that one.\r
245 function parent($parent=null)\r
246 {\r
247 // I am SURE that this doesn't work properly.\r
248 // It fails to unset the current node from it's current parents nodes or children list first.\r
249 if ($parent !== null)\r
250 {\r
251 $this->parent = $parent;\r
252 $this->parent->nodes[] = $this;\r
253 $this->parent->children[] = $this;\r
254 }\r
255\r
256 return $this->parent;\r
257 }\r
258\r
259 // verify that node has children\r
260 function has_child()\r
261 {\r
262 return !empty($this->children);\r
263 }\r
264\r
265 // returns children of node\r
266 function children($idx=-1)\r
267 {\r
268 if ($idx===-1)\r
269 {\r
270 return $this->children;\r
271 }\r
aa126ba4
MR
272 if (isset($this->children[$idx]))\r
273 {\r
274 return $this->children[$idx];\r
275 }\r
0753bfef 276 return null;\r
277 }\r
278\r
279 // returns the first child of node\r
280 function first_child()\r
281 {\r
282 if (count($this->children)>0)\r
283 {\r
284 return $this->children[0];\r
285 }\r
286 return null;\r
287 }\r
288\r
289 // returns the last child of node\r
290 function last_child()\r
291 {\r
292 if (($count=count($this->children))>0)\r
293 {\r
294 return $this->children[$count-1];\r
295 }\r
296 return null;\r
297 }\r
298\r
299 // returns the next sibling of node\r
300 function next_sibling()\r
301 {\r
302 if ($this->parent===null)\r
303 {\r
304 return null;\r
305 }\r
306\r
307 $idx = 0;\r
308 $count = count($this->parent->children);\r
309 while ($idx<$count && $this!==$this->parent->children[$idx])\r
310 {\r
311 ++$idx;\r
312 }\r
313 if (++$idx>=$count)\r
314 {\r
315 return null;\r
316 }\r
317 return $this->parent->children[$idx];\r
318 }\r
319\r
320 // returns the previous sibling of node\r
321 function prev_sibling()\r
322 {\r
323 if ($this->parent===null) return null;\r
324 $idx = 0;\r
325 $count = count($this->parent->children);\r
326 while ($idx<$count && $this!==$this->parent->children[$idx])\r
327 ++$idx;\r
328 if (--$idx<0) return null;\r
329 return $this->parent->children[$idx];\r
330 }\r
331\r
332 // function to locate a specific ancestor tag in the path to the root.\r
333 function find_ancestor_tag($tag)\r
334 {\r
335 global $debug_object;\r
aa126ba4 336 if (is_object($debug_object)) { $debug_object->debug_log_entry(1); }\r
0753bfef 337\r
338 // Start by including ourselves in the comparison.\r
339 $returnDom = $this;\r
340\r
341 while (!is_null($returnDom))\r
342 {\r
aa126ba4 343 if (is_object($debug_object)) { $debug_object->debug_log(2, "Current tag is: " . $returnDom->tag); }\r
0753bfef 344\r
345 if ($returnDom->tag == $tag)\r
346 {\r
347 break;\r
348 }\r
349 $returnDom = $returnDom->parent;\r
350 }\r
351 return $returnDom;\r
352 }\r
353\r
354 // get dom node's inner html\r
355 function innertext()\r
356 {\r
357 if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER];\r
358 if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);\r
359\r
360 $ret = '';\r
361 foreach ($this->nodes as $n)\r
362 $ret .= $n->outertext();\r
363 return $ret;\r
364 }\r
365\r
366 // get dom node's outer text (with tag)\r
367 function outertext()\r
368 {\r
369 global $debug_object;\r
370 if (is_object($debug_object))\r
371 {\r
372 $text = '';\r
373 if ($this->tag == 'text')\r
374 {\r
375 if (!empty($this->text))\r
376 {\r
377 $text = " with text: " . $this->text;\r
378 }\r
379 }\r
aa126ba4 380 $debug_object->debug_log(1, 'Innertext of tag: ' . $this->tag . $text);\r
0753bfef 381 }\r
382\r
383 if ($this->tag==='root') return $this->innertext();\r
384\r
385 // trigger callback\r
386 if ($this->dom && $this->dom->callback!==null)\r
387 {\r
388 call_user_func_array($this->dom->callback, array($this));\r
389 }\r
390\r
391 if (isset($this->_[HDOM_INFO_OUTER])) return $this->_[HDOM_INFO_OUTER];\r
392 if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);\r
393\r
394 // render begin tag\r
395 if ($this->dom && $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]])\r
396 {\r
397 $ret = $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]->makeup();\r
398 } else {\r
399 $ret = "";\r
400 }\r
401\r
402 // render inner text\r
403 if (isset($this->_[HDOM_INFO_INNER]))\r
404 {\r
405 // If it's a br tag... don't return the HDOM_INNER_INFO that we may or may not have added.\r
406 if ($this->tag != "br")\r
407 {\r
408 $ret .= $this->_[HDOM_INFO_INNER];\r
409 }\r
410 } else {\r
411 if ($this->nodes)\r
412 {\r
413 foreach ($this->nodes as $n)\r
414 {\r
415 $ret .= $this->convert_text($n->outertext());\r
416 }\r
417 }\r
418 }\r
419\r
420 // render end tag\r
421 if (isset($this->_[HDOM_INFO_END]) && $this->_[HDOM_INFO_END]!=0)\r
422 $ret .= '</'.$this->tag.'>';\r
423 return $ret;\r
424 }\r
425\r
426 // get dom node's plain text\r
427 function text()\r
428 {\r
429 if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER];\r
430 switch ($this->nodetype)\r
431 {\r
432 case HDOM_TYPE_TEXT: return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);\r
433 case HDOM_TYPE_COMMENT: return '';\r
434 case HDOM_TYPE_UNKNOWN: return '';\r
435 }\r
436 if (strcasecmp($this->tag, 'script')===0) return '';\r
437 if (strcasecmp($this->tag, 'style')===0) return '';\r
438\r
439 $ret = '';\r
440 // In rare cases, (always node type 1 or HDOM_TYPE_ELEMENT - observed for some span tags, and some p tags) $this->nodes is set to NULL.\r
441 // NOTE: This indicates that there is a problem where it's set to NULL without a clear happening.\r
442 // WHY is this happening?\r
443 if (!is_null($this->nodes))\r
444 {\r
445 foreach ($this->nodes as $n)\r
446 {\r
447 $ret .= $this->convert_text($n->text());\r
448 }\r
449\r
450 // If this node is a span... add a space at the end of it so multiple spans don't run into each other. This is plaintext after all.\r
451 if ($this->tag == "span")\r
452 {\r
453 $ret .= $this->dom->default_span_text;\r
454 }\r
455\r
456\r
457 }\r
458 return $ret;\r
459 }\r
460\r
461 function xmltext()\r
462 {\r
463 $ret = $this->innertext();\r
464 $ret = str_ireplace('<![CDATA[', '', $ret);\r
465 $ret = str_replace(']]>', '', $ret);\r
466 return $ret;\r
467 }\r
468\r
469 // build node's text with tag\r
470 function makeup()\r
471 {\r
472 // text, comment, unknown\r
473 if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);\r
474\r
475 $ret = '<'.$this->tag;\r
476 $i = -1;\r
477\r
478 foreach ($this->attr as $key=>$val)\r
479 {\r
480 ++$i;\r
481\r
482 // skip removed attribute\r
483 if ($val===null || $val===false)\r
484 continue;\r
485\r
486 $ret .= $this->_[HDOM_INFO_SPACE][$i][0];\r
487 //no value attr: nowrap, checked selected...\r
488 if ($val===true)\r
489 $ret .= $key;\r
490 else {\r
491 switch ($this->_[HDOM_INFO_QUOTE][$i])\r
492 {\r
493 case HDOM_QUOTE_DOUBLE: $quote = '"'; break;\r
494 case HDOM_QUOTE_SINGLE: $quote = '\''; break;\r
495 default: $quote = '';\r
496 }\r
497 $ret .= $key.$this->_[HDOM_INFO_SPACE][$i][1].'='.$this->_[HDOM_INFO_SPACE][$i][2].$quote.$val.$quote;\r
498 }\r
499 }\r
500 $ret = $this->dom->restore_noise($ret);\r
501 return $ret . $this->_[HDOM_INFO_ENDSPACE] . '>';\r
502 }\r
503\r
504 // find elements by css selector\r
505 //PaperG - added ability for find to lowercase the value of the selector.\r
506 function find($selector, $idx=null, $lowercase=false)\r
507 {\r
508 $selectors = $this->parse_selector($selector);\r
509 if (($count=count($selectors))===0) return array();\r
510 $found_keys = array();\r
511\r
512 // find each selector\r
513 for ($c=0; $c<$count; ++$c)\r
514 {\r
515 // The change on the below line was documented on the sourceforge code tracker id 2788009\r
516 // used to be: if (($levle=count($selectors[0]))===0) return array();\r
517 if (($levle=count($selectors[$c]))===0) return array();\r
518 if (!isset($this->_[HDOM_INFO_BEGIN])) return array();\r
519\r
520 $head = array($this->_[HDOM_INFO_BEGIN]=>1);\r
521\r
522 // handle descendant selectors, no recursive!\r
523 for ($l=0; $l<$levle; ++$l)\r
524 {\r
525 $ret = array();\r
526 foreach ($head as $k=>$v)\r
527 {\r
528 $n = ($k===-1) ? $this->dom->root : $this->dom->nodes[$k];\r
529 //PaperG - Pass this optional parameter on to the seek function.\r
530 $n->seek($selectors[$c][$l], $ret, $lowercase);\r
531 }\r
532 $head = $ret;\r
533 }\r
534\r
535 foreach ($head as $k=>$v)\r
536 {\r
537 if (!isset($found_keys[$k]))\r
aa126ba4 538 {\r
0753bfef 539 $found_keys[$k] = 1;\r
aa126ba4 540 }\r
0753bfef 541 }\r
542 }\r
543\r
544 // sort keys\r
545 ksort($found_keys);\r
546\r
547 $found = array();\r
548 foreach ($found_keys as $k=>$v)\r
549 $found[] = $this->dom->nodes[$k];\r
550\r
551 // return nth-element or array\r
552 if (is_null($idx)) return $found;\r
553 else if ($idx<0) $idx = count($found) + $idx;\r
554 return (isset($found[$idx])) ? $found[$idx] : null;\r
555 }\r
556\r
557 // seek for given conditions\r
558 // PaperG - added parameter to allow for case insensitive testing of the value of a selector.\r
559 protected function seek($selector, &$ret, $lowercase=false)\r
560 {\r
561 global $debug_object;\r
aa126ba4 562 if (is_object($debug_object)) { $debug_object->debug_log_entry(1); }\r
0753bfef 563\r
564 list($tag, $key, $val, $exp, $no_key) = $selector;\r
565\r
566 // xpath index\r
567 if ($tag && $key && is_numeric($key))\r
568 {\r
569 $count = 0;\r
570 foreach ($this->children as $c)\r
571 {\r
572 if ($tag==='*' || $tag===$c->tag) {\r
573 if (++$count==$key) {\r
574 $ret[$c->_[HDOM_INFO_BEGIN]] = 1;\r
575 return;\r
576 }\r
577 }\r
578 }\r
579 return;\r
580 }\r
581\r
582 $end = (!empty($this->_[HDOM_INFO_END])) ? $this->_[HDOM_INFO_END] : 0;\r
583 if ($end==0) {\r
584 $parent = $this->parent;\r
585 while (!isset($parent->_[HDOM_INFO_END]) && $parent!==null) {\r
586 $end -= 1;\r
587 $parent = $parent->parent;\r
588 }\r
589 $end += $parent->_[HDOM_INFO_END];\r
590 }\r
591\r
592 for ($i=$this->_[HDOM_INFO_BEGIN]+1; $i<$end; ++$i) {\r
593 $node = $this->dom->nodes[$i];\r
594\r
595 $pass = true;\r
596\r
597 if ($tag==='*' && !$key) {\r
598 if (in_array($node, $this->children, true))\r
599 $ret[$i] = 1;\r
600 continue;\r
601 }\r
602\r
603 // compare tag\r
604 if ($tag && $tag!=$node->tag && $tag!=='*') {$pass=false;}\r
605 // compare key\r
606 if ($pass && $key) {\r
607 if ($no_key) {\r
608 if (isset($node->attr[$key])) $pass=false;\r
609 } else {\r
610 if (($key != "plaintext") && !isset($node->attr[$key])) $pass=false;\r
611 }\r
612 }\r
613 // compare value\r
614 if ($pass && $key && $val && $val!=='*') {\r
615 // If they have told us that this is a "plaintext" search then we want the plaintext of the node - right?\r
616 if ($key == "plaintext") {\r
617 // $node->plaintext actually returns $node->text();\r
618 $nodeKeyValue = $node->text();\r
619 } else {\r
620 // this is a normal search, we want the value of that attribute of the tag.\r
621 $nodeKeyValue = $node->attr[$key];\r
622 }\r
aa126ba4 623 if (is_object($debug_object)) {$debug_object->debug_log(2, "testing node: " . $node->tag . " for attribute: " . $key . $exp . $val . " where nodes value is: " . $nodeKeyValue);}\r
0753bfef 624\r
625 //PaperG - If lowercase is set, do a case insensitive test of the value of the selector.\r
626 if ($lowercase) {\r
627 $check = $this->match($exp, strtolower($val), strtolower($nodeKeyValue));\r
628 } else {\r
629 $check = $this->match($exp, $val, $nodeKeyValue);\r
630 }\r
aa126ba4 631 if (is_object($debug_object)) {$debug_object->debug_log(2, "after match: " . ($check ? "true" : "false"));}\r
0753bfef 632\r
633 // handle multiple class\r
634 if (!$check && strcasecmp($key, 'class')===0) {\r
635 foreach (explode(' ',$node->attr[$key]) as $k) {\r
636 // Without this, there were cases where leading, trailing, or double spaces lead to our comparing blanks - bad form.\r
637 if (!empty($k)) {\r
638 if ($lowercase) {\r
639 $check = $this->match($exp, strtolower($val), strtolower($k));\r
640 } else {\r
641 $check = $this->match($exp, $val, $k);\r
642 }\r
643 if ($check) break;\r
644 }\r
645 }\r
646 }\r
647 if (!$check) $pass = false;\r
648 }\r
649 if ($pass) $ret[$i] = 1;\r
650 unset($node);\r
651 }\r
652 // It's passed by reference so this is actually what this function returns.\r
aa126ba4 653 if (is_object($debug_object)) {$debug_object->debug_log(1, "EXIT - ret: ", $ret);}\r
0753bfef 654 }\r
655\r
656 protected function match($exp, $pattern, $value) {\r
657 global $debug_object;\r
aa126ba4 658 if (is_object($debug_object)) {$debug_object->debug_log_entry(1);}\r
0753bfef 659\r
660 switch ($exp) {\r
661 case '=':\r
662 return ($value===$pattern);\r
663 case '!=':\r
664 return ($value!==$pattern);\r
665 case '^=':\r
666 return preg_match("/^".preg_quote($pattern,'/')."/", $value);\r
667 case '$=':\r
668 return preg_match("/".preg_quote($pattern,'/')."$/", $value);\r
669 case '*=':\r
670 if ($pattern[0]=='/') {\r
671 return preg_match($pattern, $value);\r
672 }\r
673 return preg_match("/".$pattern."/i", $value);\r
674 }\r
675 return false;\r
676 }\r
677\r
678 protected function parse_selector($selector_string) {\r
679 global $debug_object;\r
aa126ba4 680 if (is_object($debug_object)) {$debug_object->debug_log_entry(1);}\r
0753bfef 681\r
682 // pattern of CSS selectors, modified from mootools\r
683 // Paperg: Add the colon to the attrbute, so that it properly finds <tag attr:ibute="something" > like google does.\r
684 // Note: if you try to look at this attribute, yo MUST use getAttribute since $dom->x:y will fail the php syntax check.\r
685// Notice the \[ starting the attbute? and the @? following? This implies that an attribute can begin with an @ sign that is not captured.\r
686// This implies that an html attribute specifier may start with an @ sign that is NOT captured by the expression.\r
687// farther study is required to determine of this should be documented or removed.\r
688// $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is";\r
689 $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-:]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is";\r
690 preg_match_all($pattern, trim($selector_string).' ', $matches, PREG_SET_ORDER);\r
aa126ba4 691 if (is_object($debug_object)) {$debug_object->debug_log(2, "Matches Array: ", $matches);}\r
0753bfef 692\r
693 $selectors = array();\r
694 $result = array();\r
695 //print_r($matches);\r
696\r
697 foreach ($matches as $m) {\r
698 $m[0] = trim($m[0]);\r
699 if ($m[0]==='' || $m[0]==='/' || $m[0]==='//') continue;\r
700 // for browser generated xpath\r
701 if ($m[1]==='tbody') continue;\r
702\r
703 list($tag, $key, $val, $exp, $no_key) = array($m[1], null, null, '=', false);\r
704 if (!empty($m[2])) {$key='id'; $val=$m[2];}\r
705 if (!empty($m[3])) {$key='class'; $val=$m[3];}\r
706 if (!empty($m[4])) {$key=$m[4];}\r
707 if (!empty($m[5])) {$exp=$m[5];}\r
708 if (!empty($m[6])) {$val=$m[6];}\r
709\r
710 // convert to lowercase\r
711 if ($this->dom->lowercase) {$tag=strtolower($tag); $key=strtolower($key);}\r
712 //elements that do NOT have the specified attribute\r
713 if (isset($key[0]) && $key[0]==='!') {$key=substr($key, 1); $no_key=true;}\r
714\r
715 $result[] = array($tag, $key, $val, $exp, $no_key);\r
716 if (trim($m[7])===',') {\r
717 $selectors[] = $result;\r
718 $result = array();\r
719 }\r
720 }\r
721 if (count($result)>0)\r
722 $selectors[] = $result;\r
723 return $selectors;\r
724 }\r
725\r
aa126ba4
MR
726 function __get($name)\r
727 {\r
0753bfef 728 if (isset($this->attr[$name]))\r
729 {\r
730 return $this->convert_text($this->attr[$name]);\r
731 }\r
aa126ba4
MR
732 switch ($name)\r
733 {\r
0753bfef 734 case 'outertext': return $this->outertext();\r
735 case 'innertext': return $this->innertext();\r
736 case 'plaintext': return $this->text();\r
737 case 'xmltext': return $this->xmltext();\r
738 default: return array_key_exists($name, $this->attr);\r
739 }\r
740 }\r
741\r
aa126ba4
MR
742 function __set($name, $value)\r
743 {\r
744 global $debug_object;\r
745 if (is_object($debug_object)) {$debug_object->debug_log_entry(1);}\r
746\r
747 switch ($name)\r
748 {\r
0753bfef 749 case 'outertext': return $this->_[HDOM_INFO_OUTER] = $value;\r
750 case 'innertext':\r
751 if (isset($this->_[HDOM_INFO_TEXT])) return $this->_[HDOM_INFO_TEXT] = $value;\r
752 return $this->_[HDOM_INFO_INNER] = $value;\r
753 }\r
aa126ba4
MR
754 if (!isset($this->attr[$name]))\r
755 {\r
0753bfef 756 $this->_[HDOM_INFO_SPACE][] = array(' ', '', '');\r
757 $this->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;\r
758 }\r
759 $this->attr[$name] = $value;\r
760 }\r
761\r
aa126ba4
MR
762 function __isset($name)\r
763 {\r
764 switch ($name)\r
765 {\r
0753bfef 766 case 'outertext': return true;\r
767 case 'innertext': return true;\r
768 case 'plaintext': return true;\r
769 }\r
770 //no value attr: nowrap, checked selected...\r
771 return (array_key_exists($name, $this->attr)) ? true : isset($this->attr[$name]);\r
772 }\r
773\r
774 function __unset($name) {\r
775 if (isset($this->attr[$name]))\r
776 unset($this->attr[$name]);\r
777 }\r
778\r
779 // PaperG - Function to convert the text from one character set to another if the two sets are not the same.\r
780 function convert_text($text)\r
781 {\r
782 global $debug_object;\r
aa126ba4 783 if (is_object($debug_object)) {$debug_object->debug_log_entry(1);}\r
0753bfef 784\r
785 $converted_text = $text;\r
786\r
787 $sourceCharset = "";\r
788 $targetCharset = "";\r
789\r
790 if ($this->dom)\r
791 {\r
792 $sourceCharset = strtoupper($this->dom->_charset);\r
793 $targetCharset = strtoupper($this->dom->_target_charset);\r
794 }\r
aa126ba4 795 if (is_object($debug_object)) {$debug_object->debug_log(3, "source charset: " . $sourceCharset . " target charaset: " . $targetCharset);}\r
0753bfef 796\r
797 if (!empty($sourceCharset) && !empty($targetCharset) && (strcasecmp($sourceCharset, $targetCharset) != 0))\r
798 {\r
799 // Check if the reported encoding could have been incorrect and the text is actually already UTF-8\r
800 if ((strcasecmp($targetCharset, 'UTF-8') == 0) && ($this->is_utf8($text)))\r
801 {\r
802 $converted_text = $text;\r
803 }\r
804 else\r
805 {\r
806 $converted_text = iconv($sourceCharset, $targetCharset, $text);\r
807 }\r
808 }\r
809\r
810 // Lets make sure that we don't have that silly BOM issue with any of the utf-8 text we output.\r
811 if ($targetCharset == 'UTF-8')\r
812 {\r
813 if (substr($converted_text, 0, 3) == "\xef\xbb\xbf")\r
814 {\r
815 $converted_text = substr($converted_text, 3);\r
816 }\r
817 if (substr($converted_text, -3) == "\xef\xbb\xbf")\r
818 {\r
819 $converted_text = substr($converted_text, 0, -3);\r
820 }\r
821 }\r
822\r
823 return $converted_text;\r
824 }\r
825\r
826 /**\r
827 * Returns true if $string is valid UTF-8 and false otherwise.\r
828 *\r
829 * @param mixed $str String to be tested\r
830 * @return boolean\r
831 */\r
832 static function is_utf8($str)\r
833 {\r
834 $c=0; $b=0;\r
835 $bits=0;\r
836 $len=strlen($str);\r
837 for($i=0; $i<$len; $i++)\r
838 {\r
839 $c=ord($str[$i]);\r
840 if($c > 128)\r
841 {\r
842 if(($c >= 254)) return false;\r
843 elseif($c >= 252) $bits=6;\r
844 elseif($c >= 248) $bits=5;\r
845 elseif($c >= 240) $bits=4;\r
846 elseif($c >= 224) $bits=3;\r
847 elseif($c >= 192) $bits=2;\r
848 else return false;\r
849 if(($i+$bits) > $len) return false;\r
850 while($bits > 1)\r
851 {\r
852 $i++;\r
853 $b=ord($str[$i]);\r
854 if($b < 128 || $b > 191) return false;\r
855 $bits--;\r
856 }\r
857 }\r
858 }\r
859 return true;\r
860 }\r
861 /*\r
862 function is_utf8($string)\r
863 {\r
864 //this is buggy\r
865 return (utf8_encode(utf8_decode($string)) == $string);\r
866 }\r
867 */\r
868\r
869 /**\r
870 * Function to try a few tricks to determine the displayed size of an img on the page.\r
871 * NOTE: This will ONLY work on an IMG tag. Returns FALSE on all other tag types.\r
872 *\r
873 * @author John Schlick\r
874 * @version April 19 2012\r
875 * @return array an array containing the 'height' and 'width' of the image on the page or -1 if we can't figure it out.\r
876 */\r
877 function get_display_size()\r
878 {\r
879 global $debug_object;\r
880\r
881 $width = -1;\r
882 $height = -1;\r
883\r
884 if ($this->tag !== 'img')\r
885 {\r
886 return false;\r
887 }\r
888\r
889 // See if there is aheight or width attribute in the tag itself.\r
890 if (isset($this->attr['width']))\r
891 {\r
892 $width = $this->attr['width'];\r
893 }\r
894\r
895 if (isset($this->attr['height']))\r
896 {\r
897 $height = $this->attr['height'];\r
898 }\r
899\r
900 // Now look for an inline style.\r
901 if (isset($this->attr['style']))\r
902 {\r
903 // Thanks to user gnarf from stackoverflow for this regular expression.\r
904 $attributes = array();\r
905 preg_match_all("/([\w-]+)\s*:\s*([^;]+)\s*;?/", $this->attr['style'], $matches, PREG_SET_ORDER);\r
906 foreach ($matches as $match) {\r
907 $attributes[$match[1]] = $match[2];\r
908 }\r
909\r
910 // If there is a width in the style attributes:\r
911 if (isset($attributes['width']) && $width == -1)\r
912 {\r
913 // check that the last two characters are px (pixels)\r
914 if (strtolower(substr($attributes['width'], -2)) == 'px')\r
915 {\r
916 $proposed_width = substr($attributes['width'], 0, -2);\r
917 // Now make sure that it's an integer and not something stupid.\r
918 if (filter_var($proposed_width, FILTER_VALIDATE_INT))\r
919 {\r
920 $width = $proposed_width;\r
921 }\r
922 }\r
923 }\r
924\r
925 // If there is a width in the style attributes:\r
926 if (isset($attributes['height']) && $height == -1)\r
927 {\r
928 // check that the last two characters are px (pixels)\r
929 if (strtolower(substr($attributes['height'], -2)) == 'px')\r
930 {\r
931 $proposed_height = substr($attributes['height'], 0, -2);\r
932 // Now make sure that it's an integer and not something stupid.\r
933 if (filter_var($proposed_height, FILTER_VALIDATE_INT))\r
934 {\r
935 $height = $proposed_height;\r
936 }\r
937 }\r
938 }\r
939\r
940 }\r
941\r
942 // Future enhancement:\r
943 // Look in the tag to see if there is a class or id specified that has a height or width attribute to it.\r
944\r
945 // Far future enhancement\r
946 // Look at all the parent tags of this image to see if they specify a class or id that has an img selector that specifies a height or width\r
947 // Note that in this case, the class or id will have the img subselector for it to apply to the image.\r
948\r
949 // ridiculously far future development\r
950 // If the class or id is specified in a SEPARATE css file thats not on the page, go get it and do what we were just doing for the ones on the page.\r
951\r
952 $result = array('height' => $height,\r
953 'width' => $width);\r
954 return $result;\r
955 }\r
956\r
957 // camel naming conventions\r
958 function getAllAttributes() {return $this->attr;}\r
959 function getAttribute($name) {return $this->__get($name);}\r
960 function setAttribute($name, $value) {$this->__set($name, $value);}\r
961 function hasAttribute($name) {return $this->__isset($name);}\r
962 function removeAttribute($name) {$this->__set($name, null);}\r
963 function getElementById($id) {return $this->find("#$id", 0);}\r
964 function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);}\r
965 function getElementByTagName($name) {return $this->find($name, 0);}\r
966 function getElementsByTagName($name, $idx=null) {return $this->find($name, $idx);}\r
967 function parentNode() {return $this->parent();}\r
968 function childNodes($idx=-1) {return $this->children($idx);}\r
969 function firstChild() {return $this->first_child();}\r
970 function lastChild() {return $this->last_child();}\r
971 function nextSibling() {return $this->next_sibling();}\r
972 function previousSibling() {return $this->prev_sibling();}\r
973 function hasChildNodes() {return $this->has_child();}\r
974 function nodeName() {return $this->tag;}\r
975 function appendChild($node) {$node->parent($this); return $node;}\r
976\r
977}\r
978\r
979/**\r
980 * simple html dom parser\r
981 * Paperg - in the find routine: allow us to specify that we want case insensitive testing of the value of the selector.\r
982 * Paperg - change $size from protected to public so we can easily access it\r
983 * Paperg - added ForceTagsClosed in the constructor which tells us whether we trust the html or not. Default is to NOT trust it.\r
984 *\r
985 * @package PlaceLocalInclude\r
986 */\r
987class simple_html_dom\r
988{\r
989 public $root = null;\r
990 public $nodes = array();\r
991 public $callback = null;\r
992 public $lowercase = false;\r
993 // Used to keep track of how large the text was when we started.\r
994 public $original_size;\r
995 public $size;\r
996 protected $pos;\r
997 protected $doc;\r
998 protected $char;\r
999 protected $cursor;\r
1000 protected $parent;\r
1001 protected $noise = array();\r
1002 protected $token_blank = " \t\r\n";\r
1003 protected $token_equal = ' =/>';\r
1004 protected $token_slash = " />\r\n\t";\r
1005 protected $token_attr = ' >';\r
1006 // Note that this is referenced by a child node, and so it needs to be public for that node to see this information.\r
1007 public $_charset = '';\r
1008 public $_target_charset = '';\r
1009 protected $default_br_text = "";\r
1010 public $default_span_text = "";\r
1011\r
1012 // use isset instead of in_array, performance boost about 30%...\r
1013 protected $self_closing_tags = array('img'=>1, 'br'=>1, 'input'=>1, 'meta'=>1, 'link'=>1, 'hr'=>1, 'base'=>1, 'embed'=>1, 'spacer'=>1);\r
1014 protected $block_tags = array('root'=>1, 'body'=>1, 'form'=>1, 'div'=>1, 'span'=>1, 'table'=>1);\r
1015 // Known sourceforge issue #2977341\r
1016 // B tags that are not closed cause us to return everything to the end of the document.\r
1017 protected $optional_closing_tags = array(\r
1018 'tr'=>array('tr'=>1, 'td'=>1, 'th'=>1),\r
1019 'th'=>array('th'=>1),\r
1020 'td'=>array('td'=>1),\r
1021 'li'=>array('li'=>1),\r
1022 'dt'=>array('dt'=>1, 'dd'=>1),\r
1023 'dd'=>array('dd'=>1, 'dt'=>1),\r
1024 'dl'=>array('dd'=>1, 'dt'=>1),\r
1025 'p'=>array('p'=>1),\r
1026 'nobr'=>array('nobr'=>1),\r
1027 'b'=>array('b'=>1),\r
1028 'option'=>array('option'=>1),\r
1029 );\r
1030\r
1031 function __construct($str=null, $lowercase=true, $forceTagsClosed=true, $target_charset=DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)\r
1032 {\r
1033 if ($str)\r
1034 {\r
1035 if (preg_match("/^http:\/\//i",$str) || is_file($str))\r
1036 {\r
1037 $this->load_file($str);\r
1038 }\r
1039 else\r
1040 {\r
1041 $this->load($str, $lowercase, $stripRN, $defaultBRText, $defaultSpanText);\r
1042 }\r
1043 }\r
1044 // Forcing tags to be closed implies that we don't trust the html, but it can lead to parsing errors if we SHOULD trust the html.\r
1045 if (!$forceTagsClosed) {\r
1046 $this->optional_closing_array=array();\r
1047 }\r
1048 $this->_target_charset = $target_charset;\r
1049 }\r
1050\r
1051 function __destruct()\r
1052 {\r
1053 $this->clear();\r
1054 }\r
1055\r
1056 // load html from string\r
1057 function load($str, $lowercase=true, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)\r
1058 {\r
1059 global $debug_object;\r
1060\r
1061 // prepare\r
1062 $this->prepare($str, $lowercase, $stripRN, $defaultBRText, $defaultSpanText);\r
0753bfef 1063 // strip out cdata\r
1064 $this->remove_noise("'<!\[CDATA\[(.*?)\]\]>'is", true);\r
aa126ba4
MR
1065 // strip out comments\r
1066 $this->remove_noise("'<!--(.*?)-->'is");\r
0753bfef 1067 // Per sourceforge http://sourceforge.net/tracker/?func=detail&aid=2949097&group_id=218559&atid=1044037\r
1068 // Script tags removal now preceeds style tag removal.\r
1069 // strip out <script> tags\r
1070 $this->remove_noise("'<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>'is");\r
1071 $this->remove_noise("'<\s*script\s*>(.*?)<\s*/\s*script\s*>'is");\r
1072 // strip out <style> tags\r
1073 $this->remove_noise("'<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>'is");\r
1074 $this->remove_noise("'<\s*style\s*>(.*?)<\s*/\s*style\s*>'is");\r
1075 // strip out preformatted tags\r
1076 $this->remove_noise("'<\s*(?:code)[^>]*>(.*?)<\s*/\s*(?:code)\s*>'is");\r
1077 // strip out server side scripts\r
1078 $this->remove_noise("'(<\?)(.*?)(\?>)'s", true);\r
1079 // strip smarty scripts\r
1080 $this->remove_noise("'(\{\w)(.*?)(\})'s", true);\r
1081\r
1082 // parsing\r
1083 while ($this->parse());\r
1084 // end\r
1085 $this->root->_[HDOM_INFO_END] = $this->cursor;\r
1086 $this->parse_charset();\r
1087\r
1088 // make load function chainable\r
1089 return $this;\r
1090\r
1091 }\r
1092\r
1093 // load html from file\r
1094 function load_file()\r
1095 {\r
aa126ba4
MR
1096 //external error: NOT related to dom loading\r
1097 $extError=error_get_last();\r
1098\r
0753bfef 1099 $args = func_get_args();\r
1100 $this->load(call_user_func_array('file_get_contents', $args), true);\r
aa126ba4 1101\r
0753bfef 1102 // Throw an error if we can't properly load the dom.\r
aa126ba4
MR
1103 $error=error_get_last();\r
1104 if ($error!==$extError) {\r
0753bfef 1105 $this->clear();\r
1106 return false;\r
1107 }\r
1108 }\r
1109\r
1110 // set callback function\r
1111 function set_callback($function_name)\r
1112 {\r
1113 $this->callback = $function_name;\r
1114 }\r
1115\r
1116 // remove callback function\r
1117 function remove_callback()\r
1118 {\r
1119 $this->callback = null;\r
1120 }\r
1121\r
1122 // save dom as string\r
1123 function save($filepath='')\r
1124 {\r
1125 $ret = $this->root->innertext();\r
1126 if ($filepath!=='') file_put_contents($filepath, $ret, LOCK_EX);\r
1127 return $ret;\r
1128 }\r
1129\r
1130 // find dom node by css selector\r
1131 // Paperg - allow us to specify that we want case insensitive testing of the value of the selector.\r
1132 function find($selector, $idx=null, $lowercase=false)\r
1133 {\r
1134 return $this->root->find($selector, $idx, $lowercase);\r
1135 }\r
1136\r
1137 // clean up memory due to php5 circular references memory leak...\r
1138 function clear()\r
1139 {\r
1140 foreach ($this->nodes as $n) {$n->clear(); $n = null;}\r
1141 // This add next line is documented in the sourceforge repository. 2977248 as a fix for ongoing memory leaks that occur even with the use of clear.\r
1142 if (isset($this->children)) foreach ($this->children as $n) {$n->clear(); $n = null;}\r
1143 if (isset($this->parent)) {$this->parent->clear(); unset($this->parent);}\r
1144 if (isset($this->root)) {$this->root->clear(); unset($this->root);}\r
1145 unset($this->doc);\r
1146 unset($this->noise);\r
1147 }\r
1148\r
1149 function dump($show_attr=true)\r
1150 {\r
1151 $this->root->dump($show_attr);\r
1152 }\r
1153\r
1154 // prepare HTML data and init everything\r
1155 protected function prepare($str, $lowercase=true, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)\r
1156 {\r
1157 $this->clear();\r
1158\r
1159 // set the length of content before we do anything to it.\r
1160 $this->size = strlen($str);\r
1161 // Save the original size of the html that we got in. It might be useful to someone.\r
1162 $this->original_size = $this->size;\r
1163\r
1164 //before we save the string as the doc... strip out the \r \n's if we are told to.\r
1165 if ($stripRN) {\r
1166 $str = str_replace("\r", " ", $str);\r
1167 $str = str_replace("\n", " ", $str);\r
1168\r
1169 // set the length of content since we have changed it.\r
1170 $this->size = strlen($str);\r
1171 }\r
1172\r
1173 $this->doc = $str;\r
1174 $this->pos = 0;\r
1175 $this->cursor = 1;\r
1176 $this->noise = array();\r
1177 $this->nodes = array();\r
1178 $this->lowercase = $lowercase;\r
1179 $this->default_br_text = $defaultBRText;\r
1180 $this->default_span_text = $defaultSpanText;\r
1181 $this->root = new simple_html_dom_node($this);\r
1182 $this->root->tag = 'root';\r
1183 $this->root->_[HDOM_INFO_BEGIN] = -1;\r
1184 $this->root->nodetype = HDOM_TYPE_ROOT;\r
1185 $this->parent = $this->root;\r
1186 if ($this->size>0) $this->char = $this->doc[0];\r
1187 }\r
1188\r
1189 // parse html content\r
1190 protected function parse()\r
1191 {\r
1192 if (($s = $this->copy_until_char('<'))==='')\r
1193 {\r
1194 return $this->read_tag();\r
1195 }\r
1196\r
1197 // text\r
1198 $node = new simple_html_dom_node($this);\r
1199 ++$this->cursor;\r
1200 $node->_[HDOM_INFO_TEXT] = $s;\r
1201 $this->link_nodes($node, false);\r
1202 return true;\r
1203 }\r
1204\r
1205 // PAPERG - dkchou - added this to try to identify the character set of the page we have just parsed so we know better how to spit it out later.\r
1206 // NOTE: IF you provide a routine called get_last_retrieve_url_contents_content_type which returns the CURLINFO_CONTENT_TYPE from the last curl_exec\r
1207 // (or the content_type header from the last transfer), we will parse THAT, and if a charset is specified, we will use it over any other mechanism.\r
1208 protected function parse_charset()\r
1209 {\r
1210 global $debug_object;\r
1211\r
1212 $charset = null;\r
1213\r
1214 if (function_exists('get_last_retrieve_url_contents_content_type'))\r
1215 {\r
1216 $contentTypeHeader = get_last_retrieve_url_contents_content_type();\r
1217 $success = preg_match('/charset=(.+)/', $contentTypeHeader, $matches);\r
1218 if ($success)\r
1219 {\r
1220 $charset = $matches[1];\r
aa126ba4 1221 if (is_object($debug_object)) {$debug_object->debug_log(2, 'header content-type found charset of: ' . $charset);}\r
0753bfef 1222 }\r
1223\r
1224 }\r
1225\r
1226 if (empty($charset))\r
1227 {\r
aa126ba4 1228 $el = $this->root->find('meta[http-equiv=Content-Type]',0, true);\r
0753bfef 1229 if (!empty($el))\r
1230 {\r
1231 $fullvalue = $el->content;\r
aa126ba4 1232 if (is_object($debug_object)) {$debug_object->debug_log(2, 'meta content-type tag found' . $fullvalue);}\r
0753bfef 1233\r
1234 if (!empty($fullvalue))\r
1235 {\r
aa126ba4 1236 $success = preg_match('/charset=(.+)/i', $fullvalue, $matches);\r
0753bfef 1237 if ($success)\r
1238 {\r
1239 $charset = $matches[1];\r
1240 }\r
1241 else\r
1242 {\r
1243 // If there is a meta tag, and they don't specify the character set, research says that it's typically ISO-8859-1\r
aa126ba4 1244 if (is_object($debug_object)) {$debug_object->debug_log(2, 'meta content-type tag couldn\'t be parsed. using iso-8859 default.');}\r
0753bfef 1245 $charset = 'ISO-8859-1';\r
1246 }\r
1247 }\r
1248 }\r
1249 }\r
1250\r
1251 // If we couldn't find a charset above, then lets try to detect one based on the text we got...\r
1252 if (empty($charset))\r
1253 {\r
aa126ba4
MR
1254 // Use this in case mb_detect_charset isn't installed/loaded on this machine.\r
1255 $charset = false;\r
1256 if (function_exists('mb_detect_encoding'))\r
1257 {\r
1258 // Have php try to detect the encoding from the text given to us.\r
1259 $charset = mb_detect_encoding($this->root->plaintext . "ascii", $encoding_list = array( "UTF-8", "CP1252" ) );\r
1260 if (is_object($debug_object)) {$debug_object->debug_log(2, 'mb_detect found: ' . $charset);}\r
1261 }\r
0753bfef 1262\r
1263 // and if this doesn't work... then we need to just wrongheadedly assume it's UTF-8 so that we can move on - cause this will usually give us most of what we need...\r
1264 if ($charset === false)\r
1265 {\r
aa126ba4 1266 if (is_object($debug_object)) {$debug_object->debug_log(2, 'since mb_detect failed - using default of utf-8');}\r
0753bfef 1267 $charset = 'UTF-8';\r
1268 }\r
1269 }\r
1270\r
1271 // Since CP1252 is a superset, if we get one of it's subsets, we want it instead.\r
1272 if ((strtolower($charset) == strtolower('ISO-8859-1')) || (strtolower($charset) == strtolower('Latin1')) || (strtolower($charset) == strtolower('Latin-1')))\r
1273 {\r
aa126ba4 1274 if (is_object($debug_object)) {$debug_object->debug_log(2, 'replacing ' . $charset . ' with CP1252 as its a superset');}\r
0753bfef 1275 $charset = 'CP1252';\r
1276 }\r
1277\r
aa126ba4 1278 if (is_object($debug_object)) {$debug_object->debug_log(1, 'EXIT - ' . $charset);}\r
0753bfef 1279\r
1280 return $this->_charset = $charset;\r
1281 }\r
1282\r
1283 // read tag info\r
1284 protected function read_tag()\r
1285 {\r
1286 if ($this->char!=='<')\r
1287 {\r
1288 $this->root->_[HDOM_INFO_END] = $this->cursor;\r
1289 return false;\r
1290 }\r
1291 $begin_tag_pos = $this->pos;\r
1292 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1293\r
1294 // end tag\r
1295 if ($this->char==='/')\r
1296 {\r
1297 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1298 // This represents the change in the simple_html_dom trunk from revision 180 to 181.\r
1299 // $this->skip($this->token_blank_t);\r
1300 $this->skip($this->token_blank);\r
1301 $tag = $this->copy_until_char('>');\r
1302\r
1303 // skip attributes in end tag\r
1304 if (($pos = strpos($tag, ' '))!==false)\r
1305 $tag = substr($tag, 0, $pos);\r
1306\r
1307 $parent_lower = strtolower($this->parent->tag);\r
1308 $tag_lower = strtolower($tag);\r
1309\r
1310 if ($parent_lower!==$tag_lower)\r
1311 {\r
1312 if (isset($this->optional_closing_tags[$parent_lower]) && isset($this->block_tags[$tag_lower]))\r
1313 {\r
1314 $this->parent->_[HDOM_INFO_END] = 0;\r
1315 $org_parent = $this->parent;\r
1316\r
1317 while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower)\r
1318 $this->parent = $this->parent->parent;\r
1319\r
1320 if (strtolower($this->parent->tag)!==$tag_lower) {\r
1321 $this->parent = $org_parent; // restore origonal parent\r
1322 if ($this->parent->parent) $this->parent = $this->parent->parent;\r
1323 $this->parent->_[HDOM_INFO_END] = $this->cursor;\r
1324 return $this->as_text_node($tag);\r
1325 }\r
1326 }\r
1327 else if (($this->parent->parent) && isset($this->block_tags[$tag_lower]))\r
1328 {\r
1329 $this->parent->_[HDOM_INFO_END] = 0;\r
1330 $org_parent = $this->parent;\r
1331\r
1332 while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower)\r
1333 $this->parent = $this->parent->parent;\r
1334\r
1335 if (strtolower($this->parent->tag)!==$tag_lower)\r
1336 {\r
1337 $this->parent = $org_parent; // restore origonal parent\r
1338 $this->parent->_[HDOM_INFO_END] = $this->cursor;\r
1339 return $this->as_text_node($tag);\r
1340 }\r
1341 }\r
1342 else if (($this->parent->parent) && strtolower($this->parent->parent->tag)===$tag_lower)\r
1343 {\r
1344 $this->parent->_[HDOM_INFO_END] = 0;\r
1345 $this->parent = $this->parent->parent;\r
1346 }\r
1347 else\r
1348 return $this->as_text_node($tag);\r
1349 }\r
1350\r
1351 $this->parent->_[HDOM_INFO_END] = $this->cursor;\r
1352 if ($this->parent->parent) $this->parent = $this->parent->parent;\r
1353\r
1354 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1355 return true;\r
1356 }\r
1357\r
1358 $node = new simple_html_dom_node($this);\r
1359 $node->_[HDOM_INFO_BEGIN] = $this->cursor;\r
1360 ++$this->cursor;\r
1361 $tag = $this->copy_until($this->token_slash);\r
1362 $node->tag_start = $begin_tag_pos;\r
1363\r
1364 // doctype, cdata & comments...\r
1365 if (isset($tag[0]) && $tag[0]==='!') {\r
1366 $node->_[HDOM_INFO_TEXT] = '<' . $tag . $this->copy_until_char('>');\r
1367\r
1368 if (isset($tag[2]) && $tag[1]==='-' && $tag[2]==='-') {\r
1369 $node->nodetype = HDOM_TYPE_COMMENT;\r
1370 $node->tag = 'comment';\r
1371 } else {\r
1372 $node->nodetype = HDOM_TYPE_UNKNOWN;\r
1373 $node->tag = 'unknown';\r
1374 }\r
1375 if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>';\r
1376 $this->link_nodes($node, true);\r
1377 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1378 return true;\r
1379 }\r
1380\r
1381 // text\r
1382 if ($pos=strpos($tag, '<')!==false) {\r
1383 $tag = '<' . substr($tag, 0, -1);\r
1384 $node->_[HDOM_INFO_TEXT] = $tag;\r
1385 $this->link_nodes($node, false);\r
1386 $this->char = $this->doc[--$this->pos]; // prev\r
1387 return true;\r
1388 }\r
1389\r
1390 if (!preg_match("/^[\w-:]+$/", $tag)) {\r
1391 $node->_[HDOM_INFO_TEXT] = '<' . $tag . $this->copy_until('<>');\r
1392 if ($this->char==='<') {\r
1393 $this->link_nodes($node, false);\r
1394 return true;\r
1395 }\r
1396\r
1397 if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>';\r
1398 $this->link_nodes($node, false);\r
1399 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1400 return true;\r
1401 }\r
1402\r
1403 // begin tag\r
1404 $node->nodetype = HDOM_TYPE_ELEMENT;\r
1405 $tag_lower = strtolower($tag);\r
1406 $node->tag = ($this->lowercase) ? $tag_lower : $tag;\r
1407\r
1408 // handle optional closing tags\r
1409 if (isset($this->optional_closing_tags[$tag_lower]) )\r
1410 {\r
1411 while (isset($this->optional_closing_tags[$tag_lower][strtolower($this->parent->tag)]))\r
1412 {\r
1413 $this->parent->_[HDOM_INFO_END] = 0;\r
1414 $this->parent = $this->parent->parent;\r
1415 }\r
1416 $node->parent = $this->parent;\r
1417 }\r
1418\r
1419 $guard = 0; // prevent infinity loop\r
1420 $space = array($this->copy_skip($this->token_blank), '', '');\r
1421\r
1422 // attributes\r
1423 do\r
1424 {\r
1425 if ($this->char!==null && $space[0]==='')\r
1426 {\r
1427 break;\r
1428 }\r
1429 $name = $this->copy_until($this->token_equal);\r
1430 if ($guard===$this->pos)\r
1431 {\r
1432 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1433 continue;\r
1434 }\r
1435 $guard = $this->pos;\r
1436\r
1437 // handle endless '<'\r
1438 if ($this->pos>=$this->size-1 && $this->char!=='>') {\r
1439 $node->nodetype = HDOM_TYPE_TEXT;\r
1440 $node->_[HDOM_INFO_END] = 0;\r
1441 $node->_[HDOM_INFO_TEXT] = '<'.$tag . $space[0] . $name;\r
1442 $node->tag = 'text';\r
1443 $this->link_nodes($node, false);\r
1444 return true;\r
1445 }\r
1446\r
1447 // handle mismatch '<'\r
1448 if ($this->doc[$this->pos-1]=='<') {\r
1449 $node->nodetype = HDOM_TYPE_TEXT;\r
1450 $node->tag = 'text';\r
1451 $node->attr = array();\r
1452 $node->_[HDOM_INFO_END] = 0;\r
1453 $node->_[HDOM_INFO_TEXT] = substr($this->doc, $begin_tag_pos, $this->pos-$begin_tag_pos-1);\r
1454 $this->pos -= 2;\r
1455 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1456 $this->link_nodes($node, false);\r
1457 return true;\r
1458 }\r
1459\r
1460 if ($name!=='/' && $name!=='') {\r
1461 $space[1] = $this->copy_skip($this->token_blank);\r
1462 $name = $this->restore_noise($name);\r
1463 if ($this->lowercase) $name = strtolower($name);\r
1464 if ($this->char==='=') {\r
1465 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1466 $this->parse_attr($node, $name, $space);\r
1467 }\r
1468 else {\r
1469 //no value attr: nowrap, checked selected...\r
1470 $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;\r
1471 $node->attr[$name] = true;\r
1472 if ($this->char!='>') $this->char = $this->doc[--$this->pos]; // prev\r
1473 }\r
1474 $node->_[HDOM_INFO_SPACE][] = $space;\r
1475 $space = array($this->copy_skip($this->token_blank), '', '');\r
1476 }\r
1477 else\r
1478 break;\r
1479 } while ($this->char!=='>' && $this->char!=='/');\r
1480\r
1481 $this->link_nodes($node, true);\r
1482 $node->_[HDOM_INFO_ENDSPACE] = $space[0];\r
1483\r
1484 // check self closing\r
1485 if ($this->copy_until_char_escape('>')==='/')\r
1486 {\r
1487 $node->_[HDOM_INFO_ENDSPACE] .= '/';\r
1488 $node->_[HDOM_INFO_END] = 0;\r
1489 }\r
1490 else\r
1491 {\r
1492 // reset parent\r
1493 if (!isset($this->self_closing_tags[strtolower($node->tag)])) $this->parent = $node;\r
1494 }\r
1495 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1496\r
1497 // If it's a BR tag, we need to set it's text to the default text.\r
1498 // This way when we see it in plaintext, we can generate formatting that the user wants.\r
1499 // since a br tag never has sub nodes, this works well.\r
1500 if ($node->tag == "br")\r
1501 {\r
1502 $node->_[HDOM_INFO_INNER] = $this->default_br_text;\r
1503 }\r
1504\r
1505 return true;\r
1506 }\r
1507\r
1508 // parse attributes\r
1509 protected function parse_attr($node, $name, &$space)\r
1510 {\r
1511 // Per sourceforge: http://sourceforge.net/tracker/?func=detail&aid=3061408&group_id=218559&atid=1044037\r
1512 // If the attribute is already defined inside a tag, only pay atetntion to the first one as opposed to the last one.\r
1513 if (isset($node->attr[$name]))\r
1514 {\r
1515 return;\r
1516 }\r
1517\r
1518 $space[2] = $this->copy_skip($this->token_blank);\r
1519 switch ($this->char) {\r
1520 case '"':\r
1521 $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;\r
1522 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1523 $node->attr[$name] = $this->restore_noise($this->copy_until_char_escape('"'));\r
1524 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1525 break;\r
1526 case '\'':\r
1527 $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_SINGLE;\r
1528 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1529 $node->attr[$name] = $this->restore_noise($this->copy_until_char_escape('\''));\r
1530 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1531 break;\r
1532 default:\r
1533 $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;\r
1534 $node->attr[$name] = $this->restore_noise($this->copy_until($this->token_attr));\r
1535 }\r
1536 // PaperG: Attributes should not have \r or \n in them, that counts as html whitespace.\r
1537 $node->attr[$name] = str_replace("\r", "", $node->attr[$name]);\r
1538 $node->attr[$name] = str_replace("\n", "", $node->attr[$name]);\r
1539 // PaperG: If this is a "class" selector, lets get rid of the preceeding and trailing space since some people leave it in the multi class case.\r
1540 if ($name == "class") {\r
1541 $node->attr[$name] = trim($node->attr[$name]);\r
1542 }\r
1543 }\r
1544\r
1545 // link node's parent\r
1546 protected function link_nodes(&$node, $is_child)\r
1547 {\r
1548 $node->parent = $this->parent;\r
1549 $this->parent->nodes[] = $node;\r
1550 if ($is_child)\r
1551 {\r
1552 $this->parent->children[] = $node;\r
1553 }\r
1554 }\r
1555\r
1556 // as a text node\r
1557 protected function as_text_node($tag)\r
1558 {\r
1559 $node = new simple_html_dom_node($this);\r
1560 ++$this->cursor;\r
1561 $node->_[HDOM_INFO_TEXT] = '</' . $tag . '>';\r
1562 $this->link_nodes($node, false);\r
1563 $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1564 return true;\r
1565 }\r
1566\r
1567 protected function skip($chars)\r
1568 {\r
1569 $this->pos += strspn($this->doc, $chars, $this->pos);\r
1570 $this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1571 }\r
1572\r
1573 protected function copy_skip($chars)\r
1574 {\r
1575 $pos = $this->pos;\r
1576 $len = strspn($this->doc, $chars, $pos);\r
1577 $this->pos += $len;\r
1578 $this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1579 if ($len===0) return '';\r
1580 return substr($this->doc, $pos, $len);\r
1581 }\r
1582\r
1583 protected function copy_until($chars)\r
1584 {\r
1585 $pos = $this->pos;\r
1586 $len = strcspn($this->doc, $chars, $pos);\r
1587 $this->pos += $len;\r
1588 $this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
1589 return substr($this->doc, $pos, $len);\r
1590 }\r
1591\r
1592 protected function copy_until_char($char)\r
1593 {\r
1594 if ($this->char===null) return '';\r
1595\r
1596 if (($pos = strpos($this->doc, $char, $this->pos))===false) {\r
1597 $ret = substr($this->doc, $this->pos, $this->size-$this->pos);\r
1598 $this->char = null;\r
1599 $this->pos = $this->size;\r
1600 return $ret;\r
1601 }\r
1602\r
1603 if ($pos===$this->pos) return '';\r
1604 $pos_old = $this->pos;\r
1605 $this->char = $this->doc[$pos];\r
1606 $this->pos = $pos;\r
1607 return substr($this->doc, $pos_old, $pos-$pos_old);\r
1608 }\r
1609\r
1610 protected function copy_until_char_escape($char)\r
1611 {\r
1612 if ($this->char===null) return '';\r
1613\r
1614 $start = $this->pos;\r
1615 while (1)\r
1616 {\r
1617 if (($pos = strpos($this->doc, $char, $start))===false)\r
1618 {\r
1619 $ret = substr($this->doc, $this->pos, $this->size-$this->pos);\r
1620 $this->char = null;\r
1621 $this->pos = $this->size;\r
1622 return $ret;\r
1623 }\r
1624\r
1625 if ($pos===$this->pos) return '';\r
1626\r
1627 if ($this->doc[$pos-1]==='\\') {\r
1628 $start = $pos+1;\r
1629 continue;\r
1630 }\r
1631\r
1632 $pos_old = $this->pos;\r
1633 $this->char = $this->doc[$pos];\r
1634 $this->pos = $pos;\r
1635 return substr($this->doc, $pos_old, $pos-$pos_old);\r
1636 }\r
1637 }\r
1638\r
1639 // remove noise from html content\r
1640 // save the noise in the $this->noise array.\r
1641 protected function remove_noise($pattern, $remove_tag=false)\r
1642 {\r
1643 global $debug_object;\r
aa126ba4 1644 if (is_object($debug_object)) { $debug_object->debug_log_entry(1); }\r
0753bfef 1645\r
1646 $count = preg_match_all($pattern, $this->doc, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE);\r
1647\r
1648 for ($i=$count-1; $i>-1; --$i)\r
1649 {\r
1650 $key = '___noise___'.sprintf('% 5d', count($this->noise)+1000);\r
aa126ba4 1651 if (is_object($debug_object)) { $debug_object->debug_log(2, 'key is: ' . $key); }\r
0753bfef 1652 $idx = ($remove_tag) ? 0 : 1;\r
1653 $this->noise[$key] = $matches[$i][$idx][0];\r
1654 $this->doc = substr_replace($this->doc, $key, $matches[$i][$idx][1], strlen($matches[$i][$idx][0]));\r
1655 }\r
1656\r
1657 // reset the length of content\r
1658 $this->size = strlen($this->doc);\r
1659 if ($this->size>0)\r
1660 {\r
1661 $this->char = $this->doc[0];\r
1662 }\r
1663 }\r
1664\r
1665 // restore noise to html content\r
1666 function restore_noise($text)\r
1667 {\r
1668 global $debug_object;\r
aa126ba4 1669 if (is_object($debug_object)) { $debug_object->debug_log_entry(1); }\r
0753bfef 1670\r
1671 while (($pos=strpos($text, '___noise___'))!==false)\r
1672 {\r
1673 // Sometimes there is a broken piece of markup, and we don't GET the pos+11 etc... token which indicates a problem outside of us...\r
1674 if (strlen($text) > $pos+15)\r
1675 {\r
1676 $key = '___noise___'.$text[$pos+11].$text[$pos+12].$text[$pos+13].$text[$pos+14].$text[$pos+15];\r
aa126ba4 1677 if (is_object($debug_object)) { $debug_object->debug_log(2, 'located key of: ' . $key); }\r
0753bfef 1678\r
1679 if (isset($this->noise[$key]))\r
1680 {\r
1681 $text = substr($text, 0, $pos).$this->noise[$key].substr($text, $pos+16);\r
1682 }\r
1683 else\r
1684 {\r
1685 // do this to prevent an infinite loop.\r
1686 $text = substr($text, 0, $pos).'UNDEFINED NOISE FOR KEY: '.$key . substr($text, $pos+16);\r
1687 }\r
1688 }\r
1689 else\r
1690 {\r
1691 // There is no valid key being given back to us... We must get rid of the ___noise___ or we will have a problem.\r
1692 $text = substr($text, 0, $pos).'NO NUMERIC NOISE KEY' . substr($text, $pos+11);\r
1693 }\r
1694 }\r
1695 return $text;\r
1696 }\r
1697\r
1698 // Sometimes we NEED one of the noise elements.\r
1699 function search_noise($text)\r
1700 {\r
1701 global $debug_object;\r
aa126ba4 1702 if (is_object($debug_object)) { $debug_object->debug_log_entry(1); }\r
0753bfef 1703\r
1704 foreach($this->noise as $noiseElement)\r
1705 {\r
1706 if (strpos($noiseElement, $text)!==false)\r
1707 {\r
1708 return $noiseElement;\r
1709 }\r
1710 }\r
1711 }\r
1712 function __toString()\r
1713 {\r
1714 return $this->root->innertext();\r
1715 }\r
1716\r
1717 function __get($name)\r
1718 {\r
1719 switch ($name)\r
1720 {\r
1721 case 'outertext':\r
1722 return $this->root->innertext();\r
1723 case 'innertext':\r
1724 return $this->root->innertext();\r
1725 case 'plaintext':\r
1726 return $this->root->text();\r
1727 case 'charset':\r
1728 return $this->_charset;\r
1729 case 'target_charset':\r
1730 return $this->_target_charset;\r
1731 }\r
1732 }\r
1733\r
1734 // camel naming conventions\r
1735 function childNodes($idx=-1) {return $this->root->childNodes($idx);}\r
1736 function firstChild() {return $this->root->first_child();}\r
1737 function lastChild() {return $this->root->last_child();}\r
1738 function createElement($name, $value=null) {return @str_get_html("<$name>$value</$name>")->first_child();}\r
1739 function createTextNode($value) {return @end(str_get_html($value)->nodes);}\r
1740 function getElementById($id) {return $this->find("#$id", 0);}\r
1741 function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);}\r
1742 function getElementByTagName($name) {return $this->find($name, 0);}\r
1743 function getElementsByTagName($name, $idx=-1) {return $this->find($name, $idx);}\r
1744 function loadFile() {$args = func_get_args();$this->load_file($args);}\r
1745}\r
1746\r
1747?>