PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 774 lines | 647 code | 94 blank | 33 comment | 111 complexity | 93343e59875f33282127f6e11c871940 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 = addslashes($comment_author);
  14. $email_db = addslashes($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. if ( file_exists( TEMPLATEPATH . $file ) )
  19. require( TEMPLATEPATH . $file );
  20. else
  21. require( ABSPATH . 'wp-content/themes/default/comments.php');
  22. endif;
  23. }
  24. function clean_url( $url ) {
  25. if ('' == $url) return $url;
  26. $url = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $url);
  27. $url = str_replace(';//', '://', $url);
  28. $url = (!strstr($url, '://')) ? 'http://'.$url : $url;
  29. $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&#038;$1', $url);
  30. return $url;
  31. }
  32. function get_comments_number( $comment_id ) {
  33. global $wpdb, $comment_count_cache;
  34. $comment_id = (int) $comment_id;
  35. if (!isset($comment_count_cache[$comment_id]))
  36. $number = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$comment_id' AND comment_approved = '1'");
  37. else
  38. $number = $comment_count_cache[$comment_id];
  39. return apply_filters('get_comments_number', $number);
  40. }
  41. function comments_number( $zero = 'No Comments', $one = '1 Comment', $more = '% Comments', $number = '' ) {
  42. global $id, $comment;
  43. $number = get_comments_number( $id );
  44. if ($number == 0) {
  45. $blah = $zero;
  46. } elseif ($number == 1) {
  47. $blah = $one;
  48. } elseif ($number > 1) {
  49. $blah = str_replace('%', $number, $more);
  50. }
  51. echo apply_filters('comments_number', $blah);
  52. }
  53. function get_comments_link() {
  54. return get_permalink() . '#comments';
  55. }
  56. function get_comment_link() {
  57. global $comment;
  58. return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
  59. }
  60. function comments_link( $file = '', $echo = true ) {
  61. echo get_comments_link();
  62. }
  63. function comments_popup_script($width=400, $height=400, $file='') {
  64. global $wpcommentspopupfile, $wptrackbackpopupfile, $wppingbackpopupfile, $wpcommentsjavascript;
  65. if (empty ($file)) {
  66. $wpcommentspopupfile = ''; // Use the index.
  67. } else {
  68. $wpcommentspopupfile = $file;
  69. }
  70. $wpcommentsjavascript = 1;
  71. $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";
  72. echo $javascript;
  73. }
  74. function comments_popup_link($zero='No Comments', $one='1 Comment', $more='% Comments', $CSSclass='', $none='Comments Off') {
  75. global $id, $wpcommentspopupfile, $wpcommentsjavascript, $post, $wpdb;
  76. global $comment_count_cache;
  77. if (! is_single() && ! is_page()) {
  78. if ('' == $comment_count_cache["$id"]) {
  79. $number = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = $id AND comment_approved = '1';");
  80. } else {
  81. $number = $comment_count_cache["$id"];
  82. }
  83. if (0 == $number && 'closed' == $post->comment_status && 'closed' == $post->ping_status) {
  84. echo $none;
  85. return;
  86. } else {
  87. if (!empty($post->post_password)) { // if there's a password
  88. if ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie
  89. echo('Enter your password to view comments');
  90. return;
  91. }
  92. }
  93. echo '<a href="';
  94. if ($wpcommentsjavascript) {
  95. if ( empty($wpcommentspopupfile) )
  96. $home = get_settings('home');
  97. else
  98. $home = get_settings('siteurl');
  99. echo $home . '/' . $wpcommentspopupfile.'?comments_popup='.$id;
  100. echo '" onclick="wpopen(this.href); return false"';
  101. } else {
  102. // if comments_popup_script() is not in the template, display simple comment link
  103. comments_link();
  104. echo '"';
  105. }
  106. if (!empty($CSSclass)) {
  107. echo ' class="'.$CSSclass.'"';
  108. }
  109. echo '>';
  110. comments_number($zero, $one, $more, $number);
  111. echo '</a>';
  112. }
  113. }
  114. }
  115. function get_comment_ID() {
  116. global $comment;
  117. return apply_filters('get_comment_ID', $comment->comment_ID);
  118. }
  119. function comment_ID() {
  120. echo get_comment_ID();
  121. }
  122. function get_comment_author() {
  123. global $comment;
  124. if ( empty($comment->comment_author) )
  125. $author = 'Anonymous';
  126. else
  127. $author = $comment->comment_author;
  128. return apply_filters('get_comment_author', $author);
  129. }
  130. function comment_author() {
  131. $author = apply_filters('comment_author', get_comment_author() );
  132. echo $author;
  133. }
  134. function get_comment_author_email() {
  135. global $comment;
  136. return apply_filters('get_comment_author_email', $comment->comment_author_email);
  137. }
  138. function comment_author_email() {
  139. echo apply_filters('author_email', get_comment_author_email() );
  140. }
  141. function get_comment_author_link() {
  142. global $comment;
  143. $url = get_comment_author_url();
  144. $author = get_comment_author();
  145. if ( empty( $url ) )
  146. $return = $author;
  147. else
  148. $return = "<a href='$url' rel='external nofollow'>$author</a>";
  149. return apply_filters('get_comment_author_link', $return);
  150. }
  151. function comment_author_link() {
  152. echo get_comment_author_link();
  153. }
  154. function get_comment_type() {
  155. global $comment;
  156. if ( '' == $comment->comment_type )
  157. $comment->comment_type = 'comment';
  158. return apply_filters('get_comment_type', $comment->comment_type);
  159. }
  160. function comment_type($commenttxt = 'Comment', $trackbacktxt = 'Trackback', $pingbacktxt = 'Pingback') {
  161. $type = get_comment_type();
  162. switch( $type ) {
  163. case 'trackback' :
  164. echo $trackbacktxt;
  165. break;
  166. case 'pingback' :
  167. echo $pingbacktxt;
  168. break;
  169. default :
  170. echo $commenttxt;
  171. }
  172. }
  173. function get_comment_author_url() {
  174. global $comment;
  175. return apply_filters('get_comment_author_url', $comment->comment_author_url);
  176. }
  177. function comment_author_url() {
  178. echo apply_filters('comment_url', get_comment_author_url());
  179. }
  180. function comment_author_email_link($linktext='', $before='', $after='') {
  181. global $comment;
  182. $email = apply_filters('comment_email', $comment->comment_author_email);
  183. if ((!empty($email)) && ($email != '@')) {
  184. $display = ($linktext != '') ? $linktext : $email;
  185. echo $before;
  186. echo "<a href='mailto:$email'>$display</a>";
  187. echo $after;
  188. }
  189. }
  190. function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
  191. global $comment;
  192. $url = get_comment_author_url();
  193. $display = ($linktext != '') ? $linktext : $url;
  194. $return = "$before<a href='$url' rel='external'>$display</a>$after";
  195. return apply_filters('get_comment_author_url_link', $return);
  196. }
  197. function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
  198. echo get_comment_author_url_link( $linktext, $before, $after );
  199. }
  200. function get_comment_author_IP() {
  201. global $comment;
  202. return apply_filters('get_comment_author_IP', $comment->comment_author_IP);
  203. }
  204. function comment_author_IP() {
  205. echo get_comment_author_IP();
  206. }
  207. function get_comment_text() {
  208. global $comment;
  209. return apply_filters('get_comment_text', $comment->comment_content);
  210. }
  211. function comment_text() {
  212. echo apply_filters('comment_text', get_comment_text() );
  213. }
  214. function get_comment_excerpt() {
  215. global $comment;
  216. $comment_text = strip_tags($comment->comment_content);
  217. $blah = explode(' ', $comment_text);
  218. if (count($blah) > 20) {
  219. $k = 20;
  220. $use_dotdotdot = 1;
  221. } else {
  222. $k = count($blah);
  223. $use_dotdotdot = 0;
  224. }
  225. $excerpt = '';
  226. for ($i=0; $i<$k; $i++) {
  227. $excerpt .= $blah[$i] . ' ';
  228. }
  229. $excerpt .= ($use_dotdotdot) ? '...' : '';
  230. return apply_filters('get_comment_excerpt', $excerpt);
  231. }
  232. function comment_excerpt() {
  233. echo apply_filters('comment_excerpt', get_comment_excerpt() );
  234. }
  235. function get_comment_date( $d = '' ) {
  236. global $comment;
  237. if ( '' == $d )
  238. $date = mysql2date( get_settings('date_format'), $comment->comment_date);
  239. else
  240. $date = mysql2date($d, $comment->comment_date);
  241. return apply_filters('get_comment_date', $date);
  242. }
  243. function comment_date( $d = '' ) {
  244. echo get_comment_date( $d );
  245. }
  246. function get_comment_time( $d = '' ) {
  247. global $comment;
  248. if ( '' == $d )
  249. $date = mysql2date(get_settings('time_format'), $comment->comment_date);
  250. else
  251. $date = mysql2date($d, $comment->comment_date);
  252. return apply_filters('get_comment_time', $date);
  253. }
  254. function comment_time( $d = '' ) {
  255. echo get_comment_time($d);
  256. }
  257. function get_trackback_url() {
  258. global $id;
  259. $tb_url = get_settings('siteurl') . '/wp-trackback.php?p=' . $id;
  260. if ( '' != get_settings('permalink_structure') )
  261. $tb_url = trailingslashit(get_permalink()) . 'trackback/';
  262. return $tb_url;
  263. }
  264. function trackback_url( $display = true ) {
  265. if ( $display)
  266. echo get_trackback_url();
  267. else
  268. return get_trackback_url();
  269. }
  270. function trackback_rdf($timezone = 0) {
  271. global $id;
  272. if (!stristr($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator')) {
  273. echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  274. xmlns:dc="http://purl.org/dc/elements/1.1/"
  275. xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  276. <rdf:Description rdf:about="';
  277. the_permalink();
  278. echo '"'."\n";
  279. echo ' dc:identifier="';
  280. the_permalink();
  281. echo '"'."\n";
  282. echo ' dc:title="'.str_replace('--', '&#x2d;&#x2d;', wptexturize(strip_tags(get_the_title()))).'"'."\n";
  283. echo ' trackback:ping="'.trackback_url(0).'"'." />\n";
  284. echo '</rdf:RDF>';
  285. }
  286. }
  287. function comments_open() {
  288. global $post;
  289. if ( 'open' == $post->comment_status )
  290. return true;
  291. else
  292. return false;
  293. }
  294. function pings_open() {
  295. global $post;
  296. if ( 'open' == $post->ping_status )
  297. return true;
  298. else
  299. return false;
  300. }
  301. // Non-template functions
  302. function get_lastcommentmodified($timezone = 'server') {
  303. global $tablecomments, $cache_lastcommentmodified, $pagenow, $wpdb;
  304. $add_seconds_blog = get_settings('gmt_offset') * 3600;
  305. $add_seconds_server = date('Z');
  306. $now = current_time('mysql', 1);
  307. if ( !isset($cache_lastcommentmodified[$timezone]) ) {
  308. switch(strtolower($timezone)) {
  309. case 'gmt':
  310. $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $tablecomments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
  311. break;
  312. case 'blog':
  313. $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $tablecomments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
  314. break;
  315. case 'server':
  316. $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");
  317. break;
  318. }
  319. $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
  320. } else {
  321. $lastcommentmodified = $cache_lastcommentmodified[$timezone];
  322. }
  323. return $lastcommentmodified;
  324. }
  325. function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) { // less flexible, but saves DB queries
  326. global $postc, $id, $commentdata, $wpdb;
  327. if ($no_cache) {
  328. $query = "SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_ID'";
  329. if (false == $include_unapproved) {
  330. $query .= " AND comment_approved = '1'";
  331. }
  332. $myrow = $wpdb->get_row($query, ARRAY_A);
  333. } else {
  334. $myrow['comment_ID'] = $postc->comment_ID;
  335. $myrow['comment_post_ID'] = $postc->comment_post_ID;
  336. $myrow['comment_author'] = $postc->comment_author;
  337. $myrow['comment_author_email'] = $postc->comment_author_email;
  338. $myrow['comment_author_url'] = $postc->comment_author_url;
  339. $myrow['comment_author_IP'] = $postc->comment_author_IP;
  340. $myrow['comment_date'] = $postc->comment_date;
  341. $myrow['comment_content'] = $postc->comment_content;
  342. $myrow['comment_karma'] = $postc->comment_karma;
  343. $myrow['comment_approved'] = $postc->comment_approved;
  344. $myrow['comment_type'] = $postc->comment_type;
  345. }
  346. return $myrow;
  347. }
  348. function pingback($content, $post_ID) {
  349. global $wp_version, $wpdb;
  350. include_once (ABSPATH . WPINC . '/class-IXR.php');
  351. // original code by Mort (http://mort.mine.nu:8080)
  352. $log = debug_fopen(ABSPATH . '/pingback.log', 'a');
  353. $post_links = array();
  354. debug_fwrite($log, 'BEGIN '.date('YmdHis', time())."\n");
  355. $pung = get_pung($post_ID);
  356. // Variables
  357. $ltrs = '\w';
  358. $gunk = '/#~:.?+=&%@!\-';
  359. $punc = '.:?\-';
  360. $any = $ltrs . $gunk . $punc;
  361. // Step 1
  362. // Parsing the post, external links (if any) are stored in the $post_links array
  363. // This regexp comes straight from phpfreaks.com
  364. // http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
  365. preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
  366. // Debug
  367. debug_fwrite($log, 'Post contents:');
  368. debug_fwrite($log, $content."\n");
  369. // Step 2.
  370. // Walking thru the links array
  371. // first we get rid of links pointing to sites, not to specific files
  372. // Example:
  373. // http://dummy-weblog.org
  374. // http://dummy-weblog.org/
  375. // http://dummy-weblog.org/post.php
  376. // We don't wanna ping first and second types, even if they have a valid <link/>
  377. foreach($post_links_temp[0] as $link_test) :
  378. if ( !in_array($link_test, $pung) ) : // If we haven't pung it already
  379. $test = parse_url($link_test);
  380. if (isset($test['query']))
  381. $post_links[] = $link_test;
  382. elseif(($test['path'] != '/') && ($test['path'] != ''))
  383. $post_links[] = $link_test;
  384. endif;
  385. endforeach;
  386. foreach ($post_links as $pagelinkedto){
  387. debug_fwrite($log, "Processing -- $pagelinkedto\n");
  388. $pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048);
  389. if ($pingback_server_url) {
  390. set_time_limit( 60 );
  391. // Now, the RPC call
  392. debug_fwrite($log, "Page Linked To: $pagelinkedto \n");
  393. debug_fwrite($log, 'Page Linked From: ');
  394. $pagelinkedfrom = get_permalink($post_ID);
  395. debug_fwrite($log, $pagelinkedfrom."\n");
  396. // using a timeout of 3 seconds should be enough to cover slow servers
  397. $client = new IXR_Client($pingback_server_url);
  398. $client->timeout = 3;
  399. $client->useragent .= ' -- WordPress/' . $wp_version;
  400. // when set to true, this outputs debug messages by itself
  401. $client->debug = false;
  402. $client->query('pingback.ping', array($pagelinkedfrom, $pagelinkedto));
  403. if ( !$client->query('pingback.ping', array($pagelinkedfrom, $pagelinkedto) ) )
  404. debug_fwrite($log, "Error.\n Fault code: ".$client->getErrorCode()." : ".$client->getErrorMessage()."\n");
  405. else
  406. add_ping( $post_ID, $pagelinkedto );
  407. }
  408. }
  409. debug_fwrite($log, "\nEND: ".time()."\n****************************\n");
  410. debug_fclose($log);
  411. }
  412. function discover_pingback_server_uri($url, $timeout_bytes = 2048) {
  413. $byte_count = 0;
  414. $contents = '';
  415. $headers = '';
  416. $pingback_str_dquote = 'rel="pingback"';
  417. $pingback_str_squote = 'rel=\'pingback\'';
  418. $x_pingback_str = 'x-pingback: ';
  419. $pingback_href_original_pos = 27;
  420. extract(parse_url($url));
  421. if (!isset($host)) {
  422. // Not an URL. This should never happen.
  423. return false;
  424. }
  425. $path = (!isset($path)) ? '/' : $path;
  426. $path .= (isset($query)) ? '?'.$query : '';
  427. $port = (isset($port)) ? $port : 80;
  428. // Try to connect to the server at $host
  429. $fp = @fsockopen($host, $port, $errno, $errstr, 2);
  430. if (!$fp) {
  431. // Couldn't open a connection to $host;
  432. return false;
  433. }
  434. // Send the GET request
  435. $request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress/$wp_version PHP/" . phpversion() . "\r\n\r\n";
  436. ob_end_flush();
  437. fputs($fp, $request);
  438. // Let's check for an X-Pingback header first
  439. while (!feof($fp)) {
  440. $line = fgets($fp, 512);
  441. if (trim($line) == '') {
  442. break;
  443. }
  444. $headers .= trim($line)."\n";
  445. $x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str);
  446. if ($x_pingback_header_offset) {
  447. // We got it!
  448. preg_match('#x-pingback: (.+)#is', $headers, $matches);
  449. $pingback_server_url = trim($matches[1]);
  450. return $pingback_server_url;
  451. }
  452. if(strpos(strtolower($headers), 'content-type: ')) {
  453. preg_match('#content-type: (.+)#is', $headers, $matches);
  454. $content_type = trim($matches[1]);
  455. }
  456. }
  457. if (preg_match('#(image|audio|video|model)/#is', $content_type)) {
  458. // Not an (x)html, sgml, or xml page, no use going further
  459. return false;
  460. }
  461. while (!feof($fp)) {
  462. $line = fgets($fp, 1024);
  463. $contents .= trim($line);
  464. $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
  465. $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
  466. if ($pingback_link_offset_dquote || $pingback_link_offset_squote) {
  467. $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
  468. $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
  469. $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
  470. $pingback_href_start = $pingback_href_pos+6;
  471. $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
  472. $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
  473. $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
  474. // We may find rel="pingback" but an incomplete pingback URI
  475. if ($pingback_server_url_len > 0) {
  476. // We got it!
  477. return $pingback_server_url;
  478. }
  479. }
  480. $byte_count += strlen($line);
  481. if ($byte_count > $timeout_bytes) {
  482. // It's no use going further, there probably isn't any pingback
  483. // server to find in this file. (Prevents loading large files.)
  484. return false;
  485. }
  486. }
  487. // We didn't find anything.
  488. return false;
  489. }
  490. function wp_set_comment_status($comment_id, $comment_status) {
  491. global $wpdb;
  492. switch($comment_status) {
  493. case 'hold':
  494. $query = "UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1";
  495. break;
  496. case 'approve':
  497. $query = "UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1";
  498. break;
  499. case 'spam':
  500. $query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1";
  501. break;
  502. case 'delete':
  503. $query = "DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1";
  504. break;
  505. default:
  506. return false;
  507. }
  508. if ($wpdb->query($query)) {
  509. do_action('wp_set_comment_status', $comment_id, $comment_status);
  510. return true;
  511. } else {
  512. return false;
  513. }
  514. }
  515. function wp_get_comment_status($comment_id) {
  516. global $wpdb;
  517. $result = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
  518. if ($result == NULL) {
  519. return 'deleted';
  520. } else if ($result == '1') {
  521. return 'approved';
  522. } else if ($result == '0') {
  523. return 'unapproved';
  524. } else if ($result == 'spam') {
  525. return 'spam';
  526. } else {
  527. return false;
  528. }
  529. }
  530. if ( ! function_exists('wp_notify_postauthor') ) {
  531. function wp_notify_postauthor($comment_id, $comment_type='') {
  532. global $wpdb;
  533. $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
  534. $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1");
  535. $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID='$post->post_author' LIMIT 1");
  536. if ('' == $user->user_email) return false; // If there's no email to send the comment to
  537. $comment_author_domain = gethostbyaddr($comment->comment_author_IP);
  538. $blogname = get_settings('blogname');
  539. if ( empty( $comment_type ) ) $comment_type = 'comment';
  540. if ('comment' == $comment_type) {
  541. $notify_message = "New comment on your post #$comment->comment_post_ID \"".$post->post_title."\"\r\n\r\n";
  542. $notify_message .= "Author : $comment->comment_author (IP: $comment->comment_author_IP , $comment_author_domain)\r\n";
  543. $notify_message .= "E-mail : $comment->comment_author_email\r\n";
  544. $notify_message .= "URI : $comment->comment_author_url\r\n";
  545. $notify_message .= "Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=$comment->comment_author_IP\r\n";
  546. $notify_message .= "Comment:\r\n $comment->comment_content \r\n\r\n";
  547. $notify_message .= "You can see all comments on this post here: \r\n";
  548. $subject = '[' . $blogname . '] Comment: "' .$post->post_title.'"';
  549. } elseif ('trackback' == $comment_type) {
  550. $notify_message = "New trackback on your post #$comment_post_ID \"".$post->post_title."\"\r\n\r\n";
  551. $notify_message .= "Website: $comment->comment_author (IP: $comment->comment_author_IP , $comment_author_domain)\r\n";
  552. $notify_message .= "URI : $comment->comment_author_url\r\n";
  553. $notify_message .= "Excerpt: \n $comment->comment_content \r\n\r\n";
  554. $notify_message .= "You can see all trackbacks on this post here: \r\n";
  555. $subject = '[' . $blogname . '] Trackback: "' .$post->post_title.'"';
  556. } elseif ('pingback' == $comment_type) {
  557. $notify_message = "New pingback on your post #$comment_post_ID \"".$post->post_title."\"\r\n\r\n";
  558. $notify_message .= "Website: $comment->comment_author\r\n";
  559. $notify_message .= "URI : $comment->comment_author_url\r\n";
  560. $notify_message .= "Excerpt: \n[...] $comment->comment_content [...]\r\n\r\n";
  561. $notify_message .= "You can see all pingbacks on this post here: \r\n";
  562. $subject = '[' . $blogname . '] Pingback: "' .$post->post_title.'"';
  563. }
  564. $notify_message .= get_permalink($comment->comment_post_ID) . '#comments';
  565. $notify_message .= "\r\n\r\nTo delete this comment:\r\n" . get_settings('siteurl') . "/wp-admin/post.php?action=confirmdeletecomment&p=".$comment->comment_post_ID."&comment=$comment_id";
  566. if ('' == $comment->comment_author_email || '' == $comment->comment_author) {
  567. $from = "From: \"$blogname\" <wordpress@" . $_SERVER['SERVER_NAME'] . '>';
  568. } else {
  569. $from = 'From: "' . $comment->comment_author . "\" <$comment->comment_author_email>";
  570. }
  571. $message_headers = "MIME-Version: 1.0\n"
  572. . "$from\n"
  573. . "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
  574. @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
  575. return true;
  576. }
  577. }
  578. /* wp_notify_moderator
  579. notifies the moderator of the blog (usually the admin)
  580. about a new comment that waits for approval
  581. always returns true
  582. */
  583. if ( !function_exists('wp_notify_moderator') ) {
  584. function wp_notify_moderator($comment_id) {
  585. global $wpdb;
  586. if( get_settings( "moderation_notify" ) == 0 )
  587. return true;
  588. $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
  589. $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1");
  590. $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID='$post->post_author' LIMIT 1");
  591. $comment_author_domain = gethostbyaddr($comment->comment_author_IP);
  592. $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
  593. $notify_message = "A new comment on the post #$post->ID \"$post->post_title\" is waiting for your approval\r\n";
  594. $notify_message .= get_permalink($comment->comment_post_ID);
  595. $notify_message .= "\n\nAuthor : $comment->comment_author (IP: $comment->comment_author_IP , $comment_author_domain)\r\n";
  596. $notify_message .= "E-mail : $comment->comment_author_email\r\n";
  597. $notify_message .= "URL : $comment->comment_author_url\r\n";
  598. $notify_message .= "Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=$comment->comment_author_IP\r\n";
  599. $notify_message .= "Comment:\r\n".$comment->comment_content."\r\n\r\n";
  600. $notify_message .= "To approve this comment, visit: " . get_settings('siteurl') . "/wp-admin/post.php?action=mailapprovecomment&p=".$comment->comment_post_ID."&comment=$comment_id\r\n";
  601. $notify_message .= "To delete this comment, visit: " . get_settings('siteurl') . "/wp-admin/post.php?action=confirmdeletecomment&p=".$comment->comment_post_ID."&comment=$comment_id\r\n";
  602. $notify_message .= "Currently $comments_waiting comments are waiting for approval. Please visit the moderation panel:\r\n";
  603. $notify_message .= get_settings('siteurl') . "/wp-admin/moderation.php\r\n";
  604. $subject = '[' . get_settings('blogname') . '] Please moderate: "' .$post->post_title.'"';
  605. $admin_email = get_settings("admin_email");
  606. $from = "From: $admin_email";
  607. $message_headers = "MIME-Version: 1.0\n"
  608. . "$from\n"
  609. . "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
  610. @wp_mail($admin_email, $subject, $notify_message, $message_headers);
  611. return true;
  612. }
  613. }
  614. function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
  615. global $wpdb;
  616. if (1 == get_settings('comment_moderation')) return false; // If moderation is set to manual
  617. if ( (count(explode('http:', $comment)) - 1) >= get_settings('comment_max_links') )
  618. return false; // Check # of external links
  619. $mod_keys = trim( get_settings('moderation_keys') );
  620. if ('' == $mod_keys )
  621. return true; // If moderation keys are empty
  622. $words = explode("\n", $mod_keys );
  623. foreach ($words as $word) {
  624. $word = trim($word);
  625. // Skip empty lines
  626. if (empty($word)) { continue; }
  627. // Do some escaping magic so that '#' chars in the
  628. // spam words don't break things:
  629. $word = preg_quote($word, '#');
  630. $pattern = "#$word#i";
  631. if ( preg_match($pattern, $author) ) return false;
  632. if ( preg_match($pattern, $email) ) return false;
  633. if ( preg_match($pattern, $url) ) return false;
  634. if ( preg_match($pattern, $comment) ) return false;
  635. if ( preg_match($pattern, $user_ip) ) return false;
  636. if ( preg_match($pattern, $user_agent) ) return false;
  637. }
  638. // Comment whitelisting:
  639. if ( 1 == get_settings('comment_whitelist')) {
  640. if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll
  641. $uri = parse_url($url);
  642. $domain = $uri['host'];
  643. if ( $wpdb->get_var("SELECT link_id FROM $wpdb->links WHERE link_url LIKE ('%$domain%') LIMIT 1") )
  644. return true;
  645. } elseif( $author != '' && $email != '' ) {
  646. $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' ");
  647. if ( 1 == $ok_to_comment && false === strpos( $email, get_settings('moderation_keys')) )
  648. return true;
  649. else
  650. return false;
  651. } else {
  652. return false;
  653. }
  654. }
  655. return true;
  656. }
  657. ?>