PageRenderTime 61ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/vantage/framework/includes/views-login.php

https://bitbucket.org/sanders_nick/front-lawn-pawn
PHP | 585 lines | 425 code | 141 blank | 19 comment | 145 complexity | a0d5f908c7496e99ea5b5c7eb20d47e3 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. class APP_Login extends APP_View_Page {
  3. private $error;
  4. function __construct( $template ) {
  5. parent::__construct( $template, __('Login', APP_TD) );
  6. add_filter( 'login_url', array( $this, '_change_login_url' ), 10, 2 );
  7. }
  8. function _change_login_url( $url, $redirect_to ) {
  9. return self::get_url( 'redirect', $redirect_to );
  10. }
  11. function init() {
  12. global $pagenow;
  13. if($pagenow == 'wp-login.php' && ( empty($_GET['action']) || !in_array($_GET['action'], array('logout', 'lostpassword', 'retrievepassword', 'rp', 'resetpass', 'register') ) ) && self::get_id() ) {
  14. $continue = true;
  15. $continue = apply_filters('app_login_pre_redirect', $continue);
  16. if( false !== $continue ) {
  17. wp_redirect(APP_Login::get_url('redirect'));
  18. exit();
  19. }
  20. }
  21. }
  22. static function get_id() {
  23. return parent::_get_id( __CLASS__ );
  24. }
  25. static function get_url( $context = 'display', $redirect_to = '' ) {
  26. $args = wp_array_slice_assoc( $_GET, array( 'checkemail', 'registration', 'loggedout' ) );
  27. if ( !empty( $redirect_to ) )
  28. $args['redirect_to'] = urlencode( $redirect_to );
  29. if ( $page_id = self::get_id() ) {
  30. $permalink = get_permalink( $page_id );
  31. } else {
  32. $permalink = site_url( 'wp-login.php' );
  33. }
  34. return esc_url( add_query_arg( $args, $permalink ), null, $context );
  35. }
  36. function template_redirect() {
  37. $this->process_form();
  38. }
  39. function process_form() {
  40. if ( is_user_logged_in() ) {
  41. do_action('app_login');
  42. }
  43. if ( empty( $_POST['login'] ) ) return;
  44. if ( isset( $_REQUEST['redirect_to'] ) )
  45. $redirect_to = $_REQUEST['redirect_to'];
  46. else
  47. $redirect_to = admin_url('index.php');
  48. if ( is_ssl() && force_ssl_login() && !force_ssl_admin() && ( 0 !== strpos($redirect_to, 'https') ) && ( 0 === strpos($redirect_to, 'http') ) )
  49. $secure_cookie = false;
  50. else
  51. $secure_cookie = '';
  52. $user = wp_signon('', $secure_cookie);
  53. $redirect_to = apply_filters('login_redirect', $redirect_to, isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '', $user);
  54. if ( !is_wp_error($user) ) {
  55. wp_safe_redirect($redirect_to);
  56. exit;
  57. }
  58. $this->error = $user;
  59. }
  60. function notices() {
  61. $message = '';
  62. $message_type = '';
  63. // Clear errors if loggedout is set.
  64. if ( !empty($_GET['loggedout']) ) $errors = new WP_Error();
  65. if ( isset($_POST['testcookie']) && empty($_COOKIE[TEST_COOKIE]) ) {
  66. $errors->add('test_cookie', __('Cookies are blocked or not supported by your browser. You must enable cookies to continue.', APP_TD));
  67. }
  68. if ( isset($_GET['loggedout']) && TRUE == $_GET['loggedout'] ) {
  69. $message_type = 'success';
  70. $message = __('You are now logged out.', APP_TD);
  71. } elseif ( isset($_GET['registration']) && 'disabled' == $_GET['registration'] ) {
  72. $message_type = 'error';
  73. $errors->add('registerdisabled', __('User registration is currently not allowed.', APP_TD));
  74. } elseif ( isset($_GET['checkemail']) && 'confirm' == $_GET['checkemail'] ) {
  75. $message_type = 'success';
  76. $message = __('Check your email for the confirmation link.', APP_TD);
  77. } elseif ( isset($_GET['checkemail']) && 'newpass' == $_GET['checkemail'] ) {
  78. $message_type = 'success';
  79. $message = __('Check your email for your new password.', APP_TD);
  80. } elseif ( isset($_GET['checkemail']) && 'registered' == $_GET['checkemail'] ) {
  81. $message_type = 'success';
  82. $message = __('Registration complete. Please check your e-mail.', APP_TD);
  83. } elseif ( isset($_GET['action']) && 'lostpassword' == $_GET['action'] && !empty($_GET['success'])) {
  84. $message_type = 'success';
  85. $message = __('Your password has been reset. Please login.', APP_TD);
  86. }
  87. if (isset($errors) && sizeof($errors)>0 && $errors->get_error_code()) {
  88. $error_html ='<ul class="errors">';
  89. foreach ($errors->errors as $error) {
  90. $error_html .='<li>'.$error[0].'</li>';
  91. }
  92. $error_html .='</ul>';
  93. appthemes_display_notice( 'error', $error_html );
  94. }
  95. if (isset($this->error) && sizeof($this->error)>0 && $this->error->get_error_code()) {
  96. $error_html ='<ul class="errors">';
  97. foreach ($this->error->errors as $error) {
  98. $error_html .='<li>'.$error[0].'</li>';
  99. }
  100. $error_html .='</ul>';
  101. appthemes_display_notice( 'error', $error_html );
  102. }
  103. if ( !empty( $message) ) {
  104. appthemes_display_notice( $message_type, $message );
  105. }
  106. }
  107. }
  108. class APP_Password_Recovery extends APP_View_Page {
  109. private $error;
  110. function __construct( $template ) {
  111. parent::__construct( $template, __('Password Recovery', APP_TD) );
  112. }
  113. static function get_id() {
  114. return parent::_get_id( __CLASS__ );
  115. }
  116. function init() {
  117. global $pagenow;
  118. if($pagenow == 'wp-login.php' && !empty($_GET['action']) && in_array( $_GET['action'], array('lostpassword', 'retrievepassword') ) && self::get_id() ) {
  119. wp_redirect(appthemes_get_password_recovery_url('redirect'));
  120. exit();
  121. }
  122. }
  123. function template_redirect() {
  124. $this->process_form();
  125. }
  126. function process_form() {
  127. $errors = new WP_Error();
  128. if ( !empty($_POST['user_login']) ) {
  129. $errors = $this->retrieve_password();
  130. if ( !is_wp_error($errors) ) {
  131. $url = APP_Login::get_url('redirect');
  132. $pre = get_option('permalink_structure') == '' ? '&' : '?';
  133. $url = $url . $pre . 'checkemail=confirm';
  134. wp_redirect( $url );
  135. exit();
  136. }
  137. $this->error = $errors;
  138. }
  139. do_action('lost_password');
  140. }
  141. function notices() {
  142. if (isset($_GET['invalidkeyerror']) && '1' == $_GET['invalidkeyerror'] ) {
  143. appthemes_display_notice( 'error', __('Sorry, that key does not appear to be valid. Please try again.', APP_TD) );
  144. }
  145. if (isset($this->error) && sizeof($this->error)>0 && $this->error->get_error_code()) {
  146. $error_html ='<ul class="errors">';
  147. foreach ($this->error->errors as $error) {
  148. $error_html .='<li>'.$error[0].'</li>';
  149. }
  150. $error_html .='</ul>';
  151. appthemes_display_notice( 'error', $error_html );
  152. }
  153. }
  154. function retrieve_password() {
  155. global $wpdb, $current_site;
  156. $errors = new WP_Error();
  157. if ( empty( $_POST['user_login'] ) ) {
  158. $errors->add('empty_username', __('<strong>ERROR</strong>: Enter a username or e-mail address.', APP_TD));
  159. } else if ( strpos( $_POST['user_login'], '@' ) ) {
  160. $user_data = get_user_by( 'email', trim( $_POST['user_login'] ) );
  161. if ( empty( $user_data ) )
  162. $errors->add('invalid_email', __('<strong>ERROR</strong>: There is no user registered with that email address.', APP_TD));
  163. } else {
  164. $login = trim($_POST['user_login']);
  165. $user_data = get_user_by('login', $login);
  166. }
  167. do_action('lostpassword_post');
  168. if ( $errors->get_error_code() )
  169. return $errors;
  170. if ( !$user_data ) {
  171. $errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or e-mail.', APP_TD));
  172. return $errors;
  173. }
  174. // redefining user_login ensures we return the right case in the email
  175. $user_login = $user_data->user_login;
  176. $user_email = $user_data->user_email;
  177. do_action('retreive_password', $user_login); // Misspelled and deprecated
  178. do_action('retrieve_password', $user_login);
  179. $allow = apply_filters('allow_password_reset', true, $user_data->ID);
  180. if ( ! $allow )
  181. return new WP_Error('no_password_reset', __('Password reset is not allowed for this user', APP_TD));
  182. else if ( is_wp_error($allow) )
  183. return $allow;
  184. $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));
  185. if ( empty($key) ) {
  186. // Generate something random for a key...
  187. $key = wp_generate_password(20, false);
  188. do_action('retrieve_password_key', $user_login, $key);
  189. // Now insert the new md5 key into the db
  190. $wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login));
  191. }
  192. $message = __('Someone requested that the password be reset for the following account:', APP_TD) . "\r\n\r\n";
  193. $message .= site_url() . "\r\n\r\n";
  194. $message .= sprintf(__('Username: %s', APP_TD), $user_login) . "\r\n\r\n";
  195. $message .= __('If this was a mistake, just ignore this email and nothing will happen.', APP_TD) . "\r\n\r\n";
  196. $message .= __('To reset your password, visit the following address:', APP_TD) . "\r\n\r\n";
  197. $url = appthemes_get_password_reset_url();
  198. $pre = get_option('permalink_structure') == '' ? '&' : '?';
  199. $url = $url . $pre . "action=rp&key=".$key."&login=" . rawurlencode($user_login);
  200. $message .= '<' . $url . ">\r\n";
  201. if ( is_multisite() )
  202. $blogname = $GLOBALS['current_site']->site_name;
  203. else
  204. // The blogname option is escaped with esc_html on the way into the database in sanitize_option
  205. // we want to reverse this for the plain text arena of emails.
  206. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  207. $title = sprintf( __('[%s] Password Reset', APP_TD), $blogname );
  208. $title = apply_filters('retrieve_password_title', $title);
  209. $message = apply_filters('retrieve_password_message', $message, $key);
  210. if ( $message && !wp_mail($user_email, $title, $message) )
  211. wp_die( __('The e-mail could not be sent.', APP_TD) . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...', APP_TD) );
  212. return true;
  213. }
  214. }
  215. class APP_Password_Reset extends APP_View_Page {
  216. private $error;
  217. function __construct( $template ) {
  218. parent::__construct( $template, __('Password Reset', APP_TD) );
  219. }
  220. static function get_id() {
  221. return parent::_get_id( __CLASS__ );
  222. }
  223. function init() {
  224. global $pagenow;
  225. if($pagenow == 'wp-login.php' && !empty( $_GET['action'] ) && 'rp' == $_GET['action'] && !empty( $_GET['key'] ) && !empty( $_GET['login'] ) && self::get_id() ) {
  226. wp_redirect(appthemes_get_password_reset_url('redirect'));
  227. exit();
  228. }
  229. }
  230. function template_redirect() {
  231. if ( !$this->condition() ) return;
  232. if(!empty( $_GET['action'] ) && 'rp' == $_GET['action'] && !empty( $_GET['key'] ) && !empty( $_GET['login'] )) {
  233. $this->process_form();
  234. }
  235. wp_enqueue_script('utils');
  236. wp_enqueue_script('user-profile');
  237. }
  238. function process_form() {
  239. $user = $this->check_password_reset_key($_GET['key'], $_GET['login']);
  240. if ( is_wp_error($user) ) {
  241. $url = appthemes_get_password_recovery_url('redirect');
  242. $pre = get_option('permalink_structure') == '' ? '&' : '?';
  243. $url = $url . $pre . 'action=lostpassword&invalidkeyerror=1';
  244. wp_redirect( $url );
  245. exit;
  246. } else {
  247. $this->error = $user;
  248. }
  249. if ( isset($_POST['pass1']) && $_POST['pass1'] != $_POST['pass2'] ) {
  250. $this->error = new WP_Error( 'password_reset_mismatch', __('The passwords do not match.', APP_TD) );
  251. } elseif ( isset($_POST['pass1']) && !empty($_POST['pass1']) ) {
  252. $this->reset_password($user, $_POST['pass1']);
  253. $url = APP_Login::get_url('redirect');
  254. $pre = get_option('permalink_structure') == '' ? '&' : '?';
  255. $url = $url . $pre . 'action=lostpassword&success=1';
  256. wp_redirect( $url );
  257. exit;
  258. }
  259. }
  260. function check_password_reset_key($key, $login) {
  261. global $wpdb;
  262. $key = preg_replace('/[^a-z0-9]/i', '', $key);
  263. if ( empty( $key ) || !is_string( $key ) )
  264. return new WP_Error('invalid_key', __('Invalid key', APP_TD));
  265. if ( empty($login) || !is_string($login) )
  266. return new WP_Error('invalid_key', __('Invalid key', APP_TD));
  267. $user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_activation_key = %s AND user_login = %s", $key, $login));
  268. if ( empty( $user ) )
  269. return new WP_Error('invalid_key', __('Invalid key', APP_TD));
  270. return $user;
  271. }
  272. function reset_password($user, $new_pass) {
  273. do_action('password_reset', $user, $new_pass);
  274. wp_set_password($new_pass, $user->ID);
  275. wp_password_change_notification($user);
  276. }
  277. function notices() {
  278. if (isset($this->error->errors) && sizeof($this->error->errors)>0 && $this->error->get_error_code()) {
  279. $error_html ='<ul class="errors">';
  280. foreach ($this->error->errors as $error) {
  281. $error_html .='<li>'.$error[0].'</li>';
  282. }
  283. $error_html .='</ul>';
  284. appthemes_display_notice( 'error', $error_html );
  285. }
  286. }
  287. }
  288. function appthemes_get_password_reset_url($context = 'display') {
  289. $url = '';
  290. $pre = get_option('permalink_structure') == '' ? '&' : '?';
  291. if(!empty($_GET['action']) && 'rp' == $_GET['action'] && !empty($_GET['key']) && !empty($_GET['login'])) {
  292. $url .= 'action='.$_GET['action'].'&key='.$_GET['key'].'&login='.$_GET['login'];
  293. }
  294. if ( $page_id = APP_Password_Reset::get_id() ) {
  295. $permalink = get_permalink( $page_id );
  296. if ( !empty( $url ) ) {
  297. $pre = get_option('permalink_structure') == '' ? '&' : '?';
  298. $permalink = $permalink . $pre . $url;
  299. }
  300. return esc_url( $permalink, null, $context );
  301. }
  302. return esc_url( site_url( 'wp-login.php' ) . '?' . $url, null, $context );
  303. }
  304. class APP_Registration extends APP_View_Page {
  305. private $error;
  306. function __construct( $template ) {
  307. parent::__construct( $template, __('Register', APP_TD) );
  308. }
  309. static function get_id() {
  310. return parent::_get_id( __CLASS__ );
  311. }
  312. function init() {
  313. global $pagenow;
  314. if(isset($_GET['action'])) $theaction = $_GET['action']; else $theaction ='';
  315. if($pagenow == 'wp-login.php' && $theaction == 'register' && !isset($_GET['key']) && self::get_id() ) {
  316. wp_redirect(appthemes_get_registration_url('redirect'));
  317. exit();
  318. }
  319. }
  320. function template_redirect() {
  321. if ( !empty( $_POST['register'] ) && !empty( $_POST['user_login'] ) && !empty( $_POST['user_email'] )) {
  322. $this->process_form();
  323. }
  324. wp_enqueue_script('utils');
  325. wp_enqueue_script('user-profile');
  326. }
  327. function process_form() {
  328. $errors = $this->register_new_user();
  329. if ( !is_wp_error($errors) ) {
  330. $url = APP_Login::get_url('redirect');
  331. $pre = get_option('permalink_structure') == '' ? '&' : '?';
  332. $url = $url . $pre . 'checkemail=registered';
  333. $redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : $url;
  334. wp_safe_redirect( $redirect_to );
  335. exit();
  336. }
  337. }
  338. function register_new_user() {
  339. $posted = array();
  340. $errors = new WP_Error();
  341. $user_pass = wp_generate_password();
  342. $show_password_fields = apply_filters('show_password_fields', true);
  343. // Get (and clean) data
  344. $fields = array(
  345. 'user_login',
  346. 'user_email',
  347. 'pass1',
  348. 'pass2'
  349. );
  350. foreach ($fields as $field) {
  351. if ( isset($_POST[$field]) )
  352. $posted[$field] = stripslashes(trim($_POST[$field]));
  353. }
  354. $sanitized_user_login = sanitize_user( $posted['user_login'] );
  355. $user_email = apply_filters( 'user_registration_email', $posted['user_email'] );
  356. // Check the username
  357. if ( $sanitized_user_login == '' ) {
  358. $errors->add( 'empty_username', __('<strong>ERROR</strong>: Please enter a username.', APP_TD) );
  359. } elseif ( ! validate_username( $posted['user_login'] ) ) {
  360. $errors->add( 'invalid_username', __('<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.', APP_TD) );
  361. $sanitized_user_login = '';
  362. } elseif ( username_exists( $sanitized_user_login ) ) {
  363. $errors->add( 'username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.', APP_TD) );
  364. }
  365. // Check the e-mail address
  366. if ( $user_email == '' ) {
  367. $errors->add( 'empty_email', __('<strong>ERROR</strong>: Please type your e-mail address.', APP_TD) );
  368. } elseif ( ! is_email( $user_email ) ) {
  369. $errors->add( 'invalid_email', __('<strong>ERROR</strong>: The email address isn&#8217;t correct.', APP_TD) );
  370. $user_email = '';
  371. } elseif ( email_exists( $user_email ) ) {
  372. $errors->add( 'email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.', APP_TD) );
  373. }
  374. do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
  375. $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
  376. if ( $errors->get_error_code() ) {
  377. $this->error = $errors;
  378. return $this->error;
  379. }
  380. if ( $show_password_fields ) {
  381. if ( empty($posted['pass1']) ) {
  382. $errors->add( 'empty_password', __('<strong>ERROR</strong>: Please enter a password.', APP_TD) );
  383. } elseif ( empty($posted['pass2']) ) {
  384. $errors->add( 'empty_password', __('<strong>ERROR</strong>: Please enter the password twice.', APP_TD) );
  385. } elseif ( !empty($posted['pass1']) && $posted['pass1'] != $posted['pass2'] ) {
  386. $errors->add( 'password_mismatch', __('<strong>ERROR</strong>: The passwords do not match.', APP_TD) );
  387. }
  388. }
  389. if ( current_theme_supports( 'app-recaptcha' ) ) {
  390. list( $options ) = get_theme_support( 'app-recaptcha' );
  391. require_once ( $options['file'] );
  392. // check and make sure the reCaptcha values match
  393. $resp = recaptcha_check_answer( $options['private_key'], $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field'] );
  394. if ( !$resp->is_valid )
  395. $errors->add( 'invalid_recaptcha', __('<strong>ERROR</strong>: The reCaptcha anti-spam response was incorrect.', APP_TD) );
  396. }
  397. if ( $errors->get_error_code() ) {
  398. $this->error = $errors;
  399. return $this->error;
  400. }
  401. if ( isset($posted['pass1']) )
  402. $user_pass = $posted['pass1'];
  403. // create the account and pass back the new user id
  404. $user_id = wp_create_user( $posted['user_login'], $user_pass, $posted['user_email'] );
  405. // something went wrong captain
  406. if ( !$user_id ) {
  407. $errors->add('registerfail', sprintf(__('<strong>ERROR</strong>: Couldn&#39;t register you... please contact the <a href="mailto:%s">webmaster</a> !', APP_TD), get_option('admin_email')));
  408. if ( $errors->get_error_code() ) {
  409. $this->error = $errors;
  410. return $this->error;
  411. }
  412. }
  413. // change role if needed
  414. // wp_update_user( array ('ID' => $user_id, 'role' => 'contributor') ) ;
  415. // send the user a confirmation and their login details
  416. //app_new_user_notification($user_id, $user_pass);
  417. wp_new_user_notification( $user_id, $user_pass );
  418. if ( $show_password_fields ) {
  419. // set the WP login cookie (log the user in)
  420. $secure_cookie = is_ssl() ? true : false;
  421. wp_set_auth_cookie($user_id, true, $secure_cookie);
  422. if ( isset( $_REQUEST['redirect_to'] ) )
  423. $success_redirect = $_REQUEST['redirect_to'];
  424. else
  425. $success_redirect = get_option('siteurl');
  426. } else {
  427. // WP created password for user, so show a message that it's been emailed to him
  428. $success_redirect = add_query_arg( 'checkemail', 'newpass', APP_Login::get_url('redirect') );
  429. }
  430. // redirect
  431. wp_redirect($success_redirect);
  432. exit;
  433. }
  434. function notices() {
  435. if (isset($this->error->errors) && sizeof($this->error->errors)>0 && $this->error->get_error_code()) {
  436. $error_html ='<ul class="errors">';
  437. foreach ($this->error->errors as $error) {
  438. $error_html .='<li>'.$error[0].'</li>';
  439. }
  440. $error_html .='</ul>';
  441. appthemes_display_notice( 'error', $error_html );
  442. }
  443. }
  444. }