5 function &getInstance(&$db) {
7 if (!isset($instance)) {
8 $instance = new UserService($db);
15 'username' => 'username',
16 'password' => 'password'
22 var $cookietime = 1209600; // 2 weeks
24 function UserService(&$db) {
26 $this->tablename = $GLOBALS['tableprefix'] .'users';
27 $this->sessionkey = $GLOBALS['cookieprefix'] .'-currentuserid';
28 $this->cookiekey = $GLOBALS['cookieprefix'] .'-login';
29 $this->profileurl = createURL('profile', '%2$s');
32 function _checkdns($host) {
33 if (function_exists('checkdnsrr')) {
34 return checkdnsrr($host);
36 return $this->_checkdnsrr($host);
40 function _checkdnsrr($host, $type = "MX") {
42 @exec("nslookup -type=$type $host", $output);
43 while(list($k, $line) = each($output)) {
44 if(eregi("^$host", $line)) {
52 function _getuser($fieldname, $value) {
53 $query = 'SELECT * FROM '. $this->getTableName() .' WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"';
55 if (! ($dbresult =& $this->db->sql_query($query)) ) {
56 message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
60 if ($row =& $this->db->sql_fetchrow($dbresult))
66 function _randompassword() {
67 $password = mt_rand(1, 99999999);
68 $password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12));
72 function _updateuser($uId, $fieldname, $value) {
73 $updates = array ($fieldname => $value);
74 $sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId);
76 // Execute the statement.
77 $this->db->sql_transaction('begin');
78 if (!($dbresult = & $this->db->sql_query($sql))) {
79 $this->db->sql_transaction('rollback');
80 message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db);
83 $this->db->sql_transaction('commit');
85 // Everything worked out, so return true.
89 function getProfileUrl($id, $username) {
90 return sprintf($this->profileurl, urlencode($id), urlencode($username));
93 function getUserByUsername($username) {
94 return $this->_getuser($this->getFieldName('username'), $username);
97 function getUser($id) {
98 return $this->_getuser($this->getFieldName('primary'), $id);
101 function isLoggedOn() {
102 return ($this->getCurrentUserId() !== false);
105 function &getCurrentUser($refresh = FALSE, $newval = NULL) {
107 if (!is_null($newval)) //internal use only: reset currentuser
108 $currentuser = $newval;
109 else if ($refresh || !isset($currentuser)) {
110 if ($id = $this->getCurrentUserId())
111 $currentuser = $this->getUser($id);
118 function isAdmin($userid) {
119 return false; //not implemented yet
122 function getCurrentUserId() {
123 if (isset($_SESSION[$this->getSessionKey()])) {
124 return $_SESSION[$this->getSessionKey()];
125 } else if (isset($_COOKIE[$this->getCookieKey()])) {
126 $cook = split(':', $_COOKIE[$this->getCookieKey()]);
127 //cookie looks like this: 'id:md5(username+password)'
128 $query = 'SELECT * FROM '. $this->getTableName() .
129 ' WHERE MD5(CONCAT('.$this->getFieldName('username') .
130 ', '.$this->getFieldName('password') .
131 ')) = \''.$this->db->sql_escape($cook[1]).'\' AND '.
132 $this->getFieldName('primary'). ' = '. $this->db->sql_escape($cook[0]);
134 if (! ($dbresult =& $this->db->sql_query($query)) ) {
135 message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
139 if ($row = $this->db->sql_fetchrow($dbresult)) {
140 $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')];
141 return $_SESSION[$this->getSessionKey()];
147 function login($username, $password, $remember = FALSE, $path = '/') {
148 $password = $this->sanitisePassword($password);
149 $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) .'"';
151 if (! ($dbresult =& $this->db->sql_query($query)) ) {
152 message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
156 if ($row =& $this->db->sql_fetchrow($dbresult)) {
157 $id = $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')];
159 $cookie = $id .':'. md5($username.$password);
160 setcookie($this->cookiekey, $cookie, time() + $this->cookietime, $path);
168 function logout($path = '/') {
169 @setcookie($this->cookiekey, NULL, time() - 1, $path);
170 unset($_COOKIE[$this->cookiekey]);
172 $this->getCurrentUser(TRUE, false);
175 function getWatchlist($uId) {
176 // Gets the list of user IDs being watched by the given user.
177 $query = 'SELECT watched FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($uId);
179 if (! ($dbresult =& $this->db->sql_query($query)) ) {
180 message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db);
185 if ($this->db->sql_numrows($dbresult) == 0)
187 while ($row =& $this->db->sql_fetchrow($dbresult))
188 $arrWatch[] = $row['watched'];
192 function getWatchNames($uId, $watchedby = false) {
193 // Gets the list of user names being watched by the given user.
194 // - If $watchedby is false get the list of users that $uId watches
195 // - If $watchedby is true get the list of users that watch $uId
203 $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');
205 if (!($dbresult =& $this->db->sql_query($query))) {
206 message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db);
211 if ($this->db->sql_numrows($dbresult) == 0) {
214 while ($row =& $this->db->sql_fetchrow($dbresult)) {
215 $arrWatch[] = $row[$this->getFieldName('username')];
220 function getWatchStatus($watcheduser, $currentuser) {
221 // Returns true if the current user is watching the given user, and false otherwise.
222 $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);
224 if (! ($dbresult =& $this->db->sql_query($query)) ) {
225 message_die(GENERAL_ERROR, 'Could not get watchstatus', '', __LINE__, __FILE__, $query, $this->db);
230 if ($this->db->sql_numrows($dbresult) == 0)
236 function setWatchStatus($subjectUserID) {
237 if (!is_numeric($subjectUserID))
240 $currentUserID = $this->getCurrentUserId();
241 $watched = $this->getWatchStatus($subjectUserID, $currentUserID);
244 $sql = 'DELETE FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($currentUserID) .' AND watched = '. intval($subjectUserID);
245 if (!($dbresult =& $this->db->sql_query($sql))) {
246 $this->db->sql_transaction('rollback');
247 message_die(GENERAL_ERROR, 'Could not add user to watch list', '', __LINE__, __FILE__, $sql, $this->db);
252 'uId' => intval($currentUserID),
253 'watched' => intval($subjectUserID)
255 $sql = 'INSERT INTO '. $GLOBALS['tableprefix'] .'watched '. $this->db->sql_build_array('INSERT', $values);
256 if (!($dbresult =& $this->db->sql_query($sql))) {
257 $this->db->sql_transaction('rollback');
258 message_die(GENERAL_ERROR, 'Could not add user to watch list', '', __LINE__, __FILE__, $sql, $this->db);
263 $this->db->sql_transaction('commit');
267 function addUser($username, $password, $email) {
268 // Set up the SQL UPDATE statement.
269 $datetime = gmdate('Y-m-d H:i:s', time());
270 $password = $this->sanitisePassword($password);
271 $values = array('username' => $username, 'password' => $password, 'email' => $email, 'uDatetime' => $datetime, 'uModified' => $datetime);
272 $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
274 // Execute the statement.
275 $this->db->sql_transaction('begin');
276 if (!($dbresult = & $this->db->sql_query($sql))) {
277 $this->db->sql_transaction('rollback');
278 message_die(GENERAL_ERROR, 'Could not insert user', '', __LINE__, __FILE__, $sql, $this->db);
281 $this->db->sql_transaction('commit');
283 // Everything worked out, so return true.
287 function updateUser($uId, $password, $name, $email, $homepage, $uContent) {
288 if (!is_numeric($uId))
291 // Set up the SQL UPDATE statement.
292 $moddatetime = gmdate('Y-m-d H:i:s', time());
294 $updates = array ('uModified' => $moddatetime, 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent);
296 $updates = array ('uModified' => $moddatetime, 'password' => $this->sanitisePassword($password), 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent);
297 $sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId);
299 // Execute the statement.
300 $this->db->sql_transaction('begin');
301 if (!($dbresult = & $this->db->sql_query($sql))) {
302 $this->db->sql_transaction('rollback');
303 message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db);
306 $this->db->sql_transaction('commit');
308 // Everything worked out, so return true.
312 function sanitisePassword($password) {
313 return sha1(trim($password));
316 function generatePassword($uId) {
317 if (!is_numeric($uId))
320 $password = $this->_randompassword();
322 if ($this->_updateuser($uId, $this->getFieldName('password'), $this->sanitisePassword($password)))
328 function isReserved($username) {
329 if (in_array($username, $GLOBALS['reservedusers'])) {
336 function isValidEmail($email) {
337 if (preg_match("/^((?:(?:(?:\w[\.\-\+_]?)*)\w)+)\@((?:(?:(?:\w[\.\-_]?){0,62})\w)+)\.(\w{2,6})$/i", $email) > 0) {
338 list($emailUser, $emailDomain) = explode("@", $email);
340 // Check if the email domain has a DNS record
341 if ($this->_checkdns($emailDomain)) {
349 function getTableName() { return $this->tablename; }
350 function setTableName($value) { $this->tablename = $value; }
352 function getFieldName($field) { return $this->fields[$field]; }
353 function setFieldName($field, $value) { $this->fields[$field] = $value; }
355 function getSessionKey() { return $this->sessionkey; }
356 function setSessionKey($value) { $this->sessionkey = $value; }
358 function getCookieKey() { return $this->cookiekey; }
359 function setCookieKey($value) { $this->cookiekey = $value; }