user.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. 'password'=>null,
  11. 'salt'=> null,
  12. 'admin'=> null
  13. );
  14. public function __construct($id){
  15. switch(func_num_args()){
  16. // name, email, password
  17. case 3:
  18. $name = func_get_arg(0);
  19. $email= func_get_arg(1);
  20. $this->cache['salt'] = md5($name.$email);
  21. $this->cache['password'] = $this->hash(func_get_arg(2));
  22. Bugs::$sql->query("
  23. INSERT INTO users (name,email,password,salt)
  24. VALUES (?,?,?,?)
  25. ",'ssss',$name,$email,$this->password,$this->salt)->execute();
  26. $id = Bugs::$sql->insert_id;
  27. if($id === 0){
  28. trigger_error("Failed to create user with name {$name}.");
  29. }
  30. // id
  31. case 1:
  32. $this->id = intval($id);
  33. $cache = Bugs::$sql->query("
  34. SELECT name,
  35. email,
  36. date_registered,
  37. date_modified,
  38. active,
  39. password,
  40. salt
  41. FROM users
  42. WHERE id = ?;
  43. ",'i',$this->id)->assoc_result;
  44. if($cache){
  45. foreach($cache as $key => $value){
  46. $this->cache[$key] = $value;
  47. }
  48. }else{
  49. trigger_error("User with id {$id} does not exist");
  50. }
  51. break;
  52. default:
  53. trigger_error("Invalid Arguments");
  54. }
  55. }
  56. public function jsonSerialize(){
  57. return array(
  58. 'id'=> $this->id,
  59. 'name'=> $this->name,
  60. 'email'=> $this->email,
  61. 'admin'=> $this->admin,
  62. 'date_registered'=> $this->date_registered,
  63. 'date_modified'=> $this->date_modified
  64. );
  65. }
  66. public function __toString(){
  67. return $this->name;
  68. }
  69. public function __set($name,$value){
  70. switch($name){
  71. case 'name':case 'email':
  72. Bugs::$sql->query("
  73. UPDATE users
  74. SET {$name} = ?
  75. WHERE id = ?
  76. ",'si',$value,$this->id)->execute();
  77. $this->cache[$name] = $value;
  78. break;
  79. case 'active':
  80. $value = $value?1:0;
  81. Bugs::$sql->query("
  82. UPDATE users
  83. SET active = ?
  84. WHERE id = ?
  85. ",'is',$value,$this->id)->execute();
  86. $this->cache['active'] = $value;
  87. break;
  88. default:
  89. if(isset($this->cache[$name])){
  90. $this->cache[$name] = $value;
  91. }
  92. }
  93. }
  94. public function __get($name){
  95. switch($name){
  96. case 'active':
  97. return $this->cache['active']==1;
  98. break;
  99. case 'date_registered':case 'date_modified':
  100. return strtotime($this->cache[$name]);
  101. break;
  102. case 'activation_code':
  103. return hash_hmac('sha512',$this->name.$this->email.$this->date_registered,md5($this->name.$this->email));
  104. break;
  105. case 'login_key':
  106. return hash_hmac('sha512',date('c'),md5($this->date_registered));
  107. break;
  108. case 'sessions':
  109. return Bugs::$sql->query("
  110. SELECT id,
  111. ip,
  112. info
  113. FROM sessions
  114. where u_id = ?
  115. ",'i',$this->id)->assoc_results;
  116. break;
  117. case 'permissions':
  118. $perms = array();
  119. $res = Bugs::$sql->query("
  120. SELECT p.name
  121. FROM r_permission_user r
  122. JOIN permissions p
  123. ON p.id = r.per_id
  124. WHERE r.u_id = ?
  125. ",'i',$this->id)->assoc_results;
  126. foreach($res as $row){
  127. array_push($perms,$row['name']);
  128. }
  129. return $perms;
  130. break;
  131. case 'admin':
  132. if(is_null($this->cache['admin'])){
  133. $this->cache['admin'] = $this->permission('*');
  134. }
  135. return $this->cache['admin'];
  136. break;
  137. case 'project_ids':
  138. return array_column(
  139. Bugs::$sql->query("
  140. SELECT p.id
  141. FROM projects p
  142. JOIN statuses s
  143. ON s.id = p.s_id
  144. WHERE p.u_id = ?
  145. AND s.open = 1
  146. ",'i',$this->id)->assoc_results,
  147. 'id'
  148. );
  149. break;
  150. case 'closed_project_ids':
  151. return array_column(
  152. Bugs::$sql->query("
  153. SELECT p.id
  154. FROM projects p
  155. JOIN statuses s
  156. ON s.id = p.s_id
  157. WHERE p.u_id = ?
  158. AND s.open = 0
  159. ",'i',$this->id)->assoc_results,
  160. 'id'
  161. );
  162. break;
  163. case 'projects':
  164. $projects = array();
  165. foreach($this->project_ids as $id){
  166. array_push($projects,Bugs::project($id));
  167. }
  168. return $projects;
  169. break;
  170. case 'closed_projects':
  171. $projects = array();
  172. foreach($this->closed_project_ids as $id){
  173. array_push($projects,Bugs::project($id));
  174. }
  175. return $projects;
  176. break;
  177. case 'issue_ids':
  178. return array_column(
  179. Bugs::$sql->query("
  180. SELECT i.id
  181. FROM issues i
  182. JOIN statuses s
  183. ON s.id = i.s_id
  184. WHERE i.u_id = ?
  185. AND s.open = 1
  186. ",'i',$this->id)->assoc_results,
  187. 'id'
  188. );
  189. break;
  190. case 'closed_issue_ids':
  191. return array_column(
  192. Bugs::$sql->query("
  193. SELECT i.id
  194. FROM issues i
  195. JOIN statuses s
  196. ON s.id = i.s_id
  197. WHERE i.u_id = ?
  198. AND s.open = 0
  199. ",'i',$this->id)->assoc_results,
  200. 'id'
  201. );
  202. break;
  203. case 'issues':
  204. $issues = array();
  205. foreach($this->issue_ids as $id){
  206. array_push($issues,Bugs::issue($id));
  207. }
  208. return $issues;
  209. break;
  210. case 'closed_issues':
  211. $issues = array();
  212. foreach($this->closed_issue_ids as $id){
  213. array_push($issues,Bugs::issue($id));
  214. }
  215. return $issues;
  216. break;
  217. default:
  218. if(isset($this->cache)){
  219. return $this->cache[$name];
  220. }
  221. }
  222. }
  223. public function email($subject,$body){
  224. Bugs::$sql->query("
  225. INSERT INTO emails (u_id,subject,body)
  226. VALUES(?,?,?)
  227. ",'iss',$this->id,$subject,$body)->execute();
  228. }
  229. public function hash($str){
  230. return hash_hmac('sha512',$str,$this->salt);
  231. }
  232. public function permission($permission){
  233. return Bugs::$sql->query("
  234. SELECT count(1) count
  235. FROM r_permission_user r
  236. JOIN permissions p
  237. ON p.id = r.per_id
  238. AND p.name IN (?,'*')
  239. WHERE r.u_id = ?
  240. ",'si',$permission,$this->id)->assoc_result['count']!==0;
  241. }
  242. }
  243. ?>