123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- class User implements JsonSerializable{
- public $id;
- public $cache = array(
- 'name'=>null,
- 'email'=>null,
- 'date_registered'=>null,
- 'date_modified'=>null,
- 'active'=>null,
- 'password'=>null,
- 'salt'=> null,
- 'admin'=> null
- );
- public function __construct($id){
- switch(func_num_args()){
- // name, email, password
- case 3:
- $name = func_get_arg(0);
- $email= func_get_arg(1);
- $this->cache['salt'] = md5($name.$email);
- $this->cache['password'] = $this->hash(func_get_arg(2));
- Bugs::$sql->query("
- INSERT INTO users (name,email,password,salt)
- VALUES (?,?,?,?)
- ",'ssss',$name,$email,$this->password,$this->salt)->execute();
- $id = Bugs::$sql->insert_id;
- if($id === 0){
- trigger_error("Failed to create user with name {$name}.");
- }
- // id
- case 1:
- $this->id = intval($id);
- $cache = Bugs::$sql->query("
- SELECT name,
- email,
- date_registered,
- date_modified,
- active,
- password,
- salt
- FROM users
- WHERE id = ?;
- ",'i',$this->id)->assoc_result;
- if($cache){
- foreach($cache as $key => $value){
- $this->cache[$key] = $value;
- }
- }else{
- trigger_error("User with id {$id} does not exist");
- }
- break;
- default:
- trigger_error("Invalid Arguments");
- }
- }
- public function jsonSerialize(){
- return array(
- 'id'=> $this->id,
- 'name'=> $this->name,
- 'email'=> $this->email,
- 'admin'=> $this->admin,
- 'date_registered'=> $this->date_registered,
- 'date_modified'=> $this->date_modified
- );
- }
- public function __toString(){
- return $this->name;
- }
- public function __set($name,$value){
- switch($name){
- case 'name':case 'email':
- Bugs::$sql->query("
- UPDATE users
- SET {$name} = ?
- WHERE id = ?
- ",'si',$value,$this->id)->execute();
- $this->cache[$name] = $value;
- break;
- case 'active':
- $value = $value?1:0;
- Bugs::$sql->query("
- UPDATE users
- SET active = ?
- WHERE id = ?
- ",'is',$value,$this->id)->execute();
- $this->cache['active'] = $value;
- break;
- default:
- if(isset($this->cache[$name])){
- $this->cache[$name] = $value;
- }
- }
- }
- public function __get($name){
- switch($name){
- case 'active':
- return $this->cache['active']==1;
- break;
- case 'date_registered':case 'date_modified':
- return strtotime($this->cache[$name]);
- break;
- case 'activation_code':
- return hash_hmac('sha512',$this->name.$this->email.$this->date_registered,md5($this->name.$this->email));
- break;
- case 'login_key':
- return hash_hmac('sha512',date('c'),md5($this->date_registered));
- break;
- case 'sessions':
- return Bugs::$sql->query("
- SELECT id,
- ip,
- info
- FROM sessions
- where u_id = ?
- ",'i',$this->id)->assoc_results;
- break;
- case 'permissions':
- $perms = array();
- $res = Bugs::$sql->query("
- SELECT p.name
- FROM r_permission_user r
- JOIN permissions p
- ON p.id = r.per_id
- WHERE r.u_id = ?
- ",'i',$this->id)->assoc_results;
- foreach($res as $row){
- array_push($perms,$row['name']);
- }
- return $perms;
- break;
- case 'admin':
- if(is_null($this->cache['admin'])){
- $this->cache['admin'] = $this->permission('*');
- }
- return $this->cache['admin'];
- break;
- case 'project_ids':
- return array_column(
- Bugs::$sql->query("
- SELECT p.id
- FROM projects p
- JOIN statuses s
- ON s.id = p.s_id
- WHERE p.u_id = ?
- AND s.open = 1
- ",'i',$this->id)->assoc_results,
- 'id'
- );
- break;
- case 'closed_project_ids':
- return array_column(
- Bugs::$sql->query("
- SELECT p.id
- FROM projects p
- JOIN statuses s
- ON s.id = p.s_id
- WHERE p.u_id = ?
- AND s.open = 0
- ",'i',$this->id)->assoc_results,
- 'id'
- );
- break;
- case 'projects':
- $projects = array();
- foreach($this->project_ids as $id){
- array_push($projects,Bugs::project($id));
- }
- return $projects;
- break;
- case 'closed_projects':
- $projects = array();
- foreach($this->closed_project_ids as $id){
- array_push($projects,Bugs::project($id));
- }
- return $projects;
- break;
- case 'issue_ids':
- return array_column(
- Bugs::$sql->query("
- SELECT i.id
- FROM issues i
- JOIN statuses s
- ON s.id = i.s_id
- WHERE i.u_id = ?
- AND s.open = 1
- ",'i',$this->id)->assoc_results,
- 'id'
- );
- break;
- case 'closed_issue_ids':
- return array_column(
- Bugs::$sql->query("
- SELECT i.id
- FROM issues i
- JOIN statuses s
- ON s.id = i.s_id
- WHERE i.u_id = ?
- AND s.open = 0
- ",'i',$this->id)->assoc_results,
- 'id'
- );
- break;
- case 'issues':
- $issues = array();
- foreach($this->issue_ids as $id){
- array_push($issues,Bugs::issue($id));
- }
- return $issues;
- break;
- case 'closed_issues':
- $issues = array();
- foreach($this->closed_issue_ids as $id){
- array_push($issues,Bugs::issue($id));
- }
- return $issues;
- break;
- default:
- if(isset($this->cache)){
- return $this->cache[$name];
- }
- }
- }
- public function email($subject,$body){
- Bugs::$sql->query("
- INSERT INTO emails (u_id,subject,body)
- VALUES(?,?,?)
- ",'iss',$this->id,$subject,$body)->execute();
- }
- public function hash($str){
- return hash_hmac('sha512',$str,$this->salt);
- }
- public function permission($permission){
- return Bugs::$sql->query("
- SELECT count(1) count
- FROM r_permission_user r
- JOIN permissions p
- ON p.id = r.per_id
- AND p.name IN (?,'*')
- WHERE r.u_id = ?
- ",'si',$permission,$this->id)->assoc_result['count']!==0;
- }
- }
- ?>
|