4
0

userController.php 8.1 KB

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