]> git.mxchange.org Git - friendica.git/blob - src/Core/Install.php
8dd354eb91b291da750531b70110a23cc601127c
[friendica.git] / src / Core / Install.php
1 <?php\r
2 /**\r
3  * @file src/Core/Install.php\r
4  */\r
5 namespace Friendica\Core;\r
6 \r
7 use Friendica\BaseObject;\r
8 use Friendica\App;\r
9 use Friendica\Database\DBStructure;\r
10 use Friendica\Object\Image;\r
11 use Friendica\Util\Network;\r
12 \r
13 use Exception;\r
14 use DOMDocument;\r
15 \r
16 /**\r
17  * @brief Contains the class with install relevant stuff *\r
18  */\r
19 class Install extends BaseObject\r
20 {\r
21         public static function setInstallMode()\r
22         {\r
23                 self::getApp()->mode = App::MODE_INSTALL;\r
24         }\r
25 \r
26         public static function check($phpath = 'php')\r
27         {\r
28                 $checks = [];\r
29 \r
30                 self::checkFunctions($checks);\r
31 \r
32                 self::checkImagik($checks);\r
33 \r
34                 self::checkHtConfig($checks);\r
35 \r
36                 self::checkSmarty3($checks);\r
37 \r
38                 self::checkKeys($checks);\r
39 \r
40                 self::checkPHP($phpath, $checks);\r
41 \r
42                 self::checkHtAccess($checks);\r
43 \r
44                 $checkspassed = array_reduce($checks,\r
45                         function ($v, $c) {\r
46                                 if ($c['require']) {\r
47                                         $v = $v && $c['status'];\r
48                                 }\r
49                                 return $v;\r
50                         },\r
51                         true);\r
52 \r
53                 return array($checks, $checkspassed);\r
54         }\r
55 \r
56         public static function install($urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $phpath, $timezone, $language, $adminmail, $rino = 1)\r
57         {\r
58                 $tpl = get_markup_template('htconfig.tpl');\r
59                 $txt = replace_macros($tpl,[\r
60                         '$dbhost' => $dbhost,\r
61                         '$dbuser' => $dbuser,\r
62                         '$dbpass' => $dbpass,\r
63                         '$dbdata' => $dbdata,\r
64                         '$timezone' => $timezone,\r
65                         '$language' => $language,\r
66                         '$urlpath' => $urlpath,\r
67                         '$phpath' => $phpath,\r
68                         '$adminmail' => $adminmail,\r
69                         '$rino' => $rino\r
70                 ]);\r
71 \r
72 \r
73                 $result = file_put_contents('.htconfig.php', $txt);\r
74                 if (! $result) {\r
75                         self::getApp()->data['txt'] = $txt;\r
76                 }\r
77 \r
78                 $errors = self::loadDatabase();\r
79 \r
80                 if ($errors) {\r
81                         self::getApp()->data['db_failed'] = $errors;\r
82                 } else {\r
83                         self::getApp()->data['db_installed'] = true;\r
84                 }\r
85         }\r
86 \r
87         /**\r
88          * checks   : array passed to template\r
89          * title    : string\r
90          * status   : boolean\r
91          * required : boolean\r
92          * help         : string optional\r
93          */\r
94         private static function addCheck(&$checks, $title, $status, $required, $help)\r
95         {\r
96                 $checks[] = [\r
97                         'title' => $title,\r
98                         'status' => $status,\r
99                         'required' => $required,\r
100                         'help' => $help,\r
101                         'error_msg' => $error_msg,\r
102                 ];\r
103         }\r
104 \r
105         public static function checkPHP(&$phpath, &$checks)\r
106         {\r
107                 $passed = $passed2 = $passed3 = false;\r
108                 if (strlen($phpath)) {\r
109                         $passed = file_exists($phpath);\r
110                 } else {\r
111                         $phpath = trim(shell_exec('which php'));\r
112                         $passed = strlen($phpath);\r
113                 }\r
114                 $help = "";\r
115                 if (!$passed) {\r
116                         $help .= L10n::t('Could not find a command line version of PHP in the web server PATH.') . EOL;\r
117                         $help .= L10n::t("If you don't have a command line version of PHP installed on your server, you will not be able to run the background processing. See <a href='https://github.com/friendica/friendica/blob/master/doc/Install.md#set-up-the-worker'>'Setup the worker'</a>") . EOL;\r
118                         $help .= EOL . EOL;\r
119                         $tpl = get_markup_template('field_input.tpl');\r
120                         $help .= replace_macros($tpl, [\r
121                                 '$field' => ['phpath', L10n::t('PHP executable path'), $phpath, L10n::t('Enter full path to php executable. You can leave this blank to continue the installation.')],\r
122                         ]);\r
123                         $phpath = "";\r
124                 }\r
125 \r
126                 self::addCheck($checks, L10n::t('Command line PHP').($passed?" (<tt>$phpath</tt>)":""), $passed, false, $help);\r
127 \r
128                 if ($passed) {\r
129                         $cmd = "$phpath -v";\r
130                         $result = trim(shell_exec($cmd));\r
131                         $passed2 = (strpos($result, "(cli)") !== false);\r
132                         list($result) = explode("\n", $result);\r
133                         $help = "";\r
134                         if (!$passed2) {\r
135                                 $help .= L10n::t("PHP executable is not the php cli binary \x28could be cgi-fgci version\x29") . EOL;\r
136                                 $help .= L10n::t('Found PHP version: ') . "<tt>$result</tt>";\r
137                         }\r
138                         self::addCheck($checks, L10n::t('PHP cli binary'), $passed2, true, $help);\r
139                 }\r
140 \r
141 \r
142                 if ($passed2) {\r
143                         $str = autoname(8);\r
144                         $cmd = "$phpath testargs.php $str";\r
145                         $result = trim(shell_exec($cmd));\r
146                         $passed3 = $result == $str;\r
147                         $help = "";\r
148                         if (!$passed3) {\r
149                                 $help .= L10n::t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;\r
150                                 $help .= L10n::t('This is required for message delivery to work.');\r
151                         }\r
152                         self::addCheck($checks, L10n::t('PHP register_argc_argv'), $passed3, true, $help);\r
153                 }\r
154 \r
155 \r
156         }\r
157 \r
158         public static function checkKeys(&$checks)\r
159         {\r
160 \r
161                 $help = '';\r
162 \r
163                 $res = false;\r
164 \r
165                 if (function_exists('openssl_pkey_new')) {\r
166                         $res = openssl_pkey_new([\r
167                                 'digest_alg' => 'sha1',\r
168                                 'private_key_bits' => 4096,\r
169                                 'encrypt_key' => false\r
170                         ]);\r
171                 }\r
172 \r
173                 // Get private key\r
174 \r
175                 if (!$res) {\r
176                         $help .= L10n::t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;\r
177                         $help .= L10n::t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');\r
178                 }\r
179                 self::addCheck($checks, L10n::t('Generate encryption keys'), $res, true, $help);\r
180 \r
181         }\r
182 \r
183 \r
184         public static function checkFunctions(&$checks)\r
185         {\r
186                 $ck_funcs = [];\r
187                 self::addCheck($ck_funcs, L10n::t('libCurl PHP module'), true, true, "");\r
188                 self::addCheck($ck_funcs, L10n::t('GD graphics PHP module'), true, true, "");\r
189                 self::addCheck($ck_funcs, L10n::t('OpenSSL PHP module'), true, true, "");\r
190                 self::addCheck($ck_funcs, L10n::t('PDO or MySQLi PHP module'), true, true, "");\r
191                 self::addCheck($ck_funcs, L10n::t('mb_string PHP module'), true, true, "");\r
192                 self::addCheck($ck_funcs, L10n::t('XML PHP module'), true, true, "");\r
193                 self::addCheck($ck_funcs, L10n::t('iconv PHP module'), true, true, "");\r
194                 self::addCheck($ck_funcs, L10n::t('POSIX PHP module'), true, true, "");\r
195 \r
196                 if (function_exists('apache_get_modules')) {\r
197                         if (! in_array('mod_rewrite',apache_get_modules())) {\r
198                                 self::addCheck($ck_funcs, L10n::t('Apache mod_rewrite module'), false, true, L10n::t('Error: Apache webserver mod-rewrite module is required but not installed.'));\r
199                         } else {\r
200                                 self::addCheck($ck_funcs, L10n::t('Apache mod_rewrite module'), true, true, "");\r
201                         }\r
202                 }\r
203 \r
204                 if (!function_exists('curl_init')) {\r
205                         $ck_funcs[0]['status'] = false;\r
206                         $ck_funcs[0]['help'] = L10n::t('Error: libCURL PHP module required but not installed.');\r
207                 }\r
208                 if (!function_exists('imagecreatefromjpeg')) {\r
209                         $ck_funcs[1]['status'] = false;\r
210                         $ck_funcs[1]['help'] = L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.');\r
211                 }\r
212                 if (!function_exists('openssl_public_encrypt')) {\r
213                         $ck_funcs[2]['status'] = false;\r
214                         $ck_funcs[2]['help'] = L10n::t('Error: openssl PHP module required but not installed.');\r
215                 }\r
216                 if (!function_exists('mysqli_connect') && !class_exists('pdo')) {\r
217                         $ck_funcs[3]['status'] = false;\r
218                         $ck_funcs[3]['help'] = L10n::t('Error: PDO or MySQLi PHP module required but not installed.');\r
219                 }\r
220                 if (!function_exists('mysqli_connect') && class_exists('pdo') && !in_array('mysql', PDO::getAvailableDrivers())) {\r
221                         $ck_funcs[3]['status'] = false;\r
222                         $ck_funcs[3]['help'] = L10n::t('Error: The MySQL driver for PDO is not installed.');\r
223                 }\r
224                 if (!function_exists('mb_strlen')) {\r
225                         $ck_funcs[4]['status'] = false;\r
226                         $ck_funcs[4]['help'] = L10n::t('Error: mb_string PHP module required but not installed.');\r
227                 }\r
228                 if (!function_exists('iconv_strlen')) {\r
229                         $ck_funcs[6]['status'] = false;\r
230                         $ck_funcs[6]['help'] = L10n::t('Error: iconv PHP module required but not installed.');\r
231                 }\r
232                 if (!function_exists('posix_kill')) {\r
233                         $ck_funcs[7]['status'] = false;\r
234                         $ck_funcs[7]['help'] = L10n::t('Error: POSIX PHP module required but not installed.');\r
235                 }\r
236 \r
237                 $checks = array_merge($checks, $ck_funcs);\r
238 \r
239                 // check for XML DOM Documents being able to be generated\r
240                 try {\r
241                         $xml = new DOMDocument();\r
242                 } catch (Exception $e) {\r
243                         $ck_funcs[5]['status'] = false;\r
244                         $ck_funcs[5]['help'] = L10n::t('Error, XML PHP module required but not installed.');\r
245                 }\r
246         }\r
247 \r
248 \r
249         public static function checkHtConfig(&$checks)\r
250         {\r
251                 $status = true;\r
252                 $help = "";\r
253                 if ((file_exists('.htconfig.php') && !is_writable('.htconfig.php')) ||\r
254                         (!file_exists('.htconfig.php') && !is_writable('.'))) {\r
255 \r
256                         $status = false;\r
257                         $help = L10n::t('The web installer needs to be able to create a file called ".htconfig.php" in the top folder of your web server and it is unable to do so.') . EOL;\r
258                         $help .= L10n::t('This is most often a permission setting, as the web server may not be able to write files in your folder - even if you can.') . EOL;\r
259                         $help .= L10n::t('At the end of this procedure, we will give you a text to save in a file named .htconfig.php in your Friendica top folder.') . EOL;\r
260                         $help .= L10n::t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.') . EOL;\r
261                 }\r
262 \r
263                 self::addCheck($checks, L10n::t('.htconfig.php is writable'), $status, false, $help);\r
264 \r
265         }\r
266 \r
267         public static function checkSmarty3(&$checks)\r
268         {\r
269                 $status = true;\r
270                 $help = "";\r
271                 if (!is_writable('view/smarty3')) {\r
272 \r
273                         $status = false;\r
274                         $help = L10n::t('Friendica uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') . EOL;\r
275                         $help .= L10n::t('In order to store these compiled templates, the web server needs to have write access to the directory view/smarty3/ under the Friendica top level folder.') . EOL;\r
276                         $help .= L10n::t("Please ensure that the user that your web server runs as \x28e.g. www-data\x29 has write access to this folder.") . EOL;\r
277                         $help .= L10n::t("Note: as a security measure, you should give the web server write access to view/smarty3/ only--not the template files \x28.tpl\x29 that it contains.") . EOL;\r
278                 }\r
279 \r
280                 self::addCheck($checks, L10n::t('view/smarty3 is writable'), $status, true, $help);\r
281 \r
282         }\r
283 \r
284         public static function checkHtAccess(&$checks)\r
285         {\r
286                 $status = true;\r
287                 $help = "";\r
288                 $error_msg = "";\r
289                 if (function_exists('curl_init')) {\r
290                         $test = Network::fetchUrlFull(System::baseUrl() . "/install/testrewrite");\r
291 \r
292                         $url = normalise_link(System::baseUrl() . "/install/testrewrite");\r
293                         if ($test['body'] != "ok") {\r
294                                 $test = Network::fetchUrlFull($url);\r
295                         }\r
296 \r
297                         if ($test['body'] != "ok") {\r
298                                 $status = false;\r
299                                 $help = L10n::t('Url rewrite in .htaccess is not working. Check your server configuration.');\r
300                                 $error_msg = [];\r
301                                 $error_msg['head'] = L10n::t('Error message from Curl when fetching');\r
302                                 $error_msg['url'] = $test['redirect_url'];\r
303                                 $error_msg['msg'] = $test['error'];\r
304                         }\r
305                         self::addCheck($checks, L10n::t('Url rewrite is working'), $status, true, $help);\r
306                 } else {\r
307                         // cannot check modrewrite if libcurl is not installed\r
308                         /// @TODO Maybe issue warning here?\r
309                 }\r
310         }\r
311 \r
312         public static function checkImagik(&$checks)\r
313         {\r
314                 $imagick = false;\r
315                 $gif = false;\r
316 \r
317                 if (class_exists('Imagick')) {\r
318                         $imagick = true;\r
319                         $supported = Image::supportedTypes();\r
320                         if (array_key_exists('image/gif', $supported)) {\r
321                                 $gif = true;\r
322                         }\r
323                 }\r
324                 if ($imagick == false) {\r
325                         self::addCheck($checks, L10n::t('ImageMagick PHP extension is not installed'), $imagick, false, "");\r
326                 } else {\r
327                         self::addCheck($checks, L10n::t('ImageMagick PHP extension is installed'), $imagick, false, "");\r
328                         if ($imagick) {\r
329                                 self::addCheck($checks, L10n::t('ImageMagick supports GIF'), $gif, false, "");\r
330                         }\r
331                 }\r
332         }\r
333 \r
334         public static function loadDatabase()\r
335         {\r
336                 $errors = DBStructure::update(false, true, true);\r
337 \r
338                 return $errors;\r
339         }\r
340 }\r