/system/codecms/models/users_model.php

https://bitbucket.org/jo_racingdesign/codecms · PHP · 238 lines · 122 code · 84 blank · 32 comment · 16 complexity · 863b664ca06f814f6bb46119fd6484f0 MD5 · raw file

  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * CodeCMS an alternative responsive open source cms made from Philippines.
  4. *
  5. * @package CodeCMS
  6. * @author @jsd
  7. * @copyright Copyright (c) 2013
  8. * @license http://creativecommons.org/licenses/by-sa/3.0/deed.en_US
  9. * @link https://bitbucket.org/jsdecena/codecms
  10. * @since Version 0.1
  11. *
  12. */
  13. class Users_model extends CI_Model {
  14. public $database = 'codecms';
  15. public $posts_table = 'posts';
  16. public $settings_table = 'settings';
  17. public $users_table = 'users';
  18. function __construct() {
  19. // Call the Model constructor
  20. parent::__construct();
  21. }
  22. /* INSERT IDENTITY ON LOGIN */
  23. function insert_identity(){
  24. $data = $this->session->all_userdata();
  25. foreach ($data as $udata) {
  26. $user_identity = array( 'identity' => sha1($udata), 'is_logged_in' => 1, 'last_login' => time($this->session->userdata('last_activity')) );
  27. }
  28. $this->db->get_where('users', array('email' => $this->session->userdata('email')));
  29. $this->db->update('users', $user_identity);
  30. }
  31. /* CHECK IF THE PASSWORD AND EMAIL MATCHED THE RECORD IN THE DB */
  32. function login_allowed(){
  33. //CHECK IN THE DATABASE THE USERNAME AND PASSWORD COMBINATION
  34. $query = $this->db->get_where('users', array( 'email' => $this->input->post('email'), 'password' => sha1($this->input->post('password')) ));
  35. if ( $query->num_rows() == 1 ) :
  36. return TRUE;
  37. else:
  38. return FALSE;
  39. endif;
  40. } // END LOGIN ALLOWED
  41. /* READING THE USERS LIST */
  42. function users_query_list(){
  43. $query = $this->db->get('users');
  44. if ( $query->num_rows() > 0 ) :
  45. return $users_list = $query->result_array();
  46. endif;
  47. }
  48. /* UPDATE THE SPECIFIC USER */
  49. function users_query_specific() {
  50. $query = $this->db->get_where('users', array('users_id' => $this->uri->segment(4,0)));
  51. if ( $query->num_rows() > 0 ) :
  52. $data = $query->result();
  53. return $data;
  54. endif;
  55. }
  56. /* RETRIEVES THE CURRENT USER INFORMATION */
  57. function logged_in() {
  58. $query = $this->db->get_where('users', array('email' => $this->session->userdata('email')));
  59. if($query->num_rows() > 0):
  60. foreach ($query->result_array() as $row) :
  61. $data = $row;
  62. endforeach;
  63. return $data;
  64. endif;
  65. }
  66. /*CHECKING FOR THE CURRENT USER IF LOGGED IN OR NOT*/
  67. function logged_in_check(){
  68. //CHECK IF THE USER IS LOGGED IN
  69. $query = $this->db->get_where('users', array('email' => $this->session->userdata('email'), 'is_logged_in' => 1));
  70. if($query->num_rows() == 1):
  71. return TRUE;
  72. else:
  73. return FALSE;
  74. endif;
  75. }
  76. /*UPDATE THE USER*/
  77. function update_user(){
  78. //CHECK IF WE ARE UPDATING AN ADMIN OR A SUBSCRIBER
  79. if ( $this->session->userdata('role') == 'admin' ) :
  80. $data = array(
  81. 'username' => $this->input->post('username'),
  82. 'first_name' => $this->input->post('first_name'),
  83. 'last_name' => $this->input->post('last_name'),
  84. 'email' => $this->input->post('email'),
  85. 'role' => $this->input->post('role'),
  86. 'about' => $this->input->post('about')
  87. );
  88. else:
  89. $data = array(
  90. 'username' => $this->input->post('username'),
  91. 'first_name' => $this->input->post('first_name'),
  92. 'last_name' => $this->input->post('last_name'),
  93. 'email' => $this->input->post('email'),
  94. 'role' => 'subscriber',
  95. 'about' => $this->input->post('about')
  96. );
  97. endif;
  98. $this->db->where('users_id', $this->input->post('users_id'));
  99. $this->db->update($this->users_table, $data);
  100. return TRUE;
  101. }
  102. /* LOGS OUT A USER */
  103. function logout_now() {
  104. $this->db->set('identity', 0 );
  105. $this->db->set('is_logged_in', 0 );
  106. $this->db->update('users');
  107. $this->session->sess_destroy();
  108. }
  109. /* FORGOT PASSWORD CHECKING FOR THE EXISTING EMAIL OF THE USER. */
  110. function retrieve_password_check(){
  111. $query = $this->db->get_where('users', array('email' => $this->input->post('email')));
  112. if($query->num_rows() > 0) :
  113. //EMAIL EXISTING
  114. return TRUE;
  115. else :
  116. //EMAIL NOT EXISTING
  117. return FALSE;
  118. endif;
  119. }
  120. /* CHECK FOR THE VALID KEY THAT WAS RETURNED FROM THE EMAIL */
  121. function check_valid_keys($key){
  122. $query = $this->db->get_where('users', array('pw_recovery' => $key));
  123. if($query->num_rows() > 0) :
  124. //HOUSTON, WE FOUND A MATCH!
  125. return TRUE;
  126. else :
  127. //HOUSTON, WE HAVE A PROBLEM WITH THE GENERATED KEY!
  128. return FALSE;
  129. endif;
  130. }
  131. /* UPDATE THE USERS PASSWORD CONSIDERING THAT THE KEY FROM THE EMAIL IS VALID. */
  132. function update_new_pw_in_db(){
  133. $query = $this->db->get_where('users', array('pw_recovery' => $this->input->post('key')));
  134. if($query->num_rows() == 1) :
  135. //HOUSTON, WE FOUND A MATCH! LET'S UPDATE THIS USERS NEW PASSWORD
  136. $this->db->set('password', $this->input->post('password') );
  137. $this->db->update('users');
  138. return TRUE;
  139. else :
  140. //HOUSTON, WE HAVE A PROBLEM IN UPDATING THE USER'S PASSWORD
  141. return FALSE;
  142. endif;
  143. }
  144. function delete_user(){
  145. $this->db->where('users_id', $this->uri->segment(4));
  146. $this->db->delete($this->users_table);
  147. return TRUE;
  148. }
  149. function delete_my_account(){
  150. $this->db->where('users_id', $this->input->post('delete_account'));
  151. $this->db->delete($this->users_table);
  152. return TRUE;
  153. }
  154. } //END USERS_MODEL