FreshRSS.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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(_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. $this->loadExtensions();
  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. if ($file[0] === '_') {
  141. $theme_id = 'base-theme';
  142. $filename = substr($file, 1);
  143. } else {
  144. $theme_id = $theme['id'];
  145. $filename = $file;
  146. }
  147. $filetime = @filemtime(PUBLIC_PATH . '/themes/' . $theme_id . '/' . $filename);
  148. Minz_View::appendStyle(Minz_Url::display(
  149. '/themes/' . $theme_id . '/' . $filename . '?' . $filetime
  150. ));
  151. }
  152. }
  153. switch (Minz_Configuration::authType()) {
  154. case 'form':
  155. if (!$loginOk) {
  156. Minz_View::appendScript(Minz_Url::display ('/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')));
  157. }
  158. break;
  159. case 'persona':
  160. Minz_View::appendScript('https://login.persona.org/include.js');
  161. break;
  162. }
  163. Minz_View::appendScript(Minz_Url::display('/scripts/jquery.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/jquery.min.js')));
  164. Minz_View::appendScript(Minz_Url::display('/scripts/shortcut.js?' . @filemtime(PUBLIC_PATH . '/scripts/shortcut.js')));
  165. Minz_View::appendScript(Minz_Url::display('/scripts/main.js?' . @filemtime(PUBLIC_PATH . '/scripts/main.js')));
  166. }
  167. private function loadNotifications () {
  168. $notif = Minz_Session::param ('notification');
  169. if ($notif) {
  170. Minz_View::_param ('notification', $notif);
  171. Minz_Session::_param ('notification');
  172. }
  173. }
  174. private function loadExtensions() {
  175. $extensionPath = FRESHRSS_PATH . '/extensions/';
  176. //TODO: Add a preference to load only user-selected extensions
  177. foreach (scandir($extensionPath) as $key => $extension) {
  178. if (ctype_alpha($extension)) {
  179. $mtime = @filemtime($extensionPath . $extension . '/style.css');
  180. if ($mtime !== false) {
  181. Minz_View::appendStyle(Minz_Url::display('/ext.php?c&amp;e=' . $extension . '&amp;' . $mtime));
  182. }
  183. $mtime = @filemtime($extensionPath . $extension . '/script.js');
  184. if ($mtime !== false) {
  185. Minz_View::appendScript(Minz_Url::display('/ext.php?j&amp;e=' . $extension . '&amp;' . $mtime));
  186. }
  187. if (file_exists($extensionPath . $extension . '/module.php')) {
  188. //TODO: include
  189. }
  190. }
  191. }
  192. }
  193. }