usersController.php 7.5 KB

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