userController.php 8.9 KB

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