FreshRSS.php 5.1 KB

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