Browse Source

Creating Friendica\App\Mode for encapsulating the App Mode

tags/2019.01
Philipp Holzer 9 months ago
parent
commit
065340dc32
No account linked to committer's email address

+ 1
- 1
bin/auth_ejabberd.php View File

@@ -54,7 +54,7 @@ require_once "include/dba.php";
54 54
 
55 55
 $a = new App(dirname(__DIR__));
56 56
 
57
-if ($a->mode === App::MODE_NORMAL) {
57
+if (App\Mode::isNormal()) {
58 58
 	$oAuth = new ExAuth();
59 59
 	$oAuth->readStdin();
60 60
 }

+ 6
- 6
index.php View File

@@ -36,7 +36,7 @@ $a->backend = false;
36 36
 require_once "include/dba.php";
37 37
 
38 38
 // Missing DB connection: ERROR
39
-if ($a->mode & App::MODE_LOCALCONFIGPRESENT && !($a->mode & App::MODE_DBAVAILABLE)) {
39
+if (App\Mode::has(App\Mode::LOCALCONFIGPRESENT) && !App\Mode::has(App\Mode::DBAVAILABLE)) {
40 40
 	System::httpExit(500, ['title' => 'Error 500 - Internal Server Error', 'description' => 'Apologies but the website is unavailable at the moment.']);
41 41
 }
42 42
 
@@ -48,7 +48,7 @@ if ($a->isMaxProcessesReached() || $a->isMaxLoadReached()) {
48 48
 	System::httpExit(503, ['title' => 'Error 503 - Service Temporarily Unavailable', 'description' => 'System is currently overloaded. Please try again later.']);
49 49
 }
50 50
 
51
-if (!$a->isInstallMode()) {
51
+if (!App\Mode::isInstall()) {
52 52
 	if (Config::get('system', 'force_ssl') && ($a->get_scheme() == "http")
53 53
 		&& (intval(Config::get('system', 'ssl_policy')) == SSL_POLICY_FULL)
54 54
 		&& (substr(System::baseUrl(), 0, 8) == "https://")
@@ -107,7 +107,7 @@ if (!empty($_SESSION['language']) && $_SESSION['language'] !== $lang) {
107 107
 	L10n::loadTranslationTable($lang);
108 108
 }
109 109
 
110
-if (!empty($_GET['zrl']) && $a->mode == App::MODE_NORMAL) {
110
+if (!empty($_GET['zrl']) && App\Mode::isNormal()) {
111 111
 	$a->query_string = Profile::stripZrls($a->query_string);
112 112
 	if (!local_user()) {
113 113
 		// Only continue when the given profile link seems valid
@@ -130,7 +130,7 @@ if (!empty($_GET['zrl']) && $a->mode == App::MODE_NORMAL) {
130 130
 	}
131 131
 }
132 132
 
133
-if (!empty($_GET['owt']) && $a->mode == App::MODE_NORMAL) {
133
+if (!empty($_GET['owt']) && App\Mode::isNormal()) {
134 134
 	$token = $_GET['owt'];
135 135
 	$a->query_string = Profile::stripQueryParam($a->query_string, 'owt');
136 136
 	Profile::openWebAuthInit($token);
@@ -167,7 +167,7 @@ $_SESSION['last_updated'] = defaults($_SESSION, 'last_updated', []);
167 167
 // but we need "view" module for stylesheet
168 168
 if ($a->isInstallMode() && $a->module!="view") {
169 169
 	$a->module = 'install';
170
-} elseif (!($a->mode & App::MODE_MAINTENANCEDISABLED) && $a->module != "view") {
170
+} elseif (!App\Mode::has(App\Mode::MAINTENANCEDISABLED) && $a->module != "view") {
171 171
 	$a->module = 'maintenance';
172 172
 } else {
173 173
 	check_url($a);
@@ -320,7 +320,7 @@ if (file_exists($theme_info_file)) {
320 320
 
321 321
 /* initialise content region */
322 322
 
323
-if ($a->mode == App::MODE_NORMAL) {
323
+if (App\Mode::isNormal()) {
324 324
 	Addon::callHooks('page_content_top', $a->page['content']);
325 325
 }
326 326
 

+ 4
- 68
src/App.php View File

@@ -6,6 +6,7 @@ namespace Friendica;
6 6
 
7 7
 use Detection\MobileDetect;
8 8
 use Exception;
9
+use Friendica\App;
9 10
 use Friendica\Core\Config;
10 11
 use Friendica\Core\L10n;
11 12
 use Friendica\Core\PConfig;
@@ -31,21 +32,6 @@ require_once 'include/text.php';
31 32
  */
32 33
 class App
33 34
 {
34
-	const MODE_LOCALCONFIGPRESENT = 1;
35
-	const MODE_DBAVAILABLE = 2;
36
-	const MODE_DBCONFIGAVAILABLE = 4;
37
-	const MODE_MAINTENANCEDISABLED = 8;
38
-
39
-	/**
40
-	 * @deprecated since version 2008.08 Use App->isInstallMode() instead to check for install mode.
41
-	 */
42
-	const MODE_INSTALL = 0;
43
-
44
-	/**
45
-	 * @deprecated since version 2008.08 Use the precise mode constant to check for a specific capability instead.
46
-	 */
47
-	const MODE_NORMAL = App::MODE_LOCALCONFIGPRESENT | App::MODE_DBAVAILABLE | App::MODE_DBCONFIGAVAILABLE | App::MODE_MAINTENANCEDISABLED;
48
-
49 35
 	public $module_loaded = false;
50 36
 	public $module_class = null;
51 37
 	public $query_string = '';
@@ -67,7 +53,6 @@ class App
67 53
 	public $argv;
68 54
 	public $argc;
69 55
 	public $module;
70
-	public $mode = App::MODE_INSTALL;
71 56
 	public $strings;
72 57
 	public $basepath;
73 58
 	public $urlpath;
@@ -326,13 +311,13 @@ class App
326 311
 
327 312
 		$this->loadDatabase();
328 313
 
329
-		$this->determineMode();
314
+		App\Mode::determine($this->basepath);
330 315
 
331 316
 		$this->determineUrlPath();
332 317
 
333 318
 		Config::load();
334 319
 
335
-		if ($this->mode & self::MODE_DBAVAILABLE) {
320
+		if (App\Mode::has(App\Mode::DBAVAILABLE)) {
336 321
 			Core\Addon::loadHooks();
337 322
 
338 323
 			$this->loadAddonConfig();
@@ -518,45 +503,6 @@ class App
518 503
 		}
519 504
 	}
520 505
 
521
-	/**
522
-	 * Sets the App mode
523
-	 *
524
-	 * - App::MODE_INSTALL    : Either the database connection can't be established or the config table doesn't exist
525
-	 * - App::MODE_MAINTENANCE: The maintenance mode has been set
526
-	 * - App::MODE_NORMAL     : Normal run with all features enabled
527
-	 *
528
-	 * @return type
529
-	 */
530
-	private function determineMode()
531
-	{
532
-		$this->mode = 0;
533
-
534
-		if (!file_exists($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')
535
-			&& !file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) {
536
-			return;
537
-		}
538
-
539
-		$this->mode |= App::MODE_LOCALCONFIGPRESENT;
540
-
541
-		if (!DBA::connected()) {
542
-			return;
543
-		}
544
-
545
-		$this->mode |= App::MODE_DBAVAILABLE;
546
-
547
-		if (DBA::fetchFirst("SHOW TABLES LIKE 'config'") === false) {
548
-			return;
549
-		}
550
-
551
-		$this->mode |= App::MODE_DBCONFIGAVAILABLE;
552
-
553
-		if (Config::get('system', 'maintenance')) {
554
-			return;
555
-		}
556
-
557
-		$this->mode |= App::MODE_MAINTENANCEDISABLED;
558
-	}
559
-
560 506
 	public function loadDatabase()
561 507
 	{
562 508
 		if (DBA::connected()) {
@@ -596,16 +542,6 @@ class App
596 542
 		$this->save_timestamp($stamp1, 'network');
597 543
 	}
598 544
 
599
-	/**
600
-	 * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
601
-	 *
602
-	 * @return bool
603
-	 */
604
-	public function isInstallMode()
605
-	{
606
-		return !($this->mode & App::MODE_LOCALCONFIGPRESENT) || !($this->mode & App::MODE_DBCONFIGAVAILABLE);
607
-	}
608
-
609 545
 	/**
610 546
 	 * @brief Returns the base filesystem path of the App
611 547
 	 *
@@ -1467,7 +1403,7 @@ class App
1467 1403
 	 */
1468 1404
 	public function getCurrentTheme()
1469 1405
 	{
1470
-		if ($this->isInstallMode()) {
1406
+		if (App\Mode::isInstall()) {
1471 1407
 			return '';
1472 1408
 		}
1473 1409
 

+ 103
- 0
src/App/Mode.php View File

@@ -0,0 +1,103 @@
1
+<?php
2
+
3
+namespace Friendica\App;
4
+
5
+use Friendica\Core\Config;
6
+use Friendica\Database\DBA;
7
+
8
+/**
9
+ * Mode of the current Friendica Node
10
+ *
11
+ * @package Friendica\App
12
+ */
13
+class Mode
14
+{
15
+	const LOCALCONFIGPRESENT = 1;
16
+	const DBAVAILABLE = 2;
17
+	const DBCONFIGAVAILABLE = 4;
18
+	const MAINTENANCEDISABLED = 8;
19
+
20
+	/***
21
+	 * @var int the mode of this Application
22
+	 *
23
+	 * Default is 0 (= not set)
24
+	 */
25
+	private static $mode = 0;
26
+
27
+	/**
28
+	 * Sets the App mode
29
+	 *
30
+	 * - App::MODE_INSTALL    : Either the database connection can't be established or the config table doesn't exist
31
+	 * - App::MODE_MAINTENANCE: The maintenance mode has been set
32
+	 * - App::MODE_NORMAL     : Normal run with all features enabled
33
+	 *
34
+	 * @param string $basepath the Basepath of the Application
35
+	 *
36
+	 */
37
+	public static function determine($basepath)
38
+	{
39
+		self::$mode = 0;
40
+
41
+		if (!file_exists($basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')
42
+			&& !file_exists($basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) {
43
+			return;
44
+		}
45
+
46
+		self::$mode |= Mode::LOCALCONFIGPRESENT;
47
+
48
+		if (!DBA::connected()) {
49
+			return;
50
+		}
51
+
52
+		self::$mode |= Mode::DBAVAILABLE;
53
+
54
+		if (DBA::fetchFirst("SHOW TABLES LIKE 'config'") === false) {
55
+			return;
56
+		}
57
+
58
+		self::$mode |= Mode::DBCONFIGAVAILABLE;
59
+
60
+		if (Config::get('system', 'maintenance')) {
61
+			return;
62
+		}
63
+
64
+		self::$mode |= Mode::MAINTENANCEDISABLED;
65
+	}
66
+
67
+	/**
68
+	 * Checks, if the Friendica Node has the given mode
69
+	 *
70
+	 * @param int $mode A mode to test
71
+	 *
72
+	 * @return bool returns true, if the mode is set
73
+	 */
74
+	public static function has($mode)
75
+	{
76
+		return self::$mode & $mode;
77
+	}
78
+
79
+
80
+	/**
81
+	 * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
82
+	 *
83
+	 * @return bool
84
+	 */
85
+	public static function isInstall()
86
+	{
87
+		return !self::has(Mode::LOCALCONFIGPRESENT) ||
88
+			!self::has(MODE::DBCONFIGAVAILABLE);
89
+	}
90
+
91
+	/**
92
+	 * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
93
+	 *
94
+	 * @return bool
95
+	 */
96
+	public static function isNormal()
97
+	{
98
+		return self::has(Mode::LOCALCONFIGPRESENT) &&
99
+			self::has(Mode::DBAVAILABLE) &&
100
+			self::has(Mode::DBCONFIGAVAILABLE) &&
101
+			self::has(Mode::MAINTENANCEDISABLED);
102
+	}
103
+}

+ 5
- 5
src/Core/Config.php View File

@@ -31,7 +31,7 @@ class Config extends BaseObject
31 31
 	public static function init()
32 32
 	{
33 33
 		// Database isn't ready or populated yet
34
-		if (!(self::getApp()->mode & App::MODE_DBCONFIGAVAILABLE)) {
34
+		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
35 35
 			return;
36 36
 		}
37 37
 
@@ -55,7 +55,7 @@ class Config extends BaseObject
55 55
 	public static function load($family = "config")
56 56
 	{
57 57
 		// Database isn't ready or populated yet
58
-		if (!(self::getApp()->mode & App::MODE_DBCONFIGAVAILABLE)) {
58
+		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
59 59
 			return;
60 60
 		}
61 61
 
@@ -88,7 +88,7 @@ class Config extends BaseObject
88 88
 	public static function get($family, $key, $default_value = null, $refresh = false)
89 89
 	{
90 90
 		// Database isn't ready or populated yet, fallback to file config
91
-		if (!(self::getApp()->mode & App::MODE_DBCONFIGAVAILABLE)) {
91
+		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
92 92
 			return self::getApp()->getConfigValue($family, $key, $default_value);
93 93
 		}
94 94
 
@@ -116,7 +116,7 @@ class Config extends BaseObject
116 116
 	public static function set($family, $key, $value)
117 117
 	{
118 118
 		// Database isn't ready or populated yet
119
-		if (!(self::getApp()->mode & App::MODE_DBCONFIGAVAILABLE)) {
119
+		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
120 120
 			return false;
121 121
 		}
122 122
 
@@ -141,7 +141,7 @@ class Config extends BaseObject
141 141
 	public static function delete($family, $key)
142 142
 	{
143 143
 		// Database isn't ready or populated yet
144
-		if (!(self::getApp()->mode & App::MODE_DBCONFIGAVAILABLE)) {
144
+		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
145 145
 			return false;
146 146
 		}
147 147
 

+ 1
- 1
src/Core/Console/ArchiveContact.php View File

@@ -56,7 +56,7 @@ HELP;
56 56
 			throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
57 57
 		}
58 58
 
59
-		if ($a->mode === App::MODE_INSTALL) {
59
+		if (App\Mode::isInstall()) {
60 60
 			throw new RuntimeException('Friendica isn\'t properly installed yet.');
61 61
 		}
62 62
 

+ 1
- 1
src/Core/Console/Cache.php View File

@@ -65,7 +65,7 @@ HELP;
65 65
 			$this->out('Options: ' . var_export($this->options, true));
66 66
 		}
67 67
 
68
-		if (!($a->mode & App::MODE_DBCONFIGAVAILABLE)) {
68
+		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
69 69
 			$this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
70 70
 		}
71 71
 

+ 2
- 2
src/Core/Console/Config.php View File

@@ -84,7 +84,7 @@ HELP;
84 84
 			throw new CommandArgsException('Too many arguments');
85 85
 		}
86 86
 
87
-		if (!($a->mode & App::MODE_DBCONFIGAVAILABLE)) {
87
+		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
88 88
 			$this->out('Database isn\'t ready or populated yet, showing file config only');
89 89
 		}
90 90
 
@@ -143,7 +143,7 @@ HELP;
143 143
 		if (count($this->args) == 0) {
144 144
 			Core\Config::load();
145 145
 
146
-			if (Core\Config::get('system', 'config_adapter') == 'jit' && $a->mode & App::MODE_DBCONFIGAVAILABLE) {
146
+			if (Core\Config::get('system', 'config_adapter') == 'jit' && App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
147 147
 				$this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
148 148
 			}
149 149
 

+ 5
- 5
src/Core/PConfig.php View File

@@ -30,7 +30,7 @@ class PConfig extends BaseObject
30 30
 	public static function init($uid)
31 31
 	{
32 32
 		// Database isn't ready or populated yet
33
-		if (!(self::getApp()->mode & App::MODE_DBCONFIGAVAILABLE)) {
33
+		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
34 34
 			return;
35 35
 		}
36 36
 
@@ -55,7 +55,7 @@ class PConfig extends BaseObject
55 55
 	public static function load($uid, $family)
56 56
 	{
57 57
 		// Database isn't ready or populated yet
58
-		if (!(self::getApp()->mode & App::MODE_DBCONFIGAVAILABLE)) {
58
+		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
59 59
 			return;
60 60
 		}
61 61
 
@@ -84,7 +84,7 @@ class PConfig extends BaseObject
84 84
 	public static function get($uid, $family, $key, $default_value = null, $refresh = false)
85 85
 	{
86 86
 		// Database isn't ready or populated yet
87
-		if (!(self::getApp()->mode & App::MODE_DBCONFIGAVAILABLE)) {
87
+		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
88 88
 			return;
89 89
 		}
90 90
 
@@ -113,7 +113,7 @@ class PConfig extends BaseObject
113 113
 	public static function set($uid, $family, $key, $value)
114 114
 	{
115 115
 		// Database isn't ready or populated yet
116
-		if (!(self::getApp()->mode & App::MODE_DBCONFIGAVAILABLE)) {
116
+		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
117 117
 			return false;
118 118
 		}
119 119
 
@@ -139,7 +139,7 @@ class PConfig extends BaseObject
139 139
 	public static function delete($uid, $family, $key)
140 140
 	{
141 141
 		// Database isn't ready or populated yet
142
-		if (!(self::getApp()->mode & App::MODE_DBCONFIGAVAILABLE)) {
142
+		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
143 143
 			return false;
144 144
 		}
145 145
 

Loading…
Cancel
Save