]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/AutomaticInstallation.php
e888d5a4908cded5b7d018271d24457868329dd5
[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
10 require_once 'mod/install.php';
11 require_once 'include/dba.php';
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] 
21         
22 Description
23     Installs Friendica with data based on the htconfig.php file
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 HELP;
33         }
34
35         protected function doExecute()
36         {
37                 // Initialise the app
38                 $this->out("Initializing setup...\n");
39
40                 $a = get_app();
41                 $db_host = '';
42                 $db_user = '';
43                 $db_pass = '';
44                 $db_data = '';
45                 require_once 'htconfig.php';
46
47                 Install::setInstallMode();
48
49                 $this->out(" Complete!\n\n");
50
51                 // Check basic setup
52                 $this->out("Checking basic setup...\n");
53
54                 $checkResults = [];
55                 $checkResults['basic'] = $this->runBasicChecks($a);
56                 $errorMessage = $this->extractErrors($checkResults['basic']);
57
58                 if ($errorMessage !== '') {
59                         throw new \RuntimeException($errorMessage);
60                 }
61
62                 $this->out(" Complete!\n\n");
63
64                 // Check database connection
65                 $this->out("Checking database...\n");
66
67                 $checkResults['db'] = array();
68                 $checkResults['db'][] = $this->runDatabaseCheck($db_host, $db_user, $db_pass, $db_data);
69                 $errorMessage = $this->extractErrors($checkResults['db']);
70
71                 if ($errorMessage !== '') {
72                         throw new \RuntimeException($errorMessage);
73                 }
74
75                 $this->out(" Complete!\n\n");
76
77                 // Install database
78                 $this->out("Inserting data into database...\n");
79
80                 $checkResults['data'] = Install::installDatabaseStructure();
81
82                 if ($checkResults['data'] !== '') {
83                         throw new \RuntimeException("ERROR: DB Database creation error. Is the DB empty?\n");
84                 }
85
86                 $this->out(" Complete!\n\n");
87
88                 // Copy config file
89                 $this->out("Saving config file...\n");
90                 if (!copy('htconfig.php', '.htconfig.php')) {
91                         throw new \RuntimeException("ERROR: Saving config file failed. Please copy .htautoinstall.php to .htconfig.php manually.\n");
92                 }
93                 $this->out(" Complete!\n\n");
94                 $this->out("\nInstallation is finished\n");
95
96                 return 0;
97         }
98
99         /**
100          * @param App $app
101          * @return array
102          */
103         private function runBasicChecks($app)
104         {
105                 $checks = [];
106
107                 Install::checkFunctions($checks);
108                 Install::checkImagick($checks);
109                 Install::checkHtConfig($checks);
110                 Install::checkSmarty3($checks);
111                 Install::checkKeys($checks);
112
113                 if (!empty($app->config['php_path'])) {
114                         Install::checkPHP($app->config['php_path'], $checks);
115                 } else {
116                         throw new \RuntimeException(" ERROR: The php_path is not set in the config. Please check the file .htconfig.php.\n");
117                 }
118
119                 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
120
121                 return $checks;
122         }
123
124         /**
125          * @param $db_host
126          * @param $db_user
127          * @param $db_pass
128          * @param $db_data
129          * @return array
130          */
131         private function runDatabaseCheck($db_host, $db_user, $db_pass, $db_data)
132         {
133                 $result = array(
134                         'title' => 'MySQL Connection',
135                         'required' => true,
136                         'status' => true,
137                         'help' => '',
138                 );
139
140
141                 if (!dba::connect($db_host, $db_user, $db_pass, $db_data)) {
142                         $result['status'] = false;
143                         $result['help'] = 'Failed, please check your MySQL settings and credentials.';
144                 }
145
146                 return $result;
147         }
148
149         /**
150          * @param array $results
151          * @return string
152          */
153         private function extractErrors($results)
154         {
155                 $errorMessage = '';
156                 $allChecksRequired = $this->getOption('a') !== null;
157
158                 foreach ($results as $result) {
159                         if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
160                                 $errorMessage .= "--------\n";
161                                 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
162                         }
163                 }
164
165                 return $errorMessage;
166         }
167 }