FreshRSS.php 5.9 KB

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