]> git.mxchange.org Git - friendica.git/blob - src/App/Mode.php
Merge pull request #5833 from MrPetovan/develop
[friendica.git] / src / App / Mode.php
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 }