3 * These functions can be replaced via plugins. If plugins do not redefine these
4 * functions, then these will be used instead.
9 if ( !function_exists('set_current_user') ) :
11 * Changes the current user by ID or name.
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 * 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 not based on
34 * the signed in user. Therefore, it opens the ability to edit and perform
35 * 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 * 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 * 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 user
83 * will be set to the logged in person. If no user is logged in, then it will
84 * set the current user to 0, which is invalid and won't have any permissions.
87 * @uses $current_user Checks if the current user is set
88 * @uses wp_validate_auth_cookie() Retrieves current logged in user.
90 * @return bool|null False on XMLRPC Request and invalid auth cookie. Null when current user set
92 function get_currentuserinfo() {
95 if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
98 if ( ! empty($current_user) )
101 if ( ! $user = wp_validate_auth_cookie() ) {
102 if ( empty($_COOKIE[LOGGED_IN_COOKIE]) || !$user = wp_validate_auth_cookie($_COOKIE[LOGGED_IN_COOKIE], 'logged_in') ) {
103 wp_set_current_user(0);
108 wp_set_current_user($user);
112 if ( !function_exists('get_userdata') ) :
114 * Retrieve user info by user ID.
118 * @param int $user_id User ID
119 * @return bool|object False on failure, User DB row object
121 function get_userdata( $user_id ) {
124 $user_id = absint($user_id);
128 $user = wp_cache_get($user_id, 'users');
133 if ( !$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE ID = %d LIMIT 1", $user_id)) )
142 if ( !function_exists('get_user_by') ) :
144 * Retrieve user info by a given field
148 * @param string $field The field to retrieve the user with. id | slug | email | login
149 * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
150 * @return bool|object False on failure, User DB row object
152 function get_user_by($field, $value) {
157 return get_userdata($value);
160 $user_id = wp_cache_get($value, 'userslugs');
161 $field = 'user_nicename';
164 $user_id = wp_cache_get($value, 'useremail');
165 $field = 'user_email';
168 $value = sanitize_user( $value );
169 $user_id = wp_cache_get($value, 'userlogins');
170 $field = 'user_login';
176 if ( false !== $user_id )
177 return get_userdata($user_id);
179 if ( !$user = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->users WHERE $field = %s", $value) ) )
188 if ( !function_exists('get_userdatabylogin') ) :
190 * Retrieve user info by login name.
194 * @param string $user_login User's username
195 * @return bool|object False on failure, User DB row object
197 function get_userdatabylogin($user_login) {
198 return get_user_by('login', $user_login);
202 if ( !function_exists('get_user_by_email') ) :
204 * Retrieve user info by email.
208 * @param string $email User's email address
209 * @return bool|object False on failure, User DB row object
211 function get_user_by_email($email) {
212 return get_user_by('email', $email);
216 if ( !function_exists( 'wp_mail' ) ) :
218 * Send mail, similar to PHP's mail
220 * A true return value does not automatically mean that the user received the
221 * email successfully. It just only means that the method used was able to
222 * process the request without any errors.
224 * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from
225 * creating a from address like 'Name <email@address.com>' when both are set. If
226 * just 'wp_mail_from' is set, then just the email address will be used with no
229 * The default content type is 'text/plain' which does not allow using HTML.
230 * However, you can set the content type of the email by using the
231 * 'wp_mail_content_type' filter.
233 * The default charset is based on the charset used on the blog. The charset can
234 * be set using the 'wp_mail_charset' filter.
237 * @uses apply_filters() Calls 'wp_mail' hook on an array of all of the parameters.
238 * @uses apply_filters() Calls 'wp_mail_from' hook to get the from email address.
239 * @uses apply_filters() Calls 'wp_mail_from_name' hook to get the from address name.
240 * @uses apply_filters() Calls 'wp_mail_content_type' hook to get the email content type.
241 * @uses apply_filters() Calls 'wp_mail_charset' hook to get the email charset
242 * @uses do_action_ref_array() Calls 'phpmailer_init' hook on the reference to
247 * @param string $to Email address to send message
248 * @param string $subject Email subject
249 * @param string $message Message contents
250 * @param string|array $headers Optional. Additional headers.
251 * @param string|array $attachments Optional. Files to attach.
252 * @return bool Whether the email contents were sent successfully.
254 function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
255 // Compact the input, apply the filters, and extract them back out
256 extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
258 if ( !is_array($attachments) )
259 $attachments = explode( "\n", $attachments );
263 // (Re)create it, if it's gone missing
264 if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
265 require_once ABSPATH . WPINC . '/class-phpmailer.php';
266 require_once ABSPATH . WPINC . '/class-smtp.php';
267 $phpmailer = new PHPMailer();
271 if ( empty( $headers ) ) {
274 if ( !is_array( $headers ) ) {
275 // Explode the headers out, so this function can take both
276 // string headers and an array of headers.
277 $tempheaders = (array) explode( "\n", $headers );
279 $tempheaders = $headers;
283 // If it's actually got contents
284 if ( !empty( $tempheaders ) ) {
285 // Iterate through the raw headers
286 foreach ( (array) $tempheaders as $header ) {
287 if ( strpos($header, ':') === false ) {
288 if ( false !== stripos( $header, 'boundary=' ) ) {
289 $parts = preg_split('/boundary=/i', trim( $header ) );
290 $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
295 list( $name, $content ) = explode( ':', trim( $header ), 2 );
298 $name = trim( $name );
299 $content = trim( $content );
301 // Mainly for legacy -- process a From: header if it's there
302 if ( 'from' == strtolower($name) ) {
303 if ( strpos($content, '<' ) !== false ) {
304 // So... making my life hard again?
305 $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
306 $from_name = str_replace( '"', '', $from_name );
307 $from_name = trim( $from_name );
309 $from_email = substr( $content, strpos( $content, '<' ) + 1 );
310 $from_email = str_replace( '>', '', $from_email );
311 $from_email = trim( $from_email );
313 $from_email = trim( $content );
315 } elseif ( 'content-type' == strtolower($name) ) {
316 if ( strpos( $content,';' ) !== false ) {
317 list( $type, $charset ) = explode( ';', $content );
318 $content_type = trim( $type );
319 if ( false !== stripos( $charset, 'charset=' ) ) {
320 $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
321 } elseif ( false !== stripos( $charset, 'boundary=' ) ) {
322 $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
326 $content_type = trim( $content );
328 } elseif ( 'cc' == strtolower($name) ) {
329 $cc = explode(",", $content);
330 } elseif ( 'bcc' == strtolower($name) ) {
331 $bcc = explode(",", $content);
333 // Add it to our grand headers array
334 $headers[trim( $name )] = trim( $content );
340 // Empty out the values that may be set
341 $phpmailer->ClearAddresses();
342 $phpmailer->ClearAllRecipients();
343 $phpmailer->ClearAttachments();
344 $phpmailer->ClearBCCs();
345 $phpmailer->ClearCCs();
346 $phpmailer->ClearCustomHeaders();
347 $phpmailer->ClearReplyTos();
349 // From email and name
350 // If we don't have a name from the input headers
351 if ( !isset( $from_name ) ) {
352 $from_name = 'WordPress';
355 /* If we don't have an email from the input headers default to wordpress@$sitename
356 * Some hosts will block outgoing mail from this address if it doesn't exist but
357 * there's no easy alternative. Defaulting to admin_email might appear to be another
358 * option but some hosts may refuse to relay mail from an unknown domain. See
359 * http://trac.wordpress.org/ticket/5007.
362 if ( !isset( $from_email ) ) {
363 // Get the site domain and get rid of www.
364 $sitename = strtolower( $_SERVER['SERVER_NAME'] );
365 if ( substr( $sitename, 0, 4 ) == 'www.' ) {
366 $sitename = substr( $sitename, 4 );
369 $from_email = 'wordpress@' . $sitename;
372 // Plugin authors can override the potentially troublesome default
373 $phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
374 $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
376 // Set destination address
377 $phpmailer->AddAddress( $to );
379 // Set mail's subject and body
380 $phpmailer->Subject = $subject;
381 $phpmailer->Body = $message;
383 // Add any CC and BCC recipients
385 foreach ( (array) $cc as $recipient ) {
386 $phpmailer->AddCc( trim($recipient) );
389 if ( !empty($bcc) ) {
390 foreach ( (array) $bcc as $recipient) {
391 $phpmailer->AddBcc( trim($recipient) );
395 // Set to use PHP's mail()
396 $phpmailer->IsMail();
398 // Set Content-Type and charset
399 // If we don't have a content-type from the input headers
400 if ( !isset( $content_type ) ) {
401 $content_type = 'text/plain';
404 $content_type = apply_filters( 'wp_mail_content_type', $content_type );
406 $phpmailer->ContentType = $content_type;
408 // Set whether it's plaintext or not, depending on $content_type
409 if ( $content_type == 'text/html' ) {
410 $phpmailer->IsHTML( true );
413 // If we don't have a charset from the input headers
414 if ( !isset( $charset ) ) {
415 $charset = get_bloginfo( 'charset' );
418 // Set the content-type and charset
419 $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
421 // Set custom headers
422 if ( !empty( $headers ) ) {
423 foreach( (array) $headers as $name => $content ) {
424 $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
426 if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) ) {
427 $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
431 if ( !empty( $attachments ) ) {
432 foreach ( $attachments as $attachment ) {
433 $phpmailer->AddAttachment($attachment);
437 do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
440 $result = @$phpmailer->Send();
446 if ( !function_exists('wp_authenticate') ) :
448 * Checks a user's login information and logs them in if it checks out.
452 * @param string $username User's username
453 * @param string $password User's password
454 * @return WP_Error|WP_User WP_User object if login successful, otherwise WP_Error object.
456 function wp_authenticate($username, $password) {
457 $username = sanitize_user($username);
458 $password = trim($password);
460 $user = apply_filters('authenticate', null, $username, $password);
462 if ( $user == null ) {
463 // TODO what should the error message be? (Or would these even happen?)
464 // Only needed if all authentication handlers fail to return anything.
465 $user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
468 $ignore_codes = array('empty_username', 'empty_password');
470 if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
471 do_action('wp_login_failed', $username);
478 if ( !function_exists('wp_logout') ) :
480 * Log the current user out.
484 function wp_logout() {
485 wp_clear_auth_cookie();
486 do_action('wp_logout');
490 if ( !function_exists('wp_validate_auth_cookie') ) :
492 * Validates authentication cookie.
494 * The checks include making sure that the authentication cookie is set and
495 * pulling in the contents (if $cookie is not used).
497 * Makes sure the cookie is not expired. Verifies the hash in cookie is what is
498 * should be and compares the two.
502 * @param string $cookie Optional. If used, will validate contents instead of cookie's
503 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
504 * @return bool|int False if invalid cookie, User ID if valid.
506 function wp_validate_auth_cookie($cookie = '', $scheme = '') {
507 if ( ! $cookie_elements = wp_parse_auth_cookie($cookie, $scheme) ) {
508 do_action('auth_cookie_malformed', $cookie, $scheme);
512 extract($cookie_elements, EXTR_OVERWRITE);
514 $expired = $expiration;
516 // Allow a grace period for POST and AJAX requests
517 if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] )
520 // Quick check to see if an honest cookie has expired
521 if ( $expired < time() ) {
522 do_action('auth_cookie_expired', $cookie_elements);
526 $user = get_userdatabylogin($username);
528 do_action('auth_cookie_bad_username', $cookie_elements);
532 $pass_frag = substr($user->user_pass, 8, 4);
534 $key = wp_hash($username . $pass_frag . '|' . $expiration, $scheme);
535 $hash = hash_hmac('md5', $username . '|' . $expiration, $key);
537 if ( $hmac != $hash ) {
538 do_action('auth_cookie_bad_hash', $cookie_elements);
542 do_action('auth_cookie_valid', $cookie_elements, $user);
548 if ( !function_exists('wp_generate_auth_cookie') ) :
550 * Generate authentication cookie contents.
553 * @uses apply_filters() Calls 'auth_cookie' hook on $cookie contents, User ID
554 * and expiration of cookie.
556 * @param int $user_id User ID
557 * @param int $expiration Cookie expiration in seconds
558 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
559 * @return string Authentication cookie contents
561 function wp_generate_auth_cookie($user_id, $expiration, $scheme = 'auth') {
562 $user = get_userdata($user_id);
564 $pass_frag = substr($user->user_pass, 8, 4);
566 $key = wp_hash($user->user_login . $pass_frag . '|' . $expiration, $scheme);
567 $hash = hash_hmac('md5', $user->user_login . '|' . $expiration, $key);
569 $cookie = $user->user_login . '|' . $expiration . '|' . $hash;
571 return apply_filters('auth_cookie', $cookie, $user_id, $expiration, $scheme);
575 if ( !function_exists('wp_parse_auth_cookie') ) :
577 * Parse a cookie into its components
581 * @param string $cookie
582 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
583 * @return array Authentication cookie components
585 function wp_parse_auth_cookie($cookie = '', $scheme = '') {
586 if ( empty($cookie) ) {
589 $cookie_name = AUTH_COOKIE;
592 $cookie_name = SECURE_AUTH_COOKIE;
595 $cookie_name = LOGGED_IN_COOKIE;
599 $cookie_name = SECURE_AUTH_COOKIE;
600 $scheme = 'secure_auth';
602 $cookie_name = AUTH_COOKIE;
607 if ( empty($_COOKIE[$cookie_name]) )
609 $cookie = $_COOKIE[$cookie_name];
612 $cookie_elements = explode('|', $cookie);
613 if ( count($cookie_elements) != 3 )
616 list($username, $expiration, $hmac) = $cookie_elements;
618 return compact('username', 'expiration', 'hmac', 'scheme');
622 if ( !function_exists('wp_set_auth_cookie') ) :
624 * Sets the authentication cookies based User ID.
626 * The $remember parameter increases the time that the cookie will be kept. The
627 * default the cookie is kept without remembering is two days. When $remember is
628 * set, the cookies will be kept for 14 days or two weeks.
632 * @param int $user_id User ID
633 * @param bool $remember Whether to remember the user or not
635 function wp_set_auth_cookie($user_id, $remember = false, $secure = '') {
637 $expiration = $expire = time() + apply_filters('auth_cookie_expiration', 1209600, $user_id, $remember);
639 $expiration = time() + apply_filters('auth_cookie_expiration', 172800, $user_id, $remember);
643 if ( '' === $secure )
644 $secure = is_ssl() ? true : false;
647 $auth_cookie_name = SECURE_AUTH_COOKIE;
648 $scheme = 'secure_auth';
650 $auth_cookie_name = AUTH_COOKIE;
654 $auth_cookie = wp_generate_auth_cookie($user_id, $expiration, $scheme);
655 $logged_in_cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
657 do_action('set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme);
658 do_action('set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in');
660 // Set httponly if the php version is >= 5.2.0
661 if ( version_compare(phpversion(), '5.2.0', 'ge') ) {
662 setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
663 setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
664 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, false, true);
665 if ( COOKIEPATH != SITECOOKIEPATH )
666 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, false, true);
668 $cookie_domain = COOKIE_DOMAIN;
669 if ( !empty($cookie_domain) )
670 $cookie_domain .= '; HttpOnly';
671 setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, $cookie_domain, $secure);
672 setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, $cookie_domain, $secure);
673 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, $cookie_domain);
674 if ( COOKIEPATH != SITECOOKIEPATH )
675 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, $cookie_domain);
680 if ( !function_exists('wp_clear_auth_cookie') ) :
682 * Removes all of the cookies associated with authentication.
686 function wp_clear_auth_cookie() {
687 do_action('clear_auth_cookie');
689 setcookie(AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
690 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
691 setcookie(AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
692 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
693 setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
694 setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
697 setcookie(AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
698 setcookie(AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
699 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
700 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
702 // Even older cookies
703 setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
704 setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
705 setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
706 setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
710 if ( !function_exists('is_user_logged_in') ) :
712 * Checks if the current visitor is a logged in user.
716 * @return bool True if user is logged in, false if not logged in.
718 function is_user_logged_in() {
719 $user = wp_get_current_user();
721 if ( $user->id == 0 )
728 if ( !function_exists('auth_redirect') ) :
730 * Checks if a user is logged in, if not it redirects them to the login page.
734 function auth_redirect() {
735 // Checks if a user is logged in, if not redirects them to the login page
737 if ( is_ssl() || force_ssl_admin() )
742 // If https is required and request is http, redirect
743 if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
744 if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
745 wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']));
748 wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
753 if ( $user_id = wp_validate_auth_cookie() ) {
754 do_action('auth_redirect', $user_id);
756 // If the user wants ssl but the session is not ssl, redirect.
757 if ( !$secure && get_user_option('use_ssl', $user_id) && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
758 if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
759 wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']));
762 wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
767 return; // The cookie is good so we're done
770 // The cookie is no good so force login
778 $redirect = ( strpos($_SERVER['REQUEST_URI'], '/options.php') && wp_get_referer() ) ? wp_get_referer() : $proto . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
780 $login_url = wp_login_url($redirect);
782 wp_redirect($login_url);
787 if ( !function_exists('check_admin_referer') ) :
789 * Makes sure that a user was referred from another admin page.
791 * To avoid security exploits.
794 * @uses do_action() Calls 'check_admin_referer' on $action.
796 * @param string $action Action nonce
797 * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
799 function check_admin_referer($action = -1, $query_arg = '_wpnonce') {
800 $adminurl = strtolower(admin_url());
801 $referer = strtolower(wp_get_referer());
802 $result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce($_REQUEST[$query_arg], $action) : false;
803 if ( !$result && !(-1 == $action && strpos($referer, $adminurl) !== false) ) {
804 wp_nonce_ays($action);
807 do_action('check_admin_referer', $action, $result);
811 if ( !function_exists('check_ajax_referer') ) :
813 * Verifies the AJAX request to prevent processing requests external of the blog.
817 * @param string $action Action nonce
818 * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
820 function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
822 $nonce = $_REQUEST[$query_arg];
824 $nonce = $_REQUEST['_ajax_nonce'] ? $_REQUEST['_ajax_nonce'] : $_REQUEST['_wpnonce'];
826 $result = wp_verify_nonce( $nonce, $action );
828 if ( $die && false == $result )
831 do_action('check_ajax_referer', $action, $result);
837 if ( !function_exists('wp_redirect') ) :
839 * Redirects to another page, with a workaround for the IIS Set-Cookie bug.
841 * @link http://support.microsoft.com/kb/q176113/
843 * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status.
845 * @param string $location The path to redirect to
846 * @param int $status Status code to use
847 * @return bool False if $location is not set
849 function wp_redirect($location, $status = 302) {
852 $location = apply_filters('wp_redirect', $location, $status);
853 $status = apply_filters('wp_redirect_status', $status, $location);
855 if ( !$location ) // allows the wp_redirect filter to cancel a redirect
858 $location = wp_sanitize_redirect($location);
861 header("Refresh: 0;url=$location");
863 if ( php_sapi_name() != 'cgi-fcgi' )
864 status_header($status); // This causes problems on IIS and some FastCGI setups
865 header("Location: $location");
870 if ( !function_exists('wp_sanitize_redirect') ) :
872 * Sanitizes a URL for use in a redirect.
876 * @return string redirect-sanitized URL
878 function wp_sanitize_redirect($location) {
879 $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%!]|i', '', $location);
880 $location = wp_kses_no_null($location);
882 // remove %0d and %0a from location
883 $strip = array('%0d', '%0a', '%0D', '%0A');
884 $location = _deep_replace($strip, $location);
889 if ( !function_exists('wp_safe_redirect') ) :
891 * Performs a safe (local) redirect, using wp_redirect().
893 * Checks whether the $location is using an allowed host, if it has an absolute
894 * path. A plugin can therefore set or remove allowed host(s) to or from the
897 * If the host is not allowed, then the redirect is to wp-admin on the siteurl
898 * instead. This prevents malicious redirects which redirect to another host,
899 * but only used in a few places.
902 * @uses wp_validate_redirect() To validate the redirect is to an allowed host.
904 * @return void Does not return anything
906 function wp_safe_redirect($location, $status = 302) {
908 // Need to look at the URL the way it will end up in wp_redirect()
909 $location = wp_sanitize_redirect($location);
911 $location = wp_validate_redirect($location, admin_url());
913 wp_redirect($location, $status);
917 if ( !function_exists('wp_validate_redirect') ) :
919 * Validates a URL for use in a redirect.
921 * Checks whether the $location is using an allowed host, if it has an absolute
922 * path. A plugin can therefore set or remove allowed host(s) to or from the
925 * If the host is not allowed, then the redirect is to $default supplied
928 * @uses apply_filters() Calls 'allowed_redirect_hosts' on an array containing
929 * WordPress host string and $location host string.
931 * @param string $location The redirect to validate
932 * @param string $default The value to return is $location is not allowed
933 * @return string redirect-sanitized URL
935 function wp_validate_redirect($location, $default = '') {
936 // browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'
937 if ( substr($location, 0, 2) == '//' )
938 $location = 'http:' . $location;
940 // In php 5 parse_url may fail if the URL query part contains http://, bug #38143
941 $test = ( $cut = strpos($location, '?') ) ? substr( $location, 0, $cut ) : $location;
943 $lp = parse_url($test);
944 $wpp = parse_url(get_option('home'));
946 $allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']), isset($lp['host']) ? $lp['host'] : '');
948 if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
949 $location = $default;
955 if ( ! function_exists('wp_notify_postauthor') ) :
957 * Notify an author of a comment/trackback/pingback to one of their posts.
961 * @param int $comment_id Comment ID
962 * @param string $comment_type Optional. The comment type either 'comment' (default), 'trackback', or 'pingback'
963 * @return bool False if user email does not exist. True on completion.
965 function wp_notify_postauthor($comment_id, $comment_type='') {
966 $comment = get_comment($comment_id);
967 $post = get_post($comment->comment_post_ID);
968 $user = get_userdata( $post->post_author );
969 $current_user = wp_get_current_user();
971 if ( $comment->user_id == $post->post_author ) return false; // The author moderated a comment on his own post
973 if ('' == $user->user_email) return false; // If there's no email to send the comment to
975 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
977 $blogname = get_option('blogname');
979 if ( empty( $comment_type ) ) $comment_type = 'comment';
981 if ('comment' == $comment_type) {
982 /* translators: 1: post id, 2: post title */
983 $notify_message = sprintf( __('New comment on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
984 /* translators: 1: comment author, 2: author IP, 3: author domain */
985 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
986 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
987 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
988 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
989 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
990 $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
991 /* translators: 1: blog name, 2: post title */
992 $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
993 } elseif ('trackback' == $comment_type) {
994 /* translators: 1: post id, 2: post title */
995 $notify_message = sprintf( __('New trackback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
996 /* translators: 1: website name, 2: author IP, 3: author domain */
997 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
998 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
999 $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1000 $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
1001 /* translators: 1: blog name, 2: post title */
1002 $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
1003 } elseif ('pingback' == $comment_type) {
1004 /* translators: 1: post id, 2: post title */
1005 $notify_message = sprintf( __('New pingback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
1006 /* translators: 1: comment author, 2: author IP, 3: author domain */
1007 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1008 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1009 $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
1010 $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
1011 /* translators: 1: blog name, 2: post title */
1012 $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
1014 $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
1015 $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=cdc&c=$comment_id") ) . "\r\n";
1016 $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=cdc&dt=spam&c=$comment_id") ) . "\r\n";
1018 $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
1020 if ( '' == $comment->comment_author ) {
1021 $from = "From: \"$blogname\" <$wp_email>";
1022 if ( '' != $comment->comment_author_email )
1023 $reply_to = "Reply-To: $comment->comment_author_email";
1025 $from = "From: \"$comment->comment_author\" <$wp_email>";
1026 if ( '' != $comment->comment_author_email )
1027 $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
1030 $message_headers = "$from\n"
1031 . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
1033 if ( isset($reply_to) )
1034 $message_headers .= $reply_to . "\n";
1036 $notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id);
1037 $subject = apply_filters('comment_notification_subject', $subject, $comment_id);
1038 $message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id);
1040 @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
1046 if ( !function_exists('wp_notify_moderator') ) :
1048 * Notifies the moderator of the blog about a new comment that is awaiting approval.
1053 * @param int $comment_id Comment ID
1054 * @return bool Always returns true
1056 function wp_notify_moderator($comment_id) {
1059 if( get_option( "moderation_notify" ) == 0 )
1062 $comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID=%d LIMIT 1", $comment_id));
1063 $post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID=%d LIMIT 1", $comment->comment_post_ID));
1065 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
1066 $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
1068 switch ($comment->comment_type)
1071 $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";
1072 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
1073 $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1074 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1075 $notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1078 $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";
1079 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
1080 $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1081 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1082 $notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1085 $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";
1086 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
1087 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1088 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
1089 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1090 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
1091 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1095 $notify_message .= sprintf( __('Approve it: %s'), admin_url("comment.php?action=mac&c=$comment_id") ) . "\r\n";
1096 $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=cdc&c=$comment_id") ) . "\r\n";
1097 $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=cdc&dt=spam&c=$comment_id") ) . "\r\n";
1099 $notify_message .= sprintf( _n('Currently %s comment is waiting for approval. Please visit the moderation panel:',
1100 'Currently %s comments are waiting for approval. Please visit the moderation panel:', $comments_waiting), number_format_i18n($comments_waiting) ) . "\r\n";
1101 $notify_message .= admin_url("edit-comments.php?comment_status=moderated") . "\r\n";
1103 $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), get_option('blogname'), $post->post_title );
1104 $admin_email = get_option('admin_email');
1105 $message_headers = '';
1107 $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id);
1108 $subject = apply_filters('comment_moderation_subject', $subject, $comment_id);
1109 $message_headers = apply_filters('comment_moderation_headers', $message_headers);
1111 @wp_mail($admin_email, $subject, $notify_message, $message_headers);
1117 if ( !function_exists('wp_password_change_notification') ) :
1119 * Notify the blog admin of a user changing password, normally via email.
1123 * @param object $user User Object
1125 function wp_password_change_notification(&$user) {
1126 // send a copy of password change notification to the admin
1127 // but check to see if it's the admin whose password we're changing, and skip this
1128 if ( $user->user_email != get_option('admin_email') ) {
1129 $message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n";
1130 wp_mail(get_option('admin_email'), sprintf(__('[%s] Password Lost/Changed'), get_option('blogname')), $message);
1135 if ( !function_exists('wp_new_user_notification') ) :
1137 * Notify the blog admin of a new user, normally via email.
1141 * @param int $user_id User ID
1142 * @param string $plaintext_pass Optional. The user's plaintext password
1144 function wp_new_user_notification($user_id, $plaintext_pass = '') {
1145 $user = new WP_User($user_id);
1147 $user_login = stripslashes($user->user_login);
1148 $user_email = stripslashes($user->user_email);
1150 $message = sprintf(__('New user registration on your blog %s:'), get_option('blogname')) . "\r\n\r\n";
1151 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
1152 $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
1154 @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message);
1156 if ( empty($plaintext_pass) )
1159 $message = sprintf(__('Username: %s'), $user_login) . "\r\n";
1160 $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
1161 $message .= wp_login_url() . "\r\n";
1163 wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);
1168 if ( !function_exists('wp_nonce_tick') ) :
1170 * Get the time-dependent variable for nonce creation.
1172 * A nonce has a lifespan of two ticks. Nonces in their second tick may be
1173 * updated, e.g. by autosave.
1179 function wp_nonce_tick() {
1180 $nonce_life = apply_filters('nonce_life', 86400);
1182 return ceil(time() / ( $nonce_life / 2 ));
1186 if ( !function_exists('wp_verify_nonce') ) :
1188 * Verify that correct nonce was used with time limit.
1190 * The user is given an amount of time to use the token, so therefore, since the
1191 * UID and $action remain the same, the independent variable is the time.
1195 * @param string $nonce Nonce that was used in the form to verify
1196 * @param string|int $action Should give context to what is taking place and be the same when nonce was created.
1197 * @return bool Whether the nonce check passed or failed.
1199 function wp_verify_nonce($nonce, $action = -1) {
1200 $user = wp_get_current_user();
1201 $uid = (int) $user->id;
1203 $i = wp_nonce_tick();
1205 // Nonce generated 0-12 hours ago
1206 if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) == $nonce )
1208 // Nonce generated 12-24 hours ago
1209 if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) == $nonce )
1216 if ( !function_exists('wp_create_nonce') ) :
1218 * Creates a random, one time use token.
1222 * @param string|int $action Scalar value to add context to the nonce.
1223 * @return string The one use form token
1225 function wp_create_nonce($action = -1) {
1226 $user = wp_get_current_user();
1227 $uid = (int) $user->id;
1229 $i = wp_nonce_tick();
1231 return substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10);
1235 if ( !function_exists('wp_salt') ) :
1237 * Get salt to add to hashes to help prevent attacks.
1239 * The secret key is located in two places: the database in case the secret key
1240 * isn't defined in the second place, which is in the wp-config.php file. If you
1241 * are going to set the secret key, then you must do so in the wp-config.php
1244 * The secret key in the database is randomly generated and will be appended to
1245 * the secret key that is in wp-config.php file in some instances. It is
1246 * important to have the secret key defined or changed in wp-config.php.
1248 * If you have installed WordPress 2.5 or later, then you will have the
1249 * SECRET_KEY defined in the wp-config.php already. You will want to change the
1250 * value in it because hackers will know what it is. If you have upgraded to
1251 * WordPress 2.5 or later version from a version before WordPress 2.5, then you
1252 * should add the constant to your wp-config.php file.
1254 * Below is an example of how the SECRET_KEY constant is defined with a value.
1255 * You must not copy the below example and paste into your wp-config.php. If you
1256 * need an example, then you can have a
1257 * {@link https://api.wordpress.org/secret-key/1.1/ secret key created} for you.
1260 * define('SECRET_KEY', 'mAry1HadA15|\/|b17w55w1t3asSn09w');
1263 * Salting passwords helps against tools which has stored hashed values of
1264 * common dictionary strings. The added values makes it harder to crack if given
1265 * salt string is not weak.
1268 * @link https://api.wordpress.org/secret-key/1.1/ Create a Secret Key for wp-config.php
1270 * @return string Salt value from either 'SECRET_KEY' or 'secret' option
1272 function wp_salt($scheme = 'auth') {
1273 global $wp_default_secret_key;
1275 if ( defined('SECRET_KEY') && ('' != SECRET_KEY) && ( $wp_default_secret_key != SECRET_KEY) )
1276 $secret_key = SECRET_KEY;
1278 if ( 'auth' == $scheme ) {
1279 if ( defined('AUTH_KEY') && ('' != AUTH_KEY) && ( $wp_default_secret_key != AUTH_KEY) )
1280 $secret_key = AUTH_KEY;
1282 if ( defined('AUTH_SALT') ) {
1284 } elseif ( defined('SECRET_SALT') ) {
1285 $salt = SECRET_SALT;
1287 $salt = get_option('auth_salt');
1288 if ( empty($salt) ) {
1289 $salt = wp_generate_password(64);
1290 update_option('auth_salt', $salt);
1293 } elseif ( 'secure_auth' == $scheme ) {
1294 if ( defined('SECURE_AUTH_KEY') && ('' != SECURE_AUTH_KEY) && ( $wp_default_secret_key != SECURE_AUTH_KEY) )
1295 $secret_key = SECURE_AUTH_KEY;
1297 if ( defined('SECURE_AUTH_SALT') ) {
1298 $salt = SECURE_AUTH_SALT;
1300 $salt = get_option('secure_auth_salt');
1301 if ( empty($salt) ) {
1302 $salt = wp_generate_password(64);
1303 update_option('secure_auth_salt', $salt);
1306 } elseif ( 'logged_in' == $scheme ) {
1307 if ( defined('LOGGED_IN_KEY') && ('' != LOGGED_IN_KEY) && ( $wp_default_secret_key != LOGGED_IN_KEY) )
1308 $secret_key = LOGGED_IN_KEY;
1310 if ( defined('LOGGED_IN_SALT') ) {
1311 $salt = LOGGED_IN_SALT;
1313 $salt = get_option('logged_in_salt');
1314 if ( empty($salt) ) {
1315 $salt = wp_generate_password(64);
1316 update_option('logged_in_salt', $salt);
1319 } elseif ( 'nonce' == $scheme ) {
1320 if ( defined('NONCE_KEY') && ('' != NONCE_KEY) && ( $wp_default_secret_key != NONCE_KEY) )
1321 $secret_key = NONCE_KEY;
1323 if ( defined('NONCE_SALT') ) {
1326 $salt = get_option('nonce_salt');
1327 if ( empty($salt) ) {
1328 $salt = wp_generate_password(64);
1329 update_option('nonce_salt', $salt);
1333 // ensure each auth scheme has its own unique salt
1334 $salt = hash_hmac('md5', $scheme, $secret_key);
1337 return apply_filters('salt', $secret_key . $salt, $scheme);
1341 if ( !function_exists('wp_hash') ) :
1343 * Get hash of given string.
1346 * @uses wp_salt() Get WordPress salt
1348 * @param string $data Plain text to hash
1349 * @return string Hash of $data
1351 function wp_hash($data, $scheme = 'auth') {
1352 $salt = wp_salt($scheme);
1354 return hash_hmac('md5', $data, $salt);
1358 if ( !function_exists('wp_hash_password') ) :
1360 * Create a hash (encrypt) of a plain text password.
1362 * For integration with other applications, this function can be overwritten to
1363 * instead use the other package password checking algorithm.
1366 * @global object $wp_hasher PHPass object
1367 * @uses PasswordHash::HashPassword
1369 * @param string $password Plain text user password to hash
1370 * @return string The hash string of the password
1372 function wp_hash_password($password) {
1375 if ( empty($wp_hasher) ) {
1376 require_once( ABSPATH . 'wp-includes/class-phpass.php');
1377 // By default, use the portable hash from phpass
1378 $wp_hasher = new PasswordHash(8, TRUE);
1381 return $wp_hasher->HashPassword($password);
1385 if ( !function_exists('wp_check_password') ) :
1387 * Checks the plaintext password against the encrypted Password.
1389 * Maintains compatibility between old version and the new cookie authentication
1390 * protocol using PHPass library. The $hash parameter is the encrypted password
1391 * and the function compares the plain text password when encypted similarly
1392 * against the already encrypted password to see if they match.
1394 * For integration with other applications, this function can be overwritten to
1395 * instead use the other package password checking algorithm.
1398 * @global object $wp_hasher PHPass object used for checking the password
1399 * against the $hash + $password
1400 * @uses PasswordHash::CheckPassword
1402 * @param string $password Plaintext user's password
1403 * @param string $hash Hash of the user's password to check against.
1404 * @return bool False, if the $password does not match the hashed password
1406 function wp_check_password($password, $hash, $user_id = '') {
1409 // If the hash is still md5...
1410 if ( strlen($hash) <= 32 ) {
1411 $check = ( $hash == md5($password) );
1412 if ( $check && $user_id ) {
1413 // Rehash using new hash.
1414 wp_set_password($password, $user_id);
1415 $hash = wp_hash_password($password);
1418 return apply_filters('check_password', $check, $password, $hash, $user_id);
1421 // If the stored hash is longer than an MD5, presume the
1422 // new style phpass portable hash.
1423 if ( empty($wp_hasher) ) {
1424 require_once( ABSPATH . 'wp-includes/class-phpass.php');
1425 // By default, use the portable hash from phpass
1426 $wp_hasher = new PasswordHash(8, TRUE);
1429 $check = $wp_hasher->CheckPassword($password, $hash);
1431 return apply_filters('check_password', $check, $password, $hash, $user_id);
1435 if ( !function_exists('wp_generate_password') ) :
1437 * Generates a random password drawn from the defined set of characters.
1441 * @param int $length The length of password to generate
1442 * @param bool $special_chars Whether to include standard special characters
1443 * @return string The random password
1445 function wp_generate_password($length = 12, $special_chars = true) {
1446 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
1447 if ( $special_chars )
1448 $chars .= '!@#$%^&*()';
1451 for ( $i = 0; $i < $length; $i++ )
1452 $password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
1457 if ( !function_exists('wp_rand') ) :
1459 * Generates a random number
1463 * @param int $min Lower limit for the generated number (optional, default is 0)
1464 * @param int $max Upper limit for the generated number (optional, default is 4294967295)
1465 * @return int A random number between min and max
1467 function wp_rand( $min = 0, $max = 0 ) {
1470 $seed = get_transient('random_seed');
1472 // Reset $rnd_value after 14 uses
1473 // 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value
1474 if ( strlen($rnd_value) < 8 ) {
1475 $rnd_value = md5( uniqid(microtime() . mt_rand(), true ) . $seed );
1476 $rnd_value .= sha1($rnd_value);
1477 $rnd_value .= sha1($rnd_value . $seed);
1478 $seed = md5($seed . $rnd_value);
1479 set_transient('random_seed', $seed);
1482 // Take the first 8 digits for our value
1483 $value = substr($rnd_value, 0, 8);
1485 // Strip the first eight, leaving the remainder for the next call to wp_rand().
1486 $rnd_value = substr($rnd_value, 8);
1488 $value = abs(hexdec($value));
1490 // Reduce the value to be within the min - max range
1491 // 4294967295 = 0xffffffff = max random number
1493 $value = $min + (($max - $min + 1) * ($value / (4294967295 + 1)));
1495 return abs(intval($value));
1499 if ( !function_exists('wp_set_password') ) :
1501 * Updates the user's password with a new encrypted one.
1503 * For integration with other applications, this function can be overwritten to
1504 * instead use the other package password checking algorithm.
1507 * @uses $wpdb WordPress database object for queries
1508 * @uses wp_hash_password() Used to encrypt the user's password before passing to the database
1510 * @param string $password The plaintext new user password
1511 * @param int $user_id User ID
1513 function wp_set_password( $password, $user_id ) {
1516 $hash = wp_hash_password($password);
1517 $wpdb->update($wpdb->users, array('user_pass' => $hash, 'user_activation_key' => ''), array('ID' => $user_id) );
1519 wp_cache_delete($user_id, 'users');
1523 if ( !function_exists( 'get_avatar' ) ) :
1525 * Retrieve the avatar for a user who provided a user ID or email address.
1528 * @param int|string|object $id_or_email A user ID, email address, or comment object
1529 * @param int $size Size of the avatar image
1530 * @param string $default URL to a default image to use if no avatar is available
1531 * @param string $alt Alternate text to use in image tag. Defaults to blank
1532 * @return string <img> tag for the user's avatar
1534 function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
1535 if ( ! get_option('show_avatars') )
1538 if ( false === $alt)
1541 $safe_alt = esc_attr( $alt );
1543 if ( !is_numeric($size) )
1547 if ( is_numeric($id_or_email) ) {
1548 $id = (int) $id_or_email;
1549 $user = get_userdata($id);
1551 $email = $user->user_email;
1552 } elseif ( is_object($id_or_email) ) {
1553 if ( isset($id_or_email->comment_type) && '' != $id_or_email->comment_type && 'comment' != $id_or_email->comment_type )
1554 return false; // No avatar for pingbacks or trackbacks
1556 if ( !empty($id_or_email->user_id) ) {
1557 $id = (int) $id_or_email->user_id;
1558 $user = get_userdata($id);
1560 $email = $user->user_email;
1561 } elseif ( !empty($id_or_email->comment_author_email) ) {
1562 $email = $id_or_email->comment_author_email;
1565 $email = $id_or_email;
1568 if ( empty($default) ) {
1569 $avatar_default = get_option('avatar_default');
1570 if ( empty($avatar_default) )
1571 $default = 'mystery';
1573 $default = $avatar_default;
1577 $host = 'https://secure.gravatar.com';
1579 $host = 'http://www.gravatar.com';
1581 if ( 'mystery' == $default )
1582 $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
1583 elseif ( 'blank' == $default )
1584 $default = includes_url('images/blank.gif');
1585 elseif ( !empty($email) && 'gravatar_default' == $default )
1587 elseif ( 'gravatar_default' == $default )
1588 $default = "$host/avatar/s={$size}";
1589 elseif ( empty($email) )
1590 $default = "$host/avatar/?d=$default&s={$size}";
1591 elseif ( strpos($default, 'http://') === 0 )
1592 $default = add_query_arg( 's', $size, $default );
1594 if ( !empty($email) ) {
1595 $out = "$host/avatar/";
1596 $out .= md5( strtolower( $email ) );
1597 $out .= '?s='.$size;
1598 $out .= '&d=' . urlencode( $default );
1600 $rating = get_option('avatar_rating');
1601 if ( !empty( $rating ) )
1602 $out .= "&r={$rating}";
1604 $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
1606 $avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
1609 return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
1613 if ( !function_exists('wp_setcookie') ) :
1615 * Sets a cookie for a user who just logged in.
1618 * @deprecated Use wp_set_auth_cookie()
1619 * @see wp_set_auth_cookie()
1621 * @param string $username The user's username
1622 * @param string $password Optional. The user's password
1623 * @param bool $already_md5 Optional. Whether the password has already been through MD5
1624 * @param string $home Optional. Will be used instead of COOKIEPATH if set
1625 * @param string $siteurl Optional. Will be used instead of SITECOOKIEPATH if set
1626 * @param bool $remember Optional. Remember that the user is logged in
1628 function wp_setcookie($username, $password = '', $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
1629 _deprecated_function( __FUNCTION__, '2.5', 'wp_set_auth_cookie()' );
1630 $user = get_userdatabylogin($username);
1631 wp_set_auth_cookie($user->ID, $remember);
1635 if ( !function_exists('wp_clearcookie') ) :
1637 * Clears the authentication cookie, logging the user out.
1640 * @deprecated Use wp_clear_auth_cookie()
1641 * @see wp_clear_auth_cookie()
1643 function wp_clearcookie() {
1644 _deprecated_function( __FUNCTION__, '2.5', 'wp_clear_auth_cookie()' );
1645 wp_clear_auth_cookie();
1649 if ( !function_exists('wp_get_cookie_login') ):
1651 * Gets the user cookie login.
1653 * This function is deprecated and should no longer be extended as it won't be
1654 * used anywhere in WordPress. Also, plugins shouldn't use it either.
1657 * @deprecated No alternative
1659 * @return bool Always returns false
1661 function wp_get_cookie_login() {
1662 _deprecated_function( __FUNCTION__, '2.5', '' );
1667 if ( !function_exists('wp_login') ) :
1669 * Checks a users login information and logs them in if it checks out.
1671 * Use the global $error to get the reason why the login failed. If the username
1672 * is blank, no error will be set, so assume blank username on that case.
1674 * Plugins extending this function should also provide the global $error and set
1675 * what the error is, so that those checking the global for why there was a
1676 * failure can utilize it later.
1679 * @deprecated Use wp_signon()
1680 * @global string $error Error when false is returned
1682 * @param string $username User's username
1683 * @param string $password User's password
1684 * @param bool $deprecated Not used
1685 * @return bool False on login failure, true on successful check
1687 function wp_login($username, $password, $deprecated = '') {
1690 $user = wp_authenticate($username, $password);
1692 if ( ! is_wp_error($user) )
1695 $error = $user->get_error_message();
1700 if ( !function_exists( 'wp_text_diff' ) ) :
1702 * Displays a human readable HTML representation of the difference between two strings.
1704 * The Diff is available for getting the changes between versions. The output is
1705 * HTML, so the primary use is for displaying the changes. If the two strings
1706 * are equivalent, then an empty string will be returned.
1708 * The arguments supported and can be changed are listed below.
1710 * 'title' : Default is an empty string. Titles the diff in a manner compatible
1712 * 'title_left' : Default is an empty string. Change the HTML to the left of the
1714 * 'title_right' : Default is an empty string. Change the HTML to the right of
1718 * @see wp_parse_args() Used to change defaults to user defined settings.
1720 * @uses WP_Text_Diff_Renderer_Table
1722 * @param string $left_string "old" (left) version of string
1723 * @param string $right_string "new" (right) version of string
1724 * @param string|array $args Optional. Change 'title', 'title_left', and 'title_right' defaults.
1725 * @return string Empty string if strings are equivalent or HTML with differences.
1727 function wp_text_diff( $left_string, $right_string, $args = null ) {
1728 $defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' );
1729 $args = wp_parse_args( $args, $defaults );
1731 if ( !class_exists( 'WP_Text_Diff_Renderer_Table' ) )
1732 require( ABSPATH . WPINC . '/wp-diff.php' );
1734 $left_string = normalize_whitespace($left_string);
1735 $right_string = normalize_whitespace($right_string);
1737 $left_lines = split("\n", $left_string);
1738 $right_lines = split("\n", $right_string);
1740 $text_diff = new Text_Diff($left_lines, $right_lines);
1741 $renderer = new WP_Text_Diff_Renderer_Table();
1742 $diff = $renderer->render($text_diff);
1747 $r = "<table class='diff'>\n";
1748 $r .= "<col class='ltype' /><col class='content' /><col class='ltype' /><col class='content' />";
1750 if ( $args['title'] || $args['title_left'] || $args['title_right'] )
1752 if ( $args['title'] )
1753 $r .= "<tr class='diff-title'><th colspan='4'>$args[title]</th></tr>\n";
1754 if ( $args['title_left'] || $args['title_right'] ) {
1755 $r .= "<tr class='diff-sub-title'>\n";
1756 $r .= "\t<td></td><th>$args[title_left]</th>\n";
1757 $r .= "\t<td></td><th>$args[title_right]</th>\n";
1760 if ( $args['title'] || $args['title_left'] || $args['title_right'] )
1763 $r .= "<tbody>\n$diff\n</tbody>\n";