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