FreshRSS.php 5.9 KB

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