]> git.mxchange.org Git - friendica.git/blob - src/App/Mode.php
5d26a2d45e1a7daa8d0e034150647336b888ab38
[friendica.git] / src / App / Mode.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\App;
23
24 use Detection\MobileDetect;
25 use Friendica\Core\Config\ValueObject\Cache;
26 use Friendica\Database\Database;
27 use Friendica\Util\BasePath;
28 use phpDocumentor\Reflection\Types\Static_;
29
30 /**
31  * Mode of the current Friendica Node
32  *
33  * @package Friendica\App
34  */
35 class Mode
36 {
37         const LOCALCONFIGPRESENT  = 1;
38         const DBAVAILABLE         = 2;
39         const DBCONFIGAVAILABLE   = 4;
40         const MAINTENANCEDISABLED = 8;
41
42         const UNDEFINED = 0;
43         const INDEX = 1;
44         const DAEMON = 2;
45         const WORKER = 3;
46
47         const BACKEND_CONTENT_TYPES = ['application/jrd+json', 'text/xml',
48                 'application/rss+xml', 'application/atom+xml', 'application/activity+json'];
49
50         /**
51          * A list of modules, which are backend methods
52          *
53          * @var array
54          */
55         const BACKEND_MODULES = [
56                 '_well_known',
57                 'api',
58                 'dfrn_notify',
59                 'feed',
60                 'fetch',
61                 'followers',
62                 'following',
63                 'hcard',
64                 'hostxrd',
65                 'inbox',
66                 'manifest',
67                 'nodeinfo',
68                 'noscrape',
69                 'objects',
70                 'outbox',
71                 'poco',
72                 'post',
73                 'pubsub',
74                 'pubsubhubbub',
75                 'receive',
76                 'rsd_xml',
77                 'salmon',
78                 'statistics_json',
79                 'xrd',
80         ];
81
82         /***
83          * @var int The mode of this Application
84          *
85          */
86         private $mode;
87
88         /***
89          * @var int Who executes this Application
90          *
91          */
92         private $executor = self::UNDEFINED;
93
94         /**
95          * @var bool True, if the call is a backend call
96          */
97         private $isBackend;
98
99         /**
100          * @var bool True, if the call is a ajax call
101          */
102         private $isAjax;
103
104         /**
105          * @var bool True, if the call is from a mobile device
106          */
107         private $isMobile;
108
109         /**
110          * @var bool True, if the call is from a tablet device
111          */
112         private $isTablet;
113
114         public function __construct(int $mode = 0, bool $isBackend = false, bool $isAjax = false, bool $isMobile = false, bool $isTablet = false)
115         {
116                 $this->mode      = $mode;
117                 $this->isBackend = $isBackend;
118                 $this->isAjax    = $isAjax;
119                 $this->isMobile  = $isMobile;
120                 $this->isTablet  = $isTablet;
121         }
122
123         /**
124          * Sets the App mode
125          *
126          * - App::MODE_INSTALL    : Either the database connection can't be established or the config table doesn't exist
127          * - App::MODE_MAINTENANCE: The maintenance mode has been set
128          * - App::MODE_NORMAL     : Normal run with all features enabled
129          *
130          * @return Mode returns the determined mode
131          *
132          * @throws \Exception
133          */
134         public function determine(BasePath $basepath, Database $database, Cache $configCache)
135         {
136                 $mode = 0;
137
138                 $basepathName = $basepath->getPath();
139
140                 if (!file_exists($basepathName . '/config/local.config.php')
141                     && !file_exists($basepathName . '/config/local.ini.php')
142                     && !file_exists($basepathName . '/.htconfig.php')) {
143                         return new Mode($mode);
144                 }
145
146                 $mode |= Mode::LOCALCONFIGPRESENT;
147
148                 if (!$database->connected()) {
149                         return new Mode($mode);
150                 }
151
152                 $mode |= Mode::DBAVAILABLE;
153
154                 if ($database->fetchFirst("SHOW TABLES LIKE 'config'") === false) {
155                         return new Mode($mode);
156                 }
157
158                 $mode |= Mode::DBCONFIGAVAILABLE;
159
160                 if (!empty($configCache->get('system', 'maintenance')) ||
161                     // Don't use Config or Configuration here because we're possibly BEFORE initializing the Configuration,
162                     // so this could lead to a dependency circle
163                     !empty($database->selectFirst('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])['v'])) {
164                         return new Mode($mode);
165                 }
166
167                 $mode |= Mode::MAINTENANCEDISABLED;
168
169                 return new Mode($mode, $this->isBackend, $this->isAjax, $this->isMobile, $this->isTablet);
170         }
171
172         /**
173          * Checks if the site is called via a backend process
174          *
175          * @param bool             $isBackend    True, if the call is from a backend script (daemon, worker, ...)
176          * @param array            $server       The $_SERVER variable
177          * @param Arguments        $args         The Friendica App arguments
178          * @param MobileDetect     $mobileDetect The mobile detection library
179          *
180          * @return Mode returns the determined mode
181          */
182         public function determineRunMode(bool $isBackend, array $server, Arguments $args, MobileDetect $mobileDetect)
183         {
184                 foreach (self::BACKEND_CONTENT_TYPES as $type) {
185                         if (strpos(strtolower($server['HTTP_ACCEPT'] ?? ''), $type) !== false) {
186                                 $isBackend = true;
187                         }
188                 }
189
190                 $isBackend = $isBackend || in_array($args->getModuleName(), static::BACKEND_MODULES);
191                 $isMobile  = $mobileDetect->isMobile();
192                 $isTablet  = $mobileDetect->isTablet();
193                 $isAjax    = strtolower($server['HTTP_X_REQUESTED_WITH'] ?? '') == 'xmlhttprequest';
194
195                 return new Mode($this->mode, $isBackend, $isAjax, $isMobile, $isTablet);
196         }
197
198         /**
199          * Checks, if the Friendica Node has the given mode
200          *
201          * @param int $mode A mode to test
202          *
203          * @return bool returns true, if the mode is set
204          */
205         public function has($mode)
206         {
207                 return ($this->mode & $mode) > 0;
208         }
209
210         /**
211          * Set the execution mode
212          *
213          * @param integer $executor Execution Mode
214          * @return void
215          */
216         public function setExecutor(int $executor)
217         {
218                 $this->executor = $executor;
219
220                 // Daemon and worker are always backend
221                 if (in_array($executor, [self::DAEMON, self::WORKER])) {
222                         $this->isBackend = true;
223                 }
224         }
225
226         /*isBackend = true;*
227          * get the execution mode
228          *
229          * @return int Execution Mode
230          */
231         public function getExecutor()
232         {
233                 return $this->executor;
234         }
235
236         /**
237          * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
238          *
239          * @return bool
240          */
241         public function isInstall()
242         {
243                 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
244                        !$this->has(MODE::DBCONFIGAVAILABLE);
245         }
246
247         /**
248          * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
249          *
250          * @return bool
251          */
252         public function isNormal()
253         {
254                 return $this->has(Mode::LOCALCONFIGPRESENT) &&
255                        $this->has(Mode::DBAVAILABLE) &&
256                        $this->has(Mode::DBCONFIGAVAILABLE) &&
257                        $this->has(Mode::MAINTENANCEDISABLED);
258         }
259
260         /**
261          * Returns true, if the call is from a backend node (f.e. from a worker)
262          *
263          * @return bool Is it a backend call
264          */
265         public function isBackend()
266         {
267                 return $this->isBackend;
268         }
269
270         /**
271          * Check if request was an AJAX (xmlhttprequest) request.
272          *
273          * @return bool true if it was an AJAX request
274          */
275         public function isAjax()
276         {
277                 return $this->isAjax;
278         }
279
280         /**
281          * Check if request was a mobile request.
282          *
283          * @return bool true if it was an mobile request
284          */
285         public function isMobile()
286         {
287                 return $this->isMobile;
288         }
289
290         /**
291          * Check if request was a tablet request.
292          *
293          * @return bool true if it was an tablet request
294          */
295         public function isTablet()
296         {
297                 return $this->isTablet;
298         }
299 }