]> git.mxchange.org Git - friendica.git/blob - src/Console/AutomaticInstallation.php
d594b2605e19375d25c8a1b577fcdfaff03782bc
[friendica.git] / src / Console / AutomaticInstallation.php
1 <?php
2
3 namespace Friendica\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\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                 $basepath = new BasePath($a->getBasePath());
83                 $installer->setUpCache($configCache, $basepath->getPath());
84
85                 $this->out(" Complete!\n\n");
86
87                 // Check Environment
88                 $this->out("Checking environment...\n");
89
90                 $installer->resetChecks();
91
92                 if (!$this->runBasicChecks($installer, $configCache)) {
93                         $errorMessage = $this->extractErrors($installer->getChecks());
94                         throw new RuntimeException($errorMessage);
95                 }
96
97                 $this->out(" Complete!\n\n");
98
99                 // if a config file is set,
100                 $config_file = $this->getOption(['f', 'file']);
101
102                 if (!empty($config_file)) {
103                         if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
104                                 // Copy config file
105                                 $this->out("Copying config file...\n");
106                                 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
107                                         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");
108                                 }
109                         }
110
111                         //reload the config cache
112                         $loader = new ConfigFileLoader($a->getBasePath(), $a->getMode());
113                         $loader->setupCache($configCache);
114
115                 } else {
116                         // Creating config file
117                         $this->out("Creating config file...\n");
118
119                         $save_db = $this->getOption(['s', 'savedb'], false);
120
121                         $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
122                         $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
123                         $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
124                         $configCache->set('database', 'database',
125                                 $this->getOption(['d', 'dbdata'],
126                                         ($save_db) ? getenv('MYSQL_DATABASE') : ''));
127                         $configCache->set('database', 'username',
128                                 $this->getOption(['U', 'dbuser'],
129                                         ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
130                         $configCache->set('database', 'password',
131                                 $this->getOption(['P', 'dbpass'],
132                                         ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
133
134                         $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
135                         if (!empty($php_path)) {
136                                 $configCache->set('config', 'php_path', $php_path);
137                         } else {
138                                 $configCache->set('config', 'php_path', $installer->getPHPPath());
139                         }
140
141                         $configCache->set('config', 'admin_email',
142                                 $this->getOption(['A', 'admin'],
143                                         !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
144                         $configCache->set('system', 'default_timezone',
145                                 $this->getOption(['T', 'tz'],
146                                         !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
147                         $configCache->set('system', 'language',
148                                 $this->getOption(['L', 'lang'],
149                                         !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
150
151                         $basepath = $this->getOption(['b', 'basepath'], !empty(getenv('FRIENDICA_BASE_PATH')) ? getenv('FRIENDICA_BASE_PATH') : null);
152                         if (!empty($basepath)) {
153                                 $configCache->set('system', 'basepath', $basepath);
154                         }
155
156                         $url = $this->getOption(['U', 'url'], !empty(getenv('FRIENDICA_URL')) ? getenv('FRIENDICA_URL') : null);
157
158                         if (empty($url)) {
159                                 $this->out('The Friendica URL has to be set during CLI installation.');
160                                 return 1;
161                         } else {
162                                 $baseUrl = new BaseURL($a->getConfig(), []);
163                                 $baseUrl->saveByURL($url);
164                         }
165
166                         $installer->createConfig($configCache);
167                 }
168
169                 $this->out(" Complete!\n\n");
170
171                 // Check database connection
172                 $this->out("Checking database...\n");
173
174                 $installer->resetChecks();
175
176                 if (!$installer->checkDB($configCache, $a->getProfiler())) {
177                         $errorMessage = $this->extractErrors($installer->getChecks());
178                         throw new RuntimeException($errorMessage);
179                 }
180
181                 $this->out(" Complete!\n\n");
182
183                 // Install database
184                 $this->out("Inserting data into database...\n");
185
186                 $installer->resetChecks();
187
188                 if (!$installer->installDatabase($a->getBasePath())) {
189                         $errorMessage = $this->extractErrors($installer->getChecks());
190                         throw new RuntimeException($errorMessage);
191                 }
192
193                 $this->out(" Complete!\n\n");
194
195                 // Install theme
196                 $this->out("Installing theme\n");
197                 if (!empty(Config::get('system', 'theme'))) {
198                         Theme::install(Config::get('system', 'theme'));
199                         $this->out(" Complete\n\n");
200                 } else {
201                         $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
202                 }
203
204                 $this->out("\nInstallation is finished\n");
205
206                 return 0;
207         }
208
209         /**
210          * @param Installer                 $installer   The Installer instance
211          * @param Config\Cache\ConfigCache $configCache The config cache
212          *
213          * @return bool true if checks were successfully, otherwise false
214          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
215          */
216         private function runBasicChecks(Installer $installer, Config\Cache\ConfigCache $configCache)
217         {
218                 $checked = true;
219
220                 $installer->resetChecks();
221                 if (!$installer->checkFunctions())              {
222                         $checked = false;
223                 }
224                 if (!$installer->checkImagick()) {
225                         $checked = false;
226                 }
227                 if (!$installer->checkLocalIni()) {
228                         $checked = false;
229                 }
230                 if (!$installer->checkSmarty3()) {
231                         $checked = false;
232                 }
233                 if (!$installer->checkKeys()) {
234                         $checked = false;
235                 }
236
237                 $php_path = $configCache->get('config', 'php_path');
238
239                 if (!$installer->checkPHP($php_path, true)) {
240                         $checked = false;
241                 }
242
243                 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
244
245                 return $checked;
246         }
247
248         /**
249          * @param array $results
250          * @return string
251          */
252         private function extractErrors($results)
253         {
254                 $errorMessage = '';
255                 $allChecksRequired = $this->getOption('a') !== null;
256
257                 foreach ($results as $result) {
258                         if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
259                                 $errorMessage .= "--------\n";
260                                 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
261                         }
262                 }
263
264                 return $errorMessage;
265         }
266 }