]> git.mxchange.org Git - friendica.git/blob - src/App/Mode.php
[frio] Add Mute Author Server button to post actions
[friendica.git] / src / App / Mode.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Capability\IManageConfigValues;
26 use Friendica\Database\Database;
27
28 /**
29  * Mode of the current Friendica Node
30  *
31  * @package Friendica\App
32  */
33 class Mode
34 {
35         const LOCALCONFIGPRESENT  = 1;
36         const DBAVAILABLE         = 2;
37         const DBCONFIGAVAILABLE   = 4;
38         const MAINTENANCEDISABLED = 8;
39
40         const UNDEFINED = 0;
41         const INDEX = 1;
42         const DAEMON = 2;
43         const WORKER = 3;
44
45         const BACKEND_CONTENT_TYPES = ['application/jrd+json', 'text/xml',
46                 'application/rss+xml', 'application/atom+xml', 'application/activity+json'];
47
48         /**
49          * A list of modules, which are backend methods
50          *
51          * @var array
52          */
53         const BACKEND_MODULES = [
54                 '_well_known',
55                 'api',
56                 'dfrn_notify',
57                 'feed',
58                 'fetch',
59                 'followers',
60                 'following',
61                 'hcard',
62                 'hostxrd',
63                 'inbox',
64                 'manifest',
65                 'nodeinfo',
66                 'noscrape',
67                 'objects',
68                 'outbox',
69                 'poco',
70                 'pubsub',
71                 'pubsubhubbub',
72                 'receive',
73                 'rsd_xml',
74                 'salmon',
75                 'statistics_json',
76                 'xrd',
77         ];
78
79         /***
80          * @var int The mode of this Application
81          *
82          */
83         private $mode;
84
85         /***
86          * @var int Who executes this Application
87          *
88          */
89         private $executor = self::UNDEFINED;
90
91         /**
92          * @var bool True, if the call is a backend call
93          */
94         private $isBackend;
95
96         /**
97          * @var bool True, if the call is a ajax call
98          */
99         private $isAjax;
100
101         /**
102          * @var bool True, if the call is from a mobile device
103          */
104         private $isMobile;
105
106         /**
107          * @var bool True, if the call is from a tablet device
108          */
109         private $isTablet;
110
111         public function __construct(int $mode = 0, bool $isBackend = false, bool $isAjax = false, bool $isMobile = false, bool $isTablet = false)
112         {
113                 $this->mode      = $mode;
114                 $this->isBackend = $isBackend;
115                 $this->isAjax    = $isAjax;
116                 $this->isMobile  = $isMobile;
117                 $this->isTablet  = $isTablet;
118         }
119
120         /**
121          * Sets the App mode
122          *
123          * - App::MODE_INSTALL    : Either the database connection can't be established or the config table doesn't exist
124          * - App::MODE_MAINTENANCE: The maintenance mode has been set
125          * - App::MODE_NORMAL     : Normal run with all features enabled
126          *
127          * @return Mode returns the determined mode
128          *
129          * @throws \Exception
130          */
131         public function determine(string $basePath, Database $database, IManageConfigValues $config): Mode
132         {
133                 $mode = 0;
134
135                 if (!file_exists($basePath . '/config/local.config.php') &&
136                         !file_exists($basePath . '/config/local.ini.php') &&
137                         !file_exists($basePath . '/.htconfig.php')) {
138                         return new Mode($mode);
139                 }
140
141                 $mode |= Mode::LOCALCONFIGPRESENT;
142
143                 if (!$database->connected()) {
144                         return new Mode($mode);
145                 }
146
147                 $mode |= Mode::DBAVAILABLE;
148
149                 if (!empty($config->get('system', 'maintenance'))) {
150                         return new Mode($mode);
151                 }
152
153                 $mode |= Mode::MAINTENANCEDISABLED;
154
155                 return new Mode($mode, $this->isBackend, $this->isAjax, $this->isMobile, $this->isTablet);
156         }
157
158         /**
159          * Checks if the site is called via a backend process
160          *
161          * @param bool             $isBackend    True, if the call is from a backend script (daemon, worker, ...)
162          * @param array            $server       The $_SERVER variable
163          * @param Arguments        $args         The Friendica App arguments
164          * @param MobileDetect     $mobileDetect The mobile detection library
165          *
166          * @return Mode returns the determined mode
167          */
168         public function determineRunMode(bool $isBackend, array $server, Arguments $args, MobileDetect $mobileDetect): Mode
169         {
170                 foreach (self::BACKEND_CONTENT_TYPES as $type) {
171                         if (strpos(strtolower($server['HTTP_ACCEPT'] ?? ''), $type) !== false) {
172                                 $isBackend = true;
173                         }
174                 }
175
176                 $isBackend = $isBackend || in_array($args->getModuleName(), static::BACKEND_MODULES);
177                 $isMobile  = $mobileDetect->isMobile();
178                 $isTablet  = $mobileDetect->isTablet();
179                 $isAjax    = strtolower($server['HTTP_X_REQUESTED_WITH'] ?? '') == 'xmlhttprequest';
180
181                 return new Mode($this->mode, $isBackend, $isAjax, $isMobile, $isTablet);
182         }
183
184         /**
185          * Checks, if the Friendica Node has the given mode
186          *
187          * @param int $mode A mode to test
188          *
189          * @return bool returns true, if the mode is set
190          */
191         public function has(int $mode): bool
192         {
193                 return ($this->mode & $mode) > 0;
194         }
195
196         /**
197          * Set the execution mode
198          *
199          * @param integer $executor Execution Mode
200          * @return void
201          */
202         public function setExecutor(int $executor)
203         {
204                 $this->executor = $executor;
205
206                 // Daemon and worker are always backend
207                 if (in_array($executor, [self::DAEMON, self::WORKER])) {
208                         $this->isBackend = true;
209                 }
210         }
211
212         /*isBackend = true;*
213          * get the execution mode
214          *
215          * @return int Execution Mode
216          */
217         public function getExecutor(): int
218         {
219                 return $this->executor;
220         }
221
222         /**
223          * Install mode is when the local config file is missing or the database isn't available.
224          *
225          * @return bool Whether installation mode is active (local/database configuration files present or not)
226          */
227         public function isInstall(): bool
228         {
229                 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
230                        !$this->has(MODE::DBAVAILABLE);
231         }
232
233         /**
234          * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
235          *
236          * @return bool
237          */
238         public function isNormal(): bool
239         {
240                 return $this->has(Mode::LOCALCONFIGPRESENT) &&
241                        $this->has(Mode::DBAVAILABLE) &&
242                        $this->has(Mode::MAINTENANCEDISABLED);
243         }
244
245         /**
246          * Returns true, if the call is from a backend node (f.e. from a worker)
247          *
248          * @return bool Is it a backend call
249          */
250         public function isBackend(): bool
251         {
252                 return $this->isBackend;
253         }
254
255         /**
256          * Check if request was an AJAX (xmlhttprequest) request.
257          *
258          * @return bool true if it was an AJAX request
259          */
260         public function isAjax(): bool
261         {
262                 return $this->isAjax;
263         }
264
265         /**
266          * Check if request was a mobile request.
267          *
268          * @return bool true if it was an mobile request
269          */
270         public function isMobile(): bool
271         {
272                 return $this->isMobile;
273         }
274
275         /**
276          * Check if request was a tablet request.
277          *
278          * @return bool true if it was an tablet request
279          */
280         public function isTablet(): bool
281         {
282                 return $this->isTablet;
283         }
284 }