usersController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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', false);
  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', false);
  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', false));
  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. require_once(APP_PATH . '/sql.php');
  88. $new_user_language = Minz_Request::param('new_user_language', $this->view->conf->language);
  89. if (!in_array($new_user_language, $this->view->conf->availableLanguages())) {
  90. $new_user_language = $this->view->conf->language;
  91. }
  92. $new_user_name = Minz_Request::param('new_user_name');
  93. $ok = ($new_user_name != '') && ctype_alnum($new_user_name);
  94. if ($ok) {
  95. $ok &= (strcasecmp($new_user_name, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to alter the default user
  96. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
  97. $configPath = DATA_PATH . '/' . $new_user_name . '_user.php';
  98. $ok &= !file_exists($configPath);
  99. }
  100. if ($ok) {
  101. $passwordPlain = Minz_Request::param('new_user_passwordPlain', false);
  102. $passwordHash = '';
  103. if ($passwordPlain != '') {
  104. Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
  105. $_POST['new_user_passwordPlain'] = '';
  106. if (!function_exists('password_hash')) {
  107. include_once(LIB_PATH . '/password_compat.php');
  108. }
  109. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
  110. $passwordPlain = '';
  111. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
  112. $ok &= ($passwordHash != '');
  113. }
  114. if (empty($passwordHash)) {
  115. $passwordHash = '';
  116. }
  117. $new_user_email = filter_var($_POST['new_user_email'], FILTER_VALIDATE_EMAIL);
  118. if (empty($new_user_email)) {
  119. $new_user_email = '';
  120. } else {
  121. $personaFile = DATA_PATH . '/persona/' . $new_user_email . '.txt';
  122. @unlink($personaFile);
  123. $ok &= (file_put_contents($personaFile, $new_user_name) !== false);
  124. }
  125. }
  126. if ($ok) {
  127. $config_array = array(
  128. 'language' => $new_user_language,
  129. 'passwordHash' => $passwordHash,
  130. 'mail_login' => $new_user_email,
  131. );
  132. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';') !== false);
  133. }
  134. if ($ok) {
  135. $userDAO = new FreshRSS_UserDAO();
  136. $ok &= $userDAO->createUser($new_user_name);
  137. }
  138. invalidateHttpCache();
  139. $notif = array(
  140. 'type' => $ok ? 'good' : 'bad',
  141. 'content' => Minz_Translate::t($ok ? 'user_created' : 'error_occurred', $new_user_name)
  142. );
  143. Minz_Session::_param('notification', $notif);
  144. }
  145. Minz_Request::forward(array('c' => 'configure', 'a' => 'users'), true);
  146. }
  147. public function deleteAction() {
  148. if (Minz_Request::isPost() && Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
  149. require_once(APP_PATH . '/sql.php');
  150. $username = Minz_Request::param('username');
  151. $ok = ctype_alnum($username);
  152. if ($ok) {
  153. $ok &= (strcasecmp($username, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to delete the default user
  154. }
  155. if ($ok) {
  156. $configPath = DATA_PATH . '/' . $username . '_user.php';
  157. $ok &= file_exists($configPath);
  158. }
  159. if ($ok) {
  160. $userDAO = new FreshRSS_UserDAO();
  161. $ok &= $userDAO->deleteUser($username);
  162. $ok &= unlink($configPath);
  163. //TODO: delete Persona file
  164. }
  165. invalidateHttpCache();
  166. $notif = array(
  167. 'type' => $ok ? 'good' : 'bad',
  168. 'content' => Minz_Translate::t($ok ? 'user_deleted' : 'error_occurred', $username)
  169. );
  170. Minz_Session::_param('notification', $notif);
  171. }
  172. Minz_Request::forward(array('c' => 'configure', 'a' => 'users'), true);
  173. }
  174. }