]> git.mxchange.org Git - friendica.git/blob - src/App/Mode.php
Added post update to remove duplicated contacts
[friendica.git] / src / App / Mode.php
1 <?php
2
3 namespace Friendica\App;
4
5 use Detection\MobileDetect;
6 use Friendica\Core\Config\Cache\ConfigCache;
7 use Friendica\Database\Database;
8 use Friendica\Util\BasePath;
9
10 /**
11  * Mode of the current Friendica Node
12  *
13  * @package Friendica\App
14  */
15 class Mode
16 {
17         const LOCALCONFIGPRESENT  = 1;
18         const DBAVAILABLE         = 2;
19         const DBCONFIGAVAILABLE   = 4;
20         const MAINTENANCEDISABLED = 8;
21
22         /***
23          * @var int The mode of this Application
24          *
25          */
26         private $mode;
27
28         /**
29          * @var bool True, if the call is a backend call
30          */
31         private $isBackend;
32
33         /**
34          * @var bool True, if the call is a ajax call
35          */
36         private $isAjax;
37
38         /**
39          * @var bool True, if the call is from a mobile device
40          */
41         private $isMobile;
42
43         /**
44          * @var bool True, if the call is from a tablet device
45          */
46         private $isTablet;
47
48         public function __construct(int $mode = 0, bool $isBackend = false, bool $isAjax = false, bool $isMobile = false, bool $isTablet = false)
49         {
50                 $this->mode      = $mode;
51                 $this->isBackend = $isBackend;
52                 $this->isAjax    = $isAjax;
53                 $this->isMobile  = $isMobile;
54                 $this->isTablet  = $isTablet;
55         }
56
57         /**
58          * Sets the App mode
59          *
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
63          *
64          * @return Mode returns the determined mode
65          *
66          * @throws \Exception
67          */
68         public function determine(BasePath $basepath, Database $database, ConfigCache $configCache)
69         {
70                 $mode = 0;
71
72                 $basepathName = $basepath->getPath();
73
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);
78                 }
79
80                 $mode |= Mode::LOCALCONFIGPRESENT;
81
82                 if (!$database->connected()) {
83                         return new Mode($mode);
84                 }
85
86                 $mode |= Mode::DBAVAILABLE;
87
88                 if ($database->fetchFirst("SHOW TABLES LIKE 'config'") === false) {
89                         return new Mode($mode);
90                 }
91
92                 $mode |= Mode::DBCONFIGAVAILABLE;
93
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);
99                 }
100
101                 $mode |= Mode::MAINTENANCEDISABLED;
102
103                 return new Mode($mode, $this->isBackend, $this->isAjax, $this->isMobile, $this->isTablet);
104         }
105
106         /**
107          * Checks if the site is called via a backend process
108          *
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
112          *
113          * @return Mode returns the determined mode
114          */
115         public function determineRunMode(Module $module, array $server, MobileDetect $mobileDetect)
116         {
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';
122
123                 return new Mode($this->mode, $isBackend, $isAjax, $isMobile, $isTablet);
124         }
125
126         /**
127          * Checks, if the Friendica Node has the given mode
128          *
129          * @param int $mode A mode to test
130          *
131          * @return bool returns true, if the mode is set
132          */
133         public function has($mode)
134         {
135                 return ($this->mode & $mode) > 0;
136         }
137
138
139         /**
140          * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
141          *
142          * @return bool
143          */
144         public function isInstall()
145         {
146                 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
147                        !$this->has(MODE::DBCONFIGAVAILABLE);
148         }
149
150         /**
151          * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
152          *
153          * @return bool
154          */
155         public function isNormal()
156         {
157                 return $this->has(Mode::LOCALCONFIGPRESENT) &&
158                        $this->has(Mode::DBAVAILABLE) &&
159                        $this->has(Mode::DBCONFIGAVAILABLE) &&
160                        $this->has(Mode::MAINTENANCEDISABLED);
161         }
162
163         /**
164          * Returns true, if the call is from a backend node (f.e. from a worker)
165          *
166          * @return bool Is it a backend call
167          */
168         public function isBackend()
169         {
170                 return $this->isBackend;
171         }
172
173         /**
174          * Check if request was an AJAX (xmlhttprequest) request.
175          *
176          * @return bool true if it was an AJAX request
177          */
178         public function isAjax()
179         {
180                 return $this->isAjax;
181         }
182
183         /**
184          * Check if request was a mobile request.
185          *
186          * @return bool true if it was an mobile request
187          */
188         public function isMobile()
189         {
190                 return $this->isMobile;
191         }
192
193         /**
194          * Check if request was a tablet request.
195          *
196          * @return bool true if it was an tablet request
197          */
198         public function isTablet()
199         {
200                 return $this->isTablet;
201         }
202 }