FreshRSS.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. class FreshRSS extends Minz_FrontController {
  3. /**
  4. * Initialize the different FreshRSS / Minz components.
  5. *
  6. * PLEASE DON'T CHANGE THE ORDER OF INITIALIZATIONS UNLESS YOU KNOW WHAT
  7. * YOU DO!!
  8. *
  9. * Here is the list of components:
  10. * - Create a configuration setter and register it to system conf
  11. * - Init extension manager and enable system extensions (has to be done asap)
  12. * - Init authentication system
  13. * - Init user configuration (need auth system)
  14. * - Init FreshRSS context (need user conf)
  15. * - Init i18n (need context)
  16. * - Init sharing system (need user conf and i18n)
  17. * - Init generic styles and scripts (need user conf)
  18. * - Init notifications
  19. * - Enable user extensions (need all the other initializations)
  20. */
  21. public function init() {
  22. if (!isset($_SESSION)) {
  23. Minz_Session::init('FreshRSS');
  24. }
  25. // Register the configuration setter for the system configuration
  26. $configuration_setter = new FreshRSS_ConfigurationSetter();
  27. $system_conf = Minz_Configuration::get('system');
  28. $system_conf->_configurationSetter($configuration_setter);
  29. // Load list of extensions and enable the "system" ones.
  30. Minz_ExtensionManager::init();
  31. // Auth has to be initialized before using currentUser session parameter
  32. // because it's this part which create this parameter.
  33. self::initAuth();
  34. // Then, register the user configuration and use the configuration setter
  35. // created above.
  36. $current_user = Minz_Session::param('currentUser', '_');
  37. Minz_Configuration::register('user',
  38. join_path(USERS_PATH, $current_user, 'config.php'),
  39. join_path(USERS_PATH, '_', 'config.default.php'),
  40. $configuration_setter);
  41. // Finish to initialize the other FreshRSS / Minz components.
  42. FreshRSS_Context::init();
  43. self::initI18n();
  44. self::loadNotifications();
  45. // Enable extensions for the current (logged) user.
  46. if (FreshRSS_Auth::hasAccess()) {
  47. $ext_list = FreshRSS_Context::$user_conf->extensions_enabled;
  48. Minz_ExtensionManager::enableByList($ext_list);
  49. }
  50. }
  51. private static function initAuth() {
  52. FreshRSS_Auth::init();
  53. if (Minz_Request::isPost() && !is_referer_from_same_domain()) {
  54. // Basic protection against XSRF attacks
  55. FreshRSS_Auth::removeAccess();
  56. $http_referer = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER'];
  57. Minz_Translate::init('en'); //TODO: Better choice of fallback language
  58. Minz_Error::error(
  59. 403,
  60. array('error' => array(
  61. _t('feedback.access.denied'),
  62. ' [HTTP_REFERER=' . htmlspecialchars($http_referer) . ']'
  63. ))
  64. );
  65. }
  66. }
  67. private static function initI18n() {
  68. Minz_Session::_param('language', FreshRSS_Context::$user_conf->language);
  69. Minz_Translate::init(FreshRSS_Context::$user_conf->language);
  70. }
  71. public static function loadStylesAndScripts() {
  72. $theme = FreshRSS_Themes::load(FreshRSS_Context::$user_conf->theme);
  73. if ($theme) {
  74. foreach($theme['files'] as $file) {
  75. if ($file[0] === '_') {
  76. $theme_id = 'base-theme';
  77. $filename = substr($file, 1);
  78. } else {
  79. $theme_id = $theme['id'];
  80. $filename = $file;
  81. }
  82. $filetime = @filemtime(PUBLIC_PATH . '/themes/' . $theme_id . '/' . $filename);
  83. $url = '/themes/' . $theme_id . '/' . $filename . '?' . $filetime;
  84. header('Link: <' . Minz_Url::display($url, '', 'root') . '>;rel=preload', false); //HTTP2
  85. Minz_View::appendStyle(Minz_Url::display($url));
  86. }
  87. }
  88. Minz_View::appendScript(Minz_Url::display('/scripts/jquery.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/jquery.min.js')));
  89. Minz_View::appendScript(Minz_Url::display('/scripts/shortcut.js?' . @filemtime(PUBLIC_PATH . '/scripts/shortcut.js')));
  90. Minz_View::appendScript(Minz_Url::display('/scripts/main.js?' . @filemtime(PUBLIC_PATH . '/scripts/main.js')));
  91. if (FreshRSS_Context::$system_conf->auth_type === 'persona') {
  92. // TODO move it in a plugin
  93. // Needed for login AND logout with Persona.
  94. Minz_View::appendScript('https://login.persona.org/include.js');
  95. $file_mtime = @filemtime(PUBLIC_PATH . '/scripts/persona.js');
  96. Minz_View::appendScript(Minz_Url::display('/scripts/persona.js?' . $file_mtime));
  97. }
  98. }
  99. private static function loadNotifications() {
  100. $notif = Minz_Session::param('notification');
  101. if ($notif) {
  102. Minz_View::_param('notification', $notif);
  103. Minz_Session::_param('notification');
  104. }
  105. }
  106. public static function preLayout() {
  107. switch (Minz_Request::controllerName()) {
  108. case 'index':
  109. header("Content-Security-Policy: default-src 'self'; child-src *; frame-src *; img-src * data:; media-src *");
  110. break;
  111. case 'stats':
  112. header("Content-Security-Policy: default-src 'self'; style-src 'self' 'unsafe-inline'");
  113. break;
  114. default:
  115. header("Content-Security-Policy: default-src 'self'");
  116. break;
  117. }
  118. header("X-Content-Type-Options: nosniff");
  119. FreshRSS_Share::load(join_path(DATA_PATH, 'shares.php'));
  120. self::loadStylesAndScripts();
  121. }
  122. }