]> git.mxchange.org Git - friendica.git/blob - src/Console/AutomaticInstallation.php
Merge pull request #8033 from annando/contact-logging
[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\IConfiguration
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\IConfiguration $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
133                         if (!file_exists($config_file)) {
134                                 throw new RuntimeException("ERROR: Config file does not exist.\n");
135                         }
136
137                         //reload the config cache
138                         $loader = new ConfigFileLoader($config_file);
139                         $loader->setupCache($configCache);
140
141                 } else {
142                         // Creating config file
143                         $this->out("Creating config file...\n");
144
145                         $save_db = $this->getOption(['s', 'savedb'], false);
146
147                         $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
148                         $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
149                         $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
150                         $configCache->set('database', 'database',
151                                 $this->getOption(['d', 'dbdata'],
152                                         ($save_db) ? getenv('MYSQL_DATABASE') : ''));
153                         $configCache->set('database', 'username',
154                                 $this->getOption(['U', 'dbuser'],
155                                         ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
156                         $configCache->set('database', 'password',
157                                 $this->getOption(['P', 'dbpass'],
158                                         ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
159
160                         $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
161                         if (!empty($php_path)) {
162                                 $configCache->set('config', 'php_path', $php_path);
163                         } else {
164                                 $configCache->set('config', 'php_path', $installer->getPHPPath());
165                         }
166
167                         $configCache->set('config', 'admin_email',
168                                 $this->getOption(['A', 'admin'],
169                                         !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
170                         $configCache->set('system', 'default_timezone',
171                                 $this->getOption(['T', 'tz'],
172                                         !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
173                         $configCache->set('system', 'language',
174                                 $this->getOption(['L', 'lang'],
175                                         !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
176
177                         $basepath = $this->getOption(['b', 'basepath'], !empty(getenv('FRIENDICA_BASE_PATH')) ? getenv('FRIENDICA_BASE_PATH') : null);
178                         if (!empty($basepath)) {
179                                 $configCache->set('system', 'basepath', $basepath);
180                         }
181
182                         $url = $this->getOption(['U', 'url'], !empty(getenv('FRIENDICA_URL')) ? getenv('FRIENDICA_URL') : null);
183
184                         if (empty($url)) {
185                                 $this->out('The Friendica URL has to be set during CLI installation.');
186                                 return 1;
187                         } else {
188                                 $baseUrl = new BaseURL($this->config, []);
189                                 $baseUrl->saveByURL($url);
190                         }
191
192                         $installer->createConfig($configCache);
193                 }
194
195                 $this->out("Complete!\n\n");
196
197                 // Check database connection
198                 $this->out("Checking database...\n");
199
200                 $installer->resetChecks();
201
202                 if (!$installer->checkDB($this->dba)) {
203                         $errorMessage = $this->extractErrors($installer->getChecks());
204                         throw new RuntimeException($errorMessage);
205                 }
206
207                 $this->out(" Complete!\n\n");
208
209                 // Install database
210                 $this->out("Inserting data into database...\n");
211
212                 $installer->resetChecks();
213
214                 if (!$installer->installDatabase($basePathConf)) {
215                         $errorMessage = $this->extractErrors($installer->getChecks());
216                         throw new RuntimeException($errorMessage);
217                 }
218
219                 if (!empty($config_file) && $config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
220                         // Copy config file
221                         $this->out("Copying config file...\n");
222                         if (!copy($basePathConf . DIRECTORY_SEPARATOR . $config_file, $basePathConf . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
223                                 throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $basePathConf . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.config.php' manually.\n");
224                         }
225                 }
226
227                 $this->out(" Complete!\n\n");
228
229                 // Install theme
230                 $this->out("Installing theme\n");
231                 if (!empty($this->config->get('system', 'theme'))) {
232                         Theme::install($this->config->get('system', 'theme'));
233                         $this->out(" Complete\n\n");
234                 } else {
235                         $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
236                 }
237
238                 $this->out("\nInstallation is finished\n");
239
240                 return 0;
241         }
242
243         /**
244          * @param Installer                 $installer   The Installer instance
245          * @param Config\Cache\ConfigCache $configCache The config cache
246          *
247          * @return bool true if checks were successfully, otherwise false
248          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
249          */
250         private function runBasicChecks(Installer $installer, Config\Cache\ConfigCache $configCache)
251         {
252                 $checked = true;
253
254                 $installer->resetChecks();
255                 if (!$installer->checkFunctions())              {
256                         $checked = false;
257                 }
258                 if (!$installer->checkImagick()) {
259                         $checked = false;
260                 }
261                 if (!$installer->checkLocalIni()) {
262                         $checked = false;
263                 }
264                 if (!$installer->checkSmarty3()) {
265                         $checked = false;
266                 }
267                 if (!$installer->checkKeys()) {
268                         $checked = false;
269                 }
270
271                 $php_path = $configCache->get('config', 'php_path');
272
273                 if (!$installer->checkPHP($php_path, true)) {
274                         $checked = false;
275                 }
276
277                 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
278
279                 return $checked;
280         }
281
282         /**
283          * @param array $results
284          * @return string
285          */
286         private function extractErrors($results)
287         {
288                 $errorMessage = '';
289                 $allChecksRequired = $this->getOption('a') !== null;
290
291                 foreach ($results as $result) {
292                         if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
293                                 $errorMessage .= "--------\n";
294                                 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
295                         }
296                 }
297
298                 return $errorMessage;
299         }
300 }