]> git.mxchange.org Git - friendica.git/blob - src/App/Mode.php
Fix PHPDoc comments project-wide
[friendica.git] / src / App / Mode.php
1 <?php
2
3 namespace Friendica\App;
4
5 use Friendica\Core\Config;
6 use Friendica\Database\DBA;
7
8 /**
9  * Mode of the current Friendica Node
10  *
11  * @package Friendica\App
12  */
13 class Mode
14 {
15         const LOCALCONFIGPRESENT = 1;
16         const DBAVAILABLE = 2;
17         const DBCONFIGAVAILABLE = 4;
18         const MAINTENANCEDISABLED = 8;
19
20         /***
21          * @var int the mode of this Application
22          *
23          */
24         private $mode;
25
26         /**
27          * @var string the basepath of the application
28          */
29         private $basepath;
30
31         public function __construct($basepath = '')
32         {
33                 $this->basepath = $basepath;
34                 $this->mode = 0;
35         }
36
37         /**
38          * Sets the App mode
39          *
40          * - App::MODE_INSTALL    : Either the database connection can't be established or the config table doesn't exist
41          * - App::MODE_MAINTENANCE: The maintenance mode has been set
42          * - App::MODE_NORMAL     : Normal run with all features enabled
43          *
44          * @param string $basepath the Basepath of the Application
45          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
46          */
47         public function determine($basepath = null)
48         {
49                 if (!empty($basepath)) {
50                         $this->basepath = $basepath;
51                 }
52
53                 $this->mode = 0;
54
55                 if (!file_exists($this->basepath . '/config/local.config.php')
56                         && !file_exists($this->basepath . '/config/local.ini.php')
57                         && !file_exists($this->basepath . '/.htconfig.php')) {
58                         return;
59                 }
60
61                 $this->mode |= Mode::LOCALCONFIGPRESENT;
62
63                 if (!DBA::connected()) {
64                         return;
65                 }
66
67                 $this->mode |= Mode::DBAVAILABLE;
68
69                 if (DBA::fetchFirst("SHOW TABLES LIKE 'config'") === false) {
70                         return;
71                 }
72
73                 $this->mode |= Mode::DBCONFIGAVAILABLE;
74
75                 if (Config::get('system', 'maintenance')) {
76                         return;
77                 }
78
79                 $this->mode |= Mode::MAINTENANCEDISABLED;
80         }
81
82         /**
83          * Checks, if the Friendica Node has the given mode
84          *
85          * @param int $mode A mode to test
86          *
87          * @return bool returns true, if the mode is set
88          */
89         public function has($mode)
90         {
91                 return ($this->mode & $mode) > 0;
92         }
93
94
95         /**
96          * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
97          *
98          * @return bool
99          */
100         public function isInstall()
101         {
102                 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
103                         !$this->has(MODE::DBCONFIGAVAILABLE);
104         }
105
106         /**
107          * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
108          *
109          * @return bool
110          */
111         public function isNormal()
112         {
113                 return $this->has(Mode::LOCALCONFIGPRESENT) &&
114                         $this->has(Mode::DBAVAILABLE) &&
115                         $this->has(Mode::DBCONFIGAVAILABLE) &&
116                         $this->has(Mode::MAINTENANCEDISABLED);
117         }
118 }