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