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->Sender = apply_filters( 'wp_mail_from', $from_email );
229 $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
231 // Set destination address
232 $phpmailer->AddAddress( $to );
234 // Set mail's subject and body
235 $phpmailer->Subject = $subject;
236 $phpmailer->Body = $message;
238 // Set to use PHP's mail()
239 $phpmailer->IsMail();
241 // Set Content-Type and charset
242 // If we don't have a content-type from the input headers
243 if ( !isset( $content_type ) ) {
244 $content_type = 'text/plain';
247 $content_type = apply_filters( 'wp_mail_content_type', $content_type );
249 // Set whether it's plaintext or not, depending on $content_type
250 if ( $content_type == 'text/html' ) {
251 $phpmailer->IsHTML( true );
253 $phpmailer->IsHTML( false );
256 // If we don't have a charset from the input headers
257 if ( !isset( $charset ) ) {
258 $charset = get_bloginfo( 'charset' );
261 // Set the content-type and charset
262 $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
264 // Set custom headers
265 if ( !empty( $headers ) ) {
266 foreach ( $headers as $name => $content ) {
267 $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
271 do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
274 $result = @$phpmailer->Send();
280 if ( !function_exists('wp_login') ) :
281 function wp_login($username, $password, $already_md5 = false) {
282 global $wpdb, $error;
284 $username = sanitize_user($username);
286 if ( '' == $username )
289 if ( '' == $password ) {
290 $error = __('<strong>ERROR</strong>: The password field is empty.');
294 $login = get_userdatabylogin($username);
295 //$login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
298 $error = __('<strong>ERROR</strong>: Invalid username.');
301 // If the password is already_md5, it has been double hashed.
302 // Otherwise, it is plain text.
303 if ( ($already_md5 && md5($login->user_pass) == $password) || ($login->user_login == $username && $login->user_pass == md5($password)) ) {
306 $error = __('<strong>ERROR</strong>: Incorrect password.');
314 if ( !function_exists('is_user_logged_in') ) :
315 function is_user_logged_in() {
316 $user = wp_get_current_user();
318 if ( $user->id == 0 )
325 if ( !function_exists('auth_redirect') ) :
326 function auth_redirect() {
327 // Checks if a user is logged in, if not redirects them to the login page
328 if ( (!empty($_COOKIE[USER_COOKIE]) &&
329 !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true)) ||
330 (empty($_COOKIE[USER_COOKIE])) ) {
333 wp_redirect(get_option('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
339 if ( !function_exists('check_admin_referer') ) :
340 function check_admin_referer($action = -1) {
341 $adminurl = strtolower(get_option('siteurl')).'/wp-admin';
342 $referer = strtolower(wp_get_referer());
343 if ( !wp_verify_nonce($_REQUEST['_wpnonce'], $action) &&
344 !(-1 == $action && strpos($referer, $adminurl) !== false)) {
345 wp_nonce_ays($action);
348 do_action('check_admin_referer', $action);
351 if ( !function_exists('check_ajax_referer') ) :
352 function check_ajax_referer() {
354 if ( ( $current = wp_get_current_user() ) && $current->ID )
355 $current_name = $current->data->user_login;
356 if ( !$current_name )
359 $cookie = explode('; ', urldecode(empty($_POST['cookie']) ? $_GET['cookie'] : $_POST['cookie'])); // AJAX scripts must pass cookie=document.cookie
360 foreach ( $cookie as $tasty ) {
361 if ( false !== strpos($tasty, USER_COOKIE) )
362 $user = substr(strstr($tasty, '='), 1);
363 if ( false !== strpos($tasty, PASS_COOKIE) )
364 $pass = substr(strstr($tasty, '='), 1);
367 if ( $current_name != $user || !wp_login( $user, $pass, true ) )
369 do_action('check_ajax_referer');
373 // Cookie safe redirect. Works around IIS Set-Cookie bug.
374 // http://support.microsoft.com/kb/q176113/
375 if ( !function_exists('wp_redirect') ) :
376 function wp_redirect($location, $status = 302) {
379 $location = apply_filters('wp_redirect', $location, $status);
381 if ( !$location ) // allows the wp_redirect filter to cancel a redirect
384 $location = wp_sanitize_redirect($location);
387 header("Refresh: 0;url=$location");
389 if ( php_sapi_name() != 'cgi-fcgi' )
390 status_header($status); // This causes problems on IIS and some FastCGI setups
391 header("Location: $location");
396 if ( !function_exists('wp_sanitize_redirect') ) :
398 * sanitizes a URL for use in a redirect
399 * @return string redirect-sanitized URL
401 function wp_sanitize_redirect($location) {
402 $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%]|i', '', $location);
403 $location = wp_kses_no_null($location);
405 // remove %0d and %0a from location
406 $strip = array('%0d', '%0a');
410 foreach($strip as $val) {
411 while(strpos($location, $val) !== false) {
413 $location = str_replace($val, '', $location);
421 if ( !function_exists('wp_safe_redirect') ) :
423 * performs a safe (local) redirect, using wp_redirect()
426 function wp_safe_redirect($location, $status = 302) {
428 // Need to look at the URL the way it will end up in wp_redirect()
429 $location = wp_sanitize_redirect($location);
431 // browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'
432 if ( substr($location, 0, 2) == '//' )
433 $location = 'http:' . $location;
435 $lp = parse_url($location);
436 $wpp = parse_url(get_option('home'));
438 $allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']), $lp['host']);
440 if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
441 $location = get_option('siteurl') . '/wp-admin/';
443 wp_redirect($location, $status);
447 if ( !function_exists('wp_get_cookie_login') ):
448 function wp_get_cookie_login() {
449 if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) )
452 return array('login' => $_COOKIE[USER_COOKIE], 'password' => $_COOKIE[PASS_COOKIE]);
457 if ( !function_exists('wp_setcookie') ) :
458 function wp_setcookie($username, $password, $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
460 $password = md5( md5($password) ); // Double hash the password in the cookie.
463 $cookiepath = COOKIEPATH;
465 $cookiepath = preg_replace('|https?://[^/]+|i', '', $home . '/' );
467 if ( empty($siteurl) ) {
468 $sitecookiepath = SITECOOKIEPATH;
469 $cookiehash = COOKIEHASH;
471 $sitecookiepath = preg_replace('|https?://[^/]+|i', '', $siteurl . '/' );
472 $cookiehash = md5($siteurl);
476 $expire = time() + 31536000;
480 setcookie(USER_COOKIE, $username, $expire, $cookiepath, COOKIE_DOMAIN);
481 setcookie(PASS_COOKIE, $password, $expire, $cookiepath, COOKIE_DOMAIN);
483 if ( $cookiepath != $sitecookiepath ) {
484 setcookie(USER_COOKIE, $username, $expire, $sitecookiepath, COOKIE_DOMAIN);
485 setcookie(PASS_COOKIE, $password, $expire, $sitecookiepath, COOKIE_DOMAIN);
490 if ( !function_exists('wp_clearcookie') ) :
491 function wp_clearcookie() {
492 setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
493 setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
494 setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
495 setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
499 if ( ! function_exists('wp_notify_postauthor') ) :
500 function wp_notify_postauthor($comment_id, $comment_type='') {
503 $comment = get_comment($comment_id);
504 $post = get_post($comment->comment_post_ID);
505 $user = get_userdata( $post->post_author );
507 if ('' == $user->user_email) return false; // If there's no email to send the comment to
509 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
511 $blogname = get_option('blogname');
513 if ( empty( $comment_type ) ) $comment_type = 'comment';
515 if ('comment' == $comment_type) {
516 $notify_message = sprintf( __('New comment on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
517 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
518 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
519 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
520 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
521 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
522 $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
523 $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
524 } elseif ('trackback' == $comment_type) {
525 $notify_message = sprintf( __('New trackback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
526 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
527 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
528 $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
529 $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
530 $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
531 } elseif ('pingback' == $comment_type) {
532 $notify_message = sprintf( __('New pingback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
533 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
534 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
535 $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
536 $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
537 $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
539 $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
540 $notify_message .= sprintf( __('Delete it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&c=$comment_id" ) . "\r\n";
541 $notify_message .= sprintf( __('Spam it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&dt=spam&c=$comment_id" ) . "\r\n";
543 $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
545 if ( '' == $comment->comment_author ) {
546 $from = "From: \"$blogname\" <$wp_email>";
547 if ( '' != $comment->comment_author_email )
548 $reply_to = "Reply-To: $comment->comment_author_email";
550 $from = "From: \"$comment->comment_author\" <$wp_email>";
551 if ( '' != $comment->comment_author_email )
552 $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
555 $message_headers = "$from\n"
556 . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
558 if ( isset($reply_to) )
559 $message_headers .= $reply_to . "\n";
561 $notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id);
562 $subject = apply_filters('comment_notification_subject', $subject, $comment_id);
563 $message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id);
565 @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
571 /* wp_notify_moderator
572 notifies the moderator of the blog (usually the admin)
573 about a new comment that waits for approval
576 if ( !function_exists('wp_notify_moderator') ) :
577 function wp_notify_moderator($comment_id) {
580 if( get_option( "moderation_notify" ) == 0 )
583 $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
584 $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1");
586 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
587 $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
589 $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";
590 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
591 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
592 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
593 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
594 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
595 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
596 $notify_message .= sprintf( __('Approve it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=mac&c=$comment_id" ) . "\r\n";
597 $notify_message .= sprintf( __('Delete it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&c=$comment_id" ) . "\r\n";
598 $notify_message .= sprintf( __('Spam it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&dt=spam&c=$comment_id" ) . "\r\n";
599 $notify_message .= sprintf( __('Currently %s comments are waiting for approval. Please visit the moderation panel:'), $comments_waiting ) . "\r\n";
600 $notify_message .= get_option('siteurl') . "/wp-admin/moderation.php\r\n";
602 $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), get_option('blogname'), $post->post_title );
603 $admin_email = get_option('admin_email');
605 $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id);
606 $subject = apply_filters('comment_moderation_subject', $subject, $comment_id);
608 @wp_mail($admin_email, $subject, $notify_message);
614 if ( !function_exists('wp_new_user_notification') ) :
615 function wp_new_user_notification($user_id, $plaintext_pass = '') {
616 $user = new WP_User($user_id);
618 $user_login = stripslashes($user->user_login);
619 $user_email = stripslashes($user->user_email);
621 $message = sprintf(__('New user registration on your blog %s:'), get_option('blogname')) . "\r\n\r\n";
622 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
623 $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
625 @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message);
627 if ( empty($plaintext_pass) )
630 $message = sprintf(__('Username: %s'), $user_login) . "\r\n";
631 $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
632 $message .= get_option('siteurl') . "/wp-login.php\r\n";
634 wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);
639 if ( !function_exists('wp_verify_nonce') ) :
640 function wp_verify_nonce($nonce, $action = -1) {
641 $user = wp_get_current_user();
642 $uid = (int) $user->id;
644 $i = ceil(time() / 43200);
646 //Allow for expanding range, but only do one check if we can
647 if( substr(wp_hash($i . $action . $uid), -12, 10) == $nonce || substr(wp_hash(($i - 1) . $action . $uid), -12, 10) == $nonce )
653 if ( !function_exists('wp_create_nonce') ) :
654 function wp_create_nonce($action = -1) {
655 $user = wp_get_current_user();
656 $uid = (int) $user->id;
658 $i = ceil(time() / 43200);
660 return substr(wp_hash($i . $action . $uid), -12, 10);
664 if ( !function_exists('wp_salt') ) :
666 $salt = get_option('secret');
668 $salt = DB_PASSWORD . DB_USER . DB_NAME . DB_HOST . ABSPATH;
674 if ( !function_exists('wp_hash') ) :
675 function wp_hash($data) {
678 if ( function_exists('hash_hmac') ) {
679 return hash_hmac('md5', $data, $salt);
681 return md5($data . $salt);