PageRenderTime 63ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/models/redux_auth_model.php

https://github.com/nmani/OpenBama
PHP | 535 lines | 269 code | 112 blank | 154 comment | 46 complexity | e292312e553af382dde65bee1fcf4885 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. * ----------------------------------------------------------------------------
  4. * "THE BEER-WARE LICENSE" :
  5. * <thepixeldeveloper@googlemail.com> wrote this file. As long as you retain this notice you
  6. * can do whatever you want with this stuff. If we meet some day, and you think
  7. * this stuff is worth it, you can buy me a beer in return Mathew Davies
  8. * ----------------------------------------------------------------------------
  9. */
  10. /**
  11. * redux_auth_model
  12. */
  13. class redux_auth_model extends Model {
  14. /**
  15. * Holds an array of tables used in
  16. * redux.
  17. *
  18. * @var string
  19. **/
  20. public $tables = array();
  21. /**
  22. * activation code
  23. *
  24. * @var string
  25. **/
  26. public $activation_code;
  27. /**
  28. * forgotten password key
  29. *
  30. * @var string
  31. **/
  32. public $forgotten_password_code;
  33. /**
  34. * new password
  35. *
  36. * @var string
  37. **/
  38. public $new_password;
  39. /**
  40. * Identity
  41. *
  42. * @var string
  43. **/
  44. public $identity;
  45. public function __construct() {
  46. parent::__construct();
  47. $this->load->config('redux_auth');
  48. $this->tables = $this->config->item('tables');
  49. $this->columns = $this->config->item('columns');
  50. }
  51. /**
  52. * Misc functions
  53. *
  54. * Hash password : Hashes the password to be stored in the database.
  55. * Hash password db : This function takes a password and validates it
  56. * against an entry in the users table.
  57. * Salt : Generates a random salt value.
  58. *
  59. * @author Mathew
  60. */
  61. /**
  62. * Hashes the password to be stored in the database.
  63. *
  64. * @return void
  65. * @author Mathew
  66. **/
  67. public function hash_password($password = false) {
  68. $salt_length = $this->config->item('salt_length');
  69. if ($password === false) {
  70. return false;
  71. }
  72. $salt = $this->salt();
  73. $password = $salt . substr(sha1($salt . $password), 0, -$salt_length);
  74. return $password;
  75. }
  76. /**
  77. * This function takes a password and validates it
  78. * against an entry in the users table.
  79. *
  80. * @return void
  81. * @author Mathew
  82. **/
  83. public function hash_password_db($identity = false, $password = false) {
  84. $identity_column = $this->config->item('identity');
  85. $users_table = $this->tables['users'];
  86. $salt_length = $this->config->item('salt_length');
  87. if ($identity === false || $password === false) {
  88. return false;
  89. }
  90. $query = $this->db->select('password')
  91. ->where($identity_column, $identity)
  92. ->limit(1)
  93. ->get($users_table);
  94. $result = $query->row();
  95. if ($query->num_rows() !== 1) {
  96. return false;
  97. }
  98. $salt = substr($result->password, 0, $salt_length);
  99. $password = $salt . substr(sha1($salt . $password), 0, -$salt_length);
  100. return $password;
  101. }
  102. /**
  103. * Generates a random salt value.
  104. *
  105. * @return void
  106. * @author Mathew
  107. **/
  108. public function salt() {
  109. return substr(md5(uniqid(rand(), true)), 0, $this->config->item('salt_length'));
  110. }
  111. /**
  112. * Activation functions
  113. *
  114. * Activate : Validates and removes activation code.
  115. * Deactivae : Updates a users row with an activation code.
  116. *
  117. * @author Mathew
  118. */
  119. /**
  120. * activate
  121. *
  122. * @return void
  123. * @author Mathew
  124. **/
  125. public function activate($code = false) {
  126. $identity_column = $this->config->item('identity');
  127. $users_table = $this->tables['users'];
  128. if ($code === false) {
  129. return false;
  130. }
  131. $query = $this->db->select($identity_column)
  132. ->where('activation_code', $code)
  133. ->limit(1)
  134. ->get($users_table);
  135. $result = $query->row();
  136. if ($query->num_rows() !== 1) {
  137. return false;
  138. }
  139. $identity = $result->{$identity_column};
  140. $data = array('activation_code' => '');
  141. $this->db->update($users_table, $data, array($identity_column => $identity));
  142. return ($this->db->affected_rows() == 1) ? true : false;
  143. }
  144. /**
  145. * Deactivate
  146. *
  147. * @return void
  148. * @author Mathew
  149. **/
  150. public function deactivate($username = false) {
  151. $users_table = $this->tables['users'];
  152. if ($username === false) {
  153. return false;
  154. }
  155. $activation_code = sha1(md5(microtime()));
  156. $this->activation_code = $activation_code;
  157. $data = array('activation_code' => $activation_code);
  158. $this->db->update($users_table, $data, array('username' => $username));
  159. return ($this->db->affected_rows() == 1) ? true : false;
  160. }
  161. /**
  162. * change password
  163. *
  164. * @return void
  165. * @author Mathew
  166. **/
  167. public function change_password($identity = false, $old = false, $new = false) {
  168. $identity_column = $this->config->item('identity');
  169. $users_table = $this->tables['users'];
  170. if ($identity === false || $old === false || $new === false) {
  171. return false;
  172. }
  173. $query = $this->db->select('password')
  174. ->where($identity_column, $identity)
  175. ->limit(1)
  176. ->get($users_table);
  177. $result = $query->row();
  178. $db_password = $result->password;
  179. $old = $this->hash_password_db($identity, $old);
  180. $new = $this->hash_password($new);
  181. if ($db_password === $old) {
  182. $data = array('password' => $new);
  183. $this->db->update($users_table, $data, array($identity_column => $identity));
  184. return ($this->db->affected_rows() == 1) ? true : false;
  185. }
  186. return false;
  187. }
  188. /**
  189. * Checks username.
  190. *
  191. * @return void
  192. * @author Mathew
  193. **/
  194. public function username_check($username = false) {
  195. $users_table = $this->tables['users'];
  196. if ($username === false) {
  197. return false;
  198. }
  199. $query = $this->db->select('id')
  200. ->where('username', $username)
  201. ->limit(1)
  202. ->get($users_table);
  203. if ($query->num_rows() == 1) {
  204. return true;
  205. }
  206. return false;
  207. }
  208. /**
  209. * Checks email.
  210. *
  211. * @return void
  212. * @author Mathew
  213. **/
  214. public function email_check($email = false) {
  215. $users_table = $this->tables['users'];
  216. if ($email === false) {
  217. return false;
  218. }
  219. $query = $this->db->select('id')
  220. ->where('email', $email)
  221. ->limit(1)
  222. ->get($users_table);
  223. if ($query->num_rows() == 1) {
  224. return true;
  225. }
  226. return false;
  227. }
  228. /**
  229. * Identity check
  230. *
  231. * @return void
  232. * @author Mathew
  233. **/
  234. protected function identity_check($identity = false) {
  235. $identity_column = $this->config->item('identity');
  236. $users_table = $this->tables['users'];
  237. if ($identity === false) {
  238. return false;
  239. }
  240. $query = $this->db->select('id')
  241. ->where($identity_column, $identity)
  242. ->limit(1)
  243. ->get($users_table);
  244. if ($query->num_rows() == 1) {
  245. return true;
  246. }
  247. return false;
  248. }
  249. /**
  250. * Insert a forgotten password key.
  251. *
  252. * @return void
  253. * @author Mathew
  254. **/
  255. public function forgotten_password($email = false) {
  256. $users_table = $this->tables['users'];
  257. if ($email === false) {
  258. return false;
  259. }
  260. $query = $this->db->select('forgotten_password_code')
  261. ->where('email', $email)
  262. ->limit(1)
  263. ->get($users_table);
  264. $result = $query->row();
  265. $code = $result->forgotten_password_code;
  266. if (empty($code)) {
  267. $key = $this->hash_password(microtime().$email);
  268. $this->forgotten_password_code = $key;
  269. $data = array('forgotten_password_code' => $key);
  270. $this->db->update($users_table, $data, array('email' => $email));
  271. return ($this->db->affected_rows() == 1) ? true : false;
  272. }
  273. else {
  274. return false;
  275. }
  276. }
  277. /**
  278. * undocumented function
  279. *
  280. * @return void
  281. * @author Mathew
  282. **/
  283. public function forgotten_password_complete($code = false) {
  284. $users_table = $this->tables['users'];
  285. $identity_column = $this->config->item('identity');
  286. if ($code === false) {
  287. return false;
  288. }
  289. $query = $this->db->select('id')
  290. ->where('forgotten_password_code', $code)
  291. ->limit(1)
  292. ->get($users_table);
  293. $result = $query->row();
  294. if ($query->num_rows() > 0) {
  295. $salt = $this->salt();
  296. $password = $this->hash_password($salt);
  297. $this->new_password = $salt;
  298. $data = array('password' => $password,
  299. 'forgotten_password_code' => '0');
  300. $this->db->update($users_table, $data, array('forgotten_password_code' => $code));
  301. return true;
  302. }
  303. return false;
  304. }
  305. /**
  306. * profile
  307. *
  308. * @return void
  309. * @author Mathew
  310. **/
  311. public function profile($identity = false) {
  312. $users_table = $this->tables['users'];
  313. $groups_table = $this->tables['groups'];
  314. $meta_table = $this->tables['meta'];
  315. $meta_join = $this->config->item('join');
  316. $identity_column = $this->config->item('identity');
  317. if ($identity === false) {
  318. return false;
  319. }
  320. $this->db->select($users_table.'.id, '.
  321. $users_table.'.username, ' .
  322. $users_table.'.password, '.
  323. $users_table.'.email, '.
  324. $users_table.'.activation_code, '.
  325. $users_table.'.forgotten_password_code , '.
  326. $users_table.'.ip_address, '.
  327. $groups_table.'.name AS `group`');
  328. if (!empty($this->columns)) {
  329. foreach ($this->columns as $value) {
  330. $this->db->select($meta_table.'.'.$value);
  331. }
  332. }
  333. $this->db->from($users_table);
  334. $this->db->join($meta_table, $users_table.'.id = '.$meta_table.'.'.$meta_join, 'left');
  335. $this->db->join($groups_table, $users_table.'.group_id = '.$groups_table.'.id', 'left');
  336. if (strlen($identity) === 40) {
  337. $this->db->where($users_table.'.forgotten_password_code', $identity);
  338. }
  339. else {
  340. $this->db->where($users_table.'.'.$identity_column, $identity);
  341. }
  342. $this->db->limit(1);
  343. $i = $this->db->get();
  344. return ($i->num_rows > 0) ? $i->row() : false;
  345. }
  346. /**
  347. * Basic functionality
  348. *
  349. * Register
  350. * Login
  351. *
  352. * @author Mathew
  353. */
  354. /**
  355. * register
  356. *
  357. * @return void
  358. * @author Mathew
  359. **/
  360. public function register($username = false, $password = false, $email = false) {
  361. $users_table = $this->tables['users'];
  362. $meta_table = $this->tables['meta'];
  363. $groups_table = $this->tables['groups'];
  364. $meta_join = $this->config->item('join');
  365. $additional_columns = $this->config->item('columns');
  366. if ($username === false || $password === false || $email === false) {
  367. return false;
  368. }
  369. // Group ID
  370. $query = $this->db->select('id')->where('name', $this->config->item('default_group'))->get($groups_table);
  371. $result = $query->row();
  372. $group_id = $result->id;
  373. // IP Address
  374. $ip_address = $this->input->ip_address();
  375. $password = $this->hash_password($password);
  376. // Users table.
  377. $data = array('username' => $username,
  378. 'password' => $password,
  379. 'email' => $email,
  380. 'group_id' => $group_id,
  381. 'ip_address' => $ip_address);
  382. $this->db->insert($users_table, $data);
  383. // Meta table.
  384. $id = $this->db->insert_id();
  385. $data = array($meta_join => $id);
  386. if (!empty($additional_columns)) {
  387. foreach ($additional_columns as $input) {
  388. $data[$input] = $this->input->post($input);
  389. }
  390. }
  391. $this->db->insert($meta_table, $data);
  392. return ($this->db->affected_rows() > 0) ? true : false;
  393. }
  394. /**
  395. * login
  396. *
  397. * @return void
  398. * @author Mathew
  399. **/
  400. public function login($identity = false, $password = false) {
  401. $identity_column = $this->config->item('identity');
  402. $users_table = $this->tables['users'];
  403. if ($identity === false || $password === false || $this->identity_check($identity) == false) {
  404. return false;
  405. }
  406. $query = $this->db->select($identity_column.', password, activation_code')
  407. ->where($identity_column, $identity)
  408. ->limit(1)
  409. ->get($users_table);
  410. $result = $query->row();
  411. if ($query->num_rows() == 1) {
  412. $password = $this->hash_password_db($identity, $password);
  413. if (!empty($result->activation_code)) { return false; }
  414. if ($result->password === $password) {
  415. $this->session->set_userdata($identity_column, $result->{$identity_column});
  416. return true;
  417. }
  418. }
  419. return false;
  420. }
  421. }
  422. /* End of file redux_auth_model.php */
  423. /* Location: ./system/application/models/redux_auth_model.php */