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