FreshRSS.php 5.9 KB

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