3 namespace Friendica\App;
5 use Detection\MobileDetect;
6 use Friendica\Core\Config\Cache\ConfigCache;
7 use Friendica\Database\Database;
8 use Friendica\Util\BasePath;
11 * Mode of the current Friendica Node
13 * @package Friendica\App
17 const LOCALCONFIGPRESENT = 1;
18 const DBAVAILABLE = 2;
19 const DBCONFIGAVAILABLE = 4;
20 const MAINTENANCEDISABLED = 8;
23 * @var int The mode of this Application
29 * @var bool True, if the call is a backend call
34 * @var bool True, if the call is a ajax call
39 * @var bool True, if the call is from a mobile device
44 * @var bool True, if the call is from a tablet device
48 public function __construct(int $mode = 0, bool $isBackend = false, bool $isAjax = false, bool $isMobile = false, bool $isTablet = false)
51 $this->isBackend = $isBackend;
52 $this->isAjax = $isAjax;
53 $this->isMobile = $isMobile;
54 $this->isTablet = $isTablet;
60 * - App::MODE_INSTALL : Either the database connection can't be established or the config table doesn't exist
61 * - App::MODE_MAINTENANCE: The maintenance mode has been set
62 * - App::MODE_NORMAL : Normal run with all features enabled
64 * @return Mode returns the determined mode
68 public function determine(BasePath $basepath, Database $database, ConfigCache $configCache)
72 $basepathName = $basepath->getPath();
74 if (!file_exists($basepathName . '/config/local.config.php')
75 && !file_exists($basepathName . '/config/local.ini.php')
76 && !file_exists($basepathName . '/.htconfig.php')) {
77 return new Mode($mode);
80 $mode |= Mode::LOCALCONFIGPRESENT;
82 if (!$database->connected()) {
83 return new Mode($mode);
86 $mode |= Mode::DBAVAILABLE;
88 if ($database->fetchFirst("SHOW TABLES LIKE 'config'") === false) {
89 return new Mode($mode);
92 $mode |= Mode::DBCONFIGAVAILABLE;
94 if (!empty($configCache->get('system', 'maintenance')) ||
95 // Don't use Config or Configuration here because we're possibly BEFORE initializing the Configuration,
96 // so this could lead to a dependency circle
97 !empty($database->selectFirst('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])['v'])) {
98 return new Mode($mode);
101 $mode |= Mode::MAINTENANCEDISABLED;
103 return new Mode($mode, $this->isBackend, $this->isAjax, $this->isMobile, $this->isTablet);
107 * Checks if the site is called via a backend process
109 * @param Module $module The pre-loaded module (just name, not class!)
110 * @param array $server The $_SERVER variable
111 * @param MobileDetect $mobileDetect The mobile detection library
113 * @return Mode returns the determined mode
115 public function determineRunMode(Module $module, array $server, MobileDetect $mobileDetect)
117 $isBackend = basename(($server['PHP_SELF'] ?? ''), '.php') !== 'index' ||
118 $module->isBackend();
119 $isMobile = $mobileDetect->isMobile();
120 $isTablet = $mobileDetect->isTablet();
121 $isAjax = strtolower($server['HTTP_X_REQUESTED_WITH'] ?? '') == 'xmlhttprequest';
123 return new Mode($this->mode, $isBackend, $isAjax, $isMobile, $isTablet);
127 * Checks, if the Friendica Node has the given mode
129 * @param int $mode A mode to test
131 * @return bool returns true, if the mode is set
133 public function has($mode)
135 return ($this->mode & $mode) > 0;
140 * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
144 public function isInstall()
146 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
147 !$this->has(MODE::DBCONFIGAVAILABLE);
151 * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
155 public function isNormal()
157 return $this->has(Mode::LOCALCONFIGPRESENT) &&
158 $this->has(Mode::DBAVAILABLE) &&
159 $this->has(Mode::DBCONFIGAVAILABLE) &&
160 $this->has(Mode::MAINTENANCEDISABLED);
164 * Returns true, if the call is from a backend node (f.e. from a worker)
166 * @return bool Is it a backend call
168 public function isBackend()
170 return $this->isBackend;
174 * Check if request was an AJAX (xmlhttprequest) request.
176 * @return bool true if it was an AJAX request
178 public function isAjax()
180 return $this->isAjax;
184 * Check if request was a mobile request.
186 * @return bool true if it was an mobile request
188 public function isMobile()
190 return $this->isMobile;
194 * Check if request was a tablet request.
196 * @return bool true if it was an tablet request
198 public function isTablet()
200 return $this->isTablet;