FreshRSS.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. $this->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(USERS_PATH, '_', 'config.default.php'),
  40. $configuration_setter);
  41. // Finish to initialize the other FreshRSS / Minz components.
  42. FreshRSS_Context::init();
  43. $this->initI18n();
  44. FreshRSS_Share::load(join_path(DATA_PATH, 'shares.php'));
  45. $this->loadStylesAndScripts();
  46. $this->loadNotifications();
  47. // Enable extensions for the current (logged) user.
  48. if (FreshRSS_Auth::hasAccess()) {
  49. $ext_list = FreshRSS_Context::$user_conf->extensions_enabled;
  50. Minz_ExtensionManager::enableByList($ext_list);
  51. }
  52. }
  53. private function initAuth() {
  54. FreshRSS_Auth::init();
  55. if (Minz_Request::isPost() && !is_referer_from_same_domain()) {
  56. // Basic protection against XSRF attacks
  57. FreshRSS_Auth::removeAccess();
  58. $http_referer = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER'];
  59. Minz_Translate::init('en'); //TODO: Better choice of fallback language
  60. Minz_Error::error(
  61. 403,
  62. array('error' => array(
  63. _t('feedback.access.denied'),
  64. ' [HTTP_REFERER=' . htmlspecialchars($http_referer) . ']'
  65. ))
  66. );
  67. }
  68. }
  69. private function initI18n() {
  70. Minz_Session::_param('language', FreshRSS_Context::$user_conf->language);
  71. Minz_Translate::init(FreshRSS_Context::$user_conf->language);
  72. }
  73. private function loadStylesAndScripts() {
  74. $theme = FreshRSS_Themes::load(FreshRSS_Context::$user_conf->theme);
  75. if ($theme) {
  76. foreach($theme['files'] as $file) {
  77. if ($file[0] === '_') {
  78. $theme_id = 'base-theme';
  79. $filename = substr($file, 1);
  80. } else {
  81. $theme_id = $theme['id'];
  82. $filename = $file;
  83. }
  84. $filetime = @filemtime(PUBLIC_PATH . '/themes/' . $theme_id . '/' . $filename);
  85. Minz_View::appendStyle(Minz_Url::display(
  86. '/themes/' . $theme_id . '/' . $filename . '?' . $filetime
  87. ));
  88. }
  89. }
  90. Minz_View::appendScript(Minz_Url::display('/scripts/jquery.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/jquery.min.js')));
  91. Minz_View::appendScript(Minz_Url::display('/scripts/shortcut.js?' . @filemtime(PUBLIC_PATH . '/scripts/shortcut.js')));
  92. Minz_View::appendScript(Minz_Url::display('/scripts/main.js?' . @filemtime(PUBLIC_PATH . '/scripts/main.js')));
  93. if (FreshRSS_Context::$system_conf->auth_type === 'persona') {
  94. // TODO move it in a plugin
  95. // Needed for login AND logout with Persona.
  96. Minz_View::appendScript('https://login.persona.org/include.js');
  97. $file_mtime = @filemtime(PUBLIC_PATH . '/scripts/persona.js');
  98. Minz_View::appendScript(Minz_Url::display('/scripts/persona.js?' . $file_mtime));
  99. }
  100. }
  101. private static function setJavascriptCookie() {
  102. $mark = FreshRSS_Context::$user_conf->mark_when;
  103. $mail = Minz_Session::param('mail', false);
  104. $s = FreshRSS_Context::$user_conf->shortcuts;
  105. $json = json_encode(array(
  106. 'context' => array(
  107. 'auto_remove_article' => !!FreshRSS_Context::isAutoRemoveAvailable(),
  108. 'hide_posts' => !(FreshRSS_Context::$user_conf->display_posts || Minz_Request::actionName() === 'reader'),
  109. 'display_order' => Minz_Request::param('order', FreshRSS_Context::$user_conf->sort_order),
  110. 'auto_mark_article' => !!$mark['article'],
  111. 'auto_mark_site' => !!$mark['site'],
  112. 'auto_mark_scroll' => !!$mark['scroll'],
  113. 'auto_load_more' => !!FreshRSS_Context::$user_conf->auto_load_more,
  114. 'auto_actualize_feeds' => !!Minz_Session::param('actualize_feeds', false),
  115. 'does_lazyload' => !!FreshRSS_Context::$user_conf->lazyload ,
  116. 'sticky_post' => !!FreshRSS_Context::isStickyPostEnabled(),
  117. 'html5_notif_timeout' => FreshRSS_Context::$user_conf->html5_notif_timeout,
  118. 'auth_type' => FreshRSS_Context::$system_conf->auth_type,
  119. 'current_user_mail' => $mail ? ('"' . $mail . '"') : null,
  120. 'current_view' => Minz_Request::actionName(),
  121. ),
  122. 'shortcuts' => array(
  123. 'mark_read' => @$s['mark_read'],
  124. 'mark_favorite' => @$s['mark_favorite'],
  125. 'go_website' => @$s['go_website'],
  126. 'prev_entry' => @$s['prev_entry'],
  127. 'next_entry' => @$s['next_entry'],
  128. 'first_entry' => @$s['first_entry'],
  129. 'last_entry' => @$s['last_entry'],
  130. 'collapse_entry' => @$s['collapse_entry'],
  131. 'load_more' => @$s['load_more'],
  132. 'auto_share' => @$s['auto_share'],
  133. 'focus_search' => @$s['focus_search'],
  134. 'user_filter' => @$s['user_filter'],
  135. 'help' => @$s['help'],
  136. 'close_dropdown' => @$s['close_dropdown'],
  137. ),
  138. 'url' => array(
  139. 'index' => _url('index', 'index'),
  140. 'login' => Minz_Url::display(array('c' => 'auth', 'a' => 'login'), 'php'),
  141. 'logout' => Minz_Url::display(array('c' => 'auth', 'a' => 'logout'), 'php'),
  142. 'help' => FRESHRSS_WIKI,
  143. ),
  144. 'i18n' => array(
  145. 'confirmation_default' => _t('gen.js.confirm_action'),
  146. 'notif_title_articles' => _t('gen.js.feedback.title_new_articles'),
  147. 'notif_body_articles' => _t('gen.js.feedback.body_new_articles'),
  148. 'notif_request_failed' => _t('gen.js.feedback.request_failed'),
  149. 'category_empty' => _t('gen.js.category_empty'),
  150. ),
  151. 'icons' => array(
  152. 'close' => _i('close'),
  153. ),
  154. ), JSON_UNESCAPED_UNICODE);
  155. setrawcookie('FreshRSS-vars', rawurlencode($json), 0, Minz_Session::getCookieDir());
  156. }
  157. public static function preLayout() {
  158. header("Content-Security-Policy: default-src 'self'; child-src *; img-src * data:; media-src *; style-src 'self' 'unsafe-inline'");
  159. self::setJavascriptCookie();
  160. }
  161. private function loadNotifications() {
  162. $notif = Minz_Session::param('notification');
  163. if ($notif) {
  164. Minz_View::_param('notification', $notif);
  165. Minz_Session::_param('notification');
  166. }
  167. }
  168. }