PageRenderTime 62ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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