PageRenderTime 630ms CodeModel.GetById 81ms app.highlight 271ms RepoModel.GetById 67ms app.codeStats 4ms

/wordpress/wp-includes/pluggable.php

https://bitbucket.org/mpizza/aws-pizza
PHP | 1740 lines | 951 code | 240 blank | 549 comment | 272 complexity | 7731d86b7e21f69de348d659ac2393f6 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   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
   9if ( !function_exists('wp_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 * Some WordPress functionality is based on the current user and not based on
  16 * the signed in user. Therefore, it opens the ability to edit and perform
  17 * actions on users who aren't signed in.
  18 *
  19 * @since 2.0.3
  20 * @global object $current_user The current user object which holds the user data.
  21 * @uses do_action() Calls 'set_current_user' hook after setting the current user.
  22 *
  23 * @param int $id User ID
  24 * @param string $name User's username
  25 * @return WP_User Current user User object
  26 */
  27function wp_set_current_user($id, $name = '') {
  28	global $current_user;
  29
  30	if ( isset( $current_user ) && ( $current_user instanceof WP_User ) && ( $id == $current_user->ID ) )
  31		return $current_user;
  32
  33	$current_user = new WP_User( $id, $name );
  34
  35	setup_userdata( $current_user->ID );
  36
  37	do_action('set_current_user');
  38
  39	return $current_user;
  40}
  41endif;
  42
  43if ( !function_exists('wp_get_current_user') ) :
  44/**
  45 * Retrieve the current user object.
  46 *
  47 * @since 2.0.3
  48 *
  49 * @return WP_User Current user WP_User object
  50 */
  51function wp_get_current_user() {
  52	global $current_user;
  53
  54	get_currentuserinfo();
  55
  56	return $current_user;
  57}
  58endif;
  59
  60if ( !function_exists('get_currentuserinfo') ) :
  61/**
  62 * Populate global variables with information about the currently logged in user.
  63 *
  64 * Will set the current user, if the current user is not set. The current user
  65 * will be set to the logged in person. If no user is logged in, then it will
  66 * set the current user to 0, which is invalid and won't have any permissions.
  67 *
  68 * @since 0.71
  69 * @uses $current_user Checks if the current user is set
  70 * @uses wp_validate_auth_cookie() Retrieves current logged in user.
  71 *
  72 * @return bool|null False on XMLRPC Request and invalid auth cookie. Null when current user set
  73 */
  74function get_currentuserinfo() {
  75	global $current_user;
  76
  77	if ( ! empty( $current_user ) ) {
  78		if ( $current_user instanceof WP_User )
  79			return;
  80
  81		// Upgrade stdClass to WP_User
  82		if ( is_object( $current_user ) && isset( $current_user->ID ) ) {
  83			$cur_id = $current_user->ID;
  84			$current_user = null;
  85			wp_set_current_user( $cur_id );
  86			return;
  87		}
  88
  89		// $current_user has a junk value. Force to WP_User with ID 0.
  90		$current_user = null;
  91		wp_set_current_user( 0 );
  92		return false;
  93	}
  94
  95	if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) {
  96		wp_set_current_user( 0 );
  97		return false;
  98	}
  99
 100	if ( ! $user = wp_validate_auth_cookie() ) {
 101		 if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[LOGGED_IN_COOKIE] ) || !$user = wp_validate_auth_cookie( $_COOKIE[LOGGED_IN_COOKIE], 'logged_in' ) ) {
 102		 	wp_set_current_user( 0 );
 103		 	return false;
 104		 }
 105	}
 106
 107	wp_set_current_user( $user );
 108}
 109endif;
 110
 111if ( !function_exists('get_userdata') ) :
 112/**
 113 * Retrieve user info by user ID.
 114 *
 115 * @since 0.71
 116 *
 117 * @param int $user_id User ID
 118 * @return bool|object False on failure, WP_User object on success
 119 */
 120function get_userdata( $user_id ) {
 121	return get_user_by( 'id', $user_id );
 122}
 123endif;
 124
 125if ( !function_exists('get_user_by') ) :
 126/**
 127 * Retrieve user info by a given field
 128 *
 129 * @since 2.8.0
 130 *
 131 * @param string $field The field to retrieve the user with. id | slug | email | login
 132 * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
 133 * @return bool|object False on failure, WP_User object on success
 134 */
 135function get_user_by( $field, $value ) {
 136	$userdata = WP_User::get_data_by( $field, $value );
 137
 138	if ( !$userdata )
 139		return false;
 140
 141	$user = new WP_User;
 142	$user->init( $userdata );
 143
 144	return $user;
 145}
 146endif;
 147
 148if ( !function_exists('cache_users') ) :
 149/**
 150 * Retrieve info for user lists to prevent multiple queries by get_userdata()
 151 *
 152 * @since 3.0.0
 153 *
 154 * @param array $user_ids User ID numbers list
 155 */
 156function cache_users( $user_ids ) {
 157	global $wpdb;
 158
 159	$clean = _get_non_cached_ids( $user_ids, 'users' );
 160
 161	if ( empty( $clean ) )
 162		return;
 163
 164	$list = implode( ',', $clean );
 165
 166	$users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" );
 167
 168	$ids = array();
 169	foreach ( $users as $user ) {
 170		update_user_caches( $user );
 171		$ids[] = $user->ID;
 172	}
 173	update_meta_cache( 'user', $ids );
 174}
 175endif;
 176
 177if ( !function_exists( 'wp_mail' ) ) :
 178/**
 179 * Send mail, similar to PHP's mail
 180 *
 181 * A true return value does not automatically mean that the user received the
 182 * email successfully. It just only means that the method used was able to
 183 * process the request without any errors.
 184 *
 185 * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from
 186 * creating a from address like 'Name <email@address.com>' when both are set. If
 187 * just 'wp_mail_from' is set, then just the email address will be used with no
 188 * name.
 189 *
 190 * The default content type is 'text/plain' which does not allow using HTML.
 191 * However, you can set the content type of the email by using the
 192 * 'wp_mail_content_type' filter.
 193 *
 194 * The default charset is based on the charset used on the blog. The charset can
 195 * be set using the 'wp_mail_charset' filter.
 196 *
 197 * @since 1.2.1
 198 * @uses apply_filters() Calls 'wp_mail' hook on an array of all of the parameters.
 199 * @uses apply_filters() Calls 'wp_mail_from' hook to get the from email address.
 200 * @uses apply_filters() Calls 'wp_mail_from_name' hook to get the from address name.
 201 * @uses apply_filters() Calls 'wp_mail_content_type' hook to get the email content type.
 202 * @uses apply_filters() Calls 'wp_mail_charset' hook to get the email charset
 203 * @uses do_action_ref_array() Calls 'phpmailer_init' hook on the reference to
 204 *		phpmailer object.
 205 * @uses PHPMailer
 206 *
 207 * @param string|array $to Array or comma-separated list of email addresses to send message.
 208 * @param string $subject Email subject
 209 * @param string $message Message contents
 210 * @param string|array $headers Optional. Additional headers.
 211 * @param string|array $attachments Optional. Files to attach.
 212 * @return bool Whether the email contents were sent successfully.
 213 */
 214function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
 215	// Compact the input, apply the filters, and extract them back out
 216	extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
 217
 218	if ( !is_array($attachments) )
 219		$attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
 220
 221	global $phpmailer;
 222
 223	// (Re)create it, if it's gone missing
 224	if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
 225		require_once ABSPATH . WPINC . '/class-phpmailer.php';
 226		require_once ABSPATH . WPINC . '/class-smtp.php';
 227		$phpmailer = new PHPMailer( true );
 228	}
 229
 230	// Headers
 231	if ( empty( $headers ) ) {
 232		$headers = array();
 233	} else {
 234		if ( !is_array( $headers ) ) {
 235			// Explode the headers out, so this function can take both
 236			// string headers and an array of headers.
 237			$tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
 238		} else {
 239			$tempheaders = $headers;
 240		}
 241		$headers = array();
 242		$cc = array();
 243		$bcc = array();
 244
 245		// If it's actually got contents
 246		if ( !empty( $tempheaders ) ) {
 247			// Iterate through the raw headers
 248			foreach ( (array) $tempheaders as $header ) {
 249				if ( strpos($header, ':') === false ) {
 250					if ( false !== stripos( $header, 'boundary=' ) ) {
 251						$parts = preg_split('/boundary=/i', trim( $header ) );
 252						$boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
 253					}
 254					continue;
 255				}
 256				// Explode them out
 257				list( $name, $content ) = explode( ':', trim( $header ), 2 );
 258
 259				// Cleanup crew
 260				$name    = trim( $name    );
 261				$content = trim( $content );
 262
 263				switch ( strtolower( $name ) ) {
 264					// Mainly for legacy -- process a From: header if it's there
 265					case 'from':
 266						if ( strpos($content, '<' ) !== false ) {
 267							// So... making my life hard again?
 268							$from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
 269							$from_name = str_replace( '"', '', $from_name );
 270							$from_name = trim( $from_name );
 271
 272							$from_email = substr( $content, strpos( $content, '<' ) + 1 );
 273							$from_email = str_replace( '>', '', $from_email );
 274							$from_email = trim( $from_email );
 275						} else {
 276							$from_email = trim( $content );
 277						}
 278						break;
 279					case 'content-type':
 280						if ( strpos( $content, ';' ) !== false ) {
 281							list( $type, $charset ) = explode( ';', $content );
 282							$content_type = trim( $type );
 283							if ( false !== stripos( $charset, 'charset=' ) ) {
 284								$charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
 285							} elseif ( false !== stripos( $charset, 'boundary=' ) ) {
 286								$boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
 287								$charset = '';
 288							}
 289						} else {
 290							$content_type = trim( $content );
 291						}
 292						break;
 293					case 'cc':
 294						$cc = array_merge( (array) $cc, explode( ',', $content ) );
 295						break;
 296					case 'bcc':
 297						$bcc = array_merge( (array) $bcc, explode( ',', $content ) );
 298						break;
 299					default:
 300						// Add it to our grand headers array
 301						$headers[trim( $name )] = trim( $content );
 302						break;
 303				}
 304			}
 305		}
 306	}
 307
 308	// Empty out the values that may be set
 309	$phpmailer->ClearAddresses();
 310	$phpmailer->ClearAllRecipients();
 311	$phpmailer->ClearAttachments();
 312	$phpmailer->ClearBCCs();
 313	$phpmailer->ClearCCs();
 314	$phpmailer->ClearCustomHeaders();
 315	$phpmailer->ClearReplyTos();
 316
 317	// From email and name
 318	// If we don't have a name from the input headers
 319	if ( !isset( $from_name ) )
 320		$from_name = 'WordPress';
 321
 322	/* If we don't have an email from the input headers default to wordpress@$sitename
 323	 * Some hosts will block outgoing mail from this address if it doesn't exist but
 324	 * there's no easy alternative. Defaulting to admin_email might appear to be another
 325	 * option but some hosts may refuse to relay mail from an unknown domain. See
 326	 * http://trac.wordpress.org/ticket/5007.
 327	 */
 328
 329	if ( !isset( $from_email ) ) {
 330		// Get the site domain and get rid of www.
 331		$sitename = strtolower( $_SERVER['SERVER_NAME'] );
 332		if ( substr( $sitename, 0, 4 ) == 'www.' ) {
 333			$sitename = substr( $sitename, 4 );
 334		}
 335
 336		$from_email = 'wordpress@' . $sitename;
 337	}
 338
 339	// Plugin authors can override the potentially troublesome default
 340	$phpmailer->From     = apply_filters( 'wp_mail_from'     , $from_email );
 341	$phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name  );
 342
 343	// Set destination addresses
 344	if ( !is_array( $to ) )
 345		$to = explode( ',', $to );
 346
 347	foreach ( (array) $to as $recipient ) {
 348		try {
 349			// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
 350			$recipient_name = '';
 351			if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
 352				if ( count( $matches ) == 3 ) {
 353					$recipient_name = $matches[1];
 354					$recipient = $matches[2];
 355				}
 356			}
 357			$phpmailer->AddAddress( $recipient, $recipient_name);
 358		} catch ( phpmailerException $e ) {
 359			continue;
 360		}
 361	}
 362
 363	// Set mail's subject and body
 364	$phpmailer->Subject = $subject;
 365	$phpmailer->Body    = $message;
 366
 367	// Add any CC and BCC recipients
 368	if ( !empty( $cc ) ) {
 369		foreach ( (array) $cc as $recipient ) {
 370			try {
 371				// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
 372				$recipient_name = '';
 373				if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
 374					if ( count( $matches ) == 3 ) {
 375						$recipient_name = $matches[1];
 376						$recipient = $matches[2];
 377					}
 378				}
 379				$phpmailer->AddCc( $recipient, $recipient_name );
 380			} catch ( phpmailerException $e ) {
 381				continue;
 382			}
 383		}
 384	}
 385
 386	if ( !empty( $bcc ) ) {
 387		foreach ( (array) $bcc as $recipient) {
 388			try {
 389				// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
 390				$recipient_name = '';
 391				if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
 392					if ( count( $matches ) == 3 ) {
 393						$recipient_name = $matches[1];
 394						$recipient = $matches[2];
 395					}
 396				}
 397				$phpmailer->AddBcc( $recipient, $recipient_name );
 398			} catch ( phpmailerException $e ) {
 399				continue;
 400			}
 401		}
 402	}
 403
 404	// Set to use PHP's mail()
 405	$phpmailer->IsMail();
 406
 407	// Set Content-Type and charset
 408	// If we don't have a content-type from the input headers
 409	if ( !isset( $content_type ) )
 410		$content_type = 'text/plain';
 411
 412	$content_type = apply_filters( 'wp_mail_content_type', $content_type );
 413
 414	$phpmailer->ContentType = $content_type;
 415
 416	// Set whether it's plaintext, depending on $content_type
 417	if ( 'text/html' == $content_type )
 418		$phpmailer->IsHTML( true );
 419
 420	// If we don't have a charset from the input headers
 421	if ( !isset( $charset ) )
 422		$charset = get_bloginfo( 'charset' );
 423
 424	// Set the content-type and charset
 425	$phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
 426
 427	// Set custom headers
 428	if ( !empty( $headers ) ) {
 429		foreach( (array) $headers as $name => $content ) {
 430			$phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
 431		}
 432
 433		if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
 434			$phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
 435	}
 436
 437	if ( !empty( $attachments ) ) {
 438		foreach ( $attachments as $attachment ) {
 439			try {
 440				$phpmailer->AddAttachment($attachment);
 441			} catch ( phpmailerException $e ) {
 442				continue;
 443			}
 444		}
 445	}
 446
 447	do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
 448
 449	// Send!
 450	try {
 451		$phpmailer->Send();
 452	} catch ( phpmailerException $e ) {
 453		return false;
 454	}
 455
 456	return true;
 457}
 458endif;
 459
 460if ( !function_exists('wp_authenticate') ) :
 461/**
 462 * Checks a user's login information and logs them in if it checks out.
 463 *
 464 * @since 2.5.0
 465 *
 466 * @param string $username User's username
 467 * @param string $password User's password
 468 * @return WP_Error|WP_User WP_User object if login successful, otherwise WP_Error object.
 469 */
 470function wp_authenticate($username, $password) {
 471	$username = sanitize_user($username);
 472	$password = trim($password);
 473
 474	$user = apply_filters('authenticate', null, $username, $password);
 475
 476	if ( $user == null ) {
 477		// TODO what should the error message be? (Or would these even happen?)
 478		// Only needed if all authentication handlers fail to return anything.
 479		$user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
 480	}
 481
 482	$ignore_codes = array('empty_username', 'empty_password');
 483
 484	if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
 485		do_action('wp_login_failed', $username);
 486	}
 487
 488	return $user;
 489}
 490endif;
 491
 492if ( !function_exists('wp_logout') ) :
 493/**
 494 * Log the current user out.
 495 *
 496 * @since 2.5.0
 497 */
 498function wp_logout() {
 499	wp_clear_auth_cookie();
 500	do_action('wp_logout');
 501}
 502endif;
 503
 504if ( !function_exists('wp_validate_auth_cookie') ) :
 505/**
 506 * Validates authentication cookie.
 507 *
 508 * The checks include making sure that the authentication cookie is set and
 509 * pulling in the contents (if $cookie is not used).
 510 *
 511 * Makes sure the cookie is not expired. Verifies the hash in cookie is what is
 512 * should be and compares the two.
 513 *
 514 * @since 2.5
 515 *
 516 * @param string $cookie Optional. If used, will validate contents instead of cookie's
 517 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
 518 * @return bool|int False if invalid cookie, User ID if valid.
 519 */
 520function wp_validate_auth_cookie($cookie = '', $scheme = '') {
 521	if ( ! $cookie_elements = wp_parse_auth_cookie($cookie, $scheme) ) {
 522		do_action('auth_cookie_malformed', $cookie, $scheme);
 523		return false;
 524	}
 525
 526	extract($cookie_elements, EXTR_OVERWRITE);
 527
 528	$expired = $expiration;
 529
 530	// Allow a grace period for POST and AJAX requests
 531	if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] )
 532		$expired += HOUR_IN_SECONDS;
 533
 534	// Quick check to see if an honest cookie has expired
 535	if ( $expired < time() ) {
 536		do_action('auth_cookie_expired', $cookie_elements);
 537		return false;
 538	}
 539
 540	$user = get_user_by('login', $username);
 541	if ( ! $user ) {
 542		do_action('auth_cookie_bad_username', $cookie_elements);
 543		return false;
 544	}
 545
 546	$pass_frag = substr($user->user_pass, 8, 4);
 547
 548	$key = wp_hash($username . $pass_frag . '|' . $expiration, $scheme);
 549	$hash = hash_hmac('md5', $username . '|' . $expiration, $key);
 550
 551	if ( $hmac != $hash ) {
 552		do_action('auth_cookie_bad_hash', $cookie_elements);
 553		return false;
 554	}
 555
 556	if ( $expiration < time() ) // AJAX/POST grace period set above
 557		$GLOBALS['login_grace_period'] = 1;
 558
 559	do_action('auth_cookie_valid', $cookie_elements, $user);
 560
 561	return $user->ID;
 562}
 563endif;
 564
 565if ( !function_exists('wp_generate_auth_cookie') ) :
 566/**
 567 * Generate authentication cookie contents.
 568 *
 569 * @since 2.5
 570 * @uses apply_filters() Calls 'auth_cookie' hook on $cookie contents, User ID
 571 *		and expiration of cookie.
 572 *
 573 * @param int $user_id User ID
 574 * @param int $expiration Cookie expiration in seconds
 575 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
 576 * @return string Authentication cookie contents
 577 */
 578function wp_generate_auth_cookie($user_id, $expiration, $scheme = 'auth') {
 579	$user = get_userdata($user_id);
 580
 581	$pass_frag = substr($user->user_pass, 8, 4);
 582
 583	$key = wp_hash($user->user_login . $pass_frag . '|' . $expiration, $scheme);
 584	$hash = hash_hmac('md5', $user->user_login . '|' . $expiration, $key);
 585
 586	$cookie = $user->user_login . '|' . $expiration . '|' . $hash;
 587
 588	return apply_filters('auth_cookie', $cookie, $user_id, $expiration, $scheme);
 589}
 590endif;
 591
 592if ( !function_exists('wp_parse_auth_cookie') ) :
 593/**
 594 * Parse a cookie into its components
 595 *
 596 * @since 2.7
 597 *
 598 * @param string $cookie
 599 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
 600 * @return array Authentication cookie components
 601 */
 602function wp_parse_auth_cookie($cookie = '', $scheme = '') {
 603	if ( empty($cookie) ) {
 604		switch ($scheme){
 605			case 'auth':
 606				$cookie_name = AUTH_COOKIE;
 607				break;
 608			case 'secure_auth':
 609				$cookie_name = SECURE_AUTH_COOKIE;
 610				break;
 611			case "logged_in":
 612				$cookie_name = LOGGED_IN_COOKIE;
 613				break;
 614			default:
 615				if ( is_ssl() ) {
 616					$cookie_name = SECURE_AUTH_COOKIE;
 617					$scheme = 'secure_auth';
 618				} else {
 619					$cookie_name = AUTH_COOKIE;
 620					$scheme = 'auth';
 621				}
 622	    }
 623
 624		if ( empty($_COOKIE[$cookie_name]) )
 625			return false;
 626		$cookie = $_COOKIE[$cookie_name];
 627	}
 628
 629	$cookie_elements = explode('|', $cookie);
 630	if ( count($cookie_elements) != 3 )
 631		return false;
 632
 633	list($username, $expiration, $hmac) = $cookie_elements;
 634
 635	return compact('username', 'expiration', 'hmac', 'scheme');
 636}
 637endif;
 638
 639if ( !function_exists('wp_set_auth_cookie') ) :
 640/**
 641 * Sets the authentication cookies based User ID.
 642 *
 643 * The $remember parameter increases the time that the cookie will be kept. The
 644 * default the cookie is kept without remembering is two days. When $remember is
 645 * set, the cookies will be kept for 14 days or two weeks.
 646 *
 647 * @since 2.5
 648 *
 649 * @param int $user_id User ID
 650 * @param bool $remember Whether to remember the user
 651 */
 652function wp_set_auth_cookie($user_id, $remember = false, $secure = '') {
 653	if ( $remember ) {
 654		$expiration = $expire = time() + apply_filters('auth_cookie_expiration', 1209600, $user_id, $remember);
 655	} else {
 656		$expiration = time() + apply_filters('auth_cookie_expiration', 172800, $user_id, $remember);
 657		$expire = 0;
 658	}
 659
 660	if ( '' === $secure )
 661		$secure = is_ssl();
 662
 663	$secure = apply_filters('secure_auth_cookie', $secure, $user_id);
 664	$secure_logged_in_cookie = apply_filters('secure_logged_in_cookie', false, $user_id, $secure);
 665
 666	if ( $secure ) {
 667		$auth_cookie_name = SECURE_AUTH_COOKIE;
 668		$scheme = 'secure_auth';
 669	} else {
 670		$auth_cookie_name = AUTH_COOKIE;
 671		$scheme = 'auth';
 672	}
 673
 674	$auth_cookie = wp_generate_auth_cookie($user_id, $expiration, $scheme);
 675	$logged_in_cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
 676
 677	do_action('set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme);
 678	do_action('set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in');
 679
 680	setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
 681	setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
 682	setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true);
 683	if ( COOKIEPATH != SITECOOKIEPATH )
 684		setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true);
 685}
 686endif;
 687
 688if ( !function_exists('wp_clear_auth_cookie') ) :
 689/**
 690 * Removes all of the cookies associated with authentication.
 691 *
 692 * @since 2.5
 693 */
 694function wp_clear_auth_cookie() {
 695	do_action('clear_auth_cookie');
 696
 697	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH,   COOKIE_DOMAIN );
 698	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH,   COOKIE_DOMAIN );
 699	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, 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() - YEAR_IN_SECONDS, COOKIEPATH,          COOKIE_DOMAIN );
 702	setcookie( LOGGED_IN_COOKIE,   ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH,      COOKIE_DOMAIN );
 703
 704	// Old cookies
 705	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
 706	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
 707	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
 708	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
 709
 710	// Even older cookies
 711	setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
 712	setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
 713	setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
 714	setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
 715}
 716endif;
 717
 718if ( !function_exists('is_user_logged_in') ) :
 719/**
 720 * Checks if the current visitor is a logged in user.
 721 *
 722 * @since 2.0.0
 723 *
 724 * @return bool True if user is logged in, false if not logged in.
 725 */
 726function is_user_logged_in() {
 727	$user = wp_get_current_user();
 728
 729	if ( ! $user->exists() )
 730		return false;
 731
 732	return true;
 733}
 734endif;
 735
 736if ( !function_exists('auth_redirect') ) :
 737/**
 738 * Checks if a user is logged in, if not it redirects them to the login page.
 739 *
 740 * @since 1.5
 741 */
 742function auth_redirect() {
 743	// Checks if a user is logged in, if not redirects them to the login page
 744
 745	$secure = ( is_ssl() || force_ssl_admin() );
 746
 747	$secure = apply_filters('secure_auth_redirect', $secure);
 748
 749	// If https is required and request is http, redirect
 750	if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
 751		if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
 752			wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
 753			exit();
 754		} else {
 755			wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
 756			exit();
 757		}
 758	}
 759
 760	if ( is_user_admin() )
 761		$scheme = 'logged_in';
 762	else
 763		$scheme = apply_filters( 'auth_redirect_scheme', '' );
 764
 765	if ( $user_id = wp_validate_auth_cookie( '',  $scheme) ) {
 766		do_action('auth_redirect', $user_id);
 767
 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') ) {
 770			if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
 771				wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
 772				exit();
 773			} else {
 774				wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
 775				exit();
 776			}
 777		}
 778
 779		return;  // The cookie is good so we're done
 780	}
 781
 782	// The cookie is no good so force login
 783	nocache_headers();
 784
 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
 787	$login_url = wp_login_url($redirect, true);
 788
 789	wp_redirect($login_url);
 790	exit();
 791}
 792endif;
 793
 794if ( !function_exists('check_admin_referer') ) :
 795/**
 796 * Makes sure that a user was referred from another admin page.
 797 *
 798 * To avoid security exploits.
 799 *
 800 * @since 1.2.0
 801 * @uses do_action() Calls 'check_admin_referer' on $action.
 802 *
 803 * @param string $action Action nonce
 804 * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
 805 */
 806function check_admin_referer($action = -1, $query_arg = '_wpnonce') {
 807	if ( -1 == $action )
 808		_doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '3.2' );
 809
 810	$adminurl = strtolower(admin_url());
 811	$referer = strtolower(wp_get_referer());
 812	$result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce($_REQUEST[$query_arg], $action) : false;
 813	if ( !$result && !(-1 == $action && strpos($referer, $adminurl) === 0) ) {
 814		wp_nonce_ays($action);
 815		die();
 816	}
 817	do_action('check_admin_referer', $action, $result);
 818	return $result;
 819}endif;
 820
 821if ( !function_exists('check_ajax_referer') ) :
 822/**
 823 * Verifies the AJAX request to prevent processing requests external of the blog.
 824 *
 825 * @since 2.0.3
 826 *
 827 * @param string $action Action nonce
 828 * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
 829 */
 830function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
 831	if ( $query_arg )
 832		$nonce = $_REQUEST[$query_arg];
 833	else
 834		$nonce = isset($_REQUEST['_ajax_nonce']) ? $_REQUEST['_ajax_nonce'] : $_REQUEST['_wpnonce'];
 835
 836	$result = wp_verify_nonce( $nonce, $action );
 837
 838	if ( $die && false == $result ) {
 839		if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
 840			wp_die( -1 );
 841		else
 842			die( '-1' );
 843	}
 844
 845	do_action('check_ajax_referer', $action, $result);
 846
 847	return $result;
 848}
 849endif;
 850
 851if ( !function_exists('wp_redirect') ) :
 852/**
 853 * Redirects to another page.
 854 *
 855 * @since 1.5.1
 856 * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status.
 857 *
 858 * @param string $location The path to redirect to
 859 * @param int $status Status code to use
 860 * @return bool False if $location is not set
 861 */
 862function wp_redirect($location, $status = 302) {
 863	global $is_IIS;
 864
 865	$location = apply_filters('wp_redirect', $location, $status);
 866	$status = apply_filters('wp_redirect_status', $status, $location);
 867
 868	if ( !$location ) // allows the wp_redirect filter to cancel a redirect
 869		return false;
 870
 871	$location = wp_sanitize_redirect($location);
 872
 873	if ( !$is_IIS && php_sapi_name() != 'cgi-fcgi' )
 874		status_header($status); // This causes problems on IIS and some FastCGI setups
 875
 876	header("Location: $location", true, $status);
 877}
 878endif;
 879
 880if ( !function_exists('wp_sanitize_redirect') ) :
 881/**
 882 * Sanitizes a URL for use in a redirect.
 883 *
 884 * @since 2.3
 885 *
 886 * @return string redirect-sanitized URL
 887 **/
 888function wp_sanitize_redirect($location) {
 889	$location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%!]|i', '', $location);
 890	$location = wp_kses_no_null($location);
 891
 892	// remove %0d and %0a from location
 893	$strip = array('%0d', '%0a', '%0D', '%0A');
 894	$location = _deep_replace($strip, $location);
 895	return $location;
 896}
 897endif;
 898
 899if ( !function_exists('wp_safe_redirect') ) :
 900/**
 901 * Performs a safe (local) redirect, using wp_redirect().
 902 *
 903 * Checks whether the $location is using an allowed host, if it has an absolute
 904 * path. A plugin can therefore set or remove allowed host(s) to or from the
 905 * list.
 906 *
 907 * If the host is not allowed, then the redirect is to wp-admin on the siteurl
 908 * instead. This prevents malicious redirects which redirect to another host,
 909 * but only used in a few places.
 910 *
 911 * @since 2.3
 912 * @uses wp_validate_redirect() To validate the redirect is to an allowed host.
 913 *
 914 * @return void Does not return anything
 915 **/
 916function wp_safe_redirect($location, $status = 302) {
 917
 918	// Need to look at the URL the way it will end up in wp_redirect()
 919	$location = wp_sanitize_redirect($location);
 920
 921	$location = wp_validate_redirect($location, admin_url());
 922
 923	wp_redirect($location, $status);
 924}
 925endif;
 926
 927if ( !function_exists('wp_validate_redirect') ) :
 928/**
 929 * Validates a URL for use in a redirect.
 930 *
 931 * Checks whether the $location is using an allowed host, if it has an absolute
 932 * path. A plugin can therefore set or remove allowed host(s) to or from the
 933 * list.
 934 *
 935 * If the host is not allowed, then the redirect is to $default supplied
 936 *
 937 * @since 2.8.1
 938 * @uses apply_filters() Calls 'allowed_redirect_hosts' on an array containing
 939 *		WordPress host string and $location host string.
 940 *
 941 * @param string $location The redirect to validate
 942 * @param string $default The value to return if $location is not allowed
 943 * @return string redirect-sanitized URL
 944 **/
 945function wp_validate_redirect($location, $default = '') {
 946	// browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'
 947	if ( substr($location, 0, 2) == '//' )
 948		$location = 'http:' . $location;
 949
 950	// In php 5 parse_url may fail if the URL query part contains http://, bug #38143
 951	$test = ( $cut = strpos($location, '?') ) ? substr( $location, 0, $cut ) : $location;
 952
 953	$lp  = parse_url($test);
 954
 955	// Give up if malformed URL
 956	if ( false === $lp )
 957		return $default;
 958
 959	// Allow only http and https schemes. No data:, etc.
 960	if ( isset($lp['scheme']) && !('http' == $lp['scheme'] || 'https' == $lp['scheme']) )
 961		return $default;
 962
 963	// Reject if scheme is set but host is not. This catches urls like https:host.com for which parse_url does not set the host field.
 964	if ( isset($lp['scheme'])  && !isset($lp['host']) )
 965		return $default;
 966
 967	$wpp = parse_url(home_url());
 968
 969	$allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']), isset($lp['host']) ? $lp['host'] : '');
 970
 971	if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
 972		$location = $default;
 973
 974	return $location;
 975}
 976endif;
 977
 978if ( ! function_exists('wp_notify_postauthor') ) :
 979/**
 980 * Notify an author of a comment/trackback/pingback to one of their posts.
 981 *
 982 * @since 1.0.0
 983 *
 984 * @param int $comment_id Comment ID
 985 * @param string $comment_type Optional. The comment type either 'comment' (default), 'trackback', or 'pingback'
 986 * @return bool False if user email does not exist. True on completion.
 987 */
 988function wp_notify_postauthor( $comment_id, $comment_type = '' ) {
 989	$comment = get_comment( $comment_id );
 990	$post    = get_post( $comment->comment_post_ID );
 991	$author  = get_userdata( $post->post_author );
 992
 993	// The comment was left by the author
 994	if ( $comment->user_id == $post->post_author )
 995		return false;
 996
 997	// The author moderated a comment on his own post
 998	if ( $post->post_author == get_current_user_id() )
 999		return false;
1000
1001	// If there's no email to send the comment to
1002	if ( '' == $author->user_email )
1003		return false;
1004
1005	$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
1006
1007	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
1008	// we want to reverse this for the plain text arena of emails.
1009	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
1010
1011	if ( empty( $comment_type ) ) $comment_type = 'comment';
1012
1013	if ('comment' == $comment_type) {
1014		$notify_message  = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n";
1015		/* translators: 1: comment author, 2: author IP, 3: author domain */
1016		$notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1017		$notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
1018		$notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
1019		$notify_message .= sprintf( __('Whois  : http://whois.arin.net/rest/ip/%s'), $comment->comment_author_IP ) . "\r\n";
1020		$notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1021		$notify_message .= __('You can see all comments on this post here: ') . "\r\n";
1022		/* translators: 1: blog name, 2: post title */
1023		$subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
1024	} elseif ('trackback' == $comment_type) {
1025		$notify_message  = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n";
1026		/* translators: 1: website name, 2: author IP, 3: author domain */
1027		$notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1028		$notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
1029		$notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1030		$notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
1031		/* translators: 1: blog name, 2: post title */
1032		$subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
1033	} elseif ('pingback' == $comment_type) {
1034		$notify_message  = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n";
1035		/* translators: 1: comment author, 2: author IP, 3: author domain */
1036		$notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1037		$notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
1038		$notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
1039		$notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
1040		/* translators: 1: blog name, 2: post title */
1041		$subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
1042	}
1043	$notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
1044	$notify_message .= sprintf( __('Permalink: %s'), get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment_id ) . "\r\n";
1045	if ( EMPTY_TRASH_DAYS )
1046		$notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
1047	else
1048		$notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n";
1049	$notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n";
1050
1051	$wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
1052
1053	if ( '' == $comment->comment_author ) {
1054		$from = "From: \"$blogname\" <$wp_email>";
1055		if ( '' != $comment->comment_author_email )
1056			$reply_to = "Reply-To: $comment->comment_author_email";
1057	} else {
1058		$from = "From: \"$comment->comment_author\" <$wp_email>";
1059		if ( '' != $comment->comment_author_email )
1060			$reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
1061	}
1062
1063	$message_headers = "$from\n"
1064		. "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
1065
1066	if ( isset($reply_to) )
1067		$message_headers .= $reply_to . "\n";
1068
1069	$notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id);
1070	$subject = apply_filters('comment_notification_subject', $subject, $comment_id);
1071	$message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id);
1072
1073	@wp_mail( $author->user_email, $subject, $notify_message, $message_headers );
1074
1075	return true;
1076}
1077endif;
1078
1079if ( !function_exists('wp_notify_moderator') ) :
1080/**
1081 * Notifies the moderator of the blog about a new comment that is awaiting approval.
1082 *
1083 * @since 1.0
1084 * @uses $wpdb
1085 *
1086 * @param int $comment_id Comment ID
1087 * @return bool Always returns true
1088 */
1089function wp_notify_moderator($comment_id) {
1090	global $wpdb;
1091
1092	if ( 0 == get_option( 'moderation_notify' ) )
1093		return true;
1094
1095	$comment = get_comment($comment_id);
1096	$post = get_post($comment->comment_post_ID);
1097	$user = get_userdata( $post->post_author );
1098	// Send to the administration and to the post author if the author can modify the comment.
1099	$email_to = array( get_option('admin_email') );
1100	if ( user_can($user->ID, 'edit_comment', $comment_id) && !empty($user->user_email) && ( get_option('admin_email') != $user->user_email) )
1101		$email_to[] = $user->user_email;
1102
1103	$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
1104	$comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
1105
1106	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
1107	// we want to reverse this for the plain text arena of emails.
1108	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
1109
1110	switch ($comment->comment_type)
1111	{
1112		case 'trackback':
1113			$notify_message  = sprintf( __('A new trackback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
1114			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
1115			$notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1116			$notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
1117			$notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1118			break;
1119		case 'pingback':
1120			$notify_message  = sprintf( __('A new pingback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
1121			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
1122			$notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1123			$notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
1124			$notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1125			break;
1126		default: //Comments
1127			$notify_message  = sprintf( __('A new comment on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
1128			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
1129			$notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1130			$notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
1131			$notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
1132			$notify_message .= sprintf( __('Whois  : http://whois.arin.net/rest/ip/%s'), $comment->comment_author_IP ) . "\r\n";
1133			$notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1134			break;
1135	}
1136
1137	$notify_message .= sprintf( __('Approve it: %s'),  admin_url("comment.php?action=approve&c=$comment_id") ) . "\r\n";
1138	if ( EMPTY_TRASH_DAYS )
1139		$notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
1140	else
1141		$notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n";
1142	$notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n";
1143
1144	$notify_message .= sprintf( _n('Currently %s comment is waiting for approval. Please visit the moderation panel:',
1145 		'Currently %s comments are waiting for approval. Please visit the moderation panel:', $comments_waiting), number_format_i18n($comments_waiting) ) . "\r\n";
1146	$notify_message .= admin_url("edit-comments.php?comment_status=moderated") . "\r\n";
1147
1148	$subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), $blogname, $post->post_title );
1149	$message_headers = '';
1150
1151	$notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id);
1152	$subject = apply_filters('comment_moderation_subject', $subject, $comment_id);
1153	$message_headers = apply_filters('comment_moderation_headers', $message_headers);
1154
1155	foreach ( $email_to as $email )
1156		@wp_mail($email, $subject, $notify_message, $message_headers);
1157
1158	return true;
1159}
1160endif;
1161
1162if ( !function_exists('wp_password_change_notification') ) :
1163/**
1164 * Notify the blog admin of a user changing password, normally via email.
1165 *
1166 * @since 2.7
1167 *
1168 * @param object $user User Object
1169 */
1170function wp_password_change_notification(&$user) {
1171	// send a copy of password change notification to the admin
1172	// but check to see if it's the admin whose password we're changing, and skip this
1173	if ( $user->user_email != get_option('admin_email') ) {
1174		$message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n";
1175		// The blogname option is escaped with esc_html on the way into the database in sanitize_option
1176		// we want to reverse this for the plain text arena of emails.
1177		$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
1178		wp_mail(get_option('admin_email'), sprintf(__('[%s] Password Lost/Changed'), $blogname), $message);
1179	}
1180}
1181endif;
1182
1183if ( !function_exists('wp_new_user_notification') ) :
1184/**
1185 * Notify the blog admin of a new user, normally via email.
1186 *
1187 * @since 2.0
1188 *
1189 * @param int $user_id User ID
1190 * @param string $plaintext_pass Optional. The user's plaintext password
1191 */
1192function wp_new_user_notification($user_id, $plaintext_pass = '') {
1193	$user = get_userdata( $user_id );
1194
1195	$user_login = stripslashes($user->user_login);
1196	$user_email = stripslashes($user->user_email);
1197
1198	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
1199	// we want to reverse this for the plain text arena of emails.
1200	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
1201
1202	$message  = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
1203	$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
1204	$message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
1205
1206	@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);
1207
1208	if ( empty($plaintext_pass) )
1209		return;
1210
1211	$message  = sprintf(__('Username: %s'), $user_login) . "\r\n";
1212	$message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
1213	$message .= wp_login_url() . "\r\n";
1214
1215	wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
1216
1217}
1218endif;
1219
1220if ( !function_exists('wp_nonce_tick') ) :
1221/**
1222 * Get the time-dependent variable for nonce creation.
1223 *
1224 * A nonce has a lifespan of two ticks. Nonces in their second tick may be
1225 * updated, e.g. by autosave.
1226 *
1227 * @since 2.5
1228 *
1229 * @return int
1230 */
1231function wp_nonce_tick() {
1232	$nonce_life = apply_filters( 'nonce_life', DAY_IN_SECONDS );
1233
1234	return ceil(time() / ( $nonce_life / 2 ));
1235}
1236endif;
1237
1238if ( !function_exists('wp_verify_nonce') ) :
1239/**
1240 * Verify that correct nonce was used with time limit.
1241 *
1242 * The user is given an amount of time to use the token, so therefore, since the
1243 * UID and $action remain the same, the independent variable is the time.
1244 *
1245 * @since 2.0.3
1246 *
1247 * @param string $nonce Nonce that was used in the form to verify
1248 * @param string|int $action Should give context to what is taking place and be the same when nonce was created.
1249 * @return bool Whether the nonce check passed or failed.
1250 */
1251function wp_verify_nonce($nonce, $action = -1) {
1252	$user = wp_get_current_user();
1253	$uid = (int) $user->ID;
1254	if ( ! $uid )
1255		$uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
1256
1257	$i = wp_nonce_tick();
1258
1259	// Nonce generated 0-12 hours ago
1260	if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) == $nonce )
1261		return 1;
1262	// Nonce generated 12-24 hours ago
1263	if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) == $nonce )
1264		return 2;
1265	// Invalid nonce
1266	return false;
1267}
1268endif;
1269
1270if ( !function_exists('wp_create_nonce') ) :
1271/**
1272 * Creates a random, one time use token.
1273 *
1274 * @since 2.0.3
1275 *
1276 * @param string|int $action Scalar value to add context to the nonce.
1277 * @return string The one use form token
1278 */
1279function wp_create_nonce($action = -1) {
1280	$user = wp_get_current_user();
1281	$uid = (int) $user->ID;
1282	if ( ! $uid )
1283		$uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
1284
1285	$i = wp_nonce_tick();
1286
1287	return substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10);
1288}
1289endif;
1290
1291if ( !function_exists('wp_salt') ) :
1292/**
1293 * Get salt to add to hashes.
1294 *
1295 * Salts are created using secret keys. Secret keys are located in two places:
1296 * in the database and in the wp-config.php file. The secret key in the database
1297 * is randomly generated and will be appended to the secret keys in wp-config.php.
1298 *
1299 * The secret keys in wp-config.php should be updated to strong, random keys to maximize
1300 * security. Below is an example of how the secret key constants are defined.
1301 * Do not paste this example directly into wp-config.php. Instead, have a
1302 * {@link https://api.wordpress.org/secret-key/1.1/salt/ secret key created} just
1303 * for you.
1304 *
1305 * <code>
1306 * define('AUTH_KEY',         ' Xakm<o xQy rw4EMsLKM-?!T+,PFF})H4lzcW57AF0U@N@< >M%G4Yt>f`z]MON');
1307 * define('SECURE_AUTH_KEY',  'LzJ}op]mr|6+![P}Ak:uNdJCJZd>(Hx.-Mh#Tz)pCIU#uGEnfFz|f ;;eU%/U^O~');
1308 * define('LOGGED_IN_KEY',    '|i|Ux`9<p-h$aFf(qnT:sDO:D1P^wZ$$/Ra@miTJi9G;ddp_<q}6H1)o|a +&JCM');
1309 * define('NONCE_KEY',        '%:R{[P|,s.KuMltH5}cI;/k<Gx~j!f0I)m_sIyu+&NJZ)-iO>z7X>QYR0Z_XnZ@|');
1310 * define('AUTH_SALT',        'eZyT)-Naw]F8CwA*VaW#q*|.)g@o}||wf~@C-YSt}(dh_r6EbI#A,y|nU2{B#JBW');
1311 * define('SECURE_AUTH_SALT', '!=oLUTXh,QW=H `}`L|9/^4-3 STz},T(w}W<I`.JjPi)<Bmf1v,HpGe}T1:Xt7n');
1312 * define('LOGGED_IN_SALT',   '+XSqHc;@Q*K_b|Z?NC[3H!!EONbh.n<+=uKR:>*c(u`g~EJBf#8u#R{mUEZrozmm');
1313 * define('NONCE_SALT',       'h`GXHhD>SLWVfg1(1(N{;.V!MoE(SfbA_ksP@&`+AycHcAV$+?@3q+rxV{%^VyKT');
1314 * </code>
1315 *
1316 * Salting passwords helps against tools which has stored hashed values of
1317 * common dictionary strings. The added values makes it harder to crack.
1318 *
1319 * @since 2.5
1320 *
1321 * @link https://api.wordpress.org/secret-key/1.1/salt/ Create secrets for wp-config.php
1322 *
1323 * @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce)
1324 * @return string Salt value
1325 */
1326function wp_salt( $scheme = 'auth' ) {
1327	static $cached_salts = array();
1328	if ( isset( $cached_salts[ $scheme ] ) )
1329		return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme );
1330
1331	static $duplicated_keys;
1332	if ( null === $duplicated_keys ) {
1333		$duplicated_keys = array( 'put your unique phrase here' => true );
1334		foreach ( array( 'AUTH', 'SECURE_AUTH', 'LOGGED_IN', 'NONCE', 'SECRET' ) as $first ) {
1335			foreach ( array( 'KEY', 'SALT' ) as $second ) {
1336				if ( ! defined( "{$first}_{$second}" ) )
1337					continue;
1338				$value = constant( "{$first}_{$second}" );
1339				$duplicated_keys[ $value ] = isset( $duplicated_keys[ $value ] );
1340			}
1341		}
1342	}
1343
1344	$key = $salt = '';
1345	if ( defined( 'SECRET_KEY' ) && SECRET_KEY && empty( $duplicated_keys[ SECRET_KEY ] ) )
1346		$key = SECRET_KEY;
1347	if ( 'auth' == $scheme && defined( 'SECRET_SALT' ) && SECRET_SALT && empty( $duplicated_keys[ SECRET_SALT ] ) )
1348		$salt = SECRET_SALT;
1349
1350	if ( in_array( $scheme, array( 'auth', 'secure_auth', 'logged_in', 'nonce' ) ) ) {
1351		foreach ( array( 'key', 'salt' ) as $type ) {
1352			$const = strtoupper( "{$scheme}_{$type}" );
1353			if ( defined( $const ) && constant( $const ) && empty( $duplicated_keys[ constant( $const ) ] ) ) {
1354				$$type = constant( $const );
1355			} elseif ( ! $$type ) {
1356				$$type = get_site_option( "{$scheme}_{$type}" );
1357				if ( ! $$type ) {
1358					$$type = wp_generate_password( 64, true, true );
1359					update_site_option( "{$scheme}_{$type}", $$type );
1360				}
1361			}
1362		}
1363	} else {
1364		if ( ! $key ) {
1365			$key = get_site_option( 'secret_key' );
1366			if ( ! $key ) {
1367				$key = wp_generate_password( 64, true, true );
1368				update_site_option( 'secret_key', $key );
1369			}
1370		}
1371		$salt = hash_hmac( 'md5', $scheme, $key );
1372	}
1373
1374	$cached_salts[ $scheme ] = $key . $salt;
1375	return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme );
1376}
1377endif;
1378
1379if ( !function_exists('wp_hash') ) :
1380/**
1381 * Get hash of given string.
1382 *
1383 * @since 2.0.3
1384 * @uses wp_salt() Get WordPress salt
1385 *
1386 * @param string $data Plain text to hash
1387 * @return string Hash of $data
1388 */
1389function wp_hash($data, $scheme = 'auth') {
1390	$salt = wp_salt($scheme);
1391
1392	return hash_hmac('md5', $data, $salt);
1393}
1394endif;
1395
1396if ( !function_exists('wp_hash_password') ) :
1397/**
1398 * Create a hash (encrypt) of a plain text password.
1399 *
1400 * For integration with other applications, this function can be overwritten to
1401 * instead use the other package password checking algorithm.
1402 *
1403 * @since 2.5
1404 * @global object $wp_hasher PHPass object
1405 * @uses PasswordHash::HashPassword
1406 *
1407 * @param string $password Plain text user password to hash
1408 * @return string The hash string of the password
1409 */
1410function wp_hash_password($password) {
1411	global $wp_hasher;
1412
1413	if ( empty($wp_hasher) ) {
1414		require_once( ABSPATH . 'wp-includes/class-phpass.php');
1415		// By default, use the portable hash from phpass
1416		$wp_hasher = new PasswordHash(8, true);
1417	}
1418
1419	return $wp_hasher->HashPassword($password);
1420}
1421endif;
1422
1423if ( !function_exists('wp_check_password') ) :
1424/**
1425 * Checks the plaintext password against the encrypted Password.
1426 *
1427 * Maintains compatibility between old version and the new cookie authentication
1428 * protocol using PHPass library. The $hash parameter is the encrypted password
1429 * and the function compares the plain text password when encrypted similarly
1430 * against the already encrypted password to see if they match.
1431 *
1432 * For integration with other applications, this function can be overwritten to
1433 * instead use the other package password checking algorithm.
1434 *
1435 * @since 2.5
1436 * @global object $wp_hasher PHPass object used for checking the password
1437 *	against the $hash + $password
1438 * @uses PasswordHash::CheckPassword
1439 *
1440 * @param string $password Plaintext user's pa…

Large files files are truncated, but you can click here to view the full file