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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
<?php
class UserService {
var $db;
var $fields = array(
'primary' => 'uId',
'username' => 'username',
'password' => 'password');
var $profileurl;
var $tablename;
var $sessionkey;
var $cookiekey;
var $cookietime = 1209600; // 2 weeks
function &getInstance(&$db) {
static $instance;
if (!isset($instance))
$instance =& new UserService($db);
return $instance;
}
function UserService(& $db) {
$this->db =& $db;
$this->tablename = $GLOBALS['tableprefix'] .'users';
$this->sessionkey = INSTALLATION_ID.'-currentuserid';
$this->cookiekey = INSTALLATION_ID.'-login';
$this->profileurl = createURL('profile', '%2$s');
$this->updateSessionStability();
}
function _checkdns($host) {
if (function_exists('checkdnsrr')) {
return checkdnsrr($host);
} else {
return $this->_checkdnsrr($host);
}
}
function _checkdnsrr($host, $type = "MX") {
if(!empty($host)) {
@exec("nslookup -type=$type $host", $output);
while(list($k, $line) = each($output)) {
if(eregi("^$host", $line)) {
return true;
}
}
return false;
}
}
function _getuser($fieldname, $value) {
$query = 'SELECT * FROM '. $this->getTableName() .' WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"';
if (! ($dbresult =& $this->db->sql_query($query)) ) {
message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
$row =& $this->db->sql_fetchrow($dbresult);
$this->db->sql_freeresult($dbresult);
if ($row) {
return $row;
} else {
return false;
}
}
function & getUsers($nb=0) {
$query = 'SELECT * FROM '. $this->getTableName() .' ORDER BY `uId` DESC';
if($nb>0) {
$query .= ' LIMIT 0, '.$nb;
}
if (! ($dbresult =& $this->db->sql_query($query)) ) {
message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
while ($row = & $this->db->sql_fetchrow($dbresult)) {
$users[] = $row;
}
$this->db->sql_freeresult($dbresult);
return $users;
}
function & getObjectUsers($nb=0) {
$query = 'SELECT * FROM '. $this->getTableName() .' ORDER BY `uId` DESC';
if($nb>0) {
$query .= ' LIMIT 0, '.$nb;
}
if (! ($dbresult =& $this->db->sql_query($query)) ) {
message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
while ($row = & $this->db->sql_fetchrow($dbresult)) {
$users[] = new User($row[$this->getFieldName('primary')], $row[$this->getFieldName('username')]);
}
$this->db->sql_freeresult($dbresult);
return $users;
}
function _randompassword() {
$seed = (integer) md5(microtime());
mt_srand($seed);
$password = mt_rand(1, 99999999);
$password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12));
return $password;
}
function _updateuser($uId, $fieldname, $value) {
$updates = array ($fieldname => $value);
$sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId);
// Execute the statement.
$this->db->sql_transaction('begin');
if (!($dbresult = & $this->db->sql_query($sql))) {
$this->db->sql_transaction('rollback');
message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db);
return false;
}
$this->db->sql_transaction('commit');
// Everything worked out, so return true.
return true;
}
function getProfileUrl($id, $username) {
return sprintf($this->profileurl, urlencode($id), urlencode($username));
}
function getUserByUsername($username) {
return $this->_getuser($this->getFieldName('username'), $username);
}
function getObjectUserByUsername($username) {
$user = $this->_getuser($this->getFieldName('username'), $username);
if($user != false) {
return new User($user[$this->getFieldName('primary')], $username);
} else {
return NULL;
}
}
/* Takes an numerical "id" or a string "username"
and returns the numerical "id" if the user exists else returns NULL */
function getIdFromUser($user) {
if (is_int($user)) {
return intval($user);
} else {
$objectUser = $this->getObjectUserByUsername($user);
if($objectUser != NULL) {
return $objectUser->getId();
}
}
return NULL;
}
function getUser($id) {
return $this->_getuser($this->getFieldName('primary'), $id);
}
// Momentary useful in order to go to object code
function getObjectUser($id) {
$user = $this->_getuser($this->getFieldName('primary'), $id);
return new User($id, $user[$this->getFieldName('username')]);
}
function isLoggedOn() {
return ($this->getCurrentUserId() !== false);
}
function &getCurrentUser($refresh = FALSE, $newval = NULL) {
static $currentuser;
if (!is_null($newval)) { //internal use only: reset currentuser
$currentuser = $newval;
} else if ($refresh || !isset($currentuser)) {
if ($id = $this->getCurrentUserId()) {
$currentuser = $this->getUser($id);
} else {
$currentuser = null;
}
}
return $currentuser;
}
// Momentary useful in order to go to object code
function getCurrentObjectUser($refresh = FALSE, $newval = NULL) {
static $currentObjectUser;
if (!is_null($newval)) { //internal use only: reset currentuser
$currentObjectUser = $newval;
} else if ($refresh || !isset($currentObjectUser)) {
if ($id = $this->getCurrentUserId()) {
$currentObjectUser = $this->getObjectUser($id);
} else {
$currentObjectUser = null;
}
}
return $currentObjectUser;
}
function existsUserWithUsername($username) {
if($this->getUserByUsername($username) != '') {
return true;
} else {
return false;
}
}
function existsUser($id) {
if($this->getUser($id) != '') {
return true;
} else {
return false;
}
}
/**
* Checks if the given user is an administrator.
* Uses global admin_users property containing admin
* user names
*
* @param integer|array $user User ID or user row from DB
*
* @return boolean True if the user is admin
*/
function isAdmin($user)
{
if (is_numeric($user)) {
$user = $this->getUser($user);
}
if (isset($GLOBALS['admin_users'])
&& in_array($user['username'], $GLOBALS['admin_users'])
) {
return true;
} else {
return false;
}
}
/* return current user id based on session or cookie */
function getCurrentUserId() {
if (isset($_SESSION[$this->getSessionKey()])) {
return $_SESSION[$this->getSessionKey()];
} else if (isset($_COOKIE[$this->getCookieKey()])) {
$cook = split(':', $_COOKIE[$this->getCookieKey()]);
//cookie looks like this: 'id:md5(username+password)'
$query = 'SELECT * FROM '. $this->getTableName() .
' WHERE MD5(CONCAT('.$this->getFieldName('username') .
', '.$this->getFieldName('password') .
')) = \''.$this->db->sql_escape($cook[1]).'\' AND '.
$this->getFieldName('primary'). ' = '. $this->db->sql_escape($cook[0]);
if (! ($dbresult =& $this->db->sql_query($query)) ) {
message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
if ($row = $this->db->sql_fetchrow($dbresult)) {
$_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')];
$this->db->sql_freeresult($dbresult);
return $_SESSION[$this->getSessionKey()];
}
}
return false;
}
function login($username, $password, $remember = FALSE) {
$password = $this->sanitisePassword($password);
$query = 'SELECT '. $this->getFieldName('primary') .' FROM '. $this->getTableName() .' WHERE '. $this->getFieldName('username') .' = "'. $this->db->sql_escape($username) .'" AND '. $this->getFieldName('password') .' = "'. $this->db->sql_escape($password) .'"';
if (! ($dbresult =& $this->db->sql_query($query)) ) {
message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
if ($row =& $this->db->sql_fetchrow($dbresult)) {
$id = $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')];
if ($remember) {
$cookie = $id .':'. md5($username.$password);
setcookie($this->cookiekey, $cookie, time() + $this->cookietime, '/');
}
$this->db->sql_freeresult($dbresult);
return true;
} else {
return false;
}
}
function logout() {
@setcookie($this->getCookiekey(), '', time() - 1, '/');
unset($_COOKIE[$this->getCookiekey()]);
session_unset();
$this->getCurrentUser(TRUE, false);
}
function getWatchlist($uId) {
// Gets the list of user IDs being watched by the given user.
$query = 'SELECT watched FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($uId);
if (! ($dbresult =& $this->db->sql_query($query)) ) {
message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
$arrWatch = array();
if ($this->db->sql_numrows($dbresult) == 0) {
$this->db->sql_freeresult($dbresult);
return $arrWatch;
}
while ($row =& $this->db->sql_fetchrow($dbresult)) {
$arrWatch[] = $row['watched'];
}
$this->db->sql_freeresult($dbresult);
return $arrWatch;
}
function getWatchNames($uId, $watchedby = false) {
// Gets the list of user names being watched by the given user.
// - If $watchedby is false get the list of users that $uId watches
// - If $watchedby is true get the list of users that watch $uId
if ($watchedby) {
$table1 = 'b';
$table2 = 'a';
} else {
$table1 = 'a';
$table2 = 'b';
}
$query = 'SELECT '. $table1 .'.'. $this->getFieldName('username') .' FROM '. $GLOBALS['tableprefix'] .'watched AS W, '. $this->getTableName() .' AS a, '. $this->getTableName() .' AS b WHERE W.watched = a.'. $this->getFieldName('primary') .' AND W.uId = b.'. $this->getFieldName('primary') .' AND '. $table2 .'.'. $this->getFieldName('primary') .' = '. intval($uId) .' ORDER BY '. $table1 .'.'. $this->getFieldName('username');
if (!($dbresult =& $this->db->sql_query($query))) {
message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
$arrWatch = array();
if ($this->db->sql_numrows($dbresult) == 0) {
$this->db->sql_freeresult($dbresult);
return $arrWatch;
}
while ($row =& $this->db->sql_fetchrow($dbresult)) {
$arrWatch[] = $row[$this->getFieldName('username')];
}
$this->db->sql_freeresult($dbresult);
return $arrWatch;
}
function getWatchStatus($watcheduser, $currentuser) {
// Returns true if the current user is watching the given user, and false otherwise.
$query = 'SELECT watched FROM '. $GLOBALS['tableprefix'] .'watched AS W INNER JOIN '. $this->getTableName() .' AS U ON U.'. $this->getFieldName('primary') .' = W.watched WHERE U.'. $this->getFieldName('primary') .' = '. intval($watcheduser) .' AND W.uId = '. intval($currentuser);
if (! ($dbresult =& $this->db->sql_query($query)) ) {
message_die(GENERAL_ERROR, 'Could not get watchstatus', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
$arrWatch = array();
if ($this->db->sql_numrows($dbresult) == 0)
return false;
else
return true;
}
function setWatchStatus($subjectUserID) {
if (!is_numeric($subjectUserID))
return false;
$currentUserID = $this->getCurrentUserId();
$watched = $this->getWatchStatus($subjectUserID, $currentUserID);
if ($watched) {
$sql = 'DELETE FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($currentUserID) .' AND watched = '. intval($subjectUserID);
if (!($dbresult =& $this->db->sql_query($sql))) {
$this->db->sql_transaction('rollback');
message_die(GENERAL_ERROR, 'Could not add user to watch list', '', __LINE__, __FILE__, $sql, $this->db);
return false;
}
} else {
$values = array(
'uId' => intval($currentUserID),
'watched' => intval($subjectUserID)
);
$sql = 'INSERT INTO '. $GLOBALS['tableprefix'] .'watched '. $this->db->sql_build_array('INSERT', $values);
if (!($dbresult =& $this->db->sql_query($sql))) {
$this->db->sql_transaction('rollback');
message_die(GENERAL_ERROR, 'Could not add user to watch list', '', __LINE__, __FILE__, $sql, $this->db);
return false;
}
}
$this->db->sql_transaction('commit');
return true;
}
function addUser($username, $password, $email) {
// Set up the SQL UPDATE statement.
$datetime = gmdate('Y-m-d H:i:s', time());
$password = $this->sanitisePassword($password);
$values = array('username' => $username, 'password' => $password, 'email' => $email, 'uDatetime' => $datetime, 'uModified' => $datetime);
$sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
// Execute the statement.
$this->db->sql_transaction('begin');
if (!($dbresult = & $this->db->sql_query($sql))) {
$this->db->sql_transaction('rollback');
message_die(GENERAL_ERROR, 'Could not insert user', '', __LINE__, __FILE__, $sql, $this->db);
return false;
}
$this->db->sql_transaction('commit');
// Everything worked out, so return true.
return true;
}
function updateUser($uId, $password, $name, $email, $homepage, $uContent) {
if (!is_numeric($uId))
return false;
// Set up the SQL UPDATE statement.
$moddatetime = gmdate('Y-m-d H:i:s', time());
if ($password == '')
$updates = array ('uModified' => $moddatetime, 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent);
else
$updates = array ('uModified' => $moddatetime, 'password' => $this->sanitisePassword($password), 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent);
$sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId);
// Execute the statement.
$this->db->sql_transaction('begin');
if (!($dbresult = & $this->db->sql_query($sql))) {
$this->db->sql_transaction('rollback');
message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db);
return false;
}
$this->db->sql_transaction('commit');
// Everything worked out, so return true.
return true;
}
function getAllUsers ( ) {
$query = 'SELECT * FROM '. $this->getTableName();
if (! ($dbresult =& $this->db->sql_query($query)) ) {
message_die(GENERAL_ERROR, 'Could not get users', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
$rows = array();
while ( $row = $this->db->sql_fetchrow($dbresult) ) {
$rows[] = $row;
}
$this->db->sql_freeresult($dbresult);
return $rows;
}
// Returns an array with admin uIds
function getAdminIds() {
$admins = array();
foreach($GLOBALS['admin_users'] as $adminName) {
if($this->getIdFromUser($adminName) != NULL)
$admins[] = $this->getIdFromUser($adminName);
}
return $admins;
}
function deleteUser($uId) {
$query = 'DELETE FROM '. $this->getTableName() .' WHERE uId = '. intval($uId);
if (!($dbresult = & $this->db->sql_query($query))) {
message_die(GENERAL_ERROR, 'Could not delete user', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
return true;
}
function sanitisePassword($password) {
return sha1(trim($password));
}
function generatePassword($uId) {
if (!is_numeric($uId))
return false;
$password = $this->_randompassword();
if ($this->_updateuser($uId, $this->getFieldName('password'), $this->sanitisePassword($password)))
return $password;
else
return false;
}
function isReserved($username) {
if (in_array($username, $GLOBALS['reservedusers'])) {
return true;
} else {
return false;
}
}
function isValidUsername($username) {
if (strlen($username) < 4) {
return false;
}elseif (strlen($username) > 24) {
// too long usernames are cut by database and may cause bugs when compared
return false;
} elseif (preg_match('/(\W)/', $username) > 0) {
// forbidden non-alphanumeric characters
return false;
}
return true;
}
function isValidEmail($email) {
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$", $email)) {
list($emailUser, $emailDomain) = split("@", $email);
// Check if the email domain has a DNS record
//if ($this->_checkdns($emailDomain)) {
return true;
//}
}
return false;
}
/**
* Sets a session variable.
* Updates it when it is already set.
* This is used to detect if cookies work.
*
* @return void
*
* @see isSessionStable()
*/
function updateSessionStability() {
//find out if we have cookies enabled
if (!isset($_SESSION['sessionStable'])) {
$_SESSION['sessionStable'] = 0;
} else {
$_SESSION['sessionStable'] = 1;
}
}
/**
* Tells you if the session is fresh or old.
* If the session is fresh, it's the first page
* call with that session id. If the session is old,
* we know that cookies (or session persistance) works
*
* @return boolean True if the
*
* @see updateSessionStability()
*/
function isSessionStable() {
return $_SESSION['sessionStable'] == 1;
}
// Properties
function getTableName() { return $this->tablename; }
function setTableName($value) { $this->tablename = $value; }
function getFieldName($field) { return $this->fields[$field]; }
function setFieldName($field, $value) { $this->fields[$field] = $value; }
function getSessionKey() { return $this->sessionkey; }
function setSessionKey($value) { $this->sessionkey = $value; }
function getCookieKey() { return $this->cookiekey; }
function setCookieKey($value) { $this->cookiekey = $value; }
}
/* Defines a user. Rare fields are filled if required. */
class User {
var $id;
var $username;
var $name;
var $email;
var $homepage;
var $content;
var $datetime;
var $isAdmin;
function User($id, $username) {
$this->id = $id;
$this->username = $username;
}
function getId() {
return $this->id;
}
function getUsername() {
return $this->username;
}
function getName() {
// Look for value only if not already set
if(!isset($this->name)) {
$userservice =& ServiceFactory::getServiceInstance('UserService');
$user = $userservice->getUser($this->id);
$this->name = $user['name'];
}
return $this->name;
}
function getEmail() {
// Look for value only if not already set
if(!isset($this->email)) {
$userservice =& ServiceFactory::getServiceInstance('UserService');
$user = $userservice->getUser($this->id);
$this->email = $user['email'];
}
return $this->email;
}
function getHomepage() {
// Look for value only if not already set
if(!isset($this->homepage)) {
$userservice =& ServiceFactory::getServiceInstance('UserService');
$user = $userservice->getUser($this->id);
$this->homepage = $user['homepage'];
}
return $this->homepage;
}
function getContent() {
// Look for value only if not already set
if(!isset($this->content)) {
$userservice =& ServiceFactory::getServiceInstance('UserService');
$user = $userservice->getUser($this->id);
$this->content = $user['uContent'];
}
return $this->content;
}
function getDatetime() {
// Look for value only if not already set
if(!isset($this->content)) {
$userservice =& ServiceFactory::getServiceInstance('UserService');
$user = $userservice->getUser($this->id);
$this->datetime = $user['uDatetime'];
}
return $this->datetime;
}
function isAdmin() {
// Look for value only if not already set
if(!isset($this->isAdmin)) {
$userservice =& ServiceFactory::getServiceInstance('UserService');
$this->isAdmin = $userservice->isAdmin($this->id);
}
return $this->isAdmin;
}
function getNbBookmarks($range = 'public') {
$bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
return $bookmarkservice->countBookmarks($this->getId(), $range);
}
}
?>
|