user.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. class User implements JsonSerializable{
  3. public $id;
  4. public $cache = array(
  5. 'name'=>null,
  6. 'email'=>null,
  7. 'date_registered'=>null,
  8. 'date_modified'=>null,
  9. 'active'=>null
  10. );
  11. public function __construct($id){
  12. switch(func_num_args()){
  13. // name, email, password
  14. case 3:
  15. $name = func_get_arg(0);
  16. $email= func_get_arg(1);
  17. $salt = md5($name.$email);
  18. $pass = hash_hmac('sha512',func_get_arg(2),$salt);
  19. Bugs::$sql->query("
  20. INSERT INTO users (name,email,password,salt)
  21. VALUES (?,?,?,?)
  22. ",'ssss',$name,$email,$pass,$salt)->execute();
  23. $id = Bugs::$sql->insert_id;
  24. // id
  25. case 1:
  26. $this->id = intval($id);
  27. $cache = Bugs::$sql->query("
  28. SELECT name,
  29. email,
  30. date_registered,
  31. date_modified,
  32. active
  33. FROM users
  34. WHERE id = ?;
  35. ",'i',$this->id)->assoc_result;
  36. if($cache){
  37. foreach($cache as $key => $value){
  38. $this->cache[$key] = $value;
  39. }
  40. }else{
  41. trigger_error("User with id {$id} does not exist");
  42. }
  43. break;
  44. default:
  45. trigger_error("Invalid Arguments");
  46. }
  47. }
  48. public function jsonSerialize(){
  49. return array(
  50. 'id'=> $this->id,
  51. 'name'=> $this->name,
  52. 'email'=> $this->email,
  53. 'date_registered'=> $this->date_registered,
  54. 'date_modified'=> $this->date_modified
  55. );
  56. }
  57. public function __toString(){
  58. return $this->path;
  59. }
  60. public function __set($name,$value){
  61. switch($name){
  62. case 'name':case 'email':
  63. Bugs::$sql->query("
  64. UPDATE users
  65. SET {$name} = ?
  66. WHERE id = ?
  67. ",'si',$value,$this->id)->execute();
  68. break;
  69. case 'active':
  70. $value = $value?1:0;
  71. Bugs::$sql->query("
  72. UPDATE users
  73. SET active = ?
  74. WHERE id = ?
  75. ",'is',$value,$this->id)->execute();
  76. break;
  77. default:
  78. if(isset($this->cache[$name])){
  79. $this->cache[$name] = $value;
  80. }
  81. }
  82. }
  83. public function __get($name){
  84. switch($name){
  85. case 'active':
  86. return $this->cache['active']==1;
  87. break;
  88. case 'date_registered':case 'date_modified':
  89. return strtotime($this->cache[$name]);
  90. break;
  91. case 'activation_code':
  92. return hash_hmac('sha512',$this->name.$this->email.$this->date_registered,md5($this->name.$this->email));
  93. break;
  94. default:
  95. if(isset($this->cache)){
  96. return $this->cache[$name];
  97. }
  98. }
  99. }
  100. public function email($subject,$body){
  101. Bugs::$sql->query("
  102. INSERT INTO emails (u_id,subject,body)
  103. VALUES(?,?,?)
  104. ",'iss',$this->id,$subject,$body)->execute();
  105. }
  106. }
  107. ?>