usersController.php 4.2 KB

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