4
0

usersController.php 7.0 KB

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