/system/library/user.php

https://github.com/joshbatchelor/Dingo-Framework · PHP · 611 lines · 397 code · 103 blank · 111 comment · 26 complexity · 927261179ec5c63b11a0d8ecd95de524 MD5 · raw file

  1. <?php if(!defined('DINGO')){die('External Access to File Denied');}
  2. /**
  3. * User Authentication Library For Dingo Framework
  4. *
  5. * @author Evan Byrne
  6. * @copyright 2008 - 2010
  7. * @project page http://www.dingoframework.com
  8. * @docs http://www.dingoframework.com/docs/user-library
  9. */
  10. class user
  11. {
  12. public static $table;
  13. public static $types = array();
  14. public static $_id;
  15. public static $_email;
  16. public static $_username;
  17. public static $_password;
  18. public static $_type;
  19. public static $_data;
  20. public static $_valid = FALSE;
  21. // Valid
  22. // ---------------------------------------------------------------------------
  23. public static function valid()
  24. {
  25. return self::$_valid;
  26. }
  27. // Is Type
  28. // ---------------------------------------------------------------------------
  29. public static function is_type($t)
  30. {
  31. if(self::$_valid OR $t == 'banned')
  32. {
  33. // If type of user is equal to or greater than specified return TRUE
  34. return(self::$types[self::$_type] >= self::$types[$t]);
  35. }
  36. else
  37. {
  38. // If not a valid user return FALSE
  39. return FALSE;
  40. }
  41. }
  42. // Banned
  43. // ---------------------------------------------------------------------------
  44. public static function banned()
  45. {
  46. // Return TRUE is banned, FALSE otherwise
  47. return(self::$types[self::$_type] === self::$types['banned']);
  48. }
  49. // Check
  50. // ---------------------------------------------------------------------------
  51. public static function check($i,$password)
  52. {
  53. $valid = FALSE;
  54. // Get information about current user
  55. if($i AND $password)
  56. {
  57. // Find by ID
  58. if(preg_match('/^([0-9]+)$/',$i))
  59. {
  60. $user = self::$table->select('*')
  61. ->where('id','=',$i)
  62. ->clause('AND')
  63. ->where('password','=',$password)
  64. ->limit(1)
  65. ->execute();
  66. }
  67. // Find by Username
  68. elseif(preg_match('/^([\-_ a-z0-9]+)$/is',$i))
  69. {
  70. $user = self::$table->select('*')
  71. ->where('username','=',$i)
  72. ->clause('AND')
  73. ->where('password','=',$password)
  74. ->limit(1)
  75. ->execute();
  76. }
  77. // Find by E-mail
  78. else
  79. {
  80. $user = self::$table->select('*')
  81. ->where('email','=',$i)
  82. ->clause('AND')
  83. ->where('password','=',$password)
  84. ->limit(1)
  85. ->execute();
  86. }
  87. // If valid login credentials
  88. if(!empty($user[0]))
  89. {
  90. // If not banned, mark as valid
  91. if($user[0]->type != 'banned')
  92. {
  93. $valid = TRUE;
  94. }
  95. }
  96. }
  97. return $valid;
  98. }
  99. // Get
  100. // ---------------------------------------------------------------------------
  101. public static function get($i)
  102. {
  103. // Find by ID
  104. if(preg_match('/^([0-9]+)$/',$i))
  105. {
  106. $user = self::$table->select('*')
  107. ->where('id','=',$i)
  108. ->limit(1)
  109. ->execute();
  110. }
  111. // Find by Username
  112. elseif(preg_match('/^([\-_ a-z0-9]+)$/is',$i))
  113. {
  114. $user = self::$table->select('*')
  115. ->where('username','=',$i)
  116. ->limit(1)
  117. ->execute();
  118. }
  119. // Find by E-mail
  120. else
  121. {
  122. $user = self::$table->select('*')
  123. ->where('email','=',$i)
  124. ->limit(1)
  125. ->execute();
  126. }
  127. // If user is found
  128. if(!empty($user[0]))
  129. {
  130. $user[0]->data = json_decode($user[0]->data,true);
  131. return $user[0];
  132. }
  133. // Otherwise return FALSE
  134. else
  135. {
  136. return FALSE;
  137. }
  138. }
  139. // Log In
  140. // ---------------------------------------------------------------------------
  141. public static function login($i,$password)
  142. {
  143. self::$_valid = FALSE;
  144. // Try to log in
  145. if($i AND $password)
  146. {
  147. $password = self::hash($password);
  148. // Find by ID
  149. if(preg_match('/^([0-9]+)$/',$i))
  150. {
  151. $user = self::$table->select('*')
  152. ->where('id','=',$i)
  153. ->clause('AND')
  154. ->where('password','=',$password)
  155. ->limit(1)
  156. ->execute();
  157. }
  158. // Find by Username
  159. elseif(preg_match('/^([\-_ a-z0-9]+)$/is',$i))
  160. {
  161. $user = self::$table->select('*')
  162. ->where('username','=',$i)
  163. ->clause('AND')
  164. ->where('password','=',$password)
  165. ->limit(1)
  166. ->execute();
  167. }
  168. // Find by E-mail
  169. else
  170. {
  171. $user = self::$table->select('*')
  172. ->where('email','=',$i)
  173. ->clause('AND')
  174. ->where('password','=',$password)
  175. ->limit(1)
  176. ->execute();
  177. }
  178. // If valid login credentials
  179. if(!empty($user[0]))
  180. {
  181. $user = $user[0];
  182. self::$_id = $user->id;
  183. self::$_email = $user->email;
  184. self::$_username = $user->username;
  185. self::$_password = $user->password;
  186. self::$_type = $user->type;
  187. self::$_data = $user->data;
  188. // If not banned, mark as valid
  189. if(self::$_type != 'banned')
  190. {
  191. self::$_valid = TRUE;
  192. session::set('user_email',self::$_email);
  193. session::set('user_password',self::$_password);
  194. }
  195. }
  196. return self::$_valid;
  197. }
  198. }
  199. // Log Out
  200. // ---------------------------------------------------------------------------
  201. public static function logout()
  202. {
  203. session::delete('user_email');
  204. session::delete('user_password');
  205. self::$_valid = FALSE;
  206. }
  207. // Create
  208. // ---------------------------------------------------------------------------
  209. public static function create($user)
  210. {
  211. // Make sure data key is set to prevent JSON errors
  212. if(!isset($user['data']))
  213. {
  214. $user['data'] = array();
  215. }
  216. $user['data'] = json_encode($user['data']);
  217. $user['password'] = self::hash($user['password']);
  218. return self::$table->insert($user);
  219. }
  220. // Update
  221. // ---------------------------------------------------------------------------
  222. public static function update($i=FALSE)
  223. {
  224. // Defaults to current user ID
  225. if(!$i)
  226. {
  227. $i = self::$_id;
  228. }
  229. return new user_update($i,self::$table);
  230. }
  231. // Delete
  232. // ---------------------------------------------------------------------------
  233. public static function delete($i)
  234. {
  235. // Find by ID
  236. if(preg_match('/^([0-9]+)$/',$i))
  237. {
  238. self::$table->delete('id','=',$i);
  239. }
  240. // Find by Username
  241. elseif(preg_match('/^([\-_ a-z0-9]+)$/is',$i))
  242. {
  243. self::$table->delete('username','=',$i);
  244. }
  245. // Find by E-mail
  246. else
  247. {
  248. self::$table->delete('email','=',$i);
  249. }
  250. }
  251. // Ban
  252. // ---------------------------------------------------------------------------
  253. public static function ban($i)
  254. {
  255. // Find by ID
  256. if(preg_match('/^([0-9]+)$/',$i))
  257. {
  258. self::$table->update(array('type'=>'banned'))
  259. ->where('id','=',$i)
  260. ->execute();
  261. }
  262. // Find by Username
  263. elseif(preg_match('/^([\-_ a-z0-9]+)$/is',$i))
  264. {
  265. self::$table->update(array('type'=>'banned'))
  266. ->where('username','=',$i)
  267. ->execute();
  268. }
  269. // Find by E-mail
  270. else
  271. {
  272. self::$table->update(array('type'=>'banned'))
  273. ->where('email','=',$i)
  274. ->execute();
  275. }
  276. }
  277. // Unique
  278. // ---------------------------------------------------------------------------
  279. public static function unique($i)
  280. {
  281. // Find by ID
  282. if(preg_match('/^([0-9]+)$/',$i))
  283. {
  284. $user = self::$table->select('*')
  285. ->where('id','=',$i)
  286. ->limit(1)
  287. ->execute();
  288. }
  289. // Find by Username
  290. elseif(preg_match('/^([\-_ a-z0-9]+)$/i',$i))
  291. {
  292. $user = self::$table->select('*')
  293. ->where('username','=',$i)
  294. ->limit(1)
  295. ->execute();
  296. }
  297. // Find by E-mail
  298. else
  299. {
  300. $user = self::$table->select('*')
  301. ->where('email','=',$i)
  302. ->limit(1)
  303. ->execute();
  304. }
  305. return (!isset($user[0]));
  306. }
  307. // ID
  308. // ---------------------------------------------------------------------------
  309. public static function id()
  310. {
  311. return self::$_id;
  312. }
  313. // E-mail
  314. // ---------------------------------------------------------------------------
  315. public static function email()
  316. {
  317. return self::$_email;
  318. }
  319. // Username
  320. // ---------------------------------------------------------------------------
  321. public static function username()
  322. {
  323. return self::$_username;
  324. }
  325. // Type
  326. // ---------------------------------------------------------------------------
  327. public static function type()
  328. {
  329. return self::$_type;
  330. }
  331. // Password
  332. // ---------------------------------------------------------------------------
  333. public static function password()
  334. {
  335. return self::$_password;
  336. }
  337. // Data
  338. // ---------------------------------------------------------------------------
  339. public static function data($key)
  340. {
  341. return (isset(self::$_data[$key])) ? self::$_data[$key] : NULL;
  342. }
  343. // Hash
  344. // ---------------------------------------------------------------------------
  345. public static function hash($i)
  346. {
  347. return sha1($i);
  348. }
  349. }
  350. /**
  351. * User Authentication Library User Update Class For Dingo Framework
  352. *
  353. * @author Evan Byrne
  354. * @copyright 2008 - 2010
  355. * @project page http://www.dingoframework.com
  356. */
  357. class user_update
  358. {
  359. private $table;
  360. private $exists = TRUE;
  361. public $id;
  362. public $email;
  363. public $username;
  364. public $password;
  365. public $type;
  366. public $data;
  367. // Construct
  368. // ---------------------------------------------------------------------------
  369. public function __construct($i,$table)
  370. {
  371. $this->table = $table;
  372. // Find by ID
  373. if(preg_match('/^([0-9]+)$/',$i))
  374. {
  375. $user = $this->table->select('*')
  376. ->where('id','=',$i)
  377. ->limit(1)
  378. ->execute();
  379. }
  380. // Find by Username
  381. elseif(preg_match('/^([\-_ a-z0-9]+)$/i',$i))
  382. {
  383. $user = $this->table->select('*')
  384. ->where('username','=',$i)
  385. ->limit(1)
  386. ->execute();
  387. }
  388. // Find by E-mail
  389. else
  390. {
  391. $user = $this->table->select('*')
  392. ->where('email','=',$i)
  393. ->limit(1)
  394. ->execute();
  395. }
  396. if(isset($user[0]))
  397. {
  398. $this->id = $user[0]->id;
  399. $this->email = $user[0]->email;
  400. $this->username = $user[0]->username;
  401. $this->password = $user[0]->password;
  402. $this->type = $user[0]->type;
  403. $this->data = json_decode($user[0]->data,true);
  404. }
  405. else
  406. {
  407. $this->exists = FALSE;
  408. }
  409. }
  410. // ID
  411. // ---------------------------------------------------------------------------
  412. public function id($id)
  413. {
  414. $this->id = $id;
  415. return $this;
  416. }
  417. // E-mail
  418. // ---------------------------------------------------------------------------
  419. public function email($email)
  420. {
  421. $this->email = $email;
  422. return $this;
  423. }
  424. // Username
  425. // ---------------------------------------------------------------------------
  426. public function username($username)
  427. {
  428. $this->username = $username;
  429. return $this;
  430. }
  431. // Password
  432. // ---------------------------------------------------------------------------
  433. public function password($password)
  434. {
  435. $this->password = $this->hash($password);
  436. return $this;
  437. }
  438. // Type
  439. // ---------------------------------------------------------------------------
  440. public function type($type)
  441. {
  442. $this->type = $type;
  443. return $this;
  444. }
  445. // Data
  446. // ---------------------------------------------------------------------------
  447. public function data($key,$value)
  448. {
  449. $this->data[$key] = $value;
  450. return $this;
  451. }
  452. // Save
  453. // ---------------------------------------------------------------------------
  454. public function save()
  455. {
  456. $this->table->update(array(
  457. 'id'=>$this->id,
  458. 'email'=>$this->email,
  459. 'username'=>$this->username,
  460. 'password'=>$this->password,
  461. 'type'=>$this->type,
  462. 'data'=>json_encode($this->data)
  463. ))
  464. ->where('id','=',$this->id)
  465. ->execute();
  466. return $this;
  467. }
  468. // Hash
  469. // ---------------------------------------------------------------------------
  470. public function hash($i)
  471. {
  472. return sha1($i);
  473. }
  474. }
  475. // Load config file
  476. load::config('user');
  477. user::$types = config::get('user_types');
  478. // Set database table
  479. user::$table = db(config::get('user_table'),NULL,config::get('user_connection'));
  480. // Get session data
  481. user::$_email = session::get('user_email');
  482. user::$_password = session::get('user_password');
  483. // Get information about current user
  484. if(user::$_email AND user::$_password)
  485. {
  486. $user = user::$table->select('*')
  487. ->where('email','=',user::$_email)
  488. ->clause('AND')
  489. ->where('password','=',user::$_password)
  490. ->limit(1)
  491. ->execute();
  492. // If valid login credentials
  493. if(!empty($user[0]))
  494. {
  495. $user = $user[0];
  496. user::$_id = $user->id;
  497. user::$_email = $user->email;
  498. user::$_username = $user->username;
  499. user::$_password = $user->password;
  500. user::$_type = $user->type;
  501. user::$_data = json_decode($user->data,true);
  502. // If not banned, mark as valid
  503. if(user::$_type != 'banned')
  504. {
  505. user::$_valid = TRUE;
  506. }
  507. }
  508. }