usersController.php 4.6 KB

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