3 namespace Friendica\App;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Database\Database;
7 use Friendica\Util\BasePath;
10 * Mode of the current Friendica Node
12 * @package Friendica\App
16 const LOCALCONFIGPRESENT = 1;
17 const DBAVAILABLE = 2;
18 const DBCONFIGAVAILABLE = 4;
19 const MAINTENANCEDISABLED = 8;
22 * @var int The mode of this Application
28 * @var bool True, if the call is a backend call
32 public function __construct(int $mode = 0, bool $isBackend = false)
35 $this->isBackend = $isBackend;
41 * - App::MODE_INSTALL : Either the database connection can't be established or the config table doesn't exist
42 * - App::MODE_MAINTENANCE: The maintenance mode has been set
43 * - App::MODE_NORMAL : Normal run with all features enabled
45 * @return Mode returns the determined mode
49 public function determine(BasePath $basepath, Database $database, ConfigCache $configCache)
53 $basepathName = $basepath->getPath();
55 if (!file_exists($basepathName . '/config/local.config.php')
56 && !file_exists($basepathName . '/config/local.ini.php')
57 && !file_exists($basepathName . '/.htconfig.php')) {
58 return new Mode($mode);
61 $mode |= Mode::LOCALCONFIGPRESENT;
63 if (!$database->connected()) {
64 return new Mode($mode);
67 $mode |= Mode::DBAVAILABLE;
69 if ($database->fetchFirst("SHOW TABLES LIKE 'config'") === false) {
70 return new Mode($mode);
73 $mode |= Mode::DBCONFIGAVAILABLE;
75 if (!empty($configCache->get('system', 'maintenance')) ||
76 // Don't use Config or Configuration here because we're possibly BEFORE initializing the Configuration,
77 // so this could lead to a dependency circle
78 !empty($database->selectFirst('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])['v'])) {
79 return new Mode($mode);
82 $mode |= Mode::MAINTENANCEDISABLED;
84 return new Mode($mode, $this->isBackend);
88 * Checks if the site is called via a backend process
90 * @param Module $module The pre-loaded module (just name, not class!)
91 * @param array $server The $_SERVER variable
93 * @return Mode returns the determined mode
95 public function determineBackend(Module $module, array $server)
97 $isBackend = basename(($server['PHP_SELF'] ?? ''), '.php') !== 'index' ||
100 return new Mode($this->mode, $isBackend);
104 * Checks, if the Friendica Node has the given mode
106 * @param int $mode A mode to test
108 * @return bool returns true, if the mode is set
110 public function has($mode)
112 return ($this->mode & $mode) > 0;
117 * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
121 public function isInstall()
123 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
124 !$this->has(MODE::DBCONFIGAVAILABLE);
128 * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
132 public function isNormal()
134 return $this->has(Mode::LOCALCONFIGPRESENT) &&
135 $this->has(Mode::DBAVAILABLE) &&
136 $this->has(Mode::DBCONFIGAVAILABLE) &&
137 $this->has(Mode::MAINTENANCEDISABLED);
141 * Returns true, if the call is from a backend node (f.e. from a worker)
143 * @return bool Is it a backend call
145 public function isBackend()
147 return $this->isBackend;