Prechádzať zdrojové kódy

First draft for Context object.

See https://github.com/marienfressinaud/FreshRSS/issues/634
Marien Fressinaud 11 rokov pred
rodič
commit
ad92dd7dae
2 zmenil súbory, kde vykonal 52 pridanie a 20 odobranie
  1. 20 20
      app/FreshRSS.php
  2. 32 0
      app/Models/Context.php

+ 20 - 20
app/FreshRSS.php

@@ -1,50 +1,50 @@
 <?php
+
 class FreshRSS extends Minz_FrontController {
 	public function init() {
 		if (!isset($_SESSION)) {
 			Minz_Session::init('FreshRSS');
 		}
 
+		// Need to be called just after session init because it initializes
+		// current user.
 		FreshRSS_Auth::init();
-		$this->loadConfiguration();
-		$this->loadParamsView();
+
 		if (Minz_Request::isPost() && !is_referer_from_same_domain()) {
-			//Basic protection against XSRF attacks
+			// Basic protection against XSRF attacks
 			FreshRSS_Auth::removeAccess();
+			$http_referer = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER'];
 			Minz_Error::error(
 				403,
-				array('error' => array(_t('access_denied') . ' [HTTP_REFERER=' .
-				      htmlspecialchars(empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER']) . ']'))
+				array('error' => array(
+					_t('access_denied'),
+					' [HTTP_REFERER=' . htmlspecialchars($http_referer) . ']'
+				))
 			);
 		}
+
+		// Load context and configuration.
+		// TODO: remove $this->view->conf variable which is contained in context
+		FreshRSS_Context::init();
+		Minz_View::_param('conf', FreshRSS_Context::$conf);
+
+		$this->loadParamsView();
 		$this->loadStylesAndScripts();
 		$this->loadNotifications();
 		$this->loadExtensions();
 	}
 
-	private function loadConfiguration() {
-		$current_user = Minz_Session::param('currentUser');
-		try {
-			$this->conf = new FreshRSS_Configuration($current_user);
-			Minz_View::_param('conf', $this->conf);
-		} catch(Minz_Exception $e) {
-			Minz_Log::error('Cannot load configuration file of user `' . $current_user . '`');
-			die($e->getMessage());
-		}
-	}
-
 	private function loadParamsView() {
-		Minz_Session::_param('language', $this->conf->language);
-		Minz_Translate::init();
+		// TODO: outputs should be different actions.
 		$output = Minz_Request::param('output', '');
 		if (($output === '') || ($output !== 'normal' && $output !== 'rss' && $output !== 'reader' && $output !== 'global')) {
-			$output = $this->conf->view_mode;
+			$output = FreshRSS_Context::$conf->view_mode;
 			Minz_Request::_param('output', $output);
 		}
 	}
 
 	private function loadStylesAndScripts() {
-		$theme = FreshRSS_Themes::load($this->conf->theme);
+		$theme = FreshRSS_Themes::load(FreshRSS_Context::$conf->theme);
 		if ($theme) {
 			foreach($theme['files'] as $file) {
 				if ($file[0] === '_') {

+ 32 - 0
app/Models/Context.php

@@ -0,0 +1,32 @@
+<?php
+
+/**
+ * The context object handles the current configuration file and different
+ * useful functions associated to the current view state.
+ */
+class FreshRSS_Context {
+	public static $conf = null;
+	public static $state = 0;
+
+	public static function init() {
+		// Init configuration.
+		$current_user = Minz_Session::param('currentUser');
+		try {
+			self::$conf = new FreshRSS_Configuration($current_user);
+		} catch(Minz_Exception $e) {
+			Minz_Log::error('Cannot load configuration file of user `' . $current_user . '`');
+			die($e->getMessage());
+		}
+
+		// Init i18n.
+		Minz_Session::_param('language', self::$conf->language);
+		Minz_Translate::init();
+
+		// Get the current state.
+		self::$state = self::$conf->default_view;
+	}
+
+	public static function stateEnabled($state) {
+		return self::$state & $state;
+	}
+}