FreshRSS.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 static function getCredentialsFromLongTermCookie() {
  23. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  24. if (!ctype_alnum($token)) {
  25. return array();
  26. }
  27. $tokenFile = DATA_PATH . '/tokens/' . $token . '.txt';
  28. $mtime = @filemtime($tokenFile);
  29. if ($mtime + 2629744 < time()) { //1 month //TODO: Use a configuration instead
  30. @unlink($tokenFile);
  31. return array(); //Expired or token does not exist
  32. }
  33. $credentials = @file_get_contents($tokenFile);
  34. return $credentials === false ? array() : explode("\t", $credentials, 2);
  35. }
  36. private function accessControl($currentUser) {
  37. if ($currentUser == '') {
  38. switch (Minz_Configuration::authType()) {
  39. case 'form':
  40. $credentials = self::getCredentialsFromLongTermCookie();
  41. if (isset($credentials[1])) {
  42. $currentUser = trim($credentials[0]);
  43. Minz_Session::_param('passwordHash', trim($credentials[1]));
  44. }
  45. $loginOk = $currentUser != '';
  46. if (!$loginOk) {
  47. $currentUser = Minz_Configuration::defaultUser();
  48. Minz_Session::_param('passwordHash');
  49. }
  50. break;
  51. case 'http_auth':
  52. $currentUser = httpAuthUser();
  53. $loginOk = $currentUser != '';
  54. break;
  55. case 'persona':
  56. $loginOk = false;
  57. $email = filter_var(Minz_Session::param('mail'), FILTER_VALIDATE_EMAIL);
  58. if ($email != '') { //TODO: Remove redundancy with indexController
  59. $personaFile = DATA_PATH . '/persona/' . $email . '.txt';
  60. if (($currentUser = @file_get_contents($personaFile)) !== false) {
  61. $currentUser = trim($currentUser);
  62. $loginOk = true;
  63. }
  64. }
  65. if (!$loginOk) {
  66. $currentUser = Minz_Configuration::defaultUser();
  67. }
  68. break;
  69. case 'none':
  70. $currentUser = Minz_Configuration::defaultUser();
  71. $loginOk = true;
  72. break;
  73. default:
  74. $currentUser = Minz_Configuration::defaultUser();
  75. $loginOk = false;
  76. break;
  77. }
  78. } else {
  79. $loginOk = true;
  80. }
  81. if (!ctype_alnum($currentUser)) {
  82. Minz_Session::_param('currentUser', '');
  83. die('Invalid username [' . $currentUser . ']!');
  84. }
  85. try {
  86. $this->conf = new FreshRSS_Configuration($currentUser);
  87. Minz_View::_param ('conf', $this->conf);
  88. Minz_Session::_param('currentUser', $currentUser);
  89. } catch (Minz_Exception $me) {
  90. $loginOk = false;
  91. try {
  92. $this->conf = new FreshRSS_Configuration(Minz_Configuration::defaultUser());
  93. Minz_Session::_param('currentUser', Minz_Configuration::defaultUser());
  94. Minz_View::_param('conf', $this->conf);
  95. $notif = array(
  96. 'type' => 'bad',
  97. 'content' => 'Invalid configuration for user [' . $currentUser . ']!',
  98. );
  99. Minz_Session::_param ('notification', $notif);
  100. Minz_Log::record ($notif['content'] . ' ' . $me->getMessage(), Minz_Log::WARNING);
  101. Minz_Session::_param('currentUser', '');
  102. } catch (Exception $e) {
  103. die($e->getMessage());
  104. }
  105. }
  106. if ($loginOk) {
  107. switch (Minz_Configuration::authType()) {
  108. case 'form':
  109. $loginOk = Minz_Session::param('passwordHash') === $this->conf->passwordHash;
  110. break;
  111. case 'http_auth':
  112. $loginOk = strcasecmp($currentUser, httpAuthUser()) === 0;
  113. break;
  114. case 'persona':
  115. $loginOk = strcasecmp(Minz_Session::param('mail'), $this->conf->mail_login) === 0;
  116. break;
  117. case 'none':
  118. $loginOk = true;
  119. break;
  120. default:
  121. $loginOk = false;
  122. break;
  123. }
  124. }
  125. return $loginOk;
  126. }
  127. private function loadParamsView () {
  128. Minz_Session::_param ('language', $this->conf->language);
  129. Minz_Translate::init();
  130. $output = Minz_Request::param ('output', '');
  131. if (($output === '') || ($output !== 'normal' && $output !== 'rss' && $output !== 'reader' && $output !== 'global')) {
  132. $output = $this->conf->view_mode;
  133. Minz_Request::_param ('output', $output);
  134. }
  135. }
  136. private function loadStylesAndScripts ($loginOk) {
  137. $theme = FreshRSS_Themes::load($this->conf->theme);
  138. if ($theme) {
  139. foreach($theme['files'] as $file) {
  140. Minz_View::appendStyle (Minz_Url::display ('/themes/' . $theme['id'] . '/' . $file . '?' . @filemtime(PUBLIC_PATH . '/themes/' . $theme['id'] . '/' . $file)));
  141. }
  142. }
  143. switch (Minz_Configuration::authType()) {
  144. case 'form':
  145. if (!$loginOk) {
  146. Minz_View::appendScript(Minz_Url::display ('/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')));
  147. }
  148. break;
  149. case 'persona':
  150. Minz_View::appendScript('https://login.persona.org/include.js');
  151. break;
  152. }
  153. Minz_View::appendScript(Minz_Url::display('/scripts/jquery.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/jquery.min.js')));
  154. Minz_View::appendScript(Minz_Url::display('/scripts/shortcut.js?' . @filemtime(PUBLIC_PATH . '/scripts/shortcut.js')));
  155. Minz_View::appendScript(Minz_Url::display('/scripts/main.js?' . @filemtime(PUBLIC_PATH . '/scripts/main.js')));
  156. }
  157. private function loadNotifications () {
  158. $notif = Minz_Session::param ('notification');
  159. if ($notif) {
  160. Minz_View::_param ('notification', $notif);
  161. Minz_Session::_param ('notification');
  162. }
  163. }
  164. }