FreshRSS.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // Register the configuration setter for the system configuration
  26. $configuration_setter = new FreshRSS_ConfigurationSetter();
  27. $system_conf = Minz_Configuration::get('system');
  28. $system_conf->_configurationSetter($configuration_setter);
  29. // Load list of extensions and enable the "system" ones.
  30. Minz_ExtensionManager::init();
  31. // Auth has to be initialized before using currentUser session parameter
  32. // because it's this part which create this parameter.
  33. self::initAuth();
  34. // Then, register the user configuration and use the configuration setter
  35. // created above.
  36. $current_user = Minz_Session::param('currentUser', '_');
  37. Minz_Configuration::register('user',
  38. join_path(USERS_PATH, $current_user, 'config.php'),
  39. join_path(FRESHRSS_PATH, 'config-user.default.php'),
  40. $configuration_setter);
  41. // Finish to initialize the other FreshRSS / Minz components.
  42. FreshRSS_Context::init();
  43. self::initI18n();
  44. self::loadNotifications();
  45. // Enable extensions for the current (logged) user.
  46. if (FreshRSS_Auth::hasAccess() || $system_conf->allow_anonymous) {
  47. $ext_list = FreshRSS_Context::$user_conf->extensions_enabled;
  48. Minz_ExtensionManager::enableByList($ext_list);
  49. }
  50. if ($system_conf->force_email_validation && !FreshRSS_Auth::hasAccess('admin')) {
  51. self::checkEmailValidated();
  52. }
  53. Minz_ExtensionManager::callHook('freshrss_init');
  54. }
  55. private static function initAuth() {
  56. FreshRSS_Auth::init();
  57. if (Minz_Request::isPost()) {
  58. if (!is_referer_from_same_domain()) {
  59. // Basic protection against XSRF attacks
  60. FreshRSS_Auth::removeAccess();
  61. $http_referer = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER'];
  62. self::initI18n();
  63. Minz_Error::error(403, array('error' => array(
  64. _t('feedback.access.denied'),
  65. ' [HTTP_REFERER=' . htmlspecialchars($http_referer, ENT_NOQUOTES, 'UTF-8') . ']'
  66. )));
  67. }
  68. if (!(FreshRSS_Auth::isCsrfOk() ||
  69. (Minz_Request::controllerName() === 'auth' && Minz_Request::actionName() === 'login') ||
  70. (Minz_Request::controllerName() === 'user' && Minz_Request::actionName() === 'create' &&
  71. !FreshRSS_Auth::hasAccess('admin'))
  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, array('error' => array(
  76. _t('feedback.access.denied'),
  77. ' [CSRF]'
  78. )));
  79. }
  80. }
  81. }
  82. private static function initI18n() {
  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. }
  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. if ($file[0] === '_') {
  94. $theme_id = 'base-theme';
  95. $filename = substr($file, 1);
  96. } else {
  97. $theme_id = $theme['id'];
  98. $filename = $file;
  99. }
  100. if (_t('gen.dir') === 'rtl') {
  101. $filename = substr($filename, 0, -4);
  102. $filename = $filename . '.rtl.css';
  103. }
  104. $filetime = @filemtime(PUBLIC_PATH . '/themes/' . $theme_id . '/' . $filename);
  105. $url = '/themes/' . $theme_id . '/' . $filename . '?' . $filetime;
  106. Minz_View::prependStyle(Minz_Url::display($url));
  107. }
  108. }
  109. //Use prepend to insert before extensions. Added in reverse order.
  110. if (Minz_Request::controllerName() !== 'index') {
  111. Minz_View::prependScript(Minz_Url::display('/scripts/extra.js?' . @filemtime(PUBLIC_PATH . '/scripts/extra.js')));
  112. }
  113. Minz_View::prependScript(Minz_Url::display('/scripts/main.js?' . @filemtime(PUBLIC_PATH . '/scripts/main.js')));
  114. }
  115. private static function loadNotifications() {
  116. $notif = Minz_Request::getNotification();
  117. if ($notif) {
  118. Minz_View::_param('notification', $notif);
  119. }
  120. }
  121. public static function preLayout() {
  122. header("X-Content-Type-Options: nosniff");
  123. FreshRSS_Share::load(join_path(APP_PATH, 'shares.php'));
  124. self::loadStylesAndScripts();
  125. }
  126. private static function checkEmailValidated() {
  127. $email_not_verified = FreshRSS_Auth::hasAccess() && FreshRSS_Context::$user_conf->email_validation_token !== '';
  128. $action_is_allowed = (
  129. Minz_Request::is('user', 'validateEmail') ||
  130. Minz_Request::is('user', 'sendValidationEmail') ||
  131. Minz_Request::is('user', 'profile') ||
  132. Minz_Request::is('user', 'delete') ||
  133. Minz_Request::is('auth', 'logout') ||
  134. Minz_Request::is('feed', 'actualize') ||
  135. Minz_Request::is('javascript', 'nonce')
  136. );
  137. if ($email_not_verified && !$action_is_allowed) {
  138. Minz_Request::forward(array(
  139. 'c' => 'user',
  140. 'a' => 'validateEmail',
  141. ), true);
  142. }
  143. }
  144. }