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