FreshRSS.php 4.9 KB

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