userController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 updateUser($user, $passwordPlain, $apiPasswordPlain, $userConfigUpdated = array()) {
  42. $userConfig = get_user_configuration($user);
  43. if ($passwordPlain != '') {
  44. $passwordHash = self::hashPassword($passwordPlain);
  45. $userConfig->passwordHash = $passwordHash;
  46. }
  47. if ($apiPasswordPlain != '') {
  48. $apiPasswordHash = self::hashPassword($apiPasswordPlain);
  49. $userConfig->apiPasswordHash = $apiPasswordHash;
  50. }
  51. if (is_array($userConfigUpdated)) {
  52. foreach ($userConfigUpdated as $configName => $configValue) {
  53. if ($configValue !== null) {
  54. $userConfig->_param($configName, $configValue);
  55. }
  56. }
  57. }
  58. $ok = $userConfig->save();
  59. return $ok;
  60. }
  61. public function updateAction() {
  62. if (Minz_Request::isPost()) {
  63. $passwordPlain = Minz_Request::param('newPasswordPlain', '', true);
  64. Minz_Request::_param('newPasswordPlain'); //Discard plain-text password ASAP
  65. $_POST['newPasswordPlain'] = '';
  66. $apiPasswordPlain = Minz_Request::param('apiPasswordPlain', '', true);
  67. $username = Minz_Request::param('username');
  68. $ok = self::updateUser($username, $passwordPlain, $apiPasswordPlain, array(
  69. 'token' => Minz_Request::param('token', null),
  70. ));
  71. if ($ok) {
  72. Minz_Request::good(_t('feedback.user.updated', $username),
  73. array('c' => 'user', 'a' => 'manage'));
  74. } else {
  75. Minz_Request::bad(_t('feedback.user.updated.error', $username),
  76. array('c' => 'user', 'a' => 'manage'));
  77. }
  78. }
  79. }
  80. /**
  81. * This action displays the user profile page.
  82. */
  83. public function profileAction() {
  84. Minz_View::prependTitle(_t('conf.profile.title') . ' · ');
  85. Minz_View::appendScript(Minz_Url::display(
  86. '/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')
  87. ));
  88. if (Minz_Request::isPost()) {
  89. $passwordPlain = Minz_Request::param('newPasswordPlain', '', true);
  90. Minz_Request::_param('newPasswordPlain'); //Discard plain-text password ASAP
  91. $_POST['newPasswordPlain'] = '';
  92. $apiPasswordPlain = Minz_Request::param('apiPasswordPlain', '', true);
  93. $ok = self::updateUser(Minz_Session::param('currentUser'), $passwordPlain, $apiPasswordPlain, array(
  94. 'token' => Minz_Request::param('token', null),
  95. ));
  96. Minz_Session::_param('passwordHash', FreshRSS_Context::$user_conf->passwordHash);
  97. if ($ok) {
  98. Minz_Request::good(_t('feedback.profile.updated'),
  99. array('c' => 'user', 'a' => 'profile'));
  100. } else {
  101. Minz_Request::bad(_t('feedback.profile.error'),
  102. array('c' => 'user', 'a' => 'profile'));
  103. }
  104. }
  105. }
  106. /**
  107. * This action displays the user management page.
  108. */
  109. public function manageAction() {
  110. if (!FreshRSS_Auth::hasAccess('admin')) {
  111. Minz_Error::error(403);
  112. }
  113. Minz_View::prependTitle(_t('admin.user.title') . ' · ');
  114. $this->view->current_user = Minz_Request::param('u');
  115. if ($this->view->current_user) {
  116. // Get information about the current user.
  117. $entryDAO = FreshRSS_Factory::createEntryDao($this->view->current_user);
  118. $this->view->nb_articles = $entryDAO->count();
  119. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  120. $this->view->size_user = $databaseDAO->size();
  121. }
  122. }
  123. public static function createUser($new_user_name, $passwordPlain, $apiPasswordPlain, $userConfig = array(), $insertDefaultFeeds = true) {
  124. if (!is_array($userConfig)) {
  125. $userConfig = array();
  126. }
  127. $ok = self::checkUsername($new_user_name);
  128. $homeDir = join_path(DATA_PATH, 'users', $new_user_name);
  129. if ($ok) {
  130. $languages = Minz_Translate::availableLanguages();
  131. if (empty($userConfig['language']) || !in_array($userConfig['language'], $languages)) {
  132. $userConfig['language'] = 'en';
  133. }
  134. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
  135. $configPath = join_path($homeDir, 'config.php');
  136. $ok &= !file_exists($configPath);
  137. }
  138. if ($ok) {
  139. $passwordHash = '';
  140. if ($passwordPlain != '') {
  141. $passwordHash = self::hashPassword($passwordPlain);
  142. $ok &= ($passwordHash != '');
  143. }
  144. $apiPasswordHash = '';
  145. if ($apiPasswordPlain != '') {
  146. $apiPasswordHash = self::hashPassword($apiPasswordPlain);
  147. $ok &= ($apiPasswordHash != '');
  148. }
  149. }
  150. if ($ok) {
  151. if (!is_dir($homeDir)) {
  152. mkdir($homeDir);
  153. }
  154. $userConfig['passwordHash'] = $passwordHash;
  155. $userConfig['apiPasswordHash'] = $apiPasswordHash;
  156. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($userConfig, true) . ';') !== false);
  157. }
  158. if ($ok) {
  159. $userDAO = new FreshRSS_UserDAO();
  160. $ok &= $userDAO->createUser($new_user_name, $userConfig['language'], $insertDefaultFeeds);
  161. }
  162. return $ok;
  163. }
  164. /**
  165. * This action creates a new user.
  166. *
  167. * Request parameters are:
  168. * - new_user_language
  169. * - new_user_name
  170. * - new_user_passwordPlain
  171. * - r (i.e. a redirection url, optional)
  172. *
  173. * @todo clean up this method. Idea: write a method to init a user with basic information.
  174. * @todo handle r redirection in Minz_Request::forward directly?
  175. */
  176. public function createAction() {
  177. if (Minz_Request::isPost() && (
  178. FreshRSS_Auth::hasAccess('admin') ||
  179. !max_registrations_reached()
  180. )) {
  181. $new_user_name = Minz_Request::param('new_user_name');
  182. $passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
  183. $new_user_language = Minz_Request::param('new_user_language', FreshRSS_Context::$user_conf->language);
  184. $ok = self::createUser($new_user_name, $passwordPlain, '', array('language' => $new_user_language));
  185. Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
  186. $_POST['new_user_passwordPlain'] = '';
  187. invalidateHttpCache();
  188. $notif = array(
  189. 'type' => $ok ? 'good' : 'bad',
  190. 'content' => _t('feedback.user.created' . (!$ok ? '.error' : ''), $new_user_name)
  191. );
  192. Minz_Session::_param('notification', $notif);
  193. }
  194. $redirect_url = urldecode(Minz_Request::param('r', false, true));
  195. if (!$redirect_url) {
  196. $redirect_url = array('c' => 'user', 'a' => 'manage');
  197. }
  198. Minz_Request::forward($redirect_url, true);
  199. }
  200. public static function deleteUser($username) {
  201. $db = FreshRSS_Context::$system_conf->db;
  202. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  203. $ok = self::checkUsername($username);
  204. if ($ok) {
  205. $default_user = FreshRSS_Context::$system_conf->default_user;
  206. $ok &= (strcasecmp($username, $default_user) !== 0); //It is forbidden to delete the default user
  207. }
  208. $user_data = join_path(DATA_PATH, 'users', $username);
  209. if ($ok) {
  210. $ok &= is_dir($user_data);
  211. }
  212. if ($ok) {
  213. $userDAO = new FreshRSS_UserDAO();
  214. $ok &= $userDAO->deleteUser($username);
  215. $ok &= recursive_unlink($user_data);
  216. array_map('unlink', glob(PSHB_PATH . '/feeds/*/' . $username . '.txt'));
  217. }
  218. return $ok;
  219. }
  220. /**
  221. * This action delete an existing user.
  222. *
  223. * Request parameter is:
  224. * - username
  225. *
  226. * @todo clean up this method. Idea: create a User->clean() method.
  227. */
  228. public function deleteAction() {
  229. $username = Minz_Request::param('username');
  230. $redirect_url = urldecode(Minz_Request::param('r', false, true));
  231. if (!$redirect_url) {
  232. $redirect_url = array('c' => 'user', 'a' => 'manage');
  233. }
  234. $self_deletion = Minz_Session::param('currentUser', '_') === $username;
  235. if (Minz_Request::isPost() && (
  236. FreshRSS_Auth::hasAccess('admin') ||
  237. $self_deletion
  238. )) {
  239. $ok = true;
  240. if ($ok && $self_deletion) {
  241. // We check the password if it's a self-destruction
  242. $nonce = Minz_Session::param('nonce');
  243. $challenge = Minz_Request::param('challenge', '');
  244. $ok &= FreshRSS_FormAuth::checkCredentials(
  245. $username, FreshRSS_Context::$user_conf->passwordHash,
  246. $nonce, $challenge
  247. );
  248. }
  249. if ($ok) {
  250. $ok &= self::deleteUser($username);
  251. }
  252. if ($ok && $self_deletion) {
  253. FreshRSS_Auth::removeAccess();
  254. $redirect_url = array('c' => 'index', 'a' => 'index');
  255. }
  256. invalidateHttpCache();
  257. $notif = array(
  258. 'type' => $ok ? 'good' : 'bad',
  259. 'content' => _t('feedback.user.deleted' . (!$ok ? '.error' : ''), $username)
  260. );
  261. Minz_Session::_param('notification', $notif);
  262. }
  263. Minz_Request::forward($redirect_url, true);
  264. }
  265. }