3 * These functions can be replaced via plugins. They are loaded after
9 if ( !function_exists('set_current_user') ) :
11 * set_current_user() - Populates global user information for any user
13 * Set $id to null and specify a name if you do not know a user's ID
16 * @see wp_set_current_user() An alias of wp_set_current_user()
18 * @param int|null $id User ID.
19 * @param string $name Optional. The user's username
20 * @return object returns wp_set_current_user()
22 function set_current_user($id, $name = '') {
23 return wp_set_current_user($id, $name);
27 if ( !function_exists('wp_set_current_user') ) :
29 * wp_set_current_user() - Changes the current user by ID or name
31 * Set $id to null and specify a name if you do not know a user's ID
33 * Some WordPress functionality is based on the current user and
34 * not based on the signed in user. Therefore, it opens the ability
35 * to edit and perform actions on users who aren't signed in.
38 * @global object $current_user The current user object which holds the user data.
39 * @uses do_action() Calls 'set_current_user' hook after setting the current user.
41 * @param int $id User ID
42 * @param string $name User's username
43 * @return WP_User Current user User object
45 function wp_set_current_user($id, $name = '') {
48 if ( isset($current_user) && ($id == $current_user->ID) )
51 $current_user = new WP_User($id, $name);
53 setup_userdata($current_user->ID);
55 do_action('set_current_user');
61 if ( !function_exists('wp_get_current_user') ) :
63 * wp_get_current_user() - Retrieve the current user object
67 * @return WP_User Current user WP_User object
69 function wp_get_current_user() {
72 get_currentuserinfo();
78 if ( !function_exists('get_currentuserinfo') ) :
80 * get_currentuserinfo() - Populate global variables with information about the currently logged in user
82 * Will set the current user, if the current user is not set. The current
83 * user will be set to the logged in person. If no user is logged in, then
84 * it will set the current user to 0, which is invalid and won't have any
88 * @uses $current_user Checks if the current user is set
89 * @uses wp_validate_auth_cookie() Retrieves current logged in user.
91 * @return bool|null False on XMLRPC Request and invalid auth cookie. Null when current user set
93 function get_currentuserinfo() {
96 if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
99 if ( ! empty($current_user) )
102 if ( ! $user = wp_validate_auth_cookie() ) {
103 wp_set_current_user(0);
107 wp_set_current_user($user);
111 if ( !function_exists('get_userdata') ) :
113 * get_userdata() - Retrieve user info by user ID
117 * @param int $user_id User ID
118 * @return bool|object False on failure, User DB row object
120 function get_userdata( $user_id ) {
123 $user_id = absint($user_id);
127 $user = wp_cache_get($user_id, 'users');
132 if ( !$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE ID = %d LIMIT 1", $user_id)) )
141 if ( !function_exists('update_user_cache') ) :
143 * update_user_cache() - Updates a users cache when overridden by a plugin
145 * Core function does nothing.
149 * @return bool Only returns true
151 function update_user_cache() {
156 if ( !function_exists('get_userdatabylogin') ) :
158 * get_userdatabylogin() - Retrieve user info by login name
162 * @param string $user_login User's username
163 * @return bool|object False on failure, User DB row object
165 function get_userdatabylogin($user_login) {
167 $user_login = sanitize_user( $user_login );
169 if ( empty( $user_login ) )
172 $user_id = wp_cache_get($user_login, 'userlogins');
175 if ( false !== $user_id )
176 $user = wp_cache_get($user_id, 'users');
178 if ( false !== $user )
181 if ( !$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_login = %s", $user_login)) )
190 if ( !function_exists('get_user_by_email') ) :
192 * get_user_by_email() - Retrieve user info by email
196 * @param string $email User's email address
197 * @return bool|object False on failure, User DB row object
199 function get_user_by_email($email) {
202 $user_id = wp_cache_get($email, 'useremail');
205 if ( false !== $user_id )
206 $user = wp_cache_get($user_id, 'users');
208 if ( false !== $user )
211 if ( !$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_email = %s", $email)) )
220 if ( !function_exists( 'wp_mail' ) ) :
222 * wp_mail() - Function to send mail, similar to PHP's mail
224 * A true return value does not automatically mean that the
225 * user received the email successfully. It just only means
226 * that the method used was able to process the request
227 * without any errors.
229 * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks
230 * allow from creating a from address like 'Name <email@address.com>'
231 * when both are set. If just 'wp_mail_from' is set, then just
232 * the email address will be used with no name.
234 * The default content type is 'text/plain' which does not
235 * allow using HTML. However, you can set the content type
236 * of the email by using the 'wp_mail_content_type' filter.
238 * The default charset is based on the charset used on the
239 * blog. The charset can be set using the 'wp_mail_charset'
243 * @uses apply_filters() Calls 'wp_mail' hook on an array of all of the parameters.
244 * @uses apply_filters() Calls 'wp_mail_from' hook to get the from email address.
245 * @uses apply_filters() Calls 'wp_mail_from_name' hook to get the from address name.
246 * @uses apply_filters() Calls 'wp_mail_content_type' hook to get the email content type.
247 * @uses apply_filters() Calls 'wp_mail_charset' hook to get the email charset
248 * @uses do_action_ref_array() Calls 'phpmailer_init' hook on the reference to
253 * @param string $to Email address to send message
254 * @param string $subject Email subject
255 * @param string $message Message contents
256 * @param string|array $headers Optional. Additional headers.
257 * @return bool Whether the email contents were sent successfully.
259 function wp_mail( $to, $subject, $message, $headers = '' ) {
260 // Compact the input, apply the filters, and extract them back out
261 extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers' ) ) );
265 // (Re)create it, if it's gone missing
266 if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
267 require_once ABSPATH . WPINC . '/class-phpmailer.php';
268 require_once ABSPATH . WPINC . '/class-smtp.php';
269 $phpmailer = new PHPMailer();
273 if ( empty( $headers ) ) {
275 } elseif ( !is_array( $headers ) ) {
276 // Explode the headers out, so this function can take both
277 // string headers and an array of headers.
278 $tempheaders = (array) explode( "\n", $headers );
281 // If it's actually got contents
282 if ( !empty( $tempheaders ) ) {
283 // Iterate through the raw headers
284 foreach ( $tempheaders as $header ) {
285 if ( strpos($header, ':') === false )
288 list( $name, $content ) = explode( ':', trim( $header ), 2 );
291 $name = trim( $name );
292 $content = trim( $content );
294 // Mainly for legacy -- process a From: header if it's there
295 if ( 'from' == strtolower($name) ) {
296 if ( strpos($content, '<' ) !== false ) {
297 // So... making my life hard again?
298 $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
299 $from_name = str_replace( '"', '', $from_name );
300 $from_name = trim( $from_name );
302 $from_email = substr( $content, strpos( $content, '<' ) + 1 );
303 $from_email = str_replace( '>', '', $from_email );
304 $from_email = trim( $from_email );
306 $from_name = trim( $content );
308 } elseif ( 'content-type' == strtolower($name) ) {
309 if ( strpos( $content,';' ) !== false ) {
310 list( $type, $charset ) = explode( ';', $content );
311 $content_type = trim( $type );
312 $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
314 $content_type = trim( $content );
317 // Add it to our grand headers array
318 $headers[trim( $name )] = trim( $content );
324 // Empty out the values that may be set
325 $phpmailer->ClearAddresses();
326 $phpmailer->ClearAllRecipients();
327 $phpmailer->ClearAttachments();
328 $phpmailer->ClearBCCs();
329 $phpmailer->ClearCCs();
330 $phpmailer->ClearCustomHeaders();
331 $phpmailer->ClearReplyTos();
333 // From email and name
334 // If we don't have a name from the input headers
335 if ( !isset( $from_name ) ) {
336 $from_name = 'WordPress';
339 // If we don't have an email from the input headers
340 if ( !isset( $from_email ) ) {
341 // Get the site domain and get rid of www.
342 $sitename = strtolower( $_SERVER['SERVER_NAME'] );
343 if ( substr( $sitename, 0, 4 ) == 'www.' ) {
344 $sitename = substr( $sitename, 4 );
347 $from_email = 'wordpress@' . $sitename;
350 // Set the from name and email
351 $phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
352 $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
354 // Set destination address
355 $phpmailer->AddAddress( $to );
357 // Set mail's subject and body
358 $phpmailer->Subject = $subject;
359 $phpmailer->Body = $message;
361 // Set to use PHP's mail()
362 $phpmailer->IsMail();
364 // Set Content-Type and charset
365 // If we don't have a content-type from the input headers
366 if ( !isset( $content_type ) ) {
367 $content_type = 'text/plain';
370 $content_type = apply_filters( 'wp_mail_content_type', $content_type );
372 // Set whether it's plaintext or not, depending on $content_type
373 if ( $content_type == 'text/html' ) {
374 $phpmailer->IsHTML( true );
376 $phpmailer->IsHTML( false );
379 // If we don't have a charset from the input headers
380 if ( !isset( $charset ) ) {
381 $charset = get_bloginfo( 'charset' );
384 // Set the content-type and charset
385 $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
387 // Set custom headers
388 if ( !empty( $headers ) ) {
389 foreach ( $headers as $name => $content ) {
390 $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
394 do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
397 $result = @$phpmailer->Send();
404 * wp_authenticate() - Checks a user's login information and logs them in if it checks out
407 * @param string $username User's username
408 * @param string $password User's password
409 * @return WP_Error|WP_User WP_User object if login successful, otherwise WP_Error object.
411 if ( !function_exists('wp_authenticate') ) :
412 function wp_authenticate($username, $password) {
413 $username = sanitize_user($username);
415 if ( '' == $username )
416 return new WP_Error('empty_username', __('<strong>ERROR</strong>: The username field is empty.'));
418 if ( '' == $password )
419 return new WP_Error('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));
421 $user = get_userdatabylogin($username);
423 if ( !$user || ($user->user_login != $username) ) {
424 do_action( 'wp_login_failed', $username );
425 return new WP_Error('invalid_username', __('<strong>ERROR</strong>: Invalid username.'));
428 $user = apply_filters('wp_authenticate_user', $user, $password);
429 if ( is_wp_error($user) ) {
430 do_action( 'wp_login_failed', $username );
434 if ( !wp_check_password($password, $user->user_pass, $user->ID) ) {
435 do_action( 'wp_login_failed', $username );
436 return new WP_Error('incorrect_password', __('<strong>ERROR</strong>: Incorrect password.'));
439 return new WP_User($user->ID);
444 * wp_logout() - Log the current user out
448 if ( !function_exists('wp_logout') ) :
449 function wp_logout() {
450 wp_clear_auth_cookie();
451 do_action('wp_logout');
455 if ( !function_exists('wp_validate_auth_cookie') ) :
457 * wp_validate_auth_cookie() - Validates authentication cookie
459 * The checks include making sure that the authentication cookie
460 * is set and pulling in the contents (if $cookie is not used).
462 * Makes sure the cookie is not expired. Verifies the hash in
463 * cookie is what is should be and compares the two.
467 * @param string $cookie Optional. If used, will validate contents instead of cookie's
468 * @return bool|int False if invalid cookie, User ID if valid.
470 function wp_validate_auth_cookie($cookie = '') {
471 if ( empty($cookie) ) {
472 if ( empty($_COOKIE[AUTH_COOKIE]) )
474 $cookie = $_COOKIE[AUTH_COOKIE];
477 $cookie_elements = explode('|', $cookie);
478 if ( count($cookie_elements) != 3 )
481 list($username, $expiration, $hmac) = $cookie_elements;
483 $expired = $expiration;
485 // Allow a grace period for POST and AJAX requests
486 if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] )
489 // Quick check to see if an honest cookie has expired
490 if ( $expired < time() )
493 $key = wp_hash($username . '|' . $expiration);
494 $hash = hash_hmac('md5', $username . '|' . $expiration, $key);
496 if ( $hmac != $hash )
499 $user = get_userdatabylogin($username);
507 if ( !function_exists('wp_generate_auth_cookie') ) :
509 * wp_generate_auth_cookie() - Generate authentication cookie contents
512 * @uses apply_filters() Calls 'auth_cookie' hook on $cookie contents, User ID
513 * and expiration of cookie.
515 * @param int $user_id User ID
516 * @param int $expiration Cookie expiration in seconds
517 * @return string Authentication cookie contents
519 function wp_generate_auth_cookie($user_id, $expiration) {
520 $user = get_userdata($user_id);
522 $key = wp_hash($user->user_login . '|' . $expiration);
523 $hash = hash_hmac('md5', $user->user_login . '|' . $expiration, $key);
525 $cookie = $user->user_login . '|' . $expiration . '|' . $hash;
527 return apply_filters('auth_cookie', $cookie, $user_id, $expiration);
531 if ( !function_exists('wp_set_auth_cookie') ) :
533 * wp_set_auth_cookie() - Sets the authentication cookies based User ID
535 * The $remember parameter increases the time that the cookie will
536 * be kept. The default the cookie is kept without remembering is
537 * two days. When $remember is set, the cookies will be kept for
538 * 14 days or two weeks.
542 * @param int $user_id User ID
543 * @param bool $remember Whether to remember the user or not
545 function wp_set_auth_cookie($user_id, $remember = false) {
547 $expiration = $expire = time() + 1209600;
549 $expiration = time() + 172800;
553 $cookie = wp_generate_auth_cookie($user_id, $expiration);
555 do_action('set_auth_cookie', $cookie, $expire);
557 setcookie(AUTH_COOKIE, $cookie, $expire, COOKIEPATH, COOKIE_DOMAIN);
558 if ( COOKIEPATH != SITECOOKIEPATH )
559 setcookie(AUTH_COOKIE, $cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN);
563 if ( !function_exists('wp_clear_auth_cookie') ) :
565 * wp_clear_auth_cookie() - Deletes all of the cookies associated with authentication
569 function wp_clear_auth_cookie() {
570 setcookie(AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
571 setcookie(AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
574 setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
575 setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
576 setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
577 setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
581 if ( !function_exists('is_user_logged_in') ) :
583 * is_user_logged_in() - Checks if the current visitor is a logged in user
587 * @return bool True if user is logged in, false if not logged in.
589 function is_user_logged_in() {
590 $user = wp_get_current_user();
592 if ( $user->id == 0 )
599 if ( !function_exists('auth_redirect') ) :
601 * auth_redirect() - Checks if a user is logged in, if not it redirects them to the login page
605 function auth_redirect() {
606 // Checks if a user is logged in, if not redirects them to the login page
607 if ( (!empty($_COOKIE[AUTH_COOKIE]) &&
608 !wp_validate_auth_cookie($_COOKIE[AUTH_COOKIE])) ||
609 (empty($_COOKIE[AUTH_COOKIE])) ) {
612 wp_redirect(get_option('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
618 if ( !function_exists('check_admin_referer') ) :
620 * check_admin_referer() - Makes sure that a user was referred from another admin page, to avoid security exploits
623 * @uses do_action() Calls 'check_admin_referer' on $action.
625 * @param string $action Action nonce
626 * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
628 function check_admin_referer($action = -1, $query_arg = '_wpnonce') {
629 $adminurl = strtolower(get_option('siteurl')).'/wp-admin';
630 $referer = strtolower(wp_get_referer());
631 $result = wp_verify_nonce($_REQUEST[$query_arg], $action);
632 if ( !$result && !(-1 == $action && strpos($referer, $adminurl) !== false) ) {
633 wp_nonce_ays($action);
636 do_action('check_admin_referer', $action, $result);
640 if ( !function_exists('check_ajax_referer') ) :
642 * check_ajax_referer() - Verifies the AJAX request to prevent processing requests external of the blog.
646 * @param string $action Action nonce
647 * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
649 function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
651 $nonce = $_REQUEST[$query_arg];
653 $nonce = $_REQUEST['_ajax_nonce'] ? $_REQUEST['_ajax_nonce'] : $_REQUEST['_wpnonce'];
655 $result = wp_verify_nonce( $nonce, $action );
657 if ( $die && false == $result )
660 do_action('check_ajax_referer', $action, $result);
666 if ( !function_exists('wp_redirect') ) :
668 * wp_redirect() - Redirects to another page, with a workaround for the IIS Set-Cookie bug
670 * @link http://support.microsoft.com/kb/q176113/
672 * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status.
674 * @param string $location The path to redirect to
675 * @param int $status Status code to use
676 * @return bool False if $location is not set
678 function wp_redirect($location, $status = 302) {
681 $location = apply_filters('wp_redirect', $location, $status);
682 $status = apply_filters('wp_redirect_status', $status, $location);
684 if ( !$location ) // allows the wp_redirect filter to cancel a redirect
687 $location = wp_sanitize_redirect($location);
690 header("Refresh: 0;url=$location");
692 if ( php_sapi_name() != 'cgi-fcgi' )
693 status_header($status); // This causes problems on IIS and some FastCGI setups
694 header("Location: $location");
699 if ( !function_exists('wp_sanitize_redirect') ) :
701 * wp_sanitize_redirect() - Sanitizes a URL for use in a redirect
705 * @return string redirect-sanitized URL
707 function wp_sanitize_redirect($location) {
708 $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%]|i', '', $location);
709 $location = wp_kses_no_null($location);
711 // remove %0d and %0a from location
712 $strip = array('%0d', '%0a');
716 foreach($strip as $val) {
717 while(strpos($location, $val) !== false) {
719 $location = str_replace($val, '', $location);
727 if ( !function_exists('wp_safe_redirect') ) :
729 * wp_safe_redirect() - Performs a safe (local) redirect, using wp_redirect()
731 * Checks whether the $location is using an allowed host, if it has an absolute
732 * path. A plugin can therefore set or remove allowed host(s) to or from the list.
734 * If the host is not allowed, then the redirect is to wp-admin on the siteurl
735 * instead. This prevents malicious redirects which redirect to another host, but
736 * only used in a few places.
739 * @uses apply_filters() Calls 'allowed_redirect_hosts' on an array containing
740 * WordPress host string and $location host string.
742 * @return void Does not return anything
744 function wp_safe_redirect($location, $status = 302) {
746 // Need to look at the URL the way it will end up in wp_redirect()
747 $location = wp_sanitize_redirect($location);
749 // browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'
750 if ( substr($location, 0, 2) == '//' )
751 $location = 'http:' . $location;
753 $lp = parse_url($location);
754 $wpp = parse_url(get_option('home'));
756 $allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']), isset($lp['host']) ? $lp['host'] : '');
758 if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
759 $location = get_option('siteurl') . '/wp-admin/';
761 wp_redirect($location, $status);
765 if ( ! function_exists('wp_notify_postauthor') ) :
767 * wp_notify_postauthor() - Notify an author of a comment/trackback/pingback to one of their posts
771 * @param int $comment_id Comment ID
772 * @param string $comment_type Optional. The comment type either 'comment' (default), 'trackback', or 'pingback'
773 * @return bool False if user email does not exist. True on completion.
775 function wp_notify_postauthor($comment_id, $comment_type='') {
776 $comment = get_comment($comment_id);
777 $post = get_post($comment->comment_post_ID);
778 $user = get_userdata( $post->post_author );
780 if ('' == $user->user_email) return false; // If there's no email to send the comment to
782 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
784 $blogname = get_option('blogname');
786 if ( empty( $comment_type ) ) $comment_type = 'comment';
788 if ('comment' == $comment_type) {
789 $notify_message = sprintf( __('New comment on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
790 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
791 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
792 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
793 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
794 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
795 $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
796 $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
797 } elseif ('trackback' == $comment_type) {
798 $notify_message = sprintf( __('New trackback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
799 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
800 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
801 $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
802 $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
803 $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
804 } elseif ('pingback' == $comment_type) {
805 $notify_message = sprintf( __('New pingback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
806 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
807 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
808 $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
809 $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
810 $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
812 $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
813 $notify_message .= sprintf( __('Delete it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&c=$comment_id" ) . "\r\n";
814 $notify_message .= sprintf( __('Spam it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&dt=spam&c=$comment_id" ) . "\r\n";
816 $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
818 if ( '' == $comment->comment_author ) {
819 $from = "From: \"$blogname\" <$wp_email>";
820 if ( '' != $comment->comment_author_email )
821 $reply_to = "Reply-To: $comment->comment_author_email";
823 $from = "From: \"$comment->comment_author\" <$wp_email>";
824 if ( '' != $comment->comment_author_email )
825 $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
828 $message_headers = "$from\n"
829 . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
831 if ( isset($reply_to) )
832 $message_headers .= $reply_to . "\n";
834 $notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id);
835 $subject = apply_filters('comment_notification_subject', $subject, $comment_id);
836 $message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id);
838 @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
844 if ( !function_exists('wp_notify_moderator') ) :
846 * wp_notify_moderator() - Notifies the moderator of the blog about a new comment that is awaiting approval
851 * @param int $comment_id Comment ID
852 * @return bool Always returns true
854 function wp_notify_moderator($comment_id) {
857 if( get_option( "moderation_notify" ) == 0 )
860 $comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID=%d LIMIT 1", $comment_id));
861 $post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID=%d LIMIT 1", $comment->comment_post_ID));
863 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
864 $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
866 switch ($comment->comment_type)
869 $notify_message = sprintf( __('A new trackback on the post #%1$s "%2$s" is waiting for your approval'), $post->ID, $post->post_title ) . "\r\n";
870 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
871 $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
872 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
873 $notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
876 $notify_message = sprintf( __('A new pingback on the post #%1$s "%2$s" is waiting for your approval'), $post->ID, $post->post_title ) . "\r\n";
877 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
878 $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
879 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
880 $notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
883 $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";
884 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
885 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
886 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
887 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
888 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
889 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
893 $notify_message .= sprintf( __('Approve it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=mac&c=$comment_id" ) . "\r\n";
894 $notify_message .= sprintf( __('Delete it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&c=$comment_id" ) . "\r\n";
895 $notify_message .= sprintf( __('Spam it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&dt=spam&c=$comment_id" ) . "\r\n";
897 $strCommentsPending = sprintf( __ngettext('%s comment', '%s comments', $comments_waiting), $comments_waiting );
898 $notify_message .= sprintf( __('Currently %s are waiting for approval. Please visit the moderation panel:'), $strCommentsPending ) . "\r\n";
899 $notify_message .= get_option('siteurl') . "/wp-admin/edit-comments.php?comment_status=moderated\r\n";
901 $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), get_option('blogname'), $post->post_title );
902 $admin_email = get_option('admin_email');
904 $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id);
905 $subject = apply_filters('comment_moderation_subject', $subject, $comment_id);
907 @wp_mail($admin_email, $subject, $notify_message);
913 if ( !function_exists('wp_new_user_notification') ) :
915 * wp_new_user_notification() - Notify the blog admin of a new user, normally via email
919 * @param int $user_id User ID
920 * @param string $plaintext_pass Optional. The user's plaintext password
922 function wp_new_user_notification($user_id, $plaintext_pass = '') {
923 $user = new WP_User($user_id);
925 $user_login = stripslashes($user->user_login);
926 $user_email = stripslashes($user->user_email);
928 $message = sprintf(__('New user registration on your blog %s:'), get_option('blogname')) . "\r\n\r\n";
929 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
930 $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
932 @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message);
934 if ( empty($plaintext_pass) )
937 $message = sprintf(__('Username: %s'), $user_login) . "\r\n";
938 $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
939 $message .= get_option('siteurl') . "/wp-login.php\r\n";
941 wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);
946 if ( !function_exists('wp_nonce_tick') ) :
948 * wp_nonce_tick() - Get the time-dependent variable for nonce creation
950 * A nonce has a lifespan of two ticks. Nonces in their second tick may be updated, e.g. by autosave.
956 function wp_nonce_tick() {
957 $nonce_life = apply_filters('nonce_life', 86400);
959 return ceil(time() / ( $nonce_life / 2 ));
963 if ( !function_exists('wp_verify_nonce') ) :
965 * wp_verify_nonce() - Verify that correct nonce was used with time limit
967 * The user is given an amount of time to use the token, so therefore, since
968 * the UID and $action remain the same, the independent variable is the time.
972 * @param string $nonce Nonce that was used in the form to verify
973 * @param string|int $action Should give context to what is taking place and be the same when nonce was created.
974 * @return bool Whether the nonce check passed or failed.
976 function wp_verify_nonce($nonce, $action = -1) {
977 $user = wp_get_current_user();
978 $uid = (int) $user->id;
980 $i = wp_nonce_tick();
982 // Nonce generated 0-12 hours ago
983 if ( substr(wp_hash($i . $action . $uid), -12, 10) == $nonce )
985 // Nonce generated 12-24 hours ago
986 if ( substr(wp_hash(($i - 1) . $action . $uid), -12, 10) == $nonce )
993 if ( !function_exists('wp_create_nonce') ) :
995 * wp_create_nonce() - Creates a random, one time use token
999 * @param string|int $action Scalar value to add context to the nonce.
1000 * @return string The one use form token
1002 function wp_create_nonce($action = -1) {
1003 $user = wp_get_current_user();
1004 $uid = (int) $user->id;
1006 $i = wp_nonce_tick();
1008 return substr(wp_hash($i . $action . $uid), -12, 10);
1012 if ( !function_exists('wp_salt') ) :
1014 * wp_salt() - Get salt to add to hashes to help prevent attacks
1016 * You can set the salt by defining two areas. One is in the database and
1017 * the other is in your wp-config.php file. The database location is defined
1018 * in the option named 'secret', but most likely will not need to be changed.
1020 * The second, located in wp-config.php, is a constant named 'SECRET_KEY', but
1021 * is not required. If the constant is not defined then the database constants
1022 * will be used, since they are most likely given to be unique. However, given
1023 * that the salt will be added to the password and can be seen, the constant
1024 * is recommended to be set manually.
1027 * define('SECRET_KEY', 'mAry1HadA15|\/|b17w55w1t3asSn09w');
1030 * Attention: Do not use above example!
1032 * Salting passwords helps against tools which has stored hashed values
1033 * of common dictionary strings. The added values makes it harder to crack
1034 * if given salt string is not weak.
1036 * Salting only helps if the string is not predictable and should be
1037 * made up of various characters. Think of the salt as a password for
1038 * securing your passwords, but common among all of your passwords.
1039 * Therefore the salt should be as long as possible as as difficult as
1040 * possible, because you will not have to remember it.
1044 * @return string Salt value from either 'SECRET_KEY' or 'secret' option
1046 function wp_salt() {
1047 global $wp_default_secret_key;
1049 if ( defined('SECRET_KEY') && ('' != SECRET_KEY) && ( $wp_default_secret_key != SECRET_KEY) )
1050 $secret_key = SECRET_KEY;
1052 if ( defined('SECRET_SALT') ) {
1053 $salt = SECRET_SALT;
1055 $salt = get_option('secret');
1056 if ( empty($salt) ) {
1057 $salt = wp_generate_password();
1058 update_option('secret', $salt);
1062 return apply_filters('salt', $secret_key . $salt);
1066 if ( !function_exists('wp_hash') ) :
1068 * wp_hash() - Get hash of given string
1071 * @uses wp_salt() Get WordPress salt
1073 * @param string $data Plain text to hash
1074 * @return string Hash of $data
1076 function wp_hash($data) {
1079 return hash_hmac('md5', $data, $salt);
1083 if ( !function_exists('wp_hash_password') ) :
1085 * wp_hash_password() - Create a hash (encrypt) of a plain text password
1087 * For integration with other applications, this function can be
1088 * overwritten to instead use the other package password checking
1092 * @global object $wp_hasher PHPass object
1093 * @uses PasswordHash::HashPassword
1095 * @param string $password Plain text user password to hash
1096 * @return string The hash string of the password
1098 function wp_hash_password($password) {
1101 if ( empty($wp_hasher) ) {
1102 require_once( ABSPATH . 'wp-includes/class-phpass.php');
1103 // By default, use the portable hash from phpass
1104 $wp_hasher = new PasswordHash(8, TRUE);
1107 return $wp_hasher->HashPassword($password);
1111 if ( !function_exists('wp_check_password') ) :
1113 * wp_check_password() - Checks the plaintext password against the encrypted Password
1115 * Maintains compatibility between old version and the new cookie
1116 * authentication protocol using PHPass library. The $hash parameter
1117 * is the encrypted password and the function compares the plain text
1118 * password when encypted similarly against the already encrypted
1119 * password to see if they match.
1121 * For integration with other applications, this function can be
1122 * overwritten to instead use the other package password checking
1126 * @global object $wp_hasher PHPass object used for checking the password
1127 * against the $hash + $password
1128 * @uses PasswordHash::CheckPassword
1130 * @param string $password Plaintext user's password
1131 * @param string $hash Hash of the user's password to check against.
1132 * @return bool False, if the $password does not match the hashed password
1134 function wp_check_password($password, $hash, $user_id = '') {
1137 // If the hash is still md5...
1138 if ( strlen($hash) <= 32 ) {
1139 $check = ( $hash == md5($password) );
1140 if ( $check && $user_id ) {
1141 // Rehash using new hash.
1142 wp_set_password($password, $user_id);
1143 $hash = wp_hash_password($password);
1146 return apply_filters('check_password', $check, $password, $hash, $user_id);
1149 // If the stored hash is longer than an MD5, presume the
1150 // new style phpass portable hash.
1151 if ( empty($wp_hasher) ) {
1152 require_once( ABSPATH . 'wp-includes/class-phpass.php');
1153 // By default, use the portable hash from phpass
1154 $wp_hasher = new PasswordHash(8, TRUE);
1157 $check = $wp_hasher->CheckPassword($password, $hash);
1159 return apply_filters('check_password', $check, $password, $hash, $user_id);
1163 if ( !function_exists('wp_generate_password') ) :
1165 * wp_generate_password() - Generates a random password drawn from the defined set of characters
1169 * @return string The random password
1171 function wp_generate_password($length = 12) {
1172 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()";
1174 for ( $i = 0; $i < $length; $i++ )
1175 $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
1180 if ( !function_exists('wp_set_password') ) :
1182 * wp_set_password() - Updates the user's password with a new encrypted one
1184 * For integration with other applications, this function can be
1185 * overwritten to instead use the other package password checking
1189 * @uses $wpdb WordPress database object for queries
1190 * @uses wp_hash_password() Used to encrypt the user's password before passing to the database
1192 * @param string $password The plaintext new user password
1193 * @param int $user_id User ID
1195 function wp_set_password( $password, $user_id ) {
1198 $hash = wp_hash_password($password);
1199 $query = $wpdb->prepare("UPDATE $wpdb->users SET user_pass = %s, user_activation_key = '' WHERE ID = %d", $hash, $user_id);
1200 $wpdb->query($query);
1201 wp_cache_delete($user_id, 'users');
1205 if ( !function_exists( 'get_avatar' ) ) :
1207 * get_avatar() - Get avatar for a user
1209 * Retrieve the avatar for a user provided a user ID or email address
1212 * @param int|string|object $id_or_email A user ID, email address, or comment object
1213 * @param int $size Size of the avatar image
1214 * @param string $default URL to a default image to use if no avatar is available
1215 * @return string <img> tag for the user's avatar
1217 function get_avatar( $id_or_email, $size = '96', $default = '' ) {
1218 if ( ! get_option('show_avatars') )
1221 if ( !is_numeric($size) )
1225 if ( is_numeric($id_or_email) ) {
1226 $id = (int) $id_or_email;
1227 $user = get_userdata($id);
1229 $email = $user->user_email;
1230 } elseif ( is_object($id_or_email) ) {
1231 if ( !empty($id_or_email->user_id) ) {
1232 $id = (int) $id_or_email->user_id;
1233 $user = get_userdata($id);
1235 $email = $user->user_email;
1236 } elseif ( !empty($id_or_email->comment_author_email) ) {
1237 $email = $id_or_email->comment_author_email;
1240 $email = $id_or_email;
1243 if ( empty($default) )
1244 $default = "http://www.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=$size"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
1246 if ( !empty($email) ) {
1247 $out = 'http://www.gravatar.com/avatar/';
1248 $out .= md5( strtolower( $email ) );
1249 $out .= '?s='.$size;
1250 $out .= '&d=' . urlencode( $default );
1252 $rating = get_option('avatar_rating');
1253 if ( !empty( $rating ) )
1254 $out .= "&r={$rating}";
1256 $avatar = "<img alt='' src='{$out}' class='avatar avatar-{$size}' height='{$size}' width='{$size}' />";
1258 $avatar = "<img alt='' src='{$default}' class='avatar avatar-{$size} avatar-default' height='{$size}' width='{$size}' />";
1261 return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default);
1265 if ( !function_exists('wp_setcookie') ) :
1267 * wp_setcookie() - Sets a cookie for a user who just logged in
1270 * @deprecated Use wp_set_auth_cookie()
1271 * @see wp_set_auth_cookie()
1273 * @param string $username The user's username
1274 * @param string $password Optional. The user's password
1275 * @param bool $already_md5 Optional. Whether the password has already been through MD5
1276 * @param string $home Optional. Will be used instead of COOKIEPATH if set
1277 * @param string $siteurl Optional. Will be used instead of SITECOOKIEPATH if set
1278 * @param bool $remember Optional. Remember that the user is logged in
1280 function wp_setcookie($username, $password = '', $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
1281 _deprecated_function( __FUNCTION__, '2.5', 'wp_set_auth_cookie()' );
1282 $user = get_userdatabylogin($username);
1283 wp_set_auth_cookie($user->ID, $remember);
1287 if ( !function_exists('wp_clearcookie') ) :
1289 * wp_clearcookie() - Clears the authentication cookie, logging the user out
1292 * @deprecated Use wp_clear_auth_cookie()
1293 * @see wp_clear_auth_cookie()
1295 function wp_clearcookie() {
1296 _deprecated_function( __FUNCTION__, '2.5', 'wp_clear_auth_cookie()' );
1297 wp_clear_auth_cookie();
1301 if ( !function_exists('wp_get_cookie_login') ):
1303 * wp_get_cookie_login() - Gets the user cookie login
1305 * This function is deprecated and should no longer be extended as it won't
1306 * be used anywhere in WordPress. Also, plugins shouldn't use it either.
1309 * @deprecated No alternative
1311 * @return bool Always returns false
1313 function wp_get_cookie_login() {
1314 _deprecated_function( __FUNCTION__, '2.5', '' );
1319 if ( !function_exists('wp_login') ) :
1321 * wp_login() - Checks a users login information and logs them in if it checks out
1323 * Use the global $error to get the reason why the login failed.
1324 * If the username is blank, no error will be set, so assume
1325 * blank username on that case.
1327 * Plugins extending this function should also provide the global
1328 * $error and set what the error is, so that those checking the
1329 * global for why there was a failure can utilize it later.
1332 * @deprecated Use wp_signon()
1333 * @global string $error Error when false is returned
1335 * @param string $username User's username
1336 * @param string $password User's password
1337 * @param bool $deprecated Not used
1338 * @return bool False on login failure, true on successful check
1340 function wp_login($username, $password, $deprecated = '') {
1343 $user = wp_authenticate($username, $password);
1345 if ( ! is_wp_error($user) )
1348 $error = $user->get_error_message();