]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/AutomaticInstallation.php
trim whitespaces
[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\Util\BasePath;
11 use Friendica\Util\BaseURL;
12 use Friendica\Util\Config\ConfigFileLoader;
13 use RuntimeException;
14
15 class AutomaticInstallation extends Console
16 {
17         protected function getHelp()
18         {
19                 return <<<HELP
20 Installation - Install Friendica automatically
21 Synopsis
22         bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
23
24 Description
25     Installs Friendica with data based on the local.config.php file or environment variables
26
27 Notes
28     Not checking .htaccess/URL-Rewrite during CLI installation.
29
30 Options
31     -h|--help|-?            Show help information
32     -v                      Show more debug information.
33     -a                      All setup checks are required (except .htaccess)
34     -f|--file <config>      prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
35     -s|--savedb               Save the DB credentials to the file (if environment variables is used)
36     -H|--dbhost <host>        The host of the mysql/mariadb database (env MYSQL_HOST)
37     -p|--dbport <port>        The port of the mysql/mariadb database (env MYSQL_PORT)
38     -d|--dbdata <database>    The name of the mysql/mariadb database (env MYSQL_DATABASE)
39     -U|--dbuser <username>    The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
40     -P|--dbpass <password>    The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
41     -U|--url <url>            The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL) 
42     -B|--phppath <php_path>   The path of the PHP binary (env FRIENDICA_PHP_PATH)
43     -b|--basepath <base_path> The basepath of Friendica(env FRIENDICA_BASE_PATH)
44     -t|--tz <timezone>        The timezone of Friendica (env FRIENDICA_TZ)
45     -L|--lang <language>      The language of Friendica (env FRIENDICA_LANG)
46  
47 Environment variables
48    MYSQL_HOST                  The host of the mysql/mariadb database (mandatory if mysql and environment is used)
49    MYSQL_PORT                  The port of the mysql/mariadb database
50    MYSQL_USERNAME|MYSQL_USER   The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
51    MYSQL_PASSWORD              The password of the mysql/mariadb database login
52    MYSQL_DATABASE              The name of the mysql/mariadb database
53    FRIENDICA_URL               The full base URL of Friendica - f.e. 'https://friendica.local/sub'
54    FRIENDICA_PHP_PATH          The path of the PHP binary - leave empty for auto detection
55    FRIENDICA_BASE_PATH         The basepath of Friendica - leave empty for auto detection
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                 $configCache = $a->getConfigCache();
82                 $installer->setUpCache($configCache, BasePath::create($a->getBasePath(), $_SERVER));
83
84                 $this->out(" Complete!\n\n");
85
86                 // Check Environment
87                 $this->out("Checking environment...\n");
88
89                 $installer->resetChecks();
90
91                 if (!$this->runBasicChecks($installer, $configCache)) {
92                         $errorMessage = $this->extractErrors($installer->getChecks());
93                         throw new RuntimeException($errorMessage);
94                 }
95
96                 $this->out(" Complete!\n\n");
97
98                 // if a config file is set,
99                 $config_file = $this->getOption(['f', 'file']);
100
101                 if (!empty($config_file)) {
102                         if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
103                                 // Copy config file
104                                 $this->out("Copying config file...\n");
105                                 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
106                                         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");
107                                 }
108                         }
109
110                         //reload the config cache
111                         $loader = new ConfigFileLoader($a->getBasePath(), $a->getMode());
112                         $loader->setupCache($configCache);
113
114                 } else {
115                         // Creating config file
116                         $this->out("Creating config file...\n");
117
118                         $save_db = $this->getOption(['s', 'savedb'], false);
119
120                         $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
121                         $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
122                         $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
123                         $configCache->set('database', 'database',
124                                 $this->getOption(['d', 'dbdata'],
125                                         ($save_db) ? getenv('MYSQL_DATABASE') : ''));
126                         $configCache->set('database', 'username',
127                                 $this->getOption(['U', 'dbuser'],
128                                         ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
129                         $configCache->set('database', 'password',
130                                 $this->getOption(['P', 'dbpass'],
131                                         ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
132                         $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
133                         if (!empty($php_path)) {
134                                 $configCache->set('config', 'php_path', $php_path);
135                         } else {
136                                 $configCache->set('config', 'php_path', $installer->getPHPPath());
137                         }
138
139                         $configCache->set('config', 'admin_email',
140                                 $this->getOption(['A', 'admin'],
141                                         !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
142                         $configCache->set('system', 'default_timezone',
143                                 $this->getOption(['T', 'tz'],
144                                         !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
145                         $configCache->set('system', 'language',
146                                 $this->getOption(['L', 'lang'],
147                                         !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
148
149                         $basepath = $this->getOption(['b', 'basepath'], !empty(getenv('FRIENDICA_BASE_PATH')) ? getenv('FRIENDICA_BASE_PATH') : null);
150                         if (!empty($basepath)) {
151                                 $configCache->set('system', 'basepath', $basepath);
152                         }
153                         $php_path = $this->getOption(['B', 'phppath'], !empty(getenv('FRIENDICA_PHP_PATH')) ? getenv('FRIENDICA_PHP_PATH') : null);
154                         if (!empty($php_path)) {
155                                 $configCache->set('config', 'php_path', $php_path);
156                         }
157
158                         $url = $this->getOption(['U', 'url'], !empty(getenv('FRIENDICA_URL')) ? getenv('FRIENDICA_URL') : null);
159
160                         if (empty($url)) {
161                                 $this->out('The Friendica URL has to be set during CLI installation.');
162                                 return 1;
163                         } else {
164                                 $baseUrl = new BaseURL($a->getConfig(), []);
165                                 $baseUrl->saveByURL($url);
166                         }
167
168                         $installer->createConfig($configCache);
169                 }
170
171                 $this->out(" Complete!\n\n");
172
173                 // Check database connection
174                 $this->out("Checking database...\n");
175
176                 $installer->resetChecks();
177
178                 if (!$installer->checkDB($configCache, $a->getProfiler())) {
179                         $errorMessage = $this->extractErrors($installer->getChecks());
180                         throw new RuntimeException($errorMessage);
181                 }
182
183                 $this->out(" Complete!\n\n");
184
185                 // Install database
186                 $this->out("Inserting data into database...\n");
187
188                 $installer->resetChecks();
189
190                 if (!$installer->installDatabase($a->getBasePath())) {
191                         $errorMessage = $this->extractErrors($installer->getChecks());
192                         throw new RuntimeException($errorMessage);
193                 }
194
195                 $this->out(" Complete!\n\n");
196
197                 // Install theme
198                 $this->out("Installing theme\n");
199                 if (!empty(Config::get('system', 'theme'))) {
200                         Theme::install(Config::get('system', 'theme'));
201                         $this->out(" Complete\n\n");
202                 } else {
203                         $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
204                 }
205
206                 $this->out("\nInstallation is finished\n");
207
208                 return 0;
209         }
210
211         /**
212          * @param Installer                 $installer   The Installer instance
213          * @param Config\Cache\IConfigCache $configCache The config cache
214          *
215          * @return bool true if checks were successfully, otherwise false
216          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
217          */
218         private function runBasicChecks(Installer $installer, Config\Cache\IConfigCache $configCache)
219         {
220                 $checked = true;
221
222                 $installer->resetChecks();
223                 if (!$installer->checkFunctions())              {
224                         $checked = false;
225                 }
226                 if (!$installer->checkImagick()) {
227                         $checked = false;
228                 }
229                 if (!$installer->checkLocalIni()) {
230                         $checked = false;
231                 }
232                 if (!$installer->checkSmarty3()) {
233                         $checked = false;
234                 }
235                 if (!$installer->checkKeys()) {
236                         $checked = false;
237                 }
238
239                 $php_path = $configCache->get('config', 'php_path');
240
241                 if (!$installer->checkPHP($php_path, true)) {
242                         $checked = false;
243                 }
244
245                 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
246
247                 return $checked;
248         }
249
250         /**
251          * @param array $results
252          * @return string
253          */
254         private function extractErrors($results)
255         {
256                 $errorMessage = '';
257                 $allChecksRequired = $this->getOption('a') !== null;
258
259                 foreach ($results as $result) {
260                         if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
261                                 $errorMessage .= "--------\n";
262                                 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
263                         }
264                 }
265
266                 return $errorMessage;
267         }
268 }