userController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. $aValid = array('-', '_', '.');
  92. $ok = ($new_user_name != '') && ctype_alnum(str_replace($aValid, '', $new_user_name));
  93. if ($ok) {
  94. $languages = Minz_Translate::availableLanguages();
  95. if (empty($userConfig['language']) || !in_array($userConfig['language'], $languages)) {
  96. $userConfig['language'] = 'en';
  97. }
  98. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
  99. $configPath = join_path(DATA_PATH, 'users', $new_user_name, 'config.php');
  100. $ok &= !file_exists($configPath);
  101. }
  102. if ($ok) {
  103. $passwordHash = '';
  104. if ($passwordPlain != '') {
  105. $passwordHash = self::hashPassword($passwordPlain);
  106. $ok &= ($passwordHash != '');
  107. }
  108. $apiPasswordHash = '';
  109. if ($apiPasswordPlain != '') {
  110. $apiPasswordHash = self::hashPassword($apiPasswordPlain);
  111. $ok &= ($apiPasswordHash != '');
  112. }
  113. }
  114. if ($ok) {
  115. mkdir(join_path(DATA_PATH, 'users', $new_user_name));
  116. $userConfig['passwordHash'] = $passwordHash;
  117. $userConfig['apiPasswordHash'] = $apiPasswordHash;
  118. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($userConfig, true) . ';') !== false);
  119. }
  120. if ($ok) {
  121. $userDAO = new FreshRSS_UserDAO();
  122. $ok &= $userDAO->createUser($new_user_name, $userConfig['language'], $insertDefaultFeeds);
  123. }
  124. return $ok;
  125. }
  126. /**
  127. * This action creates a new user.
  128. *
  129. * Request parameters are:
  130. * - new_user_language
  131. * - new_user_name
  132. * - new_user_passwordPlain
  133. * - r (i.e. a redirection url, optional)
  134. *
  135. * @todo clean up this method. Idea: write a method to init a user with basic information.
  136. * @todo handle r redirection in Minz_Request::forward directly?
  137. */
  138. public function createAction() {
  139. if (Minz_Request::isPost() && (
  140. FreshRSS_Auth::hasAccess('admin') ||
  141. !max_registrations_reached()
  142. )) {
  143. $new_user_name = Minz_Request::param('new_user_name');
  144. $passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
  145. $new_user_language = Minz_Request::param('new_user_language', FreshRSS_Context::$user_conf->language);
  146. $ok = self::createUser($new_user_name, $passwordPlain, '', array('language' => $new_user_language));
  147. Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
  148. $_POST['new_user_passwordPlain'] = '';
  149. invalidateHttpCache();
  150. $notif = array(
  151. 'type' => $ok ? 'good' : 'bad',
  152. 'content' => _t('feedback.user.created' . (!$ok ? '.error' : ''), $new_user_name)
  153. );
  154. Minz_Session::_param('notification', $notif);
  155. }
  156. $redirect_url = urldecode(Minz_Request::param('r', false, true));
  157. if (!$redirect_url) {
  158. $redirect_url = array('c' => 'user', 'a' => 'manage');
  159. }
  160. Minz_Request::forward($redirect_url, true);
  161. }
  162. public static function deleteUser($username) {
  163. $db = FreshRSS_Context::$system_conf->db;
  164. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  165. $aValid = array('-', '_', '.');
  166. $ok = ctype_alnum(str_replace($aValid, '', $username));
  167. if ($ok) {
  168. $default_user = FreshRSS_Context::$system_conf->default_user;
  169. $ok &= (strcasecmp($username, $default_user) !== 0); //It is forbidden to delete the default user
  170. }
  171. $user_data = join_path(DATA_PATH, 'users', $username);
  172. if ($ok) {
  173. $ok &= is_dir($user_data);
  174. }
  175. if ($ok) {
  176. $userDAO = new FreshRSS_UserDAO();
  177. $ok &= $userDAO->deleteUser($username);
  178. $ok &= recursive_unlink($user_data);
  179. }
  180. return $ok;
  181. }
  182. /**
  183. * This action delete an existing user.
  184. *
  185. * Request parameter is:
  186. * - username
  187. *
  188. * @todo clean up this method. Idea: create a User->clean() method.
  189. */
  190. public function deleteAction() {
  191. $username = Minz_Request::param('username');
  192. $redirect_url = urldecode(Minz_Request::param('r', false, true));
  193. if (!$redirect_url) {
  194. $redirect_url = array('c' => 'user', 'a' => 'manage');
  195. }
  196. $self_deletion = Minz_Session::param('currentUser', '_') === $username;
  197. if (Minz_Request::isPost() && (
  198. FreshRSS_Auth::hasAccess('admin') ||
  199. $self_deletion
  200. )) {
  201. $ok = true;
  202. if ($ok && $self_deletion) {
  203. // We check the password if it's a self-destruction
  204. $nonce = Minz_Session::param('nonce');
  205. $challenge = Minz_Request::param('challenge', '');
  206. $ok &= FreshRSS_FormAuth::checkCredentials(
  207. $username, FreshRSS_Context::$user_conf->passwordHash,
  208. $nonce, $challenge
  209. );
  210. }
  211. if ($ok) {
  212. $ok &= self::deleteUser($username);
  213. }
  214. if ($ok && $self_deletion) {
  215. FreshRSS_Auth::removeAccess();
  216. $redirect_url = array('c' => 'index', 'a' => 'index');
  217. }
  218. invalidateHttpCache();
  219. $notif = array(
  220. 'type' => $ok ? 'good' : 'bad',
  221. 'content' => _t('feedback.user.deleted' . (!$ok ? '.error' : ''), $username)
  222. );
  223. Minz_Session::_param('notification', $notif);
  224. }
  225. Minz_Request::forward($redirect_url, true);
  226. }
  227. }