/mvc/models/account
[return to app]1
<?php
2 /**
3 * Functions required by the account controller.
4 */
5 class accountModel extends model {
6 /**
7 * Table/collection name - only need to change this if you will be operating multiple Vork account instances
8 * on the same database
9 * @var string
10 */
11 protected $_table = 'vorkusers';
12
13 /**
14 * Database type to use
15 * @var string Either sql or mongo
16 */
17 protected $_db;
18
19 /**
20 * Mongo collection object cache
21 * @var MongoCollection
22 */
23 protected $_mongo;
24
25 /**
26 * Sets $this->_db and if it is Mongo then it sets the MongoDB collection
27 */
28 public function __construct() {
29 $this->_db = (!in_array('mongo', config::$modelObjects) ? 'sql' : 'mongo');
30 }
31
32 /**
33 * Switchboard to route to either SQL or Mongo methods
34 *
35 * @param string $name
36 * @param array $args
37 */
38 public function __call($name, array $args) {
39 $name = '_' . $this->_db . ucfirst($name);
40 if ($this->_db == 'mongo' && !$this->_mongo) {
41 $this->_mongo = $this->mongo->selectCollection($this->_table);
42 }
43 return call_user_func_array(array($this, $name), $args);
44 }
45
46 /**
47 * The columns returned must include the data defined in the $_sessionKeys property of the account component
48 *
49 * @param string $email
50 * @param string $pass
51 * @return mixed Data array or Boolean false
52 */
53 protected function _sqlValidateLogin($email, $pass) {
54 $sql = 'select * from ' . $this->_table . ' where email=' . $this->db->cleanString($email)
55 . ' and pass=' . $this->db->cleanString($pass);
56 $res = $this->db->query($sql);
57 return $res->fetch_assoc();
58 }
59 protected function _mongoValidateLogin($email, $pass) {
60 return $this->_mongo->findOne(array('email' => $email, 'pass' => $pass));
61 }
62
63 /**
64 * Returns a users password if a record exists for them
65 *
66 * @param string $email
67 * @return mixed Password string or Boolean false
68 */
69 protected function _sqlRetrievePass($email) {
70 $sql = 'select pass from ' . $this->_table . ' where email=' . $this->db->cleanString($email);
71 $res = $this->db->query($sql);
72 $pass = $res->fetch_row();
73 return ($pass ? current($pass) : false);
74 }
75 protected function _mongoRetrievePass($email) {
76 $row = $this->_mongo->findOne(array('email' => $email), array('pass' => true));
77 return ($row ? $row['pass'] : false);
78 }
79
80 /**
81 * Checks if there is already an account registered to an email
82 *
83 * @param string $email
84 * @return boolean
85 */
86 protected function _sqlAccountExists($email) {
87 $sql = 'select email from ' . $this->_table . ' where email=' . $this->db->cleanString($email);
88 $res = $this->db->query($sql);
89 return (boolean) $res->fetch_row();
90 }
91 protected function _mongoAccountExists($email) {
92 return (boolean) $this->_mongoRetrievePass($email);
93 }
94
95 /**
96 * Adds a user
97 * If using the SQL method and the table does not exist it will get created
98 * @param string $email
99 * @param string $pass
100 * @param array $cols Optional additional data columns
101 * @return int
102 */
103 protected function _sqlAddUser($email, $pass, array $cols = array()) {
104 try {
105 $cols['email'] = $email;
106 $cols['pass'] = $pass;
107 $sqlInsert = array('table' => $this->_table, 'vals' => $this->db->cleanString($cols));
108 $isMysql = (isset($dbParents['mysqli']) || isset($dbParents['mysql']));
109 if (!$isMysql) {
110 $sql = 'select max(_id) from ' . $this->_table;
111 $res = $this->db->query($sql);
112 $_id = (int) current($res->fetch_row());
113 $sqlInsert['vals']['_id'] = ++$_id;
114 }
115 $sql = $this->db->insertSql($sqlInsert);
116 $this->db->query($sql);
117 if ($isMysql) {
118 $_id = $this->db->insert_id;
119 }
120 return $_id;
121 } catch (Exception $e) {
122 $this->_sqlInit();
123 }
124 }
125 protected function _mongoAddUser($email, $pass, array $cols = array()) {
126 if (!isset($cols['_id'])) {
127 $_id = $this->_mongo->find(array(), array('_id' => true))->sort(array('_id' =>
-1))->limit(1)->getNext();
128 $_id = ($_id ? (int) $_id['_id'] : 0);
129 $cols['_id'] = ++$_id;
130 }
131 $this->_mongo->ensureIndex(array('email' => 1), array('unique' => true));
132 $cols['email'] = $email;
133 $cols['pass'] = $pass;
134 $this->_mongo->insert($cols);
135 return $cols['_id'];
136 }
137
138 /**
139 * Update user data
140 *
141 * @param string $_id
142 * @param array $args
143 */
144 protected function _sqlUpdateUser($_id, array $args) {
145 $args = $this->db->cleanString($args);
146 foreach ($args as $key => $val) {
147 $sqlCols[] = $key . '=' . $val;
148 }
149 $sql = 'update ' . $this->_table . ' set ' . implode(',', $sqlCols) . ' where _id=' . (int) $_id;
150 $this->db->query($sql);
151 }
152 protected function _mongoUpdateUser($_id, array $args) {
153 $args['_id'] = $_id;
154 $this->_mongo->save($args);
155 }
156
157 /**
158 * Sets the initial login date of a user
159 * @param string $_id
160 */
161 protected function _sqlSetInitialLogin($_id) {
162 $sql = 'update ' . $this->_table . ' set initiallogin=now() where _id=' . (int) $_id;
163 $this->db->query($sql);
164 }
165 protected function _mongoSetInitialLogin($_id) {
166 $update = array('$set' => array('initiallogin' => date('c'), 'initialLoginTs' => time()));
167 $this->_mongo->update(compact('_id'), $update);
168 }
169
170 /**
171 * Creates the SQL database table with the minimal required columns (_id, name, email, pass)
172 */
173 protected function _sqlInit() {
174 $isMysql = (isset($dbParents['mysqli']) || isset($dbParents['mysql']));
175 $sql = 'create table ' . $this->_table . ' (
176 _id mediumint(7) unsigned not null primary key';
177 if ($isMysql) {
178 $sql .= ' auto_increment';
179 }
180 $sql .= ', name varchar(60) not null, email varchar(60) not null unique, pass varchar(16) not null'
181 . ', initiallogin datetime null)';
182 $this->db->query($sql);
183 }
184 }