PageRenderTime 22ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/scuttle/tags/scuttle-0.7.2-upstream/services/userservice.php

https://github.com/martijnvermaat/quick-hacks
PHP | 362 lines | 290 code | 56 blank | 16 comment | 52 complexity | 5d9f5dd5c9cb896db22a1de1a55cf444 MD5 | raw file
  1. <?php
  2. class UserService {
  3. var $db;
  4. function &getInstance(&$db) {
  5. static $instance;
  6. if (!isset($instance))
  7. $instance =& new UserService($db);
  8. return $instance;
  9. }
  10. var $fields = array(
  11. 'primary' => 'uId',
  12. 'username' => 'username',
  13. 'password' => 'password'
  14. );
  15. var $profileurl;
  16. var $tablename;
  17. var $sessionkey;
  18. var $cookiekey;
  19. var $cookietime = 1209600; // 2 weeks
  20. function UserService(&$db) {
  21. $this->db =& $db;
  22. $this->tablename = $GLOBALS['tableprefix'] .'users';
  23. $this->sessionkey = $GLOBALS['cookieprefix'] .'-currentuserid';
  24. $this->cookiekey = $GLOBALS['cookieprefix'] .'-login';
  25. $this->profileurl = createURL('profile', '%2$s');
  26. }
  27. function _checkdns($host) {
  28. if (function_exists('checkdnsrr')) {
  29. return checkdnsrr($host);
  30. } else {
  31. return $this->_checkdnsrr($host);
  32. }
  33. }
  34. function _checkdnsrr($host, $type = "MX") {
  35. if(!empty($host)) {
  36. @exec("nslookup -type=$type $host", $output);
  37. while(list($k, $line) = each($output)) {
  38. if(eregi("^$host", $line)) {
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. }
  45. function _getuser($fieldname, $value) {
  46. $query = 'SELECT * FROM '. $this->getTableName() .' WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"';
  47. if (! ($dbresult =& $this->db->sql_query($query)) ) {
  48. message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
  49. return false;
  50. }
  51. if ($row =& $this->db->sql_fetchrow($dbresult))
  52. return $row;
  53. else
  54. return false;
  55. }
  56. function _randompassword() {
  57. $seed = (integer) md5(microtime());
  58. mt_srand($seed);
  59. $password = mt_rand(1, 99999999);
  60. $password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12));
  61. return $password;
  62. }
  63. function _updateuser($uId, $fieldname, $value) {
  64. $updates = array ($fieldname => $value);
  65. $sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId);
  66. // Execute the statement.
  67. $this->db->sql_transaction('begin');
  68. if (!($dbresult = & $this->db->sql_query($sql))) {
  69. $this->db->sql_transaction('rollback');
  70. message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db);
  71. return false;
  72. }
  73. $this->db->sql_transaction('commit');
  74. // Everything worked out, so return true.
  75. return true;
  76. }
  77. function getProfileUrl($id, $username) {
  78. return sprintf($this->profileurl, urlencode($id), urlencode($username));
  79. }
  80. function getUserByUsername($username) {
  81. return $this->_getuser($this->getFieldName('username'), $username);
  82. }
  83. function getUser($id) {
  84. return $this->_getuser($this->getFieldName('primary'), $id);
  85. }
  86. function isLoggedOn() {
  87. return ($this->getCurrentUserId() !== false);
  88. }
  89. function &getCurrentUser($refresh = FALSE, $newval = NULL) {
  90. static $currentuser;
  91. if (!is_null($newval)) //internal use only: reset currentuser
  92. $currentuser = $newval;
  93. else if ($refresh || !isset($currentuser)) {
  94. if ($id = $this->getCurrentUserId())
  95. $currentuser = $this->getUser($id);
  96. else
  97. return;
  98. }
  99. return $currentuser;
  100. }
  101. function isAdmin($userid) {
  102. return false; //not implemented yet
  103. }
  104. function getCurrentUserId() {
  105. if (isset($_SESSION[$this->getSessionKey()])) {
  106. return $_SESSION[$this->getSessionKey()];
  107. } else if (isset($_COOKIE[$this->getCookieKey()])) {
  108. $cook = split(':', $_COOKIE[$this->getCookieKey()]);
  109. //cookie looks like this: 'id:md5(username+password)'
  110. $query = 'SELECT * FROM '. $this->getTableName() .
  111. ' WHERE MD5(CONCAT('.$this->getFieldName('username') .
  112. ', '.$this->getFieldName('password') .
  113. ')) = \''.$this->db->sql_escape($cook[1]).'\' AND '.
  114. $this->getFieldName('primary'). ' = '. $this->db->sql_escape($cook[0]);
  115. if (! ($dbresult =& $this->db->sql_query($query)) ) {
  116. message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
  117. return false;
  118. }
  119. if ($row = $this->db->sql_fetchrow($dbresult)) {
  120. $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')];
  121. return $_SESSION[$this->getSessionKey()];
  122. }
  123. }
  124. return false;
  125. }
  126. function login($username, $password, $remember = FALSE) {
  127. $password = $this->sanitisePassword($password);
  128. $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) .'"';
  129. if (! ($dbresult =& $this->db->sql_query($query)) ) {
  130. message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
  131. return false;
  132. }
  133. if ($row =& $this->db->sql_fetchrow($dbresult)) {
  134. $id = $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')];
  135. if ($remember) {
  136. $cookie = $id .':'. md5($username.$password);
  137. setcookie($this->cookiekey, $cookie, time() + $this->cookietime);
  138. }
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. }
  144. function logout() {
  145. @setcookie($this->cookiekey, NULL, time() - 1);
  146. unset($_COOKIE[$this->cookiekey]);
  147. session_unset();
  148. $this->getCurrentUser(TRUE, false);
  149. }
  150. function getWatchlist($uId) {
  151. // Gets the list of user IDs being watched by the given user.
  152. $query = 'SELECT watched FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($uId);
  153. if (! ($dbresult =& $this->db->sql_query($query)) ) {
  154. message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db);
  155. return false;
  156. }
  157. $arrWatch = array();
  158. if ($this->db->sql_numrows($dbresult) == 0)
  159. return $arrWatch;
  160. while ($row =& $this->db->sql_fetchrow($dbresult))
  161. $arrWatch[] = $row['watched'];
  162. return $arrWatch;
  163. }
  164. function getWatchNames($uId, $watchedby = false) {
  165. // Gets the list of user names being watched by the given user.
  166. // - If $watchedby is false get the list of users that $uId watches
  167. // - If $watchedby is true get the list of users that watch $uId
  168. if ($watchedby) {
  169. $table1 = 'b';
  170. $table2 = 'a';
  171. } else {
  172. $table1 = 'a';
  173. $table2 = 'b';
  174. }
  175. $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');
  176. if (!($dbresult =& $this->db->sql_query($query))) {
  177. message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db);
  178. return false;
  179. }
  180. $arrWatch = array();
  181. if ($this->db->sql_numrows($dbresult) == 0) {
  182. return $arrWatch;
  183. }
  184. while ($row =& $this->db->sql_fetchrow($dbresult)) {
  185. $arrWatch[] = $row[$this->getFieldName('username')];
  186. }
  187. return $arrWatch;
  188. }
  189. function getWatchStatus($watcheduser, $currentuser) {
  190. // Returns true if the current user is watching the given user, and false otherwise.
  191. $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);
  192. if (! ($dbresult =& $this->db->sql_query($query)) ) {
  193. message_die(GENERAL_ERROR, 'Could not get watchstatus', '', __LINE__, __FILE__, $query, $this->db);
  194. return false;
  195. }
  196. $arrWatch = array();
  197. if ($this->db->sql_numrows($dbresult) == 0)
  198. return false;
  199. else
  200. return true;
  201. }
  202. function setWatchStatus($subjectUserID) {
  203. if (!is_numeric($subjectUserID))
  204. return false;
  205. $currentUserID = $this->getCurrentUserId();
  206. $watched = $this->getWatchStatus($subjectUserID, $currentUserID);
  207. if ($watched) {
  208. $sql = 'DELETE FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($currentUserID) .' AND watched = '. intval($subjectUserID);
  209. if (!($dbresult =& $this->db->sql_query($sql))) {
  210. $this->db->sql_transaction('rollback');
  211. message_die(GENERAL_ERROR, 'Could not add user to watch list', '', __LINE__, __FILE__, $sql, $this->db);
  212. return false;
  213. }
  214. } else {
  215. $values = array(
  216. 'uId' => intval($currentUserID),
  217. 'watched' => intval($subjectUserID)
  218. );
  219. $sql = 'INSERT INTO '. $GLOBALS['tableprefix'] .'watched '. $this->db->sql_build_array('INSERT', $values);
  220. if (!($dbresult =& $this->db->sql_query($sql))) {
  221. $this->db->sql_transaction('rollback');
  222. message_die(GENERAL_ERROR, 'Could not add user to watch list', '', __LINE__, __FILE__, $sql, $this->db);
  223. return false;
  224. }
  225. }
  226. $this->db->sql_transaction('commit');
  227. return true;
  228. }
  229. function addUser($username, $password, $email) {
  230. // Set up the SQL UPDATE statement.
  231. $datetime = gmdate('Y-m-d H:i:s', time());
  232. $password = $this->sanitisePassword($password);
  233. $values = array('username' => $username, 'password' => $password, 'email' => $email, 'uDatetime' => $datetime, 'uModified' => $datetime);
  234. $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
  235. // Execute the statement.
  236. $this->db->sql_transaction('begin');
  237. if (!($dbresult = & $this->db->sql_query($sql))) {
  238. $this->db->sql_transaction('rollback');
  239. message_die(GENERAL_ERROR, 'Could not insert user', '', __LINE__, __FILE__, $sql, $this->db);
  240. return false;
  241. }
  242. $this->db->sql_transaction('commit');
  243. // Everything worked out, so return true.
  244. return true;
  245. }
  246. function updateUser($uId, $password, $name, $email, $homepage, $uContent) {
  247. if (!is_numeric($uId))
  248. return false;
  249. // Set up the SQL UPDATE statement.
  250. $moddatetime = gmdate('Y-m-d H:i:s', time());
  251. if ($password == '')
  252. $updates = array ('uModified' => $moddatetime, 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent);
  253. else
  254. $updates = array ('uModified' => $moddatetime, 'password' => $this->sanitisePassword($password), 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent);
  255. $sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId);
  256. // Execute the statement.
  257. $this->db->sql_transaction('begin');
  258. if (!($dbresult = & $this->db->sql_query($sql))) {
  259. $this->db->sql_transaction('rollback');
  260. message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db);
  261. return false;
  262. }
  263. $this->db->sql_transaction('commit');
  264. // Everything worked out, so return true.
  265. return true;
  266. }
  267. function sanitisePassword($password) {
  268. return sha1(trim($password));
  269. }
  270. function generatePassword($uId) {
  271. if (!is_numeric($uId))
  272. return false;
  273. $password = $this->_randompassword();
  274. if ($this->_updateuser($uId, $this->getFieldName('password'), $this->sanitisePassword($password)))
  275. return $password;
  276. else
  277. return false;
  278. }
  279. function isReserved($username) {
  280. if (in_array($username, $GLOBALS['reservedusers'])) {
  281. return true;
  282. } else {
  283. return false;
  284. }
  285. }
  286. function isValidEmail($email) {
  287. if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$", $email)) {
  288. list($emailUser, $emailDomain) = split("@", $email);
  289. // Check if the email domain has a DNS record
  290. if ($this->_checkdns($emailDomain)) {
  291. return true;
  292. }
  293. }
  294. return false;
  295. }
  296. // Properties
  297. function getTableName() { return $this->tablename; }
  298. function setTableName($value) { $this->tablename = $value; }
  299. function getFieldName($field) { return $this->fields[$field]; }
  300. function setFieldName($field, $value) { $this->fields[$field] = $value; }
  301. function getSessionKey() { return $this->sessionkey; }
  302. function setSessionKey($value) { $this->sessionkey = $value; }
  303. function getCookieKey() { return $this->cookiekey; }
  304. function setCookieKey($value) { $this->cookiekey = $value; }
  305. }
  306. ?>