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