WordPress 3.0 beta 1 documentation kindly provided to you by Hay Kranen
| [ Index ] |
PHP Cross Reference of WordPress 3.0 beta 1 |
|
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
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('cache_users') ) : 143 /** 144 * Retrieve info for user lists to prevent multiple queries by get_userdata() 145 * 146 * @since 3.0.0 147 * 148 * @param array $users User ID numbers list 149 */ 150 function cache_users( $users ) { 151 global $wpdb; 152 153 $clean = array(); 154 foreach($users as $id) { 155 $id = (int) $id; 156 if (wp_cache_get($id, 'users')) { 157 // seems to be cached already 158 } else { 159 $clean[] = $id; 160 } 161 } 162 163 if ( 0 == count($clean) ) 164 return; 165 166 $list = implode(',', $clean); 167 168 $results = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE ID IN ($list)"); 169 170 _fill_many_users($results); 171 } 172 endif; 173 174 if ( !function_exists('get_user_by') ) : 175 /** 176 * Retrieve user info by a given field 177 * 178 * @since 2.8.0 179 * 180 * @param string $field The field to retrieve the user with. id | slug | email | login 181 * @param int|string $value A value for $field. A user ID, slug, email address, or login name. 182 * @return bool|object False on failure, User DB row object 183 */ 184 function get_user_by($field, $value) { 185 global $wpdb; 186 187 switch ($field) { 188 case 'id': 189 return get_userdata($value); 190 break; 191 case 'slug': 192 $user_id = wp_cache_get($value, 'userslugs'); 193 $field = 'user_nicename'; 194 break; 195 case 'email': 196 $user_id = wp_cache_get($value, 'useremail'); 197 $field = 'user_email'; 198 break; 199 case 'login': 200 $value = sanitize_user( $value ); 201 $user_id = wp_cache_get($value, 'userlogins'); 202 $field = 'user_login'; 203 break; 204 default: 205 return false; 206 } 207 208 if ( false !== $user_id ) 209 return get_userdata($user_id); 210 211 if ( !$user = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->users WHERE $field = %s", $value) ) ) 212 return false; 213 214 _fill_user($user); 215 216 return $user; 217 } 218 endif; 219 220 if ( !function_exists('get_userdatabylogin') ) : 221 /** 222 * Retrieve user info by login name. 223 * 224 * @since 0.71 225 * 226 * @param string $user_login User's username 227 * @return bool|object False on failure, User DB row object 228 */ 229 function get_userdatabylogin($user_login) { 230 return get_user_by('login', $user_login); 231 } 232 endif; 233 234 if ( !function_exists('get_user_by_email') ) : 235 /** 236 * Retrieve user info by email. 237 * 238 * @since 2.5 239 * 240 * @param string $email User's email address 241 * @return bool|object False on failure, User DB row object 242 */ 243 function get_user_by_email($email) { 244 return get_user_by('email', $email); 245 } 246 endif; 247 248 if ( !function_exists( 'wp_mail' ) ) : 249 /** 250 * Send mail, similar to PHP's mail 251 * 252 * A true return value does not automatically mean that the user received the 253 * email successfully. It just only means that the method used was able to 254 * process the request without any errors. 255 * 256 * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from 257 * creating a from address like 'Name <email@address.com>' when both are set. If 258 * just 'wp_mail_from' is set, then just the email address will be used with no 259 * name. 260 * 261 * The default content type is 'text/plain' which does not allow using HTML. 262 * However, you can set the content type of the email by using the 263 * 'wp_mail_content_type' filter. 264 * 265 * The default charset is based on the charset used on the blog. The charset can 266 * be set using the 'wp_mail_charset' filter. 267 * 268 * @since 1.2.1 269 * @uses apply_filters() Calls 'wp_mail' hook on an array of all of the parameters. 270 * @uses apply_filters() Calls 'wp_mail_from' hook to get the from email address. 271 * @uses apply_filters() Calls 'wp_mail_from_name' hook to get the from address name. 272 * @uses apply_filters() Calls 'wp_mail_content_type' hook to get the email content type. 273 * @uses apply_filters() Calls 'wp_mail_charset' hook to get the email charset 274 * @uses do_action_ref_array() Calls 'phpmailer_init' hook on the reference to 275 * phpmailer object. 276 * @uses PHPMailer 277 * @ 278 * 279 * @param string|array $to Array or comma-separated list of email addresses to send message. 280 * @param string $subject Email subject 281 * @param string $message Message contents 282 * @param string|array $headers Optional. Additional headers. 283 * @param string|array $attachments Optional. Files to attach. 284 * @return bool Whether the email contents were sent successfully. 285 */ 286 function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { 287 // Compact the input, apply the filters, and extract them back out 288 extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) ); 289 290 if ( !is_array($attachments) ) 291 $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) ); 292 293 global $phpmailer; 294 295 // (Re)create it, if it's gone missing 296 if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) { 297 require_once ABSPATH . WPINC . '/class-phpmailer.php'; 298 require_once ABSPATH . WPINC . '/class-smtp.php'; 299 $phpmailer = new PHPMailer(); 300 } 301 302 // Headers 303 if ( empty( $headers ) ) { 304 $headers = array(); 305 } else { 306 if ( !is_array( $headers ) ) { 307 // Explode the headers out, so this function can take both 308 // string headers and an array of headers. 309 $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) ); 310 } else { 311 $tempheaders = $headers; 312 } 313 $headers = array(); 314 315 // If it's actually got contents 316 if ( !empty( $tempheaders ) ) { 317 // Iterate through the raw headers 318 foreach ( (array) $tempheaders as $header ) { 319 if ( strpos($header, ':') === false ) { 320 if ( false !== stripos( $header, 'boundary=' ) ) { 321 $parts = preg_split('/boundary=/i', trim( $header ) ); 322 $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) ); 323 } 324 continue; 325 } 326 // Explode them out 327 list( $name, $content ) = explode( ':', trim( $header ), 2 ); 328 329 // Cleanup crew 330 $name = trim( $name ); 331 $content = trim( $content ); 332 333 switch ( strtolower( $name ) ) { 334 // Mainly for legacy -- process a From: header if it's there 335 case 'from': 336 if ( strpos($content, '<' ) !== false ) { 337 // So... making my life hard again? 338 $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 ); 339 $from_name = str_replace( '"', '', $from_name ); 340 $from_name = trim( $from_name ); 341 342 $from_email = substr( $content, strpos( $content, '<' ) + 1 ); 343 $from_email = str_replace( '>', '', $from_email ); 344 $from_email = trim( $from_email ); 345 } else { 346 $from_email = trim( $content ); 347 } 348 break; 349 case 'content-type': 350 if ( strpos( $content, ';' ) !== false ) { 351 list( $type, $charset ) = explode( ';', $content ); 352 $content_type = trim( $type ); 353 if ( false !== stripos( $charset, 'charset=' ) ) { 354 $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) ); 355 } elseif ( false !== stripos( $charset, 'boundary=' ) ) { 356 $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) ); 357 $charset = ''; 358 } 359 } else { 360 $content_type = trim( $content ); 361 } 362 break; 363 case 'cc': 364 $cc = array_merge( (array) $cc, explode( ',', $content ) ); 365 break; 366 case 'bcc': 367 $bcc = array_merge( (array) $bcc, explode( ',', $content ) ); 368 break; 369 default: 370 // Add it to our grand headers array 371 $headers[trim( $name )] = trim( $content ); 372 break; 373 } 374 } 375 } 376 } 377 378 // Empty out the values that may be set 379 $phpmailer->ClearAddresses(); 380 $phpmailer->ClearAllRecipients(); 381 $phpmailer->ClearAttachments(); 382 $phpmailer->ClearBCCs(); 383 $phpmailer->ClearCCs(); 384 $phpmailer->ClearCustomHeaders(); 385 $phpmailer->ClearReplyTos(); 386 387 // From email and name 388 // If we don't have a name from the input headers 389 if ( !isset( $from_name ) ) 390 $from_name = 'WordPress'; 391 392 /* If we don't have an email from the input headers default to wordpress@$sitename 393 * Some hosts will block outgoing mail from this address if it doesn't exist but 394 * there's no easy alternative. Defaulting to admin_email might appear to be another 395 * option but some hosts may refuse to relay mail from an unknown domain. See 396 * http://trac.wordpress.org/ticket/5007. 397 */ 398 399 if ( !isset( $from_email ) ) { 400 // Get the site domain and get rid of www. 401 $sitename = strtolower( $_SERVER['SERVER_NAME'] ); 402 if ( substr( $sitename, 0, 4 ) == 'www.' ) { 403 $sitename = substr( $sitename, 4 ); 404 } 405 406 $from_email = 'wordpress@' . $sitename; 407 } 408 409 // Plugin authors can override the potentially troublesome default 410 $phpmailer->From = apply_filters( 'wp_mail_from' , $from_email ); 411 $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name ); 412 413 // Set destination addresses 414 if ( !is_array( $to ) ) 415 $to = explode( ',', $to ); 416 417 foreach ( (array) $to as $recipient ) { 418 $phpmailer->AddAddress( trim( $recipient ) ); 419 } 420 421 // Set mail's subject and body 422 $phpmailer->Subject = $subject; 423 $phpmailer->Body = $message; 424 425 // Add any CC and BCC recipients 426 if ( !empty( $cc ) ) { 427 foreach ( (array) $cc as $recipient ) { 428 $phpmailer->AddCc( trim($recipient) ); 429 } 430 } 431 432 if ( !empty( $bcc ) ) { 433 foreach ( (array) $bcc as $recipient) { 434 $phpmailer->AddBcc( trim($recipient) ); 435 } 436 } 437 438 // Set to use PHP's mail() 439 $phpmailer->IsMail(); 440 441 // Set Content-Type and charset 442 // If we don't have a content-type from the input headers 443 if ( !isset( $content_type ) ) 444 $content_type = 'text/plain'; 445 446 $content_type = apply_filters( 'wp_mail_content_type', $content_type ); 447 448 $phpmailer->ContentType = $content_type; 449 450 // Set whether it's plaintext, depending on $content_type 451 if ( 'text/html' == $content_type ) 452 $phpmailer->IsHTML( true ); 453 454 // If we don't have a charset from the input headers 455 if ( !isset( $charset ) ) 456 $charset = get_bloginfo( 'charset' ); 457 458 // Set the content-type and charset 459 $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset ); 460 461 // Set custom headers 462 if ( !empty( $headers ) ) { 463 foreach( (array) $headers as $name => $content ) { 464 $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) ); 465 } 466 467 if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) ) 468 $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) ); 469 } 470 471 if ( !empty( $attachments ) ) { 472 foreach ( $attachments as $attachment ) { 473 $phpmailer->AddAttachment($attachment); 474 } 475 } 476 477 do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) ); 478 479 // Send! 480 $result = @$phpmailer->Send(); 481 482 return $result; 483 } 484 endif; 485 486 if ( !function_exists('wp_authenticate') ) : 487 /** 488 * Checks a user's login information and logs them in if it checks out. 489 * 490 * @since 2.5.0 491 * 492 * @param string $username User's username 493 * @param string $password User's password 494 * @return WP_Error|WP_User WP_User object if login successful, otherwise WP_Error object. 495 */ 496 function wp_authenticate($username, $password) { 497 $username = sanitize_user($username); 498 $password = trim($password); 499 500 $user = apply_filters('authenticate', null, $username, $password); 501 502 if ( $user == null ) { 503 // TODO what should the error message be? (Or would these even happen?) 504 // Only needed if all authentication handlers fail to return anything. 505 $user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.')); 506 } 507 508 $ignore_codes = array('empty_username', 'empty_password'); 509 510 if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) { 511 do_action('wp_login_failed', $username); 512 } 513 514 return $user; 515 } 516 endif; 517 518 if ( !function_exists('wp_logout') ) : 519 /** 520 * Log the current user out. 521 * 522 * @since 2.5.0 523 */ 524 function wp_logout() { 525 wp_clear_auth_cookie(); 526 do_action('wp_logout'); 527 } 528 endif; 529 530 if ( !function_exists('wp_validate_auth_cookie') ) : 531 /** 532 * Validates authentication cookie. 533 * 534 * The checks include making sure that the authentication cookie is set and 535 * pulling in the contents (if $cookie is not used). 536 * 537 * Makes sure the cookie is not expired. Verifies the hash in cookie is what is 538 * should be and compares the two. 539 * 540 * @since 2.5 541 * 542 * @param string $cookie Optional. If used, will validate contents instead of cookie's 543 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in 544 * @return bool|int False if invalid cookie, User ID if valid. 545 */ 546 function wp_validate_auth_cookie($cookie = '', $scheme = '') { 547 if ( ! $cookie_elements = wp_parse_auth_cookie($cookie, $scheme) ) { 548 do_action('auth_cookie_malformed', $cookie, $scheme); 549 return false; 550 } 551 552 extract($cookie_elements, EXTR_OVERWRITE); 553 554 $expired = $expiration; 555 556 // Allow a grace period for POST and AJAX requests 557 if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] ) 558 $expired += 3600; 559 560 // Quick check to see if an honest cookie has expired 561 if ( $expired < time() ) { 562 do_action('auth_cookie_expired', $cookie_elements); 563 return false; 564 } 565 566 $user = get_userdatabylogin($username); 567 if ( ! $user ) { 568 do_action('auth_cookie_bad_username', $cookie_elements); 569 return false; 570 } 571 572 $pass_frag = substr($user->user_pass, 8, 4); 573 574 $key = wp_hash($username . $pass_frag . '|' . $expiration, $scheme); 575 $hash = hash_hmac('md5', $username . '|' . $expiration, $key); 576 577 if ( $hmac != $hash ) { 578 do_action('auth_cookie_bad_hash', $cookie_elements); 579 return false; 580 } 581 582 if ( $expiration < time() ) // AJAX/POST grace period set above 583 $GLOBALS['login_grace_period'] = 1; 584 585 do_action('auth_cookie_valid', $cookie_elements, $user); 586 587 return $user->ID; 588 } 589 endif; 590 591 if ( !function_exists('wp_generate_auth_cookie') ) : 592 /** 593 * Generate authentication cookie contents. 594 * 595 * @since 2.5 596 * @uses apply_filters() Calls 'auth_cookie' hook on $cookie contents, User ID 597 * and expiration of cookie. 598 * 599 * @param int $user_id User ID 600 * @param int $expiration Cookie expiration in seconds 601 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in 602 * @return string Authentication cookie contents 603 */ 604 function wp_generate_auth_cookie($user_id, $expiration, $scheme = 'auth') { 605 $user = get_userdata($user_id); 606 607 $pass_frag = substr($user->user_pass, 8, 4); 608 609 $key = wp_hash($user->user_login . $pass_frag . '|' . $expiration, $scheme); 610 $hash = hash_hmac('md5', $user->user_login . '|' . $expiration, $key); 611 612 $cookie = $user->user_login . '|' . $expiration . '|' . $hash; 613 614 return apply_filters('auth_cookie', $cookie, $user_id, $expiration, $scheme); 615 } 616 endif; 617 618 if ( !function_exists('wp_parse_auth_cookie') ) : 619 /** 620 * Parse a cookie into its components 621 * 622 * @since 2.7 623 * 624 * @param string $cookie 625 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in 626 * @return array Authentication cookie components 627 */ 628 function wp_parse_auth_cookie($cookie = '', $scheme = '') { 629 if ( empty($cookie) ) { 630 switch ($scheme){ 631 case 'auth': 632 $cookie_name = AUTH_COOKIE; 633 break; 634 case 'secure_auth': 635 $cookie_name = SECURE_AUTH_COOKIE; 636 break; 637 case "logged_in": 638 $cookie_name = LOGGED_IN_COOKIE; 639 break; 640 default: 641 if ( is_ssl() ) { 642 $cookie_name = SECURE_AUTH_COOKIE; 643 $scheme = 'secure_auth'; 644 } else { 645 $cookie_name = AUTH_COOKIE; 646 $scheme = 'auth'; 647 } 648 } 649 650 if ( empty($_COOKIE[$cookie_name]) ) 651 return false; 652 $cookie = $_COOKIE[$cookie_name]; 653 } 654 655 $cookie_elements = explode('|', $cookie); 656 if ( count($cookie_elements) != 3 ) 657 return false; 658 659 list($username, $expiration, $hmac) = $cookie_elements; 660 661 return compact('username', 'expiration', 'hmac', 'scheme'); 662 } 663 endif; 664 665 if ( !function_exists('wp_set_auth_cookie') ) : 666 /** 667 * Sets the authentication cookies based User ID. 668 * 669 * The $remember parameter increases the time that the cookie will be kept. The 670 * default the cookie is kept without remembering is two days. When $remember is 671 * set, the cookies will be kept for 14 days or two weeks. 672 * 673 * @since 2.5 674 * 675 * @param int $user_id User ID 676 * @param bool $remember Whether to remember the user 677 */ 678 function wp_set_auth_cookie($user_id, $remember = false, $secure = '') { 679 if ( $remember ) { 680 $expiration = $expire = time() + apply_filters('auth_cookie_expiration', 1209600, $user_id, $remember); 681 } else { 682 $expiration = time() + apply_filters('auth_cookie_expiration', 172800, $user_id, $remember); 683 $expire = 0; 684 } 685 686 if ( '' === $secure ) 687 $secure = is_ssl(); 688 689 if ( $secure ) { 690 $auth_cookie_name = SECURE_AUTH_COOKIE; 691 $scheme = 'secure_auth'; 692 } else { 693 $auth_cookie_name = AUTH_COOKIE; 694 $scheme = 'auth'; 695 } 696 697 $auth_cookie = wp_generate_auth_cookie($user_id, $expiration, $scheme); 698 $logged_in_cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in'); 699 700 do_action('set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme); 701 do_action('set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in'); 702 703 // Set httponly if the php version is >= 5.2.0 704 if ( version_compare(phpversion(), '5.2.0', 'ge') ) { 705 setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true); 706 setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true); 707 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, false, true); 708 if ( COOKIEPATH != SITECOOKIEPATH ) 709 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, false, true); 710 } else { 711 $cookie_domain = COOKIE_DOMAIN; 712 if ( !empty($cookie_domain) ) 713 $cookie_domain .= '; HttpOnly'; 714 setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, $cookie_domain, $secure); 715 setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, $cookie_domain, $secure); 716 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, $cookie_domain); 717 if ( COOKIEPATH != SITECOOKIEPATH ) 718 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, $cookie_domain); 719 } 720 } 721 endif; 722 723 if ( !function_exists('wp_clear_auth_cookie') ) : 724 /** 725 * Removes all of the cookies associated with authentication. 726 * 727 * @since 2.5 728 */ 729 function wp_clear_auth_cookie() { 730 do_action('clear_auth_cookie'); 731 732 setcookie(AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN); 733 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN); 734 setcookie(AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN); 735 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN); 736 setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN); 737 setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN); 738 739 // Old cookies 740 setcookie(AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN); 741 setcookie(AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN); 742 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN); 743 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN); 744 745 // Even older cookies 746 setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN); 747 setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN); 748 setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN); 749 setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN); 750 } 751 endif; 752 753 if ( !function_exists('is_user_logged_in') ) : 754 /** 755 * Checks if the current visitor is a logged in user. 756 * 757 * @since 2.0.0 758 * 759 * @return bool True if user is logged in, false if not logged in. 760 */ 761 function is_user_logged_in() { 762 $user = wp_get_current_user(); 763 764 if ( $user->id == 0 ) 765 return false; 766 767 return true; 768 } 769 endif; 770 771 if ( !function_exists('auth_redirect') ) : 772 /** 773 * Checks if a user is logged in, if not it redirects them to the login page. 774 * 775 * @since 1.5 776 */ 777 function auth_redirect() { 778 // Checks if a user is logged in, if not redirects them to the login page 779 780 $secure = ( is_ssl() || force_ssl_admin() ); 781 782 // If https is required and request is http, redirect 783 if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) { 784 if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) { 785 wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI'])); 786 exit(); 787 } else { 788 wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); 789 exit(); 790 } 791 } 792 793 if ( $user_id = wp_validate_auth_cookie( '', apply_filters( 'auth_redirect_scheme', '' ) ) ) { 794 do_action('auth_redirect', $user_id); 795 796 // If the user wants ssl but the session is not ssl, redirect. 797 if ( !$secure && get_user_option('use_ssl', $user_id) && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) { 798 if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) { 799 wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI'])); 800 exit(); 801 } else { 802 wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); 803 exit(); 804 } 805 } 806 807 return; // The cookie is good so we're done 808 } 809 810 // The cookie is no good so force login 811 nocache_headers(); 812 813 if ( is_ssl() ) 814 $proto = 'https://'; 815 else 816 $proto = 'http://'; 817 818 $redirect = ( strpos($_SERVER['REQUEST_URI'], '/options.php') && wp_get_referer() ) ? wp_get_referer() : $proto . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 819 820 $login_url = wp_login_url($redirect); 821 822 wp_redirect($login_url); 823 exit(); 824 } 825 endif; 826 827 if ( !function_exists('check_admin_referer') ) : 828 /** 829 * Makes sure that a user was referred from another admin page. 830 * 831 * To avoid security exploits. 832 * 833 * @since 1.2.0 834 * @uses do_action() Calls 'check_admin_referer' on $action. 835 * 836 * @param string $action Action nonce 837 * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5) 838 */ 839 function check_admin_referer($action = -1, $query_arg = '_wpnonce') { 840 $adminurl = strtolower(admin_url()); 841 $referer = strtolower(wp_get_referer()); 842 $result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce($_REQUEST[$query_arg], $action) : false; 843 if ( !$result && !(-1 == $action && strpos($referer, $adminurl) !== false) ) { 844 wp_nonce_ays($action); 845 die(); 846 } 847 do_action('check_admin_referer', $action, $result); 848 return $result; 849 }endif; 850 851 if ( !function_exists('check_ajax_referer') ) : 852 /** 853 * Verifies the AJAX request to prevent processing requests external of the blog. 854 * 855 * @since 2.0.3 856 * 857 * @param string $action Action nonce 858 * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5) 859 */ 860 function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) { 861 if ( $query_arg ) 862 $nonce = $_REQUEST[$query_arg]; 863 else 864 $nonce = isset($_REQUEST['_ajax_nonce']) ? $_REQUEST['_ajax_nonce'] : $_REQUEST['_wpnonce']; 865 866 $result = wp_verify_nonce( $nonce, $action ); 867 868 if ( $die && false == $result ) 869 die('-1'); 870 871 do_action('check_ajax_referer', $action, $result); 872 873 return $result; 874 } 875 endif; 876 877 if ( !function_exists('wp_redirect') ) : 878 /** 879 * Redirects to another page, with a workaround for the IIS Set-Cookie bug. 880 * 881 * @link http://support.microsoft.com/kb/q176113/ 882 * @since 1.5.1 883 * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status. 884 * 885 * @param string $location The path to redirect to 886 * @param int $status Status code to use 887 * @return bool False if $location is not set 888 */ 889 function wp_redirect($location, $status = 302) { 890 global $is_IIS; 891 892 $location = apply_filters('wp_redirect', $location, $status); 893 $status = apply_filters('wp_redirect_status', $status, $location); 894 895 if ( !$location ) // allows the wp_redirect filter to cancel a redirect 896 return false; 897 898 $location = wp_sanitize_redirect($location); 899 900 if ( $is_IIS ) { 901 header("Refresh: 0;url=$location"); 902 } else { 903 if ( php_sapi_name() != 'cgi-fcgi' ) 904 status_header($status); // This causes problems on IIS and some FastCGI setups 905 header("Location: $location", true, $status); 906 } 907 } 908 endif; 909 910 if ( !function_exists('wp_sanitize_redirect') ) : 911 /** 912 * Sanitizes a URL for use in a redirect. 913 * 914 * @since 2.3 915 * 916 * @return string redirect-sanitized URL 917 **/ 918 function wp_sanitize_redirect($location) { 919 $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%!]|i', '', $location); 920 $location = wp_kses_no_null($location); 921 922 // remove %0d and %0a from location 923 $strip = array('%0d', '%0a', '%0D', '%0A'); 924 $location = _deep_replace($strip, $location); 925 return $location; 926 } 927 endif; 928 929 if ( !function_exists('wp_safe_redirect') ) : 930 /** 931 * Performs a safe (local) redirect, using wp_redirect(). 932 * 933 * Checks whether the $location is using an allowed host, if it has an absolute 934 * path. A plugin can therefore set or remove allowed host(s) to or from the 935 * list. 936 * 937 * If the host is not allowed, then the redirect is to wp-admin on the siteurl 938 * instead. This prevents malicious redirects which redirect to another host, 939 * but only used in a few places. 940 * 941 * @since 2.3 942 * @uses wp_validate_redirect() To validate the redirect is to an allowed host. 943 * 944 * @return void Does not return anything 945 **/ 946 function wp_safe_redirect($location, $status = 302) { 947 948 // Need to look at the URL the way it will end up in wp_redirect() 949 $location = wp_sanitize_redirect($location); 950 951 $location = wp_validate_redirect($location, admin_url()); 952 953 wp_redirect($location, $status); 954 } 955 endif; 956 957 if ( !function_exists('wp_validate_redirect') ) : 958 /** 959 * Validates a URL for use in a redirect. 960 * 961 * Checks whether the $location is using an allowed host, if it has an absolute 962 * path. A plugin can therefore set or remove allowed host(s) to or from the 963 * list. 964 * 965 * If the host is not allowed, then the redirect is to $default supplied 966 * 967 * @since 2.8.1 968 * @uses apply_filters() Calls 'allowed_redirect_hosts' on an array containing 969 * WordPress host string and $location host string. 970 * 971 * @param string $location The redirect to validate 972 * @param string $default The value to return is $location is not allowed 973 * @return string redirect-sanitized URL 974 **/ 975 function wp_validate_redirect($location, $default = '') { 976 // browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//' 977 if ( substr($location, 0, 2) == '//' ) 978 $location = 'http:' . $location; 979 980 // In php 5 parse_url may fail if the URL query part contains http://, bug #38143 981 $test = ( $cut = strpos($location, '?') ) ? substr( $location, 0, $cut ) : $location; 982 983 $lp = parse_url($test); 984 985 // Give up if malformed URL 986 if ( false === $lp ) 987 return $default; 988 989 // Allow only http and https schemes. No data:, etc. 990 if ( isset($lp['scheme']) && !('http' == $lp['scheme'] || 'https' == $lp['scheme']) ) 991 return $default; 992 993 // 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. 994 if ( isset($lp['scheme']) && !isset($lp['host']) ) 995 return $default; 996 997 $wpp = parse_url(home_url()); 998 999 $allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']), isset($lp['host']) ? $lp['host'] : ''); 1000 1001 if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) ) 1002 $location = $default; 1003 1004 return $location; 1005 } 1006 endif; 1007 1008 if ( ! function_exists('wp_notify_postauthor') ) : 1009 /** 1010 * Notify an author of a comment/trackback/pingback to one of their posts. 1011 * 1012 * @since 1.0.0 1013 * 1014 * @param int $comment_id Comment ID 1015 * @param string $comment_type Optional. The comment type either 'comment' (default), 'trackback', or 'pingback' 1016 * @return bool False if user email does not exist. True on completion. 1017 */ 1018 function wp_notify_postauthor($comment_id, $comment_type='') { 1019 $comment = get_comment($comment_id); 1020 $post = get_post($comment->comment_post_ID); 1021 $user = get_userdata( $post->post_author ); 1022 1023 if ( $comment->user_id == $post->post_author ) return false; // The author moderated a comment on his own post 1024 1025 if ('' == $user->user_email) return false; // If there's no email to send the comment to 1026 1027 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP); 1028 1029 // The blogname option is escaped with esc_html on the way into the database in sanitize_option 1030 // we want to reverse this for the plain text arena of emails. 1031 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 1032 1033 if ( empty( $comment_type ) ) $comment_type = 'comment'; 1034 1035 if ('comment' == $comment_type) { 1036 $notify_message = sprintf( __('New comment on your post "%s"'),$post->post_title ) . "\r\n"; 1037 /* translators: 1: comment author, 2: author IP, 3: author domain */ 1038 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 1039 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n"; 1040 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n"; 1041 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n"; 1042 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n"; 1043 $notify_message .= __('You can see all comments on this post here: ') . "\r\n"; 1044 /* translators: 1: blog name, 2: post title */ 1045 $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title ); 1046 } elseif ('trackback' == $comment_type) { 1047 $notify_message = sprintf( __('New trackback on your post "%s"'), $post->post_title ) . "\r\n"; 1048 /* translators: 1: website name, 2: author IP, 3: author domain */ 1049 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 1050 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n"; 1051 $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n"; 1052 $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n"; 1053 /* translators: 1: blog name, 2: post title */ 1054 $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title ); 1055 } elseif ('pingback' == $comment_type) { 1056 $notify_message = sprintf( __('New pingback on your post "%s"'), $post->post_title ) . "\r\n"; 1057 /* translators: 1: comment author, 2: author IP, 3: author domain */ 1058 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 1059 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n"; 1060 $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n"; 1061 $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n"; 1062 /* translators: 1: blog name, 2: post title */ 1063 $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title ); 1064 } 1065 $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n"; 1066 if ( EMPTY_TRASH_DAYS ) 1067 $notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n"; 1068 else 1069 $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n"; 1070 $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n"; 1071 1072 $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); 1073 1074 if ( '' == $comment->comment_author ) { 1075 $from = "From: \"$blogname\" <$wp_email>"; 1076 if ( '' != $comment->comment_author_email ) 1077 $reply_to = "Reply-To: $comment->comment_author_email"; 1078 } else { 1079 $from = "From: \"$comment->comment_author\" <$wp_email>"; 1080 if ( '' != $comment->comment_author_email ) 1081 $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>"; 1082 } 1083 1084 $message_headers = "$from\n" 1085 . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n"; 1086 1087 if ( isset($reply_to) ) 1088 $message_headers .= $reply_to . "\n"; 1089 1090 $notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id); 1091 $subject = apply_filters('comment_notification_subject', $subject, $comment_id); 1092 $message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id); 1093 1094 @wp_mail($user->user_email, $subject, $notify_message, $message_headers); 1095 1096 return true; 1097 } 1098 endif; 1099 1100 if ( !function_exists('wp_notify_moderator') ) : 1101 /** 1102 * Notifies the moderator of the blog about a new comment that is awaiting approval. 1103 * 1104 * @since 1.0 1105 * @uses $wpdb 1106 * 1107 * @param int $comment_id Comment ID 1108 * @return bool Always returns true 1109 */ 1110 function wp_notify_moderator($comment_id) { 1111 global $wpdb; 1112 1113 if( get_option( "moderation_notify" ) == 0 ) 1114 return true; 1115 1116 $comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID=%d LIMIT 1", $comment_id)); 1117 $post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID=%d LIMIT 1", $comment->comment_post_ID)); 1118 1119 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP); 1120 $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'"); 1121 1122 // The blogname option is escaped with esc_html on the way into the database in sanitize_option 1123 // we want to reverse this for the plain text arena of emails. 1124 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 1125 1126 switch ($comment->comment_type) 1127 { 1128 case 'trackback': 1129 $notify_message = sprintf( __('A new trackback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n"; 1130 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n"; 1131 $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 1132 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n"; 1133 $notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n"; 1134 break; 1135 case 'pingback': 1136 $notify_message = sprintf( __('A new pingback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n"; 1137 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n"; 1138 $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 1139 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n"; 1140 $notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n"; 1141 break; 1142 default: //Comments 1143 $notify_message = sprintf( __('A new comment on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n"; 1144 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n"; 1145 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 1146 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n"; 1147 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n"; 1148 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n"; 1149 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n"; 1150 break; 1151 } 1152 1153 $notify_message .= sprintf( __('Approve it: %s'), admin_url("comment.php?action=approve&c=$comment_id") ) . "\r\n"; 1154 if ( EMPTY_TRASH_DAYS ) 1155 $notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n"; 1156 else 1157 $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n"; 1158 $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n"; 1159 1160 $notify_message .= sprintf( _n('Currently %s comment is waiting for approval. Please visit the moderation panel:', 1161 'Currently %s comments are waiting for approval. Please visit the moderation panel:', $comments_waiting), number_format_i18n($comments_waiting) ) . "\r\n"; 1162 $notify_message .= admin_url("edit-comments.php?comment_status=moderated") . "\r\n"; 1163 1164 $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), $blogname, $post->post_title ); 1165 $admin_email = get_option('admin_email'); 1166 $message_headers = ''; 1167 1168 $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id); 1169 $subject = apply_filters('comment_moderation_subject', $subject, $comment_id); 1170 $message_headers = apply_filters('comment_moderation_headers', $message_headers); 1171 1172 @wp_mail($admin_email, $subject, $notify_message, $message_headers); 1173 1174 return true; 1175 } 1176 endif; 1177 1178 if ( !function_exists('wp_password_change_notification') ) : 1179 /** 1180 * Notify the blog admin of a user changing password, normally via email. 1181 * 1182 * @since 2.7 1183 * 1184 * @param object $user User Object 1185 */ 1186 function wp_password_change_notification(&$user) { 1187 // send a copy of password change notification to the admin 1188 // but check to see if it's the admin whose password we're changing, and skip this 1189 if ( $user->user_email != get_option('admin_email') ) { 1190 $message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n"; 1191 // The blogname option is escaped with esc_html on the way into the database in sanitize_option 1192 // we want to reverse this for the plain text arena of emails. 1193 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 1194 wp_mail(get_option('admin_email'), sprintf(__('[%s] Password Lost/Changed'), $blogname), $message); 1195 } 1196 } 1197 endif; 1198 1199 if ( !function_exists('wp_new_user_notification') ) : 1200 /** 1201 * Notify the blog admin of a new user, normally via email. 1202 * 1203 * @since 2.0 1204 * 1205 * @param int $user_id User ID 1206 * @param string $plaintext_pass Optional. The user's plaintext password 1207 */ 1208 function wp_new_user_notification($user_id, $plaintext_pass = '') { 1209 $user = new WP_User($user_id); 1210 1211 $user_login = stripslashes($user->user_login); 1212 $user_email = stripslashes($user->user_email); 1213 1214 // The blogname option is escaped with esc_html on the way into the database in sanitize_option 1215 // we want to reverse this for the plain text arena of emails. 1216 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 1217 1218 $message = sprintf(__('New user registration on your blog %s:'), $blogname) . "\r\n\r\n"; 1219 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; 1220 $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n"; 1221 1222 @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message); 1223 1224 if ( empty($plaintext_pass) ) 1225 return; 1226 1227 $message = sprintf(__('Username: %s'), $user_login) . "\r\n"; 1228 $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n"; 1229 $message .= wp_login_url() . "\r\n"; 1230 1231 wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message); 1232 1233 } 1234 endif; 1235 1236 if ( !function_exists('wp_nonce_tick') ) : 1237 /** 1238 * Get the time-dependent variable for nonce creation. 1239 * 1240 * A nonce has a lifespan of two ticks. Nonces in their second tick may be 1241 * updated, e.g. by autosave. 1242 * 1243 * @since 2.5 1244 * 1245 * @return int 1246 */ 1247 function wp_nonce_tick() { 1248 $nonce_life = apply_filters('nonce_life', 86400); 1249 1250 return ceil(time() / ( $nonce_life / 2 )); 1251 } 1252 endif; 1253 1254 if ( !function_exists('wp_verify_nonce') ) : 1255 /** 1256 * Verify that correct nonce was used with time limit. 1257 * 1258 * The user is given an amount of time to use the token, so therefore, since the 1259 * UID and $action remain the same, the independent variable is the time. 1260 * 1261 * @since 2.0.3 1262 * 1263 * @param string $nonce Nonce that was used in the form to verify 1264 * @param string|int $action Should give context to what is taking place and be the same when nonce was created. 1265 * @return bool Whether the nonce check passed or failed. 1266 */ 1267 function wp_verify_nonce($nonce, $action = -1) { 1268 $user = wp_get_current_user(); 1269 $uid = (int) $user->id; 1270 1271 $i = wp_nonce_tick(); 1272 1273 // Nonce generated 0-12 hours ago 1274 if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) == $nonce ) 1275 return 1; 1276 // Nonce generated 12-24 hours ago 1277 if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) == $nonce ) 1278 return 2; 1279 // Invalid nonce 1280 return false; 1281 } 1282 endif; 1283 1284 if ( !function_exists('wp_create_nonce') ) : 1285 /** 1286 * Creates a random, one time use token. 1287 * 1288 * @since 2.0.3 1289 * 1290 * @param string|int $action Scalar value to add context to the nonce. 1291 * @return string The one use form token 1292 */ 1293 function wp_create_nonce($action = -1) { 1294 $user = wp_get_current_user(); 1295 $uid = (int) $user->id; 1296 1297 $i = wp_nonce_tick(); 1298 1299 return substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10); 1300 } 1301 endif; 1302 1303 if ( !function_exists('wp_salt') ) : 1304 /** 1305 * Get salt to add to hashes to help prevent attacks. 1306 * 1307 * The secret key is located in two places: the database in case the secret key 1308 * isn't defined in the second place, which is in the wp-config.php file. If you 1309 * are going to set the secret key, then you must do so in the wp-config.php 1310 * file. 1311 * 1312 * The secret key in the database is randomly generated and will be appended to 1313 * the secret key that is in wp-config.php file in some instances. It is 1314 * important to have the secret key defined or changed in wp-config.php. 1315 * 1316 * If you have installed WordPress 2.5 or later, then you will have the 1317 * SECRET_KEY defined in the wp-config.php already. You will want to change the 1318 * value in it because hackers will know what it is. If you have upgraded to 1319 * WordPress 2.5 or later version from a version before WordPress 2.5, then you 1320 * should add the constant to your wp-config.php file. 1321 * 1322 * Below is an example of how the SECRET_KEY constant is defined with a value. 1323 * You must not copy the below example and paste into your wp-config.php. If you 1324 * need an example, then you can have a 1325 * {@link https://api.wordpress.org/secret-key/1.1/ secret key created} for you. 1326 * 1327 * <code> 1328 * define('SECRET_KEY', 'mAry1HadA15|\/|b17w55w1t3asSn09w'); 1329 * </code> 1330 * 1331 * Salting passwords helps against tools which has stored hashed values of 1332 * common dictionary strings. The added values makes it harder to crack if given 1333 * salt string is not weak. 1334 * 1335 * @since 2.5 1336 * @link https://api.wordpress.org/secret-key/1.1/ Create a Secret Key for wp-config.php 1337 * 1338 * @param string $scheme Authentication scheme 1339 * @return string Salt value 1340 */ 1341 function wp_salt($scheme = 'auth') { 1342 global $wp_default_secret_key; 1343 $secret_key = ''; 1344 if ( defined('SECRET_KEY') && ('' != SECRET_KEY) && ( $wp_default_secret_key != SECRET_KEY) ) 1345 $secret_key = SECRET_KEY; 1346 1347 if ( 'auth' == $scheme ) { 1348 if ( defined('AUTH_KEY') && ('' != AUTH_KEY) && ( $wp_default_secret_key != AUTH_KEY) ) 1349 $secret_key = AUTH_KEY; 1350 1351 if ( defined('AUTH_SALT') && ('' != AUTH_SALT) && ( $wp_default_secret_key != AUTH_SALT) ) { 1352 $salt = AUTH_SALT; 1353 } elseif ( defined('SECRET_SALT') && ('' != SECRET_SALT) && ( $wp_default_secret_key != SECRET_SALT) ) { 1354 $salt = SECRET_SALT; 1355 } else { 1356 $salt = get_option('auth_salt'); 1357 if ( empty($salt) ) { 1358 $salt = wp_generate_password( 64, true, true ); 1359 update_option('auth_salt', $salt); 1360 } 1361 } 1362 } elseif ( 'secure_auth' == $scheme ) { 1363 if ( defined('SECURE_AUTH_KEY') && ('' != SECURE_AUTH_KEY) && ( $wp_default_secret_key != SECURE_AUTH_KEY) ) 1364 $secret_key = SECURE_AUTH_KEY; 1365 1366 if ( defined('SECURE_AUTH_SALT') && ('' != SECURE_AUTH_SALT) && ( $wp_default_secret_key != SECURE_AUTH_SALT) ) { 1367 $salt = SECURE_AUTH_SALT; 1368 } else { 1369 $salt = get_option('secure_auth_salt'); 1370 if ( empty($salt) ) { 1371 $salt = wp_generate_password( 64, true, true ); 1372 update_option('secure_auth_salt', $salt); 1373 } 1374 } 1375 } elseif ( 'logged_in' == $scheme ) { 1376 if ( defined('LOGGED_IN_KEY') && ('' != LOGGED_IN_KEY) && ( $wp_default_secret_key != LOGGED_IN_KEY) ) 1377 $secret_key = LOGGED_IN_KEY; 1378 1379 if ( defined('LOGGED_IN_SALT') && ('' != LOGGED_IN_SALT) && ( $wp_default_secret_key != LOGGED_IN_SALT) ) { 1380 $salt = LOGGED_IN_SALT; 1381 } else { 1382 $salt = get_option('logged_in_salt'); 1383 if ( empty($salt) ) { 1384 $salt = wp_generate_password( 64, true, true ); 1385 update_option('logged_in_salt', $salt); 1386 } 1387 } 1388 } elseif ( 'nonce' == $scheme ) { 1389 if ( defined('NONCE_KEY') && ('' != NONCE_KEY) && ( $wp_default_secret_key != NONCE_KEY) ) 1390 $secret_key = NONCE_KEY; 1391 1392 if ( defined('NONCE_SALT') && ('' != NONCE_SALT) && ( $wp_default_secret_key != NONCE_SALT) ) { 1393 $salt = NONCE_SALT; 1394 } else { 1395 $salt = get_option('nonce_salt'); 1396 if ( empty($salt) ) { 1397 $salt = wp_generate_password( 64, true, true ); 1398 update_option('nonce_salt', $salt); 1399 } 1400 } 1401 } else { 1402 // ensure each auth scheme has its own unique salt 1403 $salt = hash_hmac('md5', $scheme, $secret_key); 1404 } 1405 1406 return apply_filters('salt', $secret_key . $salt, $scheme); 1407 } 1408 endif; 1409 1410 if ( !function_exists('wp_hash') ) : 1411 /** 1412 * Get hash of given string. 1413 * 1414 * @since 2.0.3 1415 * @uses wp_salt() Get WordPress salt 1416 * 1417 * @param string $data Plain text to hash 1418 * @return string Hash of $data 1419 */ 1420 function wp_hash($data, $scheme = 'auth') { 1421 $salt = wp_salt($scheme); 1422 1423 return hash_hmac('md5', $data, $salt); 1424 } 1425 endif; 1426 1427 if ( !function_exists('wp_hash_password') ) : 1428 /** 1429 * Create a hash (encrypt) of a plain text password. 1430 * 1431 * For integration with other applications, this function can be overwritten to 1432 * instead use the other package password checking algorithm. 1433 * 1434 * @since 2.5 1435 * @global object $wp_hasher PHPass object 1436 * @uses PasswordHash::HashPassword 1437 * 1438 * @param string $password Plain text user password to hash 1439 * @return string The hash string of the password 1440 */ 1441 function wp_hash_password($password) { 1442 global $wp_hasher; 1443 1444 if ( empty($wp_hasher) ) { 1445 require_once ( ABSPATH . 'wp-includes/class-phpass.php'); 1446 // By default, use the portable hash from phpass 1447 $wp_hasher = new PasswordHash(8, TRUE); 1448 } 1449 1450 return $wp_hasher->HashPassword($password); 1451 } 1452 endif; 1453 1454 if ( !function_exists('wp_check_password') ) : 1455 /** 1456 * Checks the plaintext password against the encrypted Password. 1457 * 1458 * Maintains compatibility between old version and the new cookie authentication 1459 * protocol using PHPass library. The $hash parameter is the encrypted password 1460 * and the function compares the plain text password when encypted similarly 1461 * against the already encrypted password to see if they match. 1462 * 1463 * For integration with other applications, this function can be overwritten to 1464 * instead use the other package password checking algorithm. 1465 * 1466 * @since 2.5 1467 * @global object $wp_hasher PHPass object used for checking the password 1468 * against the $hash + $password 1469 * @uses PasswordHash::CheckPassword 1470 * 1471 * @param string $password Plaintext user's password 1472 * @param string $hash Hash of the user's password to check against. 1473 * @return bool False, if the $password does not match the hashed password 1474 */ 1475 function wp_check_password($password, $hash, $user_id = '') { 1476 global $wp_hasher; 1477 1478 // If the hash is still md5... 1479 if ( strlen($hash) <= 32 ) { 1480 $check = ( $hash == md5($password) ); 1481 if ( $check && $user_id ) { 1482 // Rehash using new hash. 1483 wp_set_password($password, $user_id); 1484 $hash = wp_hash_password($password); 1485 } 1486 1487 return apply_filters('check_password', $check, $password, $hash, $user_id); 1488 } 1489 1490 // If the stored hash is longer than an MD5, presume the 1491 // new style phpass portable hash. 1492 if ( empty($wp_hasher) ) { 1493 require_once ( ABSPATH . 'wp-includes/class-phpass.php'); 1494 // By default, use the portable hash from phpass 1495 $wp_hasher = new PasswordHash(8, TRUE); 1496 } 1497 1498 $check = $wp_hasher->CheckPassword($password, $hash); 1499 1500 return apply_filters('check_password', $check, $password, $hash, $user_id); 1501 } 1502 endif; 1503 1504 if ( !function_exists('wp_generate_password') ) : 1505 /** 1506 * Generates a random password drawn from the defined set of characters. 1507 * 1508 * @since 2.5 1509 * 1510 * @param int $length The length of password to generate 1511 * @param bool $special_chars Whether to include standard special characters. Default true. 1512 * @param bool $extra_special_chars Whether to include other special characters. Used when 1513 * generating secret keys and salts. Default false. 1514 * @return string The random password 1515 **/ 1516 function wp_generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) { 1517 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 1518 if ( $special_chars ) 1519 $chars .= '!@#$%^&*()'; 1520 if ( $extra_special_chars ) 1521 $chars .= '-_ []{}<>~`+=,.;:/?|'; 1522 1523 $password = ''; 1524 for ( $i = 0; $i < $length; $i++ ) { 1525 $password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1); 1526 } 1527 1528 // random_password filter was previously in random_password function which was deprecated 1529 return apply_filters('random_password', $password); 1530 } 1531 endif; 1532 1533 if ( !function_exists('wp_rand') ) : 1534 /** 1535 * Generates a random number 1536 * 1537 * @since 2.6.2 1538 * 1539 * @param int $min Lower limit for the generated number (optional, default is 0) 1540 * @param int $max Upper limit for the generated number (optional, default is 4294967295) 1541 * @return int A random number between min and max 1542 */ 1543 function wp_rand( $min = 0, $max = 0 ) { 1544 global $rnd_value; 1545 1546 // Reset $rnd_value after 14 uses 1547 // 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value 1548 if ( strlen($rnd_value) < 8 ) { 1549 if ( defined( 'WP_SETUP_CONFIG' ) ) 1550 static $seed = ''; 1551 else 1552 $seed = get_transient('random_seed'); 1553 $rnd_value = md5( uniqid(microtime() . mt_rand(), true ) . $seed ); 1554 $rnd_value .= sha1($rnd_value); 1555 $rnd_value .= sha1($rnd_value . $seed); 1556 $seed = md5($seed . $rnd_value); 1557 if ( ! defined( 'WP_SETUP_CONFIG' ) ) 1558 set_transient('random_seed', $seed); 1559 } 1560 1561 // Take the first 8 digits for our value 1562 $value = substr($rnd_value, 0, 8); 1563 1564 // Strip the first eight, leaving the remainder for the next call to wp_rand(). 1565 $rnd_value = substr($rnd_value, 8); 1566 1567 $value = abs(hexdec($value)); 1568 1569 // Reduce the value to be within the min - max range 1570 // 4294967295 = 0xffffffff = max random number 1571 if ( $max != 0 ) 1572 $value = $min + (($max - $min + 1) * ($value / (4294967295 + 1))); 1573 1574 return abs(intval($value)); 1575 } 1576 endif; 1577 1578 if ( !function_exists('wp_set_password') ) : 1579 /** 1580 * Updates the user's password with a new encrypted one. 1581 * 1582 * For integration with other applications, this function can be overwritten to 1583 * instead use the other package password checking algorithm. 1584 * 1585 * @since 2.5 1586 * @uses $wpdb WordPress database object for queries 1587 * @uses wp_hash_password() Used to encrypt the user's password before passing to the database 1588 * 1589 * @param string $password The plaintext new user password 1590 * @param int $user_id User ID 1591 */ 1592 function wp_set_password( $password, $user_id ) { 1593 global $wpdb; 1594 1595 $hash = wp_hash_password($password); 1596 $wpdb->update($wpdb->users, array('user_pass' => $hash, 'user_activation_key' => ''), array('ID' => $user_id) ); 1597 1598 wp_cache_delete($user_id, 'users'); 1599 } 1600 endif; 1601 1602 if ( !function_exists( 'get_avatar' ) ) : 1603 /** 1604 * Retrieve the avatar for a user who provided a user ID or email address. 1605 * 1606 * @since 2.5 1607 * @param int|string|object $id_or_email A user ID, email address, or comment object 1608 * @param int $size Size of the avatar image 1609 * @param string $default URL to a default image to use if no avatar is available 1610 * @param string $alt Alternate text to use in image tag. Defaults to blank 1611 * @return string <img> tag for the user's avatar 1612 */ 1613 function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) { 1614 if ( ! get_option('show_avatars') ) 1615 return false; 1616 1617 if ( false === $alt) 1618 $safe_alt = ''; 1619 else 1620 $safe_alt = esc_attr( $alt ); 1621 1622 if ( !is_numeric($size) ) 1623 $size = '96'; 1624 1625 $email = ''; 1626 if ( is_numeric($id_or_email) ) { 1627 $id = (int) $id_or_email; 1628 $user = get_userdata($id); 1629 if ( $user ) 1630 $email = $user->user_email; 1631 } elseif ( is_object($id_or_email) ) { 1632 // No avatar for pingbacks or trackbacks 1633 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 1634 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) 1635 return false; 1636 1637 if ( !empty($id_or_email->user_id) ) { 1638 $id = (int) $id_or_email->user_id; 1639 $user = get_userdata($id); 1640 if ( $user) 1641 $email = $user->user_email; 1642 } elseif ( !empty($id_or_email->comment_author_email) ) { 1643 $email = $id_or_email->comment_author_email; 1644 } 1645 } else { 1646 $email = $id_or_email; 1647 } 1648 1649 if ( empty($default) ) { 1650 $avatar_default = get_option('avatar_default'); 1651 if ( empty($avatar_default) ) 1652 $default = 'mystery'; 1653 else 1654 $default = $avatar_default; 1655 } 1656 1657 if ( !empty($email) ) 1658 $email_hash = md5( strtolower( $email ) ); 1659 1660 if ( is_ssl() ) { 1661 $host = 'https://secure.gravatar.com'; 1662 } else { 1663 if ( !empty($email) ) 1664 $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash{0} ) % 2 ) ); 1665 else 1666 $host = 'http://0.gravatar.com'; 1667 } 1668 1669 if ( 'mystery' == $default ) 1670 $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com') 1671 elseif ( 'blank' == $default ) 1672 $default = includes_url('images/blank.gif'); 1673 elseif ( !empty($email) && 'gravatar_default' == $default ) 1674 $default = ''; 1675 elseif ( 'gravatar_default' == $default ) 1676 $default = "$host/avatar/s={$size}"; 1677 elseif ( empty($email) ) 1678 $default = "$host/avatar/?d=$default&s={$size}"; 1679 elseif ( strpos($default, 'http://') === 0 ) 1680 $default = add_query_arg( 's', $size, $default ); 1681 1682 if ( !empty($email) ) { 1683 $out = "$host/avatar/"; 1684 $out .= $email_hash; 1685 $out .= '?s='.$size; 1686 $out .= '&d=' . urlencode( $default ); 1687 1688 $rating = get_option('avatar_rating'); 1689 if ( !empty( $rating ) ) 1690 $out .= "&r={$rating}"; 1691 1692 $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"; 1693 } else { 1694 $avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />"; 1695 } 1696 1697 return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt); 1698 } 1699 endif; 1700 1701 if ( !function_exists( 'wp_text_diff' ) ) : 1702 /** 1703 * Displays a human readable HTML representation of the difference between two strings. 1704 * 1705 * The Diff is available for getting the changes between versions. The output is 1706 * HTML, so the primary use is for displaying the changes. If the two strings 1707 * are equivalent, then an empty string will be returned. 1708 * 1709 * The arguments supported and can be changed are listed below. 1710 * 1711 * 'title' : Default is an empty string. Titles the diff in a manner compatible 1712 * with the output. 1713 * 'title_left' : Default is an empty string. Change the HTML to the left of the 1714 * title. 1715 * 'title_right' : Default is an empty string. Change the HTML to the right of 1716 * the title. 1717 * 1718 * @since 2.6 1719 * @see wp_parse_args() Used to change defaults to user defined settings. 1720 * @uses Text_Diff 1721 * @uses WP_Text_Diff_Renderer_Table 1722 * 1723 * @param string $left_string "old" (left) version of string 1724 * @param string $right_string "new" (right) version of string 1725 * @param string|array $args Optional. Change 'title', 'title_left', and 'title_right' defaults. 1726 * @return string Empty string if strings are equivalent or HTML with differences. 1727 */ 1728 function wp_text_diff( $left_string, $right_string, $args = null ) { 1729 $defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' ); 1730 $args = wp_parse_args( $args, $defaults ); 1731 1732 if ( !class_exists( 'WP_Text_Diff_Renderer_Table' ) ) 1733 require ( ABSPATH . WPINC . '/wp-diff.php' ); 1734 1735 $left_string = normalize_whitespace($left_string); 1736 $right_string = normalize_whitespace($right_string); 1737 1738 $left_lines = split("\n", $left_string); 1739 $right_lines = split("\n", $right_string); 1740 1741 $text_diff = new Text_Diff($left_lines, $right_lines); 1742 $renderer = new WP_Text_Diff_Renderer_Table(); 1743 $diff = $renderer->render($text_diff); 1744 1745 if ( !$diff ) 1746 return ''; 1747 1748 $r = "<table class='diff'>\n"; 1749 $r .= "<col class='ltype' /><col class='content' /><col class='ltype' /><col class='content' />"; 1750 1751 if ( $args['title'] || $args['title_left'] || $args['title_right'] ) 1752 $r .= "<thead>"; 1753 if ( $args['title'] ) 1754 $r .= "<tr class='diff-title'><th colspan='4'>$args[title]</th></tr>\n"; 1755 if ( $args['title_left'] || $args['title_right'] ) { 1756 $r .= "<tr class='diff-sub-title'>\n"; 1757 $r .= "\t<td></td><th>$args[title_left]</th>\n"; 1758 $r .= "\t<td></td><th>$args[title_right]</th>\n"; 1759 $r .= "</tr>\n"; 1760 } 1761 if ( $args['title'] || $args['title_left'] || $args['title_right'] ) 1762 $r .= "</thead>\n"; 1763 1764 $r .= "<tbody>\n$diff\n</tbody>\n"; 1765 $r .= "</table>"; 1766 1767 return $r; 1768 } 1769 endif;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Apr 5 14:26:09 2010 | Cross-referenced by PHPXref 0.7 |