]> git.mxchange.org Git - friendica.git/blob - src/App/Mode.php
10f1092a8952effd2d749d308cc05cf87682b8b6
[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          *
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 . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')
56                         && !file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) {
57                         return;
58                 }
59
60                 $this->mode |= Mode::LOCALCONFIGPRESENT;
61
62                 if (!DBA::connected()) {
63                         return;
64                 }
65
66                 $this->mode |= Mode::DBAVAILABLE;
67
68                 if (DBA::fetchFirst("SHOW TABLES LIKE 'config'") === false) {
69                         return;
70                 }
71
72                 $this->mode |= Mode::DBCONFIGAVAILABLE;
73
74                 if (Config::get('system', 'maintenance')) {
75                         return;
76                 }
77
78                 $this->mode |= Mode::MAINTENANCEDISABLED;
79         }
80
81         /**
82          * Checks, if the Friendica Node has the given mode
83          *
84          * @param int $mode A mode to test
85          *
86          * @return bool returns true, if the mode is set
87          */
88         public function has($mode)
89         {
90                 echo "mode: " . $this->mode . " with " . $mode;
91
92                 echo "value: " . ($this->mode & $mode);
93
94                 return ($this->mode & $mode) > 0;
95         }
96
97
98         /**
99          * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
100          *
101          * @return bool
102          */
103         public function isInstall()
104         {
105                 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
106                         !$this->has(MODE::DBCONFIGAVAILABLE);
107         }
108
109         /**
110          * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
111          *
112          * @return bool
113          */
114         public function isNormal()
115         {
116                 return $this->has(Mode::LOCALCONFIGPRESENT) &&
117                         $this->has(Mode::DBAVAILABLE) &&
118                         $this->has(Mode::DBCONFIGAVAILABLE) &&
119                         $this->has(Mode::MAINTENANCEDISABLED);
120         }
121 }