PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

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