]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/AutomaticInstallation.php
Rename App Methods
[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     -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH) 
44     -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH) 
45     -A|--admin <mail>       The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
46     -T|--tz <timezone>      The timezone of Friendica (env FRIENDICA_TZ)
47     -L|--lang <language>    The language of Friendica (env FRIENDICA_LANG)
48  
49 Environment variables
50    MYSQL_HOST                  The host of the mysql/mariadb database (mandatory if mysql and environment is used)
51    MYSQL_PORT                  The port of the mysql/mariadb database
52    MYSQL_USERNAME|MYSQL_USER   The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
53    MYSQL_PASSWORD              The password of the mysql/mariadb database login
54    MYSQL_DATABASE              The name of the mysql/mariadb database
55    FRIENDICA_URL_PATH          The URL path of Friendica (f.e. '/friendica')
56    FRIENDICA_PHP_PATH          The path of the PHP binary
57    FRIENDICA_ADMIN_MAIL        The admin email address of Friendica (this email will be used for admin access)
58    FRIENDICA_TZ                The timezone of Friendica
59    FRIENDICA_LANG              The langauge of Friendica
60    
61 Examples
62         bin/console autoinstall -f 'input.ini.php
63                 Installs Friendica with the prepared 'input.ini.php' file
64
65         bin/console autoinstall --savedb
66                 Installs Friendica with environment variables and saves them to the 'config/local.ini.php' file
67
68         bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
69                 Installs Friendica with a local mysql database with credentials
70 HELP;
71         }
72
73         protected function doExecute()
74         {
75                 // Initialise the app
76                 $this->out("Initializing setup...\n");
77
78                 $a = BaseObject::getApp();
79
80                 // if a config file is set,
81                 $config_file = $this->getOption(['f', 'file']);
82
83                 if (!empty($config_file)) {
84                         if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.ini.php') {
85                                 // Copy config file
86                                 $this->out("Copying config file...\n");
87                                 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')) {
88                                         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");
89                                 }
90                         }
91
92                         $db_host = $a->getConfigValue('database', 'hostname');
93                         $db_user = $a->getConfigValue('database', 'username');
94                         $db_pass = $a->getConfigValue('database', 'password');
95                         $db_data = $a->getConfigValue('database', 'database');
96                 } else {
97                         // Creating config file
98                         $this->out("Creating config file...\n");
99
100                         $save_db = $this->getOption(['s', 'savedb'], false);
101
102                         $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? getenv('MYSQL_HOST') : '');
103                         $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
104                         $db_data = $this->getOption(['d', 'dbdata'], ($save_db) ? getenv('MYSQL_DATABASE') : '');
105                         $db_user = $this->getOption(['U', 'dbuser'], ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : '');
106                         $db_pass = $this->getOption(['P', 'dbpass'], ($save_db) ? getenv('MYSQL_PASSWORD') : '');
107                         $url_path = $this->getOption(['u', 'urlpath'], (!empty('FRIENDICA_URL_PATH')) ? getenv('FRIENDICA_URL_PATH') : null);
108                         $php_path = $this->getOption(['b', 'phppath'], (!empty('FRIENDICA_PHP_PATH')) ? getenv('FRIENDICA_PHP_PATH') : '');
109                         $admin_mail = $this->getOption(['A', 'admin'], (!empty('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : '');
110                         $tz = $this->getOption(['T', 'tz'], (!empty('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : '');
111                         $lang = $this->getOption(['L', 'lang'], (!empty('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : '');
112
113                         Install::createConfig(
114                                 $url_path,
115                                 ((!empty($db_port)) ? $db_host . ':' . $db_port : $db_host),
116                                 $db_user,
117                                 $db_pass,
118                                 $db_data,
119                                 $php_path,
120                                 $tz,
121                                 $lang,
122                                 $admin_mail
123                         );
124                 }
125
126                 $this->out(" Complete!\n\n");
127
128                 // Check basic setup
129                 $this->out("Checking basic setup...\n");
130
131                 $checkResults = [];
132                 $checkResults['basic'] = $this->runBasicChecks($a);
133                 $errorMessage = $this->extractErrors($checkResults['basic']);
134
135                 if ($errorMessage !== '') {
136                         throw new RuntimeException($errorMessage);
137                 }
138
139                 $this->out(" Complete!\n\n");
140
141                 // Check database connection
142                 $this->out("Checking database...\n");
143
144                 $checkResults['db'] = array();
145                 $checkResults['db'][] = $this->runDatabaseCheck($db_host, $db_user, $db_pass, $db_data);
146                 $errorMessage = $this->extractErrors($checkResults['db']);
147
148                 if ($errorMessage !== '') {
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                 $checkResults['data'] = Install::installDatabaseStructure();
158
159                 if ($checkResults['data'] !== '') {
160                         throw new RuntimeException("ERROR: DB Database creation error. Is the DB empty?\n");
161                 }
162
163                 $this->out(" Complete!\n\n");
164
165                 // Install theme
166                 $this->out("Installing theme\n");
167                 if (!empty(Config::get('system', 'theme'))) {
168                         Theme::install(Config::get('system', 'theme'));
169                         $this->out(" Complete\n\n");
170                 } else {
171                         $this->out(" Theme setting is empty. Please check the file 'config/local.ini.php'\n\n");
172                 }
173
174                 $this->out("\nInstallation is finished\n");
175
176                 return 0;
177         }
178
179         /**
180          * @param App $app
181          * @return array
182          */
183         private function runBasicChecks($app)
184         {
185                 $checks = [];
186
187                 Install::checkFunctions($checks);
188                 Install::checkImagick($checks);
189                 Install::checkLocalIni($checks);
190                 Install::checkSmarty3($checks);
191                 Install::checkKeys($checks);
192
193                 if (!empty(Config::get('config', 'php_path'))) {
194                         Install::checkPHP(Config::get('config', 'php_path'), $checks);
195                 } else {
196                         throw new RuntimeException(" ERROR: The php_path is not set in the config.\n");
197                 }
198
199                 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
200
201                 return $checks;
202         }
203
204         /**
205          * @param $db_host
206          * @param $db_user
207          * @param $db_pass
208          * @param $db_data
209          * @return array
210          */
211         private function runDatabaseCheck($db_host, $db_user, $db_pass, $db_data)
212         {
213                 $result = array(
214                         'title' => 'MySQL Connection',
215                         'required' => true,
216                         'status' => true,
217                         'help' => '',
218                 );
219
220
221                 if (!DBA::connect($db_host, $db_user, $db_pass, $db_data)) {
222                         $result['status'] = false;
223                         $result['help'] = 'Failed, please check your MySQL settings and credentials.';
224                 }
225
226                 return $result;
227         }
228
229         /**
230          * @param array $results
231          * @return string
232          */
233         private function extractErrors($results)
234         {
235                 $errorMessage = '';
236                 $allChecksRequired = $this->getOption('a') !== null;
237
238                 foreach ($results as $result) {
239                         if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
240                                 $errorMessage .= "--------\n";
241                                 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
242                         }
243                 }
244
245                 return $errorMessage;
246         }
247 }