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