PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/pluggable.php

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

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