FreshRSS.php 6.6 KB

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