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

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

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