User.class.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. @nom: User
  4. @auteur: Idleman (idleman@idleman.fr)
  5. @description: Classe de gestion des utilisateurs
  6. */
  7. class User extends MysqlEntity{
  8. protected $id,$login,$password;
  9. protected $TABLE_NAME = 'user';
  10. protected $CLASS_NAME = 'User';
  11. protected $object_fields =
  12. array(
  13. 'id'=>'key',
  14. 'login'=>'string',
  15. 'password'=>'string'
  16. );
  17. function __construct(){
  18. parent::__construct();
  19. }
  20. function setId($id){
  21. $this->id = $id;
  22. }
  23. function exist($login,$password,$salt=''){
  24. $userManager = new User();
  25. return $userManager->load(array('login'=>$login,'password'=>User::encrypt($password,$salt)));
  26. }
  27. function getToken() {
  28. assert('!empty($this->password)');
  29. assert('!empty($this->login)');
  30. return sha1($this->password.$this->login);
  31. }
  32. function existAuthToken($auth=null){
  33. $result = false;
  34. $userManager = new User();
  35. $users = $userManager->populate('id');
  36. if (empty($auth)) $auth = @$_COOKIE['leedStaySignedIn'];
  37. foreach($users as $user){
  38. if($user->getToken()==$auth) $result = $user;
  39. }
  40. return $result;
  41. }
  42. function setStayConnected() {
  43. ///@TODO: set the current web directory, here and on del
  44. setcookie('leedStaySignedIn', $this->getToken(), time()+31536000);
  45. }
  46. static function delStayConnected() {
  47. setcookie('leedStaySignedIn', '', -1);
  48. }
  49. function getId(){
  50. return $this->id;
  51. }
  52. function getLogin(){
  53. return $this->login;
  54. }
  55. function setLogin($login){
  56. $this->login = $login;
  57. }
  58. function getPassword(){
  59. return $this->password;
  60. }
  61. function setPassword($password,$salt=''){
  62. $this->password = User::encrypt($password,$salt);
  63. }
  64. static function encrypt($password, $salt=''){
  65. return sha1($password.$salt);
  66. }
  67. static function generateSalt() {
  68. return ''.mt_rand().mt_rand();
  69. }
  70. }
  71. ?>