5 function &getInstance(&$db) {
8 $instance =& new UserService($db);
14 'username' => 'username',
15 'password' => 'password'
21 var $cookietime = 1209600; // 2 weeks
23 function UserService(&$db) {
25 $this->tablename = $GLOBALS['tableprefix'] .'users';
26 $this->sessionkey = $GLOBALS['cookieprefix'] .'-currentuserid';
27 $this->cookiekey = $GLOBALS['cookieprefix'] .'-login';
28 $this->profileurl = createURL('profile', '%2$s');
31 function _checkdns($host) {
32 if (function_exists('checkdnsrr')) {
33 return checkdnsrr($host);
35 return $this->_checkdnsrr($host);
39 function _checkdnsrr($host, $type = "MX") {
41 @exec("nslookup -type=$type $host", $output);
42 while(list($k, $line) = each($output)) {
43 if(eregi("^$host", $line)) {
51 function _getuser($fieldname, $value) {
52 $query = 'SELECT * FROM '. $this->getTableName() .' WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"';
54 if (! ($dbresult =& $this->db->sql_query($query)) ) {
55 message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
59 if ($row =& $this->db->sql_fetchrow($dbresult))
65 function _randompassword() {
66 $password = mt_rand(1, 99999999);
67 $password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12));
71 function _updateuser($uId, $fieldname, $value) {
72 $updates = array ($fieldname => $value);
73 $sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId);
75 // Execute the statement.
76 $this->db->sql_transaction('begin');
77 if (!($dbresult = & $this->db->sql_query($sql))) {
78 $this->db->sql_transaction('rollback');
79 message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db);
82 $this->db->sql_transaction('commit');
84 // Everything worked out, so return true.
88 function getProfileUrl($id, $username) {
89 return sprintf($this->profileurl, urlencode($id), urlencode($username));
92 function getUserByUsername($username) {
93 return $this->_getuser($this->getFieldName('username'), $username);
96 function getUser($id) {
97 return $this->_getuser($this->getFieldName('primary'), $id);
100 function isLoggedOn() {
101 return ($this->getCurrentUserId() !== false);
104 function &getCurrentUser($refresh = FALSE, $newval = NULL) {
106 if (!is_null($newval)) //internal use only: reset currentuser
107 $currentuser = $newval;
108 else if ($refresh || !isset($currentuser)) {
109 if ($id = $this->getCurrentUserId())
110 $currentuser = $this->getUser($id);
117 function isAdmin($userid) {
118 return false; //not implemented yet
121 function getCurrentUserId() {
122 if (isset($_SESSION[$this->getSessionKey()])) {
123 return $_SESSION[$this->getSessionKey()];
124 } else if (isset($_COOKIE[$this->getCookieKey()])) {
125 $cook = split(':', $_COOKIE[$this->getCookieKey()]);
126 //cookie looks like this: 'id:md5(username+password)'
127 $query = 'SELECT * FROM '. $this->getTableName() .
128 ' WHERE MD5(CONCAT('.$this->getFieldName('username') .
129 ', '.$this->getFieldName('password') .
130 ')) = \''.$this->db->sql_escape($cook[1]).'\' AND '.
131 $this->getFieldName('primary'). ' = '. $this->db->sql_escape($cook[0]);
133 if (! ($dbresult =& $this->db->sql_query($query)) ) {
134 message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
138 if ($row = $this->db->sql_fetchrow($dbresult)) {
139 $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')];
140 return $_SESSION[$this->getSessionKey()];
146 function login($username, $password, $remember = FALSE, $path = '/') {
147 $password = $this->sanitisePassword($password);
148 $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) .'"';
150 if (! ($dbresult =& $this->db->sql_query($query)) ) {
151 message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
155 if ($row =& $this->db->sql_fetchrow($dbresult)) {
156 $id = $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')];
158 $cookie = $id .':'. md5($username.$password);
159 setcookie($this->cookiekey, $cookie, time() + $this->cookietime, $path);
167 function logout($path = '/') {
168 @setcookie($this->cookiekey, NULL, time() - 1, $path);
169 unset($_COOKIE[$this->cookiekey]);
171 $this->getCurrentUser(TRUE, false);
174 function getWatchlist($uId) {
175 // Gets the list of user IDs being watched by the given user.
176 $query = 'SELECT watched FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($uId);
178 if (! ($dbresult =& $this->db->sql_query($query)) ) {
179 message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db);
184 if ($this->db->sql_numrows($dbresult) == 0)
186 while ($row =& $this->db->sql_fetchrow($dbresult))
187 $arrWatch[] = $row['watched'];
191 function getWatchNames($uId, $watchedby = false) {
192 // Gets the list of user names being watched by the given user.
193 // - If $watchedby is false get the list of users that $uId watches
194 // - If $watchedby is true get the list of users that watch $uId
202 $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');
204 if (!($dbresult =& $this->db->sql_query($query))) {
205 message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db);
210 if ($this->db->sql_numrows($dbresult) == 0) {
213 while ($row =& $this->db->sql_fetchrow($dbresult)) {
214 $arrWatch[] = $row[$this->getFieldName('username')];
219 function getWatchStatus($watcheduser, $currentuser) {
220 // Returns true if the current user is watching the given user, and false otherwise.
221 $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);
223 if (! ($dbresult =& $this->db->sql_query($query)) ) {
224 message_die(GENERAL_ERROR, 'Could not get watchstatus', '', __LINE__, __FILE__, $query, $this->db);
229 if ($this->db->sql_numrows($dbresult) == 0)
235 function setWatchStatus($subjectUserID) {
236 if (!is_numeric($subjectUserID))
239 $currentUserID = $this->getCurrentUserId();
240 $watched = $this->getWatchStatus($subjectUserID, $currentUserID);
243 $sql = 'DELETE FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($currentUserID) .' AND watched = '. intval($subjectUserID);
244 if (!($dbresult =& $this->db->sql_query($sql))) {
245 $this->db->sql_transaction('rollback');
246 message_die(GENERAL_ERROR, 'Could not add user to watch list', '', __LINE__, __FILE__, $sql, $this->db);
251 'uId' => intval($currentUserID),
252 'watched' => intval($subjectUserID)
254 $sql = 'INSERT INTO '. $GLOBALS['tableprefix'] .'watched '. $this->db->sql_build_array('INSERT', $values);
255 if (!($dbresult =& $this->db->sql_query($sql))) {
256 $this->db->sql_transaction('rollback');
257 message_die(GENERAL_ERROR, 'Could not add user to watch list', '', __LINE__, __FILE__, $sql, $this->db);
262 $this->db->sql_transaction('commit');
266 function addUser($username, $password, $email) {
267 // Set up the SQL UPDATE statement.
268 $datetime = gmdate('Y-m-d H:i:s', time());
269 $password = $this->sanitisePassword($password);
270 $values = array('username' => $username, 'password' => $password, 'email' => $email, 'uDatetime' => $datetime, 'uModified' => $datetime);
271 $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
273 // Execute the statement.
274 $this->db->sql_transaction('begin');
275 if (!($dbresult = & $this->db->sql_query($sql))) {
276 $this->db->sql_transaction('rollback');
277 message_die(GENERAL_ERROR, 'Could not insert user', '', __LINE__, __FILE__, $sql, $this->db);
280 $this->db->sql_transaction('commit');
282 // Everything worked out, so return true.
286 function updateUser($uId, $password, $name, $email, $homepage, $uContent) {
287 if (!is_numeric($uId))
290 // Set up the SQL UPDATE statement.
291 $moddatetime = gmdate('Y-m-d H:i:s', time());
293 $updates = array ('uModified' => $moddatetime, 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent);
295 $updates = array ('uModified' => $moddatetime, 'password' => $this->sanitisePassword($password), 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent);
296 $sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId);
298 // Execute the statement.
299 $this->db->sql_transaction('begin');
300 if (!($dbresult = & $this->db->sql_query($sql))) {
301 $this->db->sql_transaction('rollback');
302 message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db);
305 $this->db->sql_transaction('commit');
307 // Everything worked out, so return true.
311 function sanitisePassword($password) {
312 return sha1(trim($password));
315 function generatePassword($uId) {
316 if (!is_numeric($uId))
319 $password = $this->_randompassword();
321 if ($this->_updateuser($uId, $this->getFieldName('password'), $this->sanitisePassword($password)))
327 function isReserved($username) {
328 if (in_array($username, $GLOBALS['reservedusers'])) {
335 function isValidEmail($email) {
336 if (preg_match("/^((?:(?:(?:\w[\.\-\+_]?)*)\w)+)\@((?:(?:(?:\w[\.\-_]?){0,62})\w)+)\.(\w{2,6})$/i", $email) > 0) {
337 list($emailUser, $emailDomain) = split("@", $email);
339 // Check if the email domain has a DNS record
340 if ($this->_checkdns($emailDomain)) {
348 function getTableName() { return $this->tablename; }
349 function setTableName($value) { $this->tablename = $value; }
351 function getFieldName($field) { return $this->fields[$field]; }
352 function setFieldName($field, $value) { $this->fields[$field] = $value; }
354 function getSessionKey() { return $this->sessionkey; }
355 function setSessionKey($value) { $this->sessionkey = $value; }
357 function getCookieKey() { return $this->cookiekey; }
358 function setCookieKey($value) { $this->cookiekey = $value; }