├── README.md ├── css └── toc_style.css ├── Plugin.php └── simple_html_dom.php /README.md: -------------------------------------------------------------------------------- 1 | # TableOfContents 2 | A typecho plugin for toc 3 | 4 | Auto generate TOC for your post! 5 | 为你的文章自动添加文章目录! -------------------------------------------------------------------------------- /css/toc_style.css: -------------------------------------------------------------------------------- 1 | .toc-index { 2 | padding: 10px; 3 | border: 1px solid #ddd; 4 | background: #f8f8f8; 5 | line-height: 160%; 6 | font-size: 12px; 7 | width: 100%; 8 | position: relative; 9 | z-index: 900; 10 | margin: 0 0 10px 10px; 11 | 12 | } 13 | 14 | .toc-title { 15 | margin: 0 0 3px 0; 16 | padding: 0; 17 | font-size: 130%; 18 | font-weight: bold 19 | } 20 | 21 | .toc-toggle { 22 | font-size: 9pt 23 | } 24 | 25 | .toc-index ul { 26 | padding: 0; 27 | margin: 0; 28 | font-size: 100%; 29 | list-style: none; 30 | } 31 | 32 | .toc-level3 { 33 | margin-left: 20px; 34 | list-style-type: square; 35 | } 36 | 37 | @media (min-width:768px){ 38 | .toc-index { 39 | max-width: 30%;float: right; 40 | }} -------------------------------------------------------------------------------- /Plugin.php: -------------------------------------------------------------------------------- 1 | contentEx = array('TableOfContents_Plugin', 'replace'); 24 | Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('TableOfContents_Plugin', 'replace'); 25 | Typecho_Plugin::factory('Widget_Archive')->header = array('TableOfContents_Plugin', 'header'); 26 | Typecho_Plugin::factory('Widget_Archive')->footer = array('TableOfContents_Plugin', 'footer'); 27 | } 28 | 29 | /** 30 | * 禁用插件方法,如果禁用失败,直接抛出异常 31 | * 32 | * @static 33 | * @access public 34 | * @return void 35 | * @throws Typecho_Plugin_Exception 36 | */ 37 | public static function deactivate() 38 | { 39 | } 40 | 41 | /** 42 | * 获取插件配置面板 43 | * 44 | * @access public 45 | * @param Typecho_Widget_Helper_Form $form 配置面板 46 | * @return void 47 | */ 48 | public static function config(Typecho_Widget_Helper_Form $form) 49 | { 50 | 51 | } 52 | 53 | /** 54 | * 个人用户的配置面板 55 | * 56 | * @access public 57 | * @param Typecho_Widget_Helper_Form $form 58 | * @return void 59 | */ 60 | public static function personalConfig(Typecho_Widget_Helper_Form $form) 61 | { 62 | } 63 | 64 | 65 | public static function header() 66 | { 67 | $cssUrl = Helper::options()->siteUrl . "usr/plugins/TableOfContents/css/toc_style.css"; 68 | echo ''; 69 | } 70 | 71 | public static function footer() 72 | { 73 | 74 | echo ''; 90 | } 91 | 92 | /** 93 | * 插件实现方法 94 | * 95 | * @access public 96 | * @param html $string 97 | * @return string 98 | */ 99 | public static function replace($content, $class, $string) 100 | { 101 | 102 | $html_string = is_null($string) ? $content : $string; 103 | 104 | if ($class->is('index') || $class->is('search') || $class->is('date')) { 105 | return $html_string; 106 | } 107 | 108 | $toc = self::create_toc_with_dom($html_string); 109 | return $toc; 110 | } 111 | 112 | /** 113 | * copyright http://www.westhost.com/contest/php/function/create-table-of-contents/124 114 | * @param $content 115 | * @return array 116 | */ 117 | public static function create_toc($content) 118 | { 119 | preg_match_all('/([^<]+)<\/h[2-3]>/i', $content, $matches, PREG_SET_ORDER); 120 | if (count($matches) < 3) 121 | return array( 122 | 'toc' => '', 123 | 'content' => $content 124 | ); 125 | 126 | $anchors = array(); 127 | $toc = '
显隐
' . "\n"; 128 | 129 | $toc .= '

' . _t('文章目录') . '

' . "\n"; 219 | 220 | return $toc . $content; 221 | } 222 | 223 | 224 | public static function create_toc_with_dom($content) 225 | { 226 | class_exists('simple_html_dom') || require_once 'simple_html_dom.php'; 227 | 228 | $html = str_get_html($content, 1, 1, 'UTF-8', false); 229 | 230 | $toc = '
本文目录 [隐藏]
'; 231 | $toc .= ''; 232 | $last_level = 0; 233 | $count_h2 = 0; 234 | $new = $html->find('h2,h3'); 235 | 236 | if (count($new) < 3) return $content; 237 | 238 | foreach ($new as $h) { 239 | $innerTEXT = trim($h->innertext); 240 | $id = str_replace(' ', '_', $innerTEXT); 241 | $level = intval($h->tag[1]); 242 | 243 | if ($level == 2) { 244 | $count_h2++; 245 | $innerTEXT = self::dec2roman($count_h2) . ' ' . $innerTEXT; 246 | } 247 | $h->id = $id; // add id attribute so we can jump to this element 248 | 249 | $h->innertext = $innerTEXT; // add id attribute so we can jump to this element 250 | 251 | if ($level > $last_level) 252 | // add class 253 | $toc .= '
    '; 254 | else { 255 | $toc .= str_repeat('
