PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/2.0.11/wp-includes/comment-functions.php

#
PHP | 932 lines | 734 code | 153 blank | 45 comment | 170 complexity | 39b1161de75d40e88b81021ea38b32e8 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. // Template functions
  3. function wp_comment_form_unfiltered_html_nonce() {
  4. global $post;
  5. if ( current_user_can('unfiltered_html') )
  6. wp_nonce_field('unfiltered-html-comment_' . $post->ID, '_wp_unfiltered_html_comment', false);
  7. }
  8. function comments_template( $file = '/comments.php' ) {
  9. global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity;
  10. if ( ! (is_single() || is_page() || $withcomments) )
  11. return;
  12. $req = get_settings('require_name_email');
  13. $commenter = wp_get_current_commenter();
  14. extract($commenter);
  15. // TODO: Use API instead of SELECTs.
  16. if ( empty($comment_author) ) {
  17. $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_approved = '1' ORDER BY comment_date");
  18. } else {
  19. $author_db = $wpdb->escape($comment_author);
  20. $email_db = $wpdb->escape($comment_author_email);
  21. $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND ( comment_approved = '1' OR ( comment_author = '$author_db' AND comment_author_email = '$email_db' AND comment_approved = '0' ) ) ORDER BY comment_date");
  22. }
  23. define('COMMENTS_TEMPLATE', true);
  24. $include = apply_filters('comments_template', TEMPLATEPATH . $file );
  25. if ( file_exists( $include ) )
  26. require( $include );
  27. else
  28. require( ABSPATH . 'wp-content/themes/default/comments.php');
  29. }
  30. function wp_new_comment( $commentdata ) {
  31. $commentdata = apply_filters('preprocess_comment', $commentdata);
  32. $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
  33. $commentdata['user_ID'] = (int) $commentdata['user_ID'];
  34. $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR'];
  35. $commentdata['comment_agent'] = $_SERVER['HTTP_USER_AGENT'];
  36. $commentdata['comment_date'] = current_time('mysql');
  37. $commentdata['comment_date_gmt'] = current_time('mysql', 1);
  38. $commentdata = wp_filter_comment($commentdata);
  39. $commentdata['comment_approved'] = wp_allow_comment($commentdata);
  40. $comment_ID = wp_insert_comment($commentdata);
  41. do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
  42. if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
  43. if ( '0' == $commentdata['comment_approved'] )
  44. wp_notify_moderator($comment_ID);
  45. $post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment
  46. if ( get_settings('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] )
  47. wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
  48. }
  49. return $comment_ID;
  50. }
  51. function wp_insert_comment($commentdata) {
  52. global $wpdb;
  53. extract($commentdata);
  54. if ( ! isset($comment_author_IP) )
  55. $comment_author_IP = $_SERVER['REMOTE_ADDR'];
  56. if ( ! isset($comment_date) )
  57. $comment_date = current_time('mysql');
  58. if ( ! isset($comment_date_gmt) )
  59. $comment_date_gmt = gmdate('Y-m-d H:i:s', strtotime($comment_date) );
  60. if ( ! isset($comment_parent) )
  61. $comment_parent = 0;
  62. if ( ! isset($comment_approved) )
  63. $comment_approved = 1;
  64. $result = $wpdb->query("INSERT INTO $wpdb->comments
  65. (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)
  66. VALUES
  67. ('$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')
  68. ");
  69. $id = (int) $wpdb->insert_id;
  70. if ( $comment_approved == 1) {
  71. $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'");
  72. $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $count WHERE ID = '$comment_post_ID'" );
  73. }
  74. return $id;
  75. }
  76. function wp_filter_comment($commentdata) {
  77. $commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_ID']);
  78. $commentdata['comment_agent'] = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']);
  79. $commentdata['comment_author'] = apply_filters('pre_comment_author_name', $commentdata['comment_author']);
  80. $commentdata['comment_content'] = apply_filters('pre_comment_content', $commentdata['comment_content']);
  81. $commentdata['comment_author_IP'] = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']);
  82. $commentdata['comment_author_url'] = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']);
  83. $commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']);
  84. $commentdata['filtered'] = true;
  85. return $commentdata;
  86. }
  87. function wp_allow_comment($commentdata) {
  88. global $wpdb;
  89. extract($commentdata);
  90. // Simple duplicate check
  91. $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
  92. if ( $comment_author_email )
  93. $dupe .= "OR comment_author_email = '$comment_author_email' ";
  94. $dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
  95. if ( $wpdb->get_var($dupe) )
  96. die( __('Duplicate comment detected; it looks as though you\'ve already said that!') );
  97. // Simple flood-protection
  98. 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") ) {
  99. $time_lastcomment = mysql2date('U', $lasttime);
  100. $time_newcomment = mysql2date('U', $comment_date_gmt);
  101. if ( ($time_newcomment - $time_lastcomment) < 15 ) {
  102. do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment);
  103. die( __('Sorry, you can only post a new comment once every 15 seconds. Slow down cowboy.') );
  104. }
  105. }
  106. if ( $user_id ) {
  107. $userdata = get_userdata($user_id);
  108. $user = new WP_User($user_id);
  109. $post_author = $wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = '$comment_post_ID' LIMIT 1");
  110. }
  111. // The author and the admins get respect.
  112. if ( $userdata && ( $user_id == $post_author || $user->has_cap('level_9') ) ) {
  113. $approved = 1;
  114. }
  115. // Everyone else's comments will be checked.
  116. else {
  117. if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) )
  118. $approved = 1;
  119. else
  120. $approved = 0;
  121. if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) )
  122. $approved = 'spam';
  123. }
  124. $approved = apply_filters('pre_comment_approved', $approved);
  125. return $approved;
  126. }
  127. function wp_update_comment($commentarr) {
  128. global $wpdb;
  129. // First, get all of the original fields
  130. $comment = get_comment($commentarr['comment_ID'], ARRAY_A);
  131. // Escape data pulled from DB.
  132. foreach ($comment as $key => $value)
  133. $comment[$key] = $wpdb->escape($value);
  134. // Merge old and new fields with new fields overwriting old ones.
  135. $commentarr = array_merge($comment, $commentarr);
  136. $commentarr = wp_filter_comment( $commentarr );
  137. // Now extract the merged array.
  138. extract($commentarr);
  139. $comment_content = apply_filters('comment_save_pre', $comment_content);
  140. $result = $wpdb->query(
  141. "UPDATE $wpdb->comments SET
  142. comment_content = '$comment_content',
  143. comment_author = '$comment_author',
  144. comment_author_email = '$comment_author_email',
  145. comment_approved = '$comment_approved',
  146. comment_author_url = '$comment_author_url',
  147. comment_date = '$comment_date'
  148. WHERE comment_ID = $comment_ID" );
  149. $rval = $wpdb->rows_affected;
  150. $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" );
  151. if( is_object( $c ) )
  152. $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" );
  153. do_action('edit_comment', $comment_ID);
  154. return $rval;
  155. }
  156. function wp_delete_comment($comment_id) {
  157. global $wpdb;
  158. do_action('delete_comment', $comment_id);
  159. $comment = get_comment($comment_id);
  160. if ( ! $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1") )
  161. return false;
  162. $post_id = $comment->comment_post_ID;
  163. if ( $post_id && $comment->comment_approved == 1 ) {
  164. $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'");
  165. $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $count WHERE ID = '$post_id'" );
  166. }
  167. do_action('wp_set_comment_status', $comment_id, 'delete');
  168. return true;
  169. }
  170. function get_comments_number( $post_id = 0 ) {
  171. global $wpdb, $comment_count_cache, $id;
  172. $post_id = (int) $post_id;
  173. if ( !$post_id )
  174. $post_id = (int) $id;
  175. if ( !isset($comment_count_cache[$post_id]) )
  176. $comment_count_cache[$id] = $wpdb->get_var("SELECT comment_count FROM $wpdb->posts WHERE ID = '$post_id'");
  177. return apply_filters('get_comments_number', $comment_count_cache[$post_id]);
  178. }
  179. function comments_number( $zero = 'No Comments', $one = '1 Comment', $more = '% Comments', $number = '' ) {
  180. global $id, $comment;
  181. $number = get_comments_number( $id );
  182. if ($number == 0) {
  183. $blah = $zero;
  184. } elseif ($number == 1) {
  185. $blah = $one;
  186. } elseif ($number > 1) {
  187. $blah = str_replace('%', $number, $more);
  188. }
  189. echo apply_filters('comments_number', $blah);
  190. }
  191. function get_comments_link() {
  192. return get_permalink() . '#comments';
  193. }
  194. function get_comment_link() {
  195. global $comment;
  196. return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
  197. }
  198. function comments_link( $file = '', $echo = true ) {
  199. echo get_comments_link();
  200. }
  201. function comments_popup_script($width=400, $height=400, $file='') {
  202. global $wpcommentspopupfile, $wptrackbackpopupfile, $wppingbackpopupfile, $wpcommentsjavascript;
  203. if (empty ($file)) {
  204. $wpcommentspopupfile = ''; // Use the index.
  205. } else {
  206. $wpcommentspopupfile = $file;
  207. }
  208. $wpcommentsjavascript = 1;
  209. $javascript = "<script type='text/javascript'>\nfunction wpopen (macagna) {\n window.open(macagna, '_blank', 'width=$width,height=$height,scrollbars=yes,status=yes');\n}\n</script>\n";
  210. echo $javascript;
  211. }
  212. function comments_popup_link($zero='No Comments', $one='1 Comment', $more='% Comments', $CSSclass='', $none='Comments Off') {
  213. global $id, $wpcommentspopupfile, $wpcommentsjavascript, $post, $wpdb;
  214. global $comment_count_cache;
  215. if (! is_single() && ! is_page()) {
  216. if ( !isset($comment_count_cache[$id]) )
  217. $comment_count_cache[$id] = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = $id AND comment_approved = '1';");
  218. $number = $comment_count_cache[$id];
  219. if (0 == $number && 'closed' == $post->comment_status && 'closed' == $post->ping_status) {
  220. echo $none;
  221. return;
  222. } else {
  223. if (!empty($post->post_password)) { // if there's a password
  224. if ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie
  225. echo(__('Enter your password to view comments'));
  226. return;
  227. }
  228. }
  229. echo '<a href="';
  230. if ($wpcommentsjavascript) {
  231. if ( empty($wpcommentspopupfile) )
  232. $home = get_settings('home');
  233. else
  234. $home = get_settings('siteurl');
  235. echo $home . '/' . $wpcommentspopupfile.'?comments_popup='.$id;
  236. echo '" onclick="wpopen(this.href); return false"';
  237. } else { // if comments_popup_script() is not in the template, display simple comment link
  238. if ( 0 == $number )
  239. echo get_permalink() . '#respond';
  240. else
  241. comments_link();
  242. echo '"';
  243. }
  244. if (!empty($CSSclass)) {
  245. echo ' class="'.$CSSclass.'"';
  246. }
  247. $title = attribute_escape(apply_filters('the_title', get_the_title()));
  248. echo ' title="' . sprintf( __('Comment on %s'), $title ) .'">';
  249. comments_number($zero, $one, $more, $number);
  250. echo '</a>';
  251. }
  252. }
  253. }
  254. function get_comment_ID() {
  255. global $comment;
  256. return apply_filters('get_comment_ID', $comment->comment_ID);
  257. }
  258. function comment_ID() {
  259. echo get_comment_ID();
  260. }
  261. function get_comment_author() {
  262. global $comment;
  263. if ( empty($comment->comment_author) )
  264. $author = __('Anonymous');
  265. else
  266. $author = $comment->comment_author;
  267. return apply_filters('get_comment_author', $author);
  268. }
  269. function comment_author() {
  270. $author = apply_filters('comment_author', get_comment_author() );
  271. echo $author;
  272. }
  273. function get_comment_author_email() {
  274. global $comment;
  275. return apply_filters('get_comment_author_email', $comment->comment_author_email);
  276. }
  277. function comment_author_email() {
  278. echo apply_filters('author_email', get_comment_author_email() );
  279. }
  280. function get_comment_author_link() {
  281. global $comment;
  282. $url = get_comment_author_url();
  283. $author = get_comment_author();
  284. if ( empty( $url ) || 'http://' == $url )
  285. $return = $author;
  286. else
  287. $return = "<a href='$url' rel='external nofollow'>$author</a>";
  288. return apply_filters('get_comment_author_link', $return);
  289. }
  290. function comment_author_link() {
  291. echo get_comment_author_link();
  292. }
  293. function get_comment_type() {
  294. global $comment;
  295. if ( '' == $comment->comment_type )
  296. $comment->comment_type = 'comment';
  297. return apply_filters('get_comment_type', $comment->comment_type);
  298. }
  299. function comment_type($commenttxt = 'Comment', $trackbacktxt = 'Trackback', $pingbacktxt = 'Pingback') {
  300. $type = get_comment_type();
  301. switch( $type ) {
  302. case 'trackback' :
  303. echo $trackbacktxt;
  304. break;
  305. case 'pingback' :
  306. echo $pingbacktxt;
  307. break;
  308. default :
  309. echo $commenttxt;
  310. }
  311. }
  312. function get_comment_author_url() {
  313. global $comment;
  314. return apply_filters('get_comment_author_url', $comment->comment_author_url);
  315. }
  316. function comment_author_url() {
  317. echo apply_filters('comment_url', get_comment_author_url());
  318. }
  319. function comment_author_email_link($linktext='', $before='', $after='') {
  320. global $comment;
  321. $email = apply_filters('comment_email', $comment->comment_author_email);
  322. if ((!empty($email)) && ($email != '@')) {
  323. $display = ($linktext != '') ? $linktext : $email;
  324. echo $before;
  325. echo "<a href='mailto:$email'>$display</a>";
  326. echo $after;
  327. }
  328. }
  329. function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
  330. global $comment;
  331. $url = get_comment_author_url();
  332. $display = ($linktext != '') ? $linktext : $url;
  333. $return = "$before<a href='$url' rel='external'>$display</a>$after";
  334. return apply_filters('get_comment_author_url_link', $return);
  335. }
  336. function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
  337. echo get_comment_author_url_link( $linktext, $before, $after );
  338. }
  339. function get_comment_author_IP() {
  340. global $comment;
  341. return apply_filters('get_comment_author_IP', $comment->comment_author_IP);
  342. }
  343. function comment_author_IP() {
  344. echo get_comment_author_IP();
  345. }
  346. function get_comment_text() {
  347. global $comment;
  348. return apply_filters('get_comment_text', $comment->comment_content);
  349. }
  350. function comment_text() {
  351. echo apply_filters('comment_text', get_comment_text() );
  352. }
  353. function get_comment_excerpt() {
  354. global $comment;
  355. $comment_text = strip_tags($comment->comment_content);
  356. $blah = explode(' ', $comment_text);
  357. if (count($blah) > 20) {
  358. $k = 20;
  359. $use_dotdotdot = 1;
  360. } else {
  361. $k = count($blah);
  362. $use_dotdotdot = 0;
  363. }
  364. $excerpt = '';
  365. for ($i=0; $i<$k; $i++) {
  366. $excerpt .= $blah[$i] . ' ';
  367. }
  368. $excerpt .= ($use_dotdotdot) ? '...' : '';
  369. return apply_filters('get_comment_excerpt', $excerpt);
  370. }
  371. function comment_excerpt() {
  372. echo apply_filters('comment_excerpt', get_comment_excerpt() );
  373. }
  374. function get_comment_date( $d = '' ) {
  375. global $comment;
  376. if ( '' == $d )
  377. $date = mysql2date( get_settings('date_format'), $comment->comment_date);
  378. else
  379. $date = mysql2date($d, $comment->comment_date);
  380. return apply_filters('get_comment_date', $date);
  381. }
  382. function comment_date( $d = '' ) {
  383. echo get_comment_date( $d );
  384. }
  385. function get_comment_time( $d = '', $gmt = false ) {
  386. global $comment;
  387. $comment_date = $gmt? $comment->comment_date_gmt : $comment->comment_date;
  388. if ( '' == $d )
  389. $date = mysql2date(get_settings('time_format'), $comment_date);
  390. else
  391. $date = mysql2date($d, $comment_date);
  392. return apply_filters('get_comment_time', $date);
  393. }
  394. function comment_time( $d = '' ) {
  395. echo get_comment_time($d);
  396. }
  397. function get_trackback_url() {
  398. global $id;
  399. $tb_url = get_settings('siteurl') . '/wp-trackback.php?p=' . $id;
  400. if ( '' != get_settings('permalink_structure') )
  401. $tb_url = trailingslashit(get_permalink()) . 'trackback/';
  402. return $tb_url;
  403. }
  404. function trackback_url( $display = true ) {
  405. if ( $display)
  406. echo get_trackback_url();
  407. else
  408. return get_trackback_url();
  409. }
  410. function trackback_rdf($timezone = 0) {
  411. global $id;
  412. if (!stristr($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator')) {
  413. echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  414. xmlns:dc="http://purl.org/dc/elements/1.1/"
  415. xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  416. <rdf:Description rdf:about="';
  417. the_permalink();
  418. echo '"'."\n";
  419. echo ' dc:identifier="';
  420. the_permalink();
  421. echo '"'."\n";
  422. echo ' dc:title="'.str_replace('--', '&#x2d;&#x2d;', wptexturize(strip_tags(get_the_title()))).'"'."\n";
  423. echo ' trackback:ping="'.trackback_url(0).'"'." />\n";
  424. echo '</rdf:RDF>';
  425. }
  426. }
  427. function comments_open() {
  428. global $post;
  429. if ( 'open' == $post->comment_status )
  430. return true;
  431. else
  432. return false;
  433. }
  434. function pings_open() {
  435. global $post;
  436. if ( 'open' == $post->ping_status )
  437. return true;
  438. else
  439. return false;
  440. }
  441. // Non-template functions
  442. function get_lastcommentmodified($timezone = 'server') {
  443. global $cache_lastcommentmodified, $pagenow, $wpdb;
  444. $add_seconds_blog = get_settings('gmt_offset') * 3600;
  445. $add_seconds_server = date('Z');
  446. $now = current_time('mysql', 1);
  447. if ( !isset($cache_lastcommentmodified[$timezone]) ) {
  448. switch(strtolower($timezone)) {
  449. case 'gmt':
  450. $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
  451. break;
  452. case 'blog':
  453. $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
  454. break;
  455. case 'server':
  456. $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");
  457. break;
  458. }
  459. $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
  460. } else {
  461. $lastcommentmodified = $cache_lastcommentmodified[$timezone];
  462. }
  463. return $lastcommentmodified;
  464. }
  465. function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) { // less flexible, but saves DB queries
  466. global $postc, $id, $commentdata, $wpdb;
  467. if ($no_cache) {
  468. $query = "SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_ID'";
  469. if (false == $include_unapproved) {
  470. $query .= " AND comment_approved = '1'";
  471. }
  472. $myrow = $wpdb->get_row($query, ARRAY_A);
  473. } else {
  474. $myrow['comment_ID'] = $postc->comment_ID;
  475. $myrow['comment_post_ID'] = $postc->comment_post_ID;
  476. $myrow['comment_author'] = $postc->comment_author;
  477. $myrow['comment_author_email'] = $postc->comment_author_email;
  478. $myrow['comment_author_url'] = $postc->comment_author_url;
  479. $myrow['comment_author_IP'] = $postc->comment_author_IP;
  480. $myrow['comment_date'] = $postc->comment_date;
  481. $myrow['comment_content'] = $postc->comment_content;
  482. $myrow['comment_karma'] = $postc->comment_karma;
  483. $myrow['comment_approved'] = $postc->comment_approved;
  484. $myrow['comment_type'] = $postc->comment_type;
  485. }
  486. return $myrow;
  487. }
  488. function pingback($content, $post_ID) {
  489. global $wp_version, $wpdb;
  490. include_once (ABSPATH . WPINC . '/class-IXR.php');
  491. // original code by Mort (http://mort.mine.nu:8080)
  492. $log = debug_fopen(ABSPATH . '/pingback.log', 'a');
  493. $post_links = array();
  494. debug_fwrite($log, 'BEGIN '.date('YmdHis', time())."\n");
  495. $pung = get_pung($post_ID);
  496. // Variables
  497. $ltrs = '\w';
  498. $gunk = '/#~:.?+=&%@!\-';
  499. $punc = '.:?\-';
  500. $any = $ltrs . $gunk . $punc;
  501. // Step 1
  502. // Parsing the post, external links (if any) are stored in the $post_links array
  503. // This regexp comes straight from phpfreaks.com
  504. // http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
  505. preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
  506. // Debug
  507. debug_fwrite($log, 'Post contents:');
  508. debug_fwrite($log, $content."\n");
  509. // Step 2.
  510. // Walking thru the links array
  511. // first we get rid of links pointing to sites, not to specific files
  512. // Example:
  513. // http://dummy-weblog.org
  514. // http://dummy-weblog.org/
  515. // http://dummy-weblog.org/post.php
  516. // We don't wanna ping first and second types, even if they have a valid <link/>
  517. foreach($post_links_temp[0] as $link_test) :
  518. 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
  519. && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
  520. $test = parse_url($link_test);
  521. if (isset($test['query']))
  522. $post_links[] = $link_test;
  523. elseif(($test['path'] != '/') && ($test['path'] != ''))
  524. $post_links[] = $link_test;
  525. endif;
  526. endforeach;
  527. do_action('pre_ping', array(&$post_links, &$pung));
  528. foreach ($post_links as $pagelinkedto){
  529. debug_fwrite($log, "Processing -- $pagelinkedto\n");
  530. $pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048);
  531. if ($pingback_server_url) {
  532. @ set_time_limit( 60 );
  533. // Now, the RPC call
  534. debug_fwrite($log, "Page Linked To: $pagelinkedto \n");
  535. debug_fwrite($log, 'Page Linked From: ');
  536. $pagelinkedfrom = get_permalink($post_ID);
  537. debug_fwrite($log, $pagelinkedfrom."\n");
  538. // using a timeout of 3 seconds should be enough to cover slow servers
  539. $client = new IXR_Client($pingback_server_url);
  540. $client->timeout = 3;
  541. $client->useragent .= ' -- WordPress/' . $wp_version;
  542. // when set to true, this outputs debug messages by itself
  543. $client->debug = false;
  544. if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto ) )
  545. add_ping( $post_ID, $pagelinkedto );
  546. else
  547. debug_fwrite($log, "Error.\n Fault code: ".$client->getErrorCode()." : ".$client->getErrorMessage()."\n");
  548. }
  549. }
  550. debug_fwrite($log, "\nEND: ".time()."\n****************************\n");
  551. debug_fclose($log);
  552. }
  553. function discover_pingback_server_uri($url, $timeout_bytes = 2048) {
  554. global $wp_version;
  555. $byte_count = 0;
  556. $contents = '';
  557. $headers = '';
  558. $pingback_str_dquote = 'rel="pingback"';
  559. $pingback_str_squote = 'rel=\'pingback\'';
  560. $x_pingback_str = 'x-pingback: ';
  561. $pingback_href_original_pos = 27;
  562. extract(parse_url($url));
  563. if (!isset($host)) {
  564. // Not an URL. This should never happen.
  565. return false;
  566. }
  567. $path = (!isset($path)) ? '/' : $path;
  568. $path .= (isset($query)) ? '?'.$query : '';
  569. $port = (isset($port)) ? $port : 80;
  570. // Try to connect to the server at $host
  571. $fp = @fsockopen($host, $port, $errno, $errstr, 2);
  572. if (!$fp) {
  573. // Couldn't open a connection to $host;
  574. return false;
  575. }
  576. // Send the GET request
  577. $request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress/$wp_version \r\n\r\n";
  578. // ob_end_flush();
  579. fputs($fp, $request);
  580. // Let's check for an X-Pingback header first
  581. while (!feof($fp)) {
  582. $line = fgets($fp, 512);
  583. if (trim($line) == '') {
  584. break;
  585. }
  586. $headers .= trim($line)."\n";
  587. $x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str);
  588. if ($x_pingback_header_offset) {
  589. // We got it!
  590. preg_match('#x-pingback: (.+)#is', $headers, $matches);
  591. $pingback_server_url = trim($matches[1]);
  592. return $pingback_server_url;
  593. }
  594. if(strpos(strtolower($headers), 'content-type: ')) {
  595. preg_match('#content-type: (.+)#is', $headers, $matches);
  596. $content_type = trim($matches[1]);
  597. }
  598. }
  599. if (preg_match('#(image|audio|video|model)/#is', $content_type)) {
  600. // Not an (x)html, sgml, or xml page, no use going further
  601. return false;
  602. }
  603. while (!feof($fp)) {
  604. $line = fgets($fp, 1024);
  605. $contents .= trim($line);
  606. $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
  607. $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
  608. if ($pingback_link_offset_dquote || $pingback_link_offset_squote) {
  609. $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
  610. $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
  611. $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
  612. $pingback_href_start = $pingback_href_pos+6;
  613. $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
  614. $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
  615. $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
  616. // We may find rel="pingback" but an incomplete pingback URI
  617. if ($pingback_server_url_len > 0) {
  618. // We got it!
  619. return $pingback_server_url;
  620. }
  621. }
  622. $byte_count += strlen($line);
  623. if ($byte_count > $timeout_bytes) {
  624. // It's no use going further, there probably isn't any pingback
  625. // server to find in this file. (Prevents loading large files.)
  626. return false;
  627. }
  628. }
  629. // We didn't find anything.
  630. return false;
  631. }
  632. function is_local_attachment($url) {
  633. if ( !strstr($url, get_bloginfo('home') ) )
  634. return false;
  635. if ( strstr($url, get_bloginfo('home') . '/?attachment_id=') )
  636. return true;
  637. if ( $id = url_to_postid($url) ) {
  638. $post = & get_post($id);
  639. if ( 'attachment' == $post->post_status )
  640. return true;
  641. }
  642. return false;
  643. }
  644. function wp_set_comment_status($comment_id, $comment_status) {
  645. global $wpdb;
  646. switch($comment_status) {
  647. case 'hold':
  648. $query = "UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1";
  649. break;
  650. case 'approve':
  651. $query = "UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1";
  652. break;
  653. case 'spam':
  654. $query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1";
  655. break;
  656. case 'delete':
  657. return wp_delete_comment($comment_id);
  658. break;
  659. default:
  660. return false;
  661. }
  662. if ($wpdb->query($query)) {
  663. do_action('wp_set_comment_status', $comment_id, $comment_status);
  664. $comment = get_comment($comment_id);
  665. $comment_post_ID = $comment->comment_post_ID;
  666. $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" );
  667. if( is_object( $c ) )
  668. $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" );
  669. return true;
  670. } else {
  671. return false;
  672. }
  673. }
  674. function wp_get_comment_status($comment_id) {
  675. global $wpdb;
  676. $result = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
  677. if ($result == NULL) {
  678. return 'deleted';
  679. } else if ($result == '1') {
  680. return 'approved';
  681. } else if ($result == '0') {
  682. return 'unapproved';
  683. } else if ($result == 'spam') {
  684. return 'spam';
  685. } else {
  686. return false;
  687. }
  688. }
  689. function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
  690. global $wpdb;
  691. if (1 == get_settings('comment_moderation')) return false; // If moderation is set to manual
  692. if ( preg_match_all("|(href\t*?=\t*?['\"]?)?(https?:)?//|i", $comment, $out) >= get_option('comment_max_links') )
  693. return false; // Check # of external links
  694. $mod_keys = trim( get_settings('moderation_keys') );
  695. if ( !empty($mod_keys) ) {
  696. $words = explode("\n", $mod_keys );
  697. foreach ($words as $word) {
  698. $word = trim($word);
  699. // Skip empty lines
  700. if (empty($word)) { continue; }
  701. // Do some escaping magic so that '#' chars in the
  702. // spam words don't break things:
  703. $word = preg_quote($word, '#');
  704. $pattern = "#$word#i";
  705. if ( preg_match($pattern, $author) ) return false;
  706. if ( preg_match($pattern, $email) ) return false;
  707. if ( preg_match($pattern, $url) ) return false;
  708. if ( preg_match($pattern, $comment) ) return false;
  709. if ( preg_match($pattern, $user_ip) ) return false;
  710. if ( preg_match($pattern, $user_agent) ) return false;
  711. }
  712. }
  713. // Comment whitelisting:
  714. if ( 1 == get_settings('comment_whitelist')) {
  715. if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll
  716. $uri = parse_url($url);
  717. $domain = $uri['host'];
  718. $uri = parse_url( get_option('home') );
  719. $home_domain = $uri['host'];
  720. if ( $wpdb->get_var("SELECT link_id FROM $wpdb->links WHERE link_url LIKE ('%$domain%') LIMIT 1") || $domain == $home_domain )
  721. return true;
  722. else
  723. return false;
  724. } elseif( $author != '' && $email != '' ) {
  725. $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");
  726. if ( ( 1 == $ok_to_comment ) &&
  727. ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
  728. return true;
  729. else
  730. return false;
  731. } else {
  732. return false;
  733. }
  734. }
  735. return true;
  736. }
  737. function get_approved_comments($post_id) {
  738. global $wpdb;
  739. $post_id = (int) $post_id;
  740. return $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post_id AND comment_approved = '1' ORDER BY comment_date");
  741. }
  742. function sanitize_comment_cookies() {
  743. if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) {
  744. $comment_author = apply_filters('pre_comment_author_name', $_COOKIE['comment_author_'.COOKIEHASH]);
  745. $comment_author = stripslashes($comment_author);
  746. $comment_author = attribute_escape($comment_author);
  747. $_COOKIE['comment_author_'.COOKIEHASH] = $comment_author;
  748. }
  749. if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) {
  750. $comment_author_email = apply_filters('pre_comment_author_email', $_COOKIE['comment_author_email_'.COOKIEHASH]);
  751. $comment_author_email = stripslashes($comment_author_email);
  752. $comment_author_email = attribute_escape($comment_author_email);
  753. $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
  754. }
  755. if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) {
  756. $comment_author_url = apply_filters('pre_comment_author_url', $_COOKIE['comment_author_url_'.COOKIEHASH]);
  757. $comment_author_url = stripslashes($comment_author_url);
  758. $comment_author_url = attribute_escape($comment_author_url);
  759. $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
  760. }
  761. }
  762. function wp_get_current_commenter() {
  763. // Cookies should already be sanitized.
  764. $comment_author = '';
  765. if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
  766. $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
  767. $comment_author_email = '';
  768. if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
  769. $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
  770. $comment_author_url = '';
  771. if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
  772. $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
  773. return compact('comment_author', 'comment_author_email', 'comment_author_url');
  774. }
  775. ?>