PageRenderTime 24ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/revtk/lib/rtkUser.php

https://github.com/kc5nra/RevTK
PHP | 267 lines | 119 code | 30 blank | 118 comment | 10 complexity | 0b63bd5e781402c0e6b2a7866756b0ca MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the Reviewing the Kanji package.
  4. * Copyright (c) 2005-2010 Fabrice Denis
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * rtkUser adds utility methods to the Core user class.
  11. *
  12. * Methods
  13. * getUserId()
  14. * getUserName()
  15. * getUserTimeZone()
  16. * getUserDetails() UsersPeer record
  17. * getLocalPrefs() LocalPrefs instance
  18. *
  19. * redirectToLogin($options = array()) Redirect unauthenticated user to login page
  20. * sqlLocalTime($localTimezone = null) Returns SQL statement for user's local date+time
  21. *
  22. * @author Fabrice Denis
  23. */
  24. class rtkUser extends coreUserBasicSecurity
  25. {
  26. protected
  27. $localPrefs = null;
  28. /**
  29. * The "Remember me" cookie name and lifetime in seconds.
  30. */
  31. const COOKIE_NAME = 'RevTK';
  32. const COOKIE_EXPIRE = 1296000; // 60*60*24*15
  33. /**
  34. *
  35. */
  36. public function initialize(coreSessionStorage $storage, $options = array())
  37. {
  38. parent::initialize($storage, $options);
  39. // sign in unauthenticated user if a "remember me" cookie exists
  40. if (!$this->isAuthenticated())
  41. {
  42. if ($cookieData = coreContext::getInstance()->getRequest()->getCookie(self::COOKIE_NAME))
  43. {
  44. $value = unserialize(base64_decode($cookieData));
  45. $username = $value[0];
  46. $saltyPassword = $value[1];
  47. // sign in user if user is valid and password from cookie matches the one in database
  48. $user = UsersPeer::getUser($username);
  49. if ($user && ($saltyPassword == $user['password']) )
  50. {
  51. $this->signIn($user);
  52. }
  53. }
  54. }
  55. // session duration preferences
  56. $this->localPrefs = new LocalPrefs($this);
  57. }
  58. /**
  59. * Getter method for user session attribute.
  60. *
  61. */
  62. public function getUserName()
  63. {
  64. return $this->getAttribute('username', '');
  65. }
  66. /**
  67. * Getter method for user session attribute.
  68. *
  69. */
  70. public function getUserId()
  71. {
  72. return $this->getAttribute('userid', null);
  73. }
  74. /**
  75. * Getter method for user session attribute.
  76. *
  77. */
  78. public function getUserTimeZone()
  79. {
  80. return $this->getAttribute('usertimezone', null);
  81. }
  82. /**
  83. * Return UsersPeer row data for authenticated user.
  84. *
  85. */
  86. public function getUserDetails()
  87. {
  88. return UsersPeer::getUserById($this->getUserId());
  89. }
  90. /**
  91. * Return the LocalPrefs instance.
  92. *
  93. * @return
  94. */
  95. public function getLocalPrefs()
  96. {
  97. return $this->localPrefs;
  98. }
  99. /**
  100. * Sign In the user.
  101. *
  102. * Also sign in user on the PunBB forum, by way of the forum cookie.
  103. * The "remember me" option applies similarly to the forum cookie.
  104. *
  105. * @param array $user UsersPeer row
  106. * @return
  107. */
  108. public function signIn($user)
  109. {
  110. $this->setAttribute('userid', $user['userid']);
  111. $this->setAttribute('username', $user['username']);
  112. $this->setAttribute('usertimezone', $user['timezone']);
  113. $this->clearCredentials();
  114. $this->addCredential('member');
  115. switch($user['userlevel'])
  116. {
  117. case UsersPeer::USERLEVEL_ADMIN:
  118. $this->addCredential('admin');
  119. break;
  120. default:
  121. break;
  122. }
  123. // authenticate the user
  124. $this->setAuthenticated(true);
  125. // update user's last login timestamp
  126. UsersPeer::setLastlogin($user['userid']);
  127. }
  128. /**
  129. * Sign Out the user, sets user as "Guest"
  130. *
  131. * @return
  132. */
  133. public function signOut()
  134. {
  135. $this->getAttributeHolder()->clear();
  136. $this->clearCredentials();
  137. $this->setAuthenticated(false);
  138. }
  139. /**
  140. * Sets the persistent session cookie.
  141. *
  142. */
  143. public function setRememberMeCookie($username, $saltyPassword)
  144. {
  145. $value = base64_encode( serialize(array($username, $saltyPassword)) );
  146. coreContext::getInstance()->getResponse()->setCookie(self::COOKIE_NAME, $value, time()+self::COOKIE_EXPIRE, '/');
  147. }
  148. /**
  149. * Clears the persistent session cookie.
  150. *
  151. * @return
  152. */
  153. public function clearRememberMeCookie()
  154. {
  155. coreContext::getInstance()->getResponse()->setCookie(self::COOKIE_NAME, '', time() - 3600, '/');
  156. }
  157. /**
  158. * Update the user password in the main site and forum databases.
  159. *
  160. * @param object $user
  161. * @param object $raw_password
  162. */
  163. public function changePassword($username, $raw_password)
  164. {
  165. // hash password for database
  166. $hashedPassword = $this->getSaltyHashedPassword($raw_password);
  167. // set new user password
  168. $user_id = UsersPeer::getUserId($username);
  169. UsersPeer::setPassword($user_id, $hashedPassword);
  170. // set new password on forum account (not in staging)
  171. if (coreContext::getInstance()->getConfiguration()->getEnvironment() !== 'staging')
  172. {
  173. // only with linked PunBB forum
  174. if (coreConfig::get('app_path_to_punbb') !== null)
  175. {
  176. PunBBUsersPeer::setPassword($username, $raw_password);
  177. }
  178. }
  179. }
  180. /**
  181. * Returns hashed password.
  182. *
  183. * We use sha1() like PunBB to store passwords.
  184. *
  185. * Ideally could store a random salt with each user, eg:
  186. *
  187. * salt VARCHAR(32) => md5(rand(100000, 999999).$this->getNickname().$this->getEmail());
  188. * password VARCHAR(40) => sha1($salt.$raw_password)
  189. *
  190. * @param string $password Non-encrypted password.
  191. */
  192. public function getSaltyHashedPassword($raw_password)
  193. {
  194. return sha1($raw_password);
  195. }
  196. /**
  197. * Redirect unauthenticated user to login action.
  198. *
  199. * Options:
  200. *
  201. * username => Fill in the user name of the login form
  202. * referer => Page to return the user to after signing in
  203. *
  204. * @param array $params Options to pass to the login page
  205. */
  206. public function redirectToLogin($options = array())
  207. {
  208. if (isset($options['referer']))
  209. {
  210. $this->setAttribute('login_referer', $options['referer']);
  211. }
  212. if (isset($options['username']))
  213. {
  214. $this->setAttribute('login_username', $options['username']);
  215. }
  216. $login_url = coreConfig::get('login_module') . '/' . coreConfig::get('login_action');
  217. coreContext::getInstance()->getActionInstance()->redirect($login_url);
  218. }
  219. /**
  220. * Returns a SQL statement which returns a date+time adjusted to the
  221. * timezone of the user. The date returned by
  222. * this statement will switch at midnight time of the user's timezone
  223. * (assuming the user set the timezone properly).
  224. * (the user's timezone range is -12...+14)
  225. *
  226. * @return String MySQL ADDDATE() expression that evaluates to the user's localized time
  227. */
  228. public function sqlLocalTime()
  229. {
  230. $localTimezone = $this->getUserTimeZone();
  231. $timediff = $localTimezone - coreConfig::get('app_server_timezone');
  232. $hours = floor($timediff);
  233. $minutes = ($hours != $timediff) ? '30' : '0'; // some timezones have half-hour precision, convert to minutes
  234. $sqlDate = 'ADDDATE(NOW(), INTERVAL \''.$hours.':'.$minutes.'\' HOUR_MINUTE)';
  235. return $sqlDate;
  236. }
  237. }