]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/AutomaticInstallation.php
Merge pull request #7068 from MrPetovan/task/7047-theme-error-page
[friendica.git] / src / Core / Console / AutomaticInstallation.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Asika\SimpleConsole\Console;
6 use Friendica\BaseObject;
7 use Friendica\Core\Config;
8 use Friendica\Core\Installer;
9 use Friendica\Core\Theme;
10 use Friendica\Util\BasePath;
11 use Friendica\Util\BaseURL;
12 use Friendica\Util\Config\ConfigFileLoader;
13 use RuntimeException;
14
15 class AutomaticInstallation extends Console
16 {
17         protected function getHelp()
18         {
19                 return <<<HELP
20 Installation - Install Friendica automatically
21 Synopsis
22         bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
23
24 Description
25     Installs Friendica with data based on the local.config.php file or environment variables
26
27 Notes
28     Not checking .htaccess/URL-Rewrite during CLI installation.
29
30 Options
31     -h|--help|-?            Show help information
32     -v                      Show more debug information.
33     -a                      All setup checks are required (except .htaccess)
34     -f|--file <config>      prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
35     -s|--savedb               Save the DB credentials to the file (if environment variables is used)
36     -H|--dbhost <host>        The host of the mysql/mariadb database (env MYSQL_HOST)
37     -p|--dbport <port>        The port of the mysql/mariadb database (env MYSQL_PORT)
38     -d|--dbdata <database>    The name of the mysql/mariadb database (env MYSQL_DATABASE)
39     -U|--dbuser <username>    The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
40     -P|--dbpass <password>    The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
41     -U|--url <url>            The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL) 
42     -B|--phppath <php_path>   The path of the PHP binary (env FRIENDICA_PHP_PATH)
43     -b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
44     -t|--tz <timezone>        The timezone of Friendica (env FRIENDICA_TZ)
45     -L|--lang <language>      The language of Friendica (env FRIENDICA_LANG)
46  
47 Environment variables
48    MYSQL_HOST                  The host of the mysql/mariadb database (mandatory if mysql and environment is used)
49    MYSQL_PORT                  The port of the mysql/mariadb database
50    MYSQL_USERNAME|MYSQL_USER   The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
51    MYSQL_PASSWORD              The password of the mysql/mariadb database login
52    MYSQL_DATABASE              The name of the mysql/mariadb database
53    FRIENDICA_URL               The full base URL of Friendica - f.e. 'https://friendica.local/sub'
54    FRIENDICA_PHP_PATH          The path of the PHP binary - leave empty for auto detection
55    FRIENDICA_BASE_PATH         The basepath of Friendica - leave empty for auto detection
56    FRIENDICA_ADMIN_MAIL        The admin email address of Friendica (this email will be used for admin access)
57    FRIENDICA_TZ                The timezone of Friendica
58    FRIENDICA_LANG              The langauge of Friendica
59    
60 Examples
61         bin/console autoinstall -f 'input.config.php
62                 Installs Friendica with the prepared 'input.config.php' file
63
64         bin/console autoinstall --savedb
65                 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
66
67         bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
68                 Installs Friendica with a local mysql database with credentials
69 HELP;
70         }
71
72         protected function doExecute()
73         {
74                 // Initialise the app
75                 $this->out("Initializing setup...\n");
76
77                 $a = BaseObject::getApp();
78
79                 $installer = new Installer();
80
81                 $configCache = $a->getConfigCache();
82                 $installer->setUpCache($configCache, BasePath::create($a->getBasePath(), $_SERVER));
83
84                 $this->out(" Complete!\n\n");
85
86                 // Check Environment
87                 $this->out("Checking environment...\n");
88
89                 $installer->resetChecks();
90
91                 if (!$this->runBasicChecks($installer, $configCache)) {
92                         $errorMessage = $this->extractErrors($installer->getChecks());
93                         throw new RuntimeException($errorMessage);
94                 }
95
96                 $this->out(" Complete!\n\n");
97
98                 // if a config file is set,
99                 $config_file = $this->getOption(['f', 'file']);
100
101                 if (!empty($config_file)) {
102                         if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
103                                 // Copy config file
104                                 $this->out("Copying config file...\n");
105                                 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
106                                         throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $a->getBasePath() . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.config.php' manually.\n");
107                                 }
108                         }
109
110                         //reload the config cache
111                         $loader = new ConfigFileLoader($a->getBasePath(), $a->getMode());
112                         $loader->setupCache($configCache);
113
114                 } else {
115                         // Creating config file
116                         $this->out("Creating config file...\n");
117
118                         $save_db = $this->getOption(['s', 'savedb'], false);
119
120                         $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
121                         $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
122                         $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
123                         $configCache->set('database', 'database',
124                                 $this->getOption(['d', 'dbdata'],
125                                         ($save_db) ? getenv('MYSQL_DATABASE') : ''));
126                         $configCache->set('database', 'username',
127                                 $this->getOption(['U', 'dbuser'],
128                                         ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
129                         $configCache->set('database', 'password',
130                                 $this->getOption(['P', 'dbpass'],
131                                         ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
132
133                         $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
134                         if (!empty($php_path)) {
135                                 $configCache->set('config', 'php_path', $php_path);
136                         } else {
137                                 $configCache->set('config', 'php_path', $installer->getPHPPath());
138                         }
139
140                         $configCache->set('config', 'admin_email',
141                                 $this->getOption(['A', 'admin'],
142                                         !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
143                         $configCache->set('system', 'default_timezone',
144                                 $this->getOption(['T', 'tz'],
145                                         !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
146                         $configCache->set('system', 'language',
147                                 $this->getOption(['L', 'lang'],
148                                         !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
149
150                         $basepath = $this->getOption(['b', 'basepath'], !empty(getenv('FRIENDICA_BASE_PATH')) ? getenv('FRIENDICA_BASE_PATH') : null);
151                         if (!empty($basepath)) {
152                                 $configCache->set('system', 'basepath', $basepath);
153                         }
154
155                         $url = $this->getOption(['U', 'url'], !empty(getenv('FRIENDICA_URL')) ? getenv('FRIENDICA_URL') : null);
156
157                         if (empty($url)) {
158                                 $this->out('The Friendica URL has to be set during CLI installation.');
159                                 return 1;
160                         } else {
161                                 $baseUrl = new BaseURL($a->getConfig(), []);
162                                 $baseUrl->saveByURL($url);
163                         }
164
165                         $installer->createConfig($configCache);
166                 }
167
168                 $this->out(" Complete!\n\n");
169
170                 // Check database connection
171                 $this->out("Checking database...\n");
172
173                 $installer->resetChecks();
174
175                 if (!$installer->checkDB($configCache, $a->getProfiler())) {
176                         $errorMessage = $this->extractErrors($installer->getChecks());
177                         throw new RuntimeException($errorMessage);
178                 }
179
180                 $this->out(" Complete!\n\n");
181
182                 // Install database
183                 $this->out("Inserting data into database...\n");
184
185                 $installer->resetChecks();
186
187                 if (!$installer->installDatabase($a->getBasePath())) {
188                         $errorMessage = $this->extractErrors($installer->getChecks());
189                         throw new RuntimeException($errorMessage);
190                 }
191
192                 $this->out(" Complete!\n\n");
193
194                 // Install theme
195                 $this->out("Installing theme\n");
196                 if (!empty(Config::get('system', 'theme'))) {
197                         Theme::install(Config::get('system', 'theme'));
198                         $this->out(" Complete\n\n");
199                 } else {
200                         $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
201                 }
202
203                 $this->out("\nInstallation is finished\n");
204
205                 return 0;
206         }
207
208         /**
209          * @param Installer                 $installer   The Installer instance
210          * @param Config\Cache\IConfigCache $configCache The config cache
211          *
212          * @return bool true if checks were successfully, otherwise false
213          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
214          */
215         private function runBasicChecks(Installer $installer, Config\Cache\IConfigCache $configCache)
216         {
217                 $checked = true;
218
219                 $installer->resetChecks();
220                 if (!$installer->checkFunctions())              {
221                         $checked = false;
222                 }
223                 if (!$installer->checkImagick()) {
224                         $checked = false;
225                 }
226                 if (!$installer->checkLocalIni()) {
227                         $checked = false;
228                 }
229                 if (!$installer->checkSmarty3()) {
230                         $checked = false;
231                 }
232                 if (!$installer->checkKeys()) {
233                         $checked = false;
234                 }
235
236                 $php_path = $configCache->get('config', 'php_path');
237
238                 if (!$installer->checkPHP($php_path, true)) {
239                         $checked = false;
240                 }
241
242                 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
243
244                 return $checked;
245         }
246
247         /**
248          * @param array $results
249          * @return string
250          */
251         private function extractErrors($results)
252         {
253                 $errorMessage = '';
254                 $allChecksRequired = $this->getOption('a') !== null;
255
256                 foreach ($results as $result) {
257                         if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
258                                 $errorMessage .= "--------\n";
259                                 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
260                         }
261                 }
262
263                 return $errorMessage;
264         }
265 }