4
0

FreshRSS.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS extends Minz_FrontController {
  4. /**
  5. * Initialize the different FreshRSS / Minz components.
  6. *
  7. * PLEASE DON’T CHANGE THE ORDER OF INITIALIZATIONS UNLESS YOU KNOW WHAT 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. * - Enable user extensions (need all the other initializations)
  19. */
  20. public function init(): void {
  21. if (!isset($_SESSION)) {
  22. Minz_Session::init('FreshRSS');
  23. }
  24. FreshRSS_Context::initSystem();
  25. if (!FreshRSS_Context::hasSystemConf()) {
  26. $message = 'Error during context system init!';
  27. Minz_Error::error(500, $message, false);
  28. die($message);
  29. }
  30. if (FreshRSS_Context::systemConf()->logo_html != '') {
  31. // Relax Content Security Policy to allow external images if a custom logo HTML is used
  32. Minz_ActionController::_defaultCsp([
  33. 'default-src' => "'self'",
  34. 'frame-ancestors' => FreshRSS_Context::systemConf()->attributeString('csp.frame-ancestors') ?? "'none'",
  35. 'img-src' => '* data:',
  36. ]);
  37. }
  38. // Load list of extensions and enable the "system" ones.
  39. Minz_ExtensionManager::init();
  40. // Auth has to be initialized before using currentUser session parameter
  41. // because it’s this part which create this parameter.
  42. self::initAuth();
  43. if (!FreshRSS_Context::hasUserConf()) {
  44. FreshRSS_Context::initUser();
  45. }
  46. if (!FreshRSS_Context::hasUserConf()) {
  47. $message = 'Error during context user init!';
  48. Minz_Error::error(500, $message, false);
  49. die($message);
  50. }
  51. // Complete initialization of the other FreshRSS / Minz components.
  52. self::initI18n();
  53. // Enable extensions for the current (logged) user.
  54. if (FreshRSS_Auth::hasAccess() || FreshRSS_Auth::allowAnonymous()) {
  55. $ext_list = FreshRSS_Context::userConf()->extensions_enabled;
  56. Minz_ExtensionManager::enableByList($ext_list, 'user');
  57. }
  58. if (FreshRSS_Context::systemConf()->force_email_validation && !FreshRSS_Auth::hasAccess('admin')) {
  59. self::checkEmailValidated();
  60. }
  61. Minz_ExtensionManager::callHookVoid(Minz_HookType::FreshrssInit);
  62. }
  63. private static function initAuth(): void {
  64. FreshRSS_Auth::init();
  65. if (Minz_Request::isPost()) {
  66. if (!FreshRSS_Context::hasSystemConf() || !FreshRSS_Auth::isCsrfOk()) {
  67. // Token-based protection against CSRF attacks
  68. self::initI18n();
  69. Minz_Error::error(403, ['error' => [_t('feedback.access.denied'), ' [CSRF]']]);
  70. }
  71. }
  72. }
  73. private static function initI18n(): void {
  74. $userLanguage = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf()->language : null;
  75. $systemLanguage = FreshRSS_Context::hasSystemConf() ? FreshRSS_Context::systemConf()->language : null;
  76. $language = Minz_Translate::getLanguage($userLanguage, Minz_Request::getPreferredLanguages(), $systemLanguage);
  77. Minz_Session::_param('language', $language);
  78. Minz_Translate::init($language);
  79. $timezone = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf()->timezone : '';
  80. if ($timezone == '') {
  81. $timezone = FreshRSS_Context::defaultTimeZone();
  82. }
  83. date_default_timezone_set($timezone);
  84. }
  85. private static function getThemeFileUrl(string $theme_id, string $filename): string {
  86. $filetime = @filemtime(PUBLIC_PATH . '/themes/' . $theme_id . '/' . $filename);
  87. return '/themes/' . $theme_id . '/' . $filename . '?' . $filetime;
  88. }
  89. public static function loadStylesAndScripts(): void {
  90. if (!FreshRSS_Context::hasUserConf()) {
  91. return;
  92. }
  93. $theme = FreshRSS_Themes::load(FreshRSS_Context::userConf()->theme);
  94. if (is_array($theme)) {
  95. foreach (array_reverse($theme['files']) as $file) {
  96. switch (substr($file, -3)) {
  97. case '.js':
  98. $theme_id = $theme['id'];
  99. $filename = $file;
  100. FreshRSS_View::prependScript(Minz_Url::display(FreshRSS::getThemeFileUrl($theme_id, $filename)));
  101. break;
  102. case '.css':
  103. default:
  104. if ($file[0] === '_') {
  105. $theme_id = 'base-theme';
  106. $filename = substr($file, 1);
  107. } else {
  108. $theme_id = $theme['id'];
  109. $filename = $file;
  110. }
  111. if (_t('gen.dir') === 'rtl') {
  112. $filename = substr($filename, 0, -4);
  113. $filename = $filename . '.rtl.css';
  114. }
  115. FreshRSS_View::prependStyle(Minz_Url::display(FreshRSS::getThemeFileUrl($theme_id, $filename)));
  116. }
  117. }
  118. if (!empty($theme['theme-color'])) {
  119. FreshRSS_View::appendThemeColors($theme['theme-color']);
  120. }
  121. }
  122. //Use prepend to insert before extensions. Added in reverse order.
  123. if (!in_array(Minz_Request::controllerName(), ['index', ''], true)) {
  124. FreshRSS_View::prependScript(Minz_Url::display('/scripts/extra.js?' . @filemtime(PUBLIC_PATH . '/scripts/extra.js')));
  125. }
  126. FreshRSS_View::prependScript(Minz_Url::display('/scripts/main.js?' . @filemtime(PUBLIC_PATH . '/scripts/main.js')));
  127. }
  128. public static function preLayout(): void {
  129. header('X-Content-Type-Options: nosniff');
  130. FreshRSS_Share::load(join_path(APP_PATH, 'shares.php'));
  131. self::loadStylesAndScripts();
  132. }
  133. private static function checkEmailValidated(): void {
  134. $email_not_verified = FreshRSS_Auth::hasAccess() &&
  135. FreshRSS_Context::hasUserConf() && FreshRSS_Context::userConf()->email_validation_token !== '';
  136. $action_is_allowed = (
  137. Minz_Request::is('user', 'validateEmail') ||
  138. Minz_Request::is('user', 'sendValidationEmail') ||
  139. Minz_Request::is('user', 'profile') ||
  140. Minz_Request::is('user', 'delete') ||
  141. Minz_Request::is('auth', 'logout') ||
  142. Minz_Request::is('feed', 'actualize') ||
  143. Minz_Request::is('javascript', 'nonce') ||
  144. Minz_Request::is('error', 'index')
  145. );
  146. if ($email_not_verified && !$action_is_allowed) {
  147. Minz_Request::forward([
  148. 'c' => 'user',
  149. 'a' => 'validateEmail',
  150. ], true);
  151. }
  152. }
  153. }