Fixed again that stupid binding issue
[speedfreak] / Server / application / models / user.php
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /*
3  * Users model for creating and manipulating user accounts
4  * 
5  * @author      Artem Daniliants <artem@daniliants.com>
6  * @copyright   (c) 2010 Speed Freak team
7  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
8  */
9  
10 class User_Model extends Model {
11  
12         /*
13          * Initialize class and register user if all parameters are supplied
14          * 
15          * @param string $username Length 3-12
16          * @param string $password Length 6-255 (stored as sha1 hash in database)
17          * @param string $email Valid email address
18          * @return bool Returns True if operation was successfull and exception otherwise
19          */
20     public function __construct($username='', $password='', $email='', $description=''){
21         
22         // load database library into $this->db
23         parent::__construct();
24         
25         if ($username!='' and $password!='' and $email!=''){
26                 if (strlen($username)<3)
27                    throw new Exception('Username too short');
28             elseif (strlen($username)>12)
29                 throw new Exception('Username too long');
30             elseif (strlen($password)<6)
31                throw new Exception('Password too short');
32             elseif (strlen($username)>255)
33                 throw new Exception('Password too long');
34             elseif (valid::email($email) == False)
35                 throw new Exception('Invalid email supplied');
36             elseif ($this->user_exists($username, $email))
37                 throw new Exception('User already exists (login or email matched)');
38                 
39             if ($this->register($username, $password, $email, $description)->valid())
40                 return true;
41             else
42                 return false;
43             
44         }
45     }
46     
47     /*
48      * Register new user
49      * 
50      * @param string $username Length 3-12
51      * @param string $password Length 6-255 (stored as sha1 hash in database)
52      * @param string $email Valid email address
53      * @return bool Returns True if operation was successfull and exception otherwise
54      */
55     private function register($username, $password, $email, $description){
56         // hash password
57         $password = $this->hash($password);
58         
59         // @todo I can't seem to get query working when password binding has '' around it like others
60         if ($this->user_exists($username, $email)==false)
61            return $this->db->query("INSERT into users SET username = '?', password = ?, description='?', last_activity=NOW(), email = '?'",
62                    $username, $password, $description, $email);
63         else
64             return false;
65     }
66     
67     /*
68      * Hash password supplied by user using salt stored in config file
69      * 
70      * @param string $password Password in plain text format
71      * @return string Returns string containing hash generated from password
72      */
73     private function hash($password){
74         return sha1($password.Kohana::config('api.salt'));
75     }
76     
77     /*
78      * Check if user already exists in database
79      * 
80      * @param string $username Username
81      * @param string $email Email address
82      * @return bool Returns True if user exists and false otherwise
83      */
84     private function user_exists($username, $email){
85         if ($this->db->query("SELECT id FROM users WHERE username=? OR email='?'",
86                            $username, $email)->count()>0)
87                return true;
88             else
89                return false;          
90     }
91     
92     
93     public function get_info($username){
94         $result = $this->db->query("SELECT * FROM users WHERE username = ?", $username);
95                 if ($result->count()>0)
96            return $result[0];
97         else
98            return false;
99     }
100     
101     
102     /*
103      * Get user id
104      *
105      * @param string $username Username
106      * @return integer|bool User id if successful or false
107      */
108     public function get_id($username){
109         $result = $this->db->query("SELECT id FROM users WHERE username=?", $username);
110                 if ($result->count()>0)
111            return $result[0]->id;
112         else
113            return false;
114     }
115     
116     /**
117      * List all users found in database
118      * 
119      * @access public
120      * @return boolean|object Returns object containing all users or false
121      */
122     public function list_all_users(){
123         $result = $this->db->query("SELECT * FROM users");
124                 if ($result->count()>0)
125            return $result;
126         else
127            return false;
128     }
129
130     /*
131      * Check if supplied credentials are valid
132      * 
133      * @param string $username Username
134      * @param string $password Password in plain text format
135      * @return bool True if credentials match and false if supplied credentials are invalid
136      */
137     public function login($username, $password){
138         // hash password
139         $password = $this->hash($password);
140         
141         if ($this->db->query("SELECT id FROM users WHERE username = ? AND password = ?",
142                              $username, $password)->count()>0)
143             return true;
144         else
145             return false;
146     }
147  
148 }