4
0

FreshRSS.php 6.4 KB

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