usersController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. class FreshRSS_users_Controller extends Minz_ActionController {
  3. const BCRYPT_COST = 9; //Will also have to be computed client side on mobile devices, so do not use a too high cost
  4. public function firstAction() {
  5. if (!FreshRSS_Auth::hasAccess()) {
  6. Minz_Error::error(
  7. 403,
  8. array('error' => array(_t('access_denied')))
  9. );
  10. }
  11. }
  12. /**
  13. * This action display the user configuration page
  14. */
  15. public function indexAction() {
  16. Minz_View::prependTitle(_t('users') . ' · ');
  17. }
  18. public function authAction() {
  19. if (Minz_Request::isPost()) {
  20. $ok = true;
  21. $passwordPlain = Minz_Request::param('passwordPlain', '', true);
  22. if ($passwordPlain != '') {
  23. Minz_Request::_param('passwordPlain'); //Discard plain-text password ASAP
  24. $_POST['passwordPlain'] = '';
  25. if (!function_exists('password_hash')) {
  26. include_once(LIB_PATH . '/password_compat.php');
  27. }
  28. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
  29. $passwordPlain = '';
  30. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
  31. $ok &= ($passwordHash != '');
  32. $this->view->conf->_passwordHash($passwordHash);
  33. }
  34. Minz_Session::_param('passwordHash', $this->view->conf->passwordHash);
  35. $passwordPlain = Minz_Request::param('apiPasswordPlain', '', true);
  36. if ($passwordPlain != '') {
  37. if (!function_exists('password_hash')) {
  38. include_once(LIB_PATH . '/password_compat.php');
  39. }
  40. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
  41. $passwordPlain = '';
  42. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
  43. $ok &= ($passwordHash != '');
  44. $this->view->conf->_apiPasswordHash($passwordHash);
  45. }
  46. if (FreshRSS_Auth::hasAccess('admin')) {
  47. $this->view->conf->_mail_login(Minz_Request::param('mail_login', '', true));
  48. }
  49. $email = $this->view->conf->mail_login;
  50. Minz_Session::_param('mail', $email);
  51. $ok &= $this->view->conf->save();
  52. if ($email != '') {
  53. $personaFile = DATA_PATH . '/persona/' . $email . '.txt';
  54. @unlink($personaFile);
  55. $ok &= (file_put_contents($personaFile, Minz_Session::param('currentUser', '_')) !== false);
  56. }
  57. if (FreshRSS_Auth::hasAccess('admin')) {
  58. $current_token = $this->view->conf->token;
  59. $token = Minz_Request::param('token', $current_token);
  60. $this->view->conf->_token($token);
  61. $ok &= $this->view->conf->save();
  62. $anon = Minz_Request::param('anon_access', false);
  63. $anon = ((bool)$anon) && ($anon !== 'no');
  64. $anon_refresh = Minz_Request::param('anon_refresh', false);
  65. $anon_refresh = ((bool)$anon_refresh) && ($anon_refresh !== 'no');
  66. $auth_type = Minz_Request::param('auth_type', 'none');
  67. $unsafe_autologin = Minz_Request::param('unsafe_autologin', false);
  68. $api_enabled = Minz_Request::param('api_enabled', false);
  69. if ($anon != Minz_Configuration::allowAnonymous() ||
  70. $auth_type != Minz_Configuration::authType() ||
  71. $anon_refresh != Minz_Configuration::allowAnonymousRefresh() ||
  72. $unsafe_autologin != Minz_Configuration::unsafeAutologinEnabled() ||
  73. $api_enabled != Minz_Configuration::apiEnabled()) {
  74. Minz_Configuration::_authType($auth_type);
  75. Minz_Configuration::_allowAnonymous($anon);
  76. Minz_Configuration::_allowAnonymousRefresh($anon_refresh);
  77. Minz_Configuration::_enableAutologin($unsafe_autologin);
  78. Minz_Configuration::_enableApi($api_enabled);
  79. $ok &= Minz_Configuration::writeFile();
  80. }
  81. }
  82. invalidateHttpCache();
  83. $notif = array(
  84. 'type' => $ok ? 'good' : 'bad',
  85. 'content' => _t($ok ? 'configuration_updated' : 'error_occurred')
  86. );
  87. Minz_Session::_param('notification', $notif);
  88. }
  89. Minz_Request::forward(array('c' => 'users', 'a' => 'index'), true);
  90. }
  91. public function createAction() {
  92. if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
  93. $db = Minz_Configuration::dataBase();
  94. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  95. $new_user_language = Minz_Request::param('new_user_language', $this->view->conf->language);
  96. if (!in_array($new_user_language, $this->view->conf->availableLanguages())) {
  97. $new_user_language = $this->view->conf->language;
  98. }
  99. $new_user_name = Minz_Request::param('new_user_name');
  100. $ok = ($new_user_name != '') && ctype_alnum($new_user_name);
  101. if ($ok) {
  102. $ok &= (strcasecmp($new_user_name, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to alter the default user
  103. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
  104. $configPath = DATA_PATH . '/' . $new_user_name . '_user.php';
  105. $ok &= !file_exists($configPath);
  106. }
  107. if ($ok) {
  108. $passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
  109. $passwordHash = '';
  110. if ($passwordPlain != '') {
  111. Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
  112. $_POST['new_user_passwordPlain'] = '';
  113. if (!function_exists('password_hash')) {
  114. include_once(LIB_PATH . '/password_compat.php');
  115. }
  116. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
  117. $passwordPlain = '';
  118. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
  119. $ok &= ($passwordHash != '');
  120. }
  121. if (empty($passwordHash)) {
  122. $passwordHash = '';
  123. }
  124. $new_user_email = filter_var($_POST['new_user_email'], FILTER_VALIDATE_EMAIL);
  125. if (empty($new_user_email)) {
  126. $new_user_email = '';
  127. } else {
  128. $personaFile = DATA_PATH . '/persona/' . $new_user_email . '.txt';
  129. @unlink($personaFile);
  130. $ok &= (file_put_contents($personaFile, $new_user_name) !== false);
  131. }
  132. }
  133. if ($ok) {
  134. $config_array = array(
  135. 'language' => $new_user_language,
  136. 'passwordHash' => $passwordHash,
  137. 'mail_login' => $new_user_email,
  138. );
  139. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';') !== false);
  140. }
  141. if ($ok) {
  142. $userDAO = new FreshRSS_UserDAO();
  143. $ok &= $userDAO->createUser($new_user_name);
  144. }
  145. invalidateHttpCache();
  146. $notif = array(
  147. 'type' => $ok ? 'good' : 'bad',
  148. 'content' => _t($ok ? 'user_created' : 'error_occurred', $new_user_name)
  149. );
  150. Minz_Session::_param('notification', $notif);
  151. }
  152. Minz_Request::forward(array('c' => 'users', 'a' => 'index'), true);
  153. }
  154. public function deleteAction() {
  155. if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
  156. $db = Minz_Configuration::dataBase();
  157. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  158. $username = Minz_Request::param('username');
  159. $ok = ctype_alnum($username);
  160. if ($ok) {
  161. $ok &= (strcasecmp($username, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to delete the default user
  162. }
  163. if ($ok) {
  164. $configPath = DATA_PATH . '/' . $username . '_user.php';
  165. $ok &= file_exists($configPath);
  166. }
  167. if ($ok) {
  168. $userDAO = new FreshRSS_UserDAO();
  169. $ok &= $userDAO->deleteUser($username);
  170. $ok &= unlink($configPath);
  171. //TODO: delete Persona file
  172. }
  173. invalidateHttpCache();
  174. $notif = array(
  175. 'type' => $ok ? 'good' : 'bad',
  176. 'content' => _t($ok ? 'user_deleted' : 'error_occurred', $username)
  177. );
  178. Minz_Session::_param('notification', $notif);
  179. }
  180. Minz_Request::forward(array('c' => 'users', 'a' => 'index'), true);
  181. }
  182. }