PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/nanomites_webdev/heroframework
PHP | 499 lines | 332 code | 112 blank | 55 comment | 65 complexity | 4bd92b2e1f2a85832f21bdae407c771f MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-2.0
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. * Users Module
  4. *
  5. * Handles logins, registration, subscription management, profile management, forgotten passwords, password management
  6. *
  7. * @author Electric Function, Inc.
  8. * @copyright Electric Function, Inc.
  9. * @package Hero Framework
  10. *
  11. */
  12. class Users extends Front_Controller {
  13. var $public_methods; // these methods can be accessed without being loggedin
  14. function __construct() {
  15. parent::__construct();
  16. $this->public_methods = array('validate','password_reset','login','post_login','forgot_password','post_forgot_password','register','post_registration');
  17. if (!in_array($this->router->fetch_method(), $this->public_methods) and $this->user_model->logged_in() == FALSE) {
  18. redirect('users/login?return=' . query_value_encode(current_url()));
  19. }
  20. }
  21. function index () {
  22. $notice = $this->session->flashdata('notice');
  23. $this->smarty->assign('notice', $notice);
  24. return $this->smarty->display('account_templates/home.thtml');
  25. }
  26. function profile () {
  27. // get custom fields
  28. $custom_fields = $this->user_model->get_custom_fields(array('not_in_admin' => TRUE));
  29. $values = ($this->input->get('values')) ? unserialize(query_value_decode($this->input->get('values'))) : $this->user_model->get();
  30. $errors = ($this->input->get('errors') == 'true') ? $this->session->flashdata('profile_errors') : FALSE;
  31. $this->smarty->assign('custom_fields',$custom_fields);
  32. $this->smarty->assign('values',$values);
  33. $this->smarty->assign('validation_errors',$errors);
  34. return $this->smarty->display('account_templates/profile.thtml');
  35. }
  36. function post_profile () {
  37. // create an array of current values in case we redirect with an error
  38. $values = array();
  39. $values['username'] = $this->input->post('username');
  40. $values['email'] = $this->input->post('email');
  41. $values['first_name'] = $this->input->post('first_name');
  42. $values['last_name'] = $this->input->post('last_name');
  43. $custom_fields = $this->user_model->get_custom_fields(array('not_in_admin' => TRUE));
  44. $this->load->library('custom_fields/form_builder');
  45. $this->form_builder->build_form_from_array($custom_fields);
  46. $values = $this->form_builder->post_to_array();
  47. $values = query_value_encode(serialize($values));
  48. // validate standard and custom form fields
  49. $validated = $this->user_model->validation(TRUE, FALSE);
  50. if ($validated !== TRUE) {
  51. $this->session->set_flashdata('profile_errors',$validated);
  52. return redirect('users/profile?errors=true&values=' . $values);
  53. }
  54. // we validated! let's update the account
  55. $custom_fields = $this->form_builder->post_to_array();
  56. $this->user_model->update_user(
  57. $this->user_model->get('id'),
  58. $this->input->post('email'),
  59. $this->input->post('password'),
  60. $this->input->post('username'),
  61. $this->input->post('first_name'),
  62. $this->input->post('last_name'),
  63. $this->user_model->get('usergroups'),
  64. ($this->user_model->is_admin()) ? TRUE : FALSE, // not an administratior
  65. $custom_fields
  66. );
  67. $this->session->set_flashdata('notice','You have successfully updated your profile.');
  68. return redirect('users/');
  69. }
  70. function password () {
  71. $validation_errors = ($this->input->get('errors') == 'true') ? $this->session->flashdata('password_errors') : FALSE;
  72. $this->smarty->assign('validation_errors',$validation_errors);
  73. return $this->smarty->display('account_templates/change_password.thtml');
  74. }
  75. function post_password () {
  76. $this->load->library('form_validation');
  77. // this helper will verify their current password
  78. $this->load->helper('verify_password');
  79. $this->form_validation->set_rules('password','Current Password','required|verify_password');
  80. $this->form_validation->set_rules('new_password','New Password','required|min_length[6]|matches[new_password2]');
  81. $this->form_validation->set_rules('new_password2','Repeat New Password','required');
  82. if ($this->form_validation->run() == FALSE) {
  83. $this->session->set_flashdata('password_errors',validation_errors());
  84. return redirect('users/password?errors=true');
  85. }
  86. $this->user_model->update_password($this->user_model->get('id'), $this->input->post('new_password'));
  87. $this->session->set_flashdata('notice','You have successfully updated your password.');
  88. redirect('users');
  89. }
  90. function forgot_password () {
  91. $error = ($this->input->get('error')) ? query_value_decode($this->input->get('error')) : '';
  92. $this->smarty->assign('error',$error);
  93. return $this->smarty->display('account_templates/forgot_password.thtml');
  94. }
  95. function post_forgot_password () {
  96. // validate
  97. $this->load->library('form_validation');
  98. $this->form_validation->set_rules('email','Email','trim|valid_email');
  99. if ($this->form_validation->run() == FALSE) {
  100. return redirect('users/forgot_password?error=' . query_value_encode('You have entered an invalid email address.'));
  101. }
  102. $users = $this->user_model->get_users(array('email' => $this->input->post('email')));
  103. if (count($users) > 1) {
  104. return redirect('users/forgot_password?error=' . query_value_encode('There was an error retrieving your account.'));
  105. }
  106. elseif (empty($users)) {
  107. return redirect('users/forgot_password?error=' . query_value_encode('Your account record could not be retrieved.'));
  108. }
  109. elseif (count($users) == 1) {
  110. // success
  111. $user = $users[0];
  112. $this->user_model->reset_password($user['id']);
  113. return redirect('users/password_reset');
  114. }
  115. return FALSE;
  116. }
  117. function password_reset () {
  118. return $this->smarty->display('account_templates/forgot_password_complete.thtml');
  119. }
  120. function register () {
  121. // do we have a return URL?
  122. $return = ($this->input->get('return')) ? query_value_decode($this->input->get('return')) : '';
  123. // do we have any errors?
  124. $validation_errors = ($this->input->get('errors') == 'true') ? $this->session->flashdata('register_errors') : '';
  125. // get custom fields
  126. $custom_fields = $this->user_model->get_custom_fields(array('registration_form' => TRUE, 'not_in_admin' => TRUE));
  127. // do we have values being passed?
  128. $values = ($this->input->get('values')) ? unserialize(query_value_decode($this->input->get('values'))) : array();
  129. // to stop PHP notices, we'll populate empty values for custom fields if we didn't get any values
  130. if (empty($values) and is_array($custom_fields)) {
  131. foreach ($custom_fields as $field) {
  132. $values[$field['name']] = '';
  133. }
  134. }
  135. $this->smarty->assign('custom_fields',$custom_fields);
  136. $this->smarty->assign('return', $return);
  137. $this->smarty->assign('validation_errors', $validation_errors);
  138. $this->smarty->assign('values',$values);
  139. return $this->smarty->display('account_templates/registration.thtml');
  140. }
  141. function post_registration () {
  142. // get $return if available
  143. if ($this->input->post('return') != '') {
  144. $return = query_value_decode($this->input->post('return'));
  145. }
  146. else {
  147. if (module_installed('billing')) {
  148. // redirect to subscription packages?
  149. $this->load->model('billing/subscription_plan_model');
  150. $plans = $this->subscription_plan_model->get_plans();
  151. }
  152. else {
  153. $plans = FALSE;
  154. }
  155. if (setting('show_subscriptions') == '1' and !empty($plans)) {
  156. $return = site_url('subscriptions');
  157. }
  158. else {
  159. $return = setting('registration_redirect');
  160. }
  161. }
  162. // if we have activated our registration_spam_stopper in the settings,
  163. // we will check to see if the (stupid) bot completed the hidden field
  164. // if so, reject it
  165. if ($this->config->item('registration_spam_stopper') == '1') {
  166. if ($this->input->post('email_confirmation_hp') != '') {
  167. 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.'));
  168. }
  169. }
  170. // create an array of current values in case we redirect with an error
  171. $values = array();
  172. $values['username'] = $this->input->post('username');
  173. $values['email'] = $this->input->post('email');
  174. $values['first_name'] = $this->input->post('first_name');
  175. $values['last_name'] = $this->input->post('last_name');
  176. $custom_fields = $this->user_model->get_custom_fields(array('not_in_admin' => TRUE));
  177. // get values for this form
  178. if (is_array($custom_fields)) {
  179. $this->load->library('custom_fields/form_builder');
  180. $this->form_builder->build_form_from_array($custom_fields);
  181. $values = $this->form_builder->post_to_array();
  182. }
  183. $values = query_value_encode(serialize($values));
  184. // have they agreed to the Terms of Service?
  185. if (setting('require_tos') == '1' and !$this->input->post('agree_tos')) {
  186. $this->session->set_flashdata('register_errors','<p>You must accept the Terms of Service before your account is created.</p>');
  187. return redirect('users/register?return=' . query_value_encode($return) . '&errors=true&values=' . $values);
  188. }
  189. // validate standard and custom form fields
  190. $validated = $this->user_model->validation(FALSE, FALSE);
  191. if ($validated !== TRUE) {
  192. $this->session->set_flashdata('register_errors',$validated);
  193. return redirect('users/register?return=' . query_value_encode($return) . '&errors=true&values=' . $values);
  194. }
  195. // we validated! let's create the account
  196. $this->form_builder->build_form_from_array($custom_fields);
  197. $custom_fields = $this->form_builder->post_to_array();
  198. // are we validating the emails?
  199. $validation = (setting('validate_emails') == '1') ? TRUE : FALSE;
  200. $user_id = $this->user_model->new_user(
  201. $this->input->post('email'),
  202. $this->input->post('password'),
  203. $this->input->post('username'),
  204. $this->input->post('first_name'),
  205. $this->input->post('last_name'),
  206. FALSE, // default usergroup
  207. FALSE, // no affiliate
  208. FALSE, // not an administratior
  209. $custom_fields,
  210. $validation // require validation
  211. );
  212. // log them in
  213. $this->user_model->login_by_id($user_id);
  214. // do we have a relative URL?
  215. if (strpos($return,'http') === FALSE) {
  216. $return = site_url($return);
  217. }
  218. return header('Location: ' . $return);
  219. }
  220. function login () {
  221. // are they already logged in?
  222. if ($this->user_model->logged_in()) {
  223. // send to main account page
  224. return redirect('users');
  225. }
  226. // do we have a return URL?
  227. $return = ($this->input->get('return')) ? query_value_decode($this->input->get('return')) : '';
  228. // do we have any errors?
  229. $validation_errors = ($this->input->get('errors') == 'true') ? $this->session->flashdata('login_errors') : '';
  230. // do we have any notices?
  231. $notices = ($this->session->flashdata('notices')) ? $this->session->flashdata('notices') : FALSE;
  232. // do we have a username?
  233. $username = ($this->input->get('username')) ? $this->input->get('username') : '';
  234. $this->smarty->assign('return',$return);
  235. $this->smarty->assign('username',$username);
  236. $this->smarty->assign('validation_errors',$validation_errors);
  237. $this->smarty->assign('notices', $notices);
  238. return $this->smarty->display('account_templates/login.thtml');
  239. }
  240. function post_login () {
  241. // get $return if available
  242. if ($this->input->post('return') != '') {
  243. $return = query_value_decode($this->input->post('return'));
  244. }
  245. else {
  246. $return = site_url('users');
  247. }
  248. // validate fields
  249. $this->load->library('form_validation');
  250. $this->form_validation->set_rules('username','Username/Email','trim|required');
  251. $this->form_validation->set_rules('password','Password','trim|required');
  252. if ($this->form_validation->run() == FALSE) {
  253. $this->session->set_flashdata('login_errors',validation_errors());
  254. return redirect('users/login?return=' . query_value_encode($return) . '&errors=true&username=' . $this->input->post('username'));
  255. }
  256. // are we remembering this user?
  257. $remember = ($this->input->post('remember') and $this->input->post('remember') != '') ? TRUE : FALSE;
  258. // attempt login
  259. if ($this->user_model->login($this->input->post('username'), $this->input->post('password'), $remember)) {
  260. // success!
  261. // do we have a relative URL?
  262. if (strpos($return,'http') === FALSE) {
  263. $return = site_url($return);
  264. }
  265. return header('Location: ' . $return);
  266. }
  267. else {
  268. if ($this->user_model->failed_due_to_activation == TRUE) {
  269. $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.');
  270. }
  271. elseif ($this->user_model->failed_due_to_duplicate_login == TRUE) {
  272. $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.');
  273. }
  274. else {
  275. $this->session->set_flashdata('login_errors','<p>Login failed. Please verify your username/email and password.');
  276. }
  277. return redirect('users/login?return=' . query_value_encode($return) . '&errors=true');
  278. }
  279. }
  280. function logout () {
  281. $this->user_model->logout();
  282. return redirect('users');
  283. }
  284. function validate ($key) {
  285. // logout so that they go to the login after validating
  286. $this->user_model->logout();
  287. $this->db->select('user_id');
  288. $this->db->where('user_validate_key', $key);
  289. $result = $this->db->get('users');
  290. if ($result->num_rows() == 0) {
  291. 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.'));
  292. }
  293. else {
  294. $this->db->update('users',array('user_validate_key' => ''), array('user_validate_key' => $key));
  295. }
  296. $this->session->set_flashdata('notices','<p>Your account email has been validated successfully.</p>');
  297. return redirect('users/login');
  298. }
  299. function invoices ($subscription_id = FALSE) {
  300. $this->load->model('billing/invoice_model');
  301. $filters = array('user_id' => $this->user_model->get('id'));
  302. if ($subscription_id !== FALSE) {
  303. $filters['subscription_id'] = (int)$subscription_id;
  304. }
  305. $invoices = $this->invoice_model->get_invoices($filters);
  306. if ($subscription_id == FALSE) {
  307. $this->smarty->assign('for_subscription',FALSE);
  308. }
  309. else {
  310. $this->smarty->assign('for_subscription',$subscription_id);
  311. }
  312. $this->smarty->assign('invoices', $invoices);
  313. return $this->smarty->display('account_templates/invoices');
  314. }
  315. function invoice ($invoice_id) {
  316. $this->load->model('billing/invoice_model');
  317. $invoice = $this->invoice_model->get_invoice($invoice_id);
  318. if (empty($invoice_id) or empty($invoice)) {
  319. die(show_error('Unable to find an invoice by that ID.'));
  320. }
  321. // get invoice lines
  322. $lines = $this->invoice_model->invoice_lines($invoice['id']);
  323. // format address
  324. $this->load->helper('format_street_address');
  325. $formatted_address = format_street_address($invoice['billing_address']);
  326. // remove <br />'s
  327. $formatted_address = strip_tags($formatted_address);
  328. // get shipping address
  329. $this->load->model('store/order_model');
  330. $order = $this->order_model->get_order($invoice['id'], 'order_id');
  331. $shipping_address = FALSE;
  332. if (!empty($order)) {
  333. if (!empty($order['shipping'])) {
  334. $shipping_address = $order['shipping'];
  335. }
  336. }
  337. if (!empty($shipping_address)) {
  338. $formatted_shipping_address = format_street_address($shipping_address);
  339. // remove <br />'s
  340. $formatted_shipping_address = strip_tags($formatted_shipping_address);
  341. }
  342. else {
  343. $formatted_shipping_address = FALSE;
  344. }
  345. // get other invoice data
  346. $data = $this->invoice_model->get_invoice_data($invoice['id']);
  347. $this->smarty->assign('invoice',$invoice);
  348. $this->smarty->assign('lines', $lines);
  349. $this->smarty->assign('formatted_address', $formatted_address);
  350. $this->smarty->assign('shipping_address', $shipping_address);
  351. $this->smarty->assign('formatted_shipping_address', $formatted_shipping_address);
  352. $this->smarty->assign('shipping',$data['shipping']);
  353. $this->smarty->assign('subtotal',$data['subtotal']);
  354. $this->smarty->assign('tax',$data['tax']);
  355. $this->smarty->assign('total',$data['total']);
  356. $this->smarty->assign('discount',$data['discount']);
  357. return $this->smarty->display('account_templates/invoice');
  358. }
  359. function cancel ($subscription_id) {
  360. if (module_installed('billing')) {
  361. $this->load->model('billing/subscription_model');
  362. $subscription = $this->subscription_model->get_subscription($subscription_id);
  363. if (empty($subscription) or $subscription['user_id'] != $this->user_model->get('id')) {
  364. die(show_error('The subscription your attempting to cancel is invalid.'));
  365. }
  366. if ($this->input->post('confirm')) {
  367. // do the cancellation
  368. $this->subscription_model->cancel_subscription($subscription_id);
  369. $this->smarty->assign('cancelled',TRUE);
  370. }
  371. else {
  372. $this->smarty->assign('cancelled',FALSE);
  373. }
  374. $this->smarty->assign('subscription',$subscription);
  375. $this->smarty->display('account_templates/cancel_subscription');
  376. }
  377. else {
  378. die(show_error('Billing module is not installed.'));
  379. }
  380. }
  381. function update_cc ($subscription_id) {
  382. $this->load->model('billing/subscription_model');
  383. $subscription = $this->subscription_model->get_subscription($subscription_id);
  384. header('Location: ' . $subscription['renew_link']);
  385. die();
  386. }
  387. }