]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/AutomaticInstallation.php
Merge pull request #5906 from annando/ap-switch
[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\Install;
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                 $install = new Install();
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                         $install->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                 $checkResults = [];
134
135                 $this->runBasicChecks($install);
136
137                 $checkResults['basic'] = $install->getChecks();
138                 $errorMessage = $this->extractErrors($checkResults['basic']);
139
140                 if ($errorMessage !== '') {
141                         throw new RuntimeException($errorMessage);
142                 }
143
144                 $this->out(" Complete!\n\n");
145
146                 // Check database connection
147                 $this->out("Checking database...\n");
148
149                 $checkResults['db'] = array();
150                 $checkResults['db'][] = $this->runDatabaseCheck($db_host, $db_user, $db_pass, $db_data);
151                 $errorMessage = $this->extractErrors($checkResults['db']);
152
153                 if ($errorMessage !== '') {
154                         throw new RuntimeException($errorMessage);
155                 }
156
157                 $this->out(" Complete!\n\n");
158
159                 // Install database
160                 $this->out("Inserting data into database...\n");
161
162                 $checkResults['data'] = DBStructure::update(false, true, true);
163
164                 if ($checkResults['data'] !== '') {
165                         throw new RuntimeException("ERROR: DB Database creation error. Is the DB empty?\n");
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.ini.php'\n\n");
177                 }
178
179                 $this->out("\nInstallation is finished\n");
180
181                 return 0;
182         }
183
184         /**
185          * @param Install $install the Installer instance
186          */
187         private function runBasicChecks(Install $install)
188         {
189                 $install->resetChecks();
190                 $install->checkFunctions();
191                 $install->checkImagick();
192                 $install->checkLocalIni();
193                 $install->checkSmarty3();
194                 $install->checkKeys();
195
196                 if (!empty(Config::get('config', 'php_path'))) {
197                         if (!$install->checkPHP(Config::get('config', 'php_path'), true)) {
198                                 throw new RuntimeException(" ERROR: The php_path is not valid in the config.\n");
199                         }
200                 } else {
201                         throw new RuntimeException(" ERROR: The php_path is not set in the config.\n");
202                 }
203
204                 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
205         }
206
207         /**
208          * @param $db_host
209          * @param $db_user
210          * @param $db_pass
211          * @param $db_data
212          *
213          * @return array
214          */
215         private function runDatabaseCheck($db_host, $db_user, $db_pass, $db_data)
216         {
217                 $result = array(
218                         'title' => 'MySQL Connection',
219                         'required' => true,
220                         'status' => true,
221                         'help' => '',
222                 );
223
224
225                 if (!DBA::connect($db_host, $db_user, $db_pass, $db_data)) {
226                         $result['status'] = false;
227                         $result['help'] = 'Failed, please check your MySQL settings and credentials.';
228                 }
229
230                 return $result;
231         }
232
233         /**
234          * @param array $results
235          * @return string
236          */
237         private function extractErrors($results)
238         {
239                 $errorMessage = '';
240                 $allChecksRequired = $this->getOption('a') !== null;
241
242                 foreach ($results as $result) {
243                         if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
244                                 $errorMessage .= "--------\n";
245                                 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
246                         }
247                 }
248
249                 return $errorMessage;
250         }
251 }