PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/2.2.1/wp-includes/comment.php

#
PHP | 817 lines | 600 code | 152 blank | 65 comment | 147 complexity | 9e9504185d9556a333d286978290ce9d MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
  3. global $wpdb;
  4. if ( 1 == get_option('comment_moderation') )
  5. return false; // If moderation is set to manual
  6. if ( preg_match_all("|(href\t*?=\t*?['\"]?)?(https?:)?//|i", $comment, $out) >= get_option('comment_max_links') )
  7. return false; // Check # of external links
  8. $mod_keys = trim(get_option('moderation_keys'));
  9. if ( !empty($mod_keys) ) {
  10. $words = explode("\n", $mod_keys );
  11. foreach ($words as $word) {
  12. $word = trim($word);
  13. // Skip empty lines
  14. if ( empty($word) )
  15. continue;
  16. // Do some escaping magic so that '#' chars in the
  17. // spam words don't break things:
  18. $word = preg_quote($word, '#');
  19. $pattern = "#$word#i";
  20. if ( preg_match($pattern, $author) ) return false;
  21. if ( preg_match($pattern, $email) ) return false;
  22. if ( preg_match($pattern, $url) ) return false;
  23. if ( preg_match($pattern, $comment) ) return false;
  24. if ( preg_match($pattern, $user_ip) ) return false;
  25. if ( preg_match($pattern, $user_agent) ) return false;
  26. }
  27. }
  28. // Comment whitelisting:
  29. if ( 1 == get_option('comment_whitelist')) {
  30. if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll
  31. $uri = parse_url($url);
  32. $domain = $uri['host'];
  33. $uri = parse_url( get_option('home') );
  34. $home_domain = $uri['host'];
  35. if ( $wpdb->get_var("SELECT link_id FROM $wpdb->links WHERE link_url LIKE ('%$domain%') LIMIT 1") || $domain == $home_domain )
  36. return true;
  37. else
  38. return false;
  39. } elseif ( $author != '' && $email != '' ) {
  40. $ok_to_comment = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_author = '$author' AND comment_author_email = '$email' and comment_approved = '1' LIMIT 1");
  41. if ( ( 1 == $ok_to_comment ) &&
  42. ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
  43. return true;
  44. else
  45. return false;
  46. } else {
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. function get_approved_comments($post_id) {
  53. global $wpdb;
  54. $post_id = (int) $post_id;
  55. return $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1' ORDER BY comment_date");
  56. }
  57. // Retrieves comment data given a comment ID or comment object.
  58. // Handles comment caching.
  59. function &get_comment(&$comment, $output = OBJECT) {
  60. global $comment_cache, $wpdb;
  61. if ( empty($comment) )
  62. return null;
  63. if ( is_object($comment) ) {
  64. if ( !isset($comment_cache[$comment->comment_ID]) )
  65. $comment_cache[$comment->comment_ID] = &$comment;
  66. $_comment = & $comment_cache[$comment->comment_ID];
  67. } else {
  68. $comment = (int) $comment;
  69. if ( !isset($comment_cache[$comment]) ) {
  70. $_comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment' LIMIT 1");
  71. $comment_cache[$comment->comment_ID] = & $_comment;
  72. } else {
  73. $_comment = & $comment_cache[$comment];
  74. }
  75. }
  76. if ( $output == OBJECT ) {
  77. return $_comment;
  78. } elseif ( $output == ARRAY_A ) {
  79. return get_object_vars($_comment);
  80. } elseif ( $output == ARRAY_N ) {
  81. return array_values(get_object_vars($_comment));
  82. } else {
  83. return $_comment;
  84. }
  85. }
  86. // Deprecate in favor of get_comment()?
  87. function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) { // less flexible, but saves DB queries
  88. global $postc, $id, $commentdata, $wpdb;
  89. if ( $no_cache ) {
  90. $query = "SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_ID'";
  91. if ( false == $include_unapproved )
  92. $query .= " AND comment_approved = '1'";
  93. $myrow = $wpdb->get_row($query, ARRAY_A);
  94. } else {
  95. $myrow['comment_ID'] = $postc->comment_ID;
  96. $myrow['comment_post_ID'] = $postc->comment_post_ID;
  97. $myrow['comment_author'] = $postc->comment_author;
  98. $myrow['comment_author_email'] = $postc->comment_author_email;
  99. $myrow['comment_author_url'] = $postc->comment_author_url;
  100. $myrow['comment_author_IP'] = $postc->comment_author_IP;
  101. $myrow['comment_date'] = $postc->comment_date;
  102. $myrow['comment_content'] = $postc->comment_content;
  103. $myrow['comment_karma'] = $postc->comment_karma;
  104. $myrow['comment_approved'] = $postc->comment_approved;
  105. $myrow['comment_type'] = $postc->comment_type;
  106. }
  107. return $myrow;
  108. }
  109. function get_lastcommentmodified($timezone = 'server') {
  110. global $cache_lastcommentmodified, $pagenow, $wpdb;
  111. $add_seconds_blog = get_option('gmt_offset') * 3600;
  112. $add_seconds_server = date('Z');
  113. $now = current_time('mysql', 1);
  114. if ( !isset($cache_lastcommentmodified[$timezone]) ) {
  115. switch ( strtolower($timezone)) {
  116. case 'gmt':
  117. $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
  118. break;
  119. case 'blog':
  120. $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
  121. break;
  122. case 'server':
  123. $lastcommentmodified = $wpdb->get_var("SELECT DATE_ADD(comment_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
  124. break;
  125. }
  126. $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
  127. } else {
  128. $lastcommentmodified = $cache_lastcommentmodified[$timezone];
  129. }
  130. return $lastcommentmodified;
  131. }
  132. function sanitize_comment_cookies() {
  133. if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) {
  134. $comment_author = apply_filters('pre_comment_author_name', $_COOKIE['comment_author_'.COOKIEHASH]);
  135. $comment_author = stripslashes($comment_author);
  136. $comment_author = attribute_escape($comment_author);
  137. $_COOKIE['comment_author_'.COOKIEHASH] = $comment_author;
  138. }
  139. if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) {
  140. $comment_author_email = apply_filters('pre_comment_author_email', $_COOKIE['comment_author_email_'.COOKIEHASH]);
  141. $comment_author_email = stripslashes($comment_author_email);
  142. $comment_author_email = attribute_escape($comment_author_email);
  143. $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
  144. }
  145. if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) {
  146. $comment_author_url = apply_filters('pre_comment_author_url', $_COOKIE['comment_author_url_'.COOKIEHASH]);
  147. $comment_author_url = stripslashes($comment_author_url);
  148. $comment_author_url = clean_url($comment_author_url);
  149. $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
  150. }
  151. }
  152. function wp_allow_comment($commentdata) {
  153. global $wpdb;
  154. extract($commentdata, EXTR_SKIP);
  155. // Simple duplicate check
  156. $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
  157. if ( $comment_author_email )
  158. $dupe .= "OR comment_author_email = '$comment_author_email' ";
  159. $dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
  160. if ( $wpdb->get_var($dupe) )
  161. wp_die( __('Duplicate comment detected; it looks as though you\'ve already said that!') );
  162. // Simple flood-protection
  163. if ( $lasttime = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author_IP = '$comment_author_IP' OR comment_author_email = '$comment_author_email' ORDER BY comment_date DESC LIMIT 1") ) {
  164. $time_lastcomment = mysql2date('U', $lasttime);
  165. $time_newcomment = mysql2date('U', $comment_date_gmt);
  166. $flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
  167. if ( $flood_die ) {
  168. do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment);
  169. wp_die( __('You are posting comments too quickly. Slow down.') );
  170. }
  171. }
  172. if ( $user_id ) {
  173. $userdata = get_userdata($user_id);
  174. $user = new WP_User($user_id);
  175. $post_author = $wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = '$comment_post_ID' LIMIT 1");
  176. }
  177. if ( $userdata && ( $user_id == $post_author || $user->has_cap('level_9') ) ) {
  178. // The author and the admins get respect.
  179. $approved = 1;
  180. } else {
  181. // Everyone else's comments will be checked.
  182. if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) )
  183. $approved = 1;
  184. else
  185. $approved = 0;
  186. if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) )
  187. $approved = 'spam';
  188. }
  189. $approved = apply_filters('pre_comment_approved', $approved);
  190. return $approved;
  191. }
  192. function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
  193. global $wpdb;
  194. do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
  195. if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
  196. foreach ( (array) $chars[1] as $char ) {
  197. // If it's an encoded char in the normal ASCII set, reject
  198. if ( 38 == $char )
  199. continue; // Unless it's &
  200. if ( $char < 128 )
  201. return true;
  202. }
  203. }
  204. $mod_keys = trim( get_option('blacklist_keys') );
  205. if ( '' == $mod_keys )
  206. return false; // If moderation keys are empty
  207. $words = explode("\n", $mod_keys );
  208. foreach ( (array) $words as $word ) {
  209. $word = trim($word);
  210. // Skip empty lines
  211. if ( empty($word) ) { continue; }
  212. // Do some escaping magic so that '#' chars in the
  213. // spam words don't break things:
  214. $word = preg_quote($word, '#');
  215. $pattern = "#$word#i";
  216. if (
  217. preg_match($pattern, $author)
  218. || preg_match($pattern, $email)
  219. || preg_match($pattern, $url)
  220. || preg_match($pattern, $comment)
  221. || preg_match($pattern, $user_ip)
  222. || preg_match($pattern, $user_agent)
  223. )
  224. return true;
  225. }
  226. return false;
  227. }
  228. function wp_delete_comment($comment_id) {
  229. global $wpdb;
  230. do_action('delete_comment', $comment_id);
  231. $comment = get_comment($comment_id);
  232. if ( ! $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1") )
  233. return false;
  234. $post_id = $comment->comment_post_ID;
  235. if ( $post_id && $comment->comment_approved == 1 )
  236. wp_update_comment_count($post_id);
  237. do_action('wp_set_comment_status', $comment_id, 'delete');
  238. return true;
  239. }
  240. function wp_get_comment_status($comment_id) {
  241. global $wpdb;
  242. $result = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
  243. if ( $result == NULL )
  244. return 'deleted';
  245. elseif ( $result == '1' )
  246. return 'approved';
  247. elseif ( $result == '0' )
  248. return 'unapproved';
  249. elseif ( $result == 'spam' )
  250. return 'spam';
  251. else
  252. return false;
  253. }
  254. function wp_get_current_commenter() {
  255. // Cookies should already be sanitized.
  256. $comment_author = '';
  257. if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
  258. $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
  259. $comment_author_email = '';
  260. if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
  261. $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
  262. $comment_author_url = '';
  263. if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
  264. $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
  265. return compact('comment_author', 'comment_author_email', 'comment_author_url');
  266. }
  267. function wp_insert_comment($commentdata) {
  268. global $wpdb;
  269. extract($commentdata, EXTR_SKIP);
  270. if ( ! isset($comment_author_IP) )
  271. $comment_author_IP = preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] );
  272. if ( ! isset($comment_date) )
  273. $comment_date = current_time('mysql');
  274. if ( ! isset($comment_date_gmt) )
  275. $comment_date_gmt = get_gmt_from_date($comment_date);
  276. if ( ! isset($comment_parent) )
  277. $comment_parent = 0;
  278. if ( ! isset($comment_approved) )
  279. $comment_approved = 1;
  280. if ( ! isset($user_id) )
  281. $user_id = 0;
  282. $result = $wpdb->query("INSERT INTO $wpdb->comments
  283. (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type, comment_parent, user_id)
  284. VALUES
  285. ('$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id')
  286. ");
  287. $id = (int) $wpdb->insert_id;
  288. if ( $comment_approved == 1)
  289. wp_update_comment_count($comment_post_ID);
  290. return $id;
  291. }
  292. function wp_filter_comment($commentdata) {
  293. $commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_ID']);
  294. $commentdata['comment_agent'] = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']);
  295. $commentdata['comment_author'] = apply_filters('pre_comment_author_name', $commentdata['comment_author']);
  296. $commentdata['comment_content'] = apply_filters('pre_comment_content', $commentdata['comment_content']);
  297. $commentdata['comment_author_IP'] = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']);
  298. $commentdata['comment_author_url'] = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']);
  299. $commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']);
  300. $commentdata['filtered'] = true;
  301. return $commentdata;
  302. }
  303. function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
  304. if ( $block ) // a plugin has already blocked... we'll let that decision stand
  305. return $block;
  306. if ( ($time_newcomment - $time_lastcomment) < 15 )
  307. return true;
  308. return false;
  309. }
  310. function wp_new_comment( $commentdata ) {
  311. $commentdata = apply_filters('preprocess_comment', $commentdata);
  312. $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
  313. $commentdata['user_ID'] = (int) $commentdata['user_ID'];
  314. $commentdata['comment_author_IP'] = preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] );
  315. $commentdata['comment_agent'] = $_SERVER['HTTP_USER_AGENT'];
  316. $commentdata['comment_date'] = current_time('mysql');
  317. $commentdata['comment_date_gmt'] = current_time('mysql', 1);
  318. $commentdata = wp_filter_comment($commentdata);
  319. $commentdata['comment_approved'] = wp_allow_comment($commentdata);
  320. $comment_ID = wp_insert_comment($commentdata);
  321. do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
  322. if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
  323. if ( '0' == $commentdata['comment_approved'] )
  324. wp_notify_moderator($comment_ID);
  325. $post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment
  326. if ( get_option('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] )
  327. wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
  328. }
  329. return $comment_ID;
  330. }
  331. function wp_set_comment_status($comment_id, $comment_status) {
  332. global $wpdb;
  333. switch ( $comment_status ) {
  334. case 'hold':
  335. $query = "UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1";
  336. break;
  337. case 'approve':
  338. $query = "UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1";
  339. break;
  340. case 'spam':
  341. $query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1";
  342. break;
  343. case 'delete':
  344. return wp_delete_comment($comment_id);
  345. break;
  346. default:
  347. return false;
  348. }
  349. if ( !$wpdb->query($query) )
  350. return false;
  351. do_action('wp_set_comment_status', $comment_id, $comment_status);
  352. $comment = get_comment($comment_id);
  353. wp_update_comment_count($comment->comment_post_ID);
  354. return true;
  355. }
  356. function wp_update_comment($commentarr) {
  357. global $wpdb;
  358. // First, get all of the original fields
  359. $comment = get_comment($commentarr['comment_ID'], ARRAY_A);
  360. // Escape data pulled from DB.
  361. foreach ( (array) $comment as $key => $value )
  362. $comment[$key] = $wpdb->escape($value);
  363. // Merge old and new fields with new fields overwriting old ones.
  364. $commentarr = array_merge($comment, $commentarr);
  365. $commentarr = wp_filter_comment( $commentarr );
  366. // Now extract the merged array.
  367. extract($commentarr, EXTR_SKIP);
  368. $comment_content = apply_filters('comment_save_pre', $comment_content);
  369. $comment_date_gmt = get_gmt_from_date($comment_date);
  370. $result = $wpdb->query(
  371. "UPDATE $wpdb->comments SET
  372. comment_content = '$comment_content',
  373. comment_author = '$comment_author',
  374. comment_author_email = '$comment_author_email',
  375. comment_approved = '$comment_approved',
  376. comment_author_url = '$comment_author_url',
  377. comment_date = '$comment_date',
  378. comment_date_gmt = '$comment_date_gmt'
  379. WHERE comment_ID = $comment_ID" );
  380. $rval = $wpdb->rows_affected;
  381. wp_update_comment_count($comment_post_ID);
  382. do_action('edit_comment', $comment_ID);
  383. return $rval;
  384. }
  385. function wp_update_comment_count($post_id) {
  386. global $wpdb, $comment_count_cache;
  387. $post_id = (int) $post_id;
  388. if ( !$post_id )
  389. return false;
  390. $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'");
  391. $wpdb->query("UPDATE $wpdb->posts SET comment_count = $count WHERE ID = '$post_id'");
  392. $comment_count_cache[$post_id] = $count;
  393. $post = get_post($post_id);
  394. if ( 'page' == $post->post_type )
  395. clean_page_cache( $post_id );
  396. else
  397. clean_post_cache( $post_id );
  398. do_action('edit_post', $post_id);
  399. return true;
  400. }
  401. //
  402. // Ping and trackback functions.
  403. //
  404. function discover_pingback_server_uri($url, $timeout_bytes = 2048) {
  405. global $wp_version;
  406. $byte_count = 0;
  407. $contents = '';
  408. $headers = '';
  409. $pingback_str_dquote = 'rel="pingback"';
  410. $pingback_str_squote = 'rel=\'pingback\'';
  411. $x_pingback_str = 'x-pingback: ';
  412. $pingback_href_original_pos = 27;
  413. extract(parse_url($url), EXTR_SKIP);
  414. if ( !isset($host) ) // Not an URL. This should never happen.
  415. return false;
  416. $path = ( !isset($path) ) ? '/' : $path;
  417. $path .= ( isset($query) ) ? '?' . $query : '';
  418. $port = ( isset($port) ) ? $port : 80;
  419. // Try to connect to the server at $host
  420. $fp = @fsockopen($host, $port, $errno, $errstr, 2);
  421. if ( !$fp ) // Couldn't open a connection to $host
  422. return false;
  423. // Send the GET request
  424. $request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress/$wp_version \r\n\r\n";
  425. // ob_end_flush();
  426. fputs($fp, $request);
  427. // Let's check for an X-Pingback header first
  428. while ( !feof($fp) ) {
  429. $line = fgets($fp, 512);
  430. if ( trim($line) == '' )
  431. break;
  432. $headers .= trim($line)."\n";
  433. $x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str);
  434. if ( $x_pingback_header_offset ) {
  435. // We got it!
  436. preg_match('#x-pingback: (.+)#is', $headers, $matches);
  437. $pingback_server_url = trim($matches[1]);
  438. return $pingback_server_url;
  439. }
  440. if ( strpos(strtolower($headers), 'content-type: ') ) {
  441. preg_match('#content-type: (.+)#is', $headers, $matches);
  442. $content_type = trim($matches[1]);
  443. }
  444. }
  445. if ( preg_match('#(image|audio|video|model)/#is', $content_type) ) // Not an (x)html, sgml, or xml page, no use going further
  446. return false;
  447. while ( !feof($fp) ) {
  448. $line = fgets($fp, 1024);
  449. $contents .= trim($line);
  450. $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
  451. $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
  452. if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
  453. $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
  454. $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
  455. $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
  456. $pingback_href_start = $pingback_href_pos+6;
  457. $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
  458. $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
  459. $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
  460. // We may find rel="pingback" but an incomplete pingback URL
  461. if ( $pingback_server_url_len > 0 ) // We got it!
  462. return $pingback_server_url;
  463. }
  464. $byte_count += strlen($line);
  465. if ( $byte_count > $timeout_bytes ) {
  466. // It's no use going further, there probably isn't any pingback
  467. // server to find in this file. (Prevents loading large files.)
  468. return false;
  469. }
  470. }
  471. // We didn't find anything.
  472. return false;
  473. }
  474. function do_all_pings() {
  475. global $wpdb;
  476. // Do pingbacks
  477. while ($ping = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) {
  478. $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';");
  479. pingback($ping->post_content, $ping->ID);
  480. }
  481. // Do Enclosures
  482. while ($enclosure = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) {
  483. $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$enclosure->ID} AND meta_key = '_encloseme';");
  484. do_enclose($enclosure->post_content, $enclosure->ID);
  485. }
  486. // Do Trackbacks
  487. $trackbacks = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE CHAR_LENGTH(TRIM(to_ping)) > 7 AND post_status = 'publish'");
  488. if ( is_array($trackbacks) ) {
  489. foreach ( $trackbacks as $trackback )
  490. do_trackbacks($trackback->ID);
  491. }
  492. //Do Update Services/Generic Pings
  493. generic_ping();
  494. }
  495. function do_trackbacks($post_id) {
  496. global $wpdb;
  497. $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_id");
  498. $to_ping = get_to_ping($post_id);
  499. $pinged = get_pung($post_id);
  500. if ( empty($to_ping) ) {
  501. $wpdb->query("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = '$post_id'");
  502. return;
  503. }
  504. if ( empty($post->post_excerpt) )
  505. $excerpt = apply_filters('the_content', $post->post_content);
  506. else
  507. $excerpt = apply_filters('the_excerpt', $post->post_excerpt);
  508. $excerpt = str_replace(']]>', ']]&gt;', $excerpt);
  509. $excerpt = strip_tags($excerpt);
  510. if ( function_exists('mb_strcut') ) // For international trackbacks
  511. $excerpt = mb_strcut($excerpt, 0, 252, get_option('blog_charset')) . '...';
  512. else
  513. $excerpt = substr($excerpt, 0, 252) . '...';
  514. $post_title = apply_filters('the_title', $post->post_title);
  515. $post_title = strip_tags($post_title);
  516. if ( $to_ping ) {
  517. foreach ( (array) $to_ping as $tb_ping ) {
  518. $tb_ping = trim($tb_ping);
  519. if ( !in_array($tb_ping, $pinged) ) {
  520. trackback($tb_ping, $post_title, $excerpt, $post_id);
  521. $pinged[] = $tb_ping;
  522. } else {
  523. $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = '$post_id'");
  524. }
  525. }
  526. }
  527. }
  528. function generic_ping($post_id = 0) {
  529. $services = get_option('ping_sites');
  530. $services = preg_replace("|(\s)+|", '$1', $services); // Kill dupe lines
  531. $services = trim($services);
  532. if ( '' != $services ) {
  533. $services = explode("\n", $services);
  534. foreach ( (array) $services as $service )
  535. weblog_ping($service);
  536. }
  537. return $post_id;
  538. }
  539. function pingback($content, $post_ID) {
  540. global $wp_version, $wpdb;
  541. include_once(ABSPATH . WPINC . '/class-IXR.php');
  542. // original code by Mort (http://mort.mine.nu:8080)
  543. $log = debug_fopen(ABSPATH . '/pingback.log', 'a');
  544. $post_links = array();
  545. debug_fwrite($log, 'BEGIN ' . date('YmdHis', time()) . "\n");
  546. $pung = get_pung($post_ID);
  547. // Variables
  548. $ltrs = '\w';
  549. $gunk = '/#~:.?+=&%@!\-';
  550. $punc = '.:?\-';
  551. $any = $ltrs . $gunk . $punc;
  552. // Step 1
  553. // Parsing the post, external links (if any) are stored in the $post_links array
  554. // This regexp comes straight from phpfreaks.com
  555. // http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
  556. preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
  557. // Debug
  558. debug_fwrite($log, 'Post contents:');
  559. debug_fwrite($log, $content."\n");
  560. // Step 2.
  561. // Walking thru the links array
  562. // first we get rid of links pointing to sites, not to specific files
  563. // Example:
  564. // http://dummy-weblog.org
  565. // http://dummy-weblog.org/
  566. // http://dummy-weblog.org/post.php
  567. // We don't wanna ping first and second types, even if they have a valid <link/>
  568. foreach ( $post_links_temp[0] as $link_test ) :
  569. if ( !in_array($link_test, $pung) && (url_to_postid($link_test) != $post_ID) // If we haven't pung it already and it isn't a link to itself
  570. && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
  571. $test = parse_url($link_test);
  572. if ( isset($test['query']) )
  573. $post_links[] = $link_test;
  574. elseif ( ($test['path'] != '/') && ($test['path'] != '') )
  575. $post_links[] = $link_test;
  576. endif;
  577. endforeach;
  578. do_action_ref_array('pre_ping', array(&$post_links, &$pung));
  579. foreach ( (array) $post_links as $pagelinkedto ) {
  580. debug_fwrite($log, "Processing -- $pagelinkedto\n");
  581. $pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048);
  582. if ( $pingback_server_url ) {
  583. @ set_time_limit( 60 );
  584. // Now, the RPC call
  585. debug_fwrite($log, "Page Linked To: $pagelinkedto \n");
  586. debug_fwrite($log, 'Page Linked From: ');
  587. $pagelinkedfrom = get_permalink($post_ID);
  588. debug_fwrite($log, $pagelinkedfrom."\n");
  589. // using a timeout of 3 seconds should be enough to cover slow servers
  590. $client = new IXR_Client($pingback_server_url);
  591. $client->timeout = 3;
  592. $client->useragent .= ' -- WordPress/' . $wp_version;
  593. // when set to true, this outputs debug messages by itself
  594. $client->debug = false;
  595. if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto ) )
  596. add_ping( $post_ID, $pagelinkedto );
  597. else
  598. debug_fwrite($log, "Error.\n Fault code: ".$client->getErrorCode()." : ".$client->getErrorMessage()."\n");
  599. }
  600. }
  601. debug_fwrite($log, "\nEND: ".time()."\n****************************\n");
  602. debug_fclose($log);
  603. }
  604. function privacy_ping_filter($sites) {
  605. if ( '0' != get_option('blog_public') )
  606. return $sites;
  607. else
  608. return '';
  609. }
  610. // Send a Trackback
  611. function trackback($trackback_url, $title, $excerpt, $ID) {
  612. global $wpdb, $wp_version;
  613. if ( empty($trackback_url) )
  614. return;
  615. $title = urlencode($title);
  616. $excerpt = urlencode($excerpt);
  617. $blog_name = urlencode(get_option('blogname'));
  618. $tb_url = $trackback_url;
  619. $url = urlencode(get_permalink($ID));
  620. $query_string = "title=$title&url=$url&blog_name=$blog_name&excerpt=$excerpt";
  621. $trackback_url = parse_url($trackback_url);
  622. $http_request = 'POST ' . $trackback_url['path'] . ($trackback_url['query'] ? '?'.$trackback_url['query'] : '') . " HTTP/1.0\r\n";
  623. $http_request .= 'Host: '.$trackback_url['host']."\r\n";
  624. $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_option('blog_charset')."\r\n";
  625. $http_request .= 'Content-Length: '.strlen($query_string)."\r\n";
  626. $http_request .= "User-Agent: WordPress/" . $wp_version;
  627. $http_request .= "\r\n\r\n";
  628. $http_request .= $query_string;
  629. if ( '' == $trackback_url['port'] )
  630. $trackback_url['port'] = 80;
  631. $fs = @fsockopen($trackback_url['host'], $trackback_url['port'], $errno, $errstr, 4);
  632. @fputs($fs, $http_request);
  633. /*
  634. $debug_file = 'trackback.log';
  635. $fp = fopen($debug_file, 'a');
  636. fwrite($fp, "\n*****\nRequest:\n\n$http_request\n\nResponse:\n\n");
  637. while(!@feof($fs)) {
  638. fwrite($fp, @fgets($fs, 4096));
  639. }
  640. fwrite($fp, "\n\n");
  641. fclose($fp);
  642. */
  643. @fclose($fs);
  644. $tb_url = addslashes( $tb_url );
  645. $wpdb->query("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = '$ID'");
  646. return $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = '$ID'");
  647. }
  648. function weblog_ping($server = '', $path = '') {
  649. global $wp_version;
  650. include_once(ABSPATH . WPINC . '/class-IXR.php');
  651. // using a timeout of 3 seconds should be enough to cover slow servers
  652. $client = new IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
  653. $client->timeout = 3;
  654. $client->useragent .= ' -- WordPress/'.$wp_version;
  655. // when set to true, this outputs debug messages by itself
  656. $client->debug = false;
  657. $home = trailingslashit( get_option('home') );
  658. if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
  659. $client->query('weblogUpdates.ping', get_option('blogname'), $home);
  660. }
  661. ?>