-
Notifications
You must be signed in to change notification settings - Fork 678
/
class.OwnerMySQLDAO.php
498 lines (454 loc) · 17.4 KB
/
class.OwnerMySQLDAO.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<?php
/**
*
* ThinkUp/webapp/_lib/model/class.OwnerMySQLDAO.php
*
* Copyright (c) 2009-2013 Gina Trapani
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkup.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* Owner Data Access Object
* The data access object for retrieving and saving owners in the ThinkUp database.
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2013 Gina Trapani
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*/
class OwnerMySQLDAO extends PDODAO implements OwnerDAO {
/**
*
* @var str
*/
public static $default_salt = "ab194d42da0dff4a5c01ad33cb4f650a7069178b";
public function getByEmail($email) {
$q = <<<SQL
SELECT
id,
full_name,
email,
is_admin,
last_login,
is_activated,
password_token,
account_status,
failed_logins,
api_key,
email_notification_frequency,
membership_level,
timezone
FROM #prefix#owners AS o
WHERE email = :email;
SQL;
$vars = array(
':email'=>$email
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return $this->getDataRowAsObject($ps, 'Owner');
}
public function getById($id) {
$q = 'SELECT id,full_name,email,is_admin,last_login,is_activated,password_token,' .
'account_status,failed_logins,api_key,email_notification_frequency,timezone ' .
'FROM #prefix#owners AS o WHERE id = :id';
$vars = array(
':id'=>$id
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return $this->getDataRowAsObject($ps, 'Owner');
}
public function getAllOwners() {
$q = " SELECT id, full_name, email, is_admin, is_activated, last_login, email_notification_frequency ";
$q .= "FROM #prefix#owners ORDER BY last_login DESC;";
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q);
return $this->getDataRowsAsObjects($ps, 'Owner');
}
public function getAdmins() {
$q = " SELECT id, full_name, email, is_admin, is_activated, last_login ";
$q .= "FROM #prefix#owners WHERE is_admin = 1 AND is_activated = 1 ORDER BY id";
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q);
$admins = $this->getDataRowsAsObjects($ps, 'Owner');
if (count($admins) == 0) { $admins = null; }
return $admins;
}
public function doesOwnerExist($email) {
$q = " SELECT email FROM #prefix#owners WHERE email=:email";
$vars = array(
':email'=>$email
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return $this->getDataIsReturned($ps);
}
public function getPass($email) {
$q = "SELECT pwd FROM #prefix#owners WHERE email = :email AND is_activated='1' LIMIT 1;";
$vars = array(
':email'=>$email
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
$result = $this->getDataRowAsArray($ps);
if (isset($result['pwd'])) {
return $result['pwd'];
} else {
return false;
}
}
public function getActivationCode($email) {
$q = " SELECT activation_code FROM #prefix#owners WHERE email=:email";
$vars = array(
':email'=>$email
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return $this->getDataRowAsArray($ps);
}
public function activateOwner($email) {
$this->updateActivation($email, true);
}
public function deactivateOwner($email) {
$this->updateActivation($email, false);
}
/**
* Set the value of the is_activated field.
* @param str $email
* @param bool $is_activated
* @return int Count of affected rows
*/
private function updateActivation($email, $is_activated) {
$q = " UPDATE #prefix#owners SET is_activated=:is_activated WHERE email=:email";
$vars = array(
':email'=>$email,
':is_activated'=>(($is_activated)?1:0)
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return $this->getUpdateCount($ps);
}
public function updatePassword($email, $pwd) {
// Generate new unique salt and store it in the database
$salt = $this->generateSalt($email);
$this->updateSalt($email, $salt);
//Hash the password using the new salt
$hashed_password = $this->hashPassword($pwd, $salt);
//Store the new hashed password in the database
$q = " UPDATE #prefix#owners SET pwd=:hashed_password WHERE email=:email";
$vars = array(
':email'=>$email,
':hashed_password'=>$hashed_password
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return $this->getUpdateCount($ps);
}
public function create($email, $pass, $full_name) {
return $this->createOwner($email, $pass, $full_name, false);
}
public function createAdmin($email, $pass, $full_name) {
return $this->createOwner($email, $pass, $full_name, true);
}
private function createOwner($email, $pwd, $full_name, $is_admin) {
if (!$this->doesOwnerExist($email)) {
$activation_code = rand(1000, 9999);
$pwd_salt = $this->generateSalt($email);
$api_key = $this->generateAPIKey();
$hashed_pwd = $this->hashPassword($pwd, $pwd_salt);
//By default an owner's timezone is the installation's timezone
$config = Config::getInstance();
$timezone = $config->getValue('timezone');
$q = "INSERT INTO #prefix#owners SET email=:email, pwd=:hashed_pwd, pwd_salt=:pwd_salt, joined=NOW(), ";
$q .= "activation_code=:activation_code, full_name=:full_name, api_key=:api_key, timezone=:timezone";
if ($is_admin) {
$q .= ", is_admin=1";
}
$vars = array(
':email'=>$email,
':hashed_pwd'=>$hashed_pwd,
':pwd_salt'=>$pwd_salt,
':activation_code'=>$activation_code,
':full_name'=>$full_name,
':api_key'=>$api_key,
':timezone'=>$timezone
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return $activation_code;
} else {
return false;
}
}
public function updateLastLogin($email) {
$q = " UPDATE #prefix#owners SET last_login=now() WHERE email=:email";
$vars = array(
':email'=>$email
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return $this->getUpdateCount($ps);
}
public function updatePasswordToken($email, $token) {
$q = "UPDATE #prefix#owners
SET password_token=:token
WHERE email=:email";
$vars = array(
":token" => $token,
":email" => $email
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return $this->getUpdateCount($ps);
}
public function getByPasswordToken($token) {
$q = "SELECT * FROM #prefix#owners WHERE password_token LIKE :token";
$vars = array(':token' => $token . '_%');
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return $this->getDataRowAsObject($ps, 'Owner');
}
public function doesAdminExist() {
$q = "SELECT id FROM #prefix#owners WHERE is_admin = 1";
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q);
return $this->getDataIsReturned($ps);
}
public function promoteToAdmin($email) {
$q = "UPDATE #prefix#owners
SET is_admin=1
WHERE email=:email";
$vars = array(
":email" => $email
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return $this->getUpdateCount($ps);
}
public function incrementFailedLogins($email) {
$q = "UPDATE #prefix#owners
SET failed_logins=failed_logins+1
WHERE email=:email";
$vars = array(
":email" => $email
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return ( $this->getUpdateCount($ps) > 0 )? true : false;
}
public function resetFailedLogins($email) {
$q = "UPDATE #prefix#owners
SET failed_logins=0
WHERE email=:email";
$vars = array(
":email" => $email
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return ( $this->getUpdateCount($ps) > 0 )? true : false;
}
public function setAccountStatus($email, $status) {
$q = "UPDATE #prefix#owners
SET account_status=:account_status
WHERE email=:email";
$vars = array(
":account_status" => $status,
":email" => $email
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return ( $this->getUpdateCount($ps) > 0 )? true : false;
}
public function clearAccountStatus($email) {
return $this->setAccountStatus($email, '');
}
public function setOwnerActive($id, $is_activated) {
$q = "UPDATE #prefix#owners
SET is_activated=:is_activated
WHERE id=:id";
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$stmt = $this->execute($q, array(':is_activated' => $is_activated, ':id' => $id));
return $this->getUpdateCount($stmt);
}
public function setOwnerAdmin($id, $is_admin) {
$q = "UPDATE #prefix#owners
SET is_admin=:is_admin
WHERE id=:id";
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$stmt = $this->execute($q, array(':is_admin' => $is_admin, ':id' => $id));
return $this->getUpdateCount($stmt);
}
public function resetAPIKey($id) {
$q = "UPDATE #prefix#owners SET api_key=:api_key WHERE id=:id";
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$new_api_key = $this->generateAPIKey();
$stmt = $this->execute($q, array(':api_key' => $new_api_key, ':id' => $id));
if ($this->getUpdateCount($stmt) == 0) {
return false;
} else {
return $new_api_key;
}
}
public function setEmailNotificationFrequency($email, $email_notification_frequency) {
$q = "UPDATE #prefix#owners
SET email_notification_frequency=:email_notification_frequency
WHERE email=:email";
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$stmt = $this->execute($q, array(':email_notification_frequency' => $email_notification_frequency,
':email' => $email));
return $this->getUpdateCount($stmt);
}
/**
* Generate a new API KEY - md5 hashed random string
* @return str A generated API Key
*/
private function generateAPIKey() {
return md5(uniqid(mt_rand(), true)); // generate random api key
}
/**
* Generate a unique, random salt by appending the users email to a random number and returning the hash of it
* @param str $email
* @return str Salt
*/
private function generateSalt($email){
return hash('sha256', rand().$email);
}
/**
* Hashes a password with a given salt.
* @param str $password
* @param str $salt
* @param str Hashed password
*/
private function hashPassword($password, $salt) {
return hash('sha256', $password.$salt);
}
/**
* Retrives the salt for a given user
* @param str $email
* @return str Salt
*/
private function getSaltByEmail($email){
$q = "SELECT pwd_salt ";
$q .= "FROM #prefix#owners u ";
$q .= "WHERE u.email = :email";
$vars = array(':email'=>$email);
$ps = $this->execute($q, $vars);
$query = $this->getDataRowAsArray($ps);
return $query['pwd_salt'];
}
/**
* Updates the password salt for a given user
* @param str $email
* @param str $salt
* @return int Number of rows updated
*/
private function updateSalt($email, $salt) {
$q = " UPDATE #prefix#owners SET pwd_salt=:salt WHERE email=:email";
$vars = array(
':email'=>$email,
':salt'=>$salt
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
return $this->getUpdateCount($ps);
}
/**
* DEPRECATED: This method of password-hashing is no longer used. It's still here for backwards compatibility.
* @param str $pwd Password
* @return str MD5-hashed password
*/
private function md5pwd($pwd) {
return md5($pwd);
}
/**
* DEPRECATED: This method of password-hashing is no longer used. It's still here for backwards compatibility.
* @param str $pwd Password
* @return str SHA1-hashed password
*/
private function sha1pwd($pwd) {
return sha1($pwd);
}
/**
* DEPRECATED: This method of password-hashing is no longer used. It's still here for backwards compatibility.
* @param str $pwd
* @return str Salted SHA1 password
*/
private function saltedsha1($pwd) {
return sha1(sha1($pwd.self::$default_salt).self::$default_salt);
}
/**
* DEPRECATED: This method of password-hashing is no longer used. It's still here for backwards compatibility.
* Encrypt password
* @param str $pwd password
* @return str Encrypted password
*/
private function pwdCrypt($pwd) {
return $this->saltedsha1($pwd);
}
/**
* DEPRECATED: This method of password-hashing is no longer used. It's still here for backwards compatibility.
* Check password
* @param str $pwd Password
* @param str $result Result
* @return bool Whether or submitted password matches check
*/
private function pwdCheck($pwd, $result) {
if ($this->saltedsha1($pwd) == $result || $this->sha1pwd($pwd) == $result || $this->md5pwd($pwd) == $result) {
return true;
} else {
return false;
}
}
public function isOwnerAuthorized($email, $password) {
// Get salt from the database
$db_salt = $this->getSaltByEmail($email);
// Get password from the database
$db_password = $this->getPass($email);
if ($db_salt == self::$default_salt) { //using old, default salt
$hashed_pwd = $this->pwdCrypt($password); // Hash the old way
return $this->pwdCheck($password, $db_password); //Check the old way
} else {
$hashed_pwd = $this->hashPassword($password, $db_salt); // Hash the new way
// Check if it matches the password stored in the database
return ($hashed_pwd == $db_password);
}
}
public function getPrivateAPIKey($email) {
$q = "SELECT api_key_private FROM #prefix#owners WHERE email = :email AND is_activated='1' LIMIT 1;";
$vars = array(
':email'=>$email
);
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$ps = $this->execute($q, $vars);
$result = $this->getDataRowAsArray($ps);
if (!isset($result['api_key_private']) || $result['api_key_private'] == '') {
return false;
} else {
return $result['api_key_private'];
}
}
public function isOwnerAuthorizedViaPrivateAPIKey($email, $private_api_key) {
$db_private_api_key = $this->getPrivateAPIKey($email);
return ($db_private_api_key === $private_api_key);
}
public function setTimezone($email, $timezone) {
$q = "UPDATE #prefix#owners
SET timezone=:timezone
WHERE email=:email";
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$stmt = $this->execute($q, array(':timezone' => $timezone, ':email' => $email));
return $this->getUpdateCount($stmt);
}
}