Advertisement
Guest User

Untitled

a guest
Dec 1st, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.95 KB | None | 0 0
  1. <?php
  2. /**
  3. * These functions can be replaced via plugins. If plugins do not redefine these
  4. * functions, then these will be used instead.
  5. *
  6. * @package WordPress
  7. */
  8.  
  9. if ( !function_exists('wp_set_current_user') ) :
  10. /**
  11. * Changes the current user by ID or name.
  12. *
  13. * Set $id to null and specify a name if you do not know a user's ID.
  14. *
  15. * Some WordPress functionality is based on the current user and not based on
  16. * the signed in user. Therefore, it opens the ability to edit and perform
  17. * actions on users who aren't signed in.
  18. *
  19. * @since 2.0.3
  20. * @global WP_User $current_user The current user object which holds the user data.
  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.  
  29. // If `$id` matches the user who's already current, there's nothing to do.
  30. if ( isset( $current_user )
  31. && ( $current_user instanceof WP_User )
  32. && ( $id == $current_user->ID )
  33. && ( null !== $id )
  34. ) {
  35. return $current_user;
  36. }
  37.  
  38. $current_user = new WP_User( $id, $name );
  39.  
  40. setup_userdata( $current_user->ID );
  41.  
  42. /**
  43. * Fires after the current user is set.
  44. *
  45. * @since 2.0.1
  46. */
  47. do_action( 'set_current_user' );
  48.  
  49. return $current_user;
  50. }
  51. endif;
  52.  
  53. if ( !function_exists('wp_get_current_user') ) :
  54. /**
  55. * Retrieve the current user object.
  56. *
  57. * Will set the current user, if the current user is not set. The current user
  58. * will be set to the logged-in person. If no user is logged-in, then it will
  59. * set the current user to 0, which is invalid and won't have any permissions.
  60. *
  61. * @since 2.0.3
  62. *
  63. * @see _wp_get_current_user()
  64. * @global WP_User $current_user Checks if the current user is set.
  65. *
  66. * @return WP_User Current WP_User instance.
  67. */
  68. function wp_get_current_user() {
  69. return _wp_get_current_user();
  70. }
  71. endif;
  72.  
  73. if ( !function_exists('get_userdata') ) :
  74. /**
  75. * Retrieve user info by user ID.
  76. *
  77. * @since 0.71
  78. *
  79. * @param int $user_id User ID
  80. * @return WP_User|false WP_User object on success, false on failure.
  81. */
  82. function get_userdata( $user_id ) {
  83. return get_user_by( 'id', $user_id );
  84. }
  85. endif;
  86.  
  87. if ( !function_exists('get_user_by') ) :
  88. /**
  89. * Retrieve user info by a given field
  90. *
  91. * @since 2.8.0
  92. * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
  93. *
  94. * @param string $field The field to retrieve the user with. id | ID | slug | email | login.
  95. * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
  96. * @return WP_User|false WP_User object on success, false on failure.
  97. */
  98. function get_user_by( $field, $value ) {
  99. $userdata = WP_User::get_data_by( $field, $value );
  100.  
  101. if ( !$userdata )
  102. return false;
  103.  
  104. $user = new WP_User;
  105. $user->init( $userdata );
  106.  
  107. return $user;
  108. }
  109. endif;
  110.  
  111. if ( !function_exists('cache_users') ) :
  112. /**
  113. * Retrieve info for user lists to prevent multiple queries by get_userdata()
  114. *
  115. * @since 3.0.0
  116. *
  117. * @global wpdb $wpdb WordPress database abstraction object.
  118. *
  119. * @param array $user_ids User ID numbers list
  120. */
  121. function cache_users( $user_ids ) {
  122. global $wpdb;
  123.  
  124. $clean = _get_non_cached_ids( $user_ids, 'users' );
  125.  
  126. if ( empty( $clean ) )
  127. return;
  128.  
  129. $list = implode( ',', $clean );
  130.  
  131. $users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" );
  132.  
  133. $ids = array();
  134. foreach ( $users as $user ) {
  135. update_user_caches( $user );
  136. $ids[] = $user->ID;
  137. }
  138. update_meta_cache( 'user', $ids );
  139. }
  140. endif;
  141.  
  142. if ( !function_exists( 'wp_mail' ) ) :
  143. /**
  144. * Send mail, similar to PHP's mail
  145. *
  146. * A true return value does not automatically mean that the user received the
  147. * email successfully. It just only means that the method used was able to
  148. * process the request without any errors.
  149. *
  150. * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from
  151. * creating a from address like 'Name <email@address.com>' when both are set. If
  152. * just 'wp_mail_from' is set, then just the email address will be used with no
  153. * name.
  154. *
  155. * The default content type is 'text/plain' which does not allow using HTML.
  156. * However, you can set the content type of the email by using the
  157. * {@see 'wp_mail_content_type'} filter.
  158. *
  159. * The default charset is based on the charset used on the blog. The charset can
  160. * be set using the {@see 'wp_mail_charset'} filter.
  161. *
  162. * @since 1.2.1
  163. *
  164. * @global PHPMailer $phpmailer
  165. *
  166. * @param string|array $to Array or comma-separated list of email addresses to send message.
  167. * @param string $subject Email subject
  168. * @param string $message Message contents
  169. * @param string|array $headers Optional. Additional headers.
  170. * @param string|array $attachments Optional. Files to attach.
  171. * @return bool Whether the email contents were sent successfully.
  172. */
  173. function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
  174. // Compact the input, apply the filters, and extract them back out
  175.  
  176. /**
  177. * Filters the wp_mail() arguments.
  178. *
  179. * @since 2.2.0
  180. *
  181. * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
  182. * subject, message, headers, and attachments values.
  183. */
  184. $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
  185.  
  186. if ( isset( $atts['to'] ) ) {
  187. $to = $atts['to'];
  188. }
  189.  
  190. if ( !is_array( $to ) ) {
  191. $to = explode( ',', $to );
  192. }
  193.  
  194. if ( isset( $atts['subject'] ) ) {
  195. $subject = $atts['subject'];
  196. }
  197.  
  198. if ( isset( $atts['message'] ) ) {
  199. $message = $atts['message'];
  200. }
  201.  
  202. if ( isset( $atts['headers'] ) ) {
  203. $headers = $atts['headers'];
  204. }
  205.  
  206. if ( isset( $atts['attachments'] ) ) {
  207. $attachments = $atts['attachments'];
  208. }
  209.  
  210. if ( ! is_array( $attachments ) ) {
  211. $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
  212. }
  213. global $phpmailer;
  214.  
  215. // (Re)create it, if it's gone missing
  216. if ( ! ( $phpmailer instanceof PHPMailer ) ) {
  217. require_once ABSPATH . WPINC . '/class-phpmailer.php';
  218. require_once ABSPATH . WPINC . '/class-smtp.php';
  219. $phpmailer = new PHPMailer( true );
  220. }
  221.  
  222. // Headers
  223. $cc = $bcc = $reply_to = array();
  224.  
  225. if ( empty( $headers ) ) {
  226. $headers = array();
  227. } else {
  228. if ( !is_array( $headers ) ) {
  229. // Explode the headers out, so this function can take both
  230. // string headers and an array of headers.
  231. $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
  232. } else {
  233. $tempheaders = $headers;
  234. }
  235. $headers = array();
  236.  
  237. // If it's actually got contents
  238. if ( !empty( $tempheaders ) ) {
  239. // Iterate through the raw headers
  240. foreach ( (array) $tempheaders as $header ) {
  241. if ( strpos($header, ':') === false ) {
  242. if ( false !== stripos( $header, 'boundary=' ) ) {
  243. $parts = preg_split('/boundary=/i', trim( $header ) );
  244. $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
  245. }
  246. continue;
  247. }
  248. // Explode them out
  249. list( $name, $content ) = explode( ':', trim( $header ), 2 );
  250.  
  251. // Cleanup crew
  252. $name = trim( $name );
  253. $content = trim( $content );
  254.  
  255. switch ( strtolower( $name ) ) {
  256. // Mainly for legacy -- process a From: header if it's there
  257. case 'from':
  258. $bracket_pos = strpos( $content, '<' );
  259. if ( $bracket_pos !== false ) {
  260. // Text before the bracketed email is the "From" name.
  261. if ( $bracket_pos > 0 ) {
  262. $from_name = substr( $content, 0, $bracket_pos - 1 );
  263. $from_name = str_replace( '"', '', $from_name );
  264. $from_name = trim( $from_name );
  265. }
  266.  
  267. $from_email = substr( $content, $bracket_pos + 1 );
  268. $from_email = str_replace( '>', '', $from_email );
  269. $from_email = trim( $from_email );
  270.  
  271. // Avoid setting an empty $from_email.
  272. } elseif ( '' !== trim( $content ) ) {
  273. $from_email = trim( $content );
  274. }
  275. break;
  276. case 'content-type':
  277. if ( strpos( $content, ';' ) !== false ) {
  278. list( $type, $charset_content ) = explode( ';', $content );
  279. $content_type = trim( $type );
  280. if ( false !== stripos( $charset_content, 'charset=' ) ) {
  281. $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) );
  282. } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
  283. $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
  284. $charset = '';
  285. }
  286.  
  287. // Avoid setting an empty $content_type.
  288. } elseif ( '' !== trim( $content ) ) {
  289. $content_type = trim( $content );
  290. }
  291. break;
  292. case 'cc':
  293. $cc = array_merge( (array) $cc, explode( ',', $content ) );
  294. break;
  295. case 'bcc':
  296. $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
  297. break;
  298. case 'reply-to':
  299. $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) );
  300. break;
  301. default:
  302. // Add it to our grand headers array
  303. $headers[trim( $name )] = trim( $content );
  304. break;
  305. }
  306. }
  307. }
  308. }
  309.  
  310. // Empty out the values that may be set
  311. $phpmailer->clearAllRecipients();
  312. $phpmailer->clearAttachments();
  313. $phpmailer->clearCustomHeaders();
  314. $phpmailer->clearReplyTos();
  315.  
  316. // From email and name
  317. // If we don't have a name from the input headers
  318. if ( !isset( $from_name ) )
  319. $from_name = 'WordPress';
  320.  
  321. /* If we don't have an email from the input headers default to wordpress@$sitename
  322. * Some hosts will block outgoing mail from this address if it doesn't exist but
  323. * there's no easy alternative. Defaulting to admin_email might appear to be another
  324. * option but some hosts may refuse to relay mail from an unknown domain. See
  325. * https://core.trac.wordpress.org/ticket/5007.
  326. */
  327.  
  328. if ( !isset( $from_email ) ) {
  329. // Get the site domain and get rid of www.
  330. $sitename = strtolower( $_SERVER['SERVER_NAME'] );
  331. if ( substr( $sitename, 0, 4 ) == 'www.' ) {
  332. $sitename = substr( $sitename, 4 );
  333. }
  334.  
  335. $from_email = 'wordpress@' . $sitename;
  336. }
  337.  
  338. /**
  339. * Filters the email address to send from.
  340. *
  341. * @since 2.2.0
  342. *
  343. * @param string $from_email Email address to send from.
  344. */
  345. $from_email = apply_filters( 'wp_mail_from', $from_email );
  346.  
  347. /**
  348. * Filters the name to associate with the "from" email address.
  349. *
  350. * @since 2.3.0
  351. *
  352. * @param string $from_name Name associated with the "from" email address.
  353. */
  354. $from_name = apply_filters( 'wp_mail_from_name', $from_name );
  355.  
  356. try {
  357. $phpmailer->setFrom( $from_email, $from_name, false );
  358. } catch ( phpmailerException $e ) {
  359. $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
  360. $mail_error_data['phpmailer_exception_code'] = $e->getCode();
  361.  
  362. /** This filter is documented in wp-includes/pluggable.php */
  363. do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );
  364.  
  365. return false;
  366. }
  367.  
  368. // Set mail's subject and body
  369. $phpmailer->Subject = $subject;
  370. $phpmailer->Body = $message;
  371.  
  372. // Set destination addresses, using appropriate methods for handling addresses
  373. $address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' );
  374.  
  375. foreach ( $address_headers as $address_header => $addresses ) {
  376. if ( empty( $addresses ) ) {
  377. continue;
  378. }
  379.  
  380. foreach ( (array) $addresses as $address ) {
  381. try {
  382. // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
  383. $recipient_name = '';
  384.  
  385. if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) {
  386. if ( count( $matches ) == 3 ) {
  387. $recipient_name = $matches[1];
  388. $address = $matches[2];
  389. }
  390. }
  391.  
  392. switch ( $address_header ) {
  393. case 'to':
  394. $phpmailer->addAddress( $address, $recipient_name );
  395. break;
  396. case 'cc':
  397. $phpmailer->addCc( $address, $recipient_name );
  398. break;
  399. case 'bcc':
  400. $phpmailer->addBcc( $address, $recipient_name );
  401. break;
  402. case 'reply_to':
  403. $phpmailer->addReplyTo( $address, $recipient_name );
  404. break;
  405. }
  406. } catch ( phpmailerException $e ) {
  407. continue;
  408. }
  409. }
  410. }
  411.  
  412. // Set to use PHP's mail()
  413. $phpmailer->isMail();
  414.  
  415. // Set Content-Type and charset
  416. // If we don't have a content-type from the input headers
  417. if ( !isset( $content_type ) )
  418. $content_type = 'text/plain';
  419.  
  420. /**
  421. * Filters the wp_mail() content type.
  422. *
  423. * @since 2.3.0
  424. *
  425. * @param string $content_type Default wp_mail() content type.
  426. */
  427. $content_type = apply_filters( 'wp_mail_content_type', $content_type );
  428.  
  429. $phpmailer->ContentType = $content_type;
  430.  
  431. // Set whether it's plaintext, depending on $content_type
  432. if ( 'text/html' == $content_type )
  433. $phpmailer->isHTML( true );
  434.  
  435. // If we don't have a charset from the input headers
  436. if ( !isset( $charset ) )
  437. $charset = get_bloginfo( 'charset' );
  438.  
  439. // Set the content-type and charset
  440.  
  441. /**
  442. * Filters the default wp_mail() charset.
  443. *
  444. * @since 2.3.0
  445. *
  446. * @param string $charset Default email charset.
  447. */
  448. $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
  449.  
  450. // Set custom headers
  451. if ( !empty( $headers ) ) {
  452. foreach ( (array) $headers as $name => $content ) {
  453. $phpmailer->addCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
  454. }
  455.  
  456. if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
  457. $phpmailer->addCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
  458. }
  459.  
  460. if ( !empty( $attachments ) ) {
  461. foreach ( $attachments as $attachment ) {
  462. try {
  463. $phpmailer->addAttachment($attachment);
  464. } catch ( phpmailerException $e ) {
  465. continue;
  466. }
  467. }
  468. }
  469.  
  470. /**
  471. * Fires after PHPMailer is initialized.
  472. *
  473. * @since 2.2.0
  474. *
  475. * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference).
  476. */
  477. do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
  478.  
  479. // Send!
  480. try {
  481. return $phpmailer->send();
  482. } catch ( phpmailerException $e ) {
  483.  
  484. $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
  485. $mail_error_data['phpmailer_exception_code'] = $e->getCode();
  486.  
  487. /**
  488. * Fires after a phpmailerException is caught.
  489. *
  490. * @since 4.4.0
  491. *
  492. * @param WP_Error $error A WP_Error object with the phpmailerException message, and an array
  493. * containing the mail recipient, subject, message, headers, and attachments.
  494. */
  495. do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );
  496.  
  497. return false;
  498. }
  499. }
  500. endif;
  501.  
  502. if ( !function_exists('wp_authenticate') ) :
  503. /**
  504. * Authenticate a user, confirming the login credentials are valid.
  505. *
  506. * @since 2.5.0
  507. * @since 4.5.0 `$username` now accepts an email address.
  508. *
  509. * @param string $username User's username or email address.
  510. * @param string $password User's password.
  511. * @return WP_User|WP_Error WP_User object if the credentials are valid,
  512. * otherwise WP_Error.
  513. */
  514. function wp_authenticate($username, $password) {
  515. $username = sanitize_user($username);
  516. $password = trim($password);
  517.  
  518. /**
  519. * Filters whether a set of user login credentials are valid.
  520. *
  521. * A WP_User object is returned if the credentials authenticate a user.
  522. * WP_Error or null otherwise.
  523. *
  524. * @since 2.8.0
  525. * @since 4.5.0 `$username` now accepts an email address.
  526. *
  527. * @param null|WP_User|WP_Error $user WP_User if the user is authenticated.
  528. * WP_Error or null otherwise.
  529. * @param string $username Username or email address.
  530. * @param string $password User password
  531. */
  532. $user = apply_filters( 'authenticate', null, $username, $password );
  533.  
  534. if ( $user == null ) {
  535. // TODO what should the error message be? (Or would these even happen?)
  536. // Only needed if all authentication handlers fail to return anything.
  537. $user = new WP_Error( 'authentication_failed', __( '<strong>ERROR</strong>: Invalid username, email address or incorrect password.' ) );
  538. }
  539.  
  540. $ignore_codes = array('empty_username', 'empty_password');
  541.  
  542. if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
  543. /**
  544. * Fires after a user login has failed.
  545. *
  546. * @since 2.5.0
  547. * @since 4.5.0 The value of `$username` can now be an email address.
  548. *
  549. * @param string $username Username or email address.
  550. */
  551. do_action( 'wp_login_failed', $username );
  552. }
  553.  
  554. return $user;
  555. }
  556. endif;
  557.  
  558. if ( !function_exists('wp_logout') ) :
  559. /**
  560. * Log the current user out.
  561. *
  562. * @since 2.5.0
  563. */
  564. function wp_logout() {
  565. wp_destroy_current_session();
  566. wp_clear_auth_cookie();
  567.  
  568. /**
  569. * Fires after a user is logged-out.
  570. *
  571. * @since 1.5.0
  572. */
  573. do_action( 'wp_logout' );
  574. }
  575. endif;
  576.  
  577. if ( !function_exists('wp_validate_auth_cookie') ) :
  578. /**
  579. * Validates authentication cookie.
  580. *
  581. * The checks include making sure that the authentication cookie is set and
  582. * pulling in the contents (if $cookie is not used).
  583. *
  584. * Makes sure the cookie is not expired. Verifies the hash in cookie is what is
  585. * should be and compares the two.
  586. *
  587. * @since 2.5.0
  588. *
  589. * @global int $login_grace_period
  590. *
  591. * @param string $cookie Optional. If used, will validate contents instead of cookie's
  592. * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
  593. * @return false|int False if invalid cookie, User ID if valid.
  594. */
  595. function wp_validate_auth_cookie($cookie = '', $scheme = '') {
  596. if ( ! $cookie_elements = wp_parse_auth_cookie($cookie, $scheme) ) {
  597. /**
  598. * Fires if an authentication cookie is malformed.
  599. *
  600. * @since 2.7.0
  601. *
  602. * @param string $cookie Malformed auth cookie.
  603. * @param string $scheme Authentication scheme. Values include 'auth', 'secure_auth',
  604. * or 'logged_in'.
  605. */
  606. do_action( 'auth_cookie_malformed', $cookie, $scheme );
  607. return false;
  608. }
  609.  
  610. $scheme = $cookie_elements['scheme'];
  611. $username = $cookie_elements['username'];
  612. $hmac = $cookie_elements['hmac'];
  613. $token = $cookie_elements['token'];
  614. $expired = $expiration = $cookie_elements['expiration'];
  615.  
  616. // Allow a grace period for POST and Ajax requests
  617. if ( wp_doing_ajax() || 'POST' == $_SERVER['REQUEST_METHOD'] ) {
  618. $expired += HOUR_IN_SECONDS;
  619. }
  620.  
  621. // Quick check to see if an honest cookie has expired
  622. if ( $expired < time() ) {
  623. /**
  624. * Fires once an authentication cookie has expired.
  625. *
  626. * @since 2.7.0
  627. *
  628. * @param array $cookie_elements An array of data for the authentication cookie.
  629. */
  630. do_action( 'auth_cookie_expired', $cookie_elements );
  631. return false;
  632. }
  633.  
  634. $user = get_user_by('login', $username);
  635. if ( ! $user ) {
  636. /**
  637. * Fires if a bad username is entered in the user authentication process.
  638. *
  639. * @since 2.7.0
  640. *
  641. * @param array $cookie_elements An array of data for the authentication cookie.
  642. */
  643. do_action( 'auth_cookie_bad_username', $cookie_elements );
  644. return false;
  645. }
  646.  
  647. $pass_frag = substr($user->user_pass, 8, 4);
  648.  
  649. $key = wp_hash( $username . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme );
  650.  
  651. // If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
  652. $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
  653. $hash = hash_hmac( $algo, $username . '|' . $expiration . '|' . $token, $key );
  654.  
  655. if ( ! hash_equals( $hash, $hmac ) ) {
  656. /**
  657. * Fires if a bad authentication cookie hash is encountered.
  658. *
  659. * @since 2.7.0
  660. *
  661. * @param array $cookie_elements An array of data for the authentication cookie.
  662. */
  663. do_action( 'auth_cookie_bad_hash', $cookie_elements );
  664. return false;
  665. }
  666.  
  667. $manager = WP_Session_Tokens::get_instance( $user->ID );
  668. if ( ! $manager->verify( $token ) ) {
  669. do_action( 'auth_cookie_bad_session_token', $cookie_elements );
  670. return false;
  671. }
  672.  
  673. // Ajax/POST grace period set above
  674. if ( $expiration < time() ) {
  675. $GLOBALS['login_grace_period'] = 1;
  676. }
  677.  
  678. /**
  679. * Fires once an authentication cookie has been validated.
  680. *
  681. * @since 2.7.0
  682. *
  683. * @param array $cookie_elements An array of data for the authentication cookie.
  684. * @param WP_User $user User object.
  685. */
  686. do_action( 'auth_cookie_valid', $cookie_elements, $user );
  687.  
  688. return $user->ID;
  689. }
  690. endif;
  691.  
  692. if ( !function_exists('wp_generate_auth_cookie') ) :
  693. /**
  694. * Generate authentication cookie contents.
  695. *
  696. * @since 2.5.0
  697. * @since 4.0.0 The `$token` parameter was added.
  698. *
  699. * @param int $user_id User ID
  700. * @param int $expiration The time the cookie expires as a UNIX timestamp.
  701. * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
  702. * @param string $token User's session token to use for this cookie
  703. * @return string Authentication cookie contents. Empty string if user does not exist.
  704. */
  705. function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) {
  706. $user = get_userdata($user_id);
  707. if ( ! $user ) {
  708. return '';
  709. }
  710.  
  711. if ( ! $token ) {
  712. $manager = WP_Session_Tokens::get_instance( $user_id );
  713. $token = $manager->create( $expiration );
  714. }
  715.  
  716. $pass_frag = substr($user->user_pass, 8, 4);
  717.  
  718. $key = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme );
  719.  
  720. // If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
  721. $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
  722. $hash = hash_hmac( $algo, $user->user_login . '|' . $expiration . '|' . $token, $key );
  723.  
  724. $cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash;
  725.  
  726. /**
  727. * Filters the authentication cookie.
  728. *
  729. * @since 2.5.0
  730. * @since 4.0.0 The `$token` parameter was added.
  731. *
  732. * @param string $cookie Authentication cookie.
  733. * @param int $user_id User ID.
  734. * @param int $expiration The time the cookie expires as a UNIX timestamp.
  735. * @param string $scheme Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'.
  736. * @param string $token User's session token used.
  737. */
  738. return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token );
  739. }
  740. endif;
  741.  
  742. if ( !function_exists('wp_parse_auth_cookie') ) :
  743. /**
  744. * Parse a cookie into its components
  745. *
  746. * @since 2.7.0
  747. *
  748. * @param string $cookie
  749. * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
  750. * @return array|false Authentication cookie components
  751. */
  752. function wp_parse_auth_cookie($cookie = '', $scheme = '') {
  753. if ( empty($cookie) ) {
  754. switch ($scheme){
  755. case 'auth':
  756. $cookie_name = AUTH_COOKIE;
  757. break;
  758. case 'secure_auth':
  759. $cookie_name = SECURE_AUTH_COOKIE;
  760. break;
  761. case "logged_in":
  762. $cookie_name = LOGGED_IN_COOKIE;
  763. break;
  764. default:
  765. if ( is_ssl() ) {
  766. $cookie_name = SECURE_AUTH_COOKIE;
  767. $scheme = 'secure_auth';
  768. } else {
  769. $cookie_name = AUTH_COOKIE;
  770. $scheme = 'auth';
  771. }
  772. }
  773.  
  774. if ( empty($_COOKIE[$cookie_name]) )
  775. return false;
  776. $cookie = $_COOKIE[$cookie_name];
  777. }
  778.  
  779. $cookie_elements = explode('|', $cookie);
  780. if ( count( $cookie_elements ) !== 4 ) {
  781. return false;
  782. }
  783.  
  784. list( $username, $expiration, $token, $hmac ) = $cookie_elements;
  785.  
  786. return compact( 'username', 'expiration', 'token', 'hmac', 'scheme' );
  787. }
  788. endif;
  789.  
  790. if ( !function_exists('wp_set_auth_cookie') ) :
  791. /**
  792. * Log in a user by setting authentication cookies.
  793. *
  794. * The $remember parameter increases the time that the cookie will be kept. The
  795. * default the cookie is kept without remembering is two days. When $remember is
  796. * set, the cookies will be kept for 14 days or two weeks.
  797. *
  798. * @since 2.5.0
  799. * @since 4.3.0 Added the `$token` parameter.
  800. *
  801. * @param int $user_id User ID
  802. * @param bool $remember Whether to remember the user
  803. * @param mixed $secure Whether the admin cookies should only be sent over HTTPS.
  804. * Default is_ssl().
  805. * @param string $token Optional. User's session token to use for this cookie.
  806. */
  807. function wp_set_auth_cookie( $user_id, $remember = false, $secure = '', $token = '' ) {
  808. if ( $remember ) {
  809. /**
  810. * Filters the duration of the authentication cookie expiration period.
  811. *
  812. * @since 2.8.0
  813. *
  814. * @param int $length Duration of the expiration period in seconds.
  815. * @param int $user_id User ID.
  816. * @param bool $remember Whether to remember the user login. Default false.
  817. */
  818. $expiration = time() + apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember );
  819.  
  820. /*
  821. * Ensure the browser will continue to send the cookie after the expiration time is reached.
  822. * Needed for the login grace period in wp_validate_auth_cookie().
  823. */
  824. $expire = $expiration + ( 12 * HOUR_IN_SECONDS );
  825. } else {
  826. /** This filter is documented in wp-includes/pluggable.php */
  827. $expiration = time() + apply_filters( 'auth_cookie_expiration', 2 * DAY_IN_SECONDS, $user_id, $remember );
  828. $expire = 0;
  829. }
  830.  
  831. if ( '' === $secure ) {
  832. $secure = is_ssl();
  833. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement