PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

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