PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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