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