X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FApp%2FMode.php;h=7cd16b4bb905e8866af2e28675edd3c77bfaf309;hb=da58b894a4239e95342524eeacb85af7bf6c5a9d;hp=6cb79b6edd4532ac25d4ffeb9791162b82b4ee33;hpb=92c1cbeeeccf31ae311b532866610b48c97dccfe;p=friendica.git diff --git a/src/App/Mode.php b/src/App/Mode.php index 6cb79b6edd..7cd16b4bb9 100644 --- a/src/App/Mode.php +++ b/src/App/Mode.php @@ -1,10 +1,29 @@ . + * + */ namespace Friendica\App; -use Friendica\Core\Config\Cache\ConfigCache; +use Detection\MobileDetect; +use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Database\Database; -use Friendica\Util\BasePath; /** * Mode of the current Friendica Node @@ -18,33 +37,84 @@ class Mode const DBCONFIGAVAILABLE = 4; const MAINTENANCEDISABLED = 8; + const UNDEFINED = 0; + const INDEX = 1; + const DAEMON = 2; + const WORKER = 3; + + const BACKEND_CONTENT_TYPES = ['application/jrd+json', 'text/xml', + 'application/rss+xml', 'application/atom+xml', 'application/activity+json']; + + /** + * A list of modules, which are backend methods + * + * @var array + */ + const BACKEND_MODULES = [ + '_well_known', + 'api', + 'dfrn_notify', + 'feed', + 'fetch', + 'followers', + 'following', + 'hcard', + 'hostxrd', + 'inbox', + 'manifest', + 'nodeinfo', + 'noscrape', + 'objects', + 'outbox', + 'poco', + 'pubsub', + 'pubsubhubbub', + 'receive', + 'rsd_xml', + 'salmon', + 'statistics_json', + 'xrd', + ]; + /*** - * @var int the mode of this Application + * @var int The mode of this Application * */ private $mode; + /*** + * @var int Who executes this Application + * + */ + private $executor = self::UNDEFINED; + /** - * @var string the basepath of the application + * @var bool True, if the call is a backend call */ - private $basepath; + private $isBackend; /** - * @var Database + * @var bool True, if the call is a ajax call */ - private $database; + private $isAjax; /** - * @var ConfigCache + * @var bool True, if the call is from a mobile device */ - private $configCache; + private $isMobile; - public function __construct(BasePath $basepath, Database $database, ConfigCache $configCache) + /** + * @var bool True, if the call is from a tablet device + */ + private $isTablet; + + public function __construct(int $mode = 0, bool $isBackend = false, bool $isAjax = false, bool $isMobile = false, bool $isTablet = false) { - $this->basepath = $basepath->getPath(); - $this->database = $database; - $this->configCache = $configCache; - $this->mode = 0; + $this->mode = $mode; + $this->isBackend = $isBackend; + $this->isAjax = $isAjax; + $this->isMobile = $isMobile; + $this->isTablet = $isTablet; } /** @@ -54,50 +124,61 @@ class Mode * - App::MODE_MAINTENANCE: The maintenance mode has been set * - App::MODE_NORMAL : Normal run with all features enabled * - * @param string $basePath the Basepath of the Application - * - * @return Mode returns itself + * @return Mode returns the determined mode * * @throws \Exception */ - public function determine($basePath = null) + public function determine(string $basePath, Database $database, IManageConfigValues $config): Mode { - if (!empty($basePath)) { - $this->basepath = $basePath; - } + $mode = 0; - $this->mode = 0; - - if (!file_exists($this->basepath . '/config/local.config.php') - && !file_exists($this->basepath . '/config/local.ini.php') - && !file_exists($this->basepath . '/.htconfig.php')) { - return $this; + if (!file_exists($basePath . '/config/local.config.php') && + !file_exists($basePath . '/config/local.ini.php') && + !file_exists($basePath . '/.htconfig.php')) { + return new Mode($mode); } - $this->mode |= Mode::LOCALCONFIGPRESENT; + $mode |= Mode::LOCALCONFIGPRESENT; - if (!$this->database->connected()) { - return $this; + if (!$database->connected()) { + return new Mode($mode); } - $this->mode |= Mode::DBAVAILABLE; + $mode |= Mode::DBAVAILABLE; - if ($this->database->fetchFirst("SHOW TABLES LIKE 'config'") === false) { - return $this; + if (!empty($config->get('system', 'maintenance'))) { + return new Mode($mode); } - $this->mode |= Mode::DBCONFIGAVAILABLE; + $mode |= Mode::MAINTENANCEDISABLED; + + return new Mode($mode, $this->isBackend, $this->isAjax, $this->isMobile, $this->isTablet); + } - if (!empty($this->configCache->get('system', 'maintenance')) || - // Don't use Config or Configuration here because we're possibly BEFORE initializing the Configuration, - // so this could lead to a dependency circle - !empty($this->database->selectFirst('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])['v'])) { - return $this; + /** + * Checks if the site is called via a backend process + * + * @param bool $isBackend True, if the call is from a backend script (daemon, worker, ...) + * @param array $server The $_SERVER variable + * @param Arguments $args The Friendica App arguments + * @param MobileDetect $mobileDetect The mobile detection library + * + * @return Mode returns the determined mode + */ + public function determineRunMode(bool $isBackend, array $server, Arguments $args, MobileDetect $mobileDetect): Mode + { + foreach (self::BACKEND_CONTENT_TYPES as $type) { + if (strpos(strtolower($server['HTTP_ACCEPT'] ?? ''), $type) !== false) { + $isBackend = true; + } } - $this->mode |= Mode::MAINTENANCEDISABLED; + $isBackend = $isBackend || in_array($args->getModuleName(), static::BACKEND_MODULES); + $isMobile = $mobileDetect->isMobile(); + $isTablet = $mobileDetect->isTablet(); + $isAjax = strtolower($server['HTTP_X_REQUESTED_WITH'] ?? '') == 'xmlhttprequest'; - return $this; + return new Mode($this->mode, $isBackend, $isAjax, $isMobile, $isTablet); } /** @@ -107,21 +188,46 @@ class Mode * * @return bool returns true, if the mode is set */ - public function has($mode) + public function has(int $mode): bool { return ($this->mode & $mode) > 0; } + /** + * Set the execution mode + * + * @param integer $executor Execution Mode + * @return void + */ + public function setExecutor(int $executor) + { + $this->executor = $executor; + + // Daemon and worker are always backend + if (in_array($executor, [self::DAEMON, self::WORKER])) { + $this->isBackend = true; + } + } + + /*isBackend = true;* + * get the execution mode + * + * @return int Execution Mode + */ + public function getExecutor(): int + { + return $this->executor; + } /** - * Install mode is when the local config file is missing or the DB schema hasn't been installed yet. + * Install mode is when the local config file is missing or the database isn't available. * - * @return bool + * @return bool Whether installation mode is active (local/database configuration files present or not) */ - public function isInstall() + public function isInstall(): bool { return !$this->has(Mode::LOCALCONFIGPRESENT) || - !$this->has(MODE::DBCONFIGAVAILABLE); + !$this->has(MODE::DBAVAILABLE); } /** @@ -129,11 +235,50 @@ class Mode * * @return bool */ - public function isNormal() + public function isNormal(): bool { return $this->has(Mode::LOCALCONFIGPRESENT) && $this->has(Mode::DBAVAILABLE) && - $this->has(Mode::DBCONFIGAVAILABLE) && $this->has(Mode::MAINTENANCEDISABLED); } + + /** + * Returns true, if the call is from a backend node (f.e. from a worker) + * + * @return bool Is it a backend call + */ + public function isBackend(): bool + { + return $this->isBackend; + } + + /** + * Check if request was an AJAX (xmlhttprequest) request. + * + * @return bool true if it was an AJAX request + */ + public function isAjax(): bool + { + return $this->isAjax; + } + + /** + * Check if request was a mobile request. + * + * @return bool true if it was an mobile request + */ + public function isMobile(): bool + { + return $this->isMobile; + } + + /** + * Check if request was a tablet request. + * + * @return bool true if it was an tablet request + */ + public function isTablet(): bool + { + return $this->isTablet; + } }