', $last_level - $level); 256 | $toc .= ''; 257 | } 258 | if ($level >= $last_level) { 259 | $toc .= "
  • {$innerTEXT}"; 260 | } else { 261 | $toc .= "
  • {$innerTEXT}"; 262 | } 263 | 264 | 265 | $last_level = $level; 266 | } 267 | 268 | $toc .= str_repeat('
  • ', $last_level); 269 | $toc .= '
    '; 270 | 271 | 272 | return $toc . "\n\n\n\n\n\n
    " . $html->save(); 273 | 274 | } 275 | 276 | 277 | /** 278 | * copyright http://www.sharejs.com/codes/php/2433 279 | * @param $f 280 | * @return bool|mixed 281 | */ 282 | public static function dec2roman($f) 283 | { 284 | $old_k = ''; 285 | // Return false if either $f is not a real number, $f is bigger than 3999 or $f is lower or equal to 0: 286 | if (!is_numeric($f) || $f > 3999 || $f <= 0) return false; 287 | 288 | // Define the roman figures: 289 | $roman = array('M' => 1000, 'D' => 500, 'C' => 100, 'L' => 50, 'X' => 10, 'V' => 5, 'I' => 1); 290 | 291 | // Calculate the needed roman figures: 292 | foreach ($roman as $k => $v) if (($amount[$k] = floor($f / $v)) > 0) $f -= $amount[$k] * $v; 293 | 294 | // Build the string: 295 | $return = ''; 296 | foreach ($amount as $k => $v) { 297 | $return .= $v <= 3 ? str_repeat($k, $v) : $k . $old_k; 298 | $old_k = $k; 299 | } 300 | 301 | // Replace some spacial cases and return the string: 302 | return str_replace(array('VIV', 'LXL', 'DCD'), array('IX', 'XC', 'CM'), $return . '. '); 303 | } 304 | 305 | } -------------------------------------------------------------------------------- /simple_html_dom.php: -------------------------------------------------------------------------------- 1 | size is the "real" number of bytes the dom was created from. 17 | * but for most purposes, it's a really good estimation. 18 | * Paperg - Added the forceTagsClosed to the dom constructor. Forcing tags closed is great for malformed html, but it CAN lead to parsing errors. 19 | * Allow the user to tell us how much they trust the html. 20 | * 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. 21 | * This allows for us to find tags based on the text they contain. 22 | * Create find_ancestor_tag to see if a tag is - at any level - inside of another specific tag. 23 | * Paperg: added parse_charset so that we know about the character set of the source document. 24 | * 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 25 | * last transfer or curl_exec, and we will parse that and use it in preference to any other method of charset detection. 26 | * 27 | * Found infinite loop in the case of broken html in restore_noise. Rewrote to protect from that. 28 | * PaperG (John Schlick) Added get_display_size for "IMG" tags. 29 | * 30 | * Licensed under The MIT License 31 | * Redistributions of files must retain the above copyright notice. 32 | * 33 | * @author S.C. Chen 34 | * @author John Schlick 35 | * @author Rus Carroll 36 | * @version 1.5 ($Rev: 196 $) 37 | * @package PlaceLocalInclude 38 | * @subpackage simple_html_dom 39 | */ 40 | 41 | /** 42 | * All of the Defines for the classes below. 43 | * @author S.C. Chen 44 | */ 45 | define('HDOM_TYPE_ELEMENT', 1); 46 | define('HDOM_TYPE_COMMENT', 2); 47 | define('HDOM_TYPE_TEXT', 3); 48 | define('HDOM_TYPE_ENDTAG', 4); 49 | define('HDOM_TYPE_ROOT', 5); 50 | define('HDOM_TYPE_UNKNOWN', 6); 51 | define('HDOM_QUOTE_DOUBLE', 0); 52 | define('HDOM_QUOTE_SINGLE', 1); 53 | define('HDOM_QUOTE_NO', 3); 54 | define('HDOM_INFO_BEGIN', 0); 55 | define('HDOM_INFO_END', 1); 56 | define('HDOM_INFO_QUOTE', 2); 57 | define('HDOM_INFO_SPACE', 3); 58 | define('HDOM_INFO_TEXT', 4); 59 | define('HDOM_INFO_INNER', 5); 60 | define('HDOM_INFO_OUTER', 6); 61 | define('HDOM_INFO_ENDSPACE',7); 62 | define('DEFAULT_TARGET_CHARSET', 'UTF-8'); 63 | define('DEFAULT_BR_TEXT', "\r\n"); 64 | define('DEFAULT_SPAN_TEXT', " "); 65 | if (!defined('MAX_FILE_SIZE')) { 66 | define('MAX_FILE_SIZE', 600000); 67 | } 68 | 69 | // helper functions 70 | // ----------------------------------------------------------------------------- 71 | // get html dom from file 72 | // $maxlen is defined in the code as PHP_STREAM_COPY_ALL which is defined as -1. 73 | 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) 74 | { 75 | // We DO force the tags to be terminated. 76 | $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText); 77 | // For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done. 78 | $contents = file_get_contents($url, $use_include_path, $context, $offset); 79 | // Paperg - use our own mechanism for getting the contents as we want to control the timeout. 80 | //$contents = retrieve_url_contents($url); 81 | if (empty($contents) || strlen($contents) > MAX_FILE_SIZE) 82 | { 83 | return false; 84 | } 85 | // The second parameter can force the selectors to all be lowercase. 86 | $dom->load($contents, $lowercase, $stripRN); 87 | return $dom; 88 | } 89 | 90 | // get html dom from string 91 | function str_get_html($str, $lowercase=true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) 92 | { 93 | $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText); 94 | if (empty($str) || strlen($str) > MAX_FILE_SIZE) 95 | { 96 | $dom->clear(); 97 | return false; 98 | } 99 | $dom->load($str, $lowercase, $stripRN); 100 | return $dom; 101 | } 102 | 103 | // dump html dom tree 104 | function dump_html_tree($node, $show_attr=true, $deep=0) 105 | { 106 | $node->dump($node); 107 | } 108 | 109 | 110 | /** 111 | * simple html dom node 112 | * PaperG - added ability for "find" routine to lowercase the value of the selector. 113 | * PaperG - added $tag_start to track the start position of the tag in the total byte index 114 | * 115 | * @package PlaceLocalInclude 116 | */ 117 | class simple_html_dom_node 118 | { 119 | public $nodetype = HDOM_TYPE_TEXT; 120 | public $tag = 'text'; 121 | public $attr = array(); 122 | public $children = array(); 123 | public $nodes = array(); 124 | public $parent = null; 125 | // The "info" array - see HDOM_INFO_... for what each element contains. 126 | public $_ = array(); 127 | public $tag_start = 0; 128 | private $dom = null; 129 | 130 | function __construct($dom) 131 | { 132 | $this->dom = $dom; 133 | $dom->nodes[] = $this; 134 | } 135 | 136 | function __destruct() 137 | { 138 | $this->clear(); 139 | } 140 | 141 | function __toString() 142 | { 143 | return $this->outertext(); 144 | } 145 | 146 | // clean up memory due to php5 circular references memory leak... 147 | function clear() 148 | { 149 | $this->dom = null; 150 | $this->nodes = null; 151 | $this->parent = null; 152 | $this->children = null; 153 | } 154 | 155 | // dump node's tree 156 | function dump($show_attr=true, $deep=0) 157 | { 158 | $lead = str_repeat(' ', $deep); 159 | 160 | echo $lead.$this->tag; 161 | if ($show_attr && count($this->attr)>0) 162 | { 163 | echo '('; 164 | foreach ($this->attr as $k=>$v) 165 | echo "[$k]=>\"".$this->$k.'", '; 166 | echo ')'; 167 | } 168 | echo "\n"; 169 | 170 | if ($this->nodes) 171 | { 172 | foreach ($this->nodes as $c) 173 | { 174 | $c->dump($show_attr, $deep+1); 175 | } 176 | } 177 | } 178 | 179 | 180 | // Debugging function to dump a single dom node with a bunch of information about it. 181 | function dump_node($echo=true) 182 | { 183 | 184 | $string = $this->tag; 185 | if (count($this->attr)>0) 186 | { 187 | $string .= '('; 188 | foreach ($this->attr as $k=>$v) 189 | { 190 | $string .= "[$k]=>\"".$this->$k.'", '; 191 | } 192 | $string .= ')'; 193 | } 194 | if (count($this->_)>0) 195 | { 196 | $string .= ' $_ ('; 197 | foreach ($this->_ as $k=>$v) 198 | { 199 | if (is_array($v)) 200 | { 201 | $string .= "[$k]=>("; 202 | foreach ($v as $k2=>$v2) 203 | { 204 | $string .= "[$k2]=>\"".$v2.'", '; 205 | } 206 | $string .= ")"; 207 | } else { 208 | $string .= "[$k]=>\"".$v.'", '; 209 | } 210 | } 211 | $string .= ")"; 212 | } 213 | 214 | if (isset($this->text)) 215 | { 216 | $string .= " text: (" . $this->text . ")"; 217 | } 218 | 219 | $string .= " HDOM_INNER_INFO: '"; 220 | if (isset($node->_[HDOM_INFO_INNER])) 221 | { 222 | $string .= $node->_[HDOM_INFO_INNER] . "'"; 223 | } 224 | else 225 | { 226 | $string .= ' NULL '; 227 | } 228 | 229 | $string .= " children: " . count($this->children); 230 | $string .= " nodes: " . count($this->nodes); 231 | $string .= " tag_start: " . $this->tag_start; 232 | $string .= "\n"; 233 | 234 | if ($echo) 235 | { 236 | echo $string; 237 | return; 238 | } 239 | else 240 | { 241 | return $string; 242 | } 243 | } 244 | 245 | // returns the parent of node 246 | // If a node is passed in, it will reset the parent of the current node to that one. 247 | function parent($parent=null) 248 | { 249 | // I am SURE that this doesn't work properly. 250 | // It fails to unset the current node from it's current parents nodes or children list first. 251 | if ($parent !== null) 252 | { 253 | $this->parent = $parent; 254 | $this->parent->nodes[] = $this; 255 | $this->parent->children[] = $this; 256 | } 257 | 258 | return $this->parent; 259 | } 260 | 261 | // verify that node has children 262 | function has_child() 263 | { 264 | return !empty($this->children); 265 | } 266 | 267 | // returns children of node 268 | function children($idx=-1) 269 | { 270 | if ($idx===-1) 271 | { 272 | return $this->children; 273 | } 274 | if (isset($this->children[$idx])) return $this->children[$idx]; 275 | return null; 276 | } 277 | 278 | // returns the first child of node 279 | function first_child() 280 | { 281 | if (count($this->children)>0) 282 | { 283 | return $this->children[0]; 284 | } 285 | return null; 286 | } 287 | 288 | // returns the last child of node 289 | function last_child() 290 | { 291 | if (($count=count($this->children))>0) 292 | { 293 | return $this->children[$count-1]; 294 | } 295 | return null; 296 | } 297 | 298 | // returns the next sibling of node 299 | function next_sibling() 300 | { 301 | if ($this->parent===null) 302 | { 303 | return null; 304 | } 305 | 306 | $idx = 0; 307 | $count = count($this->parent->children); 308 | while ($idx<$count && $this!==$this->parent->children[$idx]) 309 | { 310 | ++$idx; 311 | } 312 | if (++$idx>=$count) 313 | { 314 | return null; 315 | } 316 | return $this->parent->children[$idx]; 317 | } 318 | 319 | // returns the previous sibling of node 320 | function prev_sibling() 321 | { 322 | if ($this->parent===null) return null; 323 | $idx = 0; 324 | $count = count($this->parent->children); 325 | while ($idx<$count && $this!==$this->parent->children[$idx]) 326 | ++$idx; 327 | if (--$idx<0) return null; 328 | return $this->parent->children[$idx]; 329 | } 330 | 331 | // function to locate a specific ancestor tag in the path to the root. 332 | function find_ancestor_tag($tag) 333 | { 334 | global $debugObject; 335 | if (is_object($debugObject)) { $debugObject->debugLogEntry(1); } 336 | 337 | // Start by including ourselves in the comparison. 338 | $returnDom = $this; 339 | 340 | while (!is_null($returnDom)) 341 | { 342 | if (is_object($debugObject)) { $debugObject->debugLog(2, "Current tag is: " . $returnDom->tag); } 343 | 344 | if ($returnDom->tag == $tag) 345 | { 346 | break; 347 | } 348 | $returnDom = $returnDom->parent; 349 | } 350 | return $returnDom; 351 | } 352 | 353 | // get dom node's inner html 354 | function innertext() 355 | { 356 | if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER]; 357 | if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); 358 | 359 | $ret = ''; 360 | foreach ($this->nodes as $n) 361 | $ret .= $n->outertext(); 362 | return $ret; 363 | } 364 | 365 | // get dom node's outer text (with tag) 366 | function outertext() 367 | { 368 | global $debugObject; 369 | if (is_object($debugObject)) 370 | { 371 | $text = ''; 372 | if ($this->tag == 'text') 373 | { 374 | if (!empty($this->text)) 375 | { 376 | $text = " with text: " . $this->text; 377 | } 378 | } 379 | $debugObject->debugLog(1, 'Innertext of tag: ' . $this->tag . $text); 380 | } 381 | 382 | if ($this->tag==='root') return $this->innertext(); 383 | 384 | // trigger callback 385 | if ($this->dom && $this->dom->callback!==null) 386 | { 387 | call_user_func_array($this->dom->callback, array($this)); 388 | } 389 | 390 | if (isset($this->_[HDOM_INFO_OUTER])) return $this->_[HDOM_INFO_OUTER]; 391 | if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); 392 | 393 | // render begin tag 394 | if ($this->dom && $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]) 395 | { 396 | $ret = $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]->makeup(); 397 | } else { 398 | $ret = ""; 399 | } 400 | 401 | // render inner text 402 | if (isset($this->_[HDOM_INFO_INNER])) 403 | { 404 | // If it's a br tag... don't return the HDOM_INNER_INFO that we may or may not have added. 405 | if ($this->tag != "br") 406 | { 407 | $ret .= $this->_[HDOM_INFO_INNER]; 408 | } 409 | } else { 410 | if ($this->nodes) 411 | { 412 | foreach ($this->nodes as $n) 413 | { 414 | $ret .= $this->convert_text($n->outertext()); 415 | } 416 | } 417 | } 418 | 419 | // render end tag 420 | if (isset($this->_[HDOM_INFO_END]) && $this->_[HDOM_INFO_END]!=0) 421 | $ret .= 'tag.'>'; 422 | return $ret; 423 | } 424 | 425 | // get dom node's plain text 426 | function text() 427 | { 428 | if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER]; 429 | switch ($this->nodetype) 430 | { 431 | case HDOM_TYPE_TEXT: return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); 432 | case HDOM_TYPE_COMMENT: return ''; 433 | case HDOM_TYPE_UNKNOWN: return ''; 434 | } 435 | if (strcasecmp($this->tag, 'script')===0) return ''; 436 | if (strcasecmp($this->tag, 'style')===0) return ''; 437 | 438 | $ret = ''; 439 | // 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. 440 | // NOTE: This indicates that there is a problem where it's set to NULL without a clear happening. 441 | // WHY is this happening? 442 | if (!is_null($this->nodes)) 443 | { 444 | foreach ($this->nodes as $n) 445 | { 446 | $ret .= $this->convert_text($n->text()); 447 | } 448 | 449 | // 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. 450 | if ($this->tag == "span") 451 | { 452 | $ret .= $this->dom->default_span_text; 453 | } 454 | 455 | 456 | } 457 | return $ret; 458 | } 459 | 460 | function xmltext() 461 | { 462 | $ret = $this->innertext(); 463 | $ret = str_ireplace('', '', $ret); 465 | return $ret; 466 | } 467 | 468 | // build node's text with tag 469 | function makeup() 470 | { 471 | // text, comment, unknown 472 | if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); 473 | 474 | $ret = '<'.$this->tag; 475 | $i = -1; 476 | 477 | foreach ($this->attr as $key=>$val) 478 | { 479 | ++$i; 480 | 481 | // skip removed attribute 482 | if ($val===null || $val===false) 483 | continue; 484 | 485 | $ret .= $this->_[HDOM_INFO_SPACE][$i][0]; 486 | //no value attr: nowrap, checked selected... 487 | if ($val===true) 488 | $ret .= $key; 489 | else { 490 | switch ($this->_[HDOM_INFO_QUOTE][$i]) 491 | { 492 | case HDOM_QUOTE_DOUBLE: $quote = '"'; break; 493 | case HDOM_QUOTE_SINGLE: $quote = '\''; break; 494 | default: $quote = ''; 495 | } 496 | $ret .= $key.$this->_[HDOM_INFO_SPACE][$i][1].'='.$this->_[HDOM_INFO_SPACE][$i][2].$quote.$val.$quote; 497 | } 498 | } 499 | $ret = $this->dom->restore_noise($ret); 500 | return $ret . $this->_[HDOM_INFO_ENDSPACE] . '>'; 501 | } 502 | 503 | // find elements by css selector 504 | //PaperG - added ability for find to lowercase the value of the selector. 505 | function find($selector, $idx=null, $lowercase=false) 506 | { 507 | $selectors = $this->parse_selector($selector); 508 | if (($count=count($selectors))===0) return array(); 509 | $found_keys = array(); 510 | 511 | // find each selector 512 | for ($c=0; $c<$count; ++$c) 513 | { 514 | // The change on the below line was documented on the sourceforge code tracker id 2788009 515 | // used to be: if (($levle=count($selectors[0]))===0) return array(); 516 | if (($levle=count($selectors[$c]))===0) return array(); 517 | if (!isset($this->_[HDOM_INFO_BEGIN])) return array(); 518 | 519 | $head = array($this->_[HDOM_INFO_BEGIN]=>1); 520 | 521 | // handle descendant selectors, no recursive! 522 | for ($l=0; $l<$levle; ++$l) 523 | { 524 | $ret = array(); 525 | foreach ($head as $k=>$v) 526 | { 527 | $n = ($k===-1) ? $this->dom->root : $this->dom->nodes[$k]; 528 | //PaperG - Pass this optional parameter on to the seek function. 529 | $n->seek($selectors[$c][$l], $ret, $lowercase); 530 | } 531 | $head = $ret; 532 | } 533 | 534 | foreach ($head as $k=>$v) 535 | { 536 | if (!isset($found_keys[$k])) 537 | $found_keys[$k] = 1; 538 | } 539 | } 540 | 541 | // sort keys 542 | ksort($found_keys); 543 | 544 | $found = array(); 545 | foreach ($found_keys as $k=>$v) 546 | $found[] = $this->dom->nodes[$k]; 547 | 548 | // return nth-element or array 549 | if (is_null($idx)) return $found; 550 | else if ($idx<0) $idx = count($found) + $idx; 551 | return (isset($found[$idx])) ? $found[$idx] : null; 552 | } 553 | 554 | // seek for given conditions 555 | // PaperG - added parameter to allow for case insensitive testing of the value of a selector. 556 | protected function seek($selector, &$ret, $lowercase=false) 557 | { 558 | global $debugObject; 559 | if (is_object($debugObject)) { $debugObject->debugLogEntry(1); } 560 | 561 | list($tag, $key, $val, $exp, $no_key) = $selector; 562 | 563 | // xpath index 564 | if ($tag && $key && is_numeric($key)) 565 | { 566 | $count = 0; 567 | foreach ($this->children as $c) 568 | { 569 | if ($tag==='*' || $tag===$c->tag) { 570 | if (++$count==$key) { 571 | $ret[$c->_[HDOM_INFO_BEGIN]] = 1; 572 | return; 573 | } 574 | } 575 | } 576 | return; 577 | } 578 | 579 | $end = (!empty($this->_[HDOM_INFO_END])) ? $this->_[HDOM_INFO_END] : 0; 580 | if ($end==0) { 581 | $parent = $this->parent; 582 | while (!isset($parent->_[HDOM_INFO_END]) && $parent!==null) { 583 | $end -= 1; 584 | $parent = $parent->parent; 585 | } 586 | $end += $parent->_[HDOM_INFO_END]; 587 | } 588 | 589 | for ($i=$this->_[HDOM_INFO_BEGIN]+1; $i<$end; ++$i) { 590 | $node = $this->dom->nodes[$i]; 591 | 592 | $pass = true; 593 | 594 | if ($tag==='*' && !$key) { 595 | if (in_array($node, $this->children, true)) 596 | $ret[$i] = 1; 597 | continue; 598 | } 599 | 600 | // compare tag 601 | if ($tag && $tag!=$node->tag && $tag!=='*') {$pass=false;} 602 | // compare key 603 | if ($pass && $key) { 604 | if ($no_key) { 605 | if (isset($node->attr[$key])) $pass=false; 606 | } else { 607 | if (($key != "plaintext") && !isset($node->attr[$key])) $pass=false; 608 | } 609 | } 610 | // compare value 611 | if ($pass && $key && $val && $val!=='*') { 612 | // If they have told us that this is a "plaintext" search then we want the plaintext of the node - right? 613 | if ($key == "plaintext") { 614 | // $node->plaintext actually returns $node->text(); 615 | $nodeKeyValue = $node->text(); 616 | } else { 617 | // this is a normal search, we want the value of that attribute of the tag. 618 | $nodeKeyValue = $node->attr[$key]; 619 | } 620 | if (is_object($debugObject)) {$debugObject->debugLog(2, "testing node: " . $node->tag . " for attribute: " . $key . $exp . $val . " where nodes value is: " . $nodeKeyValue);} 621 | 622 | //PaperG - If lowercase is set, do a case insensitive test of the value of the selector. 623 | if ($lowercase) { 624 | $check = $this->match($exp, strtolower($val), strtolower($nodeKeyValue)); 625 | } else { 626 | $check = $this->match($exp, $val, $nodeKeyValue); 627 | } 628 | if (is_object($debugObject)) {$debugObject->debugLog(2, "after match: " . ($check ? "true" : "false"));} 629 | 630 | // handle multiple class 631 | if (!$check && strcasecmp($key, 'class')===0) { 632 | foreach (explode(' ',$node->attr[$key]) as $k) { 633 | // Without this, there were cases where leading, trailing, or double spaces lead to our comparing blanks - bad form. 634 | if (!empty($k)) { 635 | if ($lowercase) { 636 | $check = $this->match($exp, strtolower($val), strtolower($k)); 637 | } else { 638 | $check = $this->match($exp, $val, $k); 639 | } 640 | if ($check) break; 641 | } 642 | } 643 | } 644 | if (!$check) $pass = false; 645 | } 646 | if ($pass) $ret[$i] = 1; 647 | unset($node); 648 | } 649 | // It's passed by reference so this is actually what this function returns. 650 | if (is_object($debugObject)) {$debugObject->debugLog(1, "EXIT - ret: ", $ret);} 651 | } 652 | 653 | protected function match($exp, $pattern, $value) { 654 | global $debugObject; 655 | if (is_object($debugObject)) {$debugObject->debugLogEntry(1);} 656 | 657 | switch ($exp) { 658 | case '=': 659 | return ($value===$pattern); 660 | case '!=': 661 | return ($value!==$pattern); 662 | case '^=': 663 | return preg_match("/^".preg_quote($pattern,'/')."/", $value); 664 | case '$=': 665 | return preg_match("/".preg_quote($pattern,'/')."$/", $value); 666 | case '*=': 667 | if ($pattern[0]=='/') { 668 | return preg_match($pattern, $value); 669 | } 670 | return preg_match("/".$pattern."/i", $value); 671 | } 672 | return false; 673 | } 674 | 675 | protected function parse_selector($selector_string) { 676 | global $debugObject; 677 | if (is_object($debugObject)) {$debugObject->debugLogEntry(1);} 678 | 679 | // pattern of CSS selectors, modified from mootools 680 | // Paperg: Add the colon to the attrbute, so that it properly finds like google does. 681 | // Note: if you try to look at this attribute, yo MUST use getAttribute since $dom->x:y will fail the php syntax check. 682 | // Notice the \[ starting the attbute? and the @? following? This implies that an attribute can begin with an @ sign that is not captured. 683 | // This implies that an html attribute specifier may start with an @ sign that is NOT captured by the expression. 684 | // farther study is required to determine of this should be documented or removed. 685 | // $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is"; 686 | $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-:]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is"; 687 | preg_match_all($pattern, trim($selector_string).' ', $matches, PREG_SET_ORDER); 688 | if (is_object($debugObject)) {$debugObject->debugLog(2, "Matches Array: ", $matches);} 689 | 690 | $selectors = array(); 691 | $result = array(); 692 | //print_r($matches); 693 | 694 | foreach ($matches as $m) { 695 | $m[0] = trim($m[0]); 696 | if ($m[0]==='' || $m[0]==='/' || $m[0]==='//') continue; 697 | // for browser generated xpath 698 | if ($m[1]==='tbody') continue; 699 | 700 | list($tag, $key, $val, $exp, $no_key) = array($m[1], null, null, '=', false); 701 | if (!empty($m[2])) {$key='id'; $val=$m[2];} 702 | if (!empty($m[3])) {$key='class'; $val=$m[3];} 703 | if (!empty($m[4])) {$key=$m[4];} 704 | if (!empty($m[5])) {$exp=$m[5];} 705 | if (!empty($m[6])) {$val=$m[6];} 706 | 707 | // convert to lowercase 708 | if ($this->dom->lowercase) {$tag=strtolower($tag); $key=strtolower($key);} 709 | //elements that do NOT have the specified attribute 710 | if (isset($key[0]) && $key[0]==='!') {$key=substr($key, 1); $no_key=true;} 711 | 712 | $result[] = array($tag, $key, $val, $exp, $no_key); 713 | if (trim($m[7])===',') { 714 | $selectors[] = $result; 715 | $result = array(); 716 | } 717 | } 718 | if (count($result)>0) 719 | $selectors[] = $result; 720 | return $selectors; 721 | } 722 | 723 | function __get($name) { 724 | if (isset($this->attr[$name])) 725 | { 726 | return $this->convert_text($this->attr[$name]); 727 | } 728 | switch ($name) { 729 | case 'outertext': return $this->outertext(); 730 | case 'innertext': return $this->innertext(); 731 | case 'plaintext': return $this->text(); 732 | case 'xmltext': return $this->xmltext(); 733 | default: return array_key_exists($name, $this->attr); 734 | } 735 | } 736 | 737 | function __set($name, $value) { 738 | switch ($name) { 739 | case 'outertext': return $this->_[HDOM_INFO_OUTER] = $value; 740 | case 'innertext': 741 | if (isset($this->_[HDOM_INFO_TEXT])) return $this->_[HDOM_INFO_TEXT] = $value; 742 | return $this->_[HDOM_INFO_INNER] = $value; 743 | } 744 | if (!isset($this->attr[$name])) { 745 | $this->_[HDOM_INFO_SPACE][] = array(' ', '', ''); 746 | $this->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE; 747 | } 748 | $this->attr[$name] = $value; 749 | } 750 | 751 | function __isset($name) { 752 | switch ($name) { 753 | case 'outertext': return true; 754 | case 'innertext': return true; 755 | case 'plaintext': return true; 756 | } 757 | //no value attr: nowrap, checked selected... 758 | return (array_key_exists($name, $this->attr)) ? true : isset($this->attr[$name]); 759 | } 760 | 761 | function __unset($name) { 762 | if (isset($this->attr[$name])) 763 | unset($this->attr[$name]); 764 | } 765 | 766 | // PaperG - Function to convert the text from one character set to another if the two sets are not the same. 767 | function convert_text($text) 768 | { 769 | global $debugObject; 770 | if (is_object($debugObject)) {$debugObject->debugLogEntry(1);} 771 | 772 | $converted_text = $text; 773 | 774 | $sourceCharset = ""; 775 | $targetCharset = ""; 776 | 777 | if ($this->dom) 778 | { 779 | $sourceCharset = strtoupper($this->dom->_charset); 780 | $targetCharset = strtoupper($this->dom->_target_charset); 781 | } 782 | if (is_object($debugObject)) {$debugObject->debugLog(3, "source charset: " . $sourceCharset . " target charaset: " . $targetCharset);} 783 | 784 | if (!empty($sourceCharset) && !empty($targetCharset) && (strcasecmp($sourceCharset, $targetCharset) != 0)) 785 | { 786 | // Check if the reported encoding could have been incorrect and the text is actually already UTF-8 787 | if ((strcasecmp($targetCharset, 'UTF-8') == 0) && ($this->is_utf8($text))) 788 | { 789 | $converted_text = $text; 790 | } 791 | else 792 | { 793 | $converted_text = iconv($sourceCharset, $targetCharset, $text); 794 | } 795 | } 796 | 797 | // Lets make sure that we don't have that silly BOM issue with any of the utf-8 text we output. 798 | if ($targetCharset == 'UTF-8') 799 | { 800 | if (substr($converted_text, 0, 3) == "\xef\xbb\xbf") 801 | { 802 | $converted_text = substr($converted_text, 3); 803 | } 804 | if (substr($converted_text, -3) == "\xef\xbb\xbf") 805 | { 806 | $converted_text = substr($converted_text, 0, -3); 807 | } 808 | } 809 | 810 | return $converted_text; 811 | } 812 | 813 | /** 814 | * Returns true if $string is valid UTF-8 and false otherwise. 815 | * 816 | * @param mixed $str String to be tested 817 | * @return boolean 818 | */ 819 | static function is_utf8($str) 820 | { 821 | $c=0; $b=0; 822 | $bits=0; 823 | $len=strlen($str); 824 | for($i=0; $i<$len; $i++) 825 | { 826 | $c=ord($str[$i]); 827 | if($c > 128) 828 | { 829 | if(($c >= 254)) return false; 830 | elseif($c >= 252) $bits=6; 831 | elseif($c >= 248) $bits=5; 832 | elseif($c >= 240) $bits=4; 833 | elseif($c >= 224) $bits=3; 834 | elseif($c >= 192) $bits=2; 835 | else return false; 836 | if(($i+$bits) > $len) return false; 837 | while($bits > 1) 838 | { 839 | $i++; 840 | $b=ord($str[$i]); 841 | if($b < 128 || $b > 191) return false; 842 | $bits--; 843 | } 844 | } 845 | } 846 | return true; 847 | } 848 | /* 849 | function is_utf8($string) 850 | { 851 | //this is buggy 852 | return (utf8_encode(utf8_decode($string)) == $string); 853 | } 854 | */ 855 | 856 | /** 857 | * Function to try a few tricks to determine the displayed size of an img on the page. 858 | * NOTE: This will ONLY work on an IMG tag. Returns FALSE on all other tag types. 859 | * 860 | * @author John Schlick 861 | * @version April 19 2012 862 | * @return array an array containing the 'height' and 'width' of the image on the page or -1 if we can't figure it out. 863 | */ 864 | function get_display_size() 865 | { 866 | global $debugObject; 867 | 868 | $width = -1; 869 | $height = -1; 870 | 871 | if ($this->tag !== 'img') 872 | { 873 | return false; 874 | } 875 | 876 | // See if there is aheight or width attribute in the tag itself. 877 | if (isset($this->attr['width'])) 878 | { 879 | $width = $this->attr['width']; 880 | } 881 | 882 | if (isset($this->attr['height'])) 883 | { 884 | $height = $this->attr['height']; 885 | } 886 | 887 | // Now look for an inline style. 888 | if (isset($this->attr['style'])) 889 | { 890 | // Thanks to user gnarf from stackoverflow for this regular expression. 891 | $attributes = array(); 892 | preg_match_all("/([\w-]+)\s*:\s*([^;]+)\s*;?/", $this->attr['style'], $matches, PREG_SET_ORDER); 893 | foreach ($matches as $match) { 894 | $attributes[$match[1]] = $match[2]; 895 | } 896 | 897 | // If there is a width in the style attributes: 898 | if (isset($attributes['width']) && $width == -1) 899 | { 900 | // check that the last two characters are px (pixels) 901 | if (strtolower(substr($attributes['width'], -2)) == 'px') 902 | { 903 | $proposed_width = substr($attributes['width'], 0, -2); 904 | // Now make sure that it's an integer and not something stupid. 905 | if (filter_var($proposed_width, FILTER_VALIDATE_INT)) 906 | { 907 | $width = $proposed_width; 908 | } 909 | } 910 | } 911 | 912 | // If there is a width in the style attributes: 913 | if (isset($attributes['height']) && $height == -1) 914 | { 915 | // check that the last two characters are px (pixels) 916 | if (strtolower(substr($attributes['height'], -2)) == 'px') 917 | { 918 | $proposed_height = substr($attributes['height'], 0, -2); 919 | // Now make sure that it's an integer and not something stupid. 920 | if (filter_var($proposed_height, FILTER_VALIDATE_INT)) 921 | { 922 | $height = $proposed_height; 923 | } 924 | } 925 | } 926 | 927 | } 928 | 929 | // Future enhancement: 930 | // Look in the tag to see if there is a class or id specified that has a height or width attribute to it. 931 | 932 | // Far future enhancement 933 | // 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 934 | // Note that in this case, the class or id will have the img subselector for it to apply to the image. 935 | 936 | // ridiculously far future development 937 | // 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. 938 | 939 | $result = array('height' => $height, 940 | 'width' => $width); 941 | return $result; 942 | } 943 | 944 | // camel naming conventions 945 | function getAllAttributes() {return $this->attr;} 946 | function getAttribute($name) {return $this->__get($name);} 947 | function setAttribute($name, $value) {$this->__set($name, $value);} 948 | function hasAttribute($name) {return $this->__isset($name);} 949 | function removeAttribute($name) {$this->__set($name, null);} 950 | function getElementById($id) {return $this->find("#$id", 0);} 951 | function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);} 952 | function getElementByTagName($name) {return $this->find($name, 0);} 953 | function getElementsByTagName($name, $idx=null) {return $this->find($name, $idx);} 954 | function parentNode() {return $this->parent();} 955 | function childNodes($idx=-1) {return $this->children($idx);} 956 | function firstChild() {return $this->first_child();} 957 | function lastChild() {return $this->last_child();} 958 | function nextSibling() {return $this->next_sibling();} 959 | function previousSibling() {return $this->prev_sibling();} 960 | function hasChildNodes() {return $this->has_child();} 961 | function nodeName() {return $this->tag;} 962 | function appendChild($node) {$node->parent($this); return $node;} 963 | 964 | } 965 | 966 | /** 967 | * simple html dom parser 968 | * Paperg - in the find routine: allow us to specify that we want case insensitive testing of the value of the selector. 969 | * Paperg - change $size from protected to public so we can easily access it 970 | * Paperg - added ForceTagsClosed in the constructor which tells us whether we trust the html or not. Default is to NOT trust it. 971 | * 972 | * @package PlaceLocalInclude 973 | */ 974 | class simple_html_dom 975 | { 976 | public $root = null; 977 | public $nodes = array(); 978 | public $callback = null; 979 | public $lowercase = false; 980 | // Used to keep track of how large the text was when we started. 981 | public $original_size; 982 | public $size; 983 | protected $pos; 984 | protected $doc; 985 | protected $char; 986 | protected $cursor; 987 | protected $parent; 988 | protected $noise = array(); 989 | protected $token_blank = " \t\r\n"; 990 | protected $token_equal = ' =/>'; 991 | protected $token_slash = " />\r\n\t"; 992 | protected $token_attr = ' >'; 993 | // Note that this is referenced by a child node, and so it needs to be public for that node to see this information. 994 | public $_charset = ''; 995 | public $_target_charset = ''; 996 | protected $default_br_text = ""; 997 | public $default_span_text = ""; 998 | 999 | // use isset instead of in_array, performance boost about 30%... 1000 | protected $self_closing_tags = array('img'=>1, 'br'=>1, 'input'=>1, 'meta'=>1, 'link'=>1, 'hr'=>1, 'base'=>1, 'embed'=>1, 'spacer'=>1); 1001 | protected $block_tags = array('root'=>1, 'body'=>1, 'form'=>1, 'div'=>1, 'span'=>1, 'table'=>1); 1002 | // Known sourceforge issue #2977341 1003 | // B tags that are not closed cause us to return everything to the end of the document. 1004 | protected $optional_closing_tags = array( 1005 | 'tr'=>array('tr'=>1, 'td'=>1, 'th'=>1), 1006 | 'th'=>array('th'=>1), 1007 | 'td'=>array('td'=>1), 1008 | 'li'=>array('li'=>1), 1009 | 'dt'=>array('dt'=>1, 'dd'=>1), 1010 | 'dd'=>array('dd'=>1, 'dt'=>1), 1011 | 'dl'=>array('dd'=>1, 'dt'=>1), 1012 | 'p'=>array('p'=>1), 1013 | 'nobr'=>array('nobr'=>1), 1014 | 'b'=>array('b'=>1), 1015 | 'option'=>array('option'=>1), 1016 | ); 1017 | 1018 | function __construct($str=null, $lowercase=true, $forceTagsClosed=true, $target_charset=DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) 1019 | { 1020 | if ($str) 1021 | { 1022 | if (preg_match("/^http:\/\//i",$str) || is_file($str)) 1023 | { 1024 | $this->load_file($str); 1025 | } 1026 | else 1027 | { 1028 | $this->load($str, $lowercase, $stripRN, $defaultBRText, $defaultSpanText); 1029 | } 1030 | } 1031 | // 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. 1032 | if (!$forceTagsClosed) { 1033 | $this->optional_closing_array=array(); 1034 | } 1035 | $this->_target_charset = $target_charset; 1036 | } 1037 | 1038 | function __destruct() 1039 | { 1040 | $this->clear(); 1041 | } 1042 | 1043 | // load html from string 1044 | function load($str, $lowercase=true, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) 1045 | { 1046 | global $debugObject; 1047 | 1048 | // prepare 1049 | $this->prepare($str, $lowercase, $stripRN, $defaultBRText, $defaultSpanText); 1050 | // strip out comments 1051 | $this->remove_noise("''is"); 1052 | // strip out cdata 1053 | $this->remove_noise("''is", true); 1054 | // Per sourceforge http://sourceforge.net/tracker/?func=detail&aid=2949097&group_id=218559&atid=1044037 1055 | // Script tags removal now preceeds style tag removal. 1056 | // strip out