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