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