PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/pluggable.php

https://bitbucket.org/MaheshDhaduk/androidmobiles
PHP | 1754 lines | 945 code | 245 blank | 564 comment | 308 complexity | b2f43e3acaf990816008647e1ad76642 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-1.0

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

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