web/wp-includes/pluggable.php
changeset 136 bde1974c263b
child 194 32102edaa81b
equal deleted inserted replaced
135:53cff4b4a802 136:bde1974c263b
       
     1 <?php
       
     2 /**
       
     3  * These functions can be replaced via plugins. If plugins do not redefine these
       
     4  * functions, then these will be used instead.
       
     5  *
       
     6  * @package WordPress
       
     7  */
       
     8 
       
     9 if ( !function_exists('set_current_user') ) :
       
    10 /**
       
    11  * Changes the current user by ID or name.
       
    12  *
       
    13  * Set $id to null and specify a name if you do not know a user's ID.
       
    14  *
       
    15  * @since 2.0.1
       
    16  * @see wp_set_current_user() An alias of wp_set_current_user()
       
    17  *
       
    18  * @param int|null $id User ID.
       
    19  * @param string $name Optional. The user's username
       
    20  * @return object returns wp_set_current_user()
       
    21  */
       
    22 function set_current_user($id, $name = '') {
       
    23 	return wp_set_current_user($id, $name);
       
    24 }
       
    25 endif;
       
    26 
       
    27 if ( !function_exists('wp_set_current_user') ) :
       
    28 /**
       
    29  * Changes the current user by ID or name.
       
    30  *
       
    31  * Set $id to null and specify a name if you do not know a user's ID.
       
    32  *
       
    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.
       
    36  *
       
    37  * @since 2.0.3
       
    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.
       
    40  *
       
    41  * @param int $id User ID
       
    42  * @param string $name User's username
       
    43  * @return WP_User Current user User object
       
    44  */
       
    45 function wp_set_current_user($id, $name = '') {
       
    46 	global $current_user;
       
    47 
       
    48 	if ( isset($current_user) && ($id == $current_user->ID) )
       
    49 		return $current_user;
       
    50 
       
    51 	$current_user = new WP_User($id, $name);
       
    52 
       
    53 	setup_userdata($current_user->ID);
       
    54 
       
    55 	do_action('set_current_user');
       
    56 
       
    57 	return $current_user;
       
    58 }
       
    59 endif;
       
    60 
       
    61 if ( !function_exists('wp_get_current_user') ) :
       
    62 /**
       
    63  * Retrieve the current user object.
       
    64  *
       
    65  * @since 2.0.3
       
    66  *
       
    67  * @return WP_User Current user WP_User object
       
    68  */
       
    69 function wp_get_current_user() {
       
    70 	global $current_user;
       
    71 
       
    72 	get_currentuserinfo();
       
    73 
       
    74 	return $current_user;
       
    75 }
       
    76 endif;
       
    77 
       
    78 if ( !function_exists('get_currentuserinfo') ) :
       
    79 /**
       
    80  * Populate global variables with information about the currently logged in user.
       
    81  *
       
    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.
       
    85  *
       
    86  * @since 0.71
       
    87  * @uses $current_user Checks if the current user is set
       
    88  * @uses wp_validate_auth_cookie() Retrieves current logged in user.
       
    89  *
       
    90  * @return bool|null False on XMLRPC Request and invalid auth cookie. Null when current user set
       
    91  */
       
    92 function get_currentuserinfo() {
       
    93 	global $current_user;
       
    94 
       
    95 	if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
       
    96 		return false;
       
    97 
       
    98 	if ( ! empty($current_user) )
       
    99 		return;
       
   100 
       
   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);
       
   104 		 	return false;
       
   105 		 }
       
   106 	}
       
   107 
       
   108 	wp_set_current_user($user);
       
   109 }
       
   110 endif;
       
   111 
       
   112 if ( !function_exists('get_userdata') ) :
       
   113 /**
       
   114  * Retrieve user info by user ID.
       
   115  *
       
   116  * @since 0.71
       
   117  *
       
   118  * @param int $user_id User ID
       
   119  * @return bool|object False on failure, User DB row object
       
   120  */
       
   121 function get_userdata( $user_id ) {
       
   122 	global $wpdb;
       
   123 
       
   124 	$user_id = absint($user_id);
       
   125 	if ( $user_id == 0 )
       
   126 		return false;
       
   127 
       
   128 	$user = wp_cache_get($user_id, 'users');
       
   129 
       
   130 	if ( $user )
       
   131 		return $user;
       
   132 
       
   133 	if ( !$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE ID = %d LIMIT 1", $user_id)) )
       
   134 		return false;
       
   135 
       
   136 	_fill_user($user);
       
   137 
       
   138 	return $user;
       
   139 }
       
   140 endif;
       
   141 
       
   142 if ( !function_exists('get_user_by') ) :
       
   143 /**
       
   144  * Retrieve user info by a given field
       
   145  *
       
   146  * @since 2.8.0
       
   147  *
       
   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
       
   151  */
       
   152 function get_user_by($field, $value) {
       
   153 	global $wpdb;
       
   154 
       
   155 	switch ($field) {
       
   156 		case 'id':
       
   157 			return get_userdata($value);
       
   158 			break;
       
   159 		case 'slug':
       
   160 			$user_id = wp_cache_get($value, 'userslugs');
       
   161 			$field = 'user_nicename';
       
   162 			break;
       
   163 		case 'email':
       
   164 			$user_id = wp_cache_get($value, 'useremail');
       
   165 			$field = 'user_email';
       
   166 			break;
       
   167 		case 'login':
       
   168 			$value = sanitize_user( $value );
       
   169 			$user_id = wp_cache_get($value, 'userlogins');
       
   170 			$field = 'user_login';
       
   171 			break;
       
   172 		default:
       
   173 			return false;
       
   174 	}
       
   175 
       
   176 	 if ( false !== $user_id )
       
   177 		return get_userdata($user_id);
       
   178 
       
   179 	if ( !$user = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->users WHERE $field = %s", $value) ) )
       
   180 		return false;
       
   181 
       
   182 	_fill_user($user);
       
   183 
       
   184 	return $user;
       
   185 }
       
   186 endif;
       
   187 
       
   188 if ( !function_exists('get_userdatabylogin') ) :
       
   189 /**
       
   190  * Retrieve user info by login name.
       
   191  *
       
   192  * @since 0.71
       
   193  *
       
   194  * @param string $user_login User's username
       
   195  * @return bool|object False on failure, User DB row object
       
   196  */
       
   197 function get_userdatabylogin($user_login) {
       
   198 	return get_user_by('login', $user_login);
       
   199 }
       
   200 endif;
       
   201 
       
   202 if ( !function_exists('get_user_by_email') ) :
       
   203 /**
       
   204  * Retrieve user info by email.
       
   205  *
       
   206  * @since 2.5
       
   207  *
       
   208  * @param string $email User's email address
       
   209  * @return bool|object False on failure, User DB row object
       
   210  */
       
   211 function get_user_by_email($email) {
       
   212 	return get_user_by('email', $email);
       
   213 }
       
   214 endif;
       
   215 
       
   216 if ( !function_exists( 'wp_mail' ) ) :
       
   217 /**
       
   218  * Send mail, similar to PHP's mail
       
   219  *
       
   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.
       
   223  *
       
   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
       
   227  * name.
       
   228  *
       
   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.
       
   232  *
       
   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.
       
   235  *
       
   236  * @since 1.2.1
       
   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
       
   243  *		phpmailer object.
       
   244  * @uses PHPMailer
       
   245  * @
       
   246  *
       
   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.
       
   253  */
       
   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' ) ) );
       
   257 
       
   258 	if ( !is_array($attachments) )
       
   259 		$attachments = explode( "\n", $attachments );
       
   260 
       
   261 	global $phpmailer;
       
   262 
       
   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();
       
   268 	}
       
   269 
       
   270 	// Headers
       
   271 	if ( empty( $headers ) ) {
       
   272 		$headers = array();
       
   273 	} else {
       
   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 );
       
   278 		} else {
       
   279 			$tempheaders = $headers;
       
   280 		}
       
   281 		$headers = array();
       
   282 
       
   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] ) );
       
   291 					}
       
   292 					continue;
       
   293 				}
       
   294 				// Explode them out
       
   295 				list( $name, $content ) = explode( ':', trim( $header ), 2 );
       
   296 
       
   297 				// Cleanup crew
       
   298 				$name = trim( $name );
       
   299 				$content = trim( $content );
       
   300 
       
   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 );
       
   308 
       
   309 						$from_email = substr( $content, strpos( $content, '<' ) + 1 );
       
   310 						$from_email = str_replace( '>', '', $from_email );
       
   311 						$from_email = trim( $from_email );
       
   312 					} else {
       
   313 						$from_email = trim( $content );
       
   314 					}
       
   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 ) );
       
   323 							$charset = '';
       
   324 						}
       
   325 					} else {
       
   326 						$content_type = trim( $content );
       
   327 					}
       
   328 				} elseif ( 'cc' == strtolower($name) ) {
       
   329 					$cc = explode(",", $content);
       
   330 				} elseif ( 'bcc' == strtolower($name) ) {
       
   331 					$bcc = explode(",", $content);
       
   332 				} else {
       
   333 					// Add it to our grand headers array
       
   334 					$headers[trim( $name )] = trim( $content );
       
   335 				}
       
   336 			}
       
   337 		}
       
   338 	}
       
   339 
       
   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();
       
   348 
       
   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';
       
   353 	}
       
   354 
       
   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.
       
   360 	 */
       
   361 
       
   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 );
       
   367 		}
       
   368 
       
   369 		$from_email = 'wordpress@' . $sitename;
       
   370 	}
       
   371 
       
   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 );
       
   375 
       
   376 	// Set destination address
       
   377 	$phpmailer->AddAddress( $to );
       
   378 
       
   379 	// Set mail's subject and body
       
   380 	$phpmailer->Subject = $subject;
       
   381 	$phpmailer->Body = $message;
       
   382 
       
   383 	// Add any CC and BCC recipients
       
   384 	if ( !empty($cc) ) {
       
   385 		foreach ( (array) $cc as $recipient ) {
       
   386 			$phpmailer->AddCc( trim($recipient) );
       
   387 		}
       
   388 	}
       
   389 	if ( !empty($bcc) ) {
       
   390 		foreach ( (array) $bcc as $recipient) {
       
   391 			$phpmailer->AddBcc( trim($recipient) );
       
   392 		}
       
   393 	}
       
   394 
       
   395 	// Set to use PHP's mail()
       
   396 	$phpmailer->IsMail();
       
   397 
       
   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';
       
   402 	}
       
   403 
       
   404 	$content_type = apply_filters( 'wp_mail_content_type', $content_type );
       
   405 
       
   406 	$phpmailer->ContentType = $content_type;
       
   407 
       
   408 	// Set whether it's plaintext or not, depending on $content_type
       
   409 	if ( $content_type == 'text/html' ) {
       
   410 		$phpmailer->IsHTML( true );
       
   411 	}
       
   412 
       
   413 	// If we don't have a charset from the input headers
       
   414 	if ( !isset( $charset ) ) {
       
   415 		$charset = get_bloginfo( 'charset' );
       
   416 	}
       
   417 
       
   418 	// Set the content-type and charset
       
   419 	$phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
       
   420 
       
   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 ) );
       
   425 		}
       
   426 		if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) ) {
       
   427 			$phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
       
   428 		}
       
   429 	}
       
   430 
       
   431 	if ( !empty( $attachments ) ) {
       
   432 		foreach ( $attachments as $attachment ) {
       
   433 			$phpmailer->AddAttachment($attachment);
       
   434 		}
       
   435 	}
       
   436 
       
   437 	do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
       
   438 
       
   439 	// Send!
       
   440 	$result = @$phpmailer->Send();
       
   441 
       
   442 	return $result;
       
   443 }
       
   444 endif;
       
   445 
       
   446 if ( !function_exists('wp_authenticate') ) :
       
   447 /**
       
   448  * Checks a user's login information and logs them in if it checks out.
       
   449  *
       
   450  * @since 2.5.0
       
   451  *
       
   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.
       
   455  */
       
   456 function wp_authenticate($username, $password) {
       
   457 	$username = sanitize_user($username);
       
   458 	$password = trim($password);
       
   459 
       
   460 	$user = apply_filters('authenticate', null, $username, $password);
       
   461 
       
   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.'));
       
   466 	}
       
   467 
       
   468 	$ignore_codes = array('empty_username', 'empty_password');
       
   469 
       
   470 	if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
       
   471 		do_action('wp_login_failed', $username);
       
   472 	}
       
   473 
       
   474 	return $user;
       
   475 }
       
   476 endif;
       
   477 
       
   478 if ( !function_exists('wp_logout') ) :
       
   479 /**
       
   480  * Log the current user out.
       
   481  *
       
   482  * @since 2.5.0
       
   483  */
       
   484 function wp_logout() {
       
   485 	wp_clear_auth_cookie();
       
   486 	do_action('wp_logout');
       
   487 }
       
   488 endif;
       
   489 
       
   490 if ( !function_exists('wp_validate_auth_cookie') ) :
       
   491 /**
       
   492  * Validates authentication cookie.
       
   493  *
       
   494  * The checks include making sure that the authentication cookie is set and
       
   495  * pulling in the contents (if $cookie is not used).
       
   496  *
       
   497  * Makes sure the cookie is not expired. Verifies the hash in cookie is what is
       
   498  * should be and compares the two.
       
   499  *
       
   500  * @since 2.5
       
   501  *
       
   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.
       
   505  */
       
   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);
       
   509 		return false;
       
   510 	}
       
   511 
       
   512 	extract($cookie_elements, EXTR_OVERWRITE);
       
   513 
       
   514 	$expired = $expiration;
       
   515 
       
   516 	// Allow a grace period for POST and AJAX requests
       
   517 	if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] )
       
   518 		$expired += 3600;
       
   519 
       
   520 	// Quick check to see if an honest cookie has expired
       
   521 	if ( $expired < time() ) {
       
   522 		do_action('auth_cookie_expired', $cookie_elements);
       
   523 		return false;
       
   524 	}
       
   525 
       
   526 	$user = get_userdatabylogin($username);
       
   527 	if ( ! $user ) {
       
   528 		do_action('auth_cookie_bad_username', $cookie_elements);
       
   529 		return false;
       
   530 	}
       
   531 
       
   532 	$pass_frag = substr($user->user_pass, 8, 4);
       
   533 
       
   534 	$key = wp_hash($username . $pass_frag . '|' . $expiration, $scheme);
       
   535 	$hash = hash_hmac('md5', $username . '|' . $expiration, $key);
       
   536 
       
   537 	if ( $hmac != $hash ) {
       
   538 		do_action('auth_cookie_bad_hash', $cookie_elements);
       
   539 		return false;
       
   540 	}
       
   541 
       
   542 	if ( $expiration < time() ) // AJAX/POST grace period set above
       
   543 		$GLOBALS['login_grace_period'] = 1;
       
   544 
       
   545 	do_action('auth_cookie_valid', $cookie_elements, $user);
       
   546 
       
   547 	return $user->ID;
       
   548 }
       
   549 endif;
       
   550 
       
   551 if ( !function_exists('wp_generate_auth_cookie') ) :
       
   552 /**
       
   553  * Generate authentication cookie contents.
       
   554  *
       
   555  * @since 2.5
       
   556  * @uses apply_filters() Calls 'auth_cookie' hook on $cookie contents, User ID
       
   557  *		and expiration of cookie.
       
   558  *
       
   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
       
   563  */
       
   564 function wp_generate_auth_cookie($user_id, $expiration, $scheme = 'auth') {
       
   565 	$user = get_userdata($user_id);
       
   566 
       
   567 	$pass_frag = substr($user->user_pass, 8, 4);
       
   568 
       
   569 	$key = wp_hash($user->user_login . $pass_frag . '|' . $expiration, $scheme);
       
   570 	$hash = hash_hmac('md5', $user->user_login . '|' . $expiration, $key);
       
   571 
       
   572 	$cookie = $user->user_login . '|' . $expiration . '|' . $hash;
       
   573 
       
   574 	return apply_filters('auth_cookie', $cookie, $user_id, $expiration, $scheme);
       
   575 }
       
   576 endif;
       
   577 
       
   578 if ( !function_exists('wp_parse_auth_cookie') ) :
       
   579 /**
       
   580  * Parse a cookie into its components
       
   581  *
       
   582  * @since 2.7
       
   583  *
       
   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
       
   587  */
       
   588 function wp_parse_auth_cookie($cookie = '', $scheme = '') {
       
   589 	if ( empty($cookie) ) {
       
   590 		switch ($scheme){
       
   591 			case 'auth':
       
   592 				$cookie_name = AUTH_COOKIE;
       
   593 				break;
       
   594 			case 'secure_auth':
       
   595 				$cookie_name = SECURE_AUTH_COOKIE;
       
   596 				break;
       
   597 			case "logged_in":
       
   598 				$cookie_name = LOGGED_IN_COOKIE;
       
   599 				break;
       
   600 			default:
       
   601 				if ( is_ssl() ) {
       
   602 					$cookie_name = SECURE_AUTH_COOKIE;
       
   603 					$scheme = 'secure_auth';
       
   604 				} else {
       
   605 					$cookie_name = AUTH_COOKIE;
       
   606 					$scheme = 'auth';
       
   607 				}
       
   608 	    }
       
   609 
       
   610 		if ( empty($_COOKIE[$cookie_name]) )
       
   611 			return false;
       
   612 		$cookie = $_COOKIE[$cookie_name];
       
   613 	}
       
   614 
       
   615 	$cookie_elements = explode('|', $cookie);
       
   616 	if ( count($cookie_elements) != 3 )
       
   617 		return false;
       
   618 
       
   619 	list($username, $expiration, $hmac) = $cookie_elements;
       
   620 
       
   621 	return compact('username', 'expiration', 'hmac', 'scheme');
       
   622 }
       
   623 endif;
       
   624 
       
   625 if ( !function_exists('wp_set_auth_cookie') ) :
       
   626 /**
       
   627  * Sets the authentication cookies based User ID.
       
   628  *
       
   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.
       
   632  *
       
   633  * @since 2.5
       
   634  *
       
   635  * @param int $user_id User ID
       
   636  * @param bool $remember Whether to remember the user or not
       
   637  */
       
   638 function wp_set_auth_cookie($user_id, $remember = false, $secure = '') {
       
   639 	if ( $remember ) {
       
   640 		$expiration = $expire = time() + apply_filters('auth_cookie_expiration', 1209600, $user_id, $remember);
       
   641 	} else {
       
   642 		$expiration = time() + apply_filters('auth_cookie_expiration', 172800, $user_id, $remember);
       
   643 		$expire = 0;
       
   644 	}
       
   645 
       
   646 	if ( '' === $secure )
       
   647 		$secure = is_ssl() ? true : false;
       
   648 
       
   649 	if ( $secure ) {
       
   650 		$auth_cookie_name = SECURE_AUTH_COOKIE;
       
   651 		$scheme = 'secure_auth';
       
   652 	} else {
       
   653 		$auth_cookie_name = AUTH_COOKIE;
       
   654 		$scheme = 'auth';
       
   655 	}
       
   656 
       
   657 	$auth_cookie = wp_generate_auth_cookie($user_id, $expiration, $scheme);
       
   658 	$logged_in_cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
       
   659 
       
   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');
       
   662 
       
   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);
       
   670 	} else {
       
   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);
       
   679 	}
       
   680 }
       
   681 endif;
       
   682 
       
   683 if ( !function_exists('wp_clear_auth_cookie') ) :
       
   684 /**
       
   685  * Removes all of the cookies associated with authentication.
       
   686  *
       
   687  * @since 2.5
       
   688  */
       
   689 function wp_clear_auth_cookie() {
       
   690 	do_action('clear_auth_cookie');
       
   691 
       
   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);
       
   698 
       
   699 	// Old cookies
       
   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);
       
   704 
       
   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);
       
   710 }
       
   711 endif;
       
   712 
       
   713 if ( !function_exists('is_user_logged_in') ) :
       
   714 /**
       
   715  * Checks if the current visitor is a logged in user.
       
   716  *
       
   717  * @since 2.0.0
       
   718  *
       
   719  * @return bool True if user is logged in, false if not logged in.
       
   720  */
       
   721 function is_user_logged_in() {
       
   722 	$user = wp_get_current_user();
       
   723 
       
   724 	if ( $user->id == 0 )
       
   725 		return false;
       
   726 
       
   727 	return true;
       
   728 }
       
   729 endif;
       
   730 
       
   731 if ( !function_exists('auth_redirect') ) :
       
   732 /**
       
   733  * Checks if a user is logged in, if not it redirects them to the login page.
       
   734  *
       
   735  * @since 1.5
       
   736  */
       
   737 function auth_redirect() {
       
   738 	// Checks if a user is logged in, if not redirects them to the login page
       
   739 
       
   740 	if ( is_ssl() || force_ssl_admin() )
       
   741 		$secure = true;
       
   742 	else
       
   743 		$secure = false;
       
   744 
       
   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']));
       
   749 			exit();
       
   750 		} else {
       
   751 			wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
       
   752 			exit();
       
   753 		}
       
   754 	}
       
   755 
       
   756 	if ( $user_id = wp_validate_auth_cookie( '', apply_filters( 'auth_redirect_scheme', '' ) ) ) {
       
   757 		do_action('auth_redirect', $user_id);
       
   758 
       
   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']));
       
   763 				exit();
       
   764 			} else {
       
   765 				wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
       
   766 				exit();
       
   767 			}
       
   768 		}
       
   769 
       
   770 		return;  // The cookie is good so we're done
       
   771 	}
       
   772 
       
   773 	// The cookie is no good so force login
       
   774 	nocache_headers();
       
   775 
       
   776 	if ( is_ssl() )
       
   777 		$proto = 'https://';
       
   778 	else
       
   779 		$proto = 'http://';
       
   780 
       
   781 	$redirect = ( strpos($_SERVER['REQUEST_URI'], '/options.php') && wp_get_referer() ) ? wp_get_referer() : $proto . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
       
   782 
       
   783 	$login_url = wp_login_url($redirect);
       
   784 
       
   785 	wp_redirect($login_url);
       
   786 	exit();
       
   787 }
       
   788 endif;
       
   789 
       
   790 if ( !function_exists('check_admin_referer') ) :
       
   791 /**
       
   792  * Makes sure that a user was referred from another admin page.
       
   793  *
       
   794  * To avoid security exploits.
       
   795  *
       
   796  * @since 1.2.0
       
   797  * @uses do_action() Calls 'check_admin_referer' on $action.
       
   798  *
       
   799  * @param string $action Action nonce
       
   800  * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
       
   801  */
       
   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);
       
   808 		die();
       
   809 	}
       
   810 	do_action('check_admin_referer', $action, $result);
       
   811 	return $result;
       
   812 }endif;
       
   813 
       
   814 if ( !function_exists('check_ajax_referer') ) :
       
   815 /**
       
   816  * Verifies the AJAX request to prevent processing requests external of the blog.
       
   817  *
       
   818  * @since 2.0.3
       
   819  *
       
   820  * @param string $action Action nonce
       
   821  * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
       
   822  */
       
   823 function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
       
   824 	if ( $query_arg )
       
   825 		$nonce = $_REQUEST[$query_arg];
       
   826 	else
       
   827 		$nonce = isset($_REQUEST['_ajax_nonce']) ? $_REQUEST['_ajax_nonce'] : $_REQUEST['_wpnonce'];
       
   828 
       
   829 	$result = wp_verify_nonce( $nonce, $action );
       
   830 
       
   831 	if ( $die && false == $result )
       
   832 		die('-1');
       
   833 
       
   834 	do_action('check_ajax_referer', $action, $result);
       
   835 
       
   836 	return $result;
       
   837 }
       
   838 endif;
       
   839 
       
   840 if ( !function_exists('wp_redirect') ) :
       
   841 /**
       
   842  * Redirects to another page, with a workaround for the IIS Set-Cookie bug.
       
   843  *
       
   844  * @link http://support.microsoft.com/kb/q176113/
       
   845  * @since 1.5.1
       
   846  * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status.
       
   847  *
       
   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
       
   851  */
       
   852 function wp_redirect($location, $status = 302) {
       
   853 	global $is_IIS;
       
   854 
       
   855 	$location = apply_filters('wp_redirect', $location, $status);
       
   856 	$status = apply_filters('wp_redirect_status', $status, $location);
       
   857 
       
   858 	if ( !$location ) // allows the wp_redirect filter to cancel a redirect
       
   859 		return false;
       
   860 
       
   861 	$location = wp_sanitize_redirect($location);
       
   862 
       
   863 	if ( $is_IIS ) {
       
   864 		header("Refresh: 0;url=$location");
       
   865 	} else {
       
   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);
       
   869 	}
       
   870 }
       
   871 endif;
       
   872 
       
   873 if ( !function_exists('wp_sanitize_redirect') ) :
       
   874 /**
       
   875  * Sanitizes a URL for use in a redirect.
       
   876  *
       
   877  * @since 2.3
       
   878  *
       
   879  * @return string redirect-sanitized URL
       
   880  **/
       
   881 function wp_sanitize_redirect($location) {
       
   882 	$location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%!]|i', '', $location);
       
   883 	$location = wp_kses_no_null($location);
       
   884 
       
   885 	// remove %0d and %0a from location
       
   886 	$strip = array('%0d', '%0a', '%0D', '%0A');
       
   887 	$location = _deep_replace($strip, $location);
       
   888 	return $location;
       
   889 }
       
   890 endif;
       
   891 
       
   892 if ( !function_exists('wp_safe_redirect') ) :
       
   893 /**
       
   894  * Performs a safe (local) redirect, using wp_redirect().
       
   895  *
       
   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
       
   898  * list.
       
   899  *
       
   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.
       
   903  *
       
   904  * @since 2.3
       
   905  * @uses wp_validate_redirect() To validate the redirect is to an allowed host.
       
   906  *
       
   907  * @return void Does not return anything
       
   908  **/
       
   909 function wp_safe_redirect($location, $status = 302) {
       
   910 
       
   911 	// Need to look at the URL the way it will end up in wp_redirect()
       
   912 	$location = wp_sanitize_redirect($location);
       
   913 
       
   914 	$location = wp_validate_redirect($location, admin_url());
       
   915 
       
   916 	wp_redirect($location, $status);
       
   917 }
       
   918 endif;
       
   919 
       
   920 if ( !function_exists('wp_validate_redirect') ) :
       
   921 /**
       
   922  * Validates a URL for use in a redirect.
       
   923  *
       
   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
       
   926  * list.
       
   927  *
       
   928  * If the host is not allowed, then the redirect is to $default supplied
       
   929  *
       
   930  * @since 2.8.1
       
   931  * @uses apply_filters() Calls 'allowed_redirect_hosts' on an array containing
       
   932  *		WordPress host string and $location host string.
       
   933  *
       
   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
       
   937  **/
       
   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;
       
   942 
       
   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;
       
   945 
       
   946 	$lp  = parse_url($test);
       
   947 	$wpp = parse_url(get_option('home'));
       
   948 
       
   949 	$allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']), isset($lp['host']) ? $lp['host'] : '');
       
   950 
       
   951 	if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
       
   952 		$location = $default;
       
   953 
       
   954 	return $location;
       
   955 }
       
   956 endif;
       
   957 
       
   958 if ( ! function_exists('wp_notify_postauthor') ) :
       
   959 /**
       
   960  * Notify an author of a comment/trackback/pingback to one of their posts.
       
   961  *
       
   962  * @since 1.0.0
       
   963  *
       
   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.
       
   967  */
       
   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();
       
   973 
       
   974 	if ( $comment->user_id == $post->post_author ) return false; // The author moderated a comment on his own post
       
   975 
       
   976 	if ('' == $user->user_email) return false; // If there's no email to send the comment to
       
   977 
       
   978 	$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
       
   979 	
       
   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);
       
   983 
       
   984 	if ( empty( $comment_type ) ) $comment_type = 'comment';
       
   985 
       
   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 );
       
  1018 	}
       
  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";
       
  1022 	else
       
  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";
       
  1025 
       
  1026 	$wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
       
  1027 
       
  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";
       
  1032 	} else {
       
  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>";
       
  1036 	}
       
  1037 
       
  1038 	$message_headers = "$from\n"
       
  1039 		. "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
       
  1040 
       
  1041 	if ( isset($reply_to) )
       
  1042 		$message_headers .= $reply_to . "\n";
       
  1043 
       
  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);
       
  1047 
       
  1048 	@wp_mail($user->user_email, $subject, $notify_message, $message_headers);
       
  1049 
       
  1050 	return true;
       
  1051 }
       
  1052 endif;
       
  1053 
       
  1054 if ( !function_exists('wp_notify_moderator') ) :
       
  1055 /**
       
  1056  * Notifies the moderator of the blog about a new comment that is awaiting approval.
       
  1057  *
       
  1058  * @since 1.0
       
  1059  * @uses $wpdb
       
  1060  *
       
  1061  * @param int $comment_id Comment ID
       
  1062  * @return bool Always returns true
       
  1063  */
       
  1064 function wp_notify_moderator($comment_id) {
       
  1065 	global $wpdb;
       
  1066 
       
  1067 	if( get_option( "moderation_notify" ) == 0 )
       
  1068 		return true;
       
  1069 
       
  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));
       
  1072 
       
  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'");
       
  1075 	
       
  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);
       
  1079 	
       
  1080 	switch ($comment->comment_type)
       
  1081 	{
       
  1082 		case 'trackback':
       
  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";
       
  1088 			break;
       
  1089 		case 'pingback':
       
  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";
       
  1095 			break;
       
  1096 		default: //Comments
       
  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";
       
  1104 			break;
       
  1105 	}
       
  1106 
       
  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";
       
  1110 	else
       
  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";
       
  1113 
       
  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";
       
  1117 
       
  1118 	$subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), $blogname, $post->post_title );
       
  1119 	$admin_email = get_option('admin_email');
       
  1120 	$message_headers = '';
       
  1121 
       
  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);
       
  1125 
       
  1126 	@wp_mail($admin_email, $subject, $notify_message, $message_headers);
       
  1127 
       
  1128 	return true;
       
  1129 }
       
  1130 endif;
       
  1131 
       
  1132 if ( !function_exists('wp_password_change_notification') ) :
       
  1133 /**
       
  1134  * Notify the blog admin of a user changing password, normally via email.
       
  1135  *
       
  1136  * @since 2.7
       
  1137  *
       
  1138  * @param object $user User Object
       
  1139  */
       
  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);
       
  1149 	}
       
  1150 }
       
  1151 endif;
       
  1152 
       
  1153 if ( !function_exists('wp_new_user_notification') ) :
       
  1154 /**
       
  1155  * Notify the blog admin of a new user, normally via email.
       
  1156  *
       
  1157  * @since 2.0
       
  1158  *
       
  1159  * @param int $user_id User ID
       
  1160  * @param string $plaintext_pass Optional. The user's plaintext password
       
  1161  */
       
  1162 function wp_new_user_notification($user_id, $plaintext_pass = '') {
       
  1163 	$user = new WP_User($user_id);
       
  1164 
       
  1165 	$user_login = stripslashes($user->user_login);
       
  1166 	$user_email = stripslashes($user->user_email);
       
  1167 	
       
  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);
       
  1171 
       
  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";
       
  1175 
       
  1176 	@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);
       
  1177 
       
  1178 	if ( empty($plaintext_pass) )
       
  1179 		return;
       
  1180 
       
  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";
       
  1184 
       
  1185 	wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
       
  1186 
       
  1187 }
       
  1188 endif;
       
  1189 
       
  1190 if ( !function_exists('wp_nonce_tick') ) :
       
  1191 /**
       
  1192  * Get the time-dependent variable for nonce creation.
       
  1193  *
       
  1194  * A nonce has a lifespan of two ticks. Nonces in their second tick may be
       
  1195  * updated, e.g. by autosave.
       
  1196  *
       
  1197  * @since 2.5
       
  1198  *
       
  1199  * @return int
       
  1200  */
       
  1201 function wp_nonce_tick() {
       
  1202 	$nonce_life = apply_filters('nonce_life', 86400);
       
  1203 
       
  1204 	return ceil(time() / ( $nonce_life / 2 ));
       
  1205 }
       
  1206 endif;
       
  1207 
       
  1208 if ( !function_exists('wp_verify_nonce') ) :
       
  1209 /**
       
  1210  * Verify that correct nonce was used with time limit.
       
  1211  *
       
  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.
       
  1214  *
       
  1215  * @since 2.0.3
       
  1216  *
       
  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.
       
  1220  */
       
  1221 function wp_verify_nonce($nonce, $action = -1) {
       
  1222 	$user = wp_get_current_user();
       
  1223 	$uid = (int) $user->id;
       
  1224 
       
  1225 	$i = wp_nonce_tick();
       
  1226 
       
  1227 	// Nonce generated 0-12 hours ago
       
  1228 	if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) == $nonce )
       
  1229 		return 1;
       
  1230 	// Nonce generated 12-24 hours ago
       
  1231 	if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) == $nonce )
       
  1232 		return 2;
       
  1233 	// Invalid nonce
       
  1234 	return false;
       
  1235 }
       
  1236 endif;
       
  1237 
       
  1238 if ( !function_exists('wp_create_nonce') ) :
       
  1239 /**
       
  1240  * Creates a random, one time use token.
       
  1241  *
       
  1242  * @since 2.0.3
       
  1243  *
       
  1244  * @param string|int $action Scalar value to add context to the nonce.
       
  1245  * @return string The one use form token
       
  1246  */
       
  1247 function wp_create_nonce($action = -1) {
       
  1248 	$user = wp_get_current_user();
       
  1249 	$uid = (int) $user->id;
       
  1250 
       
  1251 	$i = wp_nonce_tick();
       
  1252 
       
  1253 	return substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10);
       
  1254 }
       
  1255 endif;
       
  1256 
       
  1257 if ( !function_exists('wp_salt') ) :
       
  1258 /**
       
  1259  * Get salt to add to hashes to help prevent attacks.
       
  1260  *
       
  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
       
  1264  * file.
       
  1265  *
       
  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.
       
  1269  *
       
  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.
       
  1275  *
       
  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.
       
  1280  *
       
  1281  * <code>
       
  1282  * define('SECRET_KEY', 'mAry1HadA15|\/|b17w55w1t3asSn09w');
       
  1283  * </code>
       
  1284  *
       
  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.
       
  1288  *
       
  1289  * @since 2.5
       
  1290  * @link https://api.wordpress.org/secret-key/1.1/ Create a Secret Key for wp-config.php
       
  1291  *
       
  1292  * @return string Salt value from either 'SECRET_KEY' or 'secret' option
       
  1293  */
       
  1294 function wp_salt($scheme = 'auth') {
       
  1295 	global $wp_default_secret_key;
       
  1296 	$secret_key = '';
       
  1297 	if ( defined('SECRET_KEY') && ('' != SECRET_KEY) && ( $wp_default_secret_key != SECRET_KEY) )
       
  1298 		$secret_key = SECRET_KEY;
       
  1299 
       
  1300 	if ( 'auth' == $scheme ) {
       
  1301 		if ( defined('AUTH_KEY') && ('' != AUTH_KEY) && ( $wp_default_secret_key != AUTH_KEY) )
       
  1302 			$secret_key = AUTH_KEY;
       
  1303 
       
  1304 		if ( defined('AUTH_SALT') ) {
       
  1305 			$salt = AUTH_SALT;
       
  1306 		} elseif ( defined('SECRET_SALT') ) {
       
  1307 			$salt = SECRET_SALT;
       
  1308 		} else {
       
  1309 			$salt = get_option('auth_salt');
       
  1310 			if ( empty($salt) ) {
       
  1311 				$salt = wp_generate_password(64);
       
  1312 				update_option('auth_salt', $salt);
       
  1313 			}
       
  1314 		}
       
  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;
       
  1318 
       
  1319 		if ( defined('SECURE_AUTH_SALT') ) {
       
  1320 			$salt = SECURE_AUTH_SALT;
       
  1321 		} else {
       
  1322 			$salt = get_option('secure_auth_salt');
       
  1323 			if ( empty($salt) ) {
       
  1324 				$salt = wp_generate_password(64);
       
  1325 				update_option('secure_auth_salt', $salt);
       
  1326 			}
       
  1327 		}
       
  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;
       
  1331 
       
  1332 		if ( defined('LOGGED_IN_SALT') ) {
       
  1333 			$salt = LOGGED_IN_SALT;
       
  1334 		} else {
       
  1335 			$salt = get_option('logged_in_salt');
       
  1336 			if ( empty($salt) ) {
       
  1337 				$salt = wp_generate_password(64);
       
  1338 				update_option('logged_in_salt', $salt);
       
  1339 			}
       
  1340 		}
       
  1341 	} elseif ( 'nonce' == $scheme ) {
       
  1342 		if ( defined('NONCE_KEY') && ('' != NONCE_KEY) && ( $wp_default_secret_key != NONCE_KEY) )
       
  1343 			$secret_key = NONCE_KEY;
       
  1344 
       
  1345 		if ( defined('NONCE_SALT') ) {
       
  1346 			$salt = NONCE_SALT;
       
  1347 		} else {
       
  1348 			$salt = get_option('nonce_salt');
       
  1349 			if ( empty($salt) ) {
       
  1350 				$salt = wp_generate_password(64);
       
  1351 				update_option('nonce_salt', $salt);
       
  1352 			}
       
  1353 		}
       
  1354 	} else {
       
  1355 		// ensure each auth scheme has its own unique salt
       
  1356 		$salt = hash_hmac('md5', $scheme, $secret_key);
       
  1357 	}
       
  1358 
       
  1359 	return apply_filters('salt', $secret_key . $salt, $scheme);
       
  1360 }
       
  1361 endif;
       
  1362 
       
  1363 if ( !function_exists('wp_hash') ) :
       
  1364 /**
       
  1365  * Get hash of given string.
       
  1366  *
       
  1367  * @since 2.0.3
       
  1368  * @uses wp_salt() Get WordPress salt
       
  1369  *
       
  1370  * @param string $data Plain text to hash
       
  1371  * @return string Hash of $data
       
  1372  */
       
  1373 function wp_hash($data, $scheme = 'auth') {
       
  1374 	$salt = wp_salt($scheme);
       
  1375 
       
  1376 	return hash_hmac('md5', $data, $salt);
       
  1377 }
       
  1378 endif;
       
  1379 
       
  1380 if ( !function_exists('wp_hash_password') ) :
       
  1381 /**
       
  1382  * Create a hash (encrypt) of a plain text password.
       
  1383  *
       
  1384  * For integration with other applications, this function can be overwritten to
       
  1385  * instead use the other package password checking algorithm.
       
  1386  *
       
  1387  * @since 2.5
       
  1388  * @global object $wp_hasher PHPass object
       
  1389  * @uses PasswordHash::HashPassword
       
  1390  *
       
  1391  * @param string $password Plain text user password to hash
       
  1392  * @return string The hash string of the password
       
  1393  */
       
  1394 function wp_hash_password($password) {
       
  1395 	global $wp_hasher;
       
  1396 
       
  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);
       
  1401 	}
       
  1402 
       
  1403 	return $wp_hasher->HashPassword($password);
       
  1404 }
       
  1405 endif;
       
  1406 
       
  1407 if ( !function_exists('wp_check_password') ) :
       
  1408 /**
       
  1409  * Checks the plaintext password against the encrypted Password.
       
  1410  *
       
  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.
       
  1415  *
       
  1416  * For integration with other applications, this function can be overwritten to
       
  1417  * instead use the other package password checking algorithm.
       
  1418  *
       
  1419  * @since 2.5
       
  1420  * @global object $wp_hasher PHPass object used for checking the password
       
  1421  *	against the $hash + $password
       
  1422  * @uses PasswordHash::CheckPassword
       
  1423  *
       
  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
       
  1427  */
       
  1428 function wp_check_password($password, $hash, $user_id = '') {
       
  1429 	global $wp_hasher;
       
  1430 
       
  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);
       
  1438 		}
       
  1439 
       
  1440 		return apply_filters('check_password', $check, $password, $hash, $user_id);
       
  1441 	}
       
  1442 
       
  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);
       
  1449 	}
       
  1450 
       
  1451 	$check = $wp_hasher->CheckPassword($password, $hash);
       
  1452 
       
  1453 	return apply_filters('check_password', $check, $password, $hash, $user_id);
       
  1454 }
       
  1455 endif;
       
  1456 
       
  1457 if ( !function_exists('wp_generate_password') ) :
       
  1458 /**
       
  1459  * Generates a random password drawn from the defined set of characters.
       
  1460  *
       
  1461  * @since 2.5
       
  1462  *
       
  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
       
  1466  **/
       
  1467 function wp_generate_password($length = 12, $special_chars = true) {
       
  1468 	$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
       
  1469 	if ( $special_chars )
       
  1470 		$chars .= '!@#$%^&*()';
       
  1471 
       
  1472 	$password = '';
       
  1473 	for ( $i = 0; $i < $length; $i++ )
       
  1474 		$password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
       
  1475 	return $password;
       
  1476 }
       
  1477 endif;
       
  1478 
       
  1479 if ( !function_exists('wp_rand') ) :
       
  1480  /**
       
  1481  * Generates a random number
       
  1482  *
       
  1483  * @since 2.6.2
       
  1484  *
       
  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
       
  1488  */
       
  1489 function wp_rand( $min = 0, $max = 0 ) {
       
  1490 	global $rnd_value;
       
  1491 
       
  1492 	$seed = get_transient('random_seed');
       
  1493 
       
  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);
       
  1502 	}
       
  1503 
       
  1504 	// Take the first 8 digits for our value
       
  1505 	$value = substr($rnd_value, 0, 8);
       
  1506 
       
  1507 	// Strip the first eight, leaving the remainder for the next call to wp_rand().
       
  1508 	$rnd_value = substr($rnd_value, 8);
       
  1509 
       
  1510 	$value = abs(hexdec($value));
       
  1511 
       
  1512 	// Reduce the value to be within the min - max range
       
  1513 	// 4294967295 = 0xffffffff = max random number
       
  1514 	if ( $max != 0 )
       
  1515 		$value = $min + (($max - $min + 1) * ($value / (4294967295 + 1)));
       
  1516 
       
  1517 	return abs(intval($value));
       
  1518 }
       
  1519 endif;
       
  1520 
       
  1521 if ( !function_exists('wp_set_password') ) :
       
  1522 /**
       
  1523  * Updates the user's password with a new encrypted one.
       
  1524  *
       
  1525  * For integration with other applications, this function can be overwritten to
       
  1526  * instead use the other package password checking algorithm.
       
  1527  *
       
  1528  * @since 2.5
       
  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
       
  1531  *
       
  1532  * @param string $password The plaintext new user password
       
  1533  * @param int $user_id User ID
       
  1534  */
       
  1535 function wp_set_password( $password, $user_id ) {
       
  1536 	global $wpdb;
       
  1537 
       
  1538 	$hash = wp_hash_password($password);
       
  1539 	$wpdb->update($wpdb->users, array('user_pass' => $hash, 'user_activation_key' => ''), array('ID' => $user_id) );
       
  1540 
       
  1541 	wp_cache_delete($user_id, 'users');
       
  1542 }
       
  1543 endif;
       
  1544 
       
  1545 if ( !function_exists( 'get_avatar' ) ) :
       
  1546 /**
       
  1547  * Retrieve the avatar for a user who provided a user ID or email address.
       
  1548  *
       
  1549  * @since 2.5
       
  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
       
  1555 */
       
  1556 function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
       
  1557 	if ( ! get_option('show_avatars') )
       
  1558 		return false;
       
  1559 
       
  1560 	if ( false === $alt)
       
  1561 		$safe_alt = '';
       
  1562 	else
       
  1563 		$safe_alt = esc_attr( $alt );
       
  1564 
       
  1565 	if ( !is_numeric($size) )
       
  1566 		$size = '96';
       
  1567 
       
  1568 	$email = '';
       
  1569 	if ( is_numeric($id_or_email) ) {
       
  1570 		$id = (int) $id_or_email;
       
  1571 		$user = get_userdata($id);
       
  1572 		if ( $user )
       
  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
       
  1577 
       
  1578 		if ( !empty($id_or_email->user_id) ) {
       
  1579 			$id = (int) $id_or_email->user_id;
       
  1580 			$user = get_userdata($id);
       
  1581 			if ( $user)
       
  1582 				$email = $user->user_email;
       
  1583 		} elseif ( !empty($id_or_email->comment_author_email) ) {
       
  1584 			$email = $id_or_email->comment_author_email;
       
  1585 		}
       
  1586 	} else {
       
  1587 		$email = $id_or_email;
       
  1588 	}
       
  1589 
       
  1590 	if ( empty($default) ) {
       
  1591 		$avatar_default = get_option('avatar_default');
       
  1592 		if ( empty($avatar_default) )
       
  1593 			$default = 'mystery';
       
  1594 		else
       
  1595 			$default = $avatar_default;
       
  1596 	}
       
  1597 
       
  1598  	if ( is_ssl() )
       
  1599 		$host = 'https://secure.gravatar.com';
       
  1600 	else
       
  1601 		$host = 'http://www.gravatar.com';
       
  1602 
       
  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 )
       
  1608 		$default = '';
       
  1609 	elseif ( 'gravatar_default' == $default )
       
  1610 		$default = "$host/avatar/s={$size}";
       
  1611 	elseif ( empty($email) )
       
  1612 		$default = "$host/avatar/?d=$default&amp;s={$size}";
       
  1613 	elseif ( strpos($default, 'http://') === 0 )
       
  1614 		$default = add_query_arg( 's', $size, $default );
       
  1615 
       
  1616 	if ( !empty($email) ) {
       
  1617 		$out = "$host/avatar/";
       
  1618 		$out .= md5( strtolower( $email ) );
       
  1619 		$out .= '?s='.$size;
       
  1620 		$out .= '&amp;d=' . urlencode( $default );
       
  1621 
       
  1622 		$rating = get_option('avatar_rating');
       
  1623 		if ( !empty( $rating ) )
       
  1624 			$out .= "&amp;r={$rating}";
       
  1625 
       
  1626 		$avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
       
  1627 	} else {
       
  1628 		$avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
       
  1629 	}
       
  1630 
       
  1631 	return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
       
  1632 }
       
  1633 endif;
       
  1634 
       
  1635 if ( !function_exists('wp_setcookie') ) :
       
  1636 /**
       
  1637  * Sets a cookie for a user who just logged in.
       
  1638  *
       
  1639  * @since 1.5
       
  1640  * @deprecated Use wp_set_auth_cookie()
       
  1641  * @see wp_set_auth_cookie()
       
  1642  *
       
  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
       
  1649  */
       
  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);
       
  1654 }
       
  1655 endif;
       
  1656 
       
  1657 if ( !function_exists('wp_clearcookie') ) :
       
  1658 /**
       
  1659  * Clears the authentication cookie, logging the user out.
       
  1660  *
       
  1661  * @since 1.5
       
  1662  * @deprecated Use wp_clear_auth_cookie()
       
  1663  * @see wp_clear_auth_cookie()
       
  1664  */
       
  1665 function wp_clearcookie() {
       
  1666 	_deprecated_function( __FUNCTION__, '2.5', 'wp_clear_auth_cookie()' );
       
  1667 	wp_clear_auth_cookie();
       
  1668 }
       
  1669 endif;
       
  1670 
       
  1671 if ( !function_exists('wp_get_cookie_login') ):
       
  1672 /**
       
  1673  * Gets the user cookie login.
       
  1674  *
       
  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.
       
  1677  *
       
  1678  * @since 2.0.3
       
  1679  * @deprecated No alternative
       
  1680  *
       
  1681  * @return bool Always returns false
       
  1682  */
       
  1683 function wp_get_cookie_login() {
       
  1684 	_deprecated_function( __FUNCTION__, '2.5', '' );
       
  1685 	return false;
       
  1686 }
       
  1687 endif;
       
  1688 
       
  1689 if ( !function_exists('wp_login') ) :
       
  1690 /**
       
  1691  * Checks a users login information and logs them in if it checks out.
       
  1692  *
       
  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.
       
  1695  *
       
  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.
       
  1699  *
       
  1700  * @since 1.2.2
       
  1701  * @deprecated Use wp_signon()
       
  1702  * @global string $error Error when false is returned
       
  1703  *
       
  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
       
  1708  */
       
  1709 function wp_login($username, $password, $deprecated = '') {
       
  1710 	global $error;
       
  1711 
       
  1712 	$user = wp_authenticate($username, $password);
       
  1713 
       
  1714 	if ( ! is_wp_error($user) )
       
  1715 		return true;
       
  1716 
       
  1717 	$error = $user->get_error_message();
       
  1718 	return false;
       
  1719 }
       
  1720 endif;
       
  1721 
       
  1722 if ( !function_exists( 'wp_text_diff' ) ) :
       
  1723 /**
       
  1724  * Displays a human readable HTML representation of the difference between two strings.
       
  1725  *
       
  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.
       
  1729  *
       
  1730  * The arguments supported and can be changed are listed below.
       
  1731  *
       
  1732  * 'title' : Default is an empty string. Titles the diff in a manner compatible
       
  1733  *		with the output.
       
  1734  * 'title_left' : Default is an empty string. Change the HTML to the left of the
       
  1735  *		title.
       
  1736  * 'title_right' : Default is an empty string. Change the HTML to the right of
       
  1737  *		the title.
       
  1738  *
       
  1739  * @since 2.6
       
  1740  * @see wp_parse_args() Used to change defaults to user defined settings.
       
  1741  * @uses Text_Diff
       
  1742  * @uses WP_Text_Diff_Renderer_Table
       
  1743  *
       
  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.
       
  1748  */
       
  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 );
       
  1752 
       
  1753 	if ( !class_exists( 'WP_Text_Diff_Renderer_Table' ) )
       
  1754 		require( ABSPATH . WPINC . '/wp-diff.php' );
       
  1755 
       
  1756 	$left_string  = normalize_whitespace($left_string);
       
  1757 	$right_string = normalize_whitespace($right_string);
       
  1758 
       
  1759 	$left_lines  = split("\n", $left_string);
       
  1760 	$right_lines = split("\n", $right_string);
       
  1761 
       
  1762 	$text_diff = new Text_Diff($left_lines, $right_lines);
       
  1763 	$renderer  = new WP_Text_Diff_Renderer_Table();
       
  1764 	$diff = $renderer->render($text_diff);
       
  1765 
       
  1766 	if ( !$diff )
       
  1767 		return '';
       
  1768 
       
  1769 	$r  = "<table class='diff'>\n";
       
  1770 	$r .= "<col class='ltype' /><col class='content' /><col class='ltype' /><col class='content' />";
       
  1771 
       
  1772 	if ( $args['title'] || $args['title_left'] || $args['title_right'] )
       
  1773 		$r .= "<thead>";
       
  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";
       
  1780 		$r .= "</tr>\n";
       
  1781 	}
       
  1782 	if ( $args['title'] || $args['title_left'] || $args['title_right'] )
       
  1783 		$r .= "</thead>\n";
       
  1784 
       
  1785 	$r .= "<tbody>\n$diff\n</tbody>\n";
       
  1786 	$r .= "</table>";
       
  1787 
       
  1788 	return $r;
       
  1789 }
       
  1790 endif;
       
  1791