userController.php 8.4 KB

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