]> git.mxchange.org Git - friendica.git/blob - src/App/Mode.php
Merge remote-tracking branch 'upstream/develop' into item-notification
[friendica.git] / src / App / Mode.php
1 <?php
2
3 namespace Friendica\App;
4
5 use Detection\MobileDetect;
6 use Friendica\Core\Config\Cache\ConfigCache;
7 use Friendica\Database\Database;
8 use Friendica\Util\BasePath;
9
10 /**
11  * Mode of the current Friendica Node
12  *
13  * @package Friendica\App
14  */
15 class Mode
16 {
17         const LOCALCONFIGPRESENT  = 1;
18         const DBAVAILABLE         = 2;
19         const DBCONFIGAVAILABLE   = 4;
20         const MAINTENANCEDISABLED = 8;
21
22         /***
23          * @var int The mode of this Application
24          *
25          */
26         private $mode;
27
28         /**
29          * @var bool True, if the call is a backend call
30          */
31         private $isBackend;
32
33         /**
34          * @var bool True, if the call is a ajax call
35          */
36         private $isAjax;
37
38         /**
39          * @var bool True, if the call is from a mobile device
40          */
41         private $isMobile;
42
43         /**
44          * @var bool True, if the call is from a tablet device
45          */
46         private $isTablet;
47
48         public function __construct(int $mode = 0, bool $isBackend = false, bool $isAjax = false, bool $isMobile = false, bool $isTablet = false)
49         {
50                 $this->mode      = $mode;
51                 $this->isBackend = $isBackend;
52                 $this->isAjax    = $isAjax;
53                 $this->isMobile  = $isMobile;
54                 $this->isTablet  = $isTablet;
55         }
56
57         /**
58          * Sets the App mode
59          *
60          * - App::MODE_INSTALL    : Either the database connection can't be established or the config table doesn't exist
61          * - App::MODE_MAINTENANCE: The maintenance mode has been set
62          * - App::MODE_NORMAL     : Normal run with all features enabled
63          *
64          * @return Mode returns the determined mode
65          *
66          * @throws \Exception
67          */
68         public function determine(BasePath $basepath, Database $database, ConfigCache $configCache)
69         {
70                 $mode = 0;
71
72                 $basepathName = $basepath->getPath();
73
74                 if (!file_exists($basepathName . '/config/local.config.php')
75                     && !file_exists($basepathName . '/config/local.ini.php')
76                     && !file_exists($basepathName . '/.htconfig.php')) {
77                         return new Mode($mode);
78                 }
79
80                 $mode |= Mode::LOCALCONFIGPRESENT;
81
82                 if (!$database->connected()) {
83                         return new Mode($mode);
84                 }
85
86                 $mode |= Mode::DBAVAILABLE;
87
88                 if ($database->fetchFirst("SHOW TABLES LIKE 'config'") === false) {
89                         return new Mode($mode);
90                 }
91
92                 $mode |= Mode::DBCONFIGAVAILABLE;
93
94                 if (!empty($configCache->get('system', 'maintenance')) ||
95                     // Don't use Config or Configuration here because we're possibly BEFORE initializing the Configuration,
96                     // so this could lead to a dependency circle
97                     !empty($database->selectFirst('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])['v'])) {
98                         return new Mode($mode);
99                 }
100
101                 $mode |= Mode::MAINTENANCEDISABLED;
102
103                 return new Mode($mode, $this->isBackend, $this->isAjax, $this->isMobile, $this->isTablet);
104         }
105
106         /**
107          * Checks if the site is called via a backend process
108          *
109          * @param bool         $isBackend    True, if the call is from a backend script (daemon, worker, ...)
110          * @param Module       $module       The pre-loaded module (just name, not class!)
111          * @param array        $server       The $_SERVER variable
112          * @param MobileDetect $mobileDetect The mobile detection library
113          *
114          * @return Mode returns the determined mode
115          */
116         public function determineRunMode(bool $isBackend, Module $module, array $server, MobileDetect $mobileDetect)
117         {
118                 $isBackend = $isBackend ||
119                              $module->isBackend();
120                 $isMobile  = $mobileDetect->isMobile();
121                 $isTablet  = $mobileDetect->isTablet();
122                 $isAjax    = strtolower($server['HTTP_X_REQUESTED_WITH'] ?? '') == 'xmlhttprequest';
123
124                 return new Mode($this->mode, $isBackend, $isAjax, $isMobile, $isTablet);
125         }
126
127         /**
128          * Checks, if the Friendica Node has the given mode
129          *
130          * @param int $mode A mode to test
131          *
132          * @return bool returns true, if the mode is set
133          */
134         public function has($mode)
135         {
136                 return ($this->mode & $mode) > 0;
137         }
138
139
140         /**
141          * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
142          *
143          * @return bool
144          */
145         public function isInstall()
146         {
147                 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
148                        !$this->has(MODE::DBCONFIGAVAILABLE);
149         }
150
151         /**
152          * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
153          *
154          * @return bool
155          */
156         public function isNormal()
157         {
158                 return $this->has(Mode::LOCALCONFIGPRESENT) &&
159                        $this->has(Mode::DBAVAILABLE) &&
160                        $this->has(Mode::DBCONFIGAVAILABLE) &&
161                        $this->has(Mode::MAINTENANCEDISABLED);
162         }
163
164         /**
165          * Returns true, if the call is from a backend node (f.e. from a worker)
166          *
167          * @return bool Is it a backend call
168          */
169         public function isBackend()
170         {
171                 return $this->isBackend;
172         }
173
174         /**
175          * Check if request was an AJAX (xmlhttprequest) request.
176          *
177          * @return bool true if it was an AJAX request
178          */
179         public function isAjax()
180         {
181                 return $this->isAjax;
182         }
183
184         /**
185          * Check if request was a mobile request.
186          *
187          * @return bool true if it was an mobile request
188          */
189         public function isMobile()
190         {
191                 return $this->isMobile;
192         }
193
194         /**
195          * Check if request was a tablet request.
196          *
197          * @return bool true if it was an tablet request
198          */
199         public function isTablet()
200         {
201                 return $this->isTablet;
202         }
203 }