wp-login.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. <?php
  2. /**
  3. * WordPress User Page
  4. *
  5. * Handles authentication, registering, resetting passwords, forgot password,
  6. * and other user handling.
  7. *
  8. * @package WordPress
  9. */
  10. /** Make sure that the WordPress bootstrap has run before continuing. */
  11. require( dirname(__FILE__) . '/wp-load.php' );
  12. // Redirect to https login if forced to use SSL
  13. if ( force_ssl_admin() && ! is_ssl() ) {
  14. if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
  15. wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
  16. exit();
  17. } else {
  18. wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  19. exit();
  20. }
  21. }
  22. /**
  23. * Output the login page header.
  24. *
  25. * @param string $title Optional. WordPress login Page title to display in the `<title>` element.
  26. * Default 'Log In'.
  27. * @param string $message Optional. Message to display in header. Default empty.
  28. * @param WP_Error $wp_error Optional. The error to pass. Default empty.
  29. */
  30. function login_header( $title = 'Log In', $message = '', $wp_error = '' ) {
  31. global $error, $interim_login, $action;
  32. // Don't index any of these forms
  33. add_action( 'login_head', 'wp_no_robots' );
  34. add_action( 'login_head', 'wp_login_viewport_meta' );
  35. if ( empty($wp_error) )
  36. $wp_error = new WP_Error();
  37. // Shake it!
  38. $shake_error_codes = array( 'empty_password', 'empty_email', 'invalid_email', 'invalidcombo', 'empty_username', 'invalid_username', 'incorrect_password' );
  39. /**
  40. * Filters the error codes array for shaking the login form.
  41. *
  42. * @since 3.0.0
  43. *
  44. * @param array $shake_error_codes Error codes that shake the login form.
  45. */
  46. $shake_error_codes = apply_filters( 'shake_error_codes', $shake_error_codes );
  47. if ( $shake_error_codes && $wp_error->get_error_code() && in_array( $wp_error->get_error_code(), $shake_error_codes ) )
  48. add_action( 'login_head', 'wp_shake_js', 12 );
  49. $separator = is_rtl() ? ' &rsaquo; ' : ' &lsaquo; ';
  50. ?><!DOCTYPE html>
  51. <!--[if IE 8]>
  52. <html xmlns="http://www.w3.org/1999/xhtml" class="ie8" <?php language_attributes(); ?>>
  53. <![endif]-->
  54. <!--[if !(IE 8) ]><!-->
  55. <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
  56. <!--<![endif]-->
  57. <head>
  58. <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
  59. <title><?php echo get_bloginfo( 'name', 'display' ) . $separator . $title; ?></title>
  60. <?php
  61. wp_enqueue_style( 'login' );
  62. /*
  63. * Remove all stored post data on logging out.
  64. * This could be added by add_action('login_head'...) like wp_shake_js(),
  65. * but maybe better if it's not removable by plugins
  66. */
  67. if ( 'loggedout' == $wp_error->get_error_code() ) {
  68. ?>
  69. <script>if("sessionStorage" in window){try{for(var key in sessionStorage){if(key.indexOf("wp-autosave-")!=-1){sessionStorage.removeItem(key)}}}catch(e){}};</script>
  70. <?php
  71. }
  72. /**
  73. * Enqueue scripts and styles for the login page.
  74. *
  75. * @since 3.1.0
  76. */
  77. do_action( 'login_enqueue_scripts' );
  78. /**
  79. * Fires in the login page header after scripts are enqueued.
  80. *
  81. * @since 2.1.0
  82. */
  83. do_action( 'login_head' );
  84. if ( is_multisite() ) {
  85. $login_header_url = network_home_url();
  86. $login_header_title = get_network()->site_name;
  87. } else {
  88. $login_header_url = __( 'https://wordpress.org/' );
  89. $login_header_title = __( 'Powered by WordPress' );
  90. }
  91. /**
  92. * Filters link URL of the header logo above login form.
  93. *
  94. * @since 2.1.0
  95. *
  96. * @param string $login_header_url Login header logo URL.
  97. */
  98. $login_header_url = apply_filters( 'login_headerurl', $login_header_url );
  99. /**
  100. * Filters the title attribute of the header logo above login form.
  101. *
  102. * @since 2.1.0
  103. *
  104. * @param string $login_header_title Login header logo title attribute.
  105. */
  106. $login_header_title = apply_filters( 'login_headertitle', $login_header_title );
  107. $classes = array( 'login-action-' . $action, 'wp-core-ui' );
  108. if ( is_rtl() )
  109. $classes[] = 'rtl';
  110. if ( $interim_login ) {
  111. $classes[] = 'interim-login';
  112. ?>
  113. <style type="text/css">html{background-color: transparent;}</style>
  114. <?php
  115. if ( 'success' === $interim_login )
  116. $classes[] = 'interim-login-success';
  117. }
  118. $classes[] =' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
  119. /**
  120. * Filters the login page body classes.
  121. *
  122. * @since 3.5.0
  123. *
  124. * @param array $classes An array of body classes.
  125. * @param string $action The action that brought the visitor to the login page.
  126. */
  127. $classes = apply_filters( 'login_body_class', $classes, $action );
  128. ?>
  129. </head>
  130. <body class="login <?php echo esc_attr( implode( ' ', $classes ) ); ?>">
  131. <?php
  132. /**
  133. * Fires in the login page header after the body tag is opened.
  134. *
  135. * @since 4.6.0
  136. */
  137. do_action( 'login_header' );
  138. ?>
  139. <div id="login">
  140. <h1><a href="<?php echo esc_url( $login_header_url ); ?>" title="<?php echo esc_attr( $login_header_title ); ?>" tabindex="-1"><?php bloginfo( 'name' ); ?></a></h1>
  141. <?php
  142. unset( $login_header_url, $login_header_title );
  143. /**
  144. * Filters the message to display above the login form.
  145. *
  146. * @since 2.1.0
  147. *
  148. * @param string $message Login message text.
  149. */
  150. $message = apply_filters( 'login_message', $message );
  151. if ( !empty( $message ) )
  152. echo $message . "\n";
  153. // In case a plugin uses $error rather than the $wp_errors object
  154. if ( !empty( $error ) ) {
  155. $wp_error->add('error', $error);
  156. unset($error);
  157. }
  158. if ( $wp_error->get_error_code() ) {
  159. $errors = '';
  160. $messages = '';
  161. foreach ( $wp_error->get_error_codes() as $code ) {
  162. $severity = $wp_error->get_error_data( $code );
  163. foreach ( $wp_error->get_error_messages( $code ) as $error_message ) {
  164. if ( 'message' == $severity )
  165. $messages .= ' ' . $error_message . "<br />\n";
  166. else
  167. $errors .= ' ' . $error_message . "<br />\n";
  168. }
  169. }
  170. if ( ! empty( $errors ) ) {
  171. /**
  172. * Filters the error messages displayed above the login form.
  173. *
  174. * @since 2.1.0
  175. *
  176. * @param string $errors Login error message.
  177. */
  178. echo '<div id="login_error">' . apply_filters( 'login_errors', $errors ) . "</div>\n";
  179. }
  180. if ( ! empty( $messages ) ) {
  181. /**
  182. * Filters instructional messages displayed above the login form.
  183. *
  184. * @since 2.5.0
  185. *
  186. * @param string $messages Login messages.
  187. */
  188. echo '<p class="message">' . apply_filters( 'login_messages', $messages ) . "</p>\n";
  189. }
  190. }
  191. } // End of login_header()
  192. /**
  193. * Outputs the footer for the login page.
  194. *
  195. * @param string $input_id Which input to auto-focus
  196. */
  197. function login_footer($input_id = '') {
  198. global $interim_login;
  199. // Don't allow interim logins to navigate away from the page.
  200. if ( ! $interim_login ): ?>
  201. <p id="backtoblog"><a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php
  202. /* translators: %s: site title */
  203. printf( _x( '&larr; Back to %s', 'site' ), get_bloginfo( 'title', 'display' ) );
  204. ?></a></p>
  205. <?php endif; ?>
  206. </div>
  207. <?php if ( !empty($input_id) ) : ?>
  208. <script type="text/javascript">
  209. try{document.getElementById('<?php echo $input_id; ?>').focus();}catch(e){}
  210. if(typeof wpOnload=='function')wpOnload();
  211. </script>
  212. <?php endif; ?>
  213. <?php
  214. /**
  215. * Fires in the login page footer.
  216. *
  217. * @since 3.1.0
  218. */
  219. do_action( 'login_footer' ); ?>
  220. <div class="clear"></div>
  221. </body>
  222. </html>
  223. <?php
  224. }
  225. /**
  226. * @since 3.0.0
  227. */
  228. function wp_shake_js() {
  229. ?>
  230. <script type="text/javascript">
  231. addLoadEvent = function(func){if(typeof jQuery!="undefined")jQuery(document).ready(func);else if(typeof wpOnload!='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
  232. function s(id,pos){g(id).left=pos+'px';}
  233. function g(id){return document.getElementById(id).style;}
  234. function shake(id,a,d){c=a.shift();s(id,c);if(a.length>0){setTimeout(function(){shake(id,a,d);},d);}else{try{g(id).position='static';wp_attempt_focus();}catch(e){}}}
  235. addLoadEvent(function(){ var p=new Array(15,30,15,0,-15,-30,-15,0);p=p.concat(p.concat(p));var i=document.forms[0].id;g(i).position='relative';shake(i,p,20);});
  236. </script>
  237. <?php
  238. }
  239. /**
  240. * @since 3.7.0
  241. */
  242. function wp_login_viewport_meta() {
  243. ?>
  244. <meta name="viewport" content="width=device-width" />
  245. <?php
  246. }
  247. /**
  248. * Handles sending password retrieval email to user.
  249. *
  250. * @return bool|WP_Error True: when finish. WP_Error on error
  251. */
  252. function retrieve_password() {
  253. $errors = new WP_Error();
  254. if ( empty( $_POST['user_login'] ) ) {
  255. $errors->add('empty_username', __('<strong>ERROR</strong>: Enter a username or email address.'));
  256. } elseif ( strpos( $_POST['user_login'], '@' ) ) {
  257. $user_data = get_user_by( 'email', trim( wp_unslash( $_POST['user_login'] ) ) );
  258. if ( empty( $user_data ) )
  259. $errors->add('invalid_email', __('<strong>ERROR</strong>: There is no user registered with that email address.'));
  260. } else {
  261. $login = trim($_POST['user_login']);
  262. $user_data = get_user_by('login', $login);
  263. }
  264. /**
  265. * Fires before errors are returned from a password reset request.
  266. *
  267. * @since 2.1.0
  268. * @since 4.4.0 Added the `$errors` parameter.
  269. *
  270. * @param WP_Error $errors A WP_Error object containing any errors generated
  271. * by using invalid credentials.
  272. */
  273. do_action( 'lostpassword_post', $errors );
  274. if ( $errors->get_error_code() )
  275. return $errors;
  276. if ( !$user_data ) {
  277. $errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or email.'));
  278. return $errors;
  279. }
  280. // Redefining user_login ensures we return the right case in the email.
  281. $user_login = $user_data->user_login;
  282. $user_email = $user_data->user_email;
  283. $key = get_password_reset_key( $user_data );
  284. if ( is_wp_error( $key ) ) {
  285. return $key;
  286. }
  287. $message = __('Someone has requested a password reset for the following account:') . "\r\n\r\n";
  288. $message .= network_home_url( '/' ) . "\r\n\r\n";
  289. $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
  290. $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
  291. $message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
  292. $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";
  293. if ( is_multisite() ) {
  294. $blogname = get_network()->site_name;
  295. } else {
  296. /*
  297. * The blogname option is escaped with esc_html on the way into the database
  298. * in sanitize_option we want to reverse this for the plain text arena of emails.
  299. */
  300. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  301. }
  302. /* translators: Password reset email subject. 1: Site name */
  303. $title = sprintf( __('[%s] Password Reset'), $blogname );
  304. /**
  305. * Filters the subject of the password reset email.
  306. *
  307. * @since 2.8.0
  308. * @since 4.4.0 Added the `$user_login` and `$user_data` parameters.
  309. *
  310. * @param string $title Default email title.
  311. * @param string $user_login The username for the user.
  312. * @param WP_User $user_data WP_User object.
  313. */
  314. $title = apply_filters( 'retrieve_password_title', $title, $user_login, $user_data );
  315. /**
  316. * Filters the message body of the password reset mail.
  317. *
  318. * @since 2.8.0
  319. * @since 4.1.0 Added `$user_login` and `$user_data` parameters.
  320. *
  321. * @param string $message Default mail message.
  322. * @param string $key The activation key.
  323. * @param string $user_login The username for the user.
  324. * @param WP_User $user_data WP_User object.
  325. */
  326. $message = apply_filters( 'retrieve_password_message', $message, $key, $user_login, $user_data );
  327. if ( $message && !wp_mail( $user_email, wp_specialchars_decode( $title ), $message ) )
  328. wp_die( __('The email could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function.') );
  329. return true;
  330. }
  331. //
  332. // Main
  333. //
  334. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'login';
  335. $errors = new WP_Error();
  336. if ( isset($_GET['key']) )
  337. $action = 'resetpass';
  338. // validate action so as to default to the login screen
  339. if ( !in_array( $action, array( 'postpass', 'logout', 'lostpassword', 'retrievepassword', 'resetpass', 'rp', 'register', 'login' ), true ) && false === has_filter( 'login_form_' . $action ) )
  340. $action = 'login';
  341. nocache_headers();
  342. header('Content-Type: '.get_bloginfo('html_type').'; charset='.get_bloginfo('charset'));
  343. if ( defined( 'RELOCATE' ) && RELOCATE ) { // Move flag is set
  344. if ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != $_SERVER['PHP_SELF']) )
  345. $_SERVER['PHP_SELF'] = str_replace( $_SERVER['PATH_INFO'], '', $_SERVER['PHP_SELF'] );
  346. $url = dirname( set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ) );
  347. if ( $url != get_option( 'siteurl' ) )
  348. update_option( 'siteurl', $url );
  349. }
  350. //Set a cookie now to see if they are supported by the browser.
  351. $secure = ( 'https' === parse_url( wp_login_url(), PHP_URL_SCHEME ) );
  352. setcookie( TEST_COOKIE, 'WP Cookie check', 0, COOKIEPATH, COOKIE_DOMAIN, $secure );
  353. if ( SITECOOKIEPATH != COOKIEPATH )
  354. setcookie( TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );
  355. /**
  356. * Fires when the login form is initialized.
  357. *
  358. * @since 3.2.0
  359. */
  360. do_action( 'login_init' );
  361. /**
  362. * Fires before a specified login form action.
  363. *
  364. * The dynamic portion of the hook name, `$action`, refers to the action
  365. * that brought the visitor to the login form. Actions include 'postpass',
  366. * 'logout', 'lostpassword', etc.
  367. *
  368. * @since 2.8.0
  369. */
  370. do_action( "login_form_{$action}" );
  371. $http_post = ('POST' == $_SERVER['REQUEST_METHOD']);
  372. $interim_login = isset($_REQUEST['interim-login']);
  373. switch ($action) {
  374. case 'postpass' :
  375. if ( ! array_key_exists( 'post_password', $_POST ) ) {
  376. wp_safe_redirect( wp_get_referer() );
  377. exit();
  378. }
  379. $hasher = new PasswordHash( 8, true );
  380. /**
  381. * Filters the life span of the post password cookie.
  382. *
  383. * By default, the cookie expires 10 days from creation. To turn this
  384. * into a session cookie, return 0.
  385. *
  386. * @since 3.7.0
  387. *
  388. * @param int $expires The expiry time, as passed to setcookie().
  389. */
  390. $expire = apply_filters( 'post_password_expires', time() + 10 * DAY_IN_SECONDS );
  391. $referer = wp_get_referer();
  392. if ( $referer ) {
  393. $secure = ( 'https' === parse_url( $referer, PHP_URL_SCHEME ) );
  394. } else {
  395. $secure = false;
  396. }
  397. setcookie( 'wp-postpass_' . COOKIEHASH, $hasher->HashPassword( wp_unslash( $_POST['post_password'] ) ), $expire, COOKIEPATH, COOKIE_DOMAIN, $secure );
  398. wp_safe_redirect( wp_get_referer() );
  399. exit();
  400. case 'logout' :
  401. check_admin_referer('log-out');
  402. $user = wp_get_current_user();
  403. wp_logout();
  404. if ( ! empty( $_REQUEST['redirect_to'] ) ) {
  405. $redirect_to = $requested_redirect_to = $_REQUEST['redirect_to'];
  406. } else {
  407. $redirect_to = 'wp-login.php?loggedout=true';
  408. $requested_redirect_to = '';
  409. }
  410. /**
  411. * Filters the log out redirect URL.
  412. *
  413. * @since 4.2.0
  414. *
  415. * @param string $redirect_to The redirect destination URL.
  416. * @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
  417. * @param WP_User $user The WP_User object for the user that's logging out.
  418. */
  419. $redirect_to = apply_filters( 'logout_redirect', $redirect_to, $requested_redirect_to, $user );
  420. wp_safe_redirect( $redirect_to );
  421. exit();
  422. case 'lostpassword' :
  423. case 'retrievepassword' :
  424. if ( $http_post ) {
  425. $errors = retrieve_password();
  426. if ( !is_wp_error($errors) ) {
  427. $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : 'wp-login.php?checkemail=confirm';
  428. wp_safe_redirect( $redirect_to );
  429. exit();
  430. }
  431. }
  432. if ( isset( $_GET['error'] ) ) {
  433. if ( 'invalidkey' == $_GET['error'] ) {
  434. $errors->add( 'invalidkey', __( 'Your password reset link appears to be invalid. Please request a new link below.' ) );
  435. } elseif ( 'expiredkey' == $_GET['error'] ) {
  436. $errors->add( 'expiredkey', __( 'Your password reset link has expired. Please request a new link below.' ) );
  437. }
  438. }
  439. $lostpassword_redirect = ! empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
  440. /**
  441. * Filters the URL redirected to after submitting the lostpassword/retrievepassword form.
  442. *
  443. * @since 3.0.0
  444. *
  445. * @param string $lostpassword_redirect The redirect destination URL.
  446. */
  447. $redirect_to = apply_filters( 'lostpassword_redirect', $lostpassword_redirect );
  448. /**
  449. * Fires before the lost password form.
  450. *
  451. * @since 1.5.1
  452. */
  453. do_action( 'lost_password' );
  454. login_header(__('Lost Password'), '<p class="message">' . __('Please enter your username or email address. You will receive a link to create a new password via email.') . '</p>', $errors);
  455. $user_login = isset($_POST['user_login']) ? wp_unslash($_POST['user_login']) : '';
  456. ?>
  457. <form name="lostpasswordform" id="lostpasswordform" action="<?php echo esc_url( network_site_url( 'wp-login.php?action=lostpassword', 'login_post' ) ); ?>" method="post">
  458. <p>
  459. <label for="user_login" ><?php _e( 'Username or Email Address' ); ?><br />
  460. <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr($user_login); ?>" size="20" /></label>
  461. </p>
  462. <?php
  463. /**
  464. * Fires inside the lostpassword form tags, before the hidden fields.
  465. *
  466. * @since 2.1.0
  467. */
  468. do_action( 'lostpassword_form' ); ?>
  469. <input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
  470. <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="<?php esc_attr_e('Get New Password'); ?>" /></p>
  471. </form>
  472. <p id="nav">
  473. <a href="<?php echo esc_url( wp_login_url() ); ?>"><?php _e('Log in') ?></a>
  474. <?php
  475. if ( get_option( 'users_can_register' ) ) :
  476. $registration_url = sprintf( '<a href="%s">%s</a>', esc_url( wp_registration_url() ), __( 'Register' ) );
  477. /** This filter is documented in wp-includes/general-template.php */
  478. echo ' | ' . apply_filters( 'register', $registration_url );
  479. endif;
  480. ?>
  481. </p>
  482. <?php
  483. login_footer('user_login');
  484. break;
  485. case 'resetpass' :
  486. case 'rp' :
  487. list( $rp_path ) = explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) );
  488. $rp_cookie = 'wp-resetpass-' . COOKIEHASH;
  489. if ( isset( $_GET['key'] ) ) {
  490. $value = sprintf( '%s:%s', wp_unslash( $_GET['login'] ), wp_unslash( $_GET['key'] ) );
  491. setcookie( $rp_cookie, $value, 0, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
  492. wp_safe_redirect( remove_query_arg( array( 'key', 'login' ) ) );
  493. exit;
  494. }
  495. if ( isset( $_COOKIE[ $rp_cookie ] ) && 0 < strpos( $_COOKIE[ $rp_cookie ], ':' ) ) {
  496. list( $rp_login, $rp_key ) = explode( ':', wp_unslash( $_COOKIE[ $rp_cookie ] ), 2 );
  497. $user = check_password_reset_key( $rp_key, $rp_login );
  498. if ( isset( $_POST['pass1'] ) && ! hash_equals( $rp_key, $_POST['rp_key'] ) ) {
  499. $user = false;
  500. }
  501. } else {
  502. $user = false;
  503. }
  504. if ( ! $user || is_wp_error( $user ) ) {
  505. setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
  506. if ( $user && $user->get_error_code() === 'expired_key' )
  507. wp_redirect( site_url( 'wp-login.php?action=lostpassword&error=expiredkey' ) );
  508. else
  509. wp_redirect( site_url( 'wp-login.php?action=lostpassword&error=invalidkey' ) );
  510. exit;
  511. }
  512. $errors = new WP_Error();
  513. if ( isset($_POST['pass1']) && $_POST['pass1'] != $_POST['pass2'] )
  514. $errors->add( 'password_reset_mismatch', __( 'The passwords do not match.' ) );
  515. /**
  516. * Fires before the password reset procedure is validated.
  517. *
  518. * @since 3.5.0
  519. *
  520. * @param object $errors WP Error object.
  521. * @param WP_User|WP_Error $user WP_User object if the login and reset key match. WP_Error object otherwise.
  522. */
  523. do_action( 'validate_password_reset', $errors, $user );
  524. if ( ( ! $errors->get_error_code() ) && isset( $_POST['pass1'] ) && !empty( $_POST['pass1'] ) ) {
  525. reset_password($user, $_POST['pass1']);
  526. setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
  527. login_header( __( 'Password Reset' ), '<p class="message reset-pass">' . __( 'Your password has been reset.' ) . ' <a href="' . esc_url( wp_login_url() ) . '">' . __( 'Log in' ) . '</a></p>' );
  528. login_footer();
  529. exit;
  530. }
  531. wp_enqueue_script('utils');
  532. wp_enqueue_script('user-profile');
  533. login_header(__('Reset Password'), '<p class="message reset-pass">' . __('Enter your new password below.') . '</p>', $errors );
  534. ?>
  535. <form name="resetpassform" id="resetpassform" action="<?php echo esc_url( network_site_url( 'wp-login.php?action=resetpass', 'login_post' ) ); ?>" method="post" autocomplete="off">
  536. <input type="hidden" id="user_login" value="<?php echo esc_attr( $rp_login ); ?>" autocomplete="off" />
  537. <div class="user-pass1-wrap">
  538. <p>
  539. <label for="pass1"><?php _e( 'New password' ) ?></label>
  540. </p>
  541. <div class="wp-pwd">
  542. <span class="password-input-wrapper">
  543. <input type="password" data-reveal="1" data-pw="<?php echo esc_attr( wp_generate_password( 16 ) ); ?>" name="pass1" id="pass1" class="input" size="20" value="" autocomplete="off" aria-describedby="pass-strength-result" />
  544. </span>
  545. <div id="pass-strength-result" class="hide-if-no-js" aria-live="polite"><?php _e( 'Strength indicator' ); ?></div>
  546. </div>
  547. </div>
  548. <p class="user-pass2-wrap">
  549. <label for="pass2"><?php _e( 'Confirm new password' ) ?></label><br />
  550. <input type="password" name="pass2" id="pass2" class="input" size="20" value="" autocomplete="off" />
  551. </p>
  552. <p class="description indicator-hint"><?php echo wp_get_password_hint(); ?></p>
  553. <br class="clear" />
  554. <?php
  555. /**
  556. * Fires following the 'Strength indicator' meter in the user password reset form.
  557. *
  558. * @since 3.9.0
  559. *
  560. * @param WP_User $user User object of the user whose password is being reset.
  561. */
  562. do_action( 'resetpass_form', $user );
  563. ?>
  564. <input type="hidden" name="rp_key" value="<?php echo esc_attr( $rp_key ); ?>" />
  565. <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="<?php esc_attr_e('Reset Password'); ?>" /></p>
  566. </form>
  567. <p id="nav">
  568. <a href="<?php echo esc_url( wp_login_url() ); ?>"><?php _e( 'Log in' ); ?></a>
  569. <?php
  570. if ( get_option( 'users_can_register' ) ) :
  571. $registration_url = sprintf( '<a href="%s">%s</a>', esc_url( wp_registration_url() ), __( 'Register' ) );
  572. /** This filter is documented in wp-includes/general-template.php */
  573. echo ' | ' . apply_filters( 'register', $registration_url );
  574. endif;
  575. ?>
  576. </p>
  577. <?php
  578. login_footer('user_pass');
  579. break;
  580. case 'register' :
  581. if ( is_multisite() ) {
  582. /**
  583. * Filters the Multisite sign up URL.
  584. *
  585. * @since 3.0.0
  586. *
  587. * @param string $sign_up_url The sign up URL.
  588. */
  589. wp_redirect( apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ) );
  590. exit;
  591. }
  592. if ( !get_option('users_can_register') ) {
  593. wp_redirect( site_url('wp-login.php?registration=disabled') );
  594. exit();
  595. }
  596. $user_login = '';
  597. $user_email = '';
  598. if ( $http_post ) {
  599. $user_login = isset( $_POST['user_login'] ) ? $_POST['user_login'] : '';
  600. $user_email = isset( $_POST['user_email'] ) ? $_POST['user_email'] : '';
  601. $errors = register_new_user($user_login, $user_email);
  602. if ( !is_wp_error($errors) ) {
  603. $redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?checkemail=registered';
  604. wp_safe_redirect( $redirect_to );
  605. exit();
  606. }
  607. }
  608. $registration_redirect = ! empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
  609. /**
  610. * Filters the registration redirect URL.
  611. *
  612. * @since 3.0.0
  613. *
  614. * @param string $registration_redirect The redirect destination URL.
  615. */
  616. $redirect_to = apply_filters( 'registration_redirect', $registration_redirect );
  617. login_header(__('Registration Form'), '<p class="message register">' . __('Register For This Site') . '</p>', $errors);
  618. ?>
  619. <form name="registerform" id="registerform" action="<?php echo esc_url( site_url( 'wp-login.php?action=register', 'login_post' ) ); ?>" method="post" novalidate="novalidate">
  620. <p>
  621. <label for="user_login"><?php _e('Username') ?><br />
  622. <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr(wp_unslash($user_login)); ?>" size="20" /></label>
  623. </p>
  624. <p>
  625. <label for="user_email"><?php _e('Email') ?><br />
  626. <input type="email" name="user_email" id="user_email" class="input" value="<?php echo esc_attr( wp_unslash( $user_email ) ); ?>" size="25" /></label>
  627. </p>
  628. <?php
  629. /**
  630. * Fires following the 'Email' field in the user registration form.
  631. *
  632. * @since 2.1.0
  633. */
  634. do_action( 'register_form' );
  635. ?>
  636. <p id="reg_passmail"><?php _e( 'Registration confirmation will be emailed to you.' ); ?></p>
  637. <br class="clear" />
  638. <input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
  639. <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="<?php esc_attr_e('Register'); ?>" /></p>
  640. </form>
  641. <p id="nav">
  642. <a href="<?php echo esc_url( wp_login_url() ); ?>"><?php _e( 'Log in' ); ?></a> |
  643. <a href="<?php echo esc_url( wp_lostpassword_url() ); ?>"><?php _e( 'Lost your password?' ); ?></a>
  644. </p>
  645. <?php
  646. login_footer('user_login');
  647. break;
  648. case 'login' :
  649. default:
  650. $secure_cookie = '';
  651. $customize_login = isset( $_REQUEST['customize-login'] );
  652. if ( $customize_login )
  653. wp_enqueue_script( 'customize-base' );
  654. // If the user wants ssl but the session is not ssl, force a secure cookie.
  655. if ( !empty($_POST['log']) && !force_ssl_admin() ) {
  656. $user_name = sanitize_user($_POST['log']);
  657. $user = get_user_by( 'login', $user_name );
  658. if ( ! $user && strpos( $user_name, '@' ) ) {
  659. $user = get_user_by( 'email', $user_name );
  660. }
  661. if ( $user ) {
  662. if ( get_user_option('use_ssl', $user->ID) ) {
  663. $secure_cookie = true;
  664. force_ssl_admin(true);
  665. }
  666. }
  667. }
  668. if ( isset( $_REQUEST['redirect_to'] ) ) {
  669. $redirect_to = $_REQUEST['redirect_to'];
  670. // Redirect to https if user wants ssl
  671. if ( $secure_cookie && false !== strpos($redirect_to, 'wp-admin') )
  672. $redirect_to = preg_replace('|^http://|', 'https://', $redirect_to);
  673. } else {
  674. $redirect_to = admin_url();
  675. }
  676. $reauth = empty($_REQUEST['reauth']) ? false : true;
  677. $user = wp_signon( array(), $secure_cookie );
  678. if ( empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
  679. if ( headers_sent() ) {
  680. /* translators: 1: Browser cookie documentation URL, 2: Support forums URL */
  681. $user = new WP_Error( 'test_cookie', sprintf( __( '<strong>ERROR</strong>: Cookies are blocked due to unexpected output. For help, please see <a href="%1$s">this documentation</a> or try the <a href="%2$s">support forums</a>.' ),
  682. __( 'https://codex.wordpress.org/Cookies' ), __( 'https://wordpress.org/support/' ) ) );
  683. } elseif ( isset( $_POST['testcookie'] ) && empty( $_COOKIE[ TEST_COOKIE ] ) ) {
  684. // If cookies are disabled we can't log in even with a valid user+pass
  685. /* translators: 1: Browser cookie documentation URL */
  686. $user = new WP_Error( 'test_cookie', sprintf( __( '<strong>ERROR</strong>: Cookies are blocked or not supported by your browser. You must <a href="%s">enable cookies</a> to use WordPress.' ),
  687. __( 'https://codex.wordpress.org/Cookies' ) ) );
  688. }
  689. }
  690. $requested_redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
  691. /**
  692. * Filters the login redirect URL.
  693. *
  694. * @since 3.0.0
  695. *
  696. * @param string $redirect_to The redirect destination URL.
  697. * @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
  698. * @param WP_User|WP_Error $user WP_User object if login was successful, WP_Error object otherwise.
  699. */
  700. $redirect_to = apply_filters( 'login_redirect', $redirect_to, $requested_redirect_to, $user );
  701. if ( !is_wp_error($user) && !$reauth ) {
  702. if ( $interim_login ) {
  703. $message = '<p class="message">' . __('You have logged in successfully.') . '</p>';
  704. $interim_login = 'success';
  705. login_header( '', $message ); ?>
  706. </div>
  707. <?php
  708. /** This action is documented in wp-login.php */
  709. do_action( 'login_footer' ); ?>
  710. <?php if ( $customize_login ) : ?>
  711. <script type="text/javascript">setTimeout( function(){ new wp.customize.Messenger({ url: '<?php echo wp_customize_url(); ?>', channel: 'login' }).send('login') }, 1000 );</script>
  712. <?php endif; ?>
  713. </body></html>
  714. <?php exit;
  715. }
  716. if ( ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' || $redirect_to == admin_url() ) ) {
  717. // If the user doesn't belong to a blog, send them to user admin. If the user can't edit posts, send them to their profile.
  718. if ( is_multisite() && !get_active_blog_for_user($user->ID) && !is_super_admin( $user->ID ) )
  719. $redirect_to = user_admin_url();
  720. elseif ( is_multisite() && !$user->has_cap('read') )
  721. $redirect_to = get_dashboard_url( $user->ID );
  722. elseif ( !$user->has_cap('edit_posts') )
  723. $redirect_to = $user->has_cap( 'read' ) ? admin_url( 'profile.php' ) : home_url();
  724. wp_redirect( $redirect_to );
  725. exit();
  726. }
  727. wp_safe_redirect($redirect_to);
  728. exit();
  729. }
  730. $errors = $user;
  731. // Clear errors if loggedout is set.
  732. if ( !empty($_GET['loggedout']) || $reauth )
  733. $errors = new WP_Error();
  734. if ( $interim_login ) {
  735. if ( ! $errors->get_error_code() )
  736. $errors->add( 'expired', __( 'Your session has expired. Please log in to continue where you left off.' ), 'message' );
  737. } else {
  738. // Some parts of this script use the main login form to display a message
  739. if ( isset($_GET['loggedout']) && true == $_GET['loggedout'] )
  740. $errors->add('loggedout', __('You are now logged out.'), 'message');
  741. elseif ( isset($_GET['registration']) && 'disabled' == $_GET['registration'] )
  742. $errors->add('registerdisabled', __('User registration is currently not allowed.'));
  743. elseif ( isset($_GET['checkemail']) && 'confirm' == $_GET['checkemail'] )
  744. $errors->add('confirm', __('Check your email for the confirmation link.'), 'message');
  745. elseif ( isset($_GET['checkemail']) && 'newpass' == $_GET['checkemail'] )
  746. $errors->add('newpass', __('Check your email for your new password.'), 'message');
  747. elseif ( isset($_GET['checkemail']) && 'registered' == $_GET['checkemail'] )
  748. $errors->add('registered', __('Registration complete. Please check your email.'), 'message');
  749. elseif ( strpos( $redirect_to, 'about.php?updated' ) )
  750. $errors->add('updated', __( '<strong>You have successfully updated WordPress!</strong> Please log back in to see what&#8217;s new.' ), 'message' );
  751. }
  752. /**
  753. * Filters the login page errors.
  754. *
  755. * @since 3.6.0
  756. *
  757. * @param object $errors WP Error object.
  758. * @param string $redirect_to Redirect destination URL.
  759. */
  760. $errors = apply_filters( 'wp_login_errors', $errors, $redirect_to );
  761. // Clear any stale cookies.
  762. if ( $reauth )
  763. wp_clear_auth_cookie();
  764. login_header(__('Log In'), '', $errors);
  765. if ( isset($_POST['log']) )
  766. $user_login = ( 'incorrect_password' == $errors->get_error_code() || 'empty_password' == $errors->get_error_code() ) ? esc_attr(wp_unslash($_POST['log'])) : '';
  767. $rememberme = ! empty( $_POST['rememberme'] );
  768. if ( ! empty( $errors->errors ) ) {
  769. $aria_describedby_error = ' aria-describedby="login_error"';
  770. } else {
  771. $aria_describedby_error = '';
  772. }
  773. ?>
  774. <form name="loginform" id="loginform" action="<?php echo esc_url( site_url( 'wp-login.php', 'login_post' ) ); ?>" method="post">
  775. <p>
  776. <label for="user_login"><?php _e( 'Username or Email Address' ); ?><br />
  777. <input type="text" name="log" id="user_login"<?php echo $aria_describedby_error; ?> class="input" value="<?php echo esc_attr( $user_login ); ?>" size="20" /></label>
  778. </p>
  779. <p>
  780. <label for="user_pass"><?php _e( 'Password' ); ?><br />
  781. <input type="password" name="pwd" id="user_pass"<?php echo $aria_describedby_error; ?> class="input" value="" size="20" /></label>
  782. </p>
  783. <?php
  784. /**
  785. * Fires following the 'Password' field in the login form.
  786. *
  787. * @since 2.1.0
  788. */
  789. do_action( 'login_form' );
  790. ?>
  791. <p class="forgetmenot"><label for="rememberme"><input name="rememberme" type="checkbox" id="rememberme" value="forever" <?php checked( $rememberme ); ?> /> <?php esc_html_e( 'Remember Me' ); ?></label></p>
  792. <p class="submit">
  793. <input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="<?php esc_attr_e('Log In'); ?>" />
  794. <?php if ( $interim_login ) { ?>
  795. <input type="hidden" name="interim-login" value="1" />
  796. <?php } else { ?>
  797. <input type="hidden" name="redirect_to" value="<?php echo esc_attr($redirect_to); ?>" />
  798. <?php } ?>
  799. <?php if ( $customize_login ) : ?>
  800. <input type="hidden" name="customize-login" value="1" />
  801. <?php endif; ?>
  802. <input type="hidden" name="testcookie" value="1" />
  803. </p>
  804. </form>
  805. <?php if ( ! $interim_login ) { ?>
  806. <p id="nav">
  807. <?php if ( ! isset( $_GET['checkemail'] ) || ! in_array( $_GET['checkemail'], array( 'confirm', 'newpass' ) ) ) :
  808. if ( get_option( 'users_can_register' ) ) :
  809. $registration_url = sprintf( '<a href="%s">%s</a>', esc_url( wp_registration_url() ), __( 'Register' ) );
  810. /** This filter is documented in wp-includes/general-template.php */
  811. echo apply_filters( 'register', $registration_url ) . ' | ';
  812. endif;
  813. ?>
  814. <a href="<?php echo esc_url( wp_lostpassword_url() ); ?>"><?php _e( 'Lost your password?' ); ?></a>
  815. <?php endif; ?>
  816. </p>
  817. <?php } ?>
  818. <script type="text/javascript">
  819. function wp_attempt_focus(){
  820. setTimeout( function(){ try{
  821. <?php if ( $user_login ) { ?>
  822. d = document.getElementById('user_pass');
  823. d.value = '';
  824. <?php } else { ?>
  825. d = document.getElementById('user_login');
  826. <?php if ( 'invalid_username' == $errors->get_error_code() ) { ?>
  827. if( d.value != '' )
  828. d.value = '';
  829. <?php
  830. }
  831. }?>
  832. d.focus();
  833. d.select();
  834. } catch(e){}
  835. }, 200);
  836. }
  837. <?php if ( !$error ) { ?>
  838. wp_attempt_focus();
  839. <?php } ?>
  840. if(typeof wpOnload=='function')wpOnload();
  841. <?php if ( $interim_login ) { ?>
  842. (function(){
  843. try {
  844. var i, links = document.getElementsByTagName('a');
  845. for ( i in links ) {
  846. if ( links[i].href )
  847. links[i].target = '_blank';
  848. }
  849. } catch(e){}
  850. }());
  851. <?php } ?>
  852. </script>
  853. <?php
  854. login_footer();
  855. break;
  856. } // end action switch