heroframework /app/modules/users/controllers/users.php

Language PHP Lines 499
MD5 Hash 4bd92b2e1f2a85832f21bdae407c771f Estimated Cost $8,462 (why?)
Repository https://bitbucket.org/nanomites_webdev/heroframework.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
* Users Module
*
* Handles logins, registration, subscription management, profile management, forgotten passwords, password management
*
* @author Electric Function, Inc.
* @copyright Electric Function, Inc.
* @package Hero Framework
*
*/

class Users extends Front_Controller {
	var $public_methods; // these methods can be accessed without being loggedin

	function __construct() {
		parent::__construct();
		
		$this->public_methods = array('validate','password_reset','login','post_login','forgot_password','post_forgot_password','register','post_registration');
		
		if (!in_array($this->router->fetch_method(), $this->public_methods) and $this->user_model->logged_in() == FALSE) {
			redirect('users/login?return=' . query_value_encode(current_url()));
		}
	}
	
	function index () {
		$notice = $this->session->flashdata('notice');
	
		$this->smarty->assign('notice', $notice);
		return $this->smarty->display('account_templates/home.thtml');
	}
	
	function profile () {
		// get custom fields
		$custom_fields = $this->user_model->get_custom_fields(array('not_in_admin' => TRUE));
		
		$values = ($this->input->get('values')) ? unserialize(query_value_decode($this->input->get('values'))) : $this->user_model->get();
		
		$errors = ($this->input->get('errors') == 'true') ? $this->session->flashdata('profile_errors') : FALSE;
		
		$this->smarty->assign('custom_fields',$custom_fields);
		$this->smarty->assign('values',$values);
		$this->smarty->assign('validation_errors',$errors);
		return $this->smarty->display('account_templates/profile.thtml');
	}
	
	function post_profile () {
		// create an array of current values in case we redirect with an error
		$values = array();
		$values['username'] = $this->input->post('username');
		$values['email'] = $this->input->post('email');
		$values['first_name'] = $this->input->post('first_name');
		$values['last_name'] = $this->input->post('last_name');
		
		$custom_fields = $this->user_model->get_custom_fields(array('not_in_admin' => TRUE));
		
		$this->load->library('custom_fields/form_builder');
		$this->form_builder->build_form_from_array($custom_fields);
		$values = $this->form_builder->post_to_array();
		$values = query_value_encode(serialize($values));
		
		// validate standard and custom form fields
		$validated = $this->user_model->validation(TRUE, FALSE);
		
		if ($validated !== TRUE) {
			$this->session->set_flashdata('profile_errors',$validated);
		
			return redirect('users/profile?errors=true&values=' . $values);
		}
		
		// we validated!  let's update the account
		$custom_fields = $this->form_builder->post_to_array();
			
		$this->user_model->update_user(
										$this->user_model->get('id'),
										$this->input->post('email'),
										$this->input->post('password'),
										$this->input->post('username'),
										$this->input->post('first_name'),
										$this->input->post('last_name'),
										$this->user_model->get('usergroups'),
										($this->user_model->is_admin()) ? TRUE : FALSE, // not an administratior
										$custom_fields
									);
											
		$this->session->set_flashdata('notice','You have successfully updated your profile.');
		
		return redirect('users/');
	}
	
	function password () {
		$validation_errors = ($this->input->get('errors') == 'true') ? $this->session->flashdata('password_errors') : FALSE;
	
		$this->smarty->assign('validation_errors',$validation_errors);	
		return $this->smarty->display('account_templates/change_password.thtml');
	}
	
	function post_password () {
		$this->load->library('form_validation');
		
		// this helper will verify their current password
		$this->load->helper('verify_password');
		
		$this->form_validation->set_rules('password','Current Password','required|verify_password');
		$this->form_validation->set_rules('new_password','New Password','required|min_length[6]|matches[new_password2]');
		$this->form_validation->set_rules('new_password2','Repeat New Password','required');
		
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('password_errors',validation_errors());
		
			return redirect('users/password?errors=true');
		}
		
		$this->user_model->update_password($this->user_model->get('id'), $this->input->post('new_password'));
		
		$this->session->set_flashdata('notice','You have successfully updated your password.');
		
