3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\App;
24 use Detection\MobileDetect;
25 use Friendica\Core\Config\ValueObject\Cache;
26 use Friendica\Database\Database;
27 use Friendica\Util\BasePath;
30 * Mode of the current Friendica Node
32 * @package Friendica\App
36 const LOCALCONFIGPRESENT = 1;
37 const DBAVAILABLE = 2;
38 const DBCONFIGAVAILABLE = 4;
39 const MAINTENANCEDISABLED = 8;
46 const BACKEND_CONTENT_TYPES = ['application/jrd+json', 'text/xml',
47 'application/rss+xml', 'application/atom+xml', 'application/activity+json'];
50 * A list of modules, which are backend methods
54 const BACKEND_MODULES = [
81 * @var int The mode of this Application
87 * @var int Who executes this Application
90 private $executor = self::UNDEFINED;
93 * @var bool True, if the call is a backend call
98 * @var bool True, if the call is a ajax call
103 * @var bool True, if the call is from a mobile device
108 * @var bool True, if the call is from a tablet device
112 public function __construct(int $mode = 0, bool $isBackend = false, bool $isAjax = false, bool $isMobile = false, bool $isTablet = false)
115 $this->isBackend = $isBackend;
116 $this->isAjax = $isAjax;
117 $this->isMobile = $isMobile;
118 $this->isTablet = $isTablet;
124 * - App::MODE_INSTALL : Either the database connection can't be established or the config table doesn't exist
125 * - App::MODE_MAINTENANCE: The maintenance mode has been set
126 * - App::MODE_NORMAL : Normal run with all features enabled
128 * @return Mode returns the determined mode
132 public function determine(BasePath $basepath, Database $database, Cache $configCache): Mode
136 $basepathName = $basepath->getPath();
138 if (!file_exists($basepathName . '/config/local.config.php')
139 && !file_exists($basepathName . '/config/local.ini.php')
140 && !file_exists($basepathName . '/.htconfig.php')) {
141 return new Mode($mode);
144 $mode |= Mode::LOCALCONFIGPRESENT;
146 if (!$database->connected()) {
147 return new Mode($mode);
150 $mode |= Mode::DBAVAILABLE;
152 if ($database->fetchFirst("SHOW TABLES LIKE 'config'") === false) {
153 return new Mode($mode);
156 $mode |= Mode::DBCONFIGAVAILABLE;
158 if (!empty($configCache->get('system', 'maintenance')) ||
159 // Don't use Config or Configuration here because we're possibly BEFORE initializing the Configuration,
160 // so this could lead to a dependency circle
161 !empty($database->selectFirst('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])['v'])) {
162 return new Mode($mode);
165 $mode |= Mode::MAINTENANCEDISABLED;
167 return new Mode($mode, $this->isBackend, $this->isAjax, $this->isMobile, $this->isTablet);
171 * Checks if the site is called via a backend process
173 * @param bool $isBackend True, if the call is from a backend script (daemon, worker, ...)
174 * @param array $server The $_SERVER variable
175 * @param Arguments $args The Friendica App arguments
176 * @param MobileDetect $mobileDetect The mobile detection library
178 * @return Mode returns the determined mode
180 public function determineRunMode(bool $isBackend, array $server, Arguments $args, MobileDetect $mobileDetect): Mode
182 foreach (self::BACKEND_CONTENT_TYPES as $type) {
183 if (strpos(strtolower($server['HTTP_ACCEPT'] ?? ''), $type) !== false) {
188 $isBackend = $isBackend || in_array($args->getModuleName(), static::BACKEND_MODULES);
189 $isMobile = $mobileDetect->isMobile();
190 $isTablet = $mobileDetect->isTablet();
191 $isAjax = strtolower($server['HTTP_X_REQUESTED_WITH'] ?? '') == 'xmlhttprequest';
193 return new Mode($this->mode, $isBackend, $isAjax, $isMobile, $isTablet);
197 * Checks, if the Friendica Node has the given mode
199 * @param int $mode A mode to test
201 * @return bool returns true, if the mode is set
203 public function has(int $mode): bool
205 return ($this->mode & $mode) > 0;
209 * Set the execution mode
211 * @param integer $executor Execution Mode
214 public function setExecutor(int $executor)
216 $this->executor = $executor;
218 // Daemon and worker are always backend
219 if (in_array($executor, [self::DAEMON, self::WORKER])) {
220 $this->isBackend = true;
225 * get the execution mode
227 * @return int Execution Mode
229 public function getExecutor(): int
231 return $this->executor;
235 * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
237 * @return bool Whether installation mode is active (local/database configuration files present or not)
239 public function isInstall(): bool
241 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
242 !$this->has(MODE::DBCONFIGAVAILABLE);
246 * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
250 public function isNormal(): bool
252 return $this->has(Mode::LOCALCONFIGPRESENT) &&
253 $this->has(Mode::DBAVAILABLE) &&
254 $this->has(Mode::DBCONFIGAVAILABLE) &&
255 $this->has(Mode::MAINTENANCEDISABLED);
259 * Returns true, if the call is from a backend node (f.e. from a worker)
261 * @return bool Is it a backend call
263 public function isBackend(): bool
265 return $this->isBackend;
269 * Check if request was an AJAX (xmlhttprequest) request.
271 * @return bool true if it was an AJAX request
273 public function isAjax(): bool
275 return $this->isAjax;
279 * Check if request was a mobile request.
281 * @return bool true if it was an mobile request
283 public function isMobile(): bool
285 return $this->isMobile;
289 * Check if request was a tablet request.
291 * @return bool true if it was an tablet request
293 public function isTablet(): bool
295 return $this->isTablet;