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