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