]> git.mxchange.org Git - friendica.git/blob - src/App/Mode.php
Merge pull request #6076 from nupplaphil/cache_test_fix
[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          */
24         private $mode;
25
26         /**
27          * @var string the basepath of the application
28          */
29         private $basepath;
30
31         public function __construct($basepath = '')
32         {
33                 $this->basepath = $basepath;
34                 $this->mode = 0;
35         }
36
37         /**
38          * Sets the App mode
39          *
40          * - App::MODE_INSTALL    : Either the database connection can't be established or the config table doesn't exist
41          * - App::MODE_MAINTENANCE: The maintenance mode has been set
42          * - App::MODE_NORMAL     : Normal run with all features enabled
43          *
44          * @param string $basepath the Basepath of the Application
45          *
46          */
47         public function determine($basepath = null)
48         {
49                 if (!empty($basepath)) {
50                         $this->basepath = $basepath;
51                 }
52
53                 $this->mode = 0;
54
55                 if (!file_exists($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')
56                         && !file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) {
57                         return;
58                 }
59
60                 $this->mode |= Mode::LOCALCONFIGPRESENT;
61
62                 if (!DBA::connected()) {
63                         return;
64                 }
65
66                 $this->mode |= Mode::DBAVAILABLE;
67
68                 if (DBA::fetchFirst("SHOW TABLES LIKE 'config'") === false) {
69                         return;
70                 }
71
72                 $this->mode |= Mode::DBCONFIGAVAILABLE;
73
74                 if (Config::get('system', 'maintenance')) {
75                         return;
76                 }
77
78                 $this->mode |= Mode::MAINTENANCEDISABLED;
79         }
80
81         /**
82          * Checks, if the Friendica Node has the given mode
83          *
84          * @param int $mode A mode to test
85          *
86          * @return bool returns true, if the mode is set
87          */
88         public function has($mode)
89         {
90                 return ($this->mode & $mode) > 0;
91         }
92
93
94         /**
95          * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
96          *
97          * @return bool
98          */
99         public function isInstall()
100         {
101                 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
102                         !$this->has(MODE::DBCONFIGAVAILABLE);
103         }
104
105         /**
106          * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
107          *
108          * @return bool
109          */
110         public function isNormal()
111         {
112                 return $this->has(Mode::LOCALCONFIGPRESENT) &&
113                         $this->has(Mode::DBAVAILABLE) &&
114                         $this->has(Mode::DBCONFIGAVAILABLE) &&
115                         $this->has(Mode::MAINTENANCEDISABLED);
116         }
117 }