PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/pluggable.php

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

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