]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/AutomaticInstallation.php
150b7e52a77447d85d3a7b7b5197b008a171263b
[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.config.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.config.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.config.php
62                 Installs Friendica with the prepared 'input.config.php' file
63
64         bin/console autoinstall --savedb
65                 Installs Friendica with environment variables and saves them to the 'config/local.config.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                 $this->out(" Complete!\n\n");
82
83                 // Check Environment
84                 $this->out("Checking environment...\n");
85
86                 $installer->resetChecks();
87
88                 if (!$this->runBasicChecks($installer)) {
89                         $errorMessage = $this->extractErrors($installer->getChecks());
90                         throw new RuntimeException($errorMessage);
91                 }
92
93                 $this->out(" Complete!\n\n");
94
95                 // if a config file is set,
96                 $config_file = $this->getOption(['f', 'file']);
97
98                 if (!empty($config_file)) {
99                         if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
100                                 // Copy config file
101                                 $this->out("Copying config file...\n");
102                                 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
103                                         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");
104                                 }
105                         }
106
107                         $db_host = $a->getConfigValue('database', 'hostname');
108                         $db_user = $a->getConfigValue('database', 'username');
109                         $db_pass = $a->getConfigValue('database', 'password');
110                         $db_data = $a->getConfigValue('database', 'database');
111                 } else {
112                         // Creating config file
113                         $this->out("Creating config file...\n");
114
115                         $save_db = $this->getOption(['s', 'savedb'], false);
116
117                         $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? getenv('MYSQL_HOST') : '');
118                         $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
119                         $db_data = $this->getOption(['d', 'dbdata'], ($save_db) ? getenv('MYSQL_DATABASE') : '');
120                         $db_user = $this->getOption(['U', 'dbuser'], ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : '');
121                         $db_pass = $this->getOption(['P', 'dbpass'], ($save_db) ? getenv('MYSQL_PASSWORD') : '');
122                         $url_path = $this->getOption(['u', 'urlpath'], (!empty('FRIENDICA_URL_PATH')) ? getenv('FRIENDICA_URL_PATH') : null);
123                         $php_path = $this->getOption(['b', 'phppath'], (!empty('FRIENDICA_PHP_PATH')) ? getenv('FRIENDICA_PHP_PATH') : null);
124                         $admin_mail = $this->getOption(['A', 'admin'], (!empty('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : '');
125                         $tz = $this->getOption(['T', 'tz'], (!empty('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : '');
126                         $lang = $this->getOption(['L', 'lang'], (!empty('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : '');
127
128                         if (empty($php_path)) {
129                                 $php_path = $installer->getPHPPath();
130                         }
131
132                         $installer->createConfig(
133                                 $php_path,
134                                 $url_path,
135                                 ((!empty($db_port)) ? $db_host . ':' . $db_port : $db_host),
136                                 $db_user,
137                                 $db_pass,
138                                 $db_data,
139                                 $tz,
140                                 $lang,
141                                 $admin_mail,
142                                 $a->getBasePath()
143                         );
144                 }
145
146                 $this->out(" Complete!\n\n");
147
148                 // Check database connection
149                 $this->out("Checking database...\n");
150
151                 $installer->resetChecks();
152
153                 if (!$installer->checkDB($db_host, $db_user, $db_pass, $db_data)) {
154                         $errorMessage = $this->extractErrors($installer->getChecks());
155                         throw new RuntimeException($errorMessage);
156                 }
157
158                 $this->out(" Complete!\n\n");
159
160                 // Install database
161                 $this->out("Inserting data into database...\n");
162
163                 $installer->resetChecks();
164
165                 if (!$installer->installDatabase()) {
166                         $errorMessage = $this->extractErrors($installer->getChecks());
167                         throw new RuntimeException($errorMessage);
168                 }
169
170                 $this->out(" Complete!\n\n");
171
172                 // Install theme
173                 $this->out("Installing theme\n");
174                 if (!empty(Config::get('system', 'theme'))) {
175                         Theme::install(Config::get('system', 'theme'));
176                         $this->out(" Complete\n\n");
177                 } else {
178                         $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
179                 }
180
181                 $this->out("\nInstallation is finished\n");
182
183                 return 0;
184         }
185
186         /**
187          * @param Installer $installer the Installer instance
188          *
189          * @return bool true if checks were successfully, otherwise false
190          */
191         private function runBasicChecks(Installer $installer)
192         {
193                 $checked = true;
194
195                 $installer->resetChecks();
196                 if (!$installer->checkFunctions())              {
197                         $checked = false;
198                 }
199                 if (!$installer->checkImagick()) {
200                         $checked = false;
201                 }
202                 if (!$installer->checkLocalIni()) {
203                         $checked = false;
204                 }
205                 if (!$installer->checkSmarty3()) {
206                         $checked = false;
207                 }
208                 if (!$installer->checkKeys()) {
209                         $checked = false;
210                 }
211
212                 $php_path = null;
213                 if (!empty(Config::get('config', 'php_path'))) {
214                         $php_path = Config::get('config', 'php_path');
215                 }
216
217                 if (!$installer->checkPHP($php_path, true)) {
218                         $checked = false;
219                 }
220
221                 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
222
223                 return $checked;
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 }