FreshRSS.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. class FreshRSS extends Minz_FrontController {
  3. public function init() {
  4. if (!isset($_SESSION)) {
  5. Minz_Session::init('FreshRSS');
  6. }
  7. $loginOk = $this->accessControl(Minz_Session::param('currentUser', ''));
  8. $this->loadParamsView();
  9. if (Minz_Request::isPost() && (empty($_SERVER['HTTP_REFERER']) ||
  10. Minz_Request::getDomainName() !== parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST))) {
  11. $loginOk = false; //Basic protection against XSRF attacks
  12. Minz_Error::error(
  13. 403,
  14. array('error' => array(Minz_Translate::t('access_denied') . ' [HTTP_REFERER=' .
  15. htmlspecialchars(empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER']) . ']'))
  16. );
  17. }
  18. Minz_View::_param('loginOk', $loginOk);
  19. $this->loadStylesAndScripts($loginOk); //TODO: Do not load that when not needed, e.g. some Ajax requests
  20. $this->loadNotifications();
  21. }
  22. private function accessControl($currentUser) {
  23. if ($currentUser == '') {
  24. switch (Minz_Configuration::authType()) {
  25. case 'form':
  26. $currentUser = Minz_Configuration::defaultUser();
  27. Minz_Session::_param('passwordHash');
  28. $loginOk = false;
  29. break;
  30. case 'http_auth':
  31. $currentUser = httpAuthUser();
  32. $loginOk = $currentUser != '';
  33. break;
  34. case 'persona':
  35. $loginOk = false;
  36. $email = filter_var(Minz_Session::param('mail'), FILTER_VALIDATE_EMAIL);
  37. if ($email != '') { //TODO: Remove redundancy with indexController
  38. $personaFile = DATA_PATH . '/persona/' . $email . '.txt';
  39. if (($currentUser = @file_get_contents($personaFile)) !== false) {
  40. $currentUser = trim($currentUser);
  41. $loginOk = true;
  42. }
  43. }
  44. if (!$loginOk) {
  45. $currentUser = Minz_Configuration::defaultUser();
  46. }
  47. break;
  48. case 'none':
  49. $currentUser = Minz_Configuration::defaultUser();
  50. $loginOk = true;
  51. break;
  52. default:
  53. $currentUser = Minz_Configuration::defaultUser();
  54. $loginOk = false;
  55. break;
  56. }
  57. } else {
  58. $loginOk = true;
  59. }
  60. if (!ctype_alnum($currentUser)) {
  61. Minz_Session::_param('currentUser', '');
  62. die('Invalid username [' . $currentUser . ']!');
  63. }
  64. try {
  65. $this->conf = new FreshRSS_Configuration($currentUser);
  66. Minz_View::_param ('conf', $this->conf);
  67. Minz_Session::_param('currentUser', $currentUser);
  68. } catch (Minz_Exception $me) {
  69. $loginOk = false;
  70. try {
  71. $this->conf = new FreshRSS_Configuration(Minz_Configuration::defaultUser());
  72. Minz_Session::_param('currentUser', Minz_Configuration::defaultUser());
  73. Minz_View::_param('conf', $this->conf);
  74. $notif = array(
  75. 'type' => 'bad',
  76. 'content' => 'Invalid configuration for user [' . $currentUser . ']!',
  77. );
  78. Minz_Session::_param ('notification', $notif);
  79. Minz_Log::record ($notif['content'] . ' ' . $me->getMessage(), Minz_Log::WARNING);
  80. Minz_Session::_param('currentUser', '');
  81. } catch (Exception $e) {
  82. die($e->getMessage());
  83. }
  84. }
  85. if ($loginOk) {
  86. switch (Minz_Configuration::authType()) {
  87. case 'form':
  88. $loginOk = Minz_Session::param('passwordHash') === $this->conf->passwordHash;
  89. break;
  90. case 'http_auth':
  91. $loginOk = strcasecmp($currentUser, httpAuthUser()) === 0;
  92. break;
  93. case 'persona':
  94. $loginOk = strcasecmp(Minz_Session::param('mail'), $this->conf->mail_login) === 0;
  95. break;
  96. case 'none':
  97. $loginOk = true;
  98. break;
  99. default:
  100. $loginOk = false;
  101. break;
  102. }
  103. }
  104. return $loginOk;
  105. }
  106. private function loadParamsView () {
  107. Minz_Session::_param ('language', $this->conf->language);
  108. Minz_Translate::init();
  109. $output = Minz_Request::param ('output', '');
  110. if (($output === '') || ($output !== 'normal' && $output !== 'rss' && $output !== 'reader' && $output !== 'global')) {
  111. $output = $this->conf->view_mode;
  112. Minz_Request::_param ('output', $output);
  113. }
  114. }
  115. private function loadStylesAndScripts ($loginOk) {
  116. $theme = FreshRSS_Themes::load($this->conf->theme);
  117. if ($theme) {
  118. foreach($theme['files'] as $file) {
  119. Minz_View::appendStyle (Minz_Url::display ('/themes/' . $theme['id'] . '/' . $file . '?' . @filemtime(PUBLIC_PATH . '/themes/' . $theme['id'] . '/' . $file)));
  120. }
  121. }
  122. switch (Minz_Configuration::authType()) {
  123. case 'form':
  124. if (!$loginOk) {
  125. Minz_View::appendScript(Minz_Url::display ('/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')));
  126. }
  127. break;
  128. case 'persona':
  129. Minz_View::appendScript('https://login.persona.org/include.js');
  130. break;
  131. }
  132. Minz_View::appendScript(Minz_Url::display('/scripts/jquery.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/jquery.min.js')));
  133. Minz_View::appendScript(Minz_Url::display('/scripts/shortcut.js?' . @filemtime(PUBLIC_PATH . '/scripts/shortcut.js')));
  134. Minz_View::appendScript(Minz_Url::display('/scripts/main.js?' . @filemtime(PUBLIC_PATH . '/scripts/main.js')));
  135. }
  136. private function loadNotifications () {
  137. $notif = Minz_Session::param ('notification');
  138. if ($notif) {
  139. Minz_View::_param ('notification', $notif);
  140. Minz_Session::_param ('notification');
  141. }
  142. }
  143. }