FreshRSS.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_Context::systemConf()->allow_anonymous) {
  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. (Minz_Request::controllerName() === 'auth' && Minz_Request::actionName() === 'login') ||
  68. (Minz_Request::controllerName() === 'user' && Minz_Request::actionName() === 'create' && !FreshRSS_Auth::hasAccess('admin')) ||
  69. (Minz_Request::controllerName() === 'feed' && Minz_Request::actionName() === 'actualize' &&
  70. FreshRSS_Context::systemConf()->allow_anonymous_refresh) ||
  71. (Minz_Request::controllerName() === 'javascript' && Minz_Request::actionName() === 'actualize' &&
  72. FreshRSS_Context::systemConf()->allow_anonymous)
  73. )) {
  74. // Token-based protection against XSRF attacks, except for the login or self-create user forms
  75. self::initI18n();
  76. Minz_Error::error(403, ['error' => [_t('feedback.access.denied'), ' [CSRF]']]);
  77. }
  78. }
  79. }
  80. private static function initI18n(): void {
  81. $userLanguage = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf()->language : null;
  82. $systemLanguage = FreshRSS_Context::hasSystemConf() ? FreshRSS_Context::systemConf()->language : null;
  83. $language = Minz_Translate::getLanguage($userLanguage, Minz_Request::getPreferredLanguages(), $systemLanguage);
  84. Minz_Session::_param('language', $language);
  85. Minz_Translate::init($language);
  86. $timezone = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf()->timezone : '';
  87. if ($timezone == '') {
  88. $timezone = FreshRSS_Context::defaultTimeZone();
  89. }
  90. date_default_timezone_set($timezone);
  91. }
  92. private static function getThemeFileUrl(string $theme_id, string $filename): string {
  93. $filetime = @filemtime(PUBLIC_PATH . '/themes/' . $theme_id . '/' . $filename);
  94. return '/themes/' . $theme_id . '/' . $filename . '?' . $filetime;
  95. }
  96. public static function loadStylesAndScripts(): void {
  97. if (!FreshRSS_Context::hasUserConf()) {
  98. return;
  99. }
  100. $theme = FreshRSS_Themes::load(FreshRSS_Context::userConf()->theme);
  101. if (is_array($theme)) {
  102. foreach (array_reverse($theme['files']) as $file) {
  103. switch (substr($file, -3)) {
  104. case '.js':
  105. $theme_id = $theme['id'];
  106. $filename = $file;
  107. FreshRSS_View::prependScript(Minz_Url::display(FreshRSS::getThemeFileUrl($theme_id, $filename)));
  108. break;
  109. case '.css':
  110. default:
  111. if ($file[0] === '_') {
  112. $theme_id = 'base-theme';
  113. $filename = substr($file, 1);
  114. } else {
  115. $theme_id = $theme['id'];
  116. $filename = $file;
  117. }
  118. if (_t('gen.dir') === 'rtl') {
  119. $filename = substr($filename, 0, -4);
  120. $filename = $filename . '.rtl.css';
  121. }
  122. FreshRSS_View::prependStyle(Minz_Url::display(FreshRSS::getThemeFileUrl($theme_id, $filename)));
  123. }
  124. }
  125. if (!empty($theme['theme-color'])) {
  126. FreshRSS_View::appendThemeColors($theme['theme-color']);
  127. }
  128. }
  129. //Use prepend to insert before extensions. Added in reverse order.
  130. if (!in_array(Minz_Request::controllerName(), ['index', ''], true)) {
  131. FreshRSS_View::prependScript(Minz_Url::display('/scripts/extra.js?' . @filemtime(PUBLIC_PATH . '/scripts/extra.js')));
  132. }
  133. FreshRSS_View::prependScript(Minz_Url::display('/scripts/main.js?' . @filemtime(PUBLIC_PATH . '/scripts/main.js')));
  134. }
  135. public static function preLayout(): void {
  136. header('X-Content-Type-Options: nosniff');
  137. FreshRSS_Share::load(join_path(APP_PATH, 'shares.php'));
  138. self::loadStylesAndScripts();
  139. }
  140. private static function checkEmailValidated(): void {
  141. $email_not_verified = FreshRSS_Auth::hasAccess() &&
  142. FreshRSS_Context::hasUserConf() && FreshRSS_Context::userConf()->email_validation_token !== '';
  143. $action_is_allowed = (
  144. Minz_Request::is('user', 'validateEmail') ||
  145. Minz_Request::is('user', 'sendValidationEmail') ||
  146. Minz_Request::is('user', 'profile') ||
  147. Minz_Request::is('user', 'delete') ||
  148. Minz_Request::is('auth', 'logout') ||
  149. Minz_Request::is('feed', 'actualize') ||
  150. Minz_Request::is('javascript', 'nonce')
  151. );
  152. if ($email_not_verified && !$action_is_allowed) {
  153. Minz_Request::forward([
  154. 'c' => 'user',
  155. 'a' => 'validateEmail',
  156. ], true);
  157. }
  158. }
  159. }