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