Changeset 2566:9bf417837888 for inc/core/class.dc.trackback.php
- Timestamp:
- 11/17/13 20:25:53 (12 years ago)
- Branch:
- 2.6
- Children:
- 2567:6c11245cbf04, 2568:61c67a7d17fa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.trackback.php
r1689 r2566 22 22 public $core; ///< <b>dcCore</b> dcCore instance 23 23 public $table; ///< <b>string</b> done pings table name 24 24 25 25 /** 26 26 Object constructor 27 27 28 28 @param core <b>dcCore</b> dcCore instance 29 29 */ … … 34 34 $this->table = $this->core->prefix.'ping'; 35 35 } 36 36 37 37 /// @name Send trackbacks 38 38 //@{ 39 39 /** 40 40 Get all pings sent for a given post. 41 41 42 42 @param post_id <b>integer</b> Post ID 43 43 @return <b>record</b> … … 48 48 'FROM '.$this->table.' '. 49 49 'WHERE post_id = '.(integer) $post_id; 50 50 51 51 return $this->con->select($strReq); 52 52 } 53 53 54 54 /** 55 55 Sends a ping to given <var>$url</var>. 56 56 57 57 @param url <b>string</b> URL to ping 58 58 @param post_id <b>integer</b> Post ID … … 66 66 return false; 67 67 } 68 68 69 69 $post_id = (integer) $post_id; 70 70 71 71 # Check for previously done trackback 72 72 $strReq = 'SELECT post_id, ping_url FROM '.$this->table.' '. 73 73 'WHERE post_id = '.$post_id.' '. 74 74 "AND ping_url = '".$this->con->escape($url)."' "; 75 75 76 76 $rs = $this->con->select($strReq); 77 77 78 78 if (!$rs->isEmpty()) { 79 79 throw new Exception(sprintf(__('%s has still been pinged'),$url)); 80 80 } 81 81 82 82 $ping_parts = explode('|',$url); 83 83 84 84 # Let's walk by the trackback way 85 85 if (count($ping_parts) < 2) { … … 91 91 //,'__debug' => false 92 92 ); 93 93 94 94 # Ping 95 95 try … … 103 103 throw new Exception(__('Unable to ping URL')); 104 104 } 105 105 106 106 $pattern = 107 107 '|<response>.*<error>(.*)</error>(.*)'. 108 108 '(<message>(.*)</message>(.*))?'. 109 109 '</response>|msU'; 110 110 111 111 if (!preg_match($pattern,$res,$match)) 112 112 { 113 113 throw new Exception(sprintf(__('%s is not a ping URL'),$url)); 114 114 } 115 115 116 116 $ping_error = trim($match[1]); 117 117 $ping_msg = (!empty($match[4])) ? $match[4] : ''; … … 126 126 catch (xmlrpcException $e) { 127 127 $ping_error = $e->getCode(); 128 $ping_msg = $e->getMessage(); 128 $ping_msg = $e->getMessage(); 129 129 } 130 130 catch (Exception $e) { … … 132 132 } 133 133 } 134 134 135 135 if ($ping_error != '0') { 136 136 throw new Exception(sprintf(__('%s, ping error:'),$url).' '.$ping_msg); … … 141 141 $cur->ping_url = $url; 142 142 $cur->ping_dt = date('Y-m-d H:i:s'); 143 143 144 144 $cur->insert(); 145 145 } 146 146 } 147 147 //@} 148 148 149 149 private function pingAlreadyDone($post_id, $from_url) 150 150 { … … 154 154 'comment_trackback' => 1, 155 155 ); 156 156 157 157 $rs = $this->core->blog->getComments($params, true); 158 158 if ($rs && !$rs->isEmpty()) { 159 159 return ($rs->f(0)); 160 160 } 161 161 162 162 return false; 163 163 } 164 164 165 165 private function addBacklink($post_id, $url, $blog_name, $title, $excerpt, &$comment) 166 166 { … … 168 168 $blog_name = 'Anonymous blog'; 169 169 } 170 170 171 171 $comment = 172 172 "<!-- TB -->\n". 173 173 '<p><strong>'.($title ? $title : $blog_name)."</strong></p>\n". 174 174 '<p>'.$excerpt.'</p>'; 175 175 176 176 $cur = $this->core->con->openCursor($this->core->prefix.'comment'); 177 177 $cur->comment_author = (string) $blog_name; … … 182 182 $cur->comment_status = $this->core->blog->settings->system->trackbacks_pub ? 1 : -1; 183 183 $cur->comment_ip = http::realIP(); 184 184 185 185 # --BEHAVIOR-- publicBeforeTrackbackCreate 186 186 $this->core->callBehavior('publicBeforeTrackbackCreate',$cur); 187 187 if ($cur->post_id) { 188 188 $comment_id = $this->core->blog->addComment($cur); 189 189 190 190 # --BEHAVIOR-- publicAfterTrackbackCreate 191 191 $this->core->callBehavior('publicAfterTrackbackCreate',$cur,$comment_id); 192 192 } 193 193 } 194 194 195 195 /// @name Receive trackbacks 196 196 //@{ 197 197 /** 198 198 Receives a trackback and insert it as a comment of given post. 199 199 200 200 @param post_id <b>integer</b> Post ID 201 201 */ … … 213 213 return; 214 214 } 215 215 216 216 $post_id = (integer) $post_id; 217 217 218 218 $title = !empty($_POST['title']) ? $_POST['title'] : ''; 219 219 $excerpt = !empty($_POST['excerpt']) ? $_POST['excerpt'] : ''; … … 222 222 $charset = ''; 223 223 $comment = ''; 224 224 225 225 $err = false; 226 226 $msg = ''; 227 227 228 228 if ($this->core->blog === null) 229 229 { … … 240 240 $msg = 'Blog name is required.'; 241 241 } 242 242 243 243 if (!$err) 244 244 { 245 245 $post = $this->core->blog->getPosts(array('post_id'=>$post_id,'post_type'=>'')); 246 246 247 247 if ($post->isEmpty()) 248 248 { … … 262 262 } 263 263 } 264 264 265 265 if (!$err) 266 266 { 267 267 $charset = self::getCharsetFromRequest(); 268 268 269 269 if (!$charset) { 270 270 $charset = self::detectCharset($title.' '.$excerpt.' '.$blog_name); 271 271 } 272 272 273 273 if (strtolower($charset) != 'utf-8') { 274 274 $title = iconv($charset,'UTF-8',$title); … … 276 276 $blog_name = iconv($charset,'UTF-8',$blog_name); 277 277 } 278 278 279 279 $title = trim(html::clean($title)); 280 280 $title = html::decodeEntities($title); 281 281 $title = html::escapeHTML($title); 282 282 $title = text::cutString($title,60); 283 283 284 284 $excerpt = trim(html::clean($excerpt)); 285 285 $excerpt = html::decodeEntities($excerpt); 286 286 $excerpt = preg_replace('/\s+/ms',' ',$excerpt); 287 $excerpt = text::cutString($excerpt,252); 287 $excerpt = text::cutString($excerpt,252); 288 288 $excerpt = html::escapeHTML($excerpt).'...'; 289 289 290 290 $blog_name = trim(html::clean($blog_name)); 291 291 $blog_name = html::decodeEntities($blog_name); 292 292 $blog_name = html::escapeHTML($blog_name); 293 293 $blog_name = text::cutString($blog_name,60); 294 294 295 295 try 296 296 { … … 303 303 } 304 304 } 305 305 306 306 $resp = 307 307 '<?xml version="1.0" encoding="utf-8"?>'."\n". 308 308 "<response>\n". 309 309 ' <error>'.(integer) $err."</error>\n"; 310 310 311 311 if ($msg) { 312 312 $resp .= ' <message>'.$msg."</message>\n"; 313 313 } 314 314 315 315 if (!empty($_POST['__debug'])) { 316 $resp .= 316 $resp .= 317 317 " <debug>\n". 318 318 ' <title>'.$title."</title>\n". … … 324 324 " </debug>\n"; 325 325 } 326 326 327 327 echo $resp."</response>"; 328 328 } … … 333 333 /** 334 334 Receives a pingback and insert it as a comment of given post. 335 335 336 336 @param from_url <b>string</b> Source URL 337 337 @param to_url <b>string</b> Target URL … … 341 341 $reg = '!^'.preg_quote($this->core->blog->url).'(.*)!'; 342 342 $type = $args = $next = ''; 343 343 344 344 # Are you dumb? 345 345 if (!preg_match($reg, $to_url, $m)) { 346 346 throw new Exception(__('Any chance you ping one of my contents? No? Really?'), 0); 347 347 } 348 349 # Does the targeted URL look like a registered post type? 348 349 # Does the targeted URL look like a registered post type? 350 350 $url_part = $m[1]; 351 351 $p_type = ''; … … 359 359 } 360 360 } 361 361 362 362 if (empty($p_type)) { 363 363 throw new Exception(__('Sorry but you can not ping this type of content.'), 33); … … 370 370 ); 371 371 $posts = $this->core->blog->getPosts($params); 372 373 # Missed! 372 373 # Missed! 374 374 if ($posts->isEmpty()) { 375 375 throw new Exception(__('Oops. Kinda "not found" stuff. Please check the target URL twice.'), 33); 376 376 } 377 377 378 378 # Nice try. But, sorry, no. 379 379 if (!$posts->trackbacksActive()) { … … 384 384 throw new Exception(__('Don\'t repeat yourself, please.'), 48); 385 385 } 386 386 387 387 # OK. We've found our champion. Time to check the remote part. 388 388 try { 389 389 $http = self::initHttp($from_url, $from_path); 390 390 391 391 # First round : just to be sure the ping comes from an acceptable resource type. 392 392 $http->setHeadersOnly(true); … … 398 398 throw new Exception(__('Your source URL does not look like a supported content type. Sorry. Bye, bye!'), 0); 399 399 } 400 400 401 401 # Second round : let's go fetch and parse the remote content 402 402 $http->setHeadersOnly(false); … … 409 409 $charset = self::detectCharset($remote_content); 410 410 } 411 411 412 412 if (strtolower($charset) != 'utf-8') { 413 413 $remote_content = iconv($charset,'UTF-8',$remote_content); 414 414 } 415 415 416 416 # We want a title... 417 417 if (!preg_match('!<title>([^<].*?)</title>!mis', $remote_content, $m)) { … … 422 422 $title = html::escapeHTML($title); 423 423 $title = text::cutString($title,60); 424 424 425 425 preg_match('!<body[^>]*?>(.*)?</body>!msi', $remote_content, $m); 426 426 $source = $m[1]; … … 429 429 $source = strip_tags($source, '<a>'); 430 430 $source = explode("\n\n",$source); 431 431 432 432 $excerpt = ''; 433 433 foreach ($source as $line) { … … 451 451 throw new Exception(__('Sorry, an internal problem has occured.'), 0); 452 452 } 453 453 454 454 return __('Thanks, mate. It was a pleasure.'); 455 455 } 456 456 //@} 457 457 458 458 private static function initHttp($url,&$path) 459 459 { … … 463 463 $client->useGzip(false); 464 464 $client->setPersistReferers(false); 465 465 466 466 return $client; 467 467 } 468 468 469 469 private static function getCharsetFromRequest($header = '') 470 470 { … … 472 472 $header = $_SERVER['CONTENT_TYPE']; 473 473 } 474 474 475 475 if ($header) { 476 476 if (preg_match('|charset=([a-zA-Z0-9-]+)|',$header,$m)) { … … 478 478 } 479 479 } 480 480 481 481 return null; 482 482 } … … 489 489 'ISO-8859-9,ISO-8859-10,ISO-8859-13,ISO-8859-14,ISO-8859-15'); 490 490 } 491 491 492 492 /// @name Trackbacks auto discovery 493 493 //@{ … … 495 495 Returns an array containing all discovered trackbacks URLs in 496 496 <var>$text</var>. 497 497 498 498 @param text <b>string</b> Input text 499 499 @return <b>array</b> … … 502 502 { 503 503 $res = array(); 504 504 505 505 foreach ($this->getTextLinks($text) as $link) 506 506 { … … 509 509 } 510 510 } 511 511 512 512 return $res; 513 513 } 514 514 //@} 515 515 516 516 private function getTextLinks($text) 517 517 { 518 518 $res = array(); 519 519 520 520 # href attribute on "a" tags 521 521 if (preg_match_all('/<a ([^>]+)>/ms', $text, $match, PREG_SET_ORDER)) … … 529 529 } 530 530 unset($match); 531 531 532 532 # cite attributes on "blockquote" and "q" tags 533 533 if (preg_match_all('/<(blockquote|q) ([^>]+)>/ms', $text, $match, PREG_SET_ORDER)) … … 540 540 } 541 541 } 542 542 543 543 return array_keys($res); 544 544 } 545 545 546 546 private function getPingURL($url) 547 547 { … … 549 549 $url = http::getHost().$url; 550 550 } 551 551 552 552 try 553 553 { … … 561 561 return false; 562 562 } 563 563 564 564 # If we've got a X-Pingback header and it's a valid URL, it will be enough 565 565 if ($pb_url && filter_var($pb_url,FILTER_VALIDATE_URL) && preg_match('!^https?:!',$pb_url)) { 566 566 return $pb_url.'|'.$url; 567 567 } 568 568 569 569 # No X-Pingback header. A link rel=pingback, maybe ? 570 570 $pattern_pingback = '!<link rel="pingback" href="(.*?)"( /)?>!msi'; 571 571 572 572 if (preg_match($pattern_pingback,$page_content,$m)) { 573 573 $pb_url = $m[1]; … … 583 583 '.*?<\/rdf:RDF>'. 584 584 '/msi'; 585 585 586 586 preg_match_all($pattern_rdf,$page_content,$rdf_all,PREG_SET_ORDER); 587 587 588 588 $url_path = parse_url($url, PHP_URL_PATH); 589 589 $sanitized_url = str_replace($url_path, html::sanitizeURL($url_path), $url); 590 590 591 591 for ($i=0; $i<count($rdf_all); $i++) 592 592 { … … 599 599 } 600 600 } 601 601 602 602 return null; 603 603 } 604 604 } 605 ?>
Note: See TracChangeset
for help on using the changeset viewer.