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