userController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * Controller to handle user actions.
  4. */
  5. class FreshRSS_user_Controller extends Minz_ActionController {
  6. // Will also have to be computed client side on mobile devices,
  7. // so do not use a too high cost
  8. const BCRYPT_COST = 9;
  9. /**
  10. * This action is called before every other action in that class. It is
  11. * the common boiler plate for every action. It is triggered by the
  12. * underlying framework.
  13. *
  14. * @todo clean up the access condition.
  15. */
  16. public function firstAction() {
  17. if (!FreshRSS_Auth::hasAccess() && !(
  18. Minz_Request::actionName() === 'create' &&
  19. !max_registrations_reached()
  20. )) {
  21. Minz_Error::error(403);
  22. }
  23. }
  24. public static function hashPassword($passwordPlain) {
  25. if (!function_exists('password_hash')) {
  26. include_once(LIB_PATH . '/password_compat.php');
  27. }
  28. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
  29. $passwordPlain = '';
  30. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
  31. return $passwordHash == '' ? '' : $passwordHash;
  32. }
  33. /**
  34. * The username is also used as folder name, file name, and part of SQL table name.
  35. * '_' is a reserved internal username.
  36. */
  37. const USERNAME_PATTERN = '[0-9a-zA-Z_]{2,38}|[0-9a-zA-Z]';
  38. public static function checkUsername($username) {
  39. return preg_match('/^' . self::USERNAME_PATTERN . '$/', $username) === 1;
  40. }
  41. public static function updateContextUser($passwordPlain, $apiPasswordPlain, $userConfigUpdated = array()) {
  42. if ($passwordPlain != '') {
  43. $passwordHash = self::hashPassword($passwordPlain);
  44. FreshRSS_Context::$user_conf->passwordHash = $passwordHash;
  45. }
  46. if ($apiPasswordPlain != '') {
  47. $apiPasswordHash = self::hashPassword($apiPasswordPlain);
  48. FreshRSS_Context::$user_conf->apiPasswordHash = $apiPasswordHash;
  49. }
  50. if (is_array($userConfigUpdated)) {
  51. foreach ($userConfigUpdated as $configName => $configValue) {
  52. if ($configValue !== null) {
  53. FreshRSS_Context::$user_conf->_param($configName, $configValue);
  54. }
  55. }
  56. }
  57. $ok = FreshRSS_Context::$user_conf->save();
  58. return $ok;
  59. }
  60. /**
  61. * This action displays the user profile page.
  62. */
  63. public function profileAction() {
  64. Minz_View::prependTitle(_t('conf.profile.title') . ' · ');
  65. Minz_View::appendScript(Minz_Url::display(
  66. '/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')
  67. ));
  68. if (Minz_Request::isPost()) {
  69. $passwordPlain = Minz_Request::param('newPasswordPlain', '', true);
  70. Minz_Request::_param('newPasswordPlain'); //Discard plain-text password ASAP
  71. $_POST['newPasswordPlain'] = '';
  72. $apiPasswordPlain = Minz_Request::param('apiPasswordPlain', '', true);
  73. $ok = self::updateContextUser($passwordPlain, $apiPasswordPlain, array(
  74. 'token' => Minz_Request::param('token', null),
  75. ));
  76. Minz_Session::_param('passwordHash', FreshRSS_Context::$user_conf->passwordHash);
  77. if ($ok) {
  78. Minz_Request::good(_t('feedback.profile.updated'),
  79. array('c' => 'user', 'a' => 'profile'));
  80. } else {
  81. Minz_Request::bad(_t('feedback.profile.error'),
  82. array('c' => 'user', 'a' => 'profile'));
  83. }
  84. }
  85. }
  86. /**
  87. * This action displays the user management page.
  88. */
  89. public function manageAction() {
  90. if (!FreshRSS_Auth::hasAccess('admin')) {
  91. Minz_Error::error(403);
  92. }
  93. Minz_View::prependTitle(_t('admin.user.title') . ' · ');
  94. // Get the correct current user.
  95. $username = Minz_Request::param('u', Minz_Session::param('currentUser'));
  96. if (!FreshRSS_UserDAO::exist($username)) {
  97. $username = Minz_Session::param('currentUser');
  98. }
  99. $this->view->current_user = $username;
  100. // Get information about the current user.
  101. $entryDAO = FreshRSS_Factory::createEntryDao($this->view->current_user);
  102. $this->view->nb_articles = $entryDAO->count();
  103. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  104. $this->view->size_user = $databaseDAO->size();
  105. }
  106. public static function createUser($new_user_name, $passwordPlain, $apiPasswordPlain, $userConfig = array(), $insertDefaultFeeds = true) {
  107. if (!is_array($userConfig)) {
  108. $userConfig = array();
  109. }
  110. $ok = self::checkUsername($new_user_name);
  111. $homeDir = join_path(DATA_PATH, 'users', $new_user_name);
  112. if ($ok) {
  113. $languages = Minz_Translate::availableLanguages();
  114. if (empty($userConfig['language']) || !in_array($userConfig['language'], $languages)) {
  115. $userConfig['language'] = 'en';
  116. }
  117. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
  118. $configPath = join_path($homeDir, 'config.php');
  119. $ok &= !file_exists($configPath);
  120. }
  121. if ($ok) {
  122. $passwordHash = '';
  123. if ($passwordPlain != '') {
  124. $passwordHash = self::hashPassword($passwordPlain);
  125. $ok &= ($passwordHash != '');
  126. }
  127. $apiPasswordHash = '';
  128. if ($apiPasswordPlain != '') {
  129. $apiPasswordHash = self::hashPassword($apiPasswordPlain);
  130. $ok &= ($apiPasswordHash != '');
  131. }
  132. }
  133. if ($ok) {
  134. if (!is_dir($homeDir)) {
  135. mkdir($homeDir);
  136. }
  137. $userConfig['passwordHash'] = $passwordHash;
  138. $userConfig['apiPasswordHash'] = $apiPasswordHash;
  139. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($userConfig, true) . ';') !== false);
  140. }
  141. if ($ok) {
  142. $userDAO = new FreshRSS_UserDAO();
  143. $ok &= $userDAO->createUser($new_user_name, $userConfig['language'], $insertDefaultFeeds);
  144. }
  145. return $ok;
  146. }
  147. /**
  148. * This action creates a new user.
  149. *
  150. * Request parameters are:
  151. * - new_user_language
  152. * - new_user_name
  153. * - new_user_passwordPlain
  154. * - r (i.e. a redirection url, optional)
  155. *
  156. * @todo clean up this method. Idea: write a method to init a user with basic information.
  157. * @todo handle r redirection in Minz_Request::forward directly?
  158. */
  159. public function createAction() {
  160. if (Minz_Request::isPost() && (
  161. FreshRSS_Auth::hasAccess('admin') ||
  162. !max_registrations_reached()
  163. )) {
  164. $new_user_name = Minz_Request::param('new_user_name');
  165. $passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
  166. $new_user_language = Minz_Request::param('new_user_language', FreshRSS_Context::$user_conf->language);
  167. $ok = self::createUser($new_user_name, $passwordPlain, '', array('language' => $new_user_language));
  168. Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
  169. $_POST['new_user_passwordPlain'] = '';
  170. invalidateHttpCache();
  171. $notif = array(
  172. 'type' => $ok ? 'good' : 'bad',
  173. 'content' => _t('feedback.user.created' . (!$ok ? '.error' : ''), $new_user_name)
  174. );
  175. Minz_Session::_param('notification', $notif);
  176. }
  177. $redirect_url = urldecode(Minz_Request::param('r', false, true));
  178. if (!$redirect_url) {
  179. $redirect_url = array('c' => 'user', 'a' => 'manage');
  180. }
  181. Minz_Request::forward($redirect_url, true);
  182. }
  183. public static function deleteUser($username) {
  184. $db = FreshRSS_Context::$system_conf->db;
  185. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  186. $ok = self::checkUsername($username);
  187. if ($ok) {
  188. $default_user = FreshRSS_Context::$system_conf->default_user;
  189. $ok &= (strcasecmp($username, $default_user) !== 0); //It is forbidden to delete the default user
  190. }
  191. $user_data = join_path(DATA_PATH, 'users', $username);
  192. if ($ok) {
  193. $ok &= is_dir($user_data);
  194. }
  195. if ($ok) {
  196. $userDAO = new FreshRSS_UserDAO();
  197. $ok &= $userDAO->deleteUser($username);
  198. $ok &= recursive_unlink($user_data);
  199. array_map('unlink', glob(PSHB_PATH . '/feeds/*/' . $username . '.txt'));
  200. }
  201. return $ok;
  202. }
  203. /**
  204. * This action delete an existing user.
  205. *
  206. * Request parameter is:
  207. * - username
  208. *
  209. * @todo clean up this method. Idea: create a User->clean() method.
  210. */
  211. public function deleteAction() {
  212. $username = Minz_Request::param('username');
  213. $redirect_url = urldecode(Minz_Request::param('r', false, true));
  214. if (!$redirect_url) {
  215. $redirect_url = array('c' => 'user', 'a' => 'manage');
  216. }
  217. $self_deletion = Minz_Session::param('currentUser', '_') === $username;
  218. if (Minz_Request::isPost() && (
  219. FreshRSS_Auth::hasAccess('admin') ||
  220. $self_deletion
  221. )) {
  222. $ok = true;
  223. if ($ok && $self_deletion) {
  224. // We check the password if it's a self-destruction
  225. $nonce = Minz_Session::param('nonce');
  226. $challenge = Minz_Request::param('challenge', '');
  227. $ok &= FreshRSS_FormAuth::checkCredentials(
  228. $username, FreshRSS_Context::$user_conf->passwordHash,
  229. $nonce, $challenge
  230. );
  231. }
  232. if ($ok) {
  233. $ok &= self::deleteUser($username);
  234. }
  235. if ($ok && $self_deletion) {
  236. FreshRSS_Auth::removeAccess();
  237. $redirect_url = array('c' => 'index', 'a' => 'index');
  238. }
  239. invalidateHttpCache();
  240. $notif = array(
  241. 'type' => $ok ? 'good' : 'bad',
  242. 'content' => _t('feedback.user.deleted' . (!$ok ? '.error' : ''), $username)
  243. );
  244. Minz_Session::_param('notification', $notif);
  245. }
  246. Minz_Request::forward($redirect_url, true);
  247. }
  248. }