PageRenderTime 88ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/wp-includes/pluggable.php

https://bitbucket.org/bsnowman/classyblog
PHP | 1743 lines | 952 code | 240 blank | 551 comment | 310 complexity | 3999110615cd679627e1fe7c6937a9c5 MD5 | raw file
Possible License(s): BSD-3-Clause

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

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