Advertisement
130s

wp-login.php_modifiedToDisableLoginTextFields

Jun 17th, 2012
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.61 KB | None | 0 0
  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. /**
  11. * Modified 6/16/2012
  12. * With this file, text input fieds are disabled upon logging in.
  13. *
  14. * Also, registration is replaced by modified-logging in process. This can cause new users not being able to make their accounts and cannot log in.
  15. * One way to avoid this is modifying janrain's config at http://$WEBSITE_DOMAIN$/wp-admin/admin.php?page=JUMP
  16. * and check "Enable automatic user registration", which automatically create the user account once s/he logs in using social accounts.
  17. *
  18. * @author Isaac Saito
  19. */
  20.  
  21. /** Make sure that the WordPress bootstrap has run before continuing. */
  22. require( dirname(__FILE__) . '/wp-load.php' );
  23.  
  24. // Redirect to https login if forced to use SSL
  25. if ( force_ssl_admin() && !is_ssl() ) {
  26. if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
  27. wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']));
  28. exit();
  29. } else {
  30. wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  31. exit();
  32. }
  33. }
  34.  
  35. //
  36. // Main
  37. //
  38.  
  39. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'login';
  40. $errors = new WP_Error();
  41.  
  42. if ( isset($_GET['key']) )
  43. $action = 'resetpass';
  44.  
  45. // validate action so as to default to the login screen
  46. if ( !in_array( $action, array( 'postpass', 'logout', 'lostpassword', 'retrievepassword', 'resetpass', 'rp', 'register', 'login' ), true ) && false === has_filter( 'login_form_' . $action ) )
  47. $action = 'login';
  48.  
  49. nocache_headers();
  50.  
  51. header('Content-Type: '.get_bloginfo('html_type').'; charset='.get_bloginfo('charset'));
  52.  
  53. if ( defined('RELOCATE') ) { // Move flag is set
  54. if ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != $_SERVER['PHP_SELF']) )
  55. $_SERVER['PHP_SELF'] = str_replace( $_SERVER['PATH_INFO'], '', $_SERVER['PHP_SELF'] );
  56.  
  57. $schema = is_ssl() ? 'https://' : 'http://';
  58. if ( dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) != get_option('siteurl') )
  59. update_option('siteurl', dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) );
  60. }
  61.  
  62. //Set a cookie now to see if they are supported by the browser.
  63. setcookie(TEST_COOKIE, 'WP Cookie check', 0, COOKIEPATH, COOKIE_DOMAIN);
  64. if ( SITECOOKIEPATH != COOKIEPATH )
  65. setcookie(TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN);
  66.  
  67. // allow plugins to override the default actions, and to add extra actions if they want
  68. do_action( 'login_init' );
  69. do_action( 'login_form_' . $action );
  70.  
  71. $http_post = ('POST' == $_SERVER['REQUEST_METHOD']);
  72. switch ($action) {
  73. case 'postpass' :
  74. if ( empty( $wp_hasher ) ) {
  75. require_once( ABSPATH . 'wp-includes/class-phpass.php' );
  76. // By default, use the portable hash from phpass
  77. $wp_hasher = new PasswordHash(8, true);
  78. }
  79.  
  80. // 10 days
  81. setcookie( 'wp-postpass_' . COOKIEHASH, $wp_hasher->HashPassword( stripslashes( $_POST['post_password'] ) ), time() + 864000, COOKIEPATH );
  82.  
  83. wp_safe_redirect( wp_get_referer() );
  84. exit();
  85.  
  86. break;
  87.  
  88. case 'logout' :
  89. check_admin_referer('log-out');
  90. wp_logout();
  91.  
  92. $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : 'wp-login.php?loggedout=true';
  93. wp_safe_redirect( $redirect_to );
  94. exit();
  95.  
  96. break;
  97.  
  98. case 'lostpassword' :
  99. case 'retrievepassword' :
  100. case_retrievepassword();
  101. break;
  102.  
  103. case 'resetpass' :
  104. case 'rp' :
  105. case_rp();
  106. break;
  107.  
  108. case 'register' :
  109. //case_register($_POST); 6/16/2012/130s/Registration is now automatically taken care of when logging in with Social Account.
  110. // For this reason, registration is replaceable by default behavior (which shows login icons).
  111. case_default($_POST);
  112. break;
  113.  
  114. case 'login' :
  115. default:
  116. case_default($_POST);
  117. break;
  118. } // end action switch
  119.  
  120. function case_retrievepassword(){
  121. if ( $http_post ) {
  122. $errors = retrieve_password();
  123. if ( !is_wp_error($errors) ) {
  124. $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : 'wp-login.php?checkemail=confirm';
  125. wp_safe_redirect( $redirect_to );
  126. exit();
  127. }
  128. }
  129.  
  130. if ( isset($_GET['error']) && 'invalidkey' == $_GET['error'] ) $errors->add('invalidkey', __('Sorry, that key does not appear to be valid.'));
  131. $redirect_to = apply_filters( 'lostpassword_redirect', !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '' );
  132.  
  133. do_action('lost_password');
  134. 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);
  135.  
  136. $user_login = isset($_POST['user_login']) ? stripslashes($_POST['user_login']) : '';
  137. ?>
  138.  
  139. <form name="lostpasswordform" id="lostpasswordform"
  140. action="<?php echo esc_url( site_url( 'wp-login.php?action=lostpassword', 'login_post' ) ); ?>"
  141. method="post">
  142. <p>
  143. <label for="user_login"><?php _e('Username or E-mail:') ?><br /> <input
  144. type="text" name="user_login" id="user_login" class="input"
  145. value="<?php echo esc_attr($user_login); ?>" size="20" tabindex="10" />
  146. </label>
  147. </p>
  148. <?php do_action('lostpassword_form'); ?>
  149. <input type="hidden" name="redirect_to"
  150. value="<?php echo esc_attr( $redirect_to ); ?>" />
  151. <p class="submit">
  152. <input type="submit" name="wp-submit" id="wp-submit"
  153. class="button-primary"
  154. value="<?php esc_attr_e('Get New Password'); ?>" tabindex="100" />
  155. </p>
  156. </form>
  157.  
  158. <p id="nav">
  159. <a href="<?php echo esc_url( wp_login_url() ); ?>"><?php _e('Log in') ?>
  160. </a>
  161. <?php if ( get_option( 'users_can_register' ) ) : ?>
  162. <a
  163. href="<?php echo esc_url( site_url( 'wp-login.php?action=register', 'login' ) ); ?>"><?php _e( 'Register' ); ?>
  164. </a>
  165. <?php endif; ?>
  166. </p>
  167.  
  168. <?php
  169. login_footer('user_login');
  170. }
  171.  
  172.  
  173. function case_rp(){
  174. $user = check_password_reset_key($_GET['key'], $_GET['login']);
  175. if ( is_wp_error($user) ) {
  176. wp_redirect( site_url('wp-login.php?action=lostpassword&error=invalidkey') );
  177. exit;
  178. }
  179. $errors = '';
  180.  
  181. if ( isset($_POST['pass1']) && $_POST['pass1'] != $_POST['pass2'] ) {
  182. $errors = new WP_Error('password_reset_mismatch', __('The passwords do not match.'));
  183. } elseif ( isset($_POST['pass1']) && !empty($_POST['pass1']) ) {
  184. reset_password($user, $_POST['pass1']);
  185. 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>' );
  186. login_footer();
  187. exit;
  188. }
  189.  
  190. wp_enqueue_script('utils');
  191. wp_enqueue_script('user-profile');
  192.  
  193. login_header(__('Reset Password'), '<p class="message reset-pass">' . __('Enter your new password below.') . '</p>', $errors );
  194. ?>
  195. <form name="resetpassform" id="resetpassform"
  196. action="<?php echo esc_url( site_url( 'wp-login.php?action=resetpass&key=' . urlencode( $_GET['key'] ) . '&login=' . urlencode( $_GET['login'] ), 'login_post' ) ); ?>"
  197. method="post">
  198. <input type="hidden" id="user_login"
  199. value="<?php echo esc_attr( $_GET['login'] ); ?>" autocomplete="off" />
  200. <p>
  201. <label for="pass1"><?php _e('New password') ?><br /> <input
  202. type="password" name="pass1" id="pass1" class="input" size="20"
  203. value="" autocomplete="off" /> </label>
  204. </p>
  205. <p>
  206. <label for="pass2"><?php _e('Confirm new password') ?><br /> <input
  207. type="password" name="pass2" id="pass2" class="input" size="20"
  208. value="" autocomplete="off" /> </label>
  209. </p>
  210.  
  211. <div id="pass-strength-result" class="hide-if-no-js">
  212. <?php _e('Strength indicator'); ?>
  213. </div>
  214. <p class="description indicator-hint">
  215. <?php _e('Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ &amp; ).'); ?>
  216. </p>
  217.  
  218. <br class="clear" />
  219. <p class="submit">
  220. <input type="submit" name="wp-submit" id="wp-submit"
  221. class="button-primary" value="<?php esc_attr_e('Reset Password'); ?>"
  222. tabindex="100" />
  223. </p>
  224. </form>
  225.  
  226. <p id="nav">
  227. <a href="<?php echo esc_url( wp_login_url() ); ?>"><?php _e( 'Log in' ); ?>
  228. </a>
  229. <?php if ( get_option( 'users_can_register' ) ) : ?>
  230. <a
  231. href="<?php echo esc_url( site_url( 'wp-login.php?action=register', 'login' ) ); ?>"><?php _e( 'Register' ); ?>
  232. </a>
  233. <?php endif; ?>
  234. </p>
  235.  
  236. <?php login_footer('user_pass');
  237. }
  238.  
  239. function case_default($_POST){
  240. //print 'debug) 1';
  241. $secure_cookie = '';
  242. $interim_login = isset($_REQUEST['interim-login']);
  243. $customize_login = isset( $_REQUEST['customize-login'] );
  244.  
  245. // If the user wants ssl but the session is not ssl, force a secure cookie.
  246. if ( !empty($_POST['log']) && !force_ssl_admin() ) {
  247. $user_name = sanitize_user($_POST['log']);
  248. if ( $user = get_user_by('login', $user_name) ) {
  249. if ( get_user_option('use_ssl', $user->ID) ) {
  250. $secure_cookie = true;
  251. force_ssl_admin(true);
  252. }
  253. }
  254. }
  255.  
  256. if ( isset( $_REQUEST['redirect_to'] ) ) {
  257. $redirect_to = $_REQUEST['redirect_to'];
  258. // Redirect to https if user wants ssl
  259. if ( $secure_cookie && false !== strpos($redirect_to, 'wp-admin') )
  260. $redirect_to = preg_replace('|^http://|', 'https://', $redirect_to);
  261. } else {
  262. $redirect_to = admin_url();
  263. }
  264.  
  265. $reauth = empty($_REQUEST['reauth']) ? false : true;
  266.  
  267. // If the user was redirected to a secure login form from a non-secure admin page, and secure login is required but secure admin is not, then don't use a secure
  268. // cookie and redirect back to the referring non-secure admin page. This allows logins to always be POSTed over SSL while allowing the user to choose visiting
  269. // the admin via http or https.
  270. if ( !$secure_cookie && is_ssl() && force_ssl_login() && !force_ssl_admin() && ( 0 !== strpos($redirect_to, 'https') ) && ( 0 === strpos($redirect_to, 'http') ) )
  271. $secure_cookie = false;
  272.  
  273. $user = wp_signon('', $secure_cookie);
  274.  
  275. $redirect_to = apply_filters('login_redirect', $redirect_to, isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '', $user);
  276.  
  277. if ( !is_wp_error($user) && !$reauth ) {
  278. if ( $interim_login ) {
  279. $message = '<p class="message">' . __('You have logged in successfully.') . '</p>';
  280. login_header( '', $message );
  281.  
  282. if ( ! $customize_login ) : ?>
  283. <script type="text/javascript">setTimeout( function(){window.close()}, 8000);</script>
  284. <p class="alignright">
  285. <input type="button" class="button-primary"
  286. value="<?php esc_attr_e('Close'); ?>" onclick="window.close()" />
  287. </p>
  288. <?php endif; ?>
  289. </div>
  290. <?php
  291. do_action('login_footer');
  292. if ( $customize_login ) : ?>
  293. <script type="text/javascript">setTimeout( function(){ new wp.customize.Messenger({ url: '<?php echo wp_customize_url(); ?>', channel: 'login' }).send('login') }, 1000 );</script>
  294. <?php endif; ?>
  295. </body>
  296. </html>
  297. <?php exit;
  298. }
  299.  
  300. if ( ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' || $redirect_to == admin_url() ) ) {
  301. // 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.
  302. if ( is_multisite() && !get_active_blog_for_user($user->ID) && !is_super_admin( $user->ID ) )
  303. $redirect_to = user_admin_url();
  304. elseif ( is_multisite() && !$user->has_cap('read') )
  305. $redirect_to = get_dashboard_url( $user->ID );
  306. elseif ( !$user->has_cap('edit_posts') )
  307. $redirect_to = admin_url('profile.php');
  308. }
  309. wp_safe_redirect($redirect_to);
  310. exit();
  311. }
  312.  
  313. $errors = $user;
  314. // Clear errors if loggedout is set.
  315. if ( !empty($_GET['loggedout']) || $reauth )
  316. $errors = new WP_Error();
  317.  
  318. // If cookies are disabled we can't log in even with a valid user+pass
  319. if ( isset($_POST['testcookie']) && empty($_COOKIE[TEST_COOKIE]) )
  320. $errors->add('test_cookie', __("<strong>ERROR</strong>: Cookies are blocked or not supported by your browser. You must <a href='http://www.google.com/cookies.html'>enable cookies</a> to use WordPress."));
  321.  
  322. // Some parts of this script use the main login form to display a message
  323. if ( isset($_GET['loggedout']) && true == $_GET['loggedout'] )
  324. $errors->add('loggedout', __('You are now logged out.'), 'message');
  325. elseif ( isset($_GET['registration']) && 'disabled' == $_GET['registration'] )
  326. $errors->add('registerdisabled', __('User registration is currently not allowed.'));
  327. elseif ( isset($_GET['checkemail']) && 'confirm' == $_GET['checkemail'] )
  328. $errors->add('confirm', __('Check your e-mail for the confirmation link.'), 'message');
  329. elseif ( isset($_GET['checkemail']) && 'newpass' == $_GET['checkemail'] )
  330. $errors->add('newpass', __('Check your e-mail for your new password.'), 'message');
  331. elseif ( isset($_GET['checkemail']) && 'registered' == $_GET['checkemail'] )
  332. $errors->add('registered', __('Registration complete. Please check your e-mail.'), 'message');
  333. elseif ( $interim_login )
  334. $errors->add('expired', __('Your session has expired. Please log-in again.'), 'message');
  335. elseif ( strpos( $redirect_to, 'about.php?updated' ) )
  336. $errors->add('updated', __( '<strong>You have successfully updated WordPress!</strong> Please log back in to experience the awesomeness.' ), 'message' );
  337.  
  338. // Clear any stale cookies.
  339. if ( $reauth )
  340. wp_clear_auth_cookie();
  341.  
  342. login_header(__('Log In'), '', $errors);
  343.  
  344. if ( isset($_POST['log']) )
  345. $user_login = ( 'incorrect_password' == $errors->get_error_code() || 'empty_password' == $errors->get_error_code() ) ? esc_attr(stripslashes($_POST['log'])) : '';
  346. $rememberme = ! empty( $_POST['rememberme'] );
  347. ?>
  348.  
  349. <form name="loginform" id="loginform"
  350. action="<?php echo esc_url( site_url( 'wp-login.php', 'login_post' ) ); ?>"
  351. method="post">
  352. <!--
  353. <p>
  354. <label for="user_login"><?php _e('Username') ?><br /> <input
  355. type="text" name="log" id="user_login" class="input"
  356. value="<?php echo esc_attr($user_login); ?>" size="20" tabindex="10" />
  357. </label>
  358. </p>
  359. <p>
  360. <label for="user_pass"><?php _e('Password') ?><br /> <input
  361. type="password" name="pwd" id="user_pass" class="input" value=""
  362. size="20" tabindex="20" /> </label>
  363. </p>
  364. -->
  365. <?php do_action('login_form'); ?>
  366. <p class="forgetmenot">
  367. <label for="rememberme"><input name="rememberme" type="checkbox"
  368. id="rememberme" value="forever" tabindex="90"
  369. <?php checked( $rememberme ); ?> /> <?php esc_attr_e('Remember Me'); ?>
  370. </label>
  371. </p>
  372. <p class="submit">
  373. <input type="submit" name="wp-submit" id="wp-submit"
  374. class="button-primary" value="<?php esc_attr_e('Log In'); ?>"
  375. tabindex="100" />
  376. <?php if ( $interim_login ) { ?>
  377. <input type="hidden" name="interim-login" value="1" />
  378. <?php } else { ?>
  379. <input type="hidden" name="redirect_to"
  380. value="<?php echo esc_attr($redirect_to); ?>" />
  381. <?php } ?>
  382. <?php if ( $customize_login ) : ?>
  383. <input type="hidden" name="customize-login" value="1" />
  384. <?php endif; ?>
  385. <input type="hidden" name="testcookie" value="1" />
  386. </p>
  387. </form>
  388.  
  389. <?php if ( !$interim_login ) { ?>
  390. <p id="nav">
  391. <?php if ( isset($_GET['checkemail']) && in_array( $_GET['checkemail'], array('confirm', 'newpass') ) ) : ?>
  392. <?php elseif ( get_option('users_can_register') ) : ?>
  393. <a
  394. href="<?php echo esc_url( site_url( 'wp-login.php?action=register', 'login' ) ); ?>"><?php _e( 'Register' ); ?>
  395. </a> | <a href="<?php echo esc_url( wp_lostpassword_url() ); ?>"
  396. title="<?php esc_attr_e( 'Password Lost and Found' ); ?>"> <?php _e( 'Lost your password?' ); ?>
  397. </a>
  398. <?php else : ?>
  399. <a href="<?php echo esc_url( wp_lostpassword_url() ); ?>"
  400. title="<?php esc_attr_e( 'Password Lost and Found' ); ?>"><?php _e( 'Lost your password?' ); ?>
  401. </a>
  402. <?php endif; ?>
  403. </p>
  404. <?php } ?>
  405.  
  406. <script type="text/javascript">
  407. function wp_attempt_focus(){
  408. setTimeout( function(){ try{
  409. <?php if ( $user_login || $interim_login ) { ?>
  410. d = document.getElementById('user_pass');
  411. d.value = '';
  412. <?php } else { ?>
  413. d = document.getElementById('user_login');
  414. <?php if ( 'invalid_username' == $errors->get_error_code() ) { ?>
  415. if( d.value != '' )
  416. d.value = '';
  417. <?php
  418. }
  419. }?>
  420. d.focus();
  421. d.select();
  422. } catch(e){}
  423. }, 200);
  424. }
  425.  
  426. <?php if ( !$error ) { ?>
  427. wp_attempt_focus();
  428. <?php } ?>
  429. if(typeof wpOnload=='function')wpOnload();
  430. </script>
  431.  
  432. <?php
  433. login_footer();
  434. }
  435.  
  436. function case_register($_POST){
  437. if ( is_multisite() ) {
  438. // Multisite uses wp-signup.php
  439. wp_redirect( apply_filters( 'wp_signup_location', site_url('wp-signup.php') ) );
  440. exit;
  441. }
  442.  
  443. if ( !get_option('users_can_register') ) {
  444. wp_redirect( site_url('wp-login.php?registration=disabled') );
  445. exit();
  446. }
  447.  
  448. $user_login = '';
  449. $user_email = '';
  450. if ( $http_post ) {
  451. $user_login = $_POST['user_login'];
  452. $user_email = $_POST['user_email'];
  453. $errors = register_new_user($user_login, $user_email);
  454. if ( !is_wp_error($errors) ) {
  455. $redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?checkemail=registered';
  456. wp_safe_redirect( $redirect_to );
  457. exit();
  458. }
  459. }
  460.  
  461. $redirect_to = apply_filters( 'registration_redirect', !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '' );
  462. login_header(__('Registration Form'), '<p class="message register">' . __('Register For This Site') . '</p>', $errors);
  463. ?>
  464.  
  465. <!--
  466. <form name="registerform" id="registerform"
  467. action="<?php echo esc_url( site_url('wp-login.php?action=register', 'login_post') ); ?>"
  468. method="post">
  469. <p>
  470. <label for="user_login"><?php _e('Username') ?><br /> <input
  471. type="text" name="user_login" id="user_login" class="input"
  472. value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" tabindex="10" /> </label>
  473. </p>
  474. <p>
  475. <label for="user_email"><?php _e('E-mail') ?><br /> <input
  476. type="email" name="user_email" id="user_email" class="input"
  477. value="<?php echo esc_attr(stripslashes($user_email)); ?>" size="25" tabindex="20" /> </label>
  478. </p>
  479. <?php do_action('register_form'); ?>
  480. <p id="reg_passmail">
  481. <?php _e('A password will be e-mailed to you.') ?>
  482. </p>
  483. <br class="clear" /> <input type="hidden" name="redirect_to"
  484. value="<?php echo esc_attr( $redirect_to ); ?>" />
  485. <p class="submit">
  486. <input type="submit" name="wp-submit" id="wp-submit"
  487. class="button-primary" value="<?php esc_attr_e('Register'); ?>" tabindex="100" />
  488. </p>
  489. </form>
  490. -->
  491.  
  492. <p id="nav">
  493. <a href="<?php echo esc_url( wp_login_url() ); ?>"><?php _e( 'Log in' ); ?>
  494. </a> | <a href="<?php echo esc_url( wp_lostpassword_url() ); ?>"
  495. title="<?php esc_attr_e( 'Password Lost and Found' ) ?>"><?php _e( 'Lost your password?' ); ?>
  496. </a>
  497. </p>
  498.  
  499. <?php
  500. login_footer('user_login');
  501. }
  502.  
  503. /**
  504. * Outputs the header for the login page.
  505. *
  506. * @uses do_action() Calls the 'login_head' for outputting HTML in the Log In
  507. * header.
  508. * @uses apply_filters() Calls 'login_headerurl' for the top login link.
  509. * @uses apply_filters() Calls 'login_headertitle' for the top login title.
  510. * @uses apply_filters() Calls 'login_message' on the message to display in the
  511. * header.
  512. * @uses $error The error global, which is checked for displaying errors.
  513. *
  514. * @param string $title Optional. WordPress Log In Page title to display in
  515. * <title/> element.
  516. * @param string $message Optional. Message to display in header.
  517. * @param WP_Error $wp_error Optional. WordPress Error Object
  518. */
  519. function login_header($title = 'Log In', $message = '', $wp_error = '') {
  520. global $error, $interim_login, $current_site, $customize_login;
  521.  
  522. // Don't index any of these forms
  523. add_action( 'login_head', 'wp_no_robots' );
  524.  
  525. if ( empty($wp_error) )
  526. $wp_error = new WP_Error();
  527.  
  528. // Shake it!
  529. $shake_error_codes = array( 'empty_password', 'empty_email', 'invalid_email', 'invalidcombo', 'empty_username', 'invalid_username', 'incorrect_password' );
  530. $shake_error_codes = apply_filters( 'shake_error_codes', $shake_error_codes );
  531.  
  532. if ( $shake_error_codes && $wp_error->get_error_code() && in_array( $wp_error->get_error_code(), $shake_error_codes ) )
  533. add_action( 'login_head', 'wp_shake_js', 12 );
  534.  
  535. ?>
  536. <!DOCTYPE html>
  537. <html xmlns="http://www.w3.org/1999/xhtml"
  538. <?php language_attributes(); ?>>
  539. <head>
  540. <meta http-equiv="Content-Type"
  541. content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
  542. <title><?php bloginfo('name'); ?> &rsaquo; <?php echo $title; ?></title>
  543. <?php
  544.  
  545. wp_admin_css( 'wp-admin', true );
  546. wp_admin_css( 'colors-fresh', true );
  547.  
  548. if ( wp_is_mobile() ) { ?>
  549. <meta name="viewport"
  550. content="width=320; initial-scale=0.9; maximum-scale=1.0; user-scalable=0;" />
  551. <?php
  552. }
  553.  
  554. if ( $customize_login )
  555. wp_enqueue_script( 'customize-base' );
  556.  
  557. do_action( 'login_enqueue_scripts' );
  558. do_action( 'login_head' );
  559.  
  560. if ( is_multisite() ) {
  561. $login_header_url = network_home_url();
  562. $login_header_title = $current_site->site_name;
  563. } else {
  564. $login_header_url = __( 'http://wordpress.org/' );
  565. $login_header_title = __( 'Powered by WordPress' );
  566. }
  567.  
  568. $login_header_url = apply_filters( 'login_headerurl', $login_header_url );
  569. $login_header_title = apply_filters( 'login_headertitle', $login_header_title );
  570.  
  571. // Don't allow interim logins to navigate away from the page.
  572. if ( $interim_login )
  573. $login_header_url = '#';
  574.  
  575. ?>
  576. </head>
  577. <body class="login<?php if ( wp_is_mobile() ) echo ' mobile'; ?>">
  578. <div id="login">
  579. <h1>
  580. <a href="<?php echo esc_url( $login_header_url ); ?>"
  581. title="<?php echo esc_attr( $login_header_title ); ?>"><?php bloginfo( 'name' ); ?>
  582. </a>
  583. </h1>
  584. <?php
  585.  
  586. unset( $login_header_url, $login_header_title );
  587.  
  588. $message = apply_filters('login_message', $message);
  589. if ( !empty( $message ) )
  590. echo $message . "\n";
  591.  
  592. // In case a plugin uses $error rather than the $wp_errors object
  593. if ( !empty( $error ) ) {
  594. $wp_error->add('error', $error);
  595. unset($error);
  596. }
  597.  
  598. if ( $wp_error->get_error_code() ) {
  599. $errors = '';
  600. $messages = '';
  601. foreach ( $wp_error->get_error_codes() as $code ) {
  602. $severity = $wp_error->get_error_data($code);
  603. foreach ( $wp_error->get_error_messages($code) as $error ) {
  604. if ( 'message' == $severity )
  605. $messages .= ' ' . $error . "<br />\n";
  606. else
  607. $errors .= ' ' . $error . "<br />\n";
  608. }
  609. }
  610. if ( !empty($errors) )
  611. echo '<div id="login_error">' . apply_filters('login_errors', $errors) . "</div>\n";
  612. if ( !empty($messages) )
  613. echo '<p class="message">' . apply_filters('login_messages', $messages) . "</p>\n";
  614. }
  615. } // End of login_header()
  616.  
  617. /**
  618. * Outputs the footer for the login page.
  619. *
  620. * @param string $input_id Which input to auto-focus
  621. */
  622. function login_footer($input_id = '') {
  623. global $interim_login;
  624.  
  625. // Don't allow interim logins to navigate away from the page.
  626. if ( ! $interim_login ): ?>
  627. <p id="backtoblog">
  628. <a href="<?php echo esc_url( home_url( '/' ) ); ?>"
  629. title="<?php esc_attr_e( 'Are you lost?' ); ?>"><?php printf( __( '&larr; Back to %s' ), get_bloginfo( 'title', 'display' ) ); ?>
  630. </a>
  631. </p>
  632. <?php endif; ?>
  633.  
  634. </div>
  635.  
  636. <?php if ( !empty($input_id) ) : ?>
  637. <script type="text/javascript">
  638. try{document.getElementById('<?php echo $input_id; ?>').focus();}catch(e){}
  639. if(typeof wpOnload=='function')wpOnload();
  640. </script>
  641. <?php endif; ?>
  642.  
  643. <?php do_action('login_footer'); ?>
  644. <div class="clear"></div>
  645. </body>
  646. </html>
  647. <?php
  648. }
  649.  
  650. function wp_shake_js() {
  651. if ( wp_is_mobile() )
  652. return;
  653. ?>
  654. <script type="text/javascript">
  655. 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();}}};
  656. function s(id,pos){g(id).left=pos+'px';}
  657. function g(id){return document.getElementById(id).style;}
  658. 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){}}}
  659. 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);});
  660. </script>
  661. <?php
  662. }
  663.  
  664. /** ****** End of main ****** **/
  665.  
  666.  
  667. /**
  668. * Handles sending password retrieval email to user.
  669. *
  670. * @uses $wpdb WordPress Database object
  671. *
  672. * @return bool|WP_Error True: when finish. WP_Error on error
  673. */
  674. function retrieve_password() {
  675. global $wpdb, $current_site;
  676.  
  677. $errors = new WP_Error();
  678.  
  679. if ( empty( $_POST['user_login'] ) ) {
  680. $errors->add('empty_username', __('<strong>ERROR</strong>: Enter a username or e-mail address.'));
  681. } else if ( strpos( $_POST['user_login'], '@' ) ) {
  682. $user_data = get_user_by( 'email', trim( $_POST['user_login'] ) );
  683. if ( empty( $user_data ) )
  684. $errors->add('invalid_email', __('<strong>ERROR</strong>: There is no user registered with that email address.'));
  685. } else {
  686. $login = trim($_POST['user_login']);
  687. $user_data = get_user_by('login', $login);
  688. }
  689.  
  690. do_action('lostpassword_post');
  691.  
  692. if ( $errors->get_error_code() )
  693. return $errors;
  694.  
  695. if ( !$user_data ) {
  696. $errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or e-mail.'));
  697. return $errors;
  698. }
  699.  
  700. // redefining user_login ensures we return the right case in the email
  701. $user_login = $user_data->user_login;
  702. $user_email = $user_data->user_email;
  703.  
  704. do_action('retreive_password', $user_login); // Misspelled and deprecated
  705. do_action('retrieve_password', $user_login);
  706.  
  707. $allow = apply_filters('allow_password_reset', true, $user_data->ID);
  708.  
  709. if ( ! $allow )
  710. return new WP_Error('no_password_reset', __('Password reset is not allowed for this user'));
  711. else if ( is_wp_error($allow) )
  712. return $allow;
  713.  
  714. $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));
  715. if ( empty($key) ) {
  716. // Generate something random for a key...
  717. $key = wp_generate_password(20, false);
  718. do_action('retrieve_password_key', $user_login, $key);
  719. // Now insert the new md5 key into the db
  720. $wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login));
  721. }
  722. $message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";
  723. $message .= network_home_url( '/' ) . "\r\n\r\n";
  724. $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
  725. $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
  726. $message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
  727. $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";
  728.  
  729. if ( is_multisite() )
  730. $blogname = $GLOBALS['current_site']->site_name;
  731. else
  732. // The blogname option is escaped with esc_html on the way into the database in sanitize_option
  733. // we want to reverse this for the plain text arena of emails.
  734. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  735.  
  736. $title = sprintf( __('[%s] Password Reset'), $blogname );
  737.  
  738. $title = apply_filters('retrieve_password_title', $title);
  739. $message = apply_filters('retrieve_password_message', $message, $key);
  740.  
  741. if ( $message && !wp_mail($user_email, $title, $message) )
  742. wp_die( __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') );
  743.  
  744. return true;
  745. }
  746.  
  747. /**
  748. * Retrieves a user row based on password reset key and login
  749. *
  750. * @uses $wpdb WordPress Database object
  751. *
  752. * @param string $key Hash to validate sending user's password
  753. * @param string $login The user login
  754. * @return object|WP_Error User's database row on success, error object for invalid keys
  755. */
  756. function check_password_reset_key($key, $login) {
  757. global $wpdb;
  758.  
  759. $key = preg_replace('/[^a-z0-9]/i', '', $key);
  760.  
  761. if ( empty( $key ) || !is_string( $key ) )
  762. return new WP_Error('invalid_key', __('Invalid key'));
  763.  
  764. if ( empty($login) || !is_string($login) )
  765. return new WP_Error('invalid_key', __('Invalid key'));
  766.  
  767. $user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_activation_key = %s AND user_login = %s", $key, $login));
  768.  
  769. if ( empty( $user ) )
  770. return new WP_Error('invalid_key', __('Invalid key'));
  771.  
  772. return $user;
  773. }
  774.  
  775. /**
  776. * Handles resetting the user's password.
  777. *
  778. * @param object $user The user
  779. * @param string $new_pass New password for the user in plaintext
  780. */
  781. function reset_password($user, $new_pass) {
  782. do_action('password_reset', $user, $new_pass);
  783.  
  784. wp_set_password($new_pass, $user->ID);
  785.  
  786. wp_password_change_notification($user);
  787. }
  788.  
  789. /**
  790. * Handles registering a new user.
  791. *
  792. * @param string $user_login User's username for logging in
  793. * @param string $user_email User's email address to send password and add
  794. * @return int|WP_Error Either user's ID or error on failure.
  795. */
  796. function register_new_user( $user_login, $user_email ) {
  797. $errors = new WP_Error();
  798.  
  799. $sanitized_user_login = sanitize_user( $user_login );
  800. $user_email = apply_filters( 'user_registration_email', $user_email );
  801.  
  802. // Check the username
  803. if ( $sanitized_user_login == '' ) {
  804. $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
  805. } elseif ( ! validate_username( $user_login ) ) {
  806. $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
  807. $sanitized_user_login = '';
  808. } elseif ( username_exists( $sanitized_user_login ) ) {
  809. $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ) );
  810. }
  811.  
  812. // Check the e-mail address
  813. if ( $user_email == '' ) {
  814. $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );
  815. } elseif ( ! is_email( $user_email ) ) {
  816. $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ) );
  817. $user_email = '';
  818. } elseif ( email_exists( $user_email ) ) {
  819. $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
  820. }
  821.  
  822. do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
  823.  
  824. $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
  825.  
  826. if ( $errors->get_error_code() )
  827. return $errors;
  828.  
  829. $user_pass = wp_generate_password( 12, false);
  830. $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
  831. if ( ! $user_id ) {
  832. $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
  833. return $errors;
  834. }
  835.  
  836. update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
  837.  
  838. wp_new_user_notification( $user_id, $user_pass );
  839.  
  840. return $user_id;
  841. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement