usersController.php 6.2 KB

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