FreshRSS.php 6.7 KB

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