		redirect('users');
	}
	
	function forgot_password () {
		$error = ($this->input->get('error')) ? query_value_decode($this->input->get('error')) : '';
	
		$this->smarty->assign('error',$error);
		return $this->smarty->display('account_templates/forgot_password.thtml');
	}
	
	function post_forgot_password () {
		// validate
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('email','Email','trim|valid_email');
		
		if ($this->form_validation->run() == FALSE) {
			return redirect('users/forgot_password?error=' . query_value_encode('You have entered an invalid email address.'));
		}
	
		$users = $this->user_model->get_users(array('email' => $this->input->post('email')));
		
		if (count($users) > 1) {
			return redirect('users/forgot_password?error=' . query_value_encode('There was an error retrieving your account.'));
		}
		elseif (empty($users)) {
			return redirect('users/forgot_password?error=' . query_value_encode('Your account record could not be retrieved.'));
		}
		elseif (count($users) == 1) {
			// success
			$user = $users[0];
			
			$this->user_model->reset_password($user['id']);
			
			return redirect('users/password_reset');
		}
		
		return FALSE;
	}
	
	function password_reset () {
		return $this->smarty->display('account_templates/forgot_password_complete.thtml');
	}
	
	function register () {
		// do we have a return URL?
		$return = ($this->input->get('return')) ? query_value_decode($this->input->get('return')) : '';
		
		// do we have any errors?
		$validation_errors = ($this->input->get('errors') == 'true') ? $this->session->flashdata('register_errors') : '';
		
		// get custom fields
		$custom_fields = $this->user_model->get_custom_fields(array('registration_form' => TRUE, 'not_in_admin' => TRUE));
		
		// do we have values being passed?
		$values = ($this->input->get('values')) ? unserialize(query_value_decode($this->input->get('values'))) : array();
		
		// to stop PHP notices, we'll populate empty values for custom fields if we didn't get any values
		if (empty($values) and is_array($custom_fields)) {
			foreach ($custom_fields as $field) {
				$values[$field['name']] = '';
			}
		}
		
		$this->smarty->assign('custom_fields',$custom_fields);
		$this->smarty->assign('return', $return);
		$this->smarty->assign('validation_errors', $validation_errors);
		$this->smarty->assign('values',$values);
		return $this->smarty->display('account_templates/registration.thtml');
	}
	
	function post_registration () {
		// get $return if available
		if ($this->input->post('return') != '') {
			$return = query_value_decode($this->input->post('return'));
		}
		else {
			if (module_installed('billing')) {
				// redirect to subscription packages?
				$this->load->model('billing/subscription_plan_model');
				$plans = $this->subscription_plan_model->get_plans();
			}
			else {
				$plans = FALSE;
			}
			
			if (setting('show_subscriptions') == '1' and !empty($plans)) {
				$return = site_url('subscriptions');
			}
			else {
				$return = setting('registration_redirect');
			}
		}
		
		// if we have activated our registration_spam_stopper in the settings,
		// we will check to see if the (stupid) bot completed the hidden field
		// if so, reject it
		if ($this->config->item('registration_spam_stopper') == '1') {
			if ($this->input->post('email_confirmation_hp') != '') {
				die(show_error('This registration was rejected for being spam.  If you are not a spambot, please don\'t complete the "Email Confirmation HP" field.'));
			}
		}
		
		// create an array of current values in case we redirect with an error
		$values = array();
		$values['username'] = $this->input->post('username');
		$values['email'] = $this->input->post('email');
		$values['first_name'] = $this->input->post('first_name');
		$values['last_name'] = $this->input->post('last_name');
		
		$custom_fields = $this->user_model->get_custom_fields(array('not_in_admin' => TRUE));
		
		// get values for this form
		if (is_array($custom_fields)) {
			$this->load->library('custom_fields/form_builder');
			$this->form_builder->build_form_from_array($custom_fields);
		
			$values = $this->form_builder->post_to_array();
		}
		
		$values = query_value_encode(serialize($values));
		
		// have they agreed to the Terms of Service?
		if (setting('require_tos') == '1' and !$this->input->post('agree_tos')) {
			$this->session->set_flashdata('register_errors','<p>You must accept the Terms of Service before your account is created.</p>');
		
			return redirect('users/register?return=' . query_value_encode($return) . '&errors=true&values=' . $values);
		}
		
		// validate standard and custom form fields
		$validated = $this->user_model->validation(FALSE, FALSE);
		
		if ($validated !== TRUE) {
			$this->session->set_flashdata('register_errors',$validated);
		
			return redirect('users/register?return=' . query_value_encode($return) . '&errors=true&values=' . $values);
		}
		
		// we validated!  let's create the account
		$this->form_builder->build_form_from_array($custom_fields);
		$custom_fields = $this->form_builder->post_to_array();
		
		// are we validating the emails?
		$validation = (setting('validate_emails') == '1') ? TRUE : FALSE;
			
		$user_id = $this->user_model->new_user(
												$this->input->post('email'),
												$this->input->post('password'),
												$this->input->post('username'),
												$this->input->post('first_name'),
												$this->input->post('last_name'),
												FALSE, // default usergroup
												FALSE, // no affiliate
												FALSE, // not an administratior
												$custom_fields,
												$validation // require validation
											);
											
		// log them in
		$this->user_model->login_by_id($user_id);
											
		// do we have a relative URL?
		if (strpos($return,'http') === FALSE) {
			$return = site_url($return);
		}
			
		return header('Location: ' . $return);
	}
	
	function login () {
		// are they already logged in?
		if ($this->user_model->logged_in()) {
			// send to main account page
			return redirect('users');
		}
	
		// do we have a return URL?
		$return = ($this->input->get('return')) ? query_value_decode($this->input->get('return')) : '';
		
		// do we have any errors?
		$validation_errors = ($this->input->get('errors') == 'true') ? $this->session->flashdata('login_errors') : '';
		
		// do we have any notices?
		$notices = ($this->session->flashdata('notices')) ? $this->session->flashdata('notices') : FALSE;
		
		// do we have a username?
		$username = ($this->input->get('username')) ? $this->input->get('username') : '';
			
		$this->smarty->assign('return',$return);
		$this->smarty->assign('username',$username);
		$this->smarty->assign('validation_errors',$validation_errors);
		$this->smarty->assign('notices', $notices);
		return $this->smarty->display('account_templates/login.thtml');
	}
	
	function post_login () {
		// get $return if available
		if ($this->input->post('return') != '') {
			$return = query_value_decode($this->input->post('return'));
		}
		else {
			$return = site_url('users');
		}
		
		// validate fields
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username','Username/Email','trim|required');
		$this->form_validation->set_rules('password','Password','trim|required');
		
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('login_errors',validation_errors());
		
			return redirect('users/login?return=' . query_value_encode($return) . '&errors=true&username=' . $this->input->post('username'));
		}
		
		// are we remembering this user?
		$remember = ($this->input->post('remember') and $this->input->post('remember') != '') ? TRUE : FALSE;
		
		// attempt login
		if ($this->user_model->login($this->input->post('username'), $this->input->post('password'), $remember)) {
			// success!
			
			// do we have a relative URL?
			if (strpos($return,'http') === FALSE) {
				$return = site_url($return);
			}
			
			return header('Location: ' . $return);
		}
		else {
			if ($this->user_model->failed_due_to_activation == TRUE) {
				$this->session->set_flashdata('login_errors','<p>Login failed.  Your account email has not been activated yet.  Please click the link in your activation email to activate your account.  If you cannot find the email in your inbox or junk folders, contact website support for assistance.');
			}
			elseif ($this->user_model->failed_due_to_duplicate_login == TRUE) {
				$this->session->set_flashdata('login_errors','<p>Login failed.  Someone is already logged in with this account.  If you believe this is in error, wait 1 minute and try again.  Otherwise, ensure that you are not logged in the site on another device before continuing.');			
			}
			else {
				$this->session->set_flashdata('login_errors','<p>Login failed.  Please verify your username/email and password.');
			}
		
			return redirect('users/login?return=' . query_value_encode($return) . '&errors=true');
		}
	}
	
	function logout () {
		$this->user_model->logout();
		
		return redirect('users');
	}
	
	function validate ($key) {
		// logout so that they go to the login after validating
		$this->user_model->logout();
	
		$this->db->select('user_id');
		$this->db->where('user_validate_key', $key);
		$result = $this->db->get('users');
		
		if ($result->num_rows() == 0) {
			die(show_error('There was an error validating your account email.  Your email may have already been validated, or you need to copy and paste then entire URL from the email into your browser and try again.'));
		}
		else {
			$this->db->update('users',array('user_validate_key' => ''), array('user_validate_key' => $key));
		}
		
		$this->session->set_flashdata('notices','<p>Your account email has been validated successfully.</p>');
		
		return redirect('users/login');
	}
	
	function invoices ($subscription_id = FALSE) {
		$this->load->model('billing/invoice_model');
		
		$filters = array('user_id' => $this->user_model->get('id'));
		
		if ($subscription_id !== FALSE) {
			$filters['subscription_id'] = (int)$subscription_id;
		}
		
		$invoices = $this->invoice_model->get_invoices($filters);
		
		if ($subscription_id == FALSE) {
			$this->smarty->assign('for_subscription',FALSE);
		}
		else {
			$this->smarty->assign('for_subscription',$subscription_id);
		}

		$this->smarty->assign('invoices', $invoices);
		return $this->smarty->display('account_templates/invoices');		
	}
	
	function invoice ($invoice_id) {
		$this->load->model('billing/invoice_model');
		$invoice = $this->invoice_model->get_invoice($invoice_id);
		
		if (empty($invoice_id) or empty($invoice)) {
			die(show_error('Unable to find an invoice by that ID.'));
		}
		
		// get invoice lines
		$lines = $this->invoice_model->invoice_lines($invoice['id']);
		
		// format address
		$this->load->helper('format_street_address');
		$formatted_address = format_street_address($invoice['billing_address']);
		// remove <br />'s
		$formatted_address = strip_tags($formatted_address);
		
		// get shipping address
		$this->load->model('store/order_model');
		$order = $this->order_model->get_order($invoice['id'], 'order_id');
		
		$shipping_address = FALSE;
		if (!empty($order)) {
			if (!empty($order['shipping'])) {
				$shipping_address = $order['shipping'];
			}
		}
		
		if (!empty($shipping_address)) {
			$formatted_shipping_address = format_street_address($shipping_address);
			// remove <br />'s
			$formatted_shipping_address = strip_tags($formatted_shipping_address);
		}
		else {
			$formatted_shipping_address = FALSE;
		}
		
		// get other invoice data
		$data = $this->invoice_model->get_invoice_data($invoice['id']);
		
		$this->smarty->assign('invoice',$invoice);
		$this->smarty->assign('lines', $lines);
		$this->smarty->assign('formatted_address', $formatted_address);
		$this->smarty->assign('shipping_address', $shipping_address);
		$this->smarty->assign('formatted_shipping_address', $formatted_shipping_address);
		$this->smarty->assign('shipping',$data['shipping']);
		$this->smarty->assign('subtotal',$data['subtotal']);
		$this->smarty->assign('tax',$data['tax']);
		$this->smarty->assign('total',$data['total']);
		$this->smarty->assign('discount',$data['discount']);
		
		return $this->smarty->display('account_templates/invoice');
	}
	
	function cancel ($subscription_id) {
		if (module_installed('billing')) {
			$this->load->model('billing/subscription_model');
			$subscription = $this->subscription_model->get_subscription($subscription_id);
			
			if (empty($subscription) or $subscription['user_id'] != $this->user_model->get('id')) {
				die(show_error('The subscription your attempting to cancel is invalid.'));
			}
			
			if ($this->input->post('confirm')) {
				// do the cancellation
				$this->subscription_model->cancel_subscription($subscription_id);
				
				$this->smarty->assign('cancelled',TRUE);
			}
			else {
				$this->smarty->assign('cancelled',FALSE);
			}
			
			$this->smarty->assign('subscription',$subscription);
			$this->smarty->display('account_templates/cancel_subscription');
		}
		else {
			die(show_error('Billing module is not installed.'));
		}
	}
	
	function update_cc ($subscription_id) {
		$this->load->model('billing/subscription_model');
		$subscription = $this->subscription_model->get_subscription($subscription_id);
		
		header('Location: ' . $subscription['renew_link']);
		die();
	}
}
Back to Top