FreshRSS.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. class FreshRSS extends Minz_FrontController {
  3. public function init() {
  4. if (!isset($_SESSION)) {
  5. Minz_Session::init('FreshRSS');
  6. }
  7. $this->accessControl(Minz_Session::param('currentUser', ''));
  8. $this->loadParamsView();
  9. $this->loadStylesAndScripts(); //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 'http_auth':
  16. $currentUser = httpAuthUser();
  17. $loginOk = $currentUser != '';
  18. break;
  19. case 'persona':
  20. $loginOk = false;
  21. $email = filter_var(Minz_Session::param('mail'), FILTER_VALIDATE_EMAIL);
  22. if ($email != '') { //TODO: Remove redundancy with indexController
  23. $personaFile = DATA_PATH . '/persona/' . $email . '.txt';
  24. if (($currentUser = @file_get_contents($personaFile)) !== false) {
  25. $currentUser = trim($currentUser);
  26. $loginOk = true;
  27. }
  28. }
  29. if (!$loginOk) {
  30. $currentUser = Minz_Configuration::defaultUser();
  31. }
  32. break;
  33. case 'none':
  34. $currentUser = Minz_Configuration::defaultUser();
  35. $loginOk = true;
  36. break;
  37. default:
  38. $currentUser = Minz_Configuration::defaultUser();
  39. $loginOk = false;
  40. break;
  41. }
  42. } else {
  43. $loginOk = true;
  44. }
  45. if (!ctype_alnum($currentUser)) {
  46. Minz_Session::_param('currentUser', '');
  47. die('Invalid username [' . $currentUser . ']!');
  48. }
  49. try {
  50. $this->conf = new FreshRSS_Configuration($currentUser);
  51. Minz_View::_param ('conf', $this->conf);
  52. Minz_Session::_param('currentUser', $currentUser);
  53. } catch (Minz_Exception $me) {
  54. $loginOk = false;
  55. try {
  56. $this->conf = new FreshRSS_Configuration(Minz_Configuration::defaultUser());
  57. Minz_Session::_param('currentUser', Minz_Configuration::defaultUser());
  58. Minz_View::_param('conf', $this->conf);
  59. $notif = array(
  60. 'type' => 'bad',
  61. 'content' => 'Invalid configuration for user [' . $currentUser . ']!',
  62. );
  63. Minz_Session::_param ('notification', $notif);
  64. Minz_Log::record ($notif['content'] . ' ' . $me->getMessage(), Minz_Log::WARNING);
  65. Minz_Session::_param('currentUser', '');
  66. } catch (Exception $e) {
  67. die($e->getMessage());
  68. }
  69. }
  70. if ($loginOk) {
  71. switch (Minz_Configuration::authType()) {
  72. case 'http_auth':
  73. $loginOk = strcasecmp($currentUser, httpAuthUser()) === 0;
  74. break;
  75. case 'persona':
  76. $loginOk = strcasecmp(Minz_Session::param('mail'), $this->conf->mail_login) === 0;
  77. break;
  78. case 'none':
  79. $loginOk = true;
  80. break;
  81. default:
  82. $loginOk = false;
  83. break;
  84. }
  85. if ((!$loginOk) && (PHP_SAPI === 'cli') && (Minz_Request::actionName() === 'actualize')) { //Command line
  86. Minz_Configuration::_authType('none');
  87. $loginOk = true;
  88. }
  89. }
  90. Minz_View::_param ('loginOk', $loginOk);
  91. }
  92. private function loadParamsView () {
  93. Minz_Session::_param ('language', $this->conf->language);
  94. Minz_Translate::init();
  95. $output = Minz_Request::param ('output');
  96. if (!$output) {
  97. $output = $this->conf->view_mode;
  98. Minz_Request::_param ('output', $output);
  99. }
  100. }
  101. private function loadStylesAndScripts () {
  102. $theme = FreshRSS_Themes::get_infos($this->conf->theme);
  103. if ($theme) {
  104. foreach($theme['files'] as $file) {
  105. Minz_View::appendStyle (Minz_Url::display ('/themes/' . $theme['path'] . '/' . $file . '?' . @filemtime(PUBLIC_PATH . '/themes/' . $theme['path'] . '/' . $file)));
  106. }
  107. }
  108. if (Minz_Configuration::authType() === 'persona') {
  109. Minz_View::appendScript ('https://login.persona.org/include.js');
  110. }
  111. $includeLazyLoad = $this->conf->lazyload && ($this->conf->display_posts || Minz_Request::param ('output') === 'reader');
  112. Minz_View::appendScript (Minz_Url::display ('/scripts/jquery.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/jquery.min.js')), false, !$includeLazyLoad, !$includeLazyLoad);
  113. if ($includeLazyLoad) {
  114. Minz_View::appendScript (Minz_Url::display ('/scripts/jquery.lazyload.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/jquery.lazyload.min.js')));
  115. }
  116. Minz_View::appendScript (Minz_Url::display ('/scripts/main.js?' . @filemtime(PUBLIC_PATH . '/scripts/main.js')));
  117. }
  118. private function loadNotifications () {
  119. $notif = Minz_Session::param ('notification');
  120. if ($notif) {
  121. Minz_View::_param ('notification', $notif);
  122. Minz_Session::_param ('notification');
  123. }
  124. }
  125. }