usersController.php 6.4 KB

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