4
0

userController.php 9.6 KB

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