userController.php 7.9 KB

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