web/wp-includes/pluggable.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
   527 
   527 
   528 	$expired = $expiration;
   528 	$expired = $expiration;
   529 
   529 
   530 	// Allow a grace period for POST and AJAX requests
   530 	// Allow a grace period for POST and AJAX requests
   531 	if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] )
   531 	if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] )
   532 		$expired += 3600;
   532 		$expired += HOUR_IN_SECONDS;
   533 
   533 
   534 	// Quick check to see if an honest cookie has expired
   534 	// Quick check to see if an honest cookie has expired
   535 	if ( $expired < time() ) {
   535 	if ( $expired < time() ) {
   536 		do_action('auth_cookie_expired', $cookie_elements);
   536 		do_action('auth_cookie_expired', $cookie_elements);
   537 		return false;
   537 		return false;
   692  * @since 2.5
   692  * @since 2.5
   693  */
   693  */
   694 function wp_clear_auth_cookie() {
   694 function wp_clear_auth_cookie() {
   695 	do_action('clear_auth_cookie');
   695 	do_action('clear_auth_cookie');
   696 
   696 
   697 	setcookie(AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
   697 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH,   COOKIE_DOMAIN );
   698 	setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
   698 	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH,   COOKIE_DOMAIN );
   699 	setcookie(AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
   699 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN );
   700 	setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
   700 	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN );
   701 	setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
   701 	setcookie( LOGGED_IN_COOKIE,   ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,          COOKIE_DOMAIN );
   702 	setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
   702 	setcookie( LOGGED_IN_COOKIE,   ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH,      COOKIE_DOMAIN );
   703 
   703 
   704 	// Old cookies
   704 	// Old cookies
   705 	setcookie(AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
   705 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
   706 	setcookie(AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
   706 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
   707 	setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
   707 	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
   708 	setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
   708 	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
   709 
   709 
   710 	// Even older cookies
   710 	// Even older cookies
   711 	setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
   711 	setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
   712 	setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
   712 	setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
   713 	setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
   713 	setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
   714 	setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
   714 	setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
   715 }
   715 }
   716 endif;
   716 endif;
   717 
   717 
   718 if ( !function_exists('is_user_logged_in') ) :
   718 if ( !function_exists('is_user_logged_in') ) :
   719 /**
   719 /**
   746 
   746 
   747 	$secure = apply_filters('secure_auth_redirect', $secure);
   747 	$secure = apply_filters('secure_auth_redirect', $secure);
   748 
   748 
   749 	// If https is required and request is http, redirect
   749 	// If https is required and request is http, redirect
   750 	if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
   750 	if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
   751 		if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
   751 		if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
   752 			wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']));
   752 			wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
   753 			exit();
   753 			exit();
   754 		} else {
   754 		} else {
   755 			wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
   755 			wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
   756 			exit();
   756 			exit();
   757 		}
   757 		}
   758 	}
   758 	}
   759 
   759 
   760 	if ( is_user_admin() )
   760 	if ( is_user_admin() )
   765 	if ( $user_id = wp_validate_auth_cookie( '',  $scheme) ) {
   765 	if ( $user_id = wp_validate_auth_cookie( '',  $scheme) ) {
   766 		do_action('auth_redirect', $user_id);
   766 		do_action('auth_redirect', $user_id);
   767 
   767 
   768 		// If the user wants ssl but the session is not ssl, redirect.
   768 		// If the user wants ssl but the session is not ssl, redirect.
   769 		if ( !$secure && get_user_option('use_ssl', $user_id) && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
   769 		if ( !$secure && get_user_option('use_ssl', $user_id) && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
   770 			if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
   770 			if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
   771 				wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']));
   771 				wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
   772 				exit();
   772 				exit();
   773 			} else {
   773 			} else {
   774 				wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
   774 				wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
   775 				exit();
   775 				exit();
   776 			}
   776 			}
   777 		}
   777 		}
   778 
   778 
   779 		return;  // The cookie is good so we're done
   779 		return;  // The cookie is good so we're done
   780 	}
   780 	}
   781 
   781 
   782 	// The cookie is no good so force login
   782 	// The cookie is no good so force login
   783 	nocache_headers();
   783 	nocache_headers();
   784 
   784 
   785 	if ( is_ssl() )
   785 	$redirect = ( strpos( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) ? wp_get_referer() : set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
   786 		$proto = 'https://';
       
   787 	else
       
   788 		$proto = 'http://';
       
   789 
       
   790 	$redirect = ( strpos($_SERVER['REQUEST_URI'], '/options.php') && wp_get_referer() ) ? wp_get_referer() : $proto . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
       
   791 
   786 
   792 	$login_url = wp_login_url($redirect, true);
   787 	$login_url = wp_login_url($redirect, true);
   793 
   788 
   794 	wp_redirect($login_url);
   789 	wp_redirect($login_url);
   795 	exit();
   790 	exit();
  1193  *
  1188  *
  1194  * @param int $user_id User ID
  1189  * @param int $user_id User ID
  1195  * @param string $plaintext_pass Optional. The user's plaintext password
  1190  * @param string $plaintext_pass Optional. The user's plaintext password
  1196  */
  1191  */
  1197 function wp_new_user_notification($user_id, $plaintext_pass = '') {
  1192 function wp_new_user_notification($user_id, $plaintext_pass = '') {
  1198 	$user = new WP_User($user_id);
  1193 	$user = get_userdata( $user_id );
  1199 
  1194 
  1200 	$user_login = stripslashes($user->user_login);
  1195 	$user_login = stripslashes($user->user_login);
  1201 	$user_email = stripslashes($user->user_email);
  1196 	$user_email = stripslashes($user->user_email);
  1202 
  1197 
  1203 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1198 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1232  * @since 2.5
  1227  * @since 2.5
  1233  *
  1228  *
  1234  * @return int
  1229  * @return int
  1235  */
  1230  */
  1236 function wp_nonce_tick() {
  1231 function wp_nonce_tick() {
  1237 	$nonce_life = apply_filters('nonce_life', 86400);
  1232 	$nonce_life = apply_filters( 'nonce_life', DAY_IN_SECONDS );
  1238 
  1233 
  1239 	return ceil(time() / ( $nonce_life / 2 ));
  1234 	return ceil(time() / ( $nonce_life / 2 ));
  1240 }
  1235 }
  1241 endif;
  1236 endif;
  1242 
  1237 
  1254  * @return bool Whether the nonce check passed or failed.
  1249  * @return bool Whether the nonce check passed or failed.
  1255  */
  1250  */
  1256 function wp_verify_nonce($nonce, $action = -1) {
  1251 function wp_verify_nonce($nonce, $action = -1) {
  1257 	$user = wp_get_current_user();
  1252 	$user = wp_get_current_user();
  1258 	$uid = (int) $user->ID;
  1253 	$uid = (int) $user->ID;
       
  1254 	if ( ! $uid )
       
  1255 		$uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
  1259 
  1256 
  1260 	$i = wp_nonce_tick();
  1257 	$i = wp_nonce_tick();
  1261 
  1258 
  1262 	// Nonce generated 0-12 hours ago
  1259 	// Nonce generated 0-12 hours ago
  1263 	if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) == $nonce )
  1260 	if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) == $nonce )
  1280  * @return string The one use form token
  1277  * @return string The one use form token
  1281  */
  1278  */
  1282 function wp_create_nonce($action = -1) {
  1279 function wp_create_nonce($action = -1) {
  1283 	$user = wp_get_current_user();
  1280 	$user = wp_get_current_user();
  1284 	$uid = (int) $user->ID;
  1281 	$uid = (int) $user->ID;
       
  1282 	if ( ! $uid )
       
  1283 		$uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
  1285 
  1284 
  1286 	$i = wp_nonce_tick();
  1285 	$i = wp_nonce_tick();
  1287 
  1286 
  1288 	return substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10);
  1287 	return substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10);
  1289 }
  1288 }
  1499 	return apply_filters('random_password', $password);
  1498 	return apply_filters('random_password', $password);
  1500 }
  1499 }
  1501 endif;
  1500 endif;
  1502 
  1501 
  1503 if ( !function_exists('wp_rand') ) :
  1502 if ( !function_exists('wp_rand') ) :
  1504  /**
  1503 /**
  1505  * Generates a random number
  1504  * Generates a random number
  1506  *
  1505  *
  1507  * @since 2.6.2
  1506  * @since 2.6.2
  1508  *
  1507  *
  1509  * @param int $min Lower limit for the generated number (optional, default is 0)
  1508  * @param int $min Lower limit for the generated number
  1510  * @param int $max Upper limit for the generated number (optional, default is 4294967295)
  1509  * @param int $max Upper limit for the generated number
  1511  * @return int A random number between min and max
  1510  * @return int A random number between min and max
  1512  */
  1511  */
  1513 function wp_rand( $min = 0, $max = 0 ) {
  1512 function wp_rand( $min = 0, $max = 0 ) {
  1514 	global $rnd_value;
  1513 	global $rnd_value;
  1515 
  1514 
  1534 	// Strip the first eight, leaving the remainder for the next call to wp_rand().
  1533 	// Strip the first eight, leaving the remainder for the next call to wp_rand().
  1535 	$rnd_value = substr($rnd_value, 8);
  1534 	$rnd_value = substr($rnd_value, 8);
  1536 
  1535 
  1537 	$value = abs(hexdec($value));
  1536 	$value = abs(hexdec($value));
  1538 
  1537 
       
  1538 	// Some misconfigured 32bit environments (Entropy PHP, for example) truncate integers larger than PHP_INT_MAX to PHP_INT_MAX rather than overflowing them to floats.
       
  1539 	$max_random_number = 3000000000 === 2147483647 ? (float) "4294967295" : 4294967295; // 4294967295 = 0xffffffff
       
  1540 
  1539 	// Reduce the value to be within the min - max range
  1541 	// Reduce the value to be within the min - max range
  1540 	// 4294967295 = 0xffffffff = max random number
       
  1541 	if ( $max != 0 )
  1542 	if ( $max != 0 )
  1542 		$value = $min + (($max - $min + 1) * ($value / (4294967295 + 1)));
  1543 		$value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 );
  1543 
  1544 
  1544 	return abs(intval($value));
  1545 	return abs(intval($value));
  1545 }
  1546 }
  1546 endif;
  1547 endif;
  1547 
  1548 
  1575  *
  1576  *
  1576  * @since 2.5
  1577  * @since 2.5
  1577  * @param int|string|object $id_or_email A user ID,  email address, or comment object
  1578  * @param int|string|object $id_or_email A user ID,  email address, or comment object
  1578  * @param int $size Size of the avatar image
  1579  * @param int $size Size of the avatar image
  1579  * @param string $default URL to a default image to use if no avatar is available
  1580  * @param string $default URL to a default image to use if no avatar is available
  1580  * @param string $alt Alternate text to use in image tag. Defaults to blank
  1581  * @param string $alt Alternative text to use in image tag. Defaults to blank
  1581  * @return string <img> tag for the user's avatar
  1582  * @return string <img> tag for the user's avatar
  1582 */
  1583 */
  1583 function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
  1584 function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
  1584 	if ( ! get_option('show_avatars') )
  1585 	if ( ! get_option('show_avatars') )
  1585 		return false;
  1586 		return false;
  1637 	}
  1638 	}
  1638 
  1639 
  1639 	if ( 'mystery' == $default )
  1640 	if ( 'mystery' == $default )
  1640 		$default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
  1641 		$default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
  1641 	elseif ( 'blank' == $default )
  1642 	elseif ( 'blank' == $default )
  1642 		$default = includes_url('images/blank.gif');
  1643 		$default = $email ? 'blank' : includes_url( 'images/blank.gif' );
  1643 	elseif ( !empty($email) && 'gravatar_default' == $default )
  1644 	elseif ( !empty($email) && 'gravatar_default' == $default )
  1644 		$default = '';
  1645 		$default = '';
  1645 	elseif ( 'gravatar_default' == $default )
  1646 	elseif ( 'gravatar_default' == $default )
  1646 		$default = "$host/avatar/?s={$size}";
  1647 		$default = "$host/avatar/?s={$size}";
  1647 	elseif ( empty($email) )
  1648 	elseif ( empty($email) )