userController.php 9.0 KB

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