1 <?php defined('SYSPATH') or die('No direct script access.');
3 * Users model for creating and manipulating user accounts
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
10 class User_Model extends Model {
13 * Initialize class and register user if all parameters are supplied
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
20 public function __construct($username='', $password='', $email='', $description=''){
22 // load database library into $this->db
23 parent::__construct();
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)');
39 if ($this->register($username, $password, $email, $description)->valid())
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
55 private function register($username, $password, $email, $description){
57 $password = $this->hash($password);
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='".apiler::e($username)."', password='".apiler::e($password)."', description='".apiler::e($description)."', last_activity=NOW(), email='".apiler::e($email)."'");
67 * Hash password supplied by user using salt stored in config file
69 * @param string $password Password in plain text format
70 * @return string Returns string containing hash generated from password
72 private function hash($password){
73 return sha1($password.Kohana::config('api.salt'));
77 * Check if user already exists in database
79 * @param string $username Username
80 * @param string $email Email address
81 * @return bool Returns True if user exists and false otherwise
83 private function user_exists($username, $email){
84 if ($this->db->query("SELECT id FROM users WHERE username='".apiler::e($username)."' OR email='".apiler::e($email)."'")->count()>0)
91 public function get_info($username){
92 $result = $this->db->query("SELECT * FROM users WHERE username ='".apiler::e($username)."'");
93 if ($result->count()>0)
103 * @param string $username Username
104 * @return integer|bool User id if successful or false
106 public function get_id($username){
107 $result = $this->db->query("SELECT id FROM users WHERE username='".apiler::e($username)."'");
108 if ($result->count()>0)
109 return $result[0]->id;
115 * List all users found in database
118 * @return boolean|object Returns object containing all users or false
120 public function list_all_users(){
121 $result = $this->db->query("SELECT * FROM users");
122 if ($result->count()>0)
129 * Check if supplied credentials are valid
131 * @param string $username Username
132 * @param string $password Password in plain text format
133 * @return bool True if credentials match and false if supplied credentials are invalid
135 public function login($username, $password){
137 $password = $this->hash($password);
139 if ($this->db->query("SELECT id FROM users WHERE username='".apiler::e($username)."' AND password='".apiler::e($password)."'")->count()>0)