usersController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 idAction() {
  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. //TODO: use $ok
  25. $notif = array(
  26. 'type' => 'good',
  27. 'content' => Minz_Translate::t('configuration_updated')
  28. );
  29. Minz_Session::_param('notification', $notif);
  30. Minz_Request::forward(array('c' => 'configure', 'a' => 'users'), true);
  31. }
  32. }
  33. public function authAction() {
  34. if (Minz_Request::isPost() && Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
  35. $ok = true;
  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. $notif = array(
  50. 'type' => $ok ? 'good' : 'bad',
  51. 'content' => Minz_Translate::t($ok ? 'configuration_updated' : 'error_occurred')
  52. );
  53. Minz_Session::_param('notification', $notif);
  54. }
  55. Minz_Request::forward(array('c' => 'configure', 'a' => 'users'), true);
  56. }
  57. public function createAction() {
  58. if (Minz_Request::isPost() && Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
  59. require_once(APP_PATH . '/sql.php');
  60. $new_user_language = Minz_Request::param('new_user_language', $this->view->conf->language);
  61. if (!in_array($new_user_language, $this->view->conf->availableLanguages())) {
  62. $new_user_language = $this->view->conf->language;
  63. }
  64. $new_user_name = Minz_Request::param('new_user_name');
  65. $ok = ($new_user_name != '') && ctype_alnum($new_user_name);
  66. if ($ok) {
  67. $ok &= (strcasecmp($new_user_name, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to alter the default user
  68. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
  69. $configPath = DATA_PATH . '/' . $new_user_name . '_user.php';
  70. $ok &= !file_exists($configPath);
  71. }
  72. if ($ok) {
  73. $new_user_email = filter_var($_POST['new_user_email'], FILTER_VALIDATE_EMAIL);
  74. if (empty($new_user_email)) {
  75. $new_user_email = '';
  76. $ok &= Minz_Configuration::authType() !== 'persona';
  77. } else {
  78. $personaFile = DATA_PATH . '/persona/' . $new_user_email . '.txt';
  79. @unlink($personaFile);
  80. $ok &= (file_put_contents($personaFile, $new_user_name) !== false);
  81. }
  82. }
  83. if ($ok) {
  84. $config_array = array(
  85. 'language' => $new_user_language,
  86. 'mail_login' => $new_user_email,
  87. );
  88. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';') !== false);
  89. }
  90. if ($ok) {
  91. $userDAO = new FreshRSS_UserDAO();
  92. $ok &= $userDAO->createUser($new_user_name);
  93. }
  94. $notif = array(
  95. 'type' => $ok ? 'good' : 'bad',
  96. 'content' => Minz_Translate::t($ok ? 'user_created' : 'error_occurred', $new_user_name)
  97. );
  98. Minz_Session::_param('notification', $notif);
  99. }
  100. Minz_Request::forward(array('c' => 'configure', 'a' => 'users'), true);
  101. }
  102. public function deleteAction() {
  103. if (Minz_Request::isPost() && Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
  104. require_once(APP_PATH . '/sql.php');
  105. $username = Minz_Request::param('username');
  106. $ok = ctype_alnum($username);
  107. if ($ok) {
  108. $ok &= (strcasecmp($username, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to delete the default user
  109. }
  110. if ($ok) {
  111. $configPath = DATA_PATH . '/' . $username . '_user.php';
  112. $ok &= file_exists($configPath);
  113. }
  114. if ($ok) {
  115. $userDAO = new FreshRSS_UserDAO();
  116. $ok &= $userDAO->deleteUser($username);
  117. $ok &= unlink($configPath);
  118. //TODO: delete Persona file
  119. }
  120. $notif = array(
  121. 'type' => $ok ? 'good' : 'bad',
  122. 'content' => Minz_Translate::t($ok ? 'user_deleted' : 'error_occurred', $username)
  123. );
  124. Minz_Session::_param('notification', $notif);
  125. }
  126. Minz_Request::forward(array('c' => 'configure', 'a' => 'users'), true);
  127. }
  128. }