/public/application/libraries/Rm_user.php

https://gitlab.com/MichelZuniga/neoinvoice · PHP · 253 lines · 219 code · 19 blank · 15 comment · 35 complexity · 88e04e5e7026c64a82312a0e9c3d5c39 MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * USER AUTHENTICATION DATABASE CLASS CodeIgniter Version by RENOWNED MEDIA
  4. * @license http://www.opensource.org/licenses/bsd-license.php
  5. * @author Thomas Hunter of Renowned Media
  6. * @version 1.0.2
  7. * @link http://www.renownedmedia.com
  8. * @todo ANYWHERE YOU SEE mysql_num_rows() OR mysql_fetch_assoc(), this is an error!!!
  9. * RM_USER_TABLE should have at least five columns, an 'id' column (PK, integer, AI, unsigned),
  10. * a 'username' column (string, unique), a password column (string, index),
  11. * a 'created' column (timestamp default 0's), a 'modified' column (timestamp default 0's)
  12. */
  13. define('RM_USER_TABLE', 'user');
  14. define('RM_USERNAME_COLUMN', 'username');
  15. define('RM_PASSWORD_COLUMN', 'password');
  16. define('RM_EMAIL_COLUMN', 'email');
  17. define('RM_LOST_PASSWORD_COLUMN', 'lost_password'); # keep blank if you don't want this feature
  18. define('RM_CREATED_COLUMN', 'created');
  19. define('RM_MODIFIED_COLUMN', 'modified');
  20. define('RM_ID_COLUMN', 'id');
  21. define("RM_PASSWORD_SALT", 'SET-PASSWORD-SALT-HERE');
  22. class Rm_user {
  23. public $error_message = "";
  24. public function create($data) {
  25. $CI =& get_instance();
  26. $CI->load->database();
  27. if (!isset($data[RM_USERNAME_COLUMN]) || !isset($data[RM_PASSWORD_COLUMN])) {
  28. return false;
  29. }
  30. $sql = "INSERT INTO " . RM_USER_TABLE . " SET ";
  31. foreach($data AS $column => $value) {
  32. if ($column == RM_PASSWORD_COLUMN) {
  33. $value = $this->password_encrypt($value);
  34. }
  35. $sql .= "$column = " . $CI->db->escape($value) . ", ";
  36. }
  37. $sql .= RM_CREATED_COLUMN . " = NOW(), " . RM_MODIFIED_COLUMN . " = NOW()";
  38. if ($this->runQuery($sql)) {
  39. return mysql_insert_id(); /** @todo replace with CI function */
  40. } else {
  41. return false;
  42. }
  43. }
  44. public function can_create($data) {
  45. /**
  46. * @todo do a test create, check unique keys like username, email, etc.
  47. */
  48. $CI =& get_instance();
  49. $CI->load->database();
  50. $sql = "SELECT COUNT(id) AS count FROM " . RM_USER_TABLE . " WHERE " . RM_USERNAME_COLUMN . " = " . $CI->db->escape($data[RM_USERNAME_COLUMN]) . " LIMIT 1";
  51. $result = $this->runQuery($sql);
  52. $row = $result->row_array();
  53. if ($row['count']) {
  54. $this->error_message = "Username Already Exists";
  55. return FALSE;
  56. }
  57. $sql = "SELECT COUNT(id) AS count FROM " . RM_USER_TABLE . " WHERE " . RM_EMAIL_COLUMN . " = " . $CI->db->escape($data[RM_EMAIL_COLUMN]) . " LIMIT 1";
  58. $result = $this->runQuery($sql);
  59. $row = $result->row_array();
  60. if ($row['count']) {
  61. $this->error_message = "Email Already Exists";
  62. return FALSE;
  63. }
  64. return TRUE;
  65. }
  66. public function modify($user_id, $data) {
  67. $CI =& get_instance();
  68. $CI->load->database();
  69. $sql = "UPDATE " . RM_USER_TABLE . " SET ";
  70. foreach($data AS $column => $value) {
  71. if ($column == RM_PASSWORD_COLUMN) {
  72. $value = $this->password_encrypt($value);
  73. }
  74. $sql .= "$column = " . $CI->db->escape($value) . ", ";
  75. }
  76. $sql .= RM_MODIFIED_COLUMN . " = NOW()";
  77. $sql .= " WHERE " . RM_ID_COLUMN . " = $user_id LIMIT 1";
  78. return $this->runQuery($sql);
  79. }
  80. public function delete($user_id) {
  81. $user_id += 0;
  82. $sql = "DELETE FROM " . RM_USER_TABLE . " WHERE " . RM_ID_COLUMN . " = $user_id LIMIT 1";
  83. return $this->runQuery($sql);
  84. }
  85. public function touch($user_id) {
  86. $user_id += 0;
  87. $sql = "UPDATE " . RM_USER_TABLE . " SET " . RM_MODIFIED_COLUMN . " = NOW() WHERE " . RM_ID_COLUMN . " = $user_id LIMIT 1";
  88. return $this->runQuery($sql);
  89. }
  90. public function get_modified($user_id) {
  91. $user_id += 0;
  92. $sql = "SELECT " . RM_MODIFIED_COLUMN . " FROM " . RM_USER_TABLE . " WHERE " . RM_ID_COLUMN . " = $user_id LIMIT 1";
  93. $result = $this->runQuery($sql);
  94. if ($result->num_rows()) {
  95. $row = mysql_fetch_assoc($result);
  96. return strtotime($row[RM_MODIFIED_COLUMN]);
  97. } else {
  98. return false;
  99. }
  100. }
  101. public function get_created($user_id) {
  102. $user_id += 0;
  103. $sql = "SELECT " . RM_CREATED_COLUMN . " FROM " . RM_USER_TABLE . " WHERE " . RM_ID_COLUMN . " = $user_id LIMIT 1";
  104. $result = $this->runQuery($sql);
  105. if ($result->num_rows()) {
  106. $row = mysql_fetch_assoc($result);
  107. return strtotime($row[RM_CREATED_COLUMN]);
  108. } else {
  109. return false;
  110. }
  111. }
  112. public function auth($username, $password) {
  113. $CI =& get_instance();
  114. $CI->load->database();
  115. $pass = $this->password_encrypt($password);
  116. $sql = "SELECT " . RM_ID_COLUMN . " FROM " . RM_USER_TABLE . " WHERE " . RM_USERNAME_COLUMN . " = " . $CI->db->escape($username) . " AND ( " . RM_PASSWORD_COLUMN . " = '$pass' ";
  117. $test = RM_LOST_PASSWORD_COLUMN;
  118. if (!empty($test) && !empty($password)) {
  119. $sql .= "OR " . RM_LOST_PASSWORD_COLUMN . " = " . $CI->db->escape($password) . "";
  120. }
  121. $sql .= ") LIMIT 1";
  122. $query = $this->runQuery($sql);
  123. if ($query->num_rows() > 0) {
  124. $row = $query->row_array();
  125. $sql = "UPDATE " . RM_USER_TABLE . " SET " . RM_LOST_PASSWORD_COLUMN . " = '', " . RM_MODIFIED_COLUMN . " = NOW() WHERE " . RM_USERNAME_COLUMN . " = '$username' LIMIT 1";
  126. $this->runQuery($sql);
  127. return ($row[RM_ID_COLUMN]);
  128. } else {
  129. return false;
  130. }
  131. }
  132. public function set($user_id, $field, $value = null) {
  133. $CI =& get_instance();
  134. $CI->load->database();
  135. $user_id += 0;
  136. if (is_array($field)) {
  137. return $this->modify($user_id, $field);
  138. } else {
  139. $sql = "UPDATE " . RM_USER_TABLE . " SET $field = " . $CI->db->escape($value) . ", " . RM_MODIFIED_COLUMN . " = NOW() WHERE " . RM_ID_COLUMN . " = '$user_id' LIMIT 1";
  140. return $this->runQuery($sql);
  141. }
  142. }
  143. public function get($user_id, $field) {
  144. $user_id += 0;
  145. if (is_array($field)) {
  146. $sql = "SELECT ";
  147. foreach($field AS $column) {
  148. $sql .= "$column, ";
  149. }
  150. $sql = rtrim($sql,", ");
  151. $sql .= " FROM " . RM_USER_TABLE . " WHERE " . RM_ID_COLUMN . " = '$user_id' LIMIT 1";
  152. $query = $this->runQuery($sql);
  153. if ($query->num_rows() > 0) {
  154. $row = $query->row_array();
  155. return $row;
  156. } else {
  157. return false;
  158. }
  159. } else {
  160. $sql = "SELECT $field FROM " . RM_USER_TABLE . " WHERE " . RM_ID_COLUMN . " = '$user_id' LIMIT 1";
  161. $query = $this->runQuery($sql);
  162. if ($query->num_rows()) {
  163. $row = $query->row_array();
  164. return $row[$field];
  165. } else {
  166. return false;
  167. }
  168. }
  169. }
  170. public function change_password($user_id, $new_password, $old_password = false) {
  171. $user_id += 0;
  172. $pass = $this->password_encrypt($new_password);
  173. $sql = "UPDATE " . RM_USER_TABLE . " SET " . RM_PASSWORD_COLUMN . " = '$pass', " . RM_MODIFIED_COLUMN . " = NOW() WHERE " . RM_ID_COLUMN . " = '$user_id'";
  174. if ($old_password !== false) {
  175. $old_pass = $this->password_encrypt($old_password);
  176. $sql .= " AND " . RM_PASSWORD_COLUMN . " = '$old_pass'";
  177. }
  178. $sql .= " LIMIT 1";
  179. return $this->runQuery($sql);
  180. }
  181. public function get_username_from_id($user_id) {
  182. $user_id += 0;
  183. $sql = "SELECT " . RM_USERNAME_COLUMN . " FROM " . RM_USER_TABLE . " WHERE " . RM_ID_COLUMN . " = '$user_id' LIMIT 1";
  184. $result = $this->runQuery($sql);
  185. if (mysql_num_rows($result)) {
  186. $row = mysql_fetch_assoc($result);
  187. return $row[RM_USERNAME_COLUMN];
  188. } else {
  189. return false;
  190. }
  191. }
  192. public function get_id_from_username($username) {
  193. $CI =& get_instance();
  194. $CI->load->database();
  195. $sql = "SELECT " . RM_ID_COLUMN . " FROM " . RM_USER_TABLE . " WHERE " . RM_USERNAME_COLUMN . " = " . $CI->db->escape($username) . " LIMIT 1";
  196. $result = $this->runQuery($sql);
  197. if (mysql_num_rows($result)) {
  198. $row = mysql_fetch_assoc($result);
  199. return $row[RM_ID_COLUMN];
  200. } else {
  201. return false;
  202. }
  203. }
  204. public function get_id_from_field($field_name, $field_value) {
  205. $CI =& get_instance();
  206. $CI->load->database();
  207. $sql = "SELECT " . RM_ID_COLUMN . " FROM " . RM_USER_TABLE . " WHERE $field_name = " . $CI->db->escape($field_value) . " LIMIT 1";
  208. $result = $this->runQuery($sql);
  209. if ($result->num_rows()) {
  210. $row = $result->row_array();
  211. return $row[RM_ID_COLUMN];
  212. } else {
  213. return false;
  214. }
  215. }
  216. public function generate_pass($len=8) {
  217. $totalChar = $len; // number of chars in the password
  218. $salt = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ0123456789"; // salt to select chars from
  219. srand((double)microtime()*1000000); // start the random generator
  220. $password=""; // set the inital variable
  221. for ($i=0;$i<$totalChar;$i++) // loop and create password
  222. $password = $password . substr ($salt, rand() % strlen($salt), 1);
  223. return $password;
  224. }
  225. private function password_encrypt($password) {
  226. return sha1($password . RM_PASSWORD_SALT); # This could be changed to something like crypt($password) or md5($password . "SALT")
  227. }
  228. private function runQuery($query) {
  229. $CI =& get_instance();
  230. $CI->load->database();
  231. #echo $query;
  232. return $CI->db->query($query);
  233. }
  234. }