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 ( is_admin() || 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 if ( $expiration < time() ) // AJAX/POST grace period set above
543 $GLOBALS['login_grace_period'] = 1;
545 do_action('auth_cookie_valid', $cookie_elements, $user);
551 if ( !function_exists('wp_generate_auth_cookie') ) :
553 * Generate authentication cookie contents.
556 * @uses apply_filters() Calls 'auth_cookie' hook on $cookie contents, User ID
557 * and expiration of cookie.
559 * @param int $user_id User ID
560 * @param int $expiration Cookie expiration in seconds
561 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
562 * @return string Authentication cookie contents
564 function wp_generate_auth_cookie($user_id, $expiration, $scheme = 'auth') {
565 $user = get_userdata($user_id);
567 $pass_frag = substr($user->user_pass, 8, 4);
569 $key = wp_hash($user->user_login . $pass_frag . '|' . $expiration, $scheme);
570 $hash = hash_hmac('md5', $user->user_login . '|' . $expiration, $key);
572 $cookie = $user->user_login . '|' . $expiration . '|' . $hash;
574 return apply_filters('auth_cookie', $cookie, $user_id, $expiration, $scheme);
578 if ( !function_exists('wp_parse_auth_cookie') ) :
580 * Parse a cookie into its components
584 * @param string $cookie
585 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
586 * @return array Authentication cookie components
588 function wp_parse_auth_cookie($cookie = '', $scheme = '') {
589 if ( empty($cookie) ) {
592 $cookie_name = AUTH_COOKIE;
595 $cookie_name = SECURE_AUTH_COOKIE;
598 $cookie_name = LOGGED_IN_COOKIE;
602 $cookie_name = SECURE_AUTH_COOKIE;
603 $scheme = 'secure_auth';
605 $cookie_name = AUTH_COOKIE;
610 if ( empty($_COOKIE[$cookie_name]) )
612 $cookie = $_COOKIE[$cookie_name];
615 $cookie_elements = explode('|', $cookie);
616 if ( count($cookie_elements) != 3 )
619 list($username, $expiration, $hmac) = $cookie_elements;
621 return compact('username', 'expiration', 'hmac', 'scheme');
625 if ( !function_exists('wp_set_auth_cookie') ) :
627 * Sets the authentication cookies based User ID.
629 * The $remember parameter increases the time that the cookie will be kept. The
630 * default the cookie is kept without remembering is two days. When $remember is
631 * set, the cookies will be kept for 14 days or two weeks.
635 * @param int $user_id User ID
636 * @param bool $remember Whether to remember the user or not
638 function wp_set_auth_cookie($user_id, $remember = false, $secure = '') {
640 $expiration = $expire = time() + apply_filters('auth_cookie_expiration', 1209600, $user_id, $remember);
642 $expiration = time() + apply_filters('auth_cookie_expiration', 172800, $user_id, $remember);
646 if ( '' === $secure )
647 $secure = is_ssl() ? true : false;
650 $auth_cookie_name = SECURE_AUTH_COOKIE;
651 $scheme = 'secure_auth';
653 $auth_cookie_name = AUTH_COOKIE;
657 $auth_cookie = wp_generate_auth_cookie($user_id, $expiration, $scheme);
658 $logged_in_cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
660 do_action('set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme);
661 do_action('set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in');
663 // Set httponly if the php version is >= 5.2.0
664 if ( version_compare(phpversion(), '5.2.0', 'ge') ) {
665 setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
666 setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
667 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, false, true);
668 if ( COOKIEPATH != SITECOOKIEPATH )
669 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, false, true);
671 $cookie_domain = COOKIE_DOMAIN;
672 if ( !empty($cookie_domain) )
673 $cookie_domain .= '; HttpOnly';
674 setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, $cookie_domain, $secure);
675 setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, $cookie_domain, $secure);
676 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, $cookie_domain);
677 if ( COOKIEPATH != SITECOOKIEPATH )
678 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, $cookie_domain);
683 if ( !function_exists('wp_clear_auth_cookie') ) :
685 * Removes all of the cookies associated with authentication.
689 function wp_clear_auth_cookie() {
690 do_action('clear_auth_cookie');
692 setcookie(AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
693 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
694 setcookie(AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
695 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
696 setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
697 setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
700 setcookie(AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
701 setcookie(AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
702 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
703 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
705 // Even older cookies
706 setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
707 setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
708 setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
709 setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
713 if ( !function_exists('is_user_logged_in') ) :
715 * Checks if the current visitor is a logged in user.
719 * @return bool True if user is logged in, false if not logged in.
721 function is_user_logged_in() {
722 $user = wp_get_current_user();
724 if ( $user->id == 0 )
731 if ( !function_exists('auth_redirect') ) :
733 * Checks if a user is logged in, if not it redirects them to the login page.
737 function auth_redirect() {
738 // Checks if a user is logged in, if not redirects them to the login page
740 if ( is_ssl() || force_ssl_admin() )
745 // If https is required and request is http, redirect
746 if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
747 if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
748 wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']));
751 wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
756 if ( $user_id = wp_validate_auth_cookie( '', apply_filters( 'auth_redirect_scheme', '' ) ) ) {
757 do_action('auth_redirect', $user_id);
759 // If the user wants ssl but the session is not ssl, redirect.
760 if ( !$secure && get_user_option('use_ssl', $user_id) && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
761 if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
762 wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']));
765 wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
770 return; // The cookie is good so we're done
773 // The cookie is no good so force login
781 $redirect = ( strpos($_SERVER['REQUEST_URI'], '/options.php') && wp_get_referer() ) ? wp_get_referer() : $proto . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
783 $login_url = wp_login_url($redirect);
785 wp_redirect($login_url);
790 if ( !function_exists('check_admin_referer') ) :
792 * Makes sure that a user was referred from another admin page.
794 * To avoid security exploits.
797 * @uses do_action() Calls 'check_admin_referer' on $action.
799 * @param string $action Action nonce
800 * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
802 function check_admin_referer($action = -1, $query_arg = '_wpnonce') {
803 $adminurl = strtolower(admin_url());
804 $referer = strtolower(wp_get_referer());
805 $result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce($_REQUEST[$query_arg], $action) : false;
806 if ( !$result && !(-1 == $action && strpos($referer, $adminurl) !== false) ) {
807 wp_nonce_ays($action);
810 do_action('check_admin_referer', $action, $result);
814 if ( !function_exists('check_ajax_referer') ) :
816 * Verifies the AJAX request to prevent processing requests external of the blog.
820 * @param string $action Action nonce
821 * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
823 function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
825 $nonce = $_REQUEST[$query_arg];
827 $nonce = isset($_REQUEST['_ajax_nonce']) ? $_REQUEST['_ajax_nonce'] : $_REQUEST['_wpnonce'];
829 $result = wp_verify_nonce( $nonce, $action );
831 if ( $die && false == $result )
834 do_action('check_ajax_referer', $action, $result);
840 if ( !function_exists('wp_redirect') ) :
842 * Redirects to another page, with a workaround for the IIS Set-Cookie bug.
844 * @link http://support.microsoft.com/kb/q176113/
846 * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status.
848 * @param string $location The path to redirect to
849 * @param int $status Status code to use
850 * @return bool False if $location is not set
852 function wp_redirect($location, $status = 302) {
855 $location = apply_filters('wp_redirect', $location, $status);
856 $status = apply_filters('wp_redirect_status', $status, $location);
858 if ( !$location ) // allows the wp_redirect filter to cancel a redirect
861 $location = wp_sanitize_redirect($location);
864 header("Refresh: 0;url=$location");
866 if ( php_sapi_name() != 'cgi-fcgi' )
867 status_header($status); // This causes problems on IIS and some FastCGI setups
868 header("Location: $location", true, $status);
873 if ( !function_exists('wp_sanitize_redirect') ) :
875 * Sanitizes a URL for use in a redirect.
879 * @return string redirect-sanitized URL
881 function wp_sanitize_redirect($location) {
882 $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%!]|i', '', $location);
883 $location = wp_kses_no_null($location);
885 // remove %0d and %0a from location
886 $strip = array('%0d', '%0a', '%0D', '%0A');
887 $location = _deep_replace($strip, $location);
892 if ( !function_exists('wp_safe_redirect') ) :
894 * Performs a safe (local) redirect, using wp_redirect().
896 * Checks whether the $location is using an allowed host, if it has an absolute
897 * path. A plugin can therefore set or remove allowed host(s) to or from the
900 * If the host is not allowed, then the redirect is to wp-admin on the siteurl
901 * instead. This prevents malicious redirects which redirect to another host,
902 * but only used in a few places.
905 * @uses wp_validate_redirect() To validate the redirect is to an allowed host.
907 * @return void Does not return anything
909 function wp_safe_redirect($location, $status = 302) {
911 // Need to look at the URL the way it will end up in wp_redirect()
912 $location = wp_sanitize_redirect($location);
914 $location = wp_validate_redirect($location, admin_url());
916 wp_redirect($location, $status);
920 if ( !function_exists('wp_validate_redirect') ) :
922 * Validates a URL for use in a redirect.
924 * Checks whether the $location is using an allowed host, if it has an absolute
925 * path. A plugin can therefore set or remove allowed host(s) to or from the
928 * If the host is not allowed, then the redirect is to $default supplied
931 * @uses apply_filters() Calls 'allowed_redirect_hosts' on an array containing
932 * WordPress host string and $location host string.
934 * @param string $location The redirect to validate
935 * @param string $default The value to return is $location is not allowed
936 * @return string redirect-sanitized URL
938 function wp_validate_redirect($location, $default = '') {
939 // browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'
940 if ( substr($location, 0, 2) == '//' )
941 $location = 'http:' . $location;
943 // In php 5 parse_url may fail if the URL query part contains http://, bug #38143
944 $test = ( $cut = strpos($location, '?') ) ? substr( $location, 0, $cut ) : $location;
946 $lp = parse_url($test);
947 $wpp = parse_url(get_option('home'));
949 $allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']), isset($lp['host']) ? $lp['host'] : '');
951 if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
952 $location = $default;
958 if ( ! function_exists('wp_notify_postauthor') ) :
960 * Notify an author of a comment/trackback/pingback to one of their posts.
964 * @param int $comment_id Comment ID
965 * @param string $comment_type Optional. The comment type either 'comment' (default), 'trackback', or 'pingback'
966 * @return bool False if user email does not exist. True on completion.
968 function wp_notify_postauthor($comment_id, $comment_type='') {
969 $comment = get_comment($comment_id);
970 $post = get_post($comment->comment_post_ID);
971 $user = get_userdata( $post->post_author );
972 $current_user = wp_get_current_user();
974 if ( $comment->user_id == $post->post_author ) return false; // The author moderated a comment on his own post
976 if ('' == $user->user_email) return false; // If there's no email to send the comment to
978 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
980 // The blogname option is escaped with esc_html on the way into the database in sanitize_option
981 // we want to reverse this for the plain text arena of emails.
982 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
984 if ( empty( $comment_type ) ) $comment_type = 'comment';
986 if ('comment' == $comment_type) {
987 /* translators: 1: post id, 2: post title */
988 $notify_message = sprintf( __('New comment on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
989 /* translators: 1: comment author, 2: author IP, 3: author domain */
990 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
991 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
992 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
993 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
994 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
995 $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
996 /* translators: 1: blog name, 2: post title */
997 $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
998 } elseif ('trackback' == $comment_type) {
999 /* translators: 1: post id, 2: post title */
1000 $notify_message = sprintf( __('New trackback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
1001 /* translators: 1: website name, 2: author IP, 3: author domain */
1002 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1003 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1004 $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1005 $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
1006 /* translators: 1: blog name, 2: post title */
1007 $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
1008 } elseif ('pingback' == $comment_type) {
1009 /* translators: 1: post id, 2: post title */
1010 $notify_message = sprintf( __('New pingback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
1011 /* translators: 1: comment author, 2: author IP, 3: author domain */
1012 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1013 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1014 $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
1015 $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
1016 /* translators: 1: blog name, 2: post title */
1017 $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
1019 $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
1020 if ( EMPTY_TRASH_DAYS )
1021 $notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
1023 $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n";
1024 $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n";
1026 $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
1028 if ( '' == $comment->comment_author ) {
1029 $from = "From: \"$blogname\" <$wp_email>";
1030 if ( '' != $comment->comment_author_email )
1031 $reply_to = "Reply-To: $comment->comment_author_email";
1033 $from = "From: \"$comment->comment_author\" <$wp_email>";
1034 if ( '' != $comment->comment_author_email )
1035 $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
1038 $message_headers = "$from\n"
1039 . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
1041 if ( isset($reply_to) )
1042 $message_headers .= $reply_to . "\n";
1044 $notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id);
1045 $subject = apply_filters('comment_notification_subject', $subject, $comment_id);
1046 $message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id);
1048 @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
1054 if ( !function_exists('wp_notify_moderator') ) :
1056 * Notifies the moderator of the blog about a new comment that is awaiting approval.
1061 * @param int $comment_id Comment ID
1062 * @return bool Always returns true
1064 function wp_notify_moderator($comment_id) {
1067 if( get_option( "moderation_notify" ) == 0 )
1070 $comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID=%d LIMIT 1", $comment_id));
1071 $post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID=%d LIMIT 1", $comment->comment_post_ID));
1073 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
1074 $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
1076 // The blogname option is escaped with esc_html on the way into the database in sanitize_option
1077 // we want to reverse this for the plain text arena of emails.
1078 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
1080 switch ($comment->comment_type)
1083 $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";
1084 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
1085 $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1086 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1087 $notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1090 $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";
1091 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
1092 $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1093 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1094 $notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1097 $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";
1098 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
1099 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1100 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
1101 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1102 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
1103 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1107 $notify_message .= sprintf( __('Approve it: %s'), admin_url("comment.php?action=approve&c=$comment_id") ) . "\r\n";
1108 if ( EMPTY_TRASH_DAYS )
1109 $notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
1111 $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n";
1112 $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n";
1114 $notify_message .= sprintf( _n('Currently %s comment is waiting for approval. Please visit the moderation panel:',
1115 'Currently %s comments are waiting for approval. Please visit the moderation panel:', $comments_waiting), number_format_i18n($comments_waiting) ) . "\r\n";
1116 $notify_message .= admin_url("edit-comments.php?comment_status=moderated") . "\r\n";
1118 $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), $blogname, $post->post_title );
1119 $admin_email = get_option('admin_email');
1120 $message_headers = '';
1122 $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id);
1123 $subject = apply_filters('comment_moderation_subject', $subject, $comment_id);
1124 $message_headers = apply_filters('comment_moderation_headers', $message_headers);
1126 @wp_mail($admin_email, $subject, $notify_message, $message_headers);
1132 if ( !function_exists('wp_password_change_notification') ) :
1134 * Notify the blog admin of a user changing password, normally via email.
1138 * @param object $user User Object
1140 function wp_password_change_notification(&$user) {
1141 // send a copy of password change notification to the admin
1142 // but check to see if it's the admin whose password we're changing, and skip this
1143 if ( $user->user_email != get_option('admin_email') ) {
1144 $message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n";
1145 // The blogname option is escaped with esc_html on the way into the database in sanitize_option
1146 // we want to reverse this for the plain text arena of emails.
1147 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
1148 wp_mail(get_option('admin_email'), sprintf(__('[%s] Password Lost/Changed'), $blogname), $message);
1153 if ( !function_exists('wp_new_user_notification') ) :
1155 * Notify the blog admin of a new user, normally via email.
1159 * @param int $user_id User ID
1160 * @param string $plaintext_pass Optional. The user's plaintext password
1162 function wp_new_user_notification($user_id, $plaintext_pass = '') {
1163 $user = new WP_User($user_id);
1165 $user_login = stripslashes($user->user_login);
1166 $user_email = stripslashes($user->user_email);
1168 // The blogname option is escaped with esc_html on the way into the database in sanitize_option
1169 // we want to reverse this for the plain text arena of emails.
1170 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
1172 $message = sprintf(__('New user registration on your blog %s:'), $blogname) . "\r\n\r\n";
1173 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
1174 $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
1176 @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);
1178 if ( empty($plaintext_pass) )
1181 $message = sprintf(__('Username: %s'), $user_login) . "\r\n";
1182 $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
1183 $message .= wp_login_url() . "\r\n";
1185 wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
1190 if ( !function_exists('wp_nonce_tick') ) :
1192 * Get the time-dependent variable for nonce creation.
1194 * A nonce has a lifespan of two ticks. Nonces in their second tick may be
1195 * updated, e.g. by autosave.
1201 function wp_nonce_tick() {
1202 $nonce_life = apply_filters('nonce_life', 86400);
1204 return ceil(time() / ( $nonce_life / 2 ));
1208 if ( !function_exists('wp_verify_nonce') ) :
1210 * Verify that correct nonce was used with time limit.
1212 * The user is given an amount of time to use the token, so therefore, since the
1213 * UID and $action remain the same, the independent variable is the time.
1217 * @param string $nonce Nonce that was used in the form to verify
1218 * @param string|int $action Should give context to what is taking place and be the same when nonce was created.
1219 * @return bool Whether the nonce check passed or failed.
1221 function wp_verify_nonce($nonce, $action = -1) {
1222 $user = wp_get_current_user();
1223 $uid = (int) $user->id;
1225 $i = wp_nonce_tick();
1227 // Nonce generated 0-12 hours ago
1228 if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) == $nonce )
1230 // Nonce generated 12-24 hours ago
1231 if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) == $nonce )
1238 if ( !function_exists('wp_create_nonce') ) :
1240 * Creates a random, one time use token.
1244 * @param string|int $action Scalar value to add context to the nonce.
1245 * @return string The one use form token
1247 function wp_create_nonce($action = -1) {
1248 $user = wp_get_current_user();
1249 $uid = (int) $user->id;
1251 $i = wp_nonce_tick();
1253 return substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10);
1257 if ( !function_exists('wp_salt') ) :
1259 * Get salt to add to hashes to help prevent attacks.
1261 * The secret key is located in two places: the database in case the secret key
1262 * isn't defined in the second place, which is in the wp-config.php file. If you
1263 * are going to set the secret key, then you must do so in the wp-config.php
1266 * The secret key in the database is randomly generated and will be appended to
1267 * the secret key that is in wp-config.php file in some instances. It is
1268 * important to have the secret key defined or changed in wp-config.php.
1270 * If you have installed WordPress 2.5 or later, then you will have the
1271 * SECRET_KEY defined in the wp-config.php already. You will want to change the
1272 * value in it because hackers will know what it is. If you have upgraded to
1273 * WordPress 2.5 or later version from a version before WordPress 2.5, then you
1274 * should add the constant to your wp-config.php file.
1276 * Below is an example of how the SECRET_KEY constant is defined with a value.
1277 * You must not copy the below example and paste into your wp-config.php. If you
1278 * need an example, then you can have a
1279 * {@link https://api.wordpress.org/secret-key/1.1/ secret key created} for you.
1282 * define('SECRET_KEY', 'mAry1HadA15|\/|b17w55w1t3asSn09w');
1285 * Salting passwords helps against tools which has stored hashed values of
1286 * common dictionary strings. The added values makes it harder to crack if given
1287 * salt string is not weak.
1290 * @link https://api.wordpress.org/secret-key/1.1/ Create a Secret Key for wp-config.php
1292 * @return string Salt value from either 'SECRET_KEY' or 'secret' option
1294 function wp_salt($scheme = 'auth') {
1295 global $wp_default_secret_key;
1297 if ( defined('SECRET_KEY') && ('' != SECRET_KEY) && ( $wp_default_secret_key != SECRET_KEY) )
1298 $secret_key = SECRET_KEY;
1300 if ( 'auth' == $scheme ) {
1301 if ( defined('AUTH_KEY') && ('' != AUTH_KEY) && ( $wp_default_secret_key != AUTH_KEY) )
1302 $secret_key = AUTH_KEY;
1304 if ( defined('AUTH_SALT') ) {
1306 } elseif ( defined('SECRET_SALT') ) {
1307 $salt = SECRET_SALT;
1309 $salt = get_option('auth_salt');
1310 if ( empty($salt) ) {
1311 $salt = wp_generate_password(64);
1312 update_option('auth_salt', $salt);
1315 } elseif ( 'secure_auth' == $scheme ) {
1316 if ( defined('SECURE_AUTH_KEY') && ('' != SECURE_AUTH_KEY) && ( $wp_default_secret_key != SECURE_AUTH_KEY) )
1317 $secret_key = SECURE_AUTH_KEY;
1319 if ( defined('SECURE_AUTH_SALT') ) {
1320 $salt = SECURE_AUTH_SALT;
1322 $salt = get_option('secure_auth_salt');
1323 if ( empty($salt) ) {
1324 $salt = wp_generate_password(64);
1325 update_option('secure_auth_salt', $salt);
1328 } elseif ( 'logged_in' == $scheme ) {
1329 if ( defined('LOGGED_IN_KEY') && ('' != LOGGED_IN_KEY) && ( $wp_default_secret_key != LOGGED_IN_KEY) )
1330 $secret_key = LOGGED_IN_KEY;
1332 if ( defined('LOGGED_IN_SALT') ) {
1333 $salt = LOGGED_IN_SALT;
1335 $salt = get_option('logged_in_salt');
1336 if ( empty($salt) ) {
1337 $salt = wp_generate_password(64);
1338 update_option('logged_in_salt', $salt);
1341 } elseif ( 'nonce' == $scheme ) {
1342 if ( defined('NONCE_KEY') && ('' != NONCE_KEY) && ( $wp_default_secret_key != NONCE_KEY) )
1343 $secret_key = NONCE_KEY;
1345 if ( defined('NONCE_SALT') ) {
1348 $salt = get_option('nonce_salt');
1349 if ( empty($salt) ) {
1350 $salt = wp_generate_password(64);
1351 update_option('nonce_salt', $salt);
1355 // ensure each auth scheme has its own unique salt
1356 $salt = hash_hmac('md5', $scheme, $secret_key);
1359 return apply_filters('salt', $secret_key . $salt, $scheme);
1363 if ( !function_exists('wp_hash') ) :
1365 * Get hash of given string.
1368 * @uses wp_salt() Get WordPress salt
1370 * @param string $data Plain text to hash
1371 * @return string Hash of $data
1373 function wp_hash($data, $scheme = 'auth') {
1374 $salt = wp_salt($scheme);
1376 return hash_hmac('md5', $data, $salt);
1380 if ( !function_exists('wp_hash_password') ) :
1382 * Create a hash (encrypt) of a plain text password.
1384 * For integration with other applications, this function can be overwritten to
1385 * instead use the other package password checking algorithm.
1388 * @global object $wp_hasher PHPass object
1389 * @uses PasswordHash::HashPassword
1391 * @param string $password Plain text user password to hash
1392 * @return string The hash string of the password
1394 function wp_hash_password($password) {
1397 if ( empty($wp_hasher) ) {
1398 require_once( ABSPATH . 'wp-includes/class-phpass.php');
1399 // By default, use the portable hash from phpass
1400 $wp_hasher = new PasswordHash(8, TRUE);
1403 return $wp_hasher->HashPassword($password);
1407 if ( !function_exists('wp_check_password') ) :
1409 * Checks the plaintext password against the encrypted Password.
1411 * Maintains compatibility between old version and the new cookie authentication
1412 * protocol using PHPass library. The $hash parameter is the encrypted password
1413 * and the function compares the plain text password when encypted similarly
1414 * against the already encrypted password to see if they match.
1416 * For integration with other applications, this function can be overwritten to
1417 * instead use the other package password checking algorithm.
1420 * @global object $wp_hasher PHPass object used for checking the password
1421 * against the $hash + $password
1422 * @uses PasswordHash::CheckPassword
1424 * @param string $password Plaintext user's password
1425 * @param string $hash Hash of the user's password to check against.
1426 * @return bool False, if the $password does not match the hashed password
1428 function wp_check_password($password, $hash, $user_id = '') {
1431 // If the hash is still md5...
1432 if ( strlen($hash) <= 32 ) {
1433 $check = ( $hash == md5($password) );
1434 if ( $check && $user_id ) {
1435 // Rehash using new hash.
1436 wp_set_password($password, $user_id);
1437 $hash = wp_hash_password($password);
1440 return apply_filters('check_password', $check, $password, $hash, $user_id);
1443 // If the stored hash is longer than an MD5, presume the
1444 // new style phpass portable hash.
1445 if ( empty($wp_hasher) ) {
1446 require_once( ABSPATH . 'wp-includes/class-phpass.php');
1447 // By default, use the portable hash from phpass
1448 $wp_hasher = new PasswordHash(8, TRUE);
1451 $check = $wp_hasher->CheckPassword($password, $hash);
1453 return apply_filters('check_password', $check, $password, $hash, $user_id);
1457 if ( !function_exists('wp_generate_password') ) :
1459 * Generates a random password drawn from the defined set of characters.
1463 * @param int $length The length of password to generate
1464 * @param bool $special_chars Whether to include standard special characters
1465 * @return string The random password
1467 function wp_generate_password($length = 12, $special_chars = true) {
1468 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
1469 if ( $special_chars )
1470 $chars .= '!@#$%^&*()';
1473 for ( $i = 0; $i < $length; $i++ )
1474 $password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
1479 if ( !function_exists('wp_rand') ) :
1481 * Generates a random number
1485 * @param int $min Lower limit for the generated number (optional, default is 0)
1486 * @param int $max Upper limit for the generated number (optional, default is 4294967295)
1487 * @return int A random number between min and max
1489 function wp_rand( $min = 0, $max = 0 ) {
1492 $seed = get_transient('random_seed');
1494 // Reset $rnd_value after 14 uses
1495 // 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value
1496 if ( strlen($rnd_value) < 8 ) {
1497 $rnd_value = md5( uniqid(microtime() . mt_rand(), true ) . $seed );
1498 $rnd_value .= sha1($rnd_value);
1499 $rnd_value .= sha1($rnd_value . $seed);
1500 $seed = md5($seed . $rnd_value);
1501 set_transient('random_seed', $seed);
1504 // Take the first 8 digits for our value
1505 $value = substr($rnd_value, 0, 8);
1507 // Strip the first eight, leaving the remainder for the next call to wp_rand().
1508 $rnd_value = substr($rnd_value, 8);
1510 $value = abs(hexdec($value));
1512 // Reduce the value to be within the min - max range
1513 // 4294967295 = 0xffffffff = max random number
1515 $value = $min + (($max - $min + 1) * ($value / (4294967295 + 1)));
1517 return abs(intval($value));
1521 if ( !function_exists('wp_set_password') ) :
1523 * Updates the user's password with a new encrypted one.
1525 * For integration with other applications, this function can be overwritten to
1526 * instead use the other package password checking algorithm.
1529 * @uses $wpdb WordPress database object for queries
1530 * @uses wp_hash_password() Used to encrypt the user's password before passing to the database
1532 * @param string $password The plaintext new user password
1533 * @param int $user_id User ID
1535 function wp_set_password( $password, $user_id ) {
1538 $hash = wp_hash_password($password);
1539 $wpdb->update($wpdb->users, array('user_pass' => $hash, 'user_activation_key' => ''), array('ID' => $user_id) );
1541 wp_cache_delete($user_id, 'users');
1545 if ( !function_exists( 'get_avatar' ) ) :
1547 * Retrieve the avatar for a user who provided a user ID or email address.
1550 * @param int|string|object $id_or_email A user ID, email address, or comment object
1551 * @param int $size Size of the avatar image
1552 * @param string $default URL to a default image to use if no avatar is available
1553 * @param string $alt Alternate text to use in image tag. Defaults to blank
1554 * @return string <img> tag for the user's avatar
1556 function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
1557 if ( ! get_option('show_avatars') )
1560 if ( false === $alt)
1563 $safe_alt = esc_attr( $alt );
1565 if ( !is_numeric($size) )
1569 if ( is_numeric($id_or_email) ) {
1570 $id = (int) $id_or_email;
1571 $user = get_userdata($id);
1573 $email = $user->user_email;
1574 } elseif ( is_object($id_or_email) ) {
1575 if ( isset($id_or_email->comment_type) && '' != $id_or_email->comment_type && 'comment' != $id_or_email->comment_type )
1576 return false; // No avatar for pingbacks or trackbacks
1578 if ( !empty($id_or_email->user_id) ) {
1579 $id = (int) $id_or_email->user_id;
1580 $user = get_userdata($id);
1582 $email = $user->user_email;
1583 } elseif ( !empty($id_or_email->comment_author_email) ) {
1584 $email = $id_or_email->comment_author_email;
1587 $email = $id_or_email;
1590 if ( empty($default) ) {
1591 $avatar_default = get_option('avatar_default');
1592 if ( empty($avatar_default) )
1593 $default = 'mystery';
1595 $default = $avatar_default;
1599 $host = 'https://secure.gravatar.com';
1601 $host = 'http://www.gravatar.com';
1603 if ( 'mystery' == $default )
1604 $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
1605 elseif ( 'blank' == $default )
1606 $default = includes_url('images/blank.gif');
1607 elseif ( !empty($email) && 'gravatar_default' == $default )
1609 elseif ( 'gravatar_default' == $default )
1610 $default = "$host/avatar/s={$size}";
1611 elseif ( empty($email) )
1612 $default = "$host/avatar/?d=$default&s={$size}";
1613 elseif ( strpos($default, 'http://') === 0 )
1614 $default = add_query_arg( 's', $size, $default );
1616 if ( !empty($email) ) {
1617 $out = "$host/avatar/";
1618 $out .= md5( strtolower( $email ) );
1619 $out .= '?s='.$size;
1620 $out .= '&d=' . urlencode( $default );
1622 $rating = get_option('avatar_rating');
1623 if ( !empty( $rating ) )
1624 $out .= "&r={$rating}";
1626 $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
1628 $avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
1631 return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
1635 if ( !function_exists('wp_setcookie') ) :
1637 * Sets a cookie for a user who just logged in.
1640 * @deprecated Use wp_set_auth_cookie()
1641 * @see wp_set_auth_cookie()
1643 * @param string $username The user's username
1644 * @param string $password Optional. The user's password
1645 * @param bool $already_md5 Optional. Whether the password has already been through MD5
1646 * @param string $home Optional. Will be used instead of COOKIEPATH if set
1647 * @param string $siteurl Optional. Will be used instead of SITECOOKIEPATH if set
1648 * @param bool $remember Optional. Remember that the user is logged in
1650 function wp_setcookie($username, $password = '', $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
1651 _deprecated_function( __FUNCTION__, '2.5', 'wp_set_auth_cookie()' );
1652 $user = get_userdatabylogin($username);
1653 wp_set_auth_cookie($user->ID, $remember);
1657 if ( !function_exists('wp_clearcookie') ) :
1659 * Clears the authentication cookie, logging the user out.
1662 * @deprecated Use wp_clear_auth_cookie()
1663 * @see wp_clear_auth_cookie()
1665 function wp_clearcookie() {
1666 _deprecated_function( __FUNCTION__, '2.5', 'wp_clear_auth_cookie()' );
1667 wp_clear_auth_cookie();
1671 if ( !function_exists('wp_get_cookie_login') ):
1673 * Gets the user cookie login.
1675 * This function is deprecated and should no longer be extended as it won't be
1676 * used anywhere in WordPress. Also, plugins shouldn't use it either.
1679 * @deprecated No alternative
1681 * @return bool Always returns false
1683 function wp_get_cookie_login() {
1684 _deprecated_function( __FUNCTION__, '2.5', '' );
1689 if ( !function_exists('wp_login') ) :
1691 * Checks a users login information and logs them in if it checks out.
1693 * Use the global $error to get the reason why the login failed. If the username
1694 * is blank, no error will be set, so assume blank username on that case.
1696 * Plugins extending this function should also provide the global $error and set
1697 * what the error is, so that those checking the global for why there was a
1698 * failure can utilize it later.
1701 * @deprecated Use wp_signon()
1702 * @global string $error Error when false is returned
1704 * @param string $username User's username
1705 * @param string $password User's password
1706 * @param bool $deprecated Not used
1707 * @return bool False on login failure, true on successful check
1709 function wp_login($username, $password, $deprecated = '') {
1712 $user = wp_authenticate($username, $password);
1714 if ( ! is_wp_error($user) )
1717 $error = $user->get_error_message();
1722 if ( !function_exists( 'wp_text_diff' ) ) :
1724 * Displays a human readable HTML representation of the difference between two strings.
1726 * The Diff is available for getting the changes between versions. The output is
1727 * HTML, so the primary use is for displaying the changes. If the two strings
1728 * are equivalent, then an empty string will be returned.
1730 * The arguments supported and can be changed are listed below.
1732 * 'title' : Default is an empty string. Titles the diff in a manner compatible
1734 * 'title_left' : Default is an empty string. Change the HTML to the left of the
1736 * 'title_right' : Default is an empty string. Change the HTML to the right of
1740 * @see wp_parse_args() Used to change defaults to user defined settings.
1742 * @uses WP_Text_Diff_Renderer_Table
1744 * @param string $left_string "old" (left) version of string
1745 * @param string $right_string "new" (right) version of string
1746 * @param string|array $args Optional. Change 'title', 'title_left', and 'title_right' defaults.
1747 * @return string Empty string if strings are equivalent or HTML with differences.
1749 function wp_text_diff( $left_string, $right_string, $args = null ) {
1750 $defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' );
1751 $args = wp_parse_args( $args, $defaults );
1753 if ( !class_exists( 'WP_Text_Diff_Renderer_Table' ) )
1754 require( ABSPATH . WPINC . '/wp-diff.php' );
1756 $left_string = normalize_whitespace($left_string);
1757 $right_string = normalize_whitespace($right_string);
1759 $left_lines = split("\n", $left_string);
1760 $right_lines = split("\n", $right_string);
1762 $text_diff = new Text_Diff($left_lines, $right_lines);
1763 $renderer = new WP_Text_Diff_Renderer_Table();
1764 $diff = $renderer->render($text_diff);
1769 $r = "<table class='diff'>\n";
1770 $r .= "<col class='ltype' /><col class='content' /><col class='ltype' /><col class='content' />";
1772 if ( $args['title'] || $args['title_left'] || $args['title_right'] )
1774 if ( $args['title'] )
1775 $r .= "<tr class='diff-title'><th colspan='4'>$args[title]</th></tr>\n";
1776 if ( $args['title_left'] || $args['title_right'] ) {
1777 $r .= "<tr class='diff-sub-title'>\n";
1778 $r .= "\t<td></td><th>$args[title_left]</th>\n";
1779 $r .= "\t<td></td><th>$args[title_right]</th>\n";
1782 if ( $args['title'] || $args['title_left'] || $args['title_right'] )
1785 $r .= "<tbody>\n$diff\n</tbody>\n";