--- /dev/null
+<?php\r
+/**\r
+ * Website: http://sourceforge.net/projects/simplehtmldom/\r
+ * Additional projects that may be used: http://sourceforge.net/projects/debugobject/\r
+ * Acknowledge: Jose Solorzano (https://sourceforge.net/projects/php-html/)\r
+ * Contributions by:\r
+ * Yousuke Kumakura (Attribute filters)\r
+ * Vadim Voituk (Negative indexes supports of "find" method)\r
+ * Antcs (Constructor with automatically load contents either text or file/url)\r
+ *\r
+ * all affected sections have comments starting with "PaperG"\r
+ *\r
+ * Paperg - Added case insensitive testing of the value of the selector.\r
+ * Paperg - Added tag_start for the starting index of tags - NOTE: This works but not accurately.\r
+ * 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
+ * it will almost always be smaller by some amount.\r
+ * 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
+ * but for most purposes, it's a really good estimation.\r
+ * Paperg - Added the forceTagsClosed to the dom constructor. Forcing tags closed is great for malformed html, but it CAN lead to parsing errors.\r
+ * Allow the user to tell us how much they trust the html.\r
+ * 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
+ * This allows for us to find tags based on the text they contain.\r
+ * Create find_ancestor_tag to see if a tag is - at any level - inside of another specific tag.\r
+ * Paperg: added parse_charset so that we know about the character set of the source document.\r
+ * 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
+ * last transfer or curl_exec, and we will parse that and use it in preference to any other method of charset detection.\r
+ *\r
+ * Found infinite loop in the case of broken html in restore_noise. Rewrote to protect from that.\r
+ * PaperG (John Schlick) Added get_display_size for "IMG" tags.\r
+ *\r
+ * Licensed under The MIT License\r
+ * Redistributions of files must retain the above copyright notice.\r
+ *\r
+ * @author S.C. Chen <me578022@gmail.com>\r
+ * @author John Schlick\r
+ * @author Rus Carroll\r
+ * @version 1.5 ($Rev: 202 $)\r
+ * @package PlaceLocalInclude\r
+ * @subpackage simple_html_dom\r
+ */\r
+\r
+/**\r
+ * All of the Defines for the classes below.\r
+ * @author S.C. Chen <me578022@gmail.com>\r
+ */\r
+define('HDOM_TYPE_ELEMENT', 1);\r
+define('HDOM_TYPE_COMMENT', 2);\r
+define('HDOM_TYPE_TEXT', 3);\r
+define('HDOM_TYPE_ENDTAG', 4);\r
+define('HDOM_TYPE_ROOT', 5);\r
+define('HDOM_TYPE_UNKNOWN', 6);\r
+define('HDOM_QUOTE_DOUBLE', 0);\r
+define('HDOM_QUOTE_SINGLE', 1);\r
+define('HDOM_QUOTE_NO', 3);\r
+define('HDOM_INFO_BEGIN', 0);\r
+define('HDOM_INFO_END', 1);\r
+define('HDOM_INFO_QUOTE', 2);\r
+define('HDOM_INFO_SPACE', 3);\r
+define('HDOM_INFO_TEXT', 4);\r
+define('HDOM_INFO_INNER', 5);\r
+define('HDOM_INFO_OUTER', 6);\r
+define('HDOM_INFO_ENDSPACE',7);\r
+define('DEFAULT_TARGET_CHARSET', 'UTF-8');\r
+define('DEFAULT_BR_TEXT', "\r\n");\r
+define('DEFAULT_SPAN_TEXT', " ");\r
+define('MAX_FILE_SIZE', 600000);\r
+// helper functions\r
+// -----------------------------------------------------------------------------\r
+// get html dom from file\r
+// $maxlen is defined in the code as PHP_STREAM_COPY_ALL which is defined as -1.\r
+function 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
+{\r
+ // We DO force the tags to be terminated.\r
+ $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);\r
+ // For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done.\r
+ $contents = file_get_contents($url, $use_include_path, $context, $offset);\r
+ // Paperg - use our own mechanism for getting the contents as we want to control the timeout.\r
+ //$contents = retrieve_url_contents($url);\r
+ if (empty($contents) || strlen($contents) > MAX_FILE_SIZE)\r
+ {\r
+ return false;\r
+ }\r
+ // The second parameter can force the selectors to all be lowercase.\r
+ $dom->load($contents, $lowercase, $stripRN);\r
+ return $dom;\r
+}\r
+\r
+// get html dom from string\r
+function str_get_html($str, $lowercase=true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)\r
+{\r
+ $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);\r
+ if (empty($str) || strlen($str) > MAX_FILE_SIZE)\r
+ {\r
+ $dom->clear();\r
+ return false;\r
+ }\r
+ $dom->load($str, $lowercase, $stripRN);\r
+ return $dom;\r
+}\r
+\r
+// dump html dom tree\r
+function dump_html_tree($node, $show_attr=true, $deep=0)\r
+{\r
+ $node->dump($node);\r
+}\r
+\r
+\r
+/**\r
+ * simple html dom node\r
+ * PaperG - added ability for "find" routine to lowercase the value of the selector.\r
+ * PaperG - added $tag_start to track the start position of the tag in the total byte index\r
+ *\r
+ * @package PlaceLocalInclude\r
+ */\r
+class simple_html_dom_node\r
+{\r
+ public $nodetype = HDOM_TYPE_TEXT;\r
+ public $tag = 'text';\r
+ public $attr = array();\r
+ public $children = array();\r
+ public $nodes = array();\r
+ public $parent = null;\r
+ // The "info" array - see HDOM_INFO_... for what each element contains.\r
+ public $_ = array();\r
+ public $tag_start = 0;\r
+ private $dom = null;\r
+\r
+ function __construct($dom)\r
+ {\r
+ $this->dom = $dom;\r
+ $dom->nodes[] = $this;\r
+ }\r
+\r
+ function __destruct()\r
+ {\r
+ $this->clear();\r
+ }\r
+\r
+ function __toString()\r
+ {\r
+ return $this->outertext();\r
+ }\r
+\r
+ // clean up memory due to php5 circular references memory leak...\r
+ function clear()\r
+ {\r
+ $this->dom = null;\r
+ $this->nodes = null;\r
+ $this->parent = null;\r
+ $this->children = null;\r
+ }\r
+\r
+ // dump node's tree\r
+ function dump($show_attr=true, $deep=0)\r
+ {\r
+ $lead = str_repeat(' ', $deep);\r
+\r
+ echo $lead.$this->tag;\r
+ if ($show_attr && count($this->attr)>0)\r
+ {\r
+ echo '(';\r
+ foreach ($this->attr as $k=>$v)\r
+ echo "[$k]=>\"".$this->$k.'", ';\r
+ echo ')';\r
+ }\r
+ echo "\n";\r
+\r
+ if ($this->nodes)\r
+ {\r
+ foreach ($this->nodes as $c)\r
+ {\r
+ $c->dump($show_attr, $deep+1);\r
+ }\r
+ }\r
+ }\r
+\r
+\r
+ // Debugging function to dump a single dom node with a bunch of information about it.\r
+ function dump_node($echo=true)\r
+ {\r
+\r
+ $string = $this->tag;\r
+ if (count($this->attr)>0)\r
+ {\r
+ $string .= '(';\r
+ foreach ($this->attr as $k=>$v)\r
+ {\r
+ $string .= "[$k]=>\"".$this->$k.'", ';\r
+ }\r
+ $string .= ')';\r
+ }\r
+ if (count($this->_)>0)\r
+ {\r
+ $string .= ' $_ (';\r
+ foreach ($this->_ as $k=>$v)\r
+ {\r
+ if (is_array($v))\r
+ {\r
+ $string .= "[$k]=>(";\r
+ foreach ($v as $k2=>$v2)\r
+ {\r
+ $string .= "[$k2]=>\"".$v2.'", ';\r
+ }\r
+ $string .= ")";\r
+ } else {\r
+ $string .= "[$k]=>\"".$v.'", ';\r
+ }\r
+ }\r
+ $string .= ")";\r
+ }\r
+\r
+ if (isset($this->text))\r
+ {\r
+ $string .= " text: (" . $this->text . ")";\r
+ }\r
+\r
+ $string .= " HDOM_INNER_INFO: '";\r
+ if (isset($node->_[HDOM_INFO_INNER]))\r
+ {\r
+ $string .= $node->_[HDOM_INFO_INNER] . "'";\r
+ }\r
+ else\r
+ {\r
+ $string .= ' NULL ';\r
+ }\r
+\r
+ $string .= " children: " . count($this->children);\r
+ $string .= " nodes: " . count($this->nodes);\r
+ $string .= " tag_start: " . $this->tag_start;\r
+ $string .= "\n";\r
+\r
+ if ($echo)\r
+ {\r
+ echo $string;\r
+ return;\r
+ }\r
+ else\r
+ {\r
+ return $string;\r
+ }\r
+ }\r
+\r
+ // returns the parent of node\r
+ // If a node is passed in, it will reset the parent of the current node to that one.\r
+ function parent($parent=null)\r
+ {\r
+ // I am SURE that this doesn't work properly.\r
+ // It fails to unset the current node from it's current parents nodes or children list first.\r
+ if ($parent !== null)\r
+ {\r
+ $this->parent = $parent;\r
+ $this->parent->nodes[] = $this;\r
+ $this->parent->children[] = $this;\r
+ }\r
+\r
+ return $this->parent;\r
+ }\r
+\r
+ // verify that node has children\r
+ function has_child()\r
+ {\r
+ return !empty($this->children);\r
+ }\r
+\r
+ // returns children of node\r
+ function children($idx=-1)\r
+ {\r
+ if ($idx===-1)\r
+ {\r
+ return $this->children;\r
+ }\r
+ if (isset($this->children[$idx])) return $this->children[$idx];\r
+ return null;\r
+ }\r
+\r
+ // returns the first child of node\r
+ function first_child()\r
+ {\r
+ if (count($this->children)>0)\r
+ {\r
+ return $this->children[0];\r
+ }\r
+ return null;\r
+ }\r
+\r
+ // returns the last child of node\r
+ function last_child()\r
+ {\r
+ if (($count=count($this->children))>0)\r
+ {\r
+ return $this->children[$count-1];\r
+ }\r
+ return null;\r
+ }\r
+\r
+ // returns the next sibling of node\r
+ function next_sibling()\r
+ {\r
+ if ($this->parent===null)\r
+ {\r
+ return null;\r
+ }\r
+\r
+ $idx = 0;\r
+ $count = count($this->parent->children);\r
+ while ($idx<$count && $this!==$this->parent->children[$idx])\r
+ {\r
+ ++$idx;\r
+ }\r
+ if (++$idx>=$count)\r
+ {\r
+ return null;\r
+ }\r
+ return $this->parent->children[$idx];\r
+ }\r
+\r
+ // returns the previous sibling of node\r
+ function prev_sibling()\r
+ {\r
+ if ($this->parent===null) return null;\r
+ $idx = 0;\r
+ $count = count($this->parent->children);\r
+ while ($idx<$count && $this!==$this->parent->children[$idx])\r
+ ++$idx;\r
+ if (--$idx<0) return null;\r
+ return $this->parent->children[$idx];\r
+ }\r
+\r
+ // function to locate a specific ancestor tag in the path to the root.\r
+ function find_ancestor_tag($tag)\r
+ {\r
+ global $debug_object;\r
+ if (is_object($debug_object)) { $debug_object->debugLogEntry(1); }\r
+\r
+ // Start by including ourselves in the comparison.\r
+ $returnDom = $this;\r
+\r
+ while (!is_null($returnDom))\r
+ {\r
+ if (is_object($debug_object)) { $debug_object->debugLog(2, "Current tag is: " . $returnDom->tag); }\r
+\r
+ if ($returnDom->tag == $tag)\r
+ {\r
+ break;\r
+ }\r
+ $returnDom = $returnDom->parent;\r
+ }\r
+ return $returnDom;\r
+ }\r
+\r
+ // get dom node's inner html\r
+ function innertext()\r
+ {\r
+ if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER];\r
+ if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);\r
+\r
+ $ret = '';\r
+ foreach ($this->nodes as $n)\r
+ $ret .= $n->outertext();\r
+ return $ret;\r
+ }\r
+\r
+ // get dom node's outer text (with tag)\r
+ function outertext()\r
+ {\r
+ global $debug_object;\r
+ if (is_object($debug_object))\r
+ {\r
+ $text = '';\r
+ if ($this->tag == 'text')\r
+ {\r
+ if (!empty($this->text))\r
+ {\r
+ $text = " with text: " . $this->text;\r
+ }\r
+ }\r
+ $debug_object->debugLog(1, 'Innertext of tag: ' . $this->tag . $text);\r
+ }\r
+\r
+ if ($this->tag==='root') return $this->innertext();\r
+\r
+ // trigger callback\r
+ if ($this->dom && $this->dom->callback!==null)\r
+ {\r
+ call_user_func_array($this->dom->callback, array($this));\r
+ }\r
+\r
+ if (isset($this->_[HDOM_INFO_OUTER])) return $this->_[HDOM_INFO_OUTER];\r
+ if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);\r
+\r
+ // render begin tag\r
+ if ($this->dom && $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]])\r
+ {\r
+ $ret = $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]->makeup();\r
+ } else {\r
+ $ret = "";\r
+ }\r
+\r
+ // render inner text\r
+ if (isset($this->_[HDOM_INFO_INNER]))\r
+ {\r
+ // If it's a br tag... don't return the HDOM_INNER_INFO that we may or may not have added.\r
+ if ($this->tag != "br")\r
+ {\r
+ $ret .= $this->_[HDOM_INFO_INNER];\r
+ }\r
+ } else {\r
+ if ($this->nodes)\r
+ {\r
+ foreach ($this->nodes as $n)\r
+ {\r
+ $ret .= $this->convert_text($n->outertext());\r
+ }\r
+ }\r
+ }\r
+\r
+ // render end tag\r
+ if (isset($this->_[HDOM_INFO_END]) && $this->_[HDOM_INFO_END]!=0)\r
+ $ret .= '</'.$this->tag.'>';\r
+ return $ret;\r
+ }\r
+\r
+ // get dom node's plain text\r
+ function text()\r
+ {\r
+ if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER];\r
+ switch ($this->nodetype)\r
+ {\r
+ case HDOM_TYPE_TEXT: return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);\r
+ case HDOM_TYPE_COMMENT: return '';\r
+ case HDOM_TYPE_UNKNOWN: return '';\r
+ }\r
+ if (strcasecmp($this->tag, 'script')===0) return '';\r
+ if (strcasecmp($this->tag, 'style')===0) return '';\r
+\r
+ $ret = '';\r
+ // 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
+ // NOTE: This indicates that there is a problem where it's set to NULL without a clear happening.\r
+ // WHY is this happening?\r
+ if (!is_null($this->nodes))\r
+ {\r
+ foreach ($this->nodes as $n)\r
+ {\r
+ $ret .= $this->convert_text($n->text());\r
+ }\r
+\r
+ // 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
+ if ($this->tag == "span")\r
+ {\r
+ $ret .= $this->dom->default_span_text;\r
+ }\r
+\r
+\r
+ }\r
+ return $ret;\r
+ }\r
+\r
+ function xmltext()\r
+ {\r
+ $ret = $this->innertext();\r
+ $ret = str_ireplace('<![CDATA[', '', $ret);\r
+ $ret = str_replace(']]>', '', $ret);\r
+ return $ret;\r
+ }\r
+\r
+ // build node's text with tag\r
+ function makeup()\r
+ {\r
+ // text, comment, unknown\r
+ if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);\r
+\r
+ $ret = '<'.$this->tag;\r
+ $i = -1;\r
+\r
+ foreach ($this->attr as $key=>$val)\r
+ {\r
+ ++$i;\r
+\r
+ // skip removed attribute\r
+ if ($val===null || $val===false)\r
+ continue;\r
+\r
+ $ret .= $this->_[HDOM_INFO_SPACE][$i][0];\r
+ //no value attr: nowrap, checked selected...\r
+ if ($val===true)\r
+ $ret .= $key;\r
+ else {\r
+ switch ($this->_[HDOM_INFO_QUOTE][$i])\r
+ {\r
+ case HDOM_QUOTE_DOUBLE: $quote = '"'; break;\r
+ case HDOM_QUOTE_SINGLE: $quote = '\''; break;\r
+ default: $quote = '';\r
+ }\r
+ $ret .= $key.$this->_[HDOM_INFO_SPACE][$i][1].'='.$this->_[HDOM_INFO_SPACE][$i][2].$quote.$val.$quote;\r
+ }\r
+ }\r
+ $ret = $this->dom->restore_noise($ret);\r
+ return $ret . $this->_[HDOM_INFO_ENDSPACE] . '>';\r
+ }\r
+\r
+ // find elements by css selector\r
+ //PaperG - added ability for find to lowercase the value of the selector.\r
+ function find($selector, $idx=null, $lowercase=false)\r
+ {\r
+ $selectors = $this->parse_selector($selector);\r
+ if (($count=count($selectors))===0) return array();\r
+ $found_keys = array();\r
+\r
+ // find each selector\r
+ for ($c=0; $c<$count; ++$c)\r
+ {\r
+ // The change on the below line was documented on the sourceforge code tracker id 2788009\r
+ // used to be: if (($levle=count($selectors[0]))===0) return array();\r
+ if (($levle=count($selectors[$c]))===0) return array();\r
+ if (!isset($this->_[HDOM_INFO_BEGIN])) return array();\r
+\r
+ $head = array($this->_[HDOM_INFO_BEGIN]=>1);\r
+\r
+ // handle descendant selectors, no recursive!\r
+ for ($l=0; $l<$levle; ++$l)\r
+ {\r
+ $ret = array();\r
+ foreach ($head as $k=>$v)\r
+ {\r
+ $n = ($k===-1) ? $this->dom->root : $this->dom->nodes[$k];\r
+ //PaperG - Pass this optional parameter on to the seek function.\r
+ $n->seek($selectors[$c][$l], $ret, $lowercase);\r
+ }\r
+ $head = $ret;\r
+ }\r
+\r
+ foreach ($head as $k=>$v)\r
+ {\r
+ if (!isset($found_keys[$k]))\r
+ $found_keys[$k] = 1;\r
+ }\r
+ }\r
+\r
+ // sort keys\r
+ ksort($found_keys);\r
+\r
+ $found = array();\r
+ foreach ($found_keys as $k=>$v)\r
+ $found[] = $this->dom->nodes[$k];\r
+\r
+ // return nth-element or array\r
+ if (is_null($idx)) return $found;\r
+ else if ($idx<0) $idx = count($found) + $idx;\r
+ return (isset($found[$idx])) ? $found[$idx] : null;\r
+ }\r
+\r
+ // seek for given conditions\r
+ // PaperG - added parameter to allow for case insensitive testing of the value of a selector.\r
+ protected function seek($selector, &$ret, $lowercase=false)\r
+ {\r
+ global $debug_object;\r
+ if (is_object($debug_object)) { $debug_object->debugLogEntry(1); }\r
+\r
+ list($tag, $key, $val, $exp, $no_key) = $selector;\r
+\r
+ // xpath index\r
+ if ($tag && $key && is_numeric($key))\r
+ {\r
+ $count = 0;\r
+ foreach ($this->children as $c)\r
+ {\r
+ if ($tag==='*' || $tag===$c->tag) {\r
+ if (++$count==$key) {\r
+ $ret[$c->_[HDOM_INFO_BEGIN]] = 1;\r
+ return;\r
+ }\r
+ }\r
+ }\r
+ return;\r
+ }\r
+\r
+ $end = (!empty($this->_[HDOM_INFO_END])) ? $this->_[HDOM_INFO_END] : 0;\r
+ if ($end==0) {\r
+ $parent = $this->parent;\r
+ while (!isset($parent->_[HDOM_INFO_END]) && $parent!==null) {\r
+ $end -= 1;\r
+ $parent = $parent->parent;\r
+ }\r
+ $end += $parent->_[HDOM_INFO_END];\r
+ }\r
+\r
+ for ($i=$this->_[HDOM_INFO_BEGIN]+1; $i<$end; ++$i) {\r
+ $node = $this->dom->nodes[$i];\r
+\r
+ $pass = true;\r
+\r
+ if ($tag==='*' && !$key) {\r
+ if (in_array($node, $this->children, true))\r
+ $ret[$i] = 1;\r
+ continue;\r
+ }\r
+\r
+ // compare tag\r
+ if ($tag && $tag!=$node->tag && $tag!=='*') {$pass=false;}\r
+ // compare key\r
+ if ($pass && $key) {\r
+ if ($no_key) {\r
+ if (isset($node->attr[$key])) $pass=false;\r
+ } else {\r
+ if (($key != "plaintext") && !isset($node->attr[$key])) $pass=false;\r
+ }\r
+ }\r
+ // compare value\r
+ if ($pass && $key && $val && $val!=='*') {\r
+ // If they have told us that this is a "plaintext" search then we want the plaintext of the node - right?\r
+ if ($key == "plaintext") {\r
+ // $node->plaintext actually returns $node->text();\r
+ $nodeKeyValue = $node->text();\r
+ } else {\r
+ // this is a normal search, we want the value of that attribute of the tag.\r
+ $nodeKeyValue = $node->attr[$key];\r
+ }\r
+ if (is_object($debug_object)) {$debug_object->debugLog(2, "testing node: " . $node->tag . " for attribute: " . $key . $exp . $val . " where nodes value is: " . $nodeKeyValue);}\r
+\r
+ //PaperG - If lowercase is set, do a case insensitive test of the value of the selector.\r
+ if ($lowercase) {\r
+ $check = $this->match($exp, strtolower($val), strtolower($nodeKeyValue));\r
+ } else {\r
+ $check = $this->match($exp, $val, $nodeKeyValue);\r
+ }\r
+ if (is_object($debug_object)) {$debug_object->debugLog(2, "after match: " . ($check ? "true" : "false"));}\r
+\r
+ // handle multiple class\r
+ if (!$check && strcasecmp($key, 'class')===0) {\r
+ foreach (explode(' ',$node->attr[$key]) as $k) {\r
+ // Without this, there were cases where leading, trailing, or double spaces lead to our comparing blanks - bad form.\r
+ if (!empty($k)) {\r
+ if ($lowercase) {\r
+ $check = $this->match($exp, strtolower($val), strtolower($k));\r
+ } else {\r
+ $check = $this->match($exp, $val, $k);\r
+ }\r
+ if ($check) break;\r
+ }\r
+ }\r
+ }\r
+ if (!$check) $pass = false;\r
+ }\r
+ if ($pass) $ret[$i] = 1;\r
+ unset($node);\r
+ }\r
+ // It's passed by reference so this is actually what this function returns.\r
+ if (is_object($debug_object)) {$debug_object->debugLog(1, "EXIT - ret: ", $ret);}\r
+ }\r
+\r
+ protected function match($exp, $pattern, $value) {\r
+ global $debug_object;\r
+ if (is_object($debug_object)) {$debug_object->debugLogEntry(1);}\r
+\r
+ switch ($exp) {\r
+ case '=':\r
+ return ($value===$pattern);\r
+ case '!=':\r
+ return ($value!==$pattern);\r
+ case '^=':\r
+ return preg_match("/^".preg_quote($pattern,'/')."/", $value);\r
+ case '$=':\r
+ return preg_match("/".preg_quote($pattern,'/')."$/", $value);\r
+ case '*=':\r
+ if ($pattern[0]=='/') {\r
+ return preg_match($pattern, $value);\r
+ }\r
+ return preg_match("/".$pattern."/i", $value);\r
+ }\r
+ return false;\r
+ }\r
+\r
+ protected function parse_selector($selector_string) {\r
+ global $debug_object;\r
+ if (is_object($debug_object)) {$debug_object->debugLogEntry(1);}\r
+\r
+ // pattern of CSS selectors, modified from mootools\r
+ // Paperg: Add the colon to the attrbute, so that it properly finds <tag attr:ibute="something" > like google does.\r
+ // Note: if you try to look at this attribute, yo MUST use getAttribute since $dom->x:y will fail the php syntax check.\r
+// Notice the \[ starting the attbute? and the @? following? This implies that an attribute can begin with an @ sign that is not captured.\r
+// This implies that an html attribute specifier may start with an @ sign that is NOT captured by the expression.\r
+// farther study is required to determine of this should be documented or removed.\r
+// $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is";\r
+ $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-:]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is";\r
+ preg_match_all($pattern, trim($selector_string).' ', $matches, PREG_SET_ORDER);\r
+ if (is_object($debug_object)) {$debug_object->debugLog(2, "Matches Array: ", $matches);}\r
+\r
+ $selectors = array();\r
+ $result = array();\r
+ //print_r($matches);\r
+\r
+ foreach ($matches as $m) {\r
+ $m[0] = trim($m[0]);\r
+ if ($m[0]==='' || $m[0]==='/' || $m[0]==='//') continue;\r
+ // for browser generated xpath\r
+ if ($m[1]==='tbody') continue;\r
+\r
+ list($tag, $key, $val, $exp, $no_key) = array($m[1], null, null, '=', false);\r
+ if (!empty($m[2])) {$key='id'; $val=$m[2];}\r
+ if (!empty($m[3])) {$key='class'; $val=$m[3];}\r
+ if (!empty($m[4])) {$key=$m[4];}\r
+ if (!empty($m[5])) {$exp=$m[5];}\r
+ if (!empty($m[6])) {$val=$m[6];}\r
+\r
+ // convert to lowercase\r
+ if ($this->dom->lowercase) {$tag=strtolower($tag); $key=strtolower($key);}\r
+ //elements that do NOT have the specified attribute\r
+ if (isset($key[0]) && $key[0]==='!') {$key=substr($key, 1); $no_key=true;}\r
+\r
+ $result[] = array($tag, $key, $val, $exp, $no_key);\r
+ if (trim($m[7])===',') {\r
+ $selectors[] = $result;\r
+ $result = array();\r
+ }\r
+ }\r
+ if (count($result)>0)\r
+ $selectors[] = $result;\r
+ return $selectors;\r
+ }\r
+\r
+ function __get($name) {\r
+ if (isset($this->attr[$name]))\r
+ {\r
+ return $this->convert_text($this->attr[$name]);\r
+ }\r
+ switch ($name) {\r
+ case 'outertext': return $this->outertext();\r
+ case 'innertext': return $this->innertext();\r
+ case 'plaintext': return $this->text();\r
+ case 'xmltext': return $this->xmltext();\r
+ default: return array_key_exists($name, $this->attr);\r
+ }\r
+ }\r
+\r
+ function __set($name, $value) {\r
+ switch ($name) {\r
+ case 'outertext': return $this->_[HDOM_INFO_OUTER] = $value;\r
+ case 'innertext':\r
+ if (isset($this->_[HDOM_INFO_TEXT])) return $this->_[HDOM_INFO_TEXT] = $value;\r
+ return $this->_[HDOM_INFO_INNER] = $value;\r
+ }\r
+ if (!isset($this->attr[$name])) {\r
+ $this->_[HDOM_INFO_SPACE][] = array(' ', '', '');\r
+ $this->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;\r
+ }\r
+ $this->attr[$name] = $value;\r
+ }\r
+\r
+ function __isset($name) {\r
+ switch ($name) {\r
+ case 'outertext': return true;\r
+ case 'innertext': return true;\r
+ case 'plaintext': return true;\r
+ }\r
+ //no value attr: nowrap, checked selected...\r
+ return (array_key_exists($name, $this->attr)) ? true : isset($this->attr[$name]);\r
+ }\r
+\r
+ function __unset($name) {\r
+ if (isset($this->attr[$name]))\r
+ unset($this->attr[$name]);\r
+ }\r
+\r
+ // PaperG - Function to convert the text from one character set to another if the two sets are not the same.\r
+ function convert_text($text)\r
+ {\r
+ global $debug_object;\r
+ if (is_object($debug_object)) {$debug_object->debugLogEntry(1);}\r
+\r
+ $converted_text = $text;\r
+\r
+ $sourceCharset = "";\r
+ $targetCharset = "";\r
+\r
+ if ($this->dom)\r
+ {\r
+ $sourceCharset = strtoupper($this->dom->_charset);\r
+ $targetCharset = strtoupper($this->dom->_target_charset);\r
+ }\r
+ if (is_object($debug_object)) {$debug_object->debugLog(3, "source charset: " . $sourceCharset . " target charaset: " . $targetCharset);}\r
+\r
+ if (!empty($sourceCharset) && !empty($targetCharset) && (strcasecmp($sourceCharset, $targetCharset) != 0))\r
+ {\r
+ // Check if the reported encoding could have been incorrect and the text is actually already UTF-8\r
+ if ((strcasecmp($targetCharset, 'UTF-8') == 0) && ($this->is_utf8($text)))\r
+ {\r
+ $converted_text = $text;\r
+ }\r
+ else\r
+ {\r
+ $converted_text = iconv($sourceCharset, $targetCharset, $text);\r
+ }\r
+ }\r
+\r
+ // Lets make sure that we don't have that silly BOM issue with any of the utf-8 text we output.\r
+ if ($targetCharset == 'UTF-8')\r
+ {\r
+ if (substr($converted_text, 0, 3) == "\xef\xbb\xbf")\r
+ {\r
+ $converted_text = substr($converted_text, 3);\r
+ }\r
+ if (substr($converted_text, -3) == "\xef\xbb\xbf")\r
+ {\r
+ $converted_text = substr($converted_text, 0, -3);\r
+ }\r
+ }\r
+\r
+ return $converted_text;\r
+ }\r
+\r
+ /**\r
+ * Returns true if $string is valid UTF-8 and false otherwise.\r
+ *\r
+ * @param mixed $str String to be tested\r
+ * @return boolean\r
+ */\r
+ static function is_utf8($str)\r
+ {\r
+ $c=0; $b=0;\r
+ $bits=0;\r
+ $len=strlen($str);\r
+ for($i=0; $i<$len; $i++)\r
+ {\r
+ $c=ord($str[$i]);\r
+ if($c > 128)\r
+ {\r
+ if(($c >= 254)) return false;\r
+ elseif($c >= 252) $bits=6;\r
+ elseif($c >= 248) $bits=5;\r
+ elseif($c >= 240) $bits=4;\r
+ elseif($c >= 224) $bits=3;\r
+ elseif($c >= 192) $bits=2;\r
+ else return false;\r
+ if(($i+$bits) > $len) return false;\r
+ while($bits > 1)\r
+ {\r
+ $i++;\r
+ $b=ord($str[$i]);\r
+ if($b < 128 || $b > 191) return false;\r
+ $bits--;\r
+ }\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+ /*\r
+ function is_utf8($string)\r
+ {\r
+ //this is buggy\r
+ return (utf8_encode(utf8_decode($string)) == $string);\r
+ }\r
+ */\r
+\r
+ /**\r
+ * Function to try a few tricks to determine the displayed size of an img on the page.\r
+ * NOTE: This will ONLY work on an IMG tag. Returns FALSE on all other tag types.\r
+ *\r
+ * @author John Schlick\r
+ * @version April 19 2012\r
+ * @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
+ */\r
+ function get_display_size()\r
+ {\r
+ global $debug_object;\r
+\r
+ $width = -1;\r
+ $height = -1;\r
+\r
+ if ($this->tag !== 'img')\r
+ {\r
+ return false;\r
+ }\r
+\r
+ // See if there is aheight or width attribute in the tag itself.\r
+ if (isset($this->attr['width']))\r
+ {\r
+ $width = $this->attr['width'];\r
+ }\r
+\r
+ if (isset($this->attr['height']))\r
+ {\r
+ $height = $this->attr['height'];\r
+ }\r
+\r
+ // Now look for an inline style.\r
+ if (isset($this->attr['style']))\r
+ {\r
+ // Thanks to user gnarf from stackoverflow for this regular expression.\r
+ $attributes = array();\r
+ preg_match_all("/([\w-]+)\s*:\s*([^;]+)\s*;?/", $this->attr['style'], $matches, PREG_SET_ORDER);\r
+ foreach ($matches as $match) {\r
+ $attributes[$match[1]] = $match[2];\r
+ }\r
+\r
+ // If there is a width in the style attributes:\r
+ if (isset($attributes['width']) && $width == -1)\r
+ {\r
+ // check that the last two characters are px (pixels)\r
+ if (strtolower(substr($attributes['width'], -2)) == 'px')\r
+ {\r
+ $proposed_width = substr($attributes['width'], 0, -2);\r
+ // Now make sure that it's an integer and not something stupid.\r
+ if (filter_var($proposed_width, FILTER_VALIDATE_INT))\r
+ {\r
+ $width = $proposed_width;\r
+ }\r
+ }\r
+ }\r
+\r
+ // If there is a width in the style attributes:\r
+ if (isset($attributes['height']) && $height == -1)\r
+ {\r
+ // check that the last two characters are px (pixels)\r
+ if (strtolower(substr($attributes['height'], -2)) == 'px')\r
+ {\r
+ $proposed_height = substr($attributes['height'], 0, -2);\r
+ // Now make sure that it's an integer and not something stupid.\r
+ if (filter_var($proposed_height, FILTER_VALIDATE_INT))\r
+ {\r
+ $height = $proposed_height;\r
+ }\r
+ }\r
+ }\r
+\r
+ }\r
+\r
+ // Future enhancement:\r
+ // Look in the tag to see if there is a class or id specified that has a height or width attribute to it.\r
+\r
+ // Far future enhancement\r
+ // 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
+ // Note that in this case, the class or id will have the img subselector for it to apply to the image.\r
+\r
+ // ridiculously far future development\r
+ // 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
+\r
+ $result = array('height' => $height,\r
+ 'width' => $width);\r
+ return $result;\r
+ }\r
+\r
+ // camel naming conventions\r
+ function getAllAttributes() {return $this->attr;}\r
+ function getAttribute($name) {return $this->__get($name);}\r
+ function setAttribute($name, $value) {$this->__set($name, $value);}\r
+ function hasAttribute($name) {return $this->__isset($name);}\r
+ function removeAttribute($name) {$this->__set($name, null);}\r
+ function getElementById($id) {return $this->find("#$id", 0);}\r
+ function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);}\r
+ function getElementByTagName($name) {return $this->find($name, 0);}\r
+ function getElementsByTagName($name, $idx=null) {return $this->find($name, $idx);}\r
+ function parentNode() {return $this->parent();}\r
+ function childNodes($idx=-1) {return $this->children($idx);}\r
+ function firstChild() {return $this->first_child();}\r
+ function lastChild() {return $this->last_child();}\r
+ function nextSibling() {return $this->next_sibling();}\r
+ function previousSibling() {return $this->prev_sibling();}\r
+ function hasChildNodes() {return $this->has_child();}\r
+ function nodeName() {return $this->tag;}\r
+ function appendChild($node) {$node->parent($this); return $node;}\r
+\r
+}\r
+\r
+/**\r
+ * simple html dom parser\r
+ * Paperg - in the find routine: allow us to specify that we want case insensitive testing of the value of the selector.\r
+ * Paperg - change $size from protected to public so we can easily access it\r
+ * Paperg - added ForceTagsClosed in the constructor which tells us whether we trust the html or not. Default is to NOT trust it.\r
+ *\r
+ * @package PlaceLocalInclude\r
+ */\r
+class simple_html_dom\r
+{\r
+ public $root = null;\r
+ public $nodes = array();\r
+ public $callback = null;\r
+ public $lowercase = false;\r
+ // Used to keep track of how large the text was when we started.\r
+ public $original_size;\r
+ public $size;\r
+ protected $pos;\r
+ protected $doc;\r
+ protected $char;\r
+ protected $cursor;\r
+ protected $parent;\r
+ protected $noise = array();\r
+ protected $token_blank = " \t\r\n";\r
+ protected $token_equal = ' =/>';\r
+ protected $token_slash = " />\r\n\t";\r
+ protected $token_attr = ' >';\r
+ // Note that this is referenced by a child node, and so it needs to be public for that node to see this information.\r
+ public $_charset = '';\r
+ public $_target_charset = '';\r
+ protected $default_br_text = "";\r
+ public $default_span_text = "";\r
+\r
+ // use isset instead of in_array, performance boost about 30%...\r
+ protected $self_closing_tags = array('img'=>1, 'br'=>1, 'input'=>1, 'meta'=>1, 'link'=>1, 'hr'=>1, 'base'=>1, 'embed'=>1, 'spacer'=>1);\r
+ protected $block_tags = array('root'=>1, 'body'=>1, 'form'=>1, 'div'=>1, 'span'=>1, 'table'=>1);\r
+ // Known sourceforge issue #2977341\r
+ // B tags that are not closed cause us to return everything to the end of the document.\r
+ protected $optional_closing_tags = array(\r
+ 'tr'=>array('tr'=>1, 'td'=>1, 'th'=>1),\r
+ 'th'=>array('th'=>1),\r
+ 'td'=>array('td'=>1),\r
+ 'li'=>array('li'=>1),\r
+ 'dt'=>array('dt'=>1, 'dd'=>1),\r
+ 'dd'=>array('dd'=>1, 'dt'=>1),\r
+ 'dl'=>array('dd'=>1, 'dt'=>1),\r
+ 'p'=>array('p'=>1),\r
+ 'nobr'=>array('nobr'=>1),\r
+ 'b'=>array('b'=>1),\r
+ 'option'=>array('option'=>1),\r
+ );\r
+\r
+ function __construct($str=null, $lowercase=true, $forceTagsClosed=true, $target_charset=DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)\r
+ {\r
+ if ($str)\r
+ {\r
+ if (preg_match("/^http:\/\//i",$str) || is_file($str))\r
+ {\r
+ $this->load_file($str);\r
+ }\r
+ else\r
+ {\r
+ $this->load($str, $lowercase, $stripRN, $defaultBRText, $defaultSpanText);\r
+ }\r
+ }\r
+ // 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
+ if (!$forceTagsClosed) {\r
+ $this->optional_closing_array=array();\r
+ }\r
+ $this->_target_charset = $target_charset;\r
+ }\r
+\r
+ function __destruct()\r
+ {\r
+ $this->clear();\r
+ }\r
+\r
+ // load html from string\r
+ function load($str, $lowercase=true, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)\r
+ {\r
+ global $debug_object;\r
+\r
+ // prepare\r
+ $this->prepare($str, $lowercase, $stripRN, $defaultBRText, $defaultSpanText);\r
+ // strip out comments\r
+ $this->remove_noise("'<!--(.*?)-->'is");\r
+ // strip out cdata\r
+ $this->remove_noise("'<!\[CDATA\[(.*?)\]\]>'is", true);\r
+ // Per sourceforge http://sourceforge.net/tracker/?func=detail&aid=2949097&group_id=218559&atid=1044037\r
+ // Script tags removal now preceeds style tag removal.\r
+ // strip out <script> tags\r
+ $this->remove_noise("'<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>'is");\r
+ $this->remove_noise("'<\s*script\s*>(.*?)<\s*/\s*script\s*>'is");\r
+ // strip out <style> tags\r
+ $this->remove_noise("'<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>'is");\r
+ $this->remove_noise("'<\s*style\s*>(.*?)<\s*/\s*style\s*>'is");\r
+ // strip out preformatted tags\r
+ $this->remove_noise("'<\s*(?:code)[^>]*>(.*?)<\s*/\s*(?:code)\s*>'is");\r
+ // strip out server side scripts\r
+ $this->remove_noise("'(<\?)(.*?)(\?>)'s", true);\r
+ // strip smarty scripts\r
+ $this->remove_noise("'(\{\w)(.*?)(\})'s", true);\r
+\r
+ // parsing\r
+ while ($this->parse());\r
+ // end\r
+ $this->root->_[HDOM_INFO_END] = $this->cursor;\r
+ $this->parse_charset();\r
+\r
+ // make load function chainable\r
+ return $this;\r
+\r
+ }\r
+\r
+ // load html from file\r
+ function load_file()\r
+ {\r
+ $args = func_get_args();\r
+ $this->load(call_user_func_array('file_get_contents', $args), true);\r
+ // Throw an error if we can't properly load the dom.\r
+ if (($error=error_get_last())!==null) {\r
+ $this->clear();\r
+ return false;\r
+ }\r
+ }\r
+\r
+ // set callback function\r
+ function set_callback($function_name)\r
+ {\r
+ $this->callback = $function_name;\r
+ }\r
+\r
+ // remove callback function\r
+ function remove_callback()\r
+ {\r
+ $this->callback = null;\r
+ }\r
+\r
+ // save dom as string\r
+ function save($filepath='')\r
+ {\r
+ $ret = $this->root->innertext();\r
+ if ($filepath!=='') file_put_contents($filepath, $ret, LOCK_EX);\r
+ return $ret;\r
+ }\r
+\r
+ // find dom node by css selector\r
+ // Paperg - allow us to specify that we want case insensitive testing of the value of the selector.\r
+ function find($selector, $idx=null, $lowercase=false)\r
+ {\r
+ return $this->root->find($selector, $idx, $lowercase);\r
+ }\r
+\r
+ // clean up memory due to php5 circular references memory leak...\r
+ function clear()\r
+ {\r
+ foreach ($this->nodes as $n) {$n->clear(); $n = null;}\r
+ // 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
+ if (isset($this->children)) foreach ($this->children as $n) {$n->clear(); $n = null;}\r
+ if (isset($this->parent)) {$this->parent->clear(); unset($this->parent);}\r
+ if (isset($this->root)) {$this->root->clear(); unset($this->root);}\r
+ unset($this->doc);\r
+ unset($this->noise);\r
+ }\r
+\r
+ function dump($show_attr=true)\r
+ {\r
+ $this->root->dump($show_attr);\r
+ }\r
+\r
+ // prepare HTML data and init everything\r
+ protected function prepare($str, $lowercase=true, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)\r
+ {\r
+ $this->clear();\r
+\r
+ // set the length of content before we do anything to it.\r
+ $this->size = strlen($str);\r
+ // Save the original size of the html that we got in. It might be useful to someone.\r
+ $this->original_size = $this->size;\r
+\r
+ //before we save the string as the doc... strip out the \r \n's if we are told to.\r
+ if ($stripRN) {\r
+ $str = str_replace("\r", " ", $str);\r
+ $str = str_replace("\n", " ", $str);\r
+\r
+ // set the length of content since we have changed it.\r
+ $this->size = strlen($str);\r
+ }\r
+\r
+ $this->doc = $str;\r
+ $this->pos = 0;\r
+ $this->cursor = 1;\r
+ $this->noise = array();\r
+ $this->nodes = array();\r
+ $this->lowercase = $lowercase;\r
+ $this->default_br_text = $defaultBRText;\r
+ $this->default_span_text = $defaultSpanText;\r
+ $this->root = new simple_html_dom_node($this);\r
+ $this->root->tag = 'root';\r
+ $this->root->_[HDOM_INFO_BEGIN] = -1;\r
+ $this->root->nodetype = HDOM_TYPE_ROOT;\r
+ $this->parent = $this->root;\r
+ if ($this->size>0) $this->char = $this->doc[0];\r
+ }\r
+\r
+ // parse html content\r
+ protected function parse()\r
+ {\r
+ if (($s = $this->copy_until_char('<'))==='')\r
+ {\r
+ return $this->read_tag();\r
+ }\r
+\r
+ // text\r
+ $node = new simple_html_dom_node($this);\r
+ ++$this->cursor;\r
+ $node->_[HDOM_INFO_TEXT] = $s;\r
+ $this->link_nodes($node, false);\r
+ return true;\r
+ }\r
+\r
+ // 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
+ // 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
+ // (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
+ protected function parse_charset()\r
+ {\r
+ global $debug_object;\r
+\r
+ $charset = null;\r
+\r
+ if (function_exists('get_last_retrieve_url_contents_content_type'))\r
+ {\r
+ $contentTypeHeader = get_last_retrieve_url_contents_content_type();\r
+ $success = preg_match('/charset=(.+)/', $contentTypeHeader, $matches);\r
+ if ($success)\r
+ {\r
+ $charset = $matches[1];\r
+ if (is_object($debug_object)) {$debug_object->debugLog(2, 'header content-type found charset of: ' . $charset);}\r
+ }\r
+\r
+ }\r
+\r
+ if (empty($charset))\r
+ {\r
+ $el = $this->root->find('meta[http-equiv=Content-Type]',0);\r
+ if (!empty($el))\r
+ {\r
+ $fullvalue = $el->content;\r
+ if (is_object($debug_object)) {$debug_object->debugLog(2, 'meta content-type tag found' . $fullvalue);}\r
+\r
+ if (!empty($fullvalue))\r
+ {\r
+ $success = preg_match('/charset=(.+)/', $fullvalue, $matches);\r
+ if ($success)\r
+ {\r
+ $charset = $matches[1];\r
+ }\r
+ else\r
+ {\r
+ // If there is a meta tag, and they don't specify the character set, research says that it's typically ISO-8859-1\r
+ if (is_object($debug_object)) {$debug_object->debugLog(2, 'meta content-type tag couldn\'t be parsed. using iso-8859 default.');}\r
+ $charset = 'ISO-8859-1';\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ // If we couldn't find a charset above, then lets try to detect one based on the text we got...\r
+ if (empty($charset))\r
+ {\r
+ // Have php try to detect the encoding from the text given to us.\r
+ $charset = mb_detect_encoding($this->root->plaintext . "ascii", $encoding_list = array( "UTF-8", "CP1252" ) );\r
+ if (is_object($debug_object)) {$debug_object->debugLog(2, 'mb_detect found: ' . $charset);}\r
+\r
+ // 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
+ if ($charset === false)\r
+ {\r
+ if (is_object($debug_object)) {$debug_object->debugLog(2, 'since mb_detect failed - using default of utf-8');}\r
+ $charset = 'UTF-8';\r
+ }\r
+ }\r
+\r
+ // Since CP1252 is a superset, if we get one of it's subsets, we want it instead.\r
+ if ((strtolower($charset) == strtolower('ISO-8859-1')) || (strtolower($charset) == strtolower('Latin1')) || (strtolower($charset) == strtolower('Latin-1')))\r
+ {\r
+ if (is_object($debug_object)) {$debug_object->debugLog(2, 'replacing ' . $charset . ' with CP1252 as its a superset');}\r
+ $charset = 'CP1252';\r
+ }\r
+\r
+ if (is_object($debug_object)) {$debug_object->debugLog(1, 'EXIT - ' . $charset);}\r
+\r
+ return $this->_charset = $charset;\r
+ }\r
+\r
+ // read tag info\r
+ protected function read_tag()\r
+ {\r
+ if ($this->char!=='<')\r
+ {\r
+ $this->root->_[HDOM_INFO_END] = $this->cursor;\r
+ return false;\r
+ }\r
+ $begin_tag_pos = $this->pos;\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+\r
+ // end tag\r
+ if ($this->char==='/')\r
+ {\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ // This represents the change in the simple_html_dom trunk from revision 180 to 181.\r
+ // $this->skip($this->token_blank_t);\r
+ $this->skip($this->token_blank);\r
+ $tag = $this->copy_until_char('>');\r
+\r
+ // skip attributes in end tag\r
+ if (($pos = strpos($tag, ' '))!==false)\r
+ $tag = substr($tag, 0, $pos);\r
+\r
+ $parent_lower = strtolower($this->parent->tag);\r
+ $tag_lower = strtolower($tag);\r
+\r
+ if ($parent_lower!==$tag_lower)\r
+ {\r
+ if (isset($this->optional_closing_tags[$parent_lower]) && isset($this->block_tags[$tag_lower]))\r
+ {\r
+ $this->parent->_[HDOM_INFO_END] = 0;\r
+ $org_parent = $this->parent;\r
+\r
+ while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower)\r
+ $this->parent = $this->parent->parent;\r
+\r
+ if (strtolower($this->parent->tag)!==$tag_lower) {\r
+ $this->parent = $org_parent; // restore origonal parent\r
+ if ($this->parent->parent) $this->parent = $this->parent->parent;\r
+ $this->parent->_[HDOM_INFO_END] = $this->cursor;\r
+ return $this->as_text_node($tag);\r
+ }\r
+ }\r
+ else if (($this->parent->parent) && isset($this->block_tags[$tag_lower]))\r
+ {\r
+ $this->parent->_[HDOM_INFO_END] = 0;\r
+ $org_parent = $this->parent;\r
+\r
+ while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower)\r
+ $this->parent = $this->parent->parent;\r
+\r
+ if (strtolower($this->parent->tag)!==$tag_lower)\r
+ {\r
+ $this->parent = $org_parent; // restore origonal parent\r
+ $this->parent->_[HDOM_INFO_END] = $this->cursor;\r
+ return $this->as_text_node($tag);\r
+ }\r
+ }\r
+ else if (($this->parent->parent) && strtolower($this->parent->parent->tag)===$tag_lower)\r
+ {\r
+ $this->parent->_[HDOM_INFO_END] = 0;\r
+ $this->parent = $this->parent->parent;\r
+ }\r
+ else\r
+ return $this->as_text_node($tag);\r
+ }\r
+\r
+ $this->parent->_[HDOM_INFO_END] = $this->cursor;\r
+ if ($this->parent->parent) $this->parent = $this->parent->parent;\r
+\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ return true;\r
+ }\r
+\r
+ $node = new simple_html_dom_node($this);\r
+ $node->_[HDOM_INFO_BEGIN] = $this->cursor;\r
+ ++$this->cursor;\r
+ $tag = $this->copy_until($this->token_slash);\r
+ $node->tag_start = $begin_tag_pos;\r
+\r
+ // doctype, cdata & comments...\r
+ if (isset($tag[0]) && $tag[0]==='!') {\r
+ $node->_[HDOM_INFO_TEXT] = '<' . $tag . $this->copy_until_char('>');\r
+\r
+ if (isset($tag[2]) && $tag[1]==='-' && $tag[2]==='-') {\r
+ $node->nodetype = HDOM_TYPE_COMMENT;\r
+ $node->tag = 'comment';\r
+ } else {\r
+ $node->nodetype = HDOM_TYPE_UNKNOWN;\r
+ $node->tag = 'unknown';\r
+ }\r
+ if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>';\r
+ $this->link_nodes($node, true);\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ return true;\r
+ }\r
+\r
+ // text\r
+ if ($pos=strpos($tag, '<')!==false) {\r
+ $tag = '<' . substr($tag, 0, -1);\r
+ $node->_[HDOM_INFO_TEXT] = $tag;\r
+ $this->link_nodes($node, false);\r
+ $this->char = $this->doc[--$this->pos]; // prev\r
+ return true;\r
+ }\r
+\r
+ if (!preg_match("/^[\w-:]+$/", $tag)) {\r
+ $node->_[HDOM_INFO_TEXT] = '<' . $tag . $this->copy_until('<>');\r
+ if ($this->char==='<') {\r
+ $this->link_nodes($node, false);\r
+ return true;\r
+ }\r
+\r
+ if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>';\r
+ $this->link_nodes($node, false);\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ return true;\r
+ }\r
+\r
+ // begin tag\r
+ $node->nodetype = HDOM_TYPE_ELEMENT;\r
+ $tag_lower = strtolower($tag);\r
+ $node->tag = ($this->lowercase) ? $tag_lower : $tag;\r
+\r
+ // handle optional closing tags\r
+ if (isset($this->optional_closing_tags[$tag_lower]) )\r
+ {\r
+ while (isset($this->optional_closing_tags[$tag_lower][strtolower($this->parent->tag)]))\r
+ {\r
+ $this->parent->_[HDOM_INFO_END] = 0;\r
+ $this->parent = $this->parent->parent;\r
+ }\r
+ $node->parent = $this->parent;\r
+ }\r
+\r
+ $guard = 0; // prevent infinity loop\r
+ $space = array($this->copy_skip($this->token_blank), '', '');\r
+\r
+ // attributes\r
+ do\r
+ {\r
+ if ($this->char!==null && $space[0]==='')\r
+ {\r
+ break;\r
+ }\r
+ $name = $this->copy_until($this->token_equal);\r
+ if ($guard===$this->pos)\r
+ {\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ continue;\r
+ }\r
+ $guard = $this->pos;\r
+\r
+ // handle endless '<'\r
+ if ($this->pos>=$this->size-1 && $this->char!=='>') {\r
+ $node->nodetype = HDOM_TYPE_TEXT;\r
+ $node->_[HDOM_INFO_END] = 0;\r
+ $node->_[HDOM_INFO_TEXT] = '<'.$tag . $space[0] . $name;\r
+ $node->tag = 'text';\r
+ $this->link_nodes($node, false);\r
+ return true;\r
+ }\r
+\r
+ // handle mismatch '<'\r
+ if ($this->doc[$this->pos-1]=='<') {\r
+ $node->nodetype = HDOM_TYPE_TEXT;\r
+ $node->tag = 'text';\r
+ $node->attr = array();\r
+ $node->_[HDOM_INFO_END] = 0;\r
+ $node->_[HDOM_INFO_TEXT] = substr($this->doc, $begin_tag_pos, $this->pos-$begin_tag_pos-1);\r
+ $this->pos -= 2;\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ $this->link_nodes($node, false);\r
+ return true;\r
+ }\r
+\r
+ if ($name!=='/' && $name!=='') {\r
+ $space[1] = $this->copy_skip($this->token_blank);\r
+ $name = $this->restore_noise($name);\r
+ if ($this->lowercase) $name = strtolower($name);\r
+ if ($this->char==='=') {\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ $this->parse_attr($node, $name, $space);\r
+ }\r
+ else {\r
+ //no value attr: nowrap, checked selected...\r
+ $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;\r
+ $node->attr[$name] = true;\r
+ if ($this->char!='>') $this->char = $this->doc[--$this->pos]; // prev\r
+ }\r
+ $node->_[HDOM_INFO_SPACE][] = $space;\r
+ $space = array($this->copy_skip($this->token_blank), '', '');\r
+ }\r
+ else\r
+ break;\r
+ } while ($this->char!=='>' && $this->char!=='/');\r
+\r
+ $this->link_nodes($node, true);\r
+ $node->_[HDOM_INFO_ENDSPACE] = $space[0];\r
+\r
+ // check self closing\r
+ if ($this->copy_until_char_escape('>')==='/')\r
+ {\r
+ $node->_[HDOM_INFO_ENDSPACE] .= '/';\r
+ $node->_[HDOM_INFO_END] = 0;\r
+ }\r
+ else\r
+ {\r
+ // reset parent\r
+ if (!isset($this->self_closing_tags[strtolower($node->tag)])) $this->parent = $node;\r
+ }\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+\r
+ // If it's a BR tag, we need to set it's text to the default text.\r
+ // This way when we see it in plaintext, we can generate formatting that the user wants.\r
+ // since a br tag never has sub nodes, this works well.\r
+ if ($node->tag == "br")\r
+ {\r
+ $node->_[HDOM_INFO_INNER] = $this->default_br_text;\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+ // parse attributes\r
+ protected function parse_attr($node, $name, &$space)\r
+ {\r
+ // Per sourceforge: http://sourceforge.net/tracker/?func=detail&aid=3061408&group_id=218559&atid=1044037\r
+ // If the attribute is already defined inside a tag, only pay atetntion to the first one as opposed to the last one.\r
+ if (isset($node->attr[$name]))\r
+ {\r
+ return;\r
+ }\r
+\r
+ $space[2] = $this->copy_skip($this->token_blank);\r
+ switch ($this->char) {\r
+ case '"':\r
+ $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ $node->attr[$name] = $this->restore_noise($this->copy_until_char_escape('"'));\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ break;\r
+ case '\'':\r
+ $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_SINGLE;\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ $node->attr[$name] = $this->restore_noise($this->copy_until_char_escape('\''));\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ break;\r
+ default:\r
+ $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;\r
+ $node->attr[$name] = $this->restore_noise($this->copy_until($this->token_attr));\r
+ }\r
+ // PaperG: Attributes should not have \r or \n in them, that counts as html whitespace.\r
+ $node->attr[$name] = str_replace("\r", "", $node->attr[$name]);\r
+ $node->attr[$name] = str_replace("\n", "", $node->attr[$name]);\r
+ // 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
+ if ($name == "class") {\r
+ $node->attr[$name] = trim($node->attr[$name]);\r
+ }\r
+ }\r
+\r
+ // link node's parent\r
+ protected function link_nodes(&$node, $is_child)\r
+ {\r
+ $node->parent = $this->parent;\r
+ $this->parent->nodes[] = $node;\r
+ if ($is_child)\r
+ {\r
+ $this->parent->children[] = $node;\r
+ }\r
+ }\r
+\r
+ // as a text node\r
+ protected function as_text_node($tag)\r
+ {\r
+ $node = new simple_html_dom_node($this);\r
+ ++$this->cursor;\r
+ $node->_[HDOM_INFO_TEXT] = '</' . $tag . '>';\r
+ $this->link_nodes($node, false);\r
+ $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ return true;\r
+ }\r
+\r
+ protected function skip($chars)\r
+ {\r
+ $this->pos += strspn($this->doc, $chars, $this->pos);\r
+ $this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ }\r
+\r
+ protected function copy_skip($chars)\r
+ {\r
+ $pos = $this->pos;\r
+ $len = strspn($this->doc, $chars, $pos);\r
+ $this->pos += $len;\r
+ $this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ if ($len===0) return '';\r
+ return substr($this->doc, $pos, $len);\r
+ }\r
+\r
+ protected function copy_until($chars)\r
+ {\r
+ $pos = $this->pos;\r
+ $len = strcspn($this->doc, $chars, $pos);\r
+ $this->pos += $len;\r
+ $this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next\r
+ return substr($this->doc, $pos, $len);\r
+ }\r
+\r
+ protected function copy_until_char($char)\r
+ {\r
+ if ($this->char===null) return '';\r
+\r
+ if (($pos = strpos($this->doc, $char, $this->pos))===false) {\r
+ $ret = substr($this->doc, $this->pos, $this->size-$this->pos);\r
+ $this->char = null;\r
+ $this->pos = $this->size;\r
+ return $ret;\r
+ }\r
+\r
+ if ($pos===$this->pos) return '';\r
+ $pos_old = $this->pos;\r
+ $this->char = $this->doc[$pos];\r
+ $this->pos = $pos;\r
+ return substr($this->doc, $pos_old, $pos-$pos_old);\r
+ }\r
+\r
+ protected function copy_until_char_escape($char)\r
+ {\r
+ if ($this->char===null) return '';\r
+\r
+ $start = $this->pos;\r
+ while (1)\r
+ {\r
+ if (($pos = strpos($this->doc, $char, $start))===false)\r
+ {\r
+ $ret = substr($this->doc, $this->pos, $this->size-$this->pos);\r
+ $this->char = null;\r
+ $this->pos = $this->size;\r
+ return $ret;\r
+ }\r
+\r
+ if ($pos===$this->pos) return '';\r
+\r
+ if ($this->doc[$pos-1]==='\\') {\r
+ $start = $pos+1;\r
+ continue;\r
+ }\r
+\r
+ $pos_old = $this->pos;\r
+ $this->char = $this->doc[$pos];\r
+ $this->pos = $pos;\r
+ return substr($this->doc, $pos_old, $pos-$pos_old);\r
+ }\r
+ }\r
+\r
+ // remove noise from html content\r
+ // save the noise in the $this->noise array.\r
+ protected function remove_noise($pattern, $remove_tag=false)\r
+ {\r
+ global $debug_object;\r
+ if (is_object($debug_object)) { $debug_object->debugLogEntry(1); }\r
+\r
+ $count = preg_match_all($pattern, $this->doc, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE);\r
+\r
+ for ($i=$count-1; $i>-1; --$i)\r
+ {\r
+ $key = '___noise___'.sprintf('% 5d', count($this->noise)+1000);\r
+ if (is_object($debug_object)) { $debug_object->debugLog(2, 'key is: ' . $key); }\r
+ $idx = ($remove_tag) ? 0 : 1;\r
+ $this->noise[$key] = $matches[$i][$idx][0];\r
+ $this->doc = substr_replace($this->doc, $key, $matches[$i][$idx][1], strlen($matches[$i][$idx][0]));\r
+ }\r
+\r
+ // reset the length of content\r
+ $this->size = strlen($this->doc);\r
+ if ($this->size>0)\r
+ {\r
+ $this->char = $this->doc[0];\r
+ }\r
+ }\r
+\r
+ // restore noise to html content\r
+ function restore_noise($text)\r
+ {\r
+ global $debug_object;\r
+ if (is_object($debug_object)) { $debug_object->debugLogEntry(1); }\r
+\r
+ while (($pos=strpos($text, '___noise___'))!==false)\r
+ {\r
+ // 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
+ if (strlen($text) > $pos+15)\r
+ {\r
+ $key = '___noise___'.$text[$pos+11].$text[$pos+12].$text[$pos+13].$text[$pos+14].$text[$pos+15];\r
+ if (is_object($debug_object)) { $debug_object->debugLog(2, 'located key of: ' . $key); }\r
+\r
+ if (isset($this->noise[$key]))\r
+ {\r
+ $text = substr($text, 0, $pos).$this->noise[$key].substr($text, $pos+16);\r
+ }\r
+ else\r
+ {\r
+ // do this to prevent an infinite loop.\r
+ $text = substr($text, 0, $pos).'UNDEFINED NOISE FOR KEY: '.$key . substr($text, $pos+16);\r
+ }\r
+ }\r
+ else\r
+ {\r
+ // There is no valid key being given back to us... We must get rid of the ___noise___ or we will have a problem.\r
+ $text = substr($text, 0, $pos).'NO NUMERIC NOISE KEY' . substr($text, $pos+11);\r
+ }\r
+ }\r
+ return $text;\r
+ }\r
+\r
+ // Sometimes we NEED one of the noise elements.\r
+ function search_noise($text)\r
+ {\r
+ global $debug_object;\r
+ if (is_object($debug_object)) { $debug_object->debugLogEntry(1); }\r
+\r
+ foreach($this->noise as $noiseElement)\r
+ {\r
+ if (strpos($noiseElement, $text)!==false)\r
+ {\r
+ return $noiseElement;\r
+ }\r
+ }\r
+ }\r
+ function __toString()\r
+ {\r
+ return $this->root->innertext();\r
+ }\r
+\r
+ function __get($name)\r
+ {\r
+ switch ($name)\r
+ {\r
+ case 'outertext':\r
+ return $this->root->innertext();\r
+ case 'innertext':\r
+ return $this->root->innertext();\r
+ case 'plaintext':\r
+ return $this->root->text();\r
+ case 'charset':\r
+ return $this->_charset;\r
+ case 'target_charset':\r
+ return $this->_target_charset;\r
+ }\r
+ }\r
+\r
+ // camel naming conventions\r
+ function childNodes($idx=-1) {return $this->root->childNodes($idx);}\r
+ function firstChild() {return $this->root->first_child();}\r
+ function lastChild() {return $this->root->last_child();}\r
+ function createElement($name, $value=null) {return @str_get_html("<$name>$value</$name>")->first_child();}\r
+ function createTextNode($value) {return @end(str_get_html($value)->nodes);}\r
+ function getElementById($id) {return $this->find("#$id", 0);}\r
+ function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);}\r
+ function getElementByTagName($name) {return $this->find($name, 0);}\r
+ function getElementsByTagName($name, $idx=-1) {return $this->find($name, $idx);}\r
+ function loadFile() {$args = func_get_args();$this->load_file($args);}\r
+}\r
+\r
+?>
\ No newline at end of file