3 * @copyright Copyright (C) 2020, Friendica
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\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;
42 * @var int The mode of this Application
48 * @var bool True, if the call is a backend call
53 * @var bool True, if the call is a ajax call
58 * @var bool True, if the call is from a mobile device
63 * @var bool True, if the call is from a tablet device
67 public function __construct(int $mode = 0, bool $isBackend = false, bool $isAjax = false, bool $isMobile = false, bool $isTablet = false)
70 $this->isBackend = $isBackend;
71 $this->isAjax = $isAjax;
72 $this->isMobile = $isMobile;
73 $this->isTablet = $isTablet;
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
83 * @return Mode returns the determined mode
87 public function determine(BasePath $basepath, Database $database, Cache $configCache)
91 $basepathName = $basepath->getPath();
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);
99 $mode |= Mode::LOCALCONFIGPRESENT;
101 if (!$database->connected()) {
102 return new Mode($mode);
105 $mode |= Mode::DBAVAILABLE;
107 if ($database->fetchFirst("SHOW TABLES LIKE 'config'") === false) {
108 return new Mode($mode);
111 $mode |= Mode::DBCONFIGAVAILABLE;
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);
120 $mode |= Mode::MAINTENANCEDISABLED;
122 return new Mode($mode, $this->isBackend, $this->isAjax, $this->isMobile, $this->isTablet);
126 * Checks if the site is called via a backend process
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
133 * @return Mode returns the determined mode
135 public function determineRunMode(bool $isBackend, Module $module, array $server, MobileDetect $mobileDetect)
137 $isBackend = $isBackend ||
138 $module->isBackend();
139 $isMobile = $mobileDetect->isMobile();
140 $isTablet = $mobileDetect->isTablet();
141 $isAjax = strtolower($server['HTTP_X_REQUESTED_WITH'] ?? '') == 'xmlhttprequest';
143 return new Mode($this->mode, $isBackend, $isAjax, $isMobile, $isTablet);
147 * Checks, if the Friendica Node has the given mode
149 * @param int $mode A mode to test
151 * @return bool returns true, if the mode is set
153 public function has($mode)
155 return ($this->mode & $mode) > 0;
160 * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
164 public function isInstall()
166 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
167 !$this->has(MODE::DBCONFIGAVAILABLE);
171 * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
175 public function isNormal()
177 return $this->has(Mode::LOCALCONFIGPRESENT) &&
178 $this->has(Mode::DBAVAILABLE) &&
179 $this->has(Mode::DBCONFIGAVAILABLE) &&
180 $this->has(Mode::MAINTENANCEDISABLED);
184 * Returns true, if the call is from a backend node (f.e. from a worker)
186 * @return bool Is it a backend call
188 public function isBackend()
190 return $this->isBackend;
194 * Check if request was an AJAX (xmlhttprequest) request.
196 * @return bool true if it was an AJAX request
198 public function isAjax()
200 return $this->isAjax;
204 * Check if request was a mobile request.
206 * @return bool true if it was an mobile request
208 public function isMobile()
210 return $this->isMobile;
214 * Check if request was a tablet request.
216 * @return bool true if it was an tablet request
218 public function isTablet()
220 return $this->isTablet;