4
0

userController.php 8.1 KB

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