usersController.php 5.9 KB

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