Commit | Line | Data |
---|---|---|
158bd3ca TD |
1 | <?php |
2 | /** | |
3 | * This script tries to find the temp folder and unzip all setup files into. | |
e3369fd2 | 4 | * |
158bd3ca | 5 | * @author Marcel Werk |
7b7b9764 | 6 | * @copyright 2001-2019 WoltLab GmbH |
158bd3ca TD |
7 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
8 | */ | |
392cd6cb | 9 | // @codingStandardsIgnoreFile |
158bd3ca | 10 | // define constants |
728b9dd6 | 11 | define('INSTALL_SCRIPT', __FILE__); |
158bd3ca TD |
12 | define('INSTALL_SCRIPT_DIR', dirname(__FILE__).'/'); |
13 | define('SETUP_FILE', INSTALL_SCRIPT_DIR . 'WCFSetup.tar.gz'); | |
14 | define('NO_IMPORTS', 1); | |
15 | ||
16 | // set exception handler | |
17 | set_exception_handler('handleException'); | |
18 | // set php error handler | |
19 | set_error_handler('handleError', E_ALL); | |
20 | ||
21 | // define list of needed file | |
058cbd6a | 22 | $neededFilesPattern = [ |
158bd3ca | 23 | '!^setup/.*!', |
07937b16 | 24 | '!^install/files/acp/images/woltlabSuite.*!', |
e94c3830 | 25 | '!^install/files/acp/style/setup/.*!', |
158bd3ca | 26 | '!^install/files/lib/data/.*!', |
7da7f7cc | 27 | '!^install/files/icon/.*!', |
e2a34399 | 28 | '!^install/files/font/.*!', |
158bd3ca TD |
29 | '!^install/files/lib/system/.*!', |
30 | '!^install/files/lib/util/.*!', | |
158bd3ca | 31 | '!^install/lang/.*!', |
058cbd6a | 32 | '!^install/packages/.*!']; |
158bd3ca TD |
33 | |
34 | // define needed functions and classes | |
3e823cfa | 35 | /** @noinspection PhpMultipleClassesDeclarationsInOneFile */ |
158bd3ca TD |
36 | /** |
37 | * WCF::handleException() calls the show method on exceptions that implement this interface. | |
38 | * | |
f4f05aa5 | 39 | * @package com.woltlab.wcf |
158bd3ca TD |
40 | * @author Marcel Werk |
41 | */ | |
42 | interface IPrintableException { | |
43 | public function show(); | |
44 | } | |
45 | ||
46 | // define needed classes | |
47 | // needed are: | |
48 | // SystemException, PrintableException, BasicFileUtil, Tar, File, ZipFile | |
3e823cfa | 49 | /** @noinspection PhpMultipleClassesDeclarationsInOneFile */ |
158bd3ca TD |
50 | /** |
51 | * A SystemException is thrown when an unexpected error occurs. | |
52 | * | |
f4f05aa5 | 53 | * @package com.woltlab.wcf |
158bd3ca TD |
54 | * @author Marcel Werk |
55 | */ | |
56 | class SystemException extends \Exception implements IPrintableException { | |
57 | protected $description; | |
58 | protected $information = ''; | |
59 | protected $functions = ''; | |
60 | ||
61 | /** | |
62 | * Creates a new SystemException. | |
63 | * | |
ac52543a MS |
64 | * @param string $message error message |
65 | * @param integer $code error code | |
66 | * @param string $description description of the error | |
3353ca3e | 67 | * @param \Exception $previous repacked Exception |
158bd3ca | 68 | */ |
3353ca3e AE |
69 | public function __construct($message = '', $code = 0, $description = '', \Exception $previous = null) { |
70 | parent::__construct((string) $message, (int) $code, $previous); | |
158bd3ca TD |
71 | $this->description = $description; |
72 | } | |
73 | ||
74 | /** | |
75 | * Returns the description of this exception. | |
76 | * | |
39bea7dd | 77 | * @return string |
158bd3ca TD |
78 | */ |
79 | public function getDescription() { | |
80 | return $this->description; | |
81 | } | |
82 | ||
83 | /** | |
84 | * Prints this exception. | |
85 | * This method is called by WCF::handleException(). | |
86 | */ | |
87 | public function show() { | |
314f7e0e MW |
88 | /* |
89 | * A notice on the HTML used below: | |
90 | * | |
91 | * It might appear a bit weird to use <p> all over the place where semantically | |
92 | * other elements would fit in way better. The reason behind this is that we avoid | |
93 | * inheriting unwanted styles (e.g. exception displayed in an overlay) and that | |
94 | * the output needs to be properly readable when copied & pasted somewhere. | |
95 | * | |
96 | * Besides the visual appearance, the output was built to provide a maximum of | |
97 | * compatibility and readability when pasted somewhere else, e.g. a WYSIWYG editor | |
98 | * without the potential of messing up the formatting and thus harming the readability. | |
99 | */ | |
100 | ?><!DOCTYPE html> | |
158bd3ca TD |
101 | <html> |
102 | <head> | |
314f7e0e MW |
103 | <title>Fatal Error: <?php echo htmlentities($this->getMessage()); ?></title> |
104 | <meta charset="utf-8"> | |
105 | <meta name="viewport" content="width=device-width, initial-scale=1"> | |
106 | <style> | |
107 | .exceptionBody { | |
108 | margin: 0; | |
109 | padding: 0; | |
110 | } | |
111 | ||
112 | .exceptionContainer { | |
113 | box-sizing: border-box; | |
114 | font-family: 'Segoe UI', 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
115 | font-size: 14px; | |
116 | padding-bottom: 20px; | |
117 | } | |
118 | ||
119 | .exceptionContainer * { | |
120 | box-sizing: inherit; | |
121 | color: #000; | |
122 | line-height: 1.5em; | |
123 | margin: 0; | |
124 | padding: 0; | |
125 | } | |
126 | ||
127 | .exceptionHeader { | |
3bfbe63f | 128 | background-color: rgb(58, 109, 156); |
314f7e0e MW |
129 | padding: 30px 0; |
130 | } | |
131 | ||
132 | .exceptionTitle { | |
133 | color: #fff; | |
134 | font-size: 28px; | |
135 | font-weight: 300; | |
136 | } | |
137 | ||
138 | .exceptionErrorCode { | |
139 | color: #fff; | |
140 | margin-top: .5em; | |
141 | } | |
142 | ||
143 | .exceptionErrorCode .exceptionInlineCode { | |
3bfbe63f | 144 | background-color: rgb(43, 79, 113); |
314f7e0e MW |
145 | border-radius: 3px; |
146 | color: #fff; | |
147 | font-family: monospace; | |
148 | padding: 3px 10px; | |
149 | white-space: nowrap; | |
150 | } | |
151 | ||
152 | .exceptionSubtitle { | |
153 | border-bottom: 1px solid rgb(238, 238, 238); | |
154 | color: rgb(44, 62, 80); | |
155 | font-size: 24px; | |
156 | font-weight: 300; | |
157 | margin-bottom: 15px; | |
158 | padding-bottom: 10px; | |
159 | } | |
160 | ||
161 | .exceptionContainer > .exceptionBoundary { | |
162 | margin-top: 30px; | |
163 | } | |
164 | ||
165 | .exceptionText .exceptionInlineCodeWrapper { | |
166 | border: 1px solid rgb(169, 169, 169); | |
167 | border-radius: 3px; | |
168 | padding: 2px 5px; | |
169 | } | |
2d63c13c | 170 | |
314f7e0e MW |
171 | .exceptionText .exceptionInlineCode { |
172 | font-family: monospace; | |
173 | white-space: nowrap; | |
174 | } | |
175 | ||
176 | .exceptionFieldTitle { | |
177 | color: rgb(59, 109, 169); | |
178 | } | |
179 | ||
180 | .exceptionFieldTitle .exceptionColon { | |
181 | /* hide colon in browser, but will be visible after copy & paste */ | |
182 | opacity: 0; | |
183 | } | |
184 | ||
185 | .exceptionFieldValue { | |
186 | font-size: 18px; | |
187 | min-height: 1.5em; | |
188 | } | |
189 | ||
190 | .exceptionSystemInformation, | |
191 | .exceptionErrorDetails, | |
192 | .exceptionStacktrace { | |
193 | list-style-type: none; | |
194 | } | |
195 | ||
196 | .exceptionSystemInformation > li:not(:first-child), | |
197 | .exceptionErrorDetails > li:not(:first-child) { | |
198 | margin-top: 10px; | |
199 | } | |
200 | ||
201 | .exceptionStacktrace { | |
202 | display: block; | |
203 | margin-top: 5px; | |
204 | overflow: auto; | |
205 | padding-bottom: 20px; | |
206 | } | |
207 | ||
208 | .exceptionStacktraceFile, | |
209 | .exceptionStacktraceFile span, | |
210 | .exceptionStacktraceCall, | |
211 | .exceptionStacktraceCall span { | |
212 | font-family: monospace !important; | |
213 | white-space: nowrap !important; | |
214 | } | |
215 | ||
216 | .exceptionStacktraceCall + .exceptionStacktraceFile { | |
217 | margin-top: 5px; | |
218 | } | |
219 | ||
220 | .exceptionStacktraceCall { | |
221 | padding-left: 40px; | |
222 | } | |
223 | ||
224 | .exceptionStacktraceCall, | |
225 | .exceptionStacktraceCall span { | |
226 | color: rgb(102, 102, 102) !important; | |
227 | font-size: 13px !important; | |
228 | } | |
229 | ||
230 | /* mobile */ | |
231 | @media (max-width: 767px) { | |
232 | .exceptionBoundary { | |
233 | min-width: 320px; | |
234 | padding: 0 10px; | |
235 | } | |
158bd3ca | 236 | |
314f7e0e MW |
237 | .exceptionText .exceptionInlineCodeWrapper { |
238 | display: inline-block; | |
239 | overflow: auto; | |
240 | } | |
158bd3ca | 241 | |
314f7e0e MW |
242 | .exceptionErrorCode .exceptionInlineCode { |
243 | font-size: 13px; | |
244 | padding: 2px 5px; | |
245 | } | |
246 | } | |
247 | ||
248 | /* desktop */ | |
249 | @media (min-width: 768px) { | |
250 | .exceptionBoundary { | |
251 | margin: 0 auto; | |
252 | max-width: 1400px; | |
253 | min-width: 1200px; | |
254 | padding: 0 10px; | |
255 | } | |
256 | ||
257 | .exceptionSystemInformation { | |
258 | display: flex; | |
259 | flex-wrap: wrap; | |
260 | } | |
261 | ||
262 | .exceptionSystemInformation1, | |
263 | .exceptionSystemInformation3, | |
264 | .exceptionSystemInformation5 { | |
265 | flex: 0 0 200px; | |
266 | margin: 0 0 10px 0 !important; | |
267 | } | |
268 | ||
269 | .exceptionSystemInformation2, | |
270 | .exceptionSystemInformation4, | |
271 | .exceptionSystemInformation6 { | |
272 | flex: 0 0 calc(100% - 210px); | |
273 | margin: 0 0 10px 10px !important; | |
274 | max-width: calc(100% - 210px); | |
275 | } | |
276 | ||
277 | .exceptionSystemInformation1 { order: 1; } | |
278 | .exceptionSystemInformation2 { order: 2; } | |
279 | .exceptionSystemInformation3 { order: 3; } | |
280 | .exceptionSystemInformation4 { order: 4; } | |
281 | .exceptionSystemInformation5 { order: 5; } | |
282 | .exceptionSystemInformation6 { order: 6; } | |
283 | ||
284 | .exceptionSystemInformation .exceptionFieldValue { | |
285 | overflow: hidden; | |
286 | text-overflow: ellipsis; | |
287 | white-space: nowrap; | |
288 | } | |
289 | } | |
290 | </style> | |
291 | </head> | |
292 | <body class="exceptionBody"> | |
293 | <div class="exceptionContainer"> | |
294 | <div class="exceptionHeader"> | |
295 | <div class="exceptionBoundary"> | |
e8d7ee4d | 296 | <p class="exceptionTitle">An error has occurred</p> |
314f7e0e | 297 | </div> |
158bd3ca TD |
298 | </div> |
299 | ||
314f7e0e MW |
300 | <div class="exceptionBoundary"> |
301 | <p class="exceptionSubtitle">System Information</p> | |
302 | <ul class="exceptionSystemInformation"> | |
303 | <li class="exceptionSystemInformation1"> | |
304 | <p class="exceptionFieldTitle">PHP Version<span class="exceptionColon">:</span></p> | |
305 | <p class="exceptionFieldValue"><?php echo htmlentities(phpversion()); ?></p> | |
306 | </li> | |
307 | <li class="exceptionSystemInformation3"> | |
308 | <p class="exceptionFieldTitle">WoltLab Suite Core<span class="exceptionColon">:</span></p> | |
dd2d8c0c | 309 | <p class="exceptionFieldValue">5.2</p> |
314f7e0e MW |
310 | </li> |
311 | <li class="exceptionSystemInformation5"> | |
312 | <p class="exceptionFieldTitle">Peak Memory Usage<span class="exceptionColon">:</span></p> | |
313 | <p class="exceptionFieldValue"><?php echo round(memory_get_peak_usage() / 1024 / 1024, 3); ?>/<?php echo ini_get('memory_limit'); ?></p> | |
314 | </li> | |
315 | <li class="exceptionSystemInformation2"> | |
316 | <p class="exceptionFieldTitle">Request URI<span class="exceptionColon">:</span></p> | |
317 | <p class="exceptionFieldValue"><?php if (isset($_SERVER['REQUEST_URI'])) echo htmlentities($_SERVER['REQUEST_URI']); ?></p> | |
318 | </li> | |
319 | <li class="exceptionSystemInformation4"> | |
320 | <p class="exceptionFieldTitle">Referrer<span class="exceptionColon">:</span></p> | |
321 | <p class="exceptionFieldValue"><?php if (isset($_SERVER['HTTP_REFERER'])) echo htmlentities($_SERVER['HTTP_REFERER']); ?></p> | |
322 | </li> | |
323 | <li class="exceptionSystemInformation6"> | |
324 | <p class="exceptionFieldTitle">User Agent<span class="exceptionColon">:</span></p> | |
325 | <p class="exceptionFieldValue"><?php if (isset($_SERVER['HTTP_USER_AGENT'])) echo htmlentities($_SERVER['HTTP_USER_AGENT']); ?></p> | |
326 | </li> | |
327 | </ul> | |
328 | </div> | |
329 | ||
330 | <?php | |
3353ca3e | 331 | $e = $this; |
314f7e0e MW |
332 | $first = true; |
333 | do { | |
3353ca3e AE |
334 | $trace = $e->getTrace(); |
335 | if (isset($trace[0]['function']) && $trace[0]['function'] === 'handleException') { | |
336 | // ignore repacked exception | |
337 | continue; | |
338 | } | |
339 | ||
314f7e0e MW |
340 | ?> |
341 | <div class="exceptionBoundary"> | |
3353ca3e | 342 | <p class="exceptionSubtitle"><?php if (!$e->getPrevious() && !$first) { echo "Original "; } else if ($e->getPrevious() && $first) { echo "Final "; } ?>Error</p> |
2c4b885b | 343 | <?php if (($e instanceof SystemException || $e instanceof \wcf\system\exception\SystemException) && $e->getDescription()) { ?> |
3353ca3e | 344 | <p class="exceptionText"><?php echo $e->getDescription(); ?></p> |
314f7e0e MW |
345 | <?php } ?> |
346 | <ul class="exceptionErrorDetails"> | |
347 | <li> | |
348 | <p class="exceptionFieldTitle">Error Type<span class="exceptionColon">:</span></p> | |
3353ca3e | 349 | <p class="exceptionFieldValue"><?php echo htmlentities(get_class($e)); ?></p> |
314f7e0e MW |
350 | </li> |
351 | <li> | |
352 | <p class="exceptionFieldTitle">Error Message<span class="exceptionColon">:</span></p> | |
3353ca3e | 353 | <p class="exceptionFieldValue"><?php echo htmlentities($e->getMessage()); ?></p> |
314f7e0e | 354 | </li> |
3353ca3e | 355 | <?php if ($e->getCode()) { ?> |
314f7e0e MW |
356 | <li> |
357 | <p class="exceptionFieldTitle">Error Code<span class="exceptionColon">:</span></p> | |
3353ca3e | 358 | <p class="exceptionFieldValue"><?php echo intval($e->getCode()); ?></p> |
314f7e0e MW |
359 | </li> |
360 | <?php } ?> | |
361 | <li> | |
362 | <p class="exceptionFieldTitle">File<span class="exceptionColon">:</span></p> | |
3353ca3e | 363 | <p class="exceptionFieldValue" style="word-break: break-all"><?php echo htmlentities($e->getFile()); ?> (<?php echo $e->getLine(); ?>)</p> |
314f7e0e MW |
364 | </li> |
365 | ||
366 | <li> | |
367 | <p class="exceptionFieldTitle">Stack Trace<span class="exceptionColon">:</span></p> | |
368 | <ul class="exceptionStacktrace"> | |
369 | <?php | |
3353ca3e | 370 | $trace = $e->getTrace(); |
314f7e0e MW |
371 | for ($i = 0, $max = count($trace); $i < $max; $i++) { |
372 | ?> | |
373 | <li class="exceptionStacktraceFile"><?php echo '#'.$i.' '.htmlentities($trace[$i]['file']).' ('.$trace[$i]['line'].')'.':'; ?></li> | |
374 | <li class="exceptionStacktraceCall"> | |
375 | <?php | |
376 | echo $trace[$i]['class'].$trace[$i]['type'].$trace[$i]['function'].'('; | |
377 | echo implode(', ', array_map(function ($item) { | |
378 | switch (gettype($item)) { | |
379 | case 'integer': | |
380 | case 'double': | |
381 | return $item; | |
382 | case 'NULL': | |
383 | return 'null'; | |
384 | case 'string': | |
385 | return "'".addcslashes(htmlentities($item), "\\'")."'"; | |
386 | case 'boolean': | |
387 | return $item ? 'true' : 'false'; | |
388 | case 'array': | |
389 | $keys = array_keys($item); | |
390 | if (count($keys) > 5) return "[ ".count($keys)." items ]"; | |
391 | return '[ '.implode(', ', array_map(function ($item) { | |
392 | return $item.' => '; | |
393 | }, $keys)).']'; | |
394 | case 'object': | |
395 | return get_class($item); | |
396 | } | |
397 | ||
398 | throw new \LogicException('Unreachable'); | |
399 | }, $trace[$i]['args'])); | |
400 | echo ')</li>'; | |
401 | } | |
402 | ?> | |
403 | </ul> | |
404 | </li> | |
405 | </ul> | |
406 | </div> | |
407 | <?php | |
408 | $first = false; | |
3353ca3e AE |
409 | } while ($e = $e->getPrevious()); |
410 | ?> | |
158bd3ca TD |
411 | </div> |
412 | </body> | |
413 | </html> | |
414 | ||
415 | <?php | |
416 | } | |
417 | } | |
418 | ||
158bd3ca TD |
419 | /** |
420 | * Loads the required classes automatically. | |
421 | */ | |
db8aa273 | 422 | spl_autoload_register(function($className) { |
158bd3ca TD |
423 | $namespaces = explode('\\', $className); |
424 | if (count($namespaces) > 1) { | |
425 | // remove 'wcf' component | |
426 | array_shift($namespaces); | |
427 | ||
428 | $className = implode('/', $namespaces); | |
01bd2eff | 429 | $classPath = TMP_DIR . 'install/files/lib/' . $className . '.class.php'; |
158bd3ca TD |
430 | if (file_exists($classPath)) { |
431 | require_once($classPath); | |
432 | } | |
433 | } | |
db8aa273 | 434 | }); |
158bd3ca TD |
435 | |
436 | /** | |
437 | * Escapes strings for execution in sql queries. | |
ac52543a MS |
438 | * |
439 | * @param string $string | |
440 | * @return string | |
158bd3ca TD |
441 | */ |
442 | function escapeString($string) { | |
443 | return \wcf\system\WCF::getDB()->escapeString($string); | |
444 | } | |
445 | ||
1a2817c4 AE |
446 | /** |
447 | * Helper method to output debug data for all passed variables, | |
448 | * uses `print_r()` for arrays and objects, `var_dump()` otherwise. | |
449 | */ | |
450 | function wcfDebug() { | |
451 | echo "<pre>"; | |
452 | ||
453 | $args = func_get_args(); | |
454 | $length = count($args); | |
455 | if ($length === 0) { | |
456 | echo "ERROR: No arguments provided.<hr>"; | |
457 | } | |
458 | else { | |
459 | for ($i = 0; $i < $length; $i++) { | |
460 | $arg = $args[$i]; | |
461 | ||
462 | echo "<h2>Argument {$i} (" . gettype($arg) . ")</h2>"; | |
463 | ||
464 | if (is_array($arg) || is_object($arg)) { | |
465 | print_r($arg); | |
466 | } | |
467 | else { | |
468 | var_dump($arg); | |
469 | } | |
470 | ||
471 | echo "<hr>"; | |
472 | } | |
473 | } | |
474 | ||
475 | $backtrace = debug_backtrace(); | |
476 | ||
477 | // output call location to help finding these debug outputs again | |
478 | echo "wcfDebug() called in {$backtrace[0]['file']} on line {$backtrace[0]['line']}"; | |
479 | ||
480 | echo "</pre>"; | |
481 | ||
482 | exit; | |
483 | } | |
484 | ||
158bd3ca TD |
485 | /** |
486 | * Calls the show method on the given exception. | |
487 | * | |
9a132951 | 488 | * @param mixed $e |
158bd3ca | 489 | */ |
9a132951 M |
490 | function handleException($e) { |
491 | try { | |
492 | if (!($e instanceof \Exception)) throw $e; | |
493 | ||
494 | if ($e instanceof IPrintableException || $e instanceof \wcf\system\exception\IPrintableException) { | |
495 | $e->show(); | |
496 | exit; | |
497 | } | |
3353ca3e AE |
498 | |
499 | // repacking | |
500 | (new SystemException($e->getMessage(), $e->getCode(), '', $e))->show(); | |
501 | exit; | |
9a132951 M |
502 | } |
503 | catch (\Throwable $exception) { | |
504 | die("<pre>WCF::handleException() Unhandled exception: ".$exception->getMessage()."\n\n".$exception->getTraceAsString()); | |
505 | } | |
158bd3ca TD |
506 | } |
507 | ||
508 | /** | |
509 | * Catches php errors and throws instead a system exception. | |
510 | * | |
511 | * @param integer $errorNo | |
512 | * @param string $message | |
513 | * @param string $filename | |
514 | * @param integer $lineNo | |
2b770bdd | 515 | * @throws SystemException |
158bd3ca TD |
516 | */ |
517 | function handleError($errorNo, $message, $filename, $lineNo) { | |
518 | if (error_reporting() != 0) { | |
519 | $type = 'error'; | |
520 | switch ($errorNo) { | |
521 | case 2: $type = 'warning'; | |
522 | break; | |
523 | case 8: $type = 'notice'; | |
524 | break; | |
525 | } | |
526 | ||
527 | throw new SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0); | |
528 | } | |
529 | } | |
530 | ||
3e823cfa | 531 | /** @noinspection PhpMultipleClassesDeclarationsInOneFile */ |
158bd3ca TD |
532 | /** |
533 | * BasicFileUtil contains file-related functions. | |
534 | * | |
f4f05aa5 | 535 | * @package com.woltlab.wcf |
158bd3ca TD |
536 | * @author Marcel Werk |
537 | */ | |
538 | class BasicFileUtil { | |
d8fa09e0 AE |
539 | /** |
540 | * chmod mode | |
541 | * @var integer | |
542 | */ | |
543 | protected static $mode = null; | |
544 | ||
158bd3ca TD |
545 | /** |
546 | * Tries to find the temp folder. | |
547 | * | |
548 | * @return string | |
2b770bdd | 549 | * @throws SystemException |
158bd3ca TD |
550 | */ |
551 | public static function getTempFolder() { | |
158bd3ca TD |
552 | // use tmp folder in document root by default |
553 | if (!empty($_SERVER['DOCUMENT_ROOT'])) { | |
069cd37e MW |
554 | if (strpos($_SERVER['DOCUMENT_ROOT'], 'strato') !== false) { |
555 | // strato bugfix | |
556 | // create tmp folder in document root automatically | |
557 | if (!@file_exists($_SERVER['DOCUMENT_ROOT'].'/tmp')) { | |
558 | @mkdir($_SERVER['DOCUMENT_ROOT'].'/tmp/', 0777); | |
559 | try { | |
560 | self::makeWritable($_SERVER['DOCUMENT_ROOT'].'/tmp/'); | |
561 | } | |
562 | catch (SystemException $e) {} | |
563 | } | |
158bd3ca | 564 | } |
069cd37e MW |
565 | if (@file_exists($_SERVER['DOCUMENT_ROOT'].'/tmp') && @is_writable($_SERVER['DOCUMENT_ROOT'].'/tmp')) { |
566 | return $_SERVER['DOCUMENT_ROOT'].'/tmp/'; | |
158bd3ca TD |
567 | } |
568 | } | |
e3369fd2 | 569 | |
069cd37e MW |
570 | if (isset($_ENV['TMP']) && @is_writable($_ENV['TMP'])) { |
571 | return $_ENV['TMP'] . '/'; | |
158bd3ca | 572 | } |
069cd37e MW |
573 | if (isset($_ENV['TEMP']) && @is_writable($_ENV['TEMP'])) { |
574 | return $_ENV['TEMP'] . '/'; | |
575 | } | |
576 | if (isset($_ENV['TMPDIR']) && @is_writable($_ENV['TMPDIR'])) { | |
577 | return $_ENV['TMPDIR'] . '/'; | |
578 | } | |
e3369fd2 | 579 | |
069cd37e MW |
580 | if (($path = ini_get('upload_tmp_dir')) && @is_writable($path)) { |
581 | return $path . '/'; | |
582 | } | |
583 | if (@file_exists('/tmp/') && @is_writable('/tmp/')) { | |
584 | return '/tmp/'; | |
158bd3ca | 585 | } |
069cd37e MW |
586 | if (function_exists('session_save_path') && ($path = session_save_path()) && @is_writable($path)) { |
587 | return $path . '/'; | |
588 | } | |
2d63c13c | 589 | |
5805a819 | 590 | $path = INSTALL_SCRIPT_DIR.'tmp/'; |
069cd37e MW |
591 | if (@file_exists($path) && @is_writable($path)) { |
592 | return $path; | |
593 | } | |
594 | else { | |
595 | throw new SystemException('There is no access to the system temporary folder due to an unknown reason and no user specific temporary folder exists in '.INSTALL_SCRIPT_DIR.'! This is a misconfiguration of your webserver software! Please create a folder called '.$path.' using your favorite ftp program, make it writable and then retry this installation.'); | |
596 | } | |
597 | } | |
598 | ||
599 | /** | |
600 | * Returns the temp folder for the installation. | |
601 | * | |
602 | * @return string | |
603 | */ | |
604 | public static function getInstallTempFolder() { | |
605 | $dir = self::getTempFolder() . TMP_FILE_PREFIX . '/'; | |
606 | @mkdir($dir); | |
607 | self::makeWritable($dir); | |
158bd3ca | 608 | |
158bd3ca TD |
609 | return $dir; |
610 | } | |
1232bce2 AE |
611 | |
612 | /** | |
613 | * Tries to make a file or directory writable. It starts of with the least | |
d8fa09e0 | 614 | * permissions and goes up until 0666 for files and 0777 for directories. |
1232bce2 AE |
615 | * |
616 | * @param string $filename | |
2b770bdd | 617 | * @throws SystemException |
1232bce2 AE |
618 | */ |
619 | public static function makeWritable($filename) { | |
043b049d | 620 | if (!file_exists($filename)) { |
1232bce2 | 621 | return; |
158bd3ca TD |
622 | } |
623 | ||
d8fa09e0 AE |
624 | // determine mode |
625 | if (self::$mode === null) { | |
626 | // do not use PHP_OS here, as this represents the system it was built on != running on | |
0436b618 AE |
627 | // php_uname() is forbidden on some strange hosts; PHP_EOL is reliable |
628 | if (PHP_EOL == "\r\n") { | |
629 | // Windows | |
d8fa09e0 AE |
630 | self::$mode = 0777; |
631 | } | |
632 | else { | |
0436b618 | 633 | // anything but Windows |
adbd8054 AE |
634 | clearstatcache(); |
635 | ||
d8fa09e0 AE |
636 | self::$mode = 0666; |
637 | ||
0c1810be AE |
638 | $tmpFilename = '__permissions_'.sha1(time()).'.txt'; |
639 | @touch($tmpFilename); | |
d8fa09e0 AE |
640 | |
641 | // create a new file and check the file owner, if it is the same | |
642 | // as this file (uploaded through FTP), we can safely grant write | |
643 | // permissions exclusively to the owner rather than everyone | |
0c1810be | 644 | if (file_exists($tmpFilename)) { |
d8fa09e0 | 645 | $scriptOwner = fileowner(__FILE__); |
0c1810be | 646 | $fileOwner = fileowner($tmpFilename); |
d8fa09e0 AE |
647 | |
648 | if ($scriptOwner === $fileOwner) { | |
649 | self::$mode = 0644; | |
650 | } | |
651 | ||
0c1810be | 652 | @unlink($tmpFilename); |
d8fa09e0 AE |
653 | } |
654 | } | |
655 | } | |
158bd3ca | 656 | |
1232bce2 | 657 | if (is_dir($filename)) { |
d8fa09e0 | 658 | if (self::$mode == 0644) { |
7fe5312d | 659 | @chmod($filename, 0755); |
1232bce2 | 660 | } |
d8fa09e0 | 661 | else { |
7fe5312d | 662 | @chmod($filename, 0777); |
1232bce2 AE |
663 | } |
664 | } | |
d8fa09e0 | 665 | else { |
7fe5312d | 666 | @chmod($filename, self::$mode); |
d8fa09e0 AE |
667 | } |
668 | ||
669 | if (!is_writable($filename)) { | |
670 | throw new SystemException("Unable to make '".$filename."' writable. This is a misconfiguration of your server, please contact your system administrator or hosting provider."); | |
671 | } | |
158bd3ca | 672 | } |
e5521c73 TD |
673 | |
674 | /** | |
675 | * Removes a leading slash from the given path. | |
676 | * | |
677 | * @param string $path | |
678 | * @return string | |
679 | */ | |
680 | public static function removeLeadingSlash($path) { | |
681 | return ltrim($path, '/'); | |
682 | } | |
683 | ||
684 | /** | |
685 | * Removes a trailing slash from the given path. | |
686 | * | |
687 | * @param string $path | |
688 | * @return string | |
689 | */ | |
690 | public static function removeTrailingSlash($path) { | |
691 | return rtrim($path, '/'); | |
692 | } | |
693 | ||
694 | /** | |
695 | * Adds a trailing slash to the given path. | |
696 | * | |
697 | * @param string $path | |
698 | * @return string | |
699 | */ | |
700 | public static function addTrailingSlash($path) { | |
701 | return rtrim($path, '/').'/'; | |
702 | } | |
703 | ||
704 | /** | |
705 | * Adds a leading slash to the given path. | |
706 | * | |
707 | * @param string $path | |
708 | * @return string | |
709 | */ | |
710 | public static function addLeadingSlash($path) { | |
711 | return '/'.ltrim($path, '/'); | |
712 | } | |
713 | ||
714 | /** | |
715 | * Creates a path on the local filesystem and returns true on success. | |
716 | * Parent directories do not need to exists as they will be created if | |
717 | * necessary. | |
718 | * | |
719 | * @param string $path | |
720 | * @return boolean | |
721 | */ | |
722 | public static function makePath($path) { | |
723 | // directory already exists, abort | |
724 | if (file_exists($path)) { | |
725 | return false; | |
726 | } | |
727 | ||
728 | // check if parent directory exists | |
729 | $parent = dirname($path); | |
730 | if ($parent != $path) { | |
731 | // parent directory does not exist either | |
732 | // we have to create the parent directory first | |
733 | $parent = self::addTrailingSlash($parent); | |
734 | if (!@file_exists($parent)) { | |
735 | // could not create parent directory either => abort | |
736 | if (!self::makePath($parent)) { | |
737 | return false; | |
738 | } | |
739 | } | |
740 | ||
741 | // well, the parent directory exists or has been created | |
742 | // lets create this path | |
743 | if (!@mkdir($path)) { | |
744 | return false; | |
745 | } | |
746 | ||
747 | self::makeWritable($path); | |
748 | ||
749 | return true; | |
750 | } | |
751 | ||
752 | return false; | |
753 | } | |
158bd3ca TD |
754 | } |
755 | ||
3e823cfa | 756 | /** @noinspection PhpMultipleClassesDeclarationsInOneFile */ |
158bd3ca TD |
757 | /** |
758 | * Opens tar or tar.gz archives. | |
759 | * | |
760 | * Usage: | |
761 | * ------ | |
762 | * $tar = new Tar('archive.tar'); | |
763 | * $contentList = $tar->getContentList(); | |
764 | * foreach ($contentList as $key => $val) { | |
765 | * $tar->extract($key, DESTINATION); | |
766 | * } | |
767 | */ | |
768 | class Tar { | |
e5521c73 TD |
769 | /** |
770 | * name of the archive | |
771 | * @var string | |
772 | */ | |
158bd3ca | 773 | protected $archiveName = ''; |
e5521c73 TD |
774 | |
775 | /** | |
776 | * content of the tar file | |
777 | * @var array | |
778 | */ | |
058cbd6a | 779 | protected $contentList = []; |
e5521c73 TD |
780 | |
781 | /** | |
782 | * indicates if tar file is opened | |
783 | * @var boolean | |
784 | */ | |
158bd3ca | 785 | protected $opened = false; |
e5521c73 TD |
786 | |
787 | /** | |
788 | * indicates if file content has been read | |
789 | * @var boolean | |
790 | */ | |
158bd3ca | 791 | protected $read = false; |
e5521c73 TD |
792 | |
793 | /** | |
794 | * file object | |
795 | * @var File | |
796 | */ | |
158bd3ca | 797 | protected $file = null; |
e5521c73 TD |
798 | |
799 | /** | |
800 | * indicates if the tar file is (g)zipped | |
801 | * @var boolean | |
802 | */ | |
158bd3ca | 803 | protected $isZipped = false; |
e5521c73 TD |
804 | |
805 | /** | |
806 | * file access mode | |
807 | * @var string | |
808 | */ | |
158bd3ca TD |
809 | protected $mode = 'rb'; |
810 | ||
e5521c73 TD |
811 | /** |
812 | * chunk size for extracting | |
813 | * @var integer | |
814 | */ | |
815 | const CHUNK_SIZE = 8192; | |
816 | ||
158bd3ca TD |
817 | /** |
818 | * Creates a new Tar object. | |
819 | * archiveName must be tarball or gzipped tarball | |
e5521c73 | 820 | * |
39bea7dd | 821 | * @param string $archiveName |
2b770bdd | 822 | * @throws SystemException |
158bd3ca TD |
823 | */ |
824 | public function __construct($archiveName) { | |
158bd3ca | 825 | if (!is_file($archiveName)) { |
4fe0b42b | 826 | throw new SystemException("unable to find tar archive '".$archiveName."'"); |
158bd3ca TD |
827 | } |
828 | ||
829 | $this->archiveName = $archiveName; | |
830 | $this->open(); | |
831 | $this->readContent(); | |
832 | } | |
833 | ||
834 | /** | |
835 | * Destructor of this class, closes tar archive. | |
836 | */ | |
837 | public function __destruct() { | |
838 | $this->close(); | |
839 | } | |
840 | ||
841 | /** | |
842 | * Opens the tar archive and stores filehandle. | |
843 | */ | |
844 | public function open() { | |
845 | if (!$this->opened) { | |
e5521c73 | 846 | if ($this->isZipped) $this->file = new GZipFile($this->archiveName, $this->mode); |
158bd3ca TD |
847 | else { |
848 | // test compression | |
849 | $this->file = new File($this->archiveName, $this->mode); | |
850 | if ($this->file->read(2) == "\37\213") { | |
851 | $this->file->close(); | |
852 | $this->isZipped = true; | |
e5521c73 | 853 | $this->file = new GZipFile($this->archiveName, $this->mode); |
158bd3ca TD |
854 | } |
855 | else { | |
856 | $this->file->seek(0); | |
857 | } | |
858 | } | |
859 | $this->opened = true; | |
860 | } | |
861 | } | |
e5521c73 | 862 | |
158bd3ca TD |
863 | /** |
864 | * Closes the opened file. | |
865 | */ | |
866 | public function close() { | |
867 | if ($this->opened) { | |
868 | $this->file->close(); | |
869 | $this->opened = false; | |
870 | } | |
871 | } | |
872 | ||
873 | /** | |
e5521c73 | 874 | * @inheritDoc |
158bd3ca TD |
875 | */ |
876 | public function getContentList() { | |
877 | if (!$this->read) { | |
878 | $this->open(); | |
879 | $this->readContent(); | |
880 | } | |
881 | return $this->contentList; | |
882 | } | |
883 | ||
884 | /** | |
e5521c73 | 885 | * @inheritDoc |
158bd3ca TD |
886 | */ |
887 | public function getFileInfo($fileIndex) { | |
888 | if (!is_int($fileIndex)) { | |
889 | $fileIndex = $this->getIndexByFilename($fileIndex); | |
890 | } | |
891 | ||
892 | if (!isset($this->contentList[$fileIndex])) { | |
6286572b | 893 | throw new SystemException("Tar: could find file '".$fileIndex."' in archive"); |
158bd3ca TD |
894 | } |
895 | return $this->contentList[$fileIndex]; | |
896 | } | |
897 | ||
898 | /** | |
e5521c73 | 899 | * @inheritDoc |
158bd3ca TD |
900 | */ |
901 | public function getIndexByFilename($filename) { | |
902 | foreach ($this->contentList as $index => $file) { | |
903 | if ($file['filename'] == $filename) { | |
904 | return $index; | |
905 | } | |
906 | } | |
907 | return false; | |
908 | } | |
909 | ||
910 | /** | |
e5521c73 | 911 | * @inheritDoc |
158bd3ca TD |
912 | */ |
913 | public function extractToString($index) { | |
914 | if (!$this->read) { | |
915 | $this->open(); | |
916 | $this->readContent(); | |
917 | } | |
918 | $header = $this->getFileInfo($index); | |
919 | ||
920 | // can not extract a folder | |
921 | if ($header['type'] != 'file') { | |
922 | return false; | |
923 | } | |
924 | ||
925 | // seek to offset | |
926 | $this->file->seek($header['offset']); | |
927 | ||
928 | // read data | |
929 | $content = ''; | |
930 | $n = floor($header['size'] / 512); | |
e5521c73 | 931 | for ($i = 0; $i < $n; $i++) { |
158bd3ca TD |
932 | $content .= $this->file->read(512); |
933 | } | |
e5521c73 | 934 | if (($header['size'] % 512) != 0) { |
158bd3ca | 935 | $buffer = $this->file->read(512); |
63b9817b | 936 | $content .= substr($buffer, 0, $header['size'] % 512); |
158bd3ca TD |
937 | } |
938 | ||
939 | return $content; | |
940 | } | |
941 | ||
942 | /** | |
e5521c73 | 943 | * @inheritDoc |
158bd3ca TD |
944 | */ |
945 | public function extract($index, $destination) { | |
946 | if (!$this->read) { | |
947 | $this->open(); | |
948 | $this->readContent(); | |
949 | } | |
950 | $header = $this->getFileInfo($index); | |
951 | ||
e5521c73 TD |
952 | BasicFileUtil::makePath(dirname($destination)); |
953 | if ($header['type'] === 'folder') { | |
954 | BasicFileUtil::makePath($destination); | |
955 | return; | |
956 | } | |
957 | if ($header['type'] === 'symlink') { | |
958 | // skip symlinks | |
959 | return; | |
158bd3ca TD |
960 | } |
961 | ||
962 | // seek to offset | |
963 | $this->file->seek($header['offset']); | |
964 | ||
965 | $targetFile = new File($destination); | |
966 | ||
e5521c73 TD |
967 | // read and write data |
968 | if ($header['size']) { | |
969 | $buffer = $this->file->read($header['size']); | |
970 | $targetFile->write($buffer); | |
158bd3ca | 971 | } |
158bd3ca | 972 | $targetFile->close(); |
e5521c73 | 973 | |
1232bce2 | 974 | BasicFileUtil::makeWritable($destination); |
158bd3ca TD |
975 | |
976 | if ($header['mtime']) { | |
977 | @$targetFile->touch($header['mtime']); | |
978 | } | |
979 | ||
980 | // check filesize | |
981 | if (filesize($destination) != $header['size']) { | |
4fe0b42b | 982 | throw new SystemException("Could not untar file '".$header['filename']."' to '".$destination."'. Maybe disk quota exceeded in folder '".dirname($destination)."'."); |
158bd3ca TD |
983 | } |
984 | ||
985 | return true; | |
986 | } | |
987 | ||
988 | /** | |
989 | * Reads table of contents (TOC) from tar archive. | |
990 | * This does not get the entire to memory but only parts of it. | |
991 | */ | |
992 | protected function readContent() { | |
058cbd6a | 993 | $this->contentList = []; |
158bd3ca TD |
994 | $this->read = true; |
995 | $i = 0; | |
996 | ||
997 | // Read the 512 bytes header | |
db8aa273 | 998 | $longFilename = null; |
158bd3ca TD |
999 | while (strlen($binaryData = $this->file->read(512)) != 0) { |
1000 | // read header | |
1001 | $header = $this->readHeader($binaryData); | |
1002 | if ($header === false) { | |
1003 | continue; | |
1004 | } | |
db8aa273 AE |
1005 | |
1006 | // fixes a bug that files with long names aren't correctly | |
1007 | // extracted | |
1008 | if ($longFilename !== null) { | |
1009 | $header['filename'] = $longFilename; | |
1010 | $longFilename = null; | |
1011 | } | |
1012 | if ($header['typeflag'] == 'L') { | |
1013 | $format = 'Z'.$header['size'].'filename'; | |
1014 | ||
1015 | $fileData = unpack($format, $this->file->read(512)); | |
1016 | $longFilename = $fileData['filename']; | |
1017 | $header['size'] = 0; | |
1018 | } | |
1019 | // don't include the @LongLink file in the content list | |
1020 | else { | |
1021 | $this->contentList[$i] = $header; | |
1022 | $this->contentList[$i]['index'] = $i; | |
1023 | $i++; | |
1024 | } | |
158bd3ca | 1025 | |
63b9817b | 1026 | $this->file->seek($this->file->tell() + (512 * ceil($header['size'] / 512))); |
158bd3ca TD |
1027 | } |
1028 | } | |
1029 | ||
1030 | /** | |
1031 | * Unpacks file header for one file entry. | |
db8aa273 | 1032 | * |
39bea7dd | 1033 | * @param string $binaryData |
4be0ecec | 1034 | * @return array|boolean |
158bd3ca TD |
1035 | */ |
1036 | protected function readHeader($binaryData) { | |
1037 | if (strlen($binaryData) != 512) { | |
1038 | return false; | |
1039 | } | |
2d63c13c | 1040 | |
058cbd6a | 1041 | $header = []; |
158bd3ca TD |
1042 | $checksum = 0; |
1043 | // First part of the header | |
1044 | for ($i = 0; $i < 148; $i++) { | |
1045 | $checksum += ord(substr($binaryData, $i, 1)); | |
1046 | } | |
1047 | // Calculate the checksum | |
1048 | // Ignore the checksum value and replace it by ' ' (space) | |
1049 | for ($i = 148; $i < 156; $i++) { | |
1050 | $checksum += ord(' '); | |
1051 | } | |
1052 | // Last part of the header | |
1053 | for ($i = 156; $i < 512; $i++) { | |
1054 | $checksum += ord(substr($binaryData, $i, 1)); | |
1055 | } | |
2d63c13c | 1056 | |
db8aa273 AE |
1057 | // extract values |
1058 | $format = 'Z100filename/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/Z8checksum/Z1typeflag/Z100link/Z6magic/Z2version/Z32uname/Z32gname/Z8devmajor/Z8devminor/Z155prefix'; | |
32b198a0 AE |
1059 | |
1060 | $data = unpack($format, $binaryData); | |
158bd3ca TD |
1061 | |
1062 | // Extract the properties | |
379875ee | 1063 | $header['checksum'] = octdec(trim($data['checksum'])); |
158bd3ca TD |
1064 | if ($header['checksum'] == $checksum) { |
1065 | $header['filename'] = trim($data['filename']); | |
379875ee MS |
1066 | $header['mode'] = octdec(trim($data['mode'])); |
1067 | $header['uid'] = octdec(trim($data['uid'])); | |
1068 | $header['gid'] = octdec(trim($data['gid'])); | |
1069 | $header['size'] = octdec(trim($data['size'])); | |
1070 | $header['mtime'] = octdec(trim($data['mtime'])); | |
158bd3ca TD |
1071 | $header['prefix'] = trim($data['prefix']); |
1072 | if ($header['prefix']) { | |
1073 | $header['filename'] = $header['prefix'].'/'.$header['filename']; | |
1074 | } | |
e5521c73 TD |
1075 | $header['typeflag'] = $data['typeflag']; |
1076 | if ($header['typeflag'] == '5') { | |
158bd3ca TD |
1077 | $header['size'] = 0; |
1078 | $header['type'] = 'folder'; | |
1079 | } | |
e5521c73 TD |
1080 | else if ($header['typeflag'] == '2') { |
1081 | $header['type'] = 'symlink'; | |
1082 | $header['target'] = $data['link']; | |
1083 | } | |
158bd3ca TD |
1084 | else { |
1085 | $header['type'] = 'file'; | |
1086 | } | |
1087 | $header['offset'] = $this->file->tell(); | |
1088 | ||
1089 | return $header; | |
1090 | } | |
1091 | else { | |
1092 | return false; | |
1093 | } | |
1094 | } | |
e5521c73 TD |
1095 | |
1096 | /** | |
1097 | * Returns true if this tar is (g)zipped. | |
1098 | * | |
1099 | * @return boolean | |
1100 | */ | |
1101 | public function isZipped() { | |
1102 | return $this->isZipped; | |
1103 | } | |
158bd3ca TD |
1104 | } |
1105 | ||
3e823cfa | 1106 | /** @noinspection PhpMultipleClassesDeclarationsInOneFile */ |
158bd3ca TD |
1107 | /** |
1108 | * The File class handles all file operations. | |
1109 | * | |
1110 | * Example: | |
1111 | * using php functions: | |
1112 | * $fp = fopen('filename', 'wb'); | |
1113 | * fwrite($fp, '...'); | |
1114 | * fclose($fp); | |
1115 | * | |
1116 | * using this class: | |
1117 | * $file = new File('filename'); | |
1118 | * $file->write('...'); | |
1119 | * $file->close(); | |
1120 | * | |
1121 | * @author Marcel Werk | |
1122 | */ | |
1123 | class File { | |
1124 | protected $resource = null; | |
1125 | protected $filename; | |
1126 | ||
1127 | /** | |
1128 | * Opens a new file. | |
1129 | * | |
39bea7dd MS |
1130 | * @param string $filename |
1131 | * @param string $mode | |
2b770bdd | 1132 | * @throws SystemException |
158bd3ca TD |
1133 | */ |
1134 | public function __construct($filename, $mode = 'wb') { | |
1135 | $this->filename = $filename; | |
1136 | $this->resource = fopen($filename, $mode); | |
1137 | if ($this->resource === false) { | |
4fe0b42b | 1138 | throw new SystemException('Can not open file ' . $filename); |
158bd3ca TD |
1139 | } |
1140 | } | |
1141 | ||
1142 | /** | |
1143 | * Calls the specified function on the open file. | |
1144 | * Do not call this function directly. Use $file->write('') instead. | |
1145 | * | |
39bea7dd MS |
1146 | * @param string $function |
1147 | * @param array $arguments | |
71952a87 | 1148 | * @return mixed |
2b770bdd | 1149 | * @throws SystemException |
158bd3ca TD |
1150 | */ |
1151 | public function __call($function, $arguments) { | |
1152 | if (function_exists('f' . $function)) { | |
1153 | array_unshift($arguments, $this->resource); | |
39bea7dd | 1154 | return call_user_func_array('f' . $function, $arguments); |
158bd3ca TD |
1155 | } |
1156 | else if (function_exists($function)) { | |
1157 | array_unshift($arguments, $this->filename); | |
39bea7dd | 1158 | return call_user_func_array($function, $arguments); |
158bd3ca TD |
1159 | } |
1160 | else { | |
4fe0b42b | 1161 | throw new SystemException('Can not call file method ' . $function); |
158bd3ca TD |
1162 | } |
1163 | } | |
1164 | } | |
1165 | ||
3e823cfa | 1166 | /** @noinspection PhpMultipleClassesDeclarationsInOneFile */ |
158bd3ca TD |
1167 | /** |
1168 | * The File class handles all file operations on a zipped file. | |
1169 | * | |
1170 | * @author Marcel Werk | |
1171 | */ | |
e5521c73 | 1172 | class GZipFile extends File { |
eedfeca6 AE |
1173 | /** |
1174 | * checks if gz*64 functions are available instead of gz* | |
1175 | * https://bugs.php.net/bug.php?id=53829 | |
1176 | * @var boolean | |
1177 | */ | |
1178 | protected static $gzopen64 = null; | |
1179 | ||
e4bda351 | 1180 | /** @noinspection PhpMissingParentConstructorInspection */ |
158bd3ca | 1181 | /** |
e5521c73 TD |
1182 | * Opens a gzip file. |
1183 | * | |
39bea7dd MS |
1184 | * @param string $filename |
1185 | * @param string $mode | |
2b770bdd | 1186 | * @throws SystemException |
158bd3ca TD |
1187 | */ |
1188 | public function __construct($filename, $mode = 'wb') { | |
eedfeca6 | 1189 | if (self::$gzopen64 === null) { |
63b9817b | 1190 | self::$gzopen64 = function_exists('gzopen64'); |
eedfeca6 AE |
1191 | } |
1192 | ||
158bd3ca | 1193 | $this->filename = $filename; |
083a041c | 1194 | /** @noinspection PhpUndefinedFunctionInspection */ |
e5521c73 | 1195 | $this->resource = (self::$gzopen64 ? gzopen64($filename, $mode) : gzopen($filename, $mode)); |
158bd3ca | 1196 | if ($this->resource === false) { |
4fe0b42b | 1197 | throw new SystemException('Can not open file ' . $filename); |
158bd3ca TD |
1198 | } |
1199 | } | |
1200 | ||
1201 | /** | |
1202 | * Calls the specified function on the open file. | |
e5521c73 | 1203 | * |
39bea7dd MS |
1204 | * @param string $function |
1205 | * @param array $arguments | |
71952a87 | 1206 | * @return mixed |
2b770bdd | 1207 | * @throws SystemException |
158bd3ca TD |
1208 | */ |
1209 | public function __call($function, $arguments) { | |
eedfeca6 AE |
1210 | if (self::$gzopen64 && function_exists('gz' . $function . '64')) { |
1211 | array_unshift($arguments, $this->resource); | |
1212 | return call_user_func_array('gz' . $function . '64', $arguments); | |
1213 | } | |
1214 | else if (function_exists('gz' . $function)) { | |
158bd3ca | 1215 | array_unshift($arguments, $this->resource); |
39bea7dd | 1216 | return call_user_func_array('gz' . $function, $arguments); |
158bd3ca TD |
1217 | } |
1218 | else if (function_exists($function)) { | |
1219 | array_unshift($arguments, $this->filename); | |
39bea7dd | 1220 | return call_user_func_array($function, $arguments); |
158bd3ca TD |
1221 | } |
1222 | else { | |
4fe0b42b | 1223 | throw new SystemException('Can not call method ' . $function); |
158bd3ca TD |
1224 | } |
1225 | } | |
1226 | ||
1227 | /** | |
e5521c73 TD |
1228 | * Returns the filesize of the unzipped file. |
1229 | * | |
1230 | * @return integer | |
158bd3ca TD |
1231 | */ |
1232 | public function getFileSize() { | |
1233 | $byteBlock = 1<<14; | |
1234 | $eof = $byteBlock; | |
1235 | ||
1236 | // the correction is for zip files that are too small | |
1237 | // to get in the first while loop | |
1238 | $correction = 1; | |
1239 | while ($this->seek($eof) == 0) { | |
1240 | $eof += $byteBlock; | |
1241 | $correction = 0; | |
1242 | } | |
1243 | ||
1244 | while ($byteBlock > 1) { | |
1245 | $byteBlock >>= 1; | |
1246 | $eof += $byteBlock * ($this->seek($eof) ? -1 : 1); | |
1247 | } | |
1248 | ||
e5521c73 | 1249 | if ($this->seek($eof) == -1) $eof--; |
158bd3ca TD |
1250 | |
1251 | $this->rewind(); | |
1252 | return $eof - $correction; | |
1253 | } | |
1254 | } | |
1255 | ||
1256 | // let's go | |
1257 | // get temp file prefix | |
1258 | if (isset($_REQUEST['tmpFilePrefix'])) { | |
1259 | $prefix = preg_replace('/[^a-f0-9_]+/', '', $_REQUEST['tmpFilePrefix']); | |
1260 | } | |
1261 | else { | |
1262 | $prefix = substr(sha1(uniqid(microtime())), 0, 8); | |
1263 | } | |
1264 | define('TMP_FILE_PREFIX', $prefix); | |
1265 | ||
1266 | // try to find the temp folder | |
99be741e | 1267 | define('TMP_DIR', BasicFileUtil::getInstallTempFolder()); |
158bd3ca | 1268 | |
7da7f7cc AE |
1269 | /** |
1270 | * Reads a file resource from temp folder. | |
1271 | * | |
1272 | * @param string $key | |
1273 | * @param string $directory | |
1274 | */ | |
1275 | function readFileResource($key, $directory) { | |
ec6e78b9 | 1276 | if (preg_match('~[\w\-]+\.(css|jpg|png|svg|eot|woff|ttf)~', $_GET[$key], $match)) { |
7da7f7cc AE |
1277 | switch ($match[1]) { |
1278 | case 'css': | |
1279 | header('Content-Type: text/css'); | |
1280 | break; | |
1281 | ||
1282 | case 'jpg': | |
1283 | header('Content-Type: image/jpg'); | |
1284 | break; | |
1285 | ||
1286 | case 'png': | |
1287 | header('Content-Type: image/png'); | |
1288 | break; | |
1289 | ||
1290 | case 'svg': | |
1291 | header('Content-Type: image/svg+xml'); | |
1292 | break; | |
ec6e78b9 MW |
1293 | |
1294 | case 'eot': | |
1295 | header('Content-Type: application/vnd.ms-fontobject'); | |
1296 | break; | |
1297 | ||
1298 | case 'woff': | |
1299 | header('Content-Type: application/font-woff'); | |
1300 | break; | |
1301 | ||
1302 | case 'ttf': | |
1303 | header('Content-Type: application/octet-stream'); | |
1304 | break; | |
7da7f7cc AE |
1305 | } |
1306 | ||
2d9861cd AE |
1307 | header('Expires: '.gmdate('D, d M Y H:i:s', time() + 3600).' GMT'); |
1308 | header('Last-Modified: Mon, 26 Jul 1997 05:00:00 GMT'); | |
1309 | header('Cache-Control: public, max-age=3600'); | |
1310 | ||
7da7f7cc | 1311 | readfile($directory . $_GET[$key]); |
158bd3ca TD |
1312 | } |
1313 | exit; | |
1314 | } | |
1315 | ||
7da7f7cc AE |
1316 | // show image from temp folder |
1317 | if (isset($_GET['showImage'])) { | |
1318 | readFileResource('showImage', TMP_DIR . 'install/files/acp/images/'); | |
1319 | } | |
1320 | // show icon from temp folder | |
1321 | if (isset($_GET['showIcon'])) { | |
1322 | readFileResource('showIcon', TMP_DIR . 'install/files/icon/'); | |
1323 | } | |
1324 | // show css from temp folder | |
1325 | if (isset($_GET['showCSS'])) { | |
e94c3830 | 1326 | readFileResource('showCSS', TMP_DIR . 'install/files/acp/style/setup/'); |
7da7f7cc | 1327 | } |
ec6e78b9 MW |
1328 | // show fonts from temp folder |
1329 | if (isset($_GET['showFont'])) { | |
1330 | readFileResource('showFont', TMP_DIR . 'install/files/font/'); | |
1331 | } | |
7da7f7cc | 1332 | |
53e00c6b | 1333 | // check whether setup files are already unzipped |
158bd3ca TD |
1334 | if (!file_exists(TMP_DIR . 'install/files/lib/system/WCFSetup.class.php')) { |
1335 | // try to unzip all setup files into temp folder | |
1336 | $tar = new Tar(SETUP_FILE); | |
1337 | $contentList = $tar->getContentList(); | |
15fa2802 | 1338 | if (empty($contentList)) { |
3a4862d3 | 1339 | throw new SystemException("Cannot unpack 'WCFSetup.tar.gz'. File is probably broken."); |
158bd3ca TD |
1340 | } |
1341 | ||
1342 | foreach ($contentList as $file) { | |
1343 | foreach ($neededFilesPattern as $pattern) { | |
1344 | if (preg_match($pattern, $file['filename'])) { | |
1345 | // create directory if not exists | |
1346 | $dir = TMP_DIR . dirname($file['filename']); | |
1347 | if (!@is_dir($dir)) { | |
1348 | @mkdir($dir, 0777, true); | |
1232bce2 | 1349 | BasicFileUtil::makeWritable($dir); |
158bd3ca TD |
1350 | } |
1351 | ||
1352 | $tar->extract($file['index'], TMP_DIR . $file['filename']); | |
1353 | } | |
1354 | } | |
1355 | } | |
1356 | $tar->close(); | |
1357 | ||
1358 | // create cache folders | |
1359 | @mkdir(TMP_DIR . 'setup/lang/cache/', 0777); | |
1232bce2 | 1360 | BasicFileUtil::makeWritable(TMP_DIR . 'setup/lang/cache/'); |
158bd3ca TD |
1361 | |
1362 | @mkdir(TMP_DIR . 'setup/template/compiled/', 0777); | |
1232bce2 | 1363 | BasicFileUtil::makeWritable(TMP_DIR . 'setup/template/compiled/'); |
158bd3ca TD |
1364 | } |
1365 | ||
1366 | if (!class_exists('wcf\system\WCFSetup')) { | |
3a4862d3 | 1367 | throw new SystemException("Cannot find class 'WCFSetup'"); |
158bd3ca TD |
1368 | } |
1369 | ||
1370 | // start setup | |
dcb3a44c | 1371 | new \wcf\system\WCFSetup(); |