PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

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