userController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. public function createAction() {
  92. if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
  93. $db = FreshRSS_Context::$system_conf->db;
  94. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  95. $new_user_language = Minz_Request::param('new_user_language', FreshRSS_Context::$user_conf->language);
  96. $languages = Minz_Translate::availableLanguages();
  97. if (!isset($languages[$new_user_language])) {
  98. $new_user_language = FreshRSS_Context::$user_conf->language;
  99. }
  100. $new_user_name = Minz_Request::param('new_user_name');
  101. $ok = ($new_user_name != '') && ctype_alnum($new_user_name);
  102. if ($ok) {
  103. $default_user = FreshRSS_Context::$system_conf->default_user;
  104. $ok &= (strcasecmp($new_user_name, $default_user) !== 0); //It is forbidden to alter the default user
  105. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
  106. $configPath = join_path(DATA_PATH, 'users', $new_user_name, 'config.php');
  107. $ok &= !file_exists($configPath);
  108. }
  109. if ($ok) {
  110. $passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
  111. $passwordHash = '';
  112. if ($passwordPlain != '') {
  113. Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
  114. $_POST['new_user_passwordPlain'] = '';
  115. if (!function_exists('password_hash')) {
  116. include_once(LIB_PATH . '/password_compat.php');
  117. }
  118. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
  119. $passwordPlain = '';
  120. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
  121. $ok &= ($passwordHash != '');
  122. }
  123. if (empty($passwordHash)) {
  124. $passwordHash = '';
  125. }
  126. $new_user_email = filter_var($_POST['new_user_email'], FILTER_VALIDATE_EMAIL);
  127. if (empty($new_user_email)) {
  128. $new_user_email = '';
  129. } else {
  130. $personaFile = join_path(DATA_PATH, 'persona', $new_user_email . '.txt');
  131. @unlink($personaFile);
  132. $ok &= (file_put_contents($personaFile, $new_user_name) !== false);
  133. }
  134. }
  135. if ($ok) {
  136. mkdir(join_path(DATA_PATH, 'users', $new_user_name));
  137. $config_array = array(
  138. 'language' => $new_user_language,
  139. 'passwordHash' => $passwordHash,
  140. 'mail_login' => $new_user_email,
  141. );
  142. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';') !== false);
  143. }
  144. if ($ok) {
  145. $userDAO = new FreshRSS_UserDAO();
  146. $ok &= $userDAO->createUser($new_user_name);
  147. }
  148. invalidateHttpCache();
  149. $notif = array(
  150. 'type' => $ok ? 'good' : 'bad',
  151. 'content' => _t('feedback.user.created' . (!$ok ? '.error' : ''), $new_user_name)
  152. );
  153. Minz_Session::_param('notification', $notif);
  154. }
  155. Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
  156. }
  157. public function deleteAction() {
  158. if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
  159. $db = FreshRSS_Context::$system_conf->db;
  160. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  161. $username = Minz_Request::param('username');
  162. $ok = ctype_alnum($username);
  163. $user_data = join_path(DATA_PATH, 'users', $username);
  164. if ($ok) {
  165. $default_user = FreshRSS_Context::$system_conf->default_user;
  166. $ok &= (strcasecmp($username, $default_user) !== 0); //It is forbidden to delete the default user
  167. }
  168. if ($ok) {
  169. $ok &= is_dir($user_data);
  170. }
  171. if ($ok) {
  172. $userDAO = new FreshRSS_UserDAO();
  173. $ok &= $userDAO->deleteUser($username);
  174. $ok &= recursive_unlink($user_data);
  175. //TODO: delete Persona file
  176. }
  177. invalidateHttpCache();
  178. $notif = array(
  179. 'type' => $ok ? 'good' : 'bad',
  180. 'content' => _t('feedback.user.deleted' . (!$ok ? '.error' : ''), $username)
  181. );
  182. Minz_Session::_param('notification', $notif);
  183. }
  184. Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
  185. }
  186. /**
  187. * This action updates the max number of registrations.
  188. *
  189. * Request parameter is:
  190. * - max-registrations (int >= 0)
  191. */
  192. public function setRegistrationAction() {
  193. if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
  194. $limits = FreshRSS_Context::$system_conf->limits;
  195. $limits['max_registrations'] = Minz_Request::param('max-registrations', 1);
  196. FreshRSS_Context::$system_conf->limits = $limits;
  197. FreshRSS_Context::$system_conf->save();
  198. invalidateHttpCache();
  199. Minz_Session::_param('notification', array(
  200. 'type' => 'good',
  201. 'content' => _t('feedback.user.set_registration')
  202. ));
  203. }
  204. Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
  205. }
  206. }