123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- class User implements JsonSerializable{
- public $id;
- public $cache = array(
- 'name'=>null,
- 'email'=>null,
- 'date_registered'=>null,
- 'date_modified'=>null,
- 'active'=>null
- );
- public function __construct($id){
- switch(func_num_args()){
- // name, email, password
- case 3:
- $name = func_get_arg(0);
- $email= func_get_arg(1);
- $salt = md5($name.$email);
- $pass = hash_hmac('sha512',func_get_arg(2),$salt);
- Bugs::$sql->query("
- INSERT INTO users (name,email,password,salt)
- VALUES (?,?,?,?)
- ",'ssss',$name,$email,$pass,$salt)->execute();
- $id = Bugs::$sql->insert_id;
- // id
- case 1:
- $this->id = intval($id);
- $cache = Bugs::$sql->query("
- SELECT name,
- email,
- date_registered,
- date_modified,
- active
- 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,
- 'date_registered'=> $this->date_registered,
- 'date_modified'=> $this->date_modified
- );
- }
- public function __toString(){
- return $this->path;
- }
- 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();
- break;
- case 'active':
- $value = $value?1:0;
- Bugs::$sql->query("
- UPDATE users
- SET active = ?
- WHERE id = ?
- ",'is',$value,$this->id)->execute();
- 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;
- 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();
- }
- }
- ?>
|