· 10 years ago · Jul 28, 2016, 08:42 AM
1<?php defined('BASEPATH') OR exit('No direct script access allowed');
2
3class User extends Public_Controller {
4
5 function __construct()
6 {
7 parent::__construct();
8 $this->load->library('ion_auth');
9 $this->load->library('form_validation');
10
11 $this->form_validation->set_error_delimiters(
12 $this->config->item('error_start_delimiter', 'ion_auth'),
13 $this->config->item('error_end_delimiter', 'ion_auth')
14 );
15
16 $current_lang = $this->session->userdata('current_lang');
17
18 $this->lang->load('user',$current_lang['language_name']);
19 $this->lang->load('ion_auth', $current_lang['language_name']);
20 $this->lang->load('form_validation', $current_lang['language_name']);
21
22 }
23
24 // log the user in
25 function login()
26 {
27 //$this->_initial_login_check();
28
29 //validate form input
30 $this->form_validation->set_rules('identity', lang('login_validation_identity_label'), 'required|valid_email');
31 $this->form_validation->set_rules('password', lang('login_validation_password_label'), 'required');
32
33 if ($this->form_validation->run() == true)
34 {
35 // check to see if the user is logging in
36 // check for "remember me"
37 //$remember = (bool) $this->input->post('remember');
38 $remember = 1;
39
40 if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
41 {
42 //if the login is successful
43 //redirect them back to the referrer page
44 $url_after_login = $this->input->post('url_after_login');
45 $this->session->set_userdata('is_logged_in', 1);
46 $this->session->set_userdata('is_admin', $this->ion_auth->is_admin());
47 $this->session->set_userdata('username', $this->ion_auth->user()->row()->username);
48 $this->session->set_userdata('user_id', $this->ion_auth->user()->row()->user_id);
49 $this->session->set_userdata('group_id', $this->ion_auth->user()->row()->group_id);
50 $this->session->set_userdata('total_points', $this->ion_auth->user()->row()->total_points);
51 //$this->session->set_userdata('avatar', $this->ion_auth->user()->row()->avatar);
52 $this->session->set_flashdata('notice', $this->ion_auth->messages());
53 redirect($url_after_login, 'refresh');
54 }
55 else
56 {
57 // if the login was un-successful
58 // redirect them back to the login page
59 $this->session->set_flashdata('error', $this->ion_auth->errors());
60 redirect(create_href('l_user/login'), 'refresh'); // use redirects instead of loading views for compatibility with MY_Controller libraries
61 }
62 }
63 else
64 {
65
66 $this->session->sess_destroy();
67 // the user is not logging in so display the login page
68 // set the flash data error message if there is one
69 $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
70
71 $this->data['identity'] = array('name' => 'identity',
72 'id' => 'identity',
73 'type' => 'text',
74 'value' => $this->form_validation->set_value('identity'),
75 );
76 $this->data['password'] = array('name' => 'password',
77 'id' => 'password',
78 'type' => 'password',
79 );
80 $this->breadcrumbs->push(lang('site_title'), create_href('l_home'));
81 $this->breadcrumbs->push(lang('login_heading'), 'none');
82 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
83
84 $this->data['title'] = lang('seo_login_title');
85 $this->data['description'] = lang('seo_login_description');
86 $this->data['page'] = 'user/login';
87 $this->render('public/templates/general');
88 }
89 }
90
91 // log the user out
92 function logout()
93 {
94 // log the user out
95 $logout = $this->ion_auth->logout();
96
97 // redirect them to the login page
98 $this->session->set_flashdata('notice', 'hola');
99
100 redirect(create_href('l_home'), 'refresh');
101 }
102
103
104
105 // forgot password
106 function forgot_password()
107 {
108 $this->_initial_login_check();
109
110 $this->config->set_item('identity', 'email');
111
112 // setting validation rules by checking wheather identity is username or email
113 if($this->config->item('identity', 'ion_auth') != 'email' )
114 {
115 $this->form_validation->set_rules('identity', lang('forgot_password_identity_label'), 'required');
116 }
117 else
118 {
119 $this->form_validation->set_rules('identity', lang('forgot_password_validation_email_label'), 'required|valid_email');
120 }
121
122
123 if ($this->form_validation->run() == false)
124 {
125 $this->data['type'] = $this->config->item('identity','ion_auth');
126 // setup the input
127 $this->data['identity'] = array('name' => 'identity',
128 'id' => 'identity',
129 );
130 if ( $this->config->item('identity', 'ion_auth') != 'email' ){
131 $this->data['identity_label'] = lang('forgot_password_identity_label');
132 }
133 else
134 {
135 $this->data['identity_label'] = lang('forgot_password_email_identity_label');
136 }
137 // set any errors and display the form
138 $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
139
140
141 $this->breadcrumbs->push(lang('site_title'), create_href('l_home'));
142 $this->breadcrumbs->push(lang('forgot_password_heading'), 'none');
143 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
144
145 $this->data['noindex'] = 1;
146 $this->data['title'] = lang('forgot_password_heading');
147 $this->data['description'] = lang('forgot_password_heading');
148 $this->data['page'] = 'user/forgot_password';
149 $this->render('public/templates/general');
150
151
152 }
153 else
154 {
155 $identity_column = $this->config->item('identity','ion_auth');
156 $identity = $this->ion_auth->where($identity_column, $this->input->post('identity'))->users()->row();
157 if(empty($identity)) {
158 if($this->config->item('identity', 'ion_auth') != 'email')
159 {
160 $this->ion_auth->set_error('forgot_password_identity_not_found');
161 }
162 else
163 {
164 $this->ion_auth->set_error('forgot_password_email_not_found');
165 }
166 $this->session->set_flashdata('error', $this->ion_auth->errors());
167 redirect(create_href('l_user/forgot_password'), 'refresh');
168 }
169 // run the forgotten password method to email an activation code to the user
170 $forgotten = $this->ion_auth->forgotten_password($identity->{$this->config->item('identity', 'ion_auth')});
171 if ($forgotten)
172 {
173 // if there were no errors
174 $this->session->set_flashdata('notice', $this->ion_auth->messages());
175 redirect(create_href('l_user/login'), 'refresh'); //we should display a confirmation page here instead of the login page
176 }
177 else
178 {
179 $this->session->set_flashdata('error', $this->ion_auth->errors());
180 redirect(create_href('l_user/forgot_password'), 'refresh');
181 }
182 }
183 }
184
185 // reset password - final step for forgotten password
186 public function reset_password($code = NULL)
187 {
188 if (!$code)
189 {
190 show_404();
191 }
192
193 $user = $this->ion_auth->forgotten_password_check($code);
194
195 if ($user)
196 {
197 // if the code is valid then display the password reset form
198 $this->form_validation->set_rules('new', lang('reset_password_validation_new_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[new_confirm]');
199 $this->form_validation->set_rules('new_confirm', lang('reset_password_validation_new_password_confirm_label'), 'required');
200
201 if ($this->form_validation->run() == false)
202 {
203 // display the form
204
205 // set the flash data error message if there is one
206 $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
207
208 $this->data['min_password_length'] = $this->config->item('min_password_length', 'ion_auth');
209 $this->data['new_password'] = array(
210 'name' => 'new',
211 'id' => 'new',
212 'type' => 'password'
213 );
214 $this->data['new_password_confirm'] = array(
215 'name' => 'new_confirm',
216 'id' => 'new_confirm',
217 'type' => 'password'
218 );
219 $this->data['user_id'] = array(
220 'name' => 'user_id',
221 'id' => 'user_id',
222 'type' => 'hidden',
223 'value' => $user->id,
224 );
225 $this->data['csrf'] = $this->_get_csrf_nonce();
226 $this->data['code'] = $code;
227
228
229 $this->breadcrumbs->push(lang('site_title'), create_href('l_home'));
230 $this->breadcrumbs->push(lang('reset_password_heading'), 'none');
231 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
232
233 $this->data['noindex'] = 1;
234 $this->data['title'] = lang('reset_password_heading');
235 $this->data['description'] = lang('reset_password_heading');
236 $this->data['page'] = 'user/reset_password';
237 $this->render('public/templates/general');
238
239 }
240 else
241 {
242 // do we have a valid request?
243 /*
244 COMMENTED cause a problem with flash data and crsf protection
245 if ($this->_valid_csrf_nonce() === FALSE || $user->id != $this->input->post('user_id'))
246
247 {
248
249 // something fishy might be up
250 $this->ion_auth->clear_forgotten_password_code($code);
251
252 show_error(lang('error_csrf'));
253
254 }
255 else
256 {
257 // finally change the password
258 $identity = $user->{$this->config->item('identity', 'ion_auth')};
259
260 $change = $this->ion_auth->reset_password($identity, $this->input->post('new'));
261
262 if ($change)
263 {
264 // if the password was successfully changed
265 $this->session->set_flashdata('notice', $this->ion_auth->messages());
266 redirect(create_href('l_user/login'), 'refresh');
267 }
268 else
269 {
270 $this->session->set_flashdata('error', $this->ion_auth->errors());
271 redirect(create_href('l_user/reset_password/', $code), 'refresh');
272 }
273 }*/
274
275 // finally change the password
276 $identity = $user->{$this->config->item('identity', 'ion_auth')};
277
278 $change = $this->ion_auth->reset_password($identity, $this->input->post('new'));
279
280 if ($change)
281 {
282 // if the password was successfully changed
283 $this->session->set_flashdata('notice', $this->ion_auth->messages());
284 redirect(create_href('l_user/login'), 'refresh');
285 }
286 else
287 {
288 $this->session->set_flashdata('error', $this->ion_auth->errors());
289 redirect(create_href('l_user/reset_password/', $code), 'refresh');
290 }
291 }
292 }
293 else
294 {
295 // if the code is invalid then send them back to the forgot password page
296 $this->session->set_flashdata('error', $this->ion_auth->errors());
297 redirect(create_href('l_forgot/password'), 'refresh');
298 }
299 }
300
301 // create a new user
302 function signup()
303 {
304 // check to know if comes from birthday form
305 if(!$this->session->flashdata('step1'))
306 {
307 //propagate status and msg to keep msg under age
308 if ($this->input->post('status'))
309 {
310 $this->data['status'] = $this->input->post('status');
311 }
312 else
313 {
314 redirect(create_href('l_user/signup'));
315 }
316 $this->data['msg_age'] = $this->input->post('msg_age');
317 }
318 else
319 {
320 $flash_data = $this->session->flashdata('step1');
321 $msg_age = $flash_data['msg_age'];
322 $birthday = $flash_data['birthday'];
323 $status = $flash_data['status'];
324
325 $this->data['status'] = $status;
326 $this->data['msg_age'] = $msg_age;
327 }
328
329 $this->_initial_login_check();
330
331 $tables = $this->config->item('tables','ion_auth');
332 $identity_column = $this->config->item('identity','ion_auth');
333 $this->data['identity_column'] = $identity_column;
334
335
336 $this->form_validation->set_rules('username',lang('create_user_validation_username_label'),'required|min_length[3]|max_length[14]|alpha_numeric|is_unique['.$tables['users'].'.username]');
337 $this->form_validation->set_rules('email',lang('create_user_validation_email_label'),'required|valid_email|is_unique['.$tables['users'].'.email]|matches[email_confirm]');
338 $this->form_validation->set_rules('email_confirm',lang('create_user_validation_email_confirm_label'),'required|valid_email');
339 $this->form_validation->set_rules('password', lang('create_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
340 $this->form_validation->set_rules('password_confirm', lang('create_user_validation_password_confirm_label'), 'required');
341
342 $this->form_validation->set_rules('first_name', lang('create_user_validation_fname_label'), 'required');
343 $this->form_validation->set_rules('last_name', lang('create_user_validation_lname_label'), 'required');
344 $this->form_validation->set_rules('birthday', lang('create_user_validation_birthday_label'), 'required');
345 $this->form_validation->set_rules('legal', lang('create_user_validation_birthday_label'), 'callback_accept_terms');
346
347 if ($this->form_validation->run() == true)
348 {
349 $email = strtolower($this->input->post('email'));
350 $identity = ($identity_column==='email') ? $email : $this->input->post('identity');
351 $password = $this->input->post('password');
352
353 $additional_data = array(
354 'username' => $this->input->post('username'),
355 'first_name' => $this->input->post('first_name'),
356 'last_name' => $this->input->post('last_name'),
357 'country_code' => $this->input->post('country'),
358 'gender' => $this->input->post('gender'),
359 'birthday' => date('Y-m-d',strtotime($this->input->post('birthday')))
360 );
361 }
362 if ($this->form_validation->run() == true && $this->ion_auth->register($identity, $password, $email, $additional_data))
363 {
364 // check to see if we are creating the user
365
366 // If registered we add email to subscribers table
367 $exists = $this->public_model->ns_exists_email($email);
368 if (!$exists)
369 {
370 $this->public_model->ns_subscribe_email($email);
371 }
372
373 // redirect them back to the admin page
374 $this->session->set_flashdata('notice', $this->ion_auth->messages());
375 redirect(create_href('l_home'), 'refresh');
376 }
377 else
378 {
379
380 // display the create user form
381 // set the flash data error message if there is one
382 $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
383
384 //propagate birthday when there are error
385 if(!isset($birthday))
386 {
387
388 $birthday = $this->form_validation->set_value('birthday');
389 }
390
391 $this->data['username'] = array(
392 'name' => 'username',
393 'id' => 'username',
394 'type' => 'text',
395 'value' => $this->form_validation->set_value('username'),
396 );
397 $this->data['email'] = array(
398 'name' => 'email',
399 'id' => 'email',
400 'type' => 'text',
401 'value' => $this->form_validation->set_value('email'),
402 );
403 $this->data['email_confirm'] = array(
404 'name' => 'email_confirm',
405 'id' => 'email_confirm',
406 'type' => 'text',
407 'value' => $this->form_validation->set_value('email_confirm'),
408 );
409 $this->data['password'] = array(
410 'name' => 'password',
411 'id' => 'password',
412 'type' => 'password',
413 'value' => $this->form_validation->set_value('password'),
414 );
415 $this->data['password_confirm'] = array(
416 'name' => 'password_confirm',
417 'id' => 'password_confirm',
418 'type' => 'password',
419 'value' => $this->form_validation->set_value('password_confirm'),
420 );
421 $this->data['first_name'] = array(
422 'name' => 'first_name',
423 'id' => 'first_name',
424 'type' => 'text',
425 'value' => $this->form_validation->set_value('first_name'),
426 );
427 $this->data['last_name'] = array(
428 'name' => 'last_name',
429 'id' => 'last_name',
430 'type' => 'text',
431 'value' => $this->form_validation->set_value('last_name'),
432 );
433 $this->data['birthday'] = array(
434 'name' => 'birthday',
435 'id' => 'birthday',
436 'type' => 'text',
437 'placeholder' => 'dd-mm-yyyy',
438 'value' => $birthday,
439 );
440 $this->data['country'] = array(
441 'name' => 'country',
442 'id' => 'country',
443 'type' => 'dropdown',
444 );
445 $this->data['countries'] = $this->public_model->get_countries_array();
446 $this->data['gender'] = array(
447 'name' => 'gender',
448 'id' => 'gender',
449 'type' => 'dropdown',
450 );
451 $this->data['genders'] = array(
452 'male' => lang('male'),
453 'female'=> lang('female')
454 );
455 $this->data['legal'] = array(
456 'name' => 'legal',
457 'id' => 'legal',
458 'value' => 'legal',
459 'checked' => FALSE,
460 'class' => 'legal_check_box'
461 );
462
463 $this->breadcrumbs->push(lang('site_title'), create_href('l_home'));
464 $this->breadcrumbs->push(lang('create_user_heading'), 'none');
465 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
466
467 $this->data['noindex'] = 1;
468 $this->data['title'] = lang('seo_signup2_title');
469 $this->data['description'] = lang('seo_signup2_description');
470 $this->data['page'] = 'user/signup';
471 $this->render('public/templates/general');
472 }
473 }
474 function accept_terms() {
475 if (isset($_POST['legal'])) return true;
476 $this->form_validation->set_message('accept_terms', lang('create_user_validation_legal_label'));
477 return false;
478 }
479
480 // user birthdate
481 function birthday()
482 {
483 $this->_initial_login_check();
484
485 $this->form_validation->set_rules('birthday', lang('create_user_validation_birthday_label'), 'required');
486
487 if ($this->form_validation->run() == true)
488 {
489 if (calculate_age($this->input->post('birthday')) >= '18')
490 {
491 $msg_age = 0;
492 }
493 else
494 {
495 $msg_age = 1;
496 }
497
498 //send to singup 2;
499 $data = array(
500 'status' => 'ok',
501 'birthday' => $this->input->post('birthday'),
502 'msg_age' => $msg_age
503 );
504 //store data to flashdata
505 $this->session->set_flashdata('step1',$data);
506 redirect(create_href('l_user/signup2'));
507 }
508 else
509 {
510 // display the create user form
511 // set the flash data error message if there is one
512 $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
513
514 $this->data['birthday'] = array(
515 'name' => 'birthday',
516 'id' => 'birthday',
517 'type' => 'text',
518 'placeholder' => 'dd-mm-yyyy',
519 'value' => $this->form_validation->set_value('birthday'),
520 );
521
522 $this->breadcrumbs->push(lang('site_title'), create_href('l_home'));
523 $this->breadcrumbs->push(lang('create_user_heading'), 'none');
524 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
525
526 $this->data['title'] = lang('seo_signup1_title');
527 $this->data['description'] = lang('seo_signup1_description');
528 $this->data['page'] = 'user/birthday';
529 $this->render('public/templates/general');
530 }
531 }
532
533 // activate the user
534 function activate($id, $code=false)
535 {
536 if ($code !== false)
537 {
538 $activation = $this->ion_auth->activate($id, $code);
539 }
540
541 if ($activation)
542 {
543 // redirect them to the auth page
544 $this->session->set_flashdata('notice', $this->ion_auth->messages());
545 redirect(create_href('l_user/login'), 'refresh');
546 }
547 else
548 {
549 // redirect them to the forgot password page
550 $this->session->set_flashdata('error', $this->ion_auth->errors());
551 redirect(create_href('l_user/forgot_password'), 'refresh');
552
553 }
554 }
555
556 function profile()
557 {
558 $this->_is_logged_in();
559
560 $this->data['user'] = $this->ion_auth->user()->row();
561 $this->data['points_info'] = $this->public_model->get_user_points_info();
562
563 $this->breadcrumbs->push(lang('site_title'), create_href('l_home'));
564 $this->breadcrumbs->push(lang('profile_heading'), 'none');
565 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
566
567 $this->data['noindex'] = 1;
568 $this->data['title'] = lang('seo_profile_title');
569 $this->data['description'] = lang('seo_profile_description');
570 $this->data['page'] = 'user/profile';
571 $this->render('public/templates/general_noads');
572 }
573
574 function avatar()
575 {
576 $this->_is_logged_in();
577
578 $this->breadcrumbs->push(lang('site_title'), create_href('l_home'));
579 $this->breadcrumbs->push(lang('profile_heading'), create_href('l_user/profile'));
580 $this->breadcrumbs->push(lang('edit_user_avatar'), 'none');
581 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
582
583 $this->data['noindex'] = 1;
584 $this->data['title'] = lang('seo_avatar_title');
585 $this->data['description'] = lang('seo_avatar_description');
586 $this->data['page'] = 'user/avatar';
587 $this->render('public/templates/avatar');
588 }
589
590 function delete()
591 {
592 $this->_is_logged_in();
593 $id = $this->ion_auth->user()->row()->id;
594 $user = $this->ion_auth->user($id)->row();
595
596 $this->form_validation->set_rules('confirm', lang('delete_user_confirm_label'), 'required');
597
598 if (isset($_POST) && !empty($_POST))
599 {
600 // do we have a valid request?
601 /*
602 COMMENTED cause a problem with flash data and crsf protection
603 if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
604 {
605 show_error(lang('error_csrf'));
606 }
607 */
608
609 if ($this->form_validation->run() == true)
610 {
611 $this->ion_auth->delete_user($id);
612 $this->ion_auth->logout();
613 $this->session->set_flashdata('notice', $this->ion_auth->messages() );
614 redirect(create_href('l_home'), 'refresh');
615 }
616 }
617
618 // display the edit user form
619 $this->data['csrf'] = $this->_get_csrf_nonce();
620
621 // set the flash data error message if there is one
622 $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
623
624 // pass the user to the view
625 $this->data['user'] = $user;
626
627 $this->breadcrumbs->push(lang('site_title'), create_href('l_home'));
628 $this->breadcrumbs->push(lang('profile_heading'), create_href('l_user/profile'));
629 $this->breadcrumbs->push(lang('delete_user_heading'), 'none');
630 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
631
632 $this->data['noindex'] = 1;
633 $this->data['title'] = lang('seo_delete_title');
634 $this->data['description'] = lang('seo_delete_description');
635 $this->data['page'] = 'user/delete';
636 $this->render('public/templates/general_noads');
637
638 }
639
640 function edit()
641 {
642 $this->_is_logged_in();
643
644 $tables = $this->config->item('tables','ion_auth');
645 $id = $this->ion_auth->user()->row()->id;
646
647
648 $user = $this->ion_auth->user($id)->row();
649 $groups=$this->ion_auth->groups()->result_array();
650 $currentGroups = $this->ion_auth->get_users_groups($id)->result();
651
652 //CARLOS
653 if($this->input->post('username') != $this->ion_auth->user()->row()->username)
654 {
655 $is_unique_username = '|is_unique['.$tables['users'].'.username]';
656 }
657 else
658 {
659 $is_unique_username = '';
660 }
661 if($this->input->post('email') != $this->ion_auth->user()->row()->email)
662 {
663 $is_unique_email = '|is_unique['.$tables['users'].'.email]';
664 }
665 else
666 {
667 $is_unique_email = '';
668 }
669
670
671
672 $this->form_validation->set_rules('username',lang('create_user_validation_username_label'),'required|min_length[3]|max_length[14]|alpha_numeric'.$is_unique_username);
673 $this->form_validation->set_rules('email',lang('create_user_validation_email_label'),'required|valid_email'.$is_unique_email);
674
675 $this->form_validation->set_rules('first_name', lang('create_user_validation_fname_label'), 'required');
676 $this->form_validation->set_rules('last_name', lang('create_user_validation_lname_label'), 'required');
677 $this->form_validation->set_rules('birthday', lang('create_user_validation_birthday_label'), 'required');
678
679
680 if (isset($_POST) && !empty($_POST))
681 {
682 // do we have a valid request?
683 /*
684 COMMENTED cause a problem with flash data and crsf protection
685 if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
686 {
687 echo 'error:<br>';
688 echo $this->ion_auth->errors();
689 echo 'sessiondata<br>';
690 print_r($this->session->userdata);
691 echo 'valid?<br>';
692 echo $this->_valid_csrf_nonce();
693 echo '<br>';
694 echo 'post_id<br>';
695 echo $this->input->post('id');
696 echo '<br>';
697 show_error(lang('error_csrf'));
698 }
699 */
700
701 // update the password if it was posted
702 if ($this->input->post('password'))
703 {
704 $this->form_validation->set_rules('password', lang('edit_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
705 $this->form_validation->set_rules('password_confirm', lang('edit_user_validation_password_confirm_label'), 'required');
706 }
707
708 if ($this->form_validation->run() === TRUE)
709 {
710 $data = array(
711 'first_name' => $this->input->post('first_name'),
712 'last_name' => $this->input->post('last_name'),
713 'country_code' => $this->input->post('country'),
714 'birthday' => date('Y-m-d',strtotime($this->input->post('birthday'))),
715 'username' => $this->input->post('username'),
716 'email' => $this->input->post('email'),
717 'gender' => $this->input->post('gender')
718 );
719
720 // update the password if it was posted
721 if ($this->input->post('password'))
722 {
723 $data['password'] = $this->input->post('password');
724 }
725
726 // update username session
727 $this->session->set_userdata('username', $this->input->post('username'));
728
729 // check to see if we are updating the user
730 if($this->ion_auth->update($user->id, $data))
731 {
732 // redirect them back to the admin page if admin, or to the base url if non admin
733 $this->session->set_flashdata('notice', $this->ion_auth->messages() );
734 redirect(create_href('l_user/profile'), 'refresh');
735 }
736 else
737 {
738 // redirect them back to the admin page if admin, or to the base url if non admin
739 $this->session->set_flashdata('error', $this->ion_auth->errors() );
740 if ($this->ion_auth->is_admin())
741 redirect(create_href('l_user/profile'), 'refresh');
742 }
743
744 }
745 }
746
747 // display the edit user form
748 $this->data['csrf'] = $this->_get_csrf_nonce();
749
750 // set the flash data error message if there is one
751 $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
752
753 // pass the user to the view
754 $this->data['user'] = $user;
755
756 $this->data['first_name'] = array(
757 'name' => 'first_name',
758 'id' => 'first_name',
759 'type' => 'text',
760 'value' => $this->form_validation->set_value('first_name', $user->first_name),
761 );
762 $this->data['last_name'] = array(
763 'name' => 'last_name',
764 'id' => 'last_name',
765 'type' => 'text',
766 'value' => $this->form_validation->set_value('last_name', $user->last_name),
767 );
768 $this->data['birthday'] = array(
769 'name' => 'birthday',
770 'id' => 'birthday',
771 'type' => 'text',
772 'placeholder' => 'dd-mm-yyyy',
773 'value' => $this->form_validation->set_value('birthday', date("d-m-Y", strtotime($user->birthday))),
774 );
775 $this->data['country'] = array(
776 'name' => 'country',
777 'id' => 'country',
778 'type' => 'dropdown',
779 'class' => 'country',
780 );
781 $this->data['user_country_code'] = $user->country_code;
782 $this->data['countries'] = $this->public_model->get_countries_array();
783 $this->data['user_gender'] = $user->gender;
784 $this->data['gender'] = array(
785 'name' => 'gender',
786 'id' => 'gender',
787 'type' => 'dropdown',
788 );
789 $this->data['genders'] = array(
790 'male' => lang('male'),
791 'female'=> lang('female')
792 );
793 $this->data['username2'] = array(
794 'name' => 'username',
795 'id' => 'username',
796 'type' => 'text',
797 'value' => $this->form_validation->set_value('username', $user->username),
798 );
799 $this->data['email'] = array(
800 'name' => 'email',
801 'id' => 'email',
802 'type' => 'text',
803 'value' => $this->form_validation->set_value('email', $user->email),
804 );
805 $this->data['password'] = array(
806 'name' => 'password',
807 'id' => 'password',
808 'type' => 'password'
809 );
810 $this->data['password_confirm'] = array(
811 'name' => 'password_confirm',
812 'id' => 'password_confirm',
813 'type' => 'password'
814 );
815
816 $this->breadcrumbs->push(lang('site_title'), create_href('l_home'));
817 $this->breadcrumbs->push(lang('profile_heading'), create_href('l_user/profile'));
818 $this->breadcrumbs->push(lang('edit_user_heading'), 'none');
819 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
820
821 $this->data['noindex'] = 1;
822 $this->data['title'] = lang('seo_edit_title');
823 $this->data['description'] = lang('seo_edit_description');
824 $this->data['page'] = 'user/edit';
825 $this->render('public/templates/general_noads');
826
827 }
828
829 public function results()
830 {
831 $this->_is_logged_in();
832
833 $this->breadcrumbs->push( lang('site_title'), create_href());
834 $this->breadcrumbs->push( lang('results'), 'none');
835 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
836
837 $this->data['results_summary_all'] = $this->public_model->get_user_results_summary('all');
838 $this->data['results_summary_last_month'] = $this->public_model->get_user_results_summary('last_month');
839 $this->data['results_summary_this_month'] = $this->public_model->get_user_results_summary('this_month');
840 $this->data['results_summary_this_week'] = $this->public_model->get_user_results_summary('this_week');
841 $this->data['results_summary_today'] = $this->public_model->get_user_results_summary('today');
842
843 //$this->data['results_detail_all'] = $this->public_model->get_user_results_detail('all');
844 $this->data['results_detail_last_month'] = $this->public_model->get_user_results_detail('last_month');
845 $this->data['results_detail_this_month'] = $this->public_model->get_user_results_detail('this_month');
846 $this->data['results_detail_this_week'] = $this->public_model->get_user_results_detail('this_week');
847 $this->data['results_detail_today'] = $this->public_model->get_user_results_detail('today');
848
849 $this->data['noindex'] = 1;
850 $this->data['title'] = lang('seo_results_title');
851 $this->data['description'] = lang('seo_results_description');
852 $this->data['page'] = 'user/results';
853 $this->render('public/templates/general_noads');
854 }
855
856 public function comments()
857 {
858 $this->_is_logged_in();
859
860 $this->breadcrumbs->push( lang('site_title'), create_href());
861 $this->breadcrumbs->push( lang('comments'), 'none');
862 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
863
864 $this->data['games_comments'] = $this->comments->get_games_comments();
865 $this->data['posts_comments'] = $this->comments->get_posts_comments();
866
867 $this->data['noindex'] = 1;
868 $this->data['title'] = lang('seo_comments_title');
869 $this->data['description'] = lang('seo_comments_description');
870 $this->data['page'] = 'user/comments';
871 $this->render('public/templates/general_noads');
872 }
873
874 public function points()
875 {
876 $this->_is_logged_in();
877
878 $this->breadcrumbs->push( lang('site_title'), create_href());
879 $this->breadcrumbs->push( lang('points'), 'none');
880 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
881
882 $this->data['points_info'] = $this->public_model->get_user_points_info();
883
884 $this->data['noindex'] = 1;
885 $this->data['title'] = lang('seo_points_title');
886 $this->data['description'] = lang('seo_points_description');
887 $this->data['page'] = 'user/points';
888 $this->render('public/templates/general_noads');
889 }
890
891 public function achievements()
892 {
893 $this->_is_logged_in();
894
895 $this->breadcrumbs->push( lang('site_title'), create_href());
896 $this->breadcrumbs->push( lang('achievements'), 'none');
897 $this->data['breadcrumbs'] = $this->breadcrumbs->show();
898
899 $this->data['achievements'] = $this->public_model->get_user_achievements();
900
901 $this->data['noindex'] = 1;
902 $this->data['title'] = lang('seo_achievements_title');
903 $this->data['description'] = lang('seo_achievements_description');
904 $this->data['page'] = 'user/achievements';
905 $this->render('public/templates/general_noads');
906 }
907
908 function _get_csrf_nonce()
909 {
910 $this->load->helper('string');
911 $key = random_string('alnum', 8);
912 $value = random_string('alnum', 20);
913 $this->session->set_flashdata('csrfkey', $key);
914 $this->session->set_flashdata('csrfvalue', $value);
915
916 return array($key => $value);
917 }
918
919 function _valid_csrf_nonce()
920 {
921 if ($this->input->post($this->session->flashdata('csrfkey')) !== FALSE &&
922 $this->input->post($this->session->flashdata('csrfkey')) == $this->session->flashdata('csrfvalue'))
923 {
924 return TRUE;
925 }
926 else
927 {
928 return FALSE;
929 }
930 }
931
932 function _is_logged_in()
933 {
934 if (!$this->session->userdata('is_logged_in'))
935 {
936 $this->session->set_flashdata('error', 'Estás intentando acceder a una zona restringida a usuarios, debes <a href="/user/login" class="tipso top" data-tipso="Acceder">identificarte</a> para poder acceder.');
937 redirect('', 'refresh');
938 }
939 /*
940 if (!$this->ion_auth->logged_in())
941 {
942 $this->session->set_flashdata('error', 'Estás intentando acceder a una zona restringida a usuarios, debes <a href="/user/login" class="tipso top" data-tipso="Acceder">identificarte</a> para poder acceder.');
943 redirect('', 'refresh');
944 }
945 */
946 }
947
948 function _initial_login_check()
949 {
950 if ($this->session->userdata('is_logged_in'))
951 {
952 $this->session->set_flashdata('notice', lang('login_successful'));
953 redirect('', 'refresh');
954 }
955 /*
956 if ($this->ion_auth->logged_in())
957 {
958 $this->session->set_flashdata('notice', lang('login_successful'));
959 redirect('', 'refresh');
960 }
961 */
962 }
963
964}