Registration is functional
[speedfreak] / Server / application / models / user.php
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /*
3  * API controller for communicating with mobile clients
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=''){
21         
22         // load database library into $this->db
23         parent::__construct();
24         
25         if (isset($username, $password, $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)->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){
56         return $this->db->query('INSERT into users SET username = ?, password = ?, email = ?',
57                    $this->db->escape($username), $this->db->escape($password), $this->db->escape($email));
58     }
59     
60     /*
61      * Check if user already exists in database
62      * 
63      * @param string $username Username
64      * @param string $email Email address
65      * @return bool Returns True if user exists and false otherwise
66      */
67     private function user_exists($username, $email){
68         if ($this->db->query('SELECT id FROM users WHERE username = ? OR email = ?',
69                    $this->db->escape($username), $this->db->escape($email))->count()>0)
70             return true;
71         else
72             return false;
73     }
74  
75 }