PageRenderTime 6ms CodeModel.GetById 380ms app.highlight 200ms RepoModel.GetById 122ms app.codeStats 1ms

/tags/2.2.1/wp-includes/comment.php

#
PHP | 817 lines | 600 code | 152 blank | 65 comment | 147 complexity | 9e9504185d9556a333d286978290ce9d MD5 | raw file
  1<?php
  2
  3function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
  4	global $wpdb;
  5
  6	if ( 1 == get_option('comment_moderation') )
  7		return false; // If moderation is set to manual
  8
  9	if ( preg_match_all("|(href\t*?=\t*?['\"]?)?(https?:)?//|i", $comment, $out) >= get_option('comment_max_links') )
 10		return false; // Check # of external links
 11
 12	$mod_keys = trim(get_option('moderation_keys'));
 13	if ( !empty($mod_keys) ) {
 14		$words = explode("\n", $mod_keys );
 15
 16		foreach ($words as $word) {
 17			$word = trim($word);
 18
 19			// Skip empty lines
 20			if ( empty($word) )
 21				continue;
 22
 23			// Do some escaping magic so that '#' chars in the
 24			// spam words don't break things:
 25			$word = preg_quote($word, '#');
 26
 27			$pattern = "#$word#i";
 28			if ( preg_match($pattern, $author) ) return false;
 29			if ( preg_match($pattern, $email) ) return false;
 30			if ( preg_match($pattern, $url) ) return false;
 31			if ( preg_match($pattern, $comment) ) return false;
 32			if ( preg_match($pattern, $user_ip) ) return false;
 33			if ( preg_match($pattern, $user_agent) ) return false;
 34		}
 35	}
 36
 37	// Comment whitelisting:
 38	if ( 1 == get_option('comment_whitelist')) {
 39		if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll
 40			$uri = parse_url($url);
 41			$domain = $uri['host'];
 42			$uri = parse_url( get_option('home') );
 43			$home_domain = $uri['host'];
 44			if ( $wpdb->get_var("SELECT link_id FROM $wpdb->links WHERE link_url LIKE ('%$domain%') LIMIT 1") || $domain == $home_domain )
 45				return true;
 46			else
 47				return false;
 48		} elseif ( $author != '' && $email != '' ) {
 49			$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");
 50			if ( ( 1 == $ok_to_comment ) &&
 51				( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
 52					return true;
 53			else
 54				return false;
 55		} else {
 56			return false;
 57		}
 58	}
 59	return true;
 60}
 61
 62
 63function get_approved_comments($post_id) {
 64	global $wpdb;
 65
 66	$post_id = (int) $post_id;
 67	return $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1' ORDER BY comment_date");
 68}
 69
 70
 71// Retrieves comment data given a comment ID or comment object.
 72// Handles comment caching.
 73function &get_comment(&$comment, $output = OBJECT) {
 74	global $comment_cache, $wpdb;
 75
 76	if ( empty($comment) )
 77		return null;
 78
 79	if ( is_object($comment) ) {
 80		if ( !isset($comment_cache[$comment->comment_ID]) )
 81			$comment_cache[$comment->comment_ID] = &$comment;
 82		$_comment = & $comment_cache[$comment->comment_ID];
 83	} else {
 84		$comment = (int) $comment;
 85		if ( !isset($comment_cache[$comment]) ) {
 86			$_comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment' LIMIT 1");
 87			$comment_cache[$comment->comment_ID] = & $_comment;
 88		} else {
 89			$_comment = & $comment_cache[$comment];
 90		}
 91	}
 92
 93	if ( $output == OBJECT ) {
 94		return $_comment;
 95	} elseif ( $output == ARRAY_A ) {
 96		return get_object_vars($_comment);
 97	} elseif ( $output == ARRAY_N ) {
 98		return array_values(get_object_vars($_comment));
 99	} else {
100		return $_comment;
101	}
102}
103
104
105// Deprecate in favor of get_comment()?
106function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) { // less flexible, but saves DB queries
107	global $postc, $id, $commentdata, $wpdb;
108	if ( $no_cache ) {
109		$query = "SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_ID'";
110		if ( false == $include_unapproved )
111			$query .= " AND comment_approved = '1'";
112		$myrow = $wpdb->get_row($query, ARRAY_A);
113	} else {
114		$myrow['comment_ID']           = $postc->comment_ID;
115		$myrow['comment_post_ID']      = $postc->comment_post_ID;
116		$myrow['comment_author']       = $postc->comment_author;
117		$myrow['comment_author_email'] = $postc->comment_author_email;
118		$myrow['comment_author_url']   = $postc->comment_author_url;
119		$myrow['comment_author_IP']    = $postc->comment_author_IP;
120		$myrow['comment_date']         = $postc->comment_date;
121		$myrow['comment_content']      = $postc->comment_content;
122		$myrow['comment_karma']        = $postc->comment_karma;
123		$myrow['comment_approved']     = $postc->comment_approved;
124		$myrow['comment_type']         = $postc->comment_type;
125	}
126	return $myrow;
127}
128
129
130function get_lastcommentmodified($timezone = 'server') {
131	global $cache_lastcommentmodified, $pagenow, $wpdb;
132	$add_seconds_blog = get_option('gmt_offset') * 3600;
133	$add_seconds_server = date('Z');
134	$now = current_time('mysql', 1);
135	if ( !isset($cache_lastcommentmodified[$timezone]) ) {
136		switch ( strtolower($timezone)) {
137			case 'gmt':
138				$lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
139				break;
140			case 'blog':
141				$lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
142				break;
143			case 'server':
144				$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");
145				break;
146		}
147		$cache_lastcommentmodified[$timezone] = $lastcommentmodified;
148	} else {
149		$lastcommentmodified = $cache_lastcommentmodified[$timezone];
150	}
151	return $lastcommentmodified;
152}
153
154
155function sanitize_comment_cookies() {
156	if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) {
157		$comment_author = apply_filters('pre_comment_author_name', $_COOKIE['comment_author_'.COOKIEHASH]);
158		$comment_author = stripslashes($comment_author);
159		$comment_author = attribute_escape($comment_author);
160		$_COOKIE['comment_author_'.COOKIEHASH] = $comment_author;
161	}
162
163	if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) {
164		$comment_author_email = apply_filters('pre_comment_author_email', $_COOKIE['comment_author_email_'.COOKIEHASH]);
165		$comment_author_email = stripslashes($comment_author_email);
166		$comment_author_email = attribute_escape($comment_author_email);
167		$_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
168	}
169
170	if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) {
171		$comment_author_url = apply_filters('pre_comment_author_url', $_COOKIE['comment_author_url_'.COOKIEHASH]);
172		$comment_author_url = stripslashes($comment_author_url);
173		$comment_author_url = clean_url($comment_author_url);
174		$_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
175	}
176}
177
178
179function wp_allow_comment($commentdata) {
180	global $wpdb;
181	extract($commentdata, EXTR_SKIP);
182
183	// Simple duplicate check
184	$dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
185	if ( $comment_author_email )
186		$dupe .= "OR comment_author_email = '$comment_author_email' ";
187	$dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
188	if ( $wpdb->get_var($dupe) )
189		wp_die( __('Duplicate comment detected; it looks as though you\'ve already said that!') );
190
191	// Simple flood-protection
192	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") ) {
193		$time_lastcomment = mysql2date('U', $lasttime);
194		$time_newcomment  = mysql2date('U', $comment_date_gmt);
195		$flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
196		if ( $flood_die ) {
197			do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment);
198			wp_die( __('You are posting comments too quickly.  Slow down.') );
199		}
200	}
201
202	if ( $user_id ) {
203		$userdata = get_userdata($user_id);
204		$user = new WP_User($user_id);
205		$post_author = $wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = '$comment_post_ID' LIMIT 1");
206	}
207
208	if ( $userdata && ( $user_id == $post_author || $user->has_cap('level_9') ) ) {
209		// The author and the admins get respect.
210		$approved = 1;
211	 } else {
212		// Everyone else's comments will be checked.
213		if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) )
214			$approved = 1;
215		else
216			$approved = 0;
217		if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) )
218			$approved = 'spam';
219	}
220
221	$approved = apply_filters('pre_comment_approved', $approved);
222	return $approved;
223}
224
225
226function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
227	global $wpdb;
228
229	do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
230
231	if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
232		foreach ( (array) $chars[1] as $char ) {
233			// If it's an encoded char in the normal ASCII set, reject
234			if ( 38 == $char )
235				continue; // Unless it's &
236			if ( $char < 128 )
237				return true;
238		}
239	}
240
241	$mod_keys = trim( get_option('blacklist_keys') );
242	if ( '' == $mod_keys )
243		return false; // If moderation keys are empty
244	$words = explode("\n", $mod_keys );
245
246	foreach ( (array) $words as $word ) {
247		$word = trim($word);
248
249		// Skip empty lines
250		if ( empty($word) ) { continue; }
251
252		// Do some escaping magic so that '#' chars in the
253		// spam words don't break things:
254		$word = preg_quote($word, '#');
255
256		$pattern = "#$word#i";
257		if (
258			   preg_match($pattern, $author)
259			|| preg_match($pattern, $email)
260			|| preg_match($pattern, $url)
261			|| preg_match($pattern, $comment)
262			|| preg_match($pattern, $user_ip)
263			|| preg_match($pattern, $user_agent)
264		 )
265			return true;
266	}
267	return false;
268}
269
270
271function wp_delete_comment($comment_id) {
272	global $wpdb;
273	do_action('delete_comment', $comment_id);
274
275	$comment = get_comment($comment_id);
276
277	if ( ! $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1") )
278		return false;
279
280	$post_id = $comment->comment_post_ID;
281	if ( $post_id && $comment->comment_approved == 1 )
282		wp_update_comment_count($post_id);
283
284	do_action('wp_set_comment_status', $comment_id, 'delete');
285	return true;
286}
287
288
289function wp_get_comment_status($comment_id) {
290	global $wpdb;
291
292	$result = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
293
294	if ( $result == NULL )
295		return 'deleted';
296	elseif ( $result == '1' )
297		return 'approved';
298	elseif ( $result == '0' )
299		return 'unapproved';
300	elseif ( $result == 'spam' )
301		return 'spam';
302	else
303		return false;
304}
305
306
307function wp_get_current_commenter() {
308	// Cookies should already be sanitized.
309
310	$comment_author = '';
311	if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
312		$comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
313
314	$comment_author_email = '';
315	if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
316		$comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
317
318	$comment_author_url = '';
319	if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
320		$comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
321
322	return compact('comment_author', 'comment_author_email', 'comment_author_url');
323}
324
325
326function wp_insert_comment($commentdata) {
327	global $wpdb;
328	extract($commentdata, EXTR_SKIP);
329
330	if ( ! isset($comment_author_IP) )
331		$comment_author_IP = preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] );
332	if ( ! isset($comment_date) )
333		$comment_date = current_time('mysql');
334	if ( ! isset($comment_date_gmt) )
335		$comment_date_gmt = get_gmt_from_date($comment_date);
336	if ( ! isset($comment_parent) )
337		$comment_parent = 0;
338	if ( ! isset($comment_approved) )
339		$comment_approved = 1;
340	if ( ! isset($user_id) )
341		$user_id = 0;
342
343	$result = $wpdb->query("INSERT INTO $wpdb->comments
344	(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)
345	VALUES
346	('$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')
347	");
348
349	$id = (int) $wpdb->insert_id;
350
351	if ( $comment_approved == 1)
352		wp_update_comment_count($comment_post_ID);
353
354	return $id;
355}
356
357
358function wp_filter_comment($commentdata) {
359	$commentdata['user_id']              = apply_filters('pre_user_id', $commentdata['user_ID']);
360	$commentdata['comment_agent']        = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']);
361	$commentdata['comment_author']       = apply_filters('pre_comment_author_name', $commentdata['comment_author']);
362	$commentdata['comment_content']      = apply_filters('pre_comment_content', $commentdata['comment_content']);
363	$commentdata['comment_author_IP']    = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']);
364	$commentdata['comment_author_url']   = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']);
365	$commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']);
366	$commentdata['filtered'] = true;
367	return $commentdata;
368}
369
370
371function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
372	if ( $block ) // a plugin has already blocked... we'll let that decision stand
373		return $block;
374	if ( ($time_newcomment - $time_lastcomment) < 15 )
375		return true;
376	return false;
377}
378
379
380function wp_new_comment( $commentdata ) {
381	$commentdata = apply_filters('preprocess_comment', $commentdata);
382
383	$commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
384	$commentdata['user_ID']         = (int) $commentdata['user_ID'];
385
386	$commentdata['comment_author_IP'] = preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] );
387	$commentdata['comment_agent']     = $_SERVER['HTTP_USER_AGENT'];
388
389	$commentdata['comment_date']     = current_time('mysql');
390	$commentdata['comment_date_gmt'] = current_time('mysql', 1);
391
392	$commentdata = wp_filter_comment($commentdata);
393
394	$commentdata['comment_approved'] = wp_allow_comment($commentdata);
395
396	$comment_ID = wp_insert_comment($commentdata);
397
398	do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
399
400	if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
401		if ( '0' == $commentdata['comment_approved'] )
402			wp_notify_moderator($comment_ID);
403
404		$post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment
405
406		if ( get_option('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] )
407			wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
408	}
409
410	return $comment_ID;
411}
412
413
414function wp_set_comment_status($comment_id, $comment_status) {
415	global $wpdb;
416
417	switch ( $comment_status ) {
418		case 'hold':
419			$query = "UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1";
420			break;
421		case 'approve':
422			$query = "UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1";
423			break;
424		case 'spam':
425			$query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1";
426			break;
427		case 'delete':
428			return wp_delete_comment($comment_id);
429			break;
430		default:
431			return false;
432	}
433
434	if ( !$wpdb->query($query) )
435		return false;
436
437	do_action('wp_set_comment_status', $comment_id, $comment_status);
438	$comment = get_comment($comment_id);
439	wp_update_comment_count($comment->comment_post_ID);
440	return true;
441}
442
443
444function wp_update_comment($commentarr) {
445	global $wpdb;
446
447	// First, get all of the original fields
448	$comment = get_comment($commentarr['comment_ID'], ARRAY_A);
449
450	// Escape data pulled from DB.
451	foreach ( (array) $comment as $key => $value )
452		$comment[$key] = $wpdb->escape($value);
453
454	// Merge old and new fields with new fields overwriting old ones.
455	$commentarr = array_merge($comment, $commentarr);
456
457	$commentarr = wp_filter_comment( $commentarr );
458
459	// Now extract the merged array.
460	extract($commentarr, EXTR_SKIP);
461
462	$comment_content = apply_filters('comment_save_pre', $comment_content);
463
464	$comment_date_gmt = get_gmt_from_date($comment_date);
465
466	$result = $wpdb->query(
467		"UPDATE $wpdb->comments SET
468			comment_content      = '$comment_content',
469			comment_author       = '$comment_author',
470			comment_author_email = '$comment_author_email',
471			comment_approved     = '$comment_approved',
472			comment_author_url   = '$comment_author_url',
473			comment_date         = '$comment_date',
474			comment_date_gmt     = '$comment_date_gmt'
475		WHERE comment_ID = $comment_ID" );
476
477	$rval = $wpdb->rows_affected;
478	wp_update_comment_count($comment_post_ID);
479	do_action('edit_comment', $comment_ID);
480	return $rval;
481}
482
483
484function wp_update_comment_count($post_id) {
485	global $wpdb, $comment_count_cache;
486	$post_id = (int) $post_id;
487	if ( !$post_id )
488		return false;
489	$count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'");
490	$wpdb->query("UPDATE $wpdb->posts SET comment_count = $count WHERE ID = '$post_id'");
491	$comment_count_cache[$post_id] = $count;
492
493	$post = get_post($post_id);
494	if ( 'page' == $post->post_type )
495		clean_page_cache( $post_id );
496	else
497		clean_post_cache( $post_id );
498
499	do_action('edit_post', $post_id);
500
501	return true;
502}
503
504
505//
506// Ping and trackback functions.
507//
508
509function discover_pingback_server_uri($url, $timeout_bytes = 2048) {
510	global $wp_version;
511
512	$byte_count = 0;
513	$contents = '';
514	$headers = '';
515	$pingback_str_dquote = 'rel="pingback"';
516	$pingback_str_squote = 'rel=\'pingback\'';
517	$x_pingback_str = 'x-pingback: ';
518	$pingback_href_original_pos = 27;
519
520	extract(parse_url($url), EXTR_SKIP);
521
522	if ( !isset($host) ) // Not an URL. This should never happen.
523		return false;
524
525	$path  = ( !isset($path) ) ? '/'          : $path;
526	$path .= ( isset($query) ) ? '?' . $query : '';
527	$port  = ( isset($port)  ) ? $port        : 80;
528
529	// Try to connect to the server at $host
530	$fp = @fsockopen($host, $port, $errno, $errstr, 2);
531	if ( !$fp ) // Couldn't open a connection to $host
532		return false;
533
534	// Send the GET request
535	$request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress/$wp_version \r\n\r\n";
536	// ob_end_flush();
537	fputs($fp, $request);
538
539	// Let's check for an X-Pingback header first
540	while ( !feof($fp) ) {
541		$line = fgets($fp, 512);
542		if ( trim($line) == '' )
543			break;
544		$headers .= trim($line)."\n";
545		$x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str);
546		if ( $x_pingback_header_offset ) {
547			// We got it!
548			preg_match('#x-pingback: (.+)#is', $headers, $matches);
549			$pingback_server_url = trim($matches[1]);
550			return $pingback_server_url;
551		}
552		if ( strpos(strtolower($headers), 'content-type: ') ) {
553			preg_match('#content-type: (.+)#is', $headers, $matches);
554			$content_type = trim($matches[1]);
555		}
556	}
557
558	if ( preg_match('#(image|audio|video|model)/#is', $content_type) ) // Not an (x)html, sgml, or xml page, no use going further
559		return false;
560
561	while ( !feof($fp) ) {
562		$line = fgets($fp, 1024);
563		$contents .= trim($line);
564		$pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
565		$pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
566		if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
567			$quote = ($pingback_link_offset_dquote) ? '"' : '\'';
568			$pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
569			$pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
570			$pingback_href_start = $pingback_href_pos+6;
571			$pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
572			$pingback_server_url_len = $pingback_href_end - $pingback_href_start;
573			$pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
574			// We may find rel="pingback" but an incomplete pingback URL
575			if ( $pingback_server_url_len > 0 ) // We got it!
576				return $pingback_server_url;
577		}
578		$byte_count += strlen($line);
579		if ( $byte_count > $timeout_bytes ) {
580			// It's no use going further, there probably isn't any pingback
581			// server to find in this file. (Prevents loading large files.)
582			return false;
583		}
584	}
585
586	// We didn't find anything.
587	return false;
588}
589
590
591function do_all_pings() {
592	global $wpdb;
593
594	// Do pingbacks
595	while ($ping = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) {
596		$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';");
597		pingback($ping->post_content, $ping->ID);
598	}
599
600	// Do Enclosures
601	while ($enclosure = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) {
602		$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$enclosure->ID} AND meta_key = '_encloseme';");
603		do_enclose($enclosure->post_content, $enclosure->ID);
604	}
605
606	// Do Trackbacks
607	$trackbacks = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE CHAR_LENGTH(TRIM(to_ping)) > 7 AND post_status = 'publish'");
608	if ( is_array($trackbacks) ) {
609		foreach ( $trackbacks as $trackback )
610			do_trackbacks($trackback->ID);
611	}
612
613	//Do Update Services/Generic Pings
614	generic_ping();
615}
616
617function do_trackbacks($post_id) {
618	global $wpdb;
619
620	$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_id");
621	$to_ping = get_to_ping($post_id);
622	$pinged  = get_pung($post_id);
623	if ( empty($to_ping) ) {
624		$wpdb->query("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = '$post_id'");
625		return;
626	}
627
628	if ( empty($post->post_excerpt) )
629		$excerpt = apply_filters('the_content', $post->post_content);
630	else
631		$excerpt = apply_filters('the_excerpt', $post->post_excerpt);
632	$excerpt = str_replace(']]>', ']]&gt;', $excerpt);
633	$excerpt = strip_tags($excerpt);
634	if ( function_exists('mb_strcut') ) // For international trackbacks
635    	$excerpt = mb_strcut($excerpt, 0, 252, get_option('blog_charset')) . '...';
636	else
637		$excerpt = substr($excerpt, 0, 252) . '...';
638
639	$post_title = apply_filters('the_title', $post->post_title);
640	$post_title = strip_tags($post_title);
641
642	if ( $to_ping ) {
643		foreach ( (array) $to_ping as $tb_ping ) {
644			$tb_ping = trim($tb_ping);
645			if ( !in_array($tb_ping, $pinged) ) {
646				trackback($tb_ping, $post_title, $excerpt, $post_id);
647				$pinged[] = $tb_ping;
648			} else {
649				$wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = '$post_id'");
650			}
651		}
652	}
653}
654
655
656function generic_ping($post_id = 0) {
657	$services = get_option('ping_sites');
658	$services = preg_replace("|(\s)+|", '$1', $services); // Kill dupe lines
659	$services = trim($services);
660	if ( '' != $services ) {
661		$services = explode("\n", $services);
662		foreach ( (array) $services as $service )
663			weblog_ping($service);
664	}
665
666	return $post_id;
667}
668
669
670function pingback($content, $post_ID) {
671	global $wp_version, $wpdb;
672	include_once(ABSPATH . WPINC . '/class-IXR.php');
673
674	// original code by Mort (http://mort.mine.nu:8080)
675	$log = debug_fopen(ABSPATH . '/pingback.log', 'a');
676	$post_links = array();
677	debug_fwrite($log, 'BEGIN ' . date('YmdHis', time()) . "\n");
678
679	$pung = get_pung($post_ID);
680
681	// Variables
682	$ltrs = '\w';
683	$gunk = '/#~:.?+=&%@!\-';
684	$punc = '.:?\-';
685	$any = $ltrs . $gunk . $punc;
686
687	// Step 1
688	// Parsing the post, external links (if any) are stored in the $post_links array
689	// This regexp comes straight from phpfreaks.com
690	// http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
691	preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
692
693	// Debug
694	debug_fwrite($log, 'Post contents:');
695	debug_fwrite($log, $content."\n");
696
697	// Step 2.
698	// Walking thru the links array
699	// first we get rid of links pointing to sites, not to specific files
700	// Example:
701	// http://dummy-weblog.org
702	// http://dummy-weblog.org/
703	// http://dummy-weblog.org/post.php
704	// We don't wanna ping first and second types, even if they have a valid <link/>
705
706	foreach ( $post_links_temp[0] as $link_test ) :
707		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
708				&& !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
709			$test = parse_url($link_test);
710			if ( isset($test['query']) )
711				$post_links[] = $link_test;
712			elseif ( ($test['path'] != '/') && ($test['path'] != '') )
713				$post_links[] = $link_test;
714		endif;
715	endforeach;
716
717	do_action_ref_array('pre_ping', array(&$post_links, &$pung));
718
719	foreach ( (array) $post_links as $pagelinkedto ) {
720		debug_fwrite($log, "Processing -- $pagelinkedto\n");
721		$pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048);
722
723		if ( $pingback_server_url ) {
724			@ set_time_limit( 60 );
725			 // Now, the RPC call
726			debug_fwrite($log, "Page Linked To: $pagelinkedto \n");
727			debug_fwrite($log, 'Page Linked From: ');
728			$pagelinkedfrom = get_permalink($post_ID);
729			debug_fwrite($log, $pagelinkedfrom."\n");
730
731			// using a timeout of 3 seconds should be enough to cover slow servers
732			$client = new IXR_Client($pingback_server_url);
733			$client->timeout = 3;
734			$client->useragent .= ' -- WordPress/' . $wp_version;
735
736			// when set to true, this outputs debug messages by itself
737			$client->debug = false;
738
739			if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto ) )
740				add_ping( $post_ID, $pagelinkedto );
741			else
742				debug_fwrite($log, "Error.\n Fault code: ".$client->getErrorCode()." : ".$client->getErrorMessage()."\n");
743		}
744	}
745
746	debug_fwrite($log, "\nEND: ".time()."\n****************************\n");
747	debug_fclose($log);
748}
749
750
751function privacy_ping_filter($sites) {
752	if ( '0' != get_option('blog_public') )
753		return $sites;
754	else
755		return '';
756}
757
758// Send a Trackback
759function trackback($trackback_url, $title, $excerpt, $ID) {
760	global $wpdb, $wp_version;
761
762	if ( empty($trackback_url) )
763		return;
764
765	$title = urlencode($title);
766	$excerpt = urlencode($excerpt);
767	$blog_name = urlencode(get_option('blogname'));
768	$tb_url = $trackback_url;
769	$url = urlencode(get_permalink($ID));
770	$query_string = "title=$title&url=$url&blog_name=$blog_name&excerpt=$excerpt";
771	$trackback_url = parse_url($trackback_url);
772	$http_request = 'POST ' . $trackback_url['path'] . ($trackback_url['query'] ? '?'.$trackback_url['query'] : '') . " HTTP/1.0\r\n";
773	$http_request .= 'Host: '.$trackback_url['host']."\r\n";
774	$http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_option('blog_charset')."\r\n";
775	$http_request .= 'Content-Length: '.strlen($query_string)."\r\n";
776	$http_request .= "User-Agent: WordPress/" . $wp_version;
777	$http_request .= "\r\n\r\n";
778	$http_request .= $query_string;
779	if ( '' == $trackback_url['port'] )
780		$trackback_url['port'] = 80;
781	$fs = @fsockopen($trackback_url['host'], $trackback_url['port'], $errno, $errstr, 4);
782	@fputs($fs, $http_request);
783/*
784	$debug_file = 'trackback.log';
785	$fp = fopen($debug_file, 'a');
786	fwrite($fp, "\n*****\nRequest:\n\n$http_request\n\nResponse:\n\n");
787	while(!@feof($fs)) {
788		fwrite($fp, @fgets($fs, 4096));
789	}
790	fwrite($fp, "\n\n");
791	fclose($fp);
792*/
793	@fclose($fs);
794
795	$tb_url = addslashes( $tb_url );
796	$wpdb->query("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = '$ID'");
797	return $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = '$ID'");
798}
799
800
801function weblog_ping($server = '', $path = '') {
802	global $wp_version;
803	include_once(ABSPATH . WPINC . '/class-IXR.php');
804
805	// using a timeout of 3 seconds should be enough to cover slow servers
806	$client = new IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
807	$client->timeout = 3;
808	$client->useragent .= ' -- WordPress/'.$wp_version;
809
810	// when set to true, this outputs debug messages by itself
811	$client->debug = false;
812	$home = trailingslashit( get_option('home') );
813	if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
814		$client->query('weblogUpdates.ping', get_option('blogname'), $home);
815}
816
817?>