usersController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. class FreshRSS_users_Controller extends Minz_ActionController {
  3. public function firstAction() {
  4. if (!$this->view->loginOk) {
  5. Minz_Error::error(
  6. 403,
  7. array('error' => array(Minz_Translate::t('access_denied')))
  8. );
  9. }
  10. }
  11. public function authAction() {
  12. if (Minz_Request::isPost()) {
  13. $ok = true;
  14. $passwordPlain = Minz_Request::param('passwordPlain', false);
  15. if ($passwordPlain != '') {
  16. Minz_Request::_param('passwordPlain'); //Discard plain-text password ASAP
  17. $_POST['passwordPlain'] = '';
  18. if (!function_exists('password_hash')) {
  19. include_once(LIB_PATH . '/password_compat.php');
  20. }
  21. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT); //A bit expensive, on purpose
  22. $passwordPlain = '';
  23. $this->view->conf->_passwordHash($passwordHash);
  24. }
  25. $mail = Minz_Request::param('mail_login', false);
  26. $this->view->conf->_mail_login($mail);
  27. $ok &= $this->view->conf->save();
  28. $email = $this->view->conf->mail_login;
  29. Minz_Session::_param('mail', $email);
  30. if ($email != '') {
  31. $personaFile = DATA_PATH . '/persona/' . $email . '.txt';
  32. @unlink($personaFile);
  33. $ok &= (file_put_contents($personaFile, Minz_Session::param('currentUser', '_')) !== false);
  34. }
  35. if (Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
  36. $current_token = $this->view->conf->token;
  37. $token = Minz_Request::param('token', $current_token);
  38. $this->view->conf->_token($token);
  39. $ok &= $this->view->conf->save();
  40. $anon = Minz_Request::param('anon_access', false);
  41. $anon = ((bool)$anon) && ($anon !== 'no');
  42. $auth_type = Minz_Request::param('auth_type', 'none');
  43. if ($anon != Minz_Configuration::allowAnonymous() ||
  44. $auth_type != Minz_Configuration::authType()) {
  45. Minz_Configuration::_allowAnonymous($anon);
  46. Minz_Configuration::_authType($auth_type);
  47. $ok &= Minz_Configuration::writeFile();
  48. }
  49. }
  50. invalidateHttpCache();
  51. $notif = array(
  52. 'type' => $ok ? 'good' : 'bad',
  53. 'content' => Minz_Translate::t($ok ? 'configuration_updated' : 'error_occurred')
  54. );
  55. Minz_Session::_param('notification', $notif);
  56. }
  57. Minz_Request::forward(array('c' => 'configure', 'a' => 'users'), true);
  58. }
  59. public function createAction() {
  60. if (Minz_Request::isPost() && Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
  61. require_once(APP_PATH . '/sql.php');
  62. $new_user_language = Minz_Request::param('new_user_language', $this->view->conf->language);
  63. if (!in_array($new_user_language, $this->view->conf->availableLanguages())) {
  64. $new_user_language = $this->view->conf->language;
  65. }
  66. $new_user_name = Minz_Request::param('new_user_name');
  67. $ok = ($new_user_name != '') && ctype_alnum($new_user_name);
  68. if ($ok) {
  69. $ok &= (strcasecmp($new_user_name, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to alter the default user
  70. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
  71. $configPath = DATA_PATH . '/' . $new_user_name . '_user.php';
  72. $ok &= !file_exists($configPath);
  73. }
  74. if ($ok) {
  75. $new_user_email = filter_var($_POST['new_user_email'], FILTER_VALIDATE_EMAIL);
  76. if (empty($new_user_email)) {
  77. $new_user_email = '';
  78. $ok &= Minz_Configuration::authType() !== 'persona';
  79. } else {
  80. $personaFile = DATA_PATH . '/persona/' . $new_user_email . '.txt';
  81. @unlink($personaFile);
  82. $ok &= (file_put_contents($personaFile, $new_user_name) !== false);
  83. }
  84. }
  85. if ($ok) {
  86. $config_array = array(
  87. 'language' => $new_user_language,
  88. 'mail_login' => $new_user_email,
  89. );
  90. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';') !== false);
  91. }
  92. if ($ok) {
  93. $userDAO = new FreshRSS_UserDAO();
  94. $ok &= $userDAO->createUser($new_user_name);
  95. }
  96. invalidateHttpCache();
  97. $notif = array(
  98. 'type' => $ok ? 'good' : 'bad',
  99. 'content' => Minz_Translate::t($ok ? 'user_created' : 'error_occurred', $new_user_name)
  100. );
  101. Minz_Session::_param('notification', $notif);
  102. }
  103. Minz_Request::forward(array('c' => 'configure', 'a' => 'users'), true);
  104. }
  105. public function deleteAction() {
  106. if (Minz_Request::isPost() && Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
  107. require_once(APP_PATH . '/sql.php');
  108. $username = Minz_Request::param('username');
  109. $ok = ctype_alnum($username);
  110. if ($ok) {
  111. $ok &= (strcasecmp($username, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to delete the default user
  112. }
  113. if ($ok) {
  114. $configPath = DATA_PATH . '/' . $username . '_user.php';
  115. $ok &= file_exists($configPath);
  116. }
  117. if ($ok) {
  118. $userDAO = new FreshRSS_UserDAO();
  119. $ok &= $userDAO->deleteUser($username);
  120. $ok &= unlink($configPath);
  121. //TODO: delete Persona file
  122. }
  123. invalidateHttpCache();
  124. $notif = array(
  125. 'type' => $ok ? 'good' : 'bad',
  126. 'content' => Minz_Translate::t($ok ? 'user_deleted' : 'error_occurred', $username)
  127. );
  128. Minz_Session::_param('notification', $notif);
  129. }
  130. Minz_Request::forward(array('c' => 'configure', 'a' => 'users'), true);
  131. }
  132. }