]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/AutomaticInstallation.php
e0eea1f507abf474a6f2d0fecbbd0a73b6ad2d04
[friendica.git] / src / Core / Console / AutomaticInstallation.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Asika\SimpleConsole\Console;
6 use dba;
7 use Friendica\App;
8 use Friendica\Core\Install;
9 use Friendica\Core\Theme;
10
11 require_once 'mod/install.php';
12 require_once 'include/dba.php';
13
14 class AutomaticInstallation extends Console
15 {
16         protected function getHelp()
17         {
18                 return <<<HELP
19 Installation - Install Friendica automatically
20 Synopsis
21         bin/console autoinstall [-h|--help|-?] [-v] [-a] 
22         
23 Description
24     Installs Friendica with data based on the htconfig.php file
25
26 Notes:
27     Not checking .htaccess/URL-Rewrite during CLI installation.
28
29 Options
30     -h|--help|-? Show help information
31     -v           Show more debug information.
32     -a           All setup checks are required (except .htaccess)
33     -f           prepared config file (e.g. ".htconfig.php" itself)
34 HELP;
35         }
36
37         protected function doExecute()
38         {
39                 // Initialise the app
40                 $this->out("Initializing setup...\n");
41
42                 $a = get_app();
43                 $db_host = '';
44                 $db_user = '';
45                 $db_pass = '';
46                 $db_data = '';
47
48                 $config_file = $this->getOption('f', 'htconfig.php');
49
50                 $this->out("Using config $config_file...\n");
51                 require_once $config_file;
52
53                 Install::setInstallMode();
54
55                 $this->out(" Complete!\n\n");
56
57                 // Check basic setup
58                 $this->out("Checking basic setup...\n");
59
60                 $checkResults = [];
61                 $checkResults['basic'] = $this->runBasicChecks($a);
62                 $errorMessage = $this->extractErrors($checkResults['basic']);
63
64                 if ($errorMessage !== '') {
65                         throw new \RuntimeException($errorMessage);
66                 }
67
68                 $this->out(" Complete!\n\n");
69
70                 // Check database connection
71                 $this->out("Checking database...\n");
72
73                 $checkResults['db'] = array();
74                 $checkResults['db'][] = $this->runDatabaseCheck($db_host, $db_user, $db_pass, $db_data);
75                 $errorMessage = $this->extractErrors($checkResults['db']);
76
77                 if ($errorMessage !== '') {
78                         throw new \RuntimeException($errorMessage);
79                 }
80
81                 $this->out(" Complete!\n\n");
82
83                 // Install database
84                 $this->out("Inserting data into database...\n");
85
86                 $checkResults['data'] = Install::installDatabaseStructure();
87
88                 if ($checkResults['data'] !== '') {
89                         throw new \RuntimeException("ERROR: DB Database creation error. Is the DB empty?\n");
90                 }
91
92                 $this->out(" Complete!\n\n");
93
94                 // Install theme
95                 $this->out("Installing theme\n");
96                 if (!empty($a->config['system']['theme'])) {
97                         Theme::install($a->config['system']['theme']);
98                         $this->out(" Complete\n\n");
99                 } else {
100                         $this->out(" Theme setting is empty. Please check the file htconfig.php\n\n");
101                 }
102
103                 // Copy config file
104                 $this->out("Saving config file...\n");
105                 if ($config_file != '.htconfig.php' && !copy($config_file, '.htconfig.php')) {
106                         throw new \RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '.htconfig.php' manually.\n");
107                 }
108                 $this->out(" Complete!\n\n");
109                 $this->out("\nInstallation is finished\n");
110
111                 return 0;
112         }
113
114         /**
115          * @param App $app
116          * @return array
117          */
118         private function runBasicChecks($app)
119         {
120                 $checks = [];
121
122                 Install::checkFunctions($checks);
123                 Install::checkImagick($checks);
124                 Install::checkHtConfig($checks);
125                 Install::checkSmarty3($checks);
126                 Install::checkKeys($checks);
127
128                 if (!empty($app->config['php_path'])) {
129                         Install::checkPHP($app->config['php_path'], $checks);
130                 } else {
131                         throw new \RuntimeException(" ERROR: The php_path is not set in the config. Please check the file .htconfig.php.\n");
132                 }
133
134                 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
135
136                 return $checks;
137         }
138
139         /**
140          * @param $db_host
141          * @param $db_user
142          * @param $db_pass
143          * @param $db_data
144          * @return array
145          */
146         private function runDatabaseCheck($db_host, $db_user, $db_pass, $db_data)
147         {
148                 $result = array(
149                         'title' => 'MySQL Connection',
150                         'required' => true,
151                         'status' => true,
152                         'help' => '',
153                 );
154
155
156                 if (!dba::connect($db_host, $db_user, $db_pass, $db_data)) {
157                         $result['status'] = false;
158                         $result['help'] = 'Failed, please check your MySQL settings and credentials.';
159                 }
160
161                 return $result;
162         }
163
164         /**
165          * @param array $results
166          * @return string
167          */
168         private function extractErrors($results)
169         {
170                 $errorMessage = '';
171                 $allChecksRequired = $this->getOption('a') !== null;
172
173                 foreach ($results as $result) {
174                         if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
175                                 $errorMessage .= "--------\n";
176                                 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
177                         }
178                 }
179
180                 return $errorMessage;
181         }
182 }