3 /* These functions can be replaced via plugins. They are loaded after
6 if ( !function_exists('set_current_user') ) :
7 function set_current_user($id, $name = '') {
8 return wp_set_current_user($id, $name);
12 if ( !function_exists('wp_set_current_user') ) :
13 function wp_set_current_user($id, $name = '') {
16 if ( isset($current_user) && ($id == $current_user->ID) )
19 $current_user = new WP_User($id, $name);
21 setup_userdata($current_user->ID);
23 do_action('set_current_user');
29 if ( !function_exists('wp_get_current_user') ) :
30 function wp_get_current_user() {
33 get_currentuserinfo();
39 if ( !function_exists('get_currentuserinfo') ) :
40 function get_currentuserinfo() {
43 if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
46 if ( ! empty($current_user) )
49 if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) ||
50 !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true) ) {
51 wp_set_current_user(0);
55 $user_login = $_COOKIE[USER_COOKIE];
56 wp_set_current_user(0, $user_login);
60 if ( !function_exists('get_userdata') ) :
61 function get_userdata( $user_id ) {
63 $user_id = (int) $user_id;
67 $user = wp_cache_get($user_id, 'users');
72 if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = '$user_id' LIMIT 1") )
75 $show = $wpdb->hide_errors();
76 $metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id'");
77 $wpdb->show_errors($show);
80 foreach ( $metavalues as $meta ) {
81 $value = maybe_unserialize($meta->meta_value);
82 $user->{$meta->meta_key} = $value;
84 // We need to set user_level from meta, not row
85 if ( $wpdb->prefix . 'user_level' == $meta->meta_key )
86 $user->user_level = $meta->meta_value;
90 // For backwards compat.
91 if ( isset($user->first_name) )
92 $user->user_firstname = $user->first_name;
93 if ( isset($user->last_name) )
94 $user->user_lastname = $user->last_name;
95 if ( isset($user->description) )
96 $user->user_description = $user->description;
98 wp_cache_add($user_id, $user, 'users');
99 wp_cache_add($user->user_login, $user_id, 'userlogins');
104 if ( !function_exists('update_user_cache') ) :
105 function update_user_cache() {
110 if ( !function_exists('get_userdatabylogin') ) :
111 function get_userdatabylogin($user_login) {
113 $user_login = sanitize_user( $user_login );
115 if ( empty( $user_login ) )
118 $user_id = wp_cache_get($user_login, 'userlogins');
119 $userdata = wp_cache_get($user_id, 'users');
124 $user_login = $wpdb->escape($user_login);
126 if ( !$user_ID = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = '$user_login'") )
129 $user = get_userdata($user_ID);
134 if ( !function_exists( 'wp_mail' ) ) :
135 function wp_mail( $to, $subject, $message, $headers = '' ) {
136 // Compact the input, apply the filters, and extract them back out
137 extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers' ) ) );
141 // (Re)create it, if it's gone missing
142 if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
143 require_once ABSPATH . WPINC . '/class-phpmailer.php';
144 require_once ABSPATH . WPINC . '/class-smtp.php';
145 $phpmailer = new PHPMailer();
149 if ( empty( $headers ) ) {
151 } elseif ( !is_array( $headers ) ) {
152 // Explode the headers out, so this function can take both
153 // string headers and an array of headers.
154 $tempheaders = (array) explode( "\n", $headers );
157 // If it's actually got contents
158 if ( !empty( $tempheaders ) ) {
159 // Iterate through the raw headers
160 foreach ( $tempheaders as $header ) {
161 if ( strpos($header, ':') === false )
164 list( $name, $content ) = explode( ':', trim( $header ), 2 );
167 $name = trim( $name );
168 $content = trim( $content );
170 // Mainly for legacy -- process a From: header if it's there
171 if ( 'from' == strtolower($name) ) {
172 if ( strpos($content, '<' ) !== false ) {
173 // So... making my life hard again?
174 $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
175 $from_name = str_replace( '"', '', $from_name );
176 $from_name = trim( $from_name );
178 $from_email = substr( $content, strpos( $content, '<' ) + 1 );
179 $from_email = str_replace( '>', '', $from_email );
180 $from_email = trim( $from_email );
182 $from_name = trim( $content );
184 } elseif ( 'content-type' == strtolower($name) ) {
185 if ( strpos( $content,';' ) !== false ) {
186 list( $type, $charset ) = explode( ';', $content );
187 $content_type = trim( $type );
188 $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
190 $content_type = trim( $content );
193 // Add it to our grand headers array
194 $headers[trim( $name )] = trim( $content );
200 // Empty out the values that may be set
201 $phpmailer->ClearAddresses();
202 $phpmailer->ClearAllRecipients();
203 $phpmailer->ClearAttachments();
204 $phpmailer->ClearBCCs();
205 $phpmailer->ClearCCs();
206 $phpmailer->ClearCustomHeaders();
207 $phpmailer->ClearReplyTos();
209 // From email and name
210 // If we don't have a name from the input headers
211 if ( !isset( $from_name ) ) {
212 $from_name = 'WordPress';
215 // If we don't have an email from the input headers
216 if ( !isset( $from_email ) ) {
217 // Get the site domain and get rid of www.
218 $sitename = strtolower( $_SERVER['SERVER_NAME'] );
219 if ( substr( $sitename, 0, 4 ) == 'www.' ) {
220 $sitename = substr( $sitename, 4 );
223 $from_email = 'wordpress@' . $sitename;
226 // Set the from name and email
227 $phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
228 $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
230 // Set destination address
231 $phpmailer->AddAddress( $to );
233 // Set mail's subject and body
234 $phpmailer->Subject = $subject;
235 $phpmailer->Body = $message;
237 // Set to use PHP's mail()
238 $phpmailer->IsMail();
240 // Set Content-Type and charset
241 // If we don't have a content-type from the input headers
242 if ( !isset( $content_type ) ) {
243 $content_type = 'text/plain';
246 $content_type = apply_filters( 'wp_mail_content_type', $content_type );
248 // Set whether it's plaintext or not, depending on $content_type
249 if ( $content_type == 'text/html' ) {
250 $phpmailer->IsHTML( true );
252 $phpmailer->IsHTML( false );
255 // If we don't have a charset from the input headers
256 if ( !isset( $charset ) ) {
257 $charset = get_bloginfo( 'charset' );
260 // Set the content-type and charset
261 $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
263 // Set custom headers
264 if ( !empty( $headers ) ) {
265 foreach ( $headers as $name => $content ) {
266 $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
270 do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
273 $result = @$phpmailer->Send();
279 if ( !function_exists('wp_login') ) :
280 function wp_login($username, $password, $already_md5 = false) {
281 global $wpdb, $error;
283 $username = sanitize_user($username);
285 if ( '' == $username )
288 if ( '' == $password ) {
289 $error = __('<strong>ERROR</strong>: The password field is empty.');
293 $login = get_userdatabylogin($username);
294 //$login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
297 $error = __('<strong>ERROR</strong>: Invalid username.');
300 // If the password is already_md5, it has been double hashed.
301 // Otherwise, it is plain text.
302 if ( ($already_md5 && md5($login->user_pass) == $password) || ($login->user_login == $username && $login->user_pass == md5($password)) ) {
305 $error = __('<strong>ERROR</strong>: Incorrect password.');
313 if ( !function_exists('is_user_logged_in') ) :
314 function is_user_logged_in() {
315 $user = wp_get_current_user();
317 if ( $user->id == 0 )
324 if ( !function_exists('auth_redirect') ) :
325 function auth_redirect() {
326 // Checks if a user is logged in, if not redirects them to the login page
327 if ( (!empty($_COOKIE[USER_COOKIE]) &&
328 !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true)) ||
329 (empty($_COOKIE[USER_COOKIE])) ) {
332 wp_redirect(get_option('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
338 if ( !function_exists('check_admin_referer') ) :
339 function check_admin_referer($action = -1) {
340 $adminurl = strtolower(get_option('siteurl')).'/wp-admin';
341 $referer = strtolower(wp_get_referer());
342 if ( !wp_verify_nonce($_REQUEST['_wpnonce'], $action) &&
343 !(-1 == $action && strpos($referer, $adminurl) !== false)) {
344 wp_nonce_ays($action);
347 do_action('check_admin_referer', $action);
350 if ( !function_exists('check_ajax_referer') ) :
351 function check_ajax_referer() {
353 if ( ( $current = wp_get_current_user() ) && $current->ID )
354 $current_name = $current->data->user_login;
355 if ( !$current_name )
358 $cookie = explode('; ', urldecode(empty($_POST['cookie']) ? $_GET['cookie'] : $_POST['cookie'])); // AJAX scripts must pass cookie=document.cookie
359 foreach ( $cookie as $tasty ) {
360 if ( false !== strpos($tasty, USER_COOKIE) )
361 $user = substr(strstr($tasty, '='), 1);
362 if ( false !== strpos($tasty, PASS_COOKIE) )
363 $pass = substr(strstr($tasty, '='), 1);
366 if ( $current_name != $user || !wp_login( $user, $pass, true ) )
368 do_action('check_ajax_referer');
372 // Cookie safe redirect. Works around IIS Set-Cookie bug.
373 // http://support.microsoft.com/kb/q176113/
374 if ( !function_exists('wp_redirect') ) :
375 function wp_redirect($location, $status = 302) {
378 $location = apply_filters('wp_redirect', $location, $status);
380 if ( !$location ) // allows the wp_redirect filter to cancel a redirect
383 $location = wp_sanitize_redirect($location);
386 header("Refresh: 0;url=$location");
388 if ( php_sapi_name() != 'cgi-fcgi' )
389 status_header($status); // This causes problems on IIS and some FastCGI setups
390 header("Location: $location");
395 if ( !function_exists('wp_sanitize_redirect') ) :
397 * sanitizes a URL for use in a redirect
398 * @return string redirect-sanitized URL
400 function wp_sanitize_redirect($location) {
401 $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%]|i', '', $location);
402 $location = wp_kses_no_null($location);
404 // remove %0d and %0a from location
405 $strip = array('%0d', '%0a');
409 foreach($strip as $val) {
410 while(strpos($location, $val) !== false) {
412 $location = str_replace($val, '', $location);
420 if ( !function_exists('wp_safe_redirect') ) :
422 * performs a safe (local) redirect, using wp_redirect()
425 function wp_safe_redirect($location, $status = 302) {
427 // Need to look at the URL the way it will end up in wp_redirect()
428 $location = wp_sanitize_redirect($location);
430 // browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'
431 if ( substr($location, 0, 2) == '//' )
432 $location = 'http:' . $location;
434 $lp = parse_url($location);
435 $wpp = parse_url(get_option('home'));
437 $allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']), $lp['host']);
439 if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
440 $location = get_option('siteurl') . '/wp-admin/';
442 wp_redirect($location, $status);
446 if ( !function_exists('wp_get_cookie_login') ):
447 function wp_get_cookie_login() {
448 if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) )
451 return array('login' => $_COOKIE[USER_COOKIE], 'password' => $_COOKIE[PASS_COOKIE]);
456 if ( !function_exists('wp_setcookie') ) :
457 function wp_setcookie($username, $password, $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
459 $password = md5( md5($password) ); // Double hash the password in the cookie.
462 $cookiepath = COOKIEPATH;
464 $cookiepath = preg_replace('|https?://[^/]+|i', '', $home . '/' );
466 if ( empty($siteurl) ) {
467 $sitecookiepath = SITECOOKIEPATH;
468 $cookiehash = COOKIEHASH;
470 $sitecookiepath = preg_replace('|https?://[^/]+|i', '', $siteurl . '/' );
471 $cookiehash = md5($siteurl);
475 $expire = time() + 31536000;
479 setcookie(USER_COOKIE, $username, $expire, $cookiepath, COOKIE_DOMAIN);
480 setcookie(PASS_COOKIE, $password, $expire, $cookiepath, COOKIE_DOMAIN);
482 if ( $cookiepath != $sitecookiepath ) {
483 setcookie(USER_COOKIE, $username, $expire, $sitecookiepath, COOKIE_DOMAIN);
484 setcookie(PASS_COOKIE, $password, $expire, $sitecookiepath, COOKIE_DOMAIN);
489 if ( !function_exists('wp_clearcookie') ) :
490 function wp_clearcookie() {
491 setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
492 setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
493 setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
494 setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
498 if ( ! function_exists('wp_notify_postauthor') ) :
499 function wp_notify_postauthor($comment_id, $comment_type='') {
502 $comment = get_comment($comment_id);
503 $post = get_post($comment->comment_post_ID);
504 $user = get_userdata( $post->post_author );
506 if ('' == $user->user_email) return false; // If there's no email to send the comment to
508 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
510 $blogname = get_option('blogname');
512 if ( empty( $comment_type ) ) $comment_type = 'comment';
514 if ('comment' == $comment_type) {
515 $notify_message = sprintf( __('New comment on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
516 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
517 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
518 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
519 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
520 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
521 $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
522 $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
523 } elseif ('trackback' == $comment_type) {
524 $notify_message = sprintf( __('New trackback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
525 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
526 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
527 $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
528 $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
529 $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
530 } elseif ('pingback' == $comment_type) {
531 $notify_message = sprintf( __('New pingback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
532 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
533 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
534 $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
535 $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
536 $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
538 $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
539 $notify_message .= sprintf( __('Delete it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&c=$comment_id" ) . "\r\n";
540 $notify_message .= sprintf( __('Spam it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&dt=spam&c=$comment_id" ) . "\r\n";
542 $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
544 if ( '' == $comment->comment_author ) {
545 $from = "From: \"$blogname\" <$wp_email>";
546 if ( '' != $comment->comment_author_email )
547 $reply_to = "Reply-To: $comment->comment_author_email";
549 $from = "From: \"$comment->comment_author\" <$wp_email>";
550 if ( '' != $comment->comment_author_email )
551 $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
554 $message_headers = "$from\n"
555 . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
557 if ( isset($reply_to) )
558 $message_headers .= $reply_to . "\n";
560 $notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id);
561 $subject = apply_filters('comment_notification_subject', $subject, $comment_id);
562 $message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id);
564 @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
570 /* wp_notify_moderator
571 notifies the moderator of the blog (usually the admin)
572 about a new comment that waits for approval
575 if ( !function_exists('wp_notify_moderator') ) :
576 function wp_notify_moderator($comment_id) {
579 if( get_option( "moderation_notify" ) == 0 )
582 $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
583 $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1");
585 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
586 $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
588 $notify_message = sprintf( __('A new comment on the post #%1$s "%2$s" is waiting for your approval'), $post->ID, $post->post_title ) . "\r\n";
589 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
590 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
591 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
592 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
593 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
594 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
595 $notify_message .= sprintf( __('Approve it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=mac&c=$comment_id" ) . "\r\n";
596 $notify_message .= sprintf( __('Delete it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&c=$comment_id" ) . "\r\n";
597 $notify_message .= sprintf( __('Spam it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&dt=spam&c=$comment_id" ) . "\r\n";
598 $notify_message .= sprintf( __('Currently %s comments are waiting for approval. Please visit the moderation panel:'), $comments_waiting ) . "\r\n";
599 $notify_message .= get_option('siteurl') . "/wp-admin/moderation.php\r\n";
601 $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), get_option('blogname'), $post->post_title );
602 $admin_email = get_option('admin_email');
604 $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id);
605 $subject = apply_filters('comment_moderation_subject', $subject, $comment_id);
607 @wp_mail($admin_email, $subject, $notify_message);
613 if ( !function_exists('wp_new_user_notification') ) :
614 function wp_new_user_notification($user_id, $plaintext_pass = '') {
615 $user = new WP_User($user_id);
617 $user_login = stripslashes($user->user_login);
618 $user_email = stripslashes($user->user_email);
620 $message = sprintf(__('New user registration on your blog %s:'), get_option('blogname')) . "\r\n\r\n";
621 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
622 $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
624 @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message);
626 if ( empty($plaintext_pass) )
629 $message = sprintf(__('Username: %s'), $user_login) . "\r\n";
630 $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
631 $message .= get_option('siteurl') . "/wp-login.php\r\n";
633 wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);
638 if ( !function_exists('wp_verify_nonce') ) :
639 function wp_verify_nonce($nonce, $action = -1) {
640 $user = wp_get_current_user();
641 $uid = (int) $user->id;
643 $i = ceil(time() / 43200);
645 //Allow for expanding range, but only do one check if we can
646 if( substr(wp_hash($i . $action . $uid), -12, 10) == $nonce || substr(wp_hash(($i - 1) . $action . $uid), -12, 10) == $nonce )
652 if ( !function_exists('wp_create_nonce') ) :
653 function wp_create_nonce($action = -1) {
654 $user = wp_get_current_user();
655 $uid = (int) $user->id;
657 $i = ceil(time() / 43200);
659 return substr(wp_hash($i . $action . $uid), -12, 10);
663 if ( !function_exists('wp_salt') ) :
665 $salt = get_option('secret');
667 $salt = DB_PASSWORD . DB_USER . DB_NAME . DB_HOST . ABSPATH;
673 if ( !function_exists('wp_hash') ) :
674 function wp_hash($data) {
677 if ( function_exists('hash_hmac') ) {
678 return hash_hmac('md5', $data, $salt);
680 return md5($data . $salt);