PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/wordpress3.4.2/wp-includes/pluggable.php

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

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