Merge branch 'next' of github.com:WoltLab/WCF into next
[GitHub/WoltLab/WCF.git] / wcfsetup / install.php
CommitLineData
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
2b6cb5c2 6 * @copyright 2001-2015 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 11define('INSTALL_SCRIPT', __FILE__);
158bd3ca
TD
12define('INSTALL_SCRIPT_DIR', dirname(__FILE__).'/');
13define('SETUP_FILE', INSTALL_SCRIPT_DIR . 'WCFSetup.tar.gz');
14define('NO_IMPORTS', 1);
15
16// set exception handler
17set_exception_handler('handleException');
18// set php error handler
19set_error_handler('handleError', E_ALL);
20
21// define list of needed file
22$neededFilesPattern = array(
23 '!^setup/.*!',
7da7f7cc 24 '!^install/files/acp/images/wcfLogo.*!',
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
TD
31 '!^install/lang/.*!',
32 '!^install/packages/.*!');
33
34// define needed functions and classes
35/**
36 * WCF::handleException() calls the show method on exceptions that implement this interface.
37 *
f4f05aa5 38 * @package com.woltlab.wcf
158bd3ca
TD
39 * @author Marcel Werk
40 */
41interface IPrintableException {
42 public function show();
43}
44
45// define needed classes
46// needed are:
47// SystemException, PrintableException, BasicFileUtil, Tar, File, ZipFile
48/**
49 * A SystemException is thrown when an unexpected error occurs.
50 *
f4f05aa5 51 * @package com.woltlab.wcf
158bd3ca
TD
52 * @author Marcel Werk
53 */
54class SystemException extends \Exception implements IPrintableException {
55 protected $description;
56 protected $information = '';
57 protected $functions = '';
58
59 /**
60 * Creates a new SystemException.
61 *
62 * @param message string error message
63 * @param code integer error code
64 * @param description string description of the error
65 */
66 public function __construct($message = '', $code = 0, $description = '') {
67 parent::__construct($message, $code);
68 $this->description = $description;
69 }
70
71 /**
72 * Returns the description of this exception.
73 *
39bea7dd 74 * @return string
158bd3ca
TD
75 */
76 public function getDescription() {
77 return $this->description;
78 }
79
80 /**
81 * Prints this exception.
82 * This method is called by WCF::handleException().
83 */
84 public function show() {
85 ?>
86<html>
87<head>
88<title>Fatal error: <?php echo htmlspecialchars($this->getMessage()); ?></title>
158bd3ca 89
53e00c6b
L
90<style type="text/css">
91 body {
92 font-family: Verdana, Helvetica, sans-serif;
93 font-size: 0.8em;
94 }
95 div {
96 border: 1px outset lightgrey;
97 padding: 3px;
98 background-color: lightgrey;
99 }
100
101 div div {
102 border: 1px inset lightgrey;
103 padding: 4px;
104 }
105
106 h1 {
107 background-color: #154268;
108 padding: 4px;
109 color: #fff;
110 margin: 0 0 3px 0;
111 font-size: 1.15em;
112 }
113 h2 {
114 font-size: 1.1em;
115 margin-bottom: 0;
116 }
117
118 pre, p {
119 margin: 0;
120 }
158bd3ca
TD
121</style>
122</head>
53e00c6b 123
158bd3ca
TD
124<body>
125 <div>
126 <h1>Fatal error: <?php echo htmlspecialchars($this->getMessage()); ?></h1>
2d63c13c 127
158bd3ca
TD
128 <div>
129 <p><?php echo $this->getDescription(); ?></p>
130 <?php if ($this->getCode()) { ?><p>You get more information about the problem in our knowledge base: <a href="http://www.woltlab.com/help/?code=<?php echo intval($this->getCode()); ?>">http://www.woltlab.com/help/?code=<?php echo intval($this->getCode()); ?></a></p><?php } ?>
131
132 <h2>Information:</h2>
133 <p>
134 <b>error message:</b> <?php echo htmlspecialchars($this->getMessage()); ?><br />
135 <b>error code:</b> <?php echo intval($this->getCode()); ?><br />
136 <?php echo $this->information; ?>
137 <b>file:</b> <?php echo htmlspecialchars($this->getFile()); ?> (<?php echo $this->getLine(); ?>)<br />
138 <b>php version:</b> <?php echo htmlspecialchars(phpversion()); ?><br />
139 <b>wcf version:</b> <?php if (defined('WCF_VERSION')) echo WCF_VERSION; ?><br />
140 <b>date:</b> <?php echo gmdate('r'); ?><br />
141 <b>request:</b> <?php if (isset($_SERVER['REQUEST_URI'])) echo htmlspecialchars($_SERVER['REQUEST_URI']); ?><br />
142 <b>referer:</b> <?php if (isset($_SERVER['HTTP_REFERER'])) echo htmlspecialchars($_SERVER['HTTP_REFERER']); ?><br />
143 </p>
144
145 <h2>Stacktrace:</h2>
146 <pre><?php echo htmlspecialchars($this->getTraceAsString()); ?></pre>
147 </div>
148
149 <?php echo $this->functions; ?>
150 </div>
151</body>
152</html>
153
154<?php
155 }
156}
157
158bd3ca
TD
158/**
159 * Loads the required classes automatically.
160 */
db8aa273 161spl_autoload_register(function($className) {
158bd3ca
TD
162 $namespaces = explode('\\', $className);
163 if (count($namespaces) > 1) {
164 // remove 'wcf' component
165 array_shift($namespaces);
166
167 $className = implode('/', $namespaces);
01bd2eff 168 $classPath = TMP_DIR . 'install/files/lib/' . $className . '.class.php';
158bd3ca
TD
169 if (file_exists($classPath)) {
170 require_once($classPath);
171 }
172 }
db8aa273 173});
158bd3ca
TD
174
175/**
176 * Escapes strings for execution in sql queries.
177 */
178function escapeString($string) {
179 return \wcf\system\WCF::getDB()->escapeString($string);
180}
181
182/**
183 * Calls the show method on the given exception.
184 *
185 * @param Exception $e
186 */
187function handleException(\Exception $e) {
188 if ($e instanceof IPrintableException || $e instanceof \wcf\system\exception\IPrintableException) {
189 $e->show();
190 exit;
191 }
192
193 print $e;
194}
195
196/**
197 * Catches php errors and throws instead a system exception.
198 *
199 * @param integer $errorNo
200 * @param string $message
201 * @param string $filename
202 * @param integer $lineNo
203 */
204function handleError($errorNo, $message, $filename, $lineNo) {
205 if (error_reporting() != 0) {
206 $type = 'error';
207 switch ($errorNo) {
208 case 2: $type = 'warning';
209 break;
210 case 8: $type = 'notice';
211 break;
212 }
213
214 throw new SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);
215 }
216}
217
218/**
219 * BasicFileUtil contains file-related functions.
220 *
f4f05aa5 221 * @package com.woltlab.wcf
158bd3ca
TD
222 * @author Marcel Werk
223 */
224class BasicFileUtil {
d8fa09e0
AE
225 /**
226 * chmod mode
227 * @var integer
228 */
229 protected static $mode = null;
230
158bd3ca
TD
231 /**
232 * Tries to find the temp folder.
233 *
234 * @return string
235 */
236 public static function getTempFolder() {
158bd3ca
TD
237 // use tmp folder in document root by default
238 if (!empty($_SERVER['DOCUMENT_ROOT'])) {
069cd37e
MW
239 if (strpos($_SERVER['DOCUMENT_ROOT'], 'strato') !== false) {
240 // strato bugfix
241 // create tmp folder in document root automatically
242 if (!@file_exists($_SERVER['DOCUMENT_ROOT'].'/tmp')) {
243 @mkdir($_SERVER['DOCUMENT_ROOT'].'/tmp/', 0777);
244 try {
245 self::makeWritable($_SERVER['DOCUMENT_ROOT'].'/tmp/');
246 }
247 catch (SystemException $e) {}
248 }
158bd3ca 249 }
069cd37e
MW
250 if (@file_exists($_SERVER['DOCUMENT_ROOT'].'/tmp') && @is_writable($_SERVER['DOCUMENT_ROOT'].'/tmp')) {
251 return $_SERVER['DOCUMENT_ROOT'].'/tmp/';
158bd3ca
TD
252 }
253 }
e3369fd2 254
069cd37e
MW
255 if (isset($_ENV['TMP']) && @is_writable($_ENV['TMP'])) {
256 return $_ENV['TMP'] . '/';
158bd3ca 257 }
069cd37e
MW
258 if (isset($_ENV['TEMP']) && @is_writable($_ENV['TEMP'])) {
259 return $_ENV['TEMP'] . '/';
260 }
261 if (isset($_ENV['TMPDIR']) && @is_writable($_ENV['TMPDIR'])) {
262 return $_ENV['TMPDIR'] . '/';
263 }
e3369fd2 264
069cd37e
MW
265 if (($path = ini_get('upload_tmp_dir')) && @is_writable($path)) {
266 return $path . '/';
267 }
268 if (@file_exists('/tmp/') && @is_writable('/tmp/')) {
269 return '/tmp/';
158bd3ca 270 }
069cd37e
MW
271 if (function_exists('session_save_path') && ($path = session_save_path()) && @is_writable($path)) {
272 return $path . '/';
273 }
2d63c13c 274
5805a819 275 $path = INSTALL_SCRIPT_DIR.'tmp/';
069cd37e
MW
276 if (@file_exists($path) && @is_writable($path)) {
277 return $path;
278 }
279 else {
280 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.');
281 }
282 }
283
284 /**
285 * Returns the temp folder for the installation.
286 *
287 * @return string
288 */
289 public static function getInstallTempFolder() {
290 $dir = self::getTempFolder() . TMP_FILE_PREFIX . '/';
291 @mkdir($dir);
292 self::makeWritable($dir);
158bd3ca 293
158bd3ca
TD
294 return $dir;
295 }
1232bce2
AE
296
297 /**
298 * Tries to make a file or directory writable. It starts of with the least
d8fa09e0 299 * permissions and goes up until 0666 for files and 0777 for directories.
1232bce2
AE
300 *
301 * @param string $filename
302 */
303 public static function makeWritable($filename) {
043b049d 304 if (!file_exists($filename)) {
1232bce2 305 return;
158bd3ca
TD
306 }
307
d8fa09e0
AE
308 // determine mode
309 if (self::$mode === null) {
310 // do not use PHP_OS here, as this represents the system it was built on != running on
0436b618
AE
311 // php_uname() is forbidden on some strange hosts; PHP_EOL is reliable
312 if (PHP_EOL == "\r\n") {
313 // Windows
d8fa09e0
AE
314 self::$mode = 0777;
315 }
316 else {
0436b618 317 // anything but Windows
adbd8054
AE
318 clearstatcache();
319
d8fa09e0
AE
320 self::$mode = 0666;
321
0c1810be
AE
322 $tmpFilename = '__permissions_'.sha1(time()).'.txt';
323 @touch($tmpFilename);
d8fa09e0
AE
324
325 // create a new file and check the file owner, if it is the same
326 // as this file (uploaded through FTP), we can safely grant write
327 // permissions exclusively to the owner rather than everyone
0c1810be 328 if (file_exists($tmpFilename)) {
d8fa09e0 329 $scriptOwner = fileowner(__FILE__);
0c1810be 330 $fileOwner = fileowner($tmpFilename);
d8fa09e0
AE
331
332 if ($scriptOwner === $fileOwner) {
333 self::$mode = 0644;
334 }
335
0c1810be 336 @unlink($tmpFilename);
d8fa09e0
AE
337 }
338 }
339 }
158bd3ca 340
1232bce2
AE
341 $startIndex = 0;
342 if (is_dir($filename)) {
d8fa09e0 343 if (self::$mode == 0644) {
7fe5312d 344 @chmod($filename, 0755);
1232bce2 345 }
d8fa09e0 346 else {
7fe5312d 347 @chmod($filename, 0777);
1232bce2
AE
348 }
349 }
d8fa09e0 350 else {
7fe5312d 351 @chmod($filename, self::$mode);
d8fa09e0
AE
352 }
353
354 if (!is_writable($filename)) {
355 throw new SystemException("Unable to make '".$filename."' writable. This is a misconfiguration of your server, please contact your system administrator or hosting provider.");
356 }
158bd3ca
TD
357 }
358}
359
360/**
361 * Opens tar or tar.gz archives.
362 *
363 * Usage:
364 * ------
365 * $tar = new Tar('archive.tar');
366 * $contentList = $tar->getContentList();
367 * foreach ($contentList as $key => $val) {
368 * $tar->extract($key, DESTINATION);
369 * }
370 */
371class Tar {
372 protected $archiveName = '';
373 protected $contentList = array();
374 protected $opened = false;
375 protected $read = false;
376 protected $file = null;
377 protected $isZipped = false;
378 protected $mode = 'rb';
379
380 /**
381 * Creates a new Tar object.
382 * archiveName must be tarball or gzipped tarball
383 *
39bea7dd 384 * @param string $archiveName
158bd3ca
TD
385 */
386 public function __construct($archiveName) {
158bd3ca 387 if (!is_file($archiveName)) {
4fe0b42b 388 throw new SystemException("unable to find tar archive '".$archiveName."'");
158bd3ca
TD
389 }
390
391 $this->archiveName = $archiveName;
392 $this->open();
393 $this->readContent();
394 }
395
396 /**
397 * Destructor of this class, closes tar archive.
398 */
399 public function __destruct() {
400 $this->close();
401 }
402
403 /**
404 * Opens the tar archive and stores filehandle.
405 */
406 public function open() {
407 if (!$this->opened) {
408 if ($this->isZipped) $this->file = new ZipFile($this->archiveName, $this->mode);
409 else {
410 // test compression
411 $this->file = new File($this->archiveName, $this->mode);
412 if ($this->file->read(2) == "\37\213") {
413 $this->file->close();
414 $this->isZipped = true;
415 $this->file = new ZipFile($this->archiveName, $this->mode);
416 }
417 else {
418 $this->file->seek(0);
419 }
420 }
421 $this->opened = true;
422 }
423 }
424
425 /**
426 * Closes the opened file.
427 */
428 public function close() {
429 if ($this->opened) {
430 $this->file->close();
431 $this->opened = false;
432 }
433 }
434
435 /**
436 * Returns the table of contents (TOC) list for this tar archive.
437 *
39bea7dd 438 * @return array list of content
158bd3ca
TD
439 */
440 public function getContentList() {
441 if (!$this->read) {
442 $this->open();
443 $this->readContent();
444 }
445 return $this->contentList;
446 }
447
448 /**
449 * Returns an associative array with information
450 * about a specific file in the archive.
451 *
39bea7dd
MS
452 * @param mixed $fileindex index or name of the requested file
453 * @return array $fileInfo
158bd3ca
TD
454 */
455 public function getFileInfo($fileIndex) {
456 if (!is_int($fileIndex)) {
457 $fileIndex = $this->getIndexByFilename($fileIndex);
458 }
459
460 if (!isset($this->contentList[$fileIndex])) {
6286572b 461 throw new SystemException("Tar: could find file '".$fileIndex."' in archive");
158bd3ca
TD
462 }
463 return $this->contentList[$fileIndex];
464 }
465
466 /**
467 * Searchs a file in the tar archive
468 * and returns the numeric fileindex.
469 * Returns false if not found.
470 *
39bea7dd
MS
471 * @param string $filename
472 * @return integer index of the requested file
158bd3ca
TD
473 */
474 public function getIndexByFilename($filename) {
475 foreach ($this->contentList as $index => $file) {
476 if ($file['filename'] == $filename) {
477 return $index;
478 }
479 }
480 return false;
481 }
482
483 /**
484 * Extracts a specific file and returns the content as string.
485 * Returns false if extraction failed.
486 *
39bea7dd
MS
487 * @param mixed $index index or name of the requested file
488 * @return string content of the requested file
158bd3ca
TD
489 */
490 public function extractToString($index) {
491 if (!$this->read) {
492 $this->open();
493 $this->readContent();
494 }
495 $header = $this->getFileInfo($index);
496
497 // can not extract a folder
498 if ($header['type'] != 'file') {
499 return false;
500 }
501
502 // seek to offset
503 $this->file->seek($header['offset']);
504
505 // read data
506 $content = '';
507 $n = floor($header['size'] / 512);
508 for($i = 0; $i < $n; $i++) {
509 $content .= $this->file->read(512);
510 }
511 if(($header['size'] % 512) != 0) {
512 $buffer = $this->file->read(512);
513 $content .= substr($buffer, 0, ($header['size'] % 512));
514 }
515
516 return $content;
517 }
518
519 /**
520 * Extracts a specific file and writes it's content
521 * to the file specified with $destination.
522 *
39bea7dd
MS
523 * @param mixed $index index or name of the requested file
524 * @param string $destination
525 * @return boolean $success
158bd3ca
TD
526 */
527 public function extract($index, $destination) {
528 if (!$this->read) {
529 $this->open();
530 $this->readContent();
531 }
532 $header = $this->getFileInfo($index);
533
534 // can not extract a folder
535 if ($header['type'] != 'file') {
536 return false;
537 }
538
539 // seek to offset
540 $this->file->seek($header['offset']);
541
542 $targetFile = new File($destination);
543
544 // read data
545 $n = floor($header['size'] / 512);
546 for ($i = 0; $i < $n; $i++) {
547 $content = $this->file->read(512);
548 $targetFile->write($content, 512);
549 }
550 if (($header['size'] % 512) != 0) {
551 $content = $this->file->read(512);
552 $targetFile->write($content, ($header['size'] % 512));
553 }
554
555 $targetFile->close();
1232bce2 556 BasicFileUtil::makeWritable($destination);
158bd3ca
TD
557
558 if ($header['mtime']) {
559 @$targetFile->touch($header['mtime']);
560 }
561
562 // check filesize
563 if (filesize($destination) != $header['size']) {
4fe0b42b 564 throw new SystemException("Could not untar file '".$header['filename']."' to '".$destination."'. Maybe disk quota exceeded in folder '".dirname($destination)."'.");
158bd3ca
TD
565 }
566
567 return true;
568 }
569
570 /**
571 * Reads table of contents (TOC) from tar archive.
572 * This does not get the entire to memory but only parts of it.
573 */
574 protected function readContent() {
575 $this->contentList = array();
576 $this->read = true;
577 $i = 0;
578
579 // Read the 512 bytes header
db8aa273 580 $longFilename = null;
158bd3ca
TD
581 while (strlen($binaryData = $this->file->read(512)) != 0) {
582 // read header
583 $header = $this->readHeader($binaryData);
584 if ($header === false) {
585 continue;
586 }
db8aa273
AE
587
588 // fixes a bug that files with long names aren't correctly
589 // extracted
590 if ($longFilename !== null) {
591 $header['filename'] = $longFilename;
592 $longFilename = null;
593 }
594 if ($header['typeflag'] == 'L') {
595 $format = 'Z'.$header['size'].'filename';
596
597 $fileData = unpack($format, $this->file->read(512));
598 $longFilename = $fileData['filename'];
599 $header['size'] = 0;
600 }
601 // don't include the @LongLink file in the content list
602 else {
603 $this->contentList[$i] = $header;
604 $this->contentList[$i]['index'] = $i;
605 $i++;
606 }
158bd3ca
TD
607
608 $this->file->seek($this->file->tell() + (512 * ceil(($header['size'] / 512))));
609 }
610 }
611
612 /**
613 * Unpacks file header for one file entry.
db8aa273 614 *
39bea7dd
MS
615 * @param string $binaryData
616 * @return array $fileheader
158bd3ca
TD
617 */
618 protected function readHeader($binaryData) {
619 if (strlen($binaryData) != 512) {
620 return false;
621 }
2d63c13c 622
158bd3ca
TD
623 $header = array();
624 $checksum = 0;
625 // First part of the header
626 for ($i = 0; $i < 148; $i++) {
627 $checksum += ord(substr($binaryData, $i, 1));
628 }
629 // Calculate the checksum
630 // Ignore the checksum value and replace it by ' ' (space)
631 for ($i = 148; $i < 156; $i++) {
632 $checksum += ord(' ');
633 }
634 // Last part of the header
635 for ($i = 156; $i < 512; $i++) {
636 $checksum += ord(substr($binaryData, $i, 1));
637 }
2d63c13c 638
db8aa273
AE
639 // extract values
640 $format = 'Z100filename/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/Z8checksum/Z1typeflag/Z100link/Z6magic/Z2version/Z32uname/Z32gname/Z8devmajor/Z8devminor/Z155prefix';
32b198a0
AE
641
642 $data = unpack($format, $binaryData);
158bd3ca
TD
643
644 // Extract the properties
379875ee 645 $header['checksum'] = octdec(trim($data['checksum']));
158bd3ca
TD
646 if ($header['checksum'] == $checksum) {
647 $header['filename'] = trim($data['filename']);
379875ee
MS
648 $header['mode'] = octdec(trim($data['mode']));
649 $header['uid'] = octdec(trim($data['uid']));
650 $header['gid'] = octdec(trim($data['gid']));
651 $header['size'] = octdec(trim($data['size']));
652 $header['mtime'] = octdec(trim($data['mtime']));
158bd3ca
TD
653 $header['prefix'] = trim($data['prefix']);
654 if ($header['prefix']) {
655 $header['filename'] = $header['prefix'].'/'.$header['filename'];
656 }
657 if (($header['typeflag'] = $data['typeflag']) == '5') {
658 $header['size'] = 0;
659 $header['type'] = 'folder';
660 }
661 else {
662 $header['type'] = 'file';
663 }
664 $header['offset'] = $this->file->tell();
665
666 return $header;
667 }
668 else {
669 return false;
670 }
671 }
672}
673
674/**
675 * The File class handles all file operations.
676 *
677 * Example:
678 * using php functions:
679 * $fp = fopen('filename', 'wb');
680 * fwrite($fp, '...');
681 * fclose($fp);
682 *
683 * using this class:
684 * $file = new File('filename');
685 * $file->write('...');
686 * $file->close();
687 *
688 * @author Marcel Werk
689 */
690class File {
691 protected $resource = null;
692 protected $filename;
693
694 /**
695 * Opens a new file.
696 *
39bea7dd
MS
697 * @param string $filename
698 * @param string $mode
158bd3ca
TD
699 */
700 public function __construct($filename, $mode = 'wb') {
701 $this->filename = $filename;
702 $this->resource = fopen($filename, $mode);
703 if ($this->resource === false) {
4fe0b42b 704 throw new SystemException('Can not open file ' . $filename);
158bd3ca
TD
705 }
706 }
707
708 /**
709 * Calls the specified function on the open file.
710 * Do not call this function directly. Use $file->write('') instead.
711 *
39bea7dd
MS
712 * @param string $function
713 * @param array $arguments
71952a87 714 * @return mixed
158bd3ca
TD
715 */
716 public function __call($function, $arguments) {
717 if (function_exists('f' . $function)) {
718 array_unshift($arguments, $this->resource);
39bea7dd 719 return call_user_func_array('f' . $function, $arguments);
158bd3ca
TD
720 }
721 else if (function_exists($function)) {
722 array_unshift($arguments, $this->filename);
39bea7dd 723 return call_user_func_array($function, $arguments);
158bd3ca
TD
724 }
725 else {
4fe0b42b 726 throw new SystemException('Can not call file method ' . $function);
158bd3ca
TD
727 }
728 }
729}
730
731/**
732 * The File class handles all file operations on a zipped file.
733 *
734 * @author Marcel Werk
735 */
736class ZipFile extends File {
eedfeca6
AE
737 /**
738 * checks if gz*64 functions are available instead of gz*
739 * https://bugs.php.net/bug.php?id=53829
740 * @var boolean
741 */
742 protected static $gzopen64 = null;
743
158bd3ca
TD
744 /**
745 * Opens a new zipped file.
746 *
39bea7dd
MS
747 * @param string $filename
748 * @param string $mode
158bd3ca
TD
749 */
750 public function __construct($filename, $mode = 'wb') {
eedfeca6
AE
751 if (self::$gzopen64 === null) {
752 self::$gzopen64 = (function_exists('gzopen64'));
753 }
754
158bd3ca 755 $this->filename = $filename;
eedfeca6 756 if (!self::$gzopen64 && !function_exists('gzopen')) {
4fe0b42b 757 throw new SystemException('Can not find functions of the zlib extension');
158bd3ca 758 }
eedfeca6 759 $this->resource = (self::$gzopen64 ? @gzopen64($filename, $mode) : @gzopen($filename, $mode));
158bd3ca 760 if ($this->resource === false) {
4fe0b42b 761 throw new SystemException('Can not open file ' . $filename);
158bd3ca
TD
762 }
763 }
764
765 /**
766 * Calls the specified function on the open file.
767 *
39bea7dd
MS
768 * @param string $function
769 * @param array $arguments
71952a87 770 * @return mixed
158bd3ca
TD
771 */
772 public function __call($function, $arguments) {
eedfeca6
AE
773 if (self::$gzopen64 && function_exists('gz' . $function . '64')) {
774 array_unshift($arguments, $this->resource);
775 return call_user_func_array('gz' . $function . '64', $arguments);
776 }
777 else if (function_exists('gz' . $function)) {
158bd3ca 778 array_unshift($arguments, $this->resource);
39bea7dd 779 return call_user_func_array('gz' . $function, $arguments);
158bd3ca
TD
780 }
781 else if (function_exists($function)) {
782 array_unshift($arguments, $this->filename);
39bea7dd 783 return call_user_func_array($function, $arguments);
158bd3ca
TD
784 }
785 else {
4fe0b42b 786 throw new SystemException('Can not call method ' . $function);
158bd3ca
TD
787 }
788 }
789
790 /**
791 * Returns the filesize of the unzipped file
792 */
793 public function getFileSize() {
794 $byteBlock = 1<<14;
795 $eof = $byteBlock;
796
797 // the correction is for zip files that are too small
798 // to get in the first while loop
799 $correction = 1;
800 while ($this->seek($eof) == 0) {
801 $eof += $byteBlock;
802 $correction = 0;
803 }
804
805 while ($byteBlock > 1) {
806 $byteBlock >>= 1;
807 $eof += $byteBlock * ($this->seek($eof) ? -1 : 1);
808 }
809
810 if ($this->seek($eof) == -1) $eof -= 1;
811
812 $this->rewind();
813 return $eof - $correction;
814 }
815}
816
817// let's go
818// get temp file prefix
819if (isset($_REQUEST['tmpFilePrefix'])) {
820 $prefix = preg_replace('/[^a-f0-9_]+/', '', $_REQUEST['tmpFilePrefix']);
821}
822else {
823 $prefix = substr(sha1(uniqid(microtime())), 0, 8);
824}
825define('TMP_FILE_PREFIX', $prefix);
826
827// try to find the temp folder
99be741e 828define('TMP_DIR', BasicFileUtil::getInstallTempFolder());
158bd3ca 829
7da7f7cc
AE
830/**
831 * Reads a file resource from temp folder.
832 *
833 * @param string $key
834 * @param string $directory
835 */
836function readFileResource($key, $directory) {
ec6e78b9 837 if (preg_match('~[\w\-]+\.(css|jpg|png|svg|eot|woff|ttf)~', $_GET[$key], $match)) {
7da7f7cc
AE
838 switch ($match[1]) {
839 case 'css':
840 header('Content-Type: text/css');
841 break;
842
843 case 'jpg':
844 header('Content-Type: image/jpg');
845 break;
846
847 case 'png':
848 header('Content-Type: image/png');
849 break;
850
851 case 'svg':
852 header('Content-Type: image/svg+xml');
853 break;
ec6e78b9
MW
854
855 case 'eot':
856 header('Content-Type: application/vnd.ms-fontobject');
857 break;
858
859 case 'woff':
860 header('Content-Type: application/font-woff');
861 break;
862
863 case 'ttf':
864 header('Content-Type: application/octet-stream');
865 break;
7da7f7cc
AE
866 }
867
2d9861cd
AE
868 header('Expires: '.gmdate('D, d M Y H:i:s', time() + 3600).' GMT');
869 header('Last-Modified: Mon, 26 Jul 1997 05:00:00 GMT');
870 header('Cache-Control: public, max-age=3600');
871
7da7f7cc 872 readfile($directory . $_GET[$key]);
158bd3ca
TD
873 }
874 exit;
875}
876
7da7f7cc
AE
877// show image from temp folder
878if (isset($_GET['showImage'])) {
879 readFileResource('showImage', TMP_DIR . 'install/files/acp/images/');
880}
881// show icon from temp folder
882if (isset($_GET['showIcon'])) {
883 readFileResource('showIcon', TMP_DIR . 'install/files/icon/');
884}
885// show css from temp folder
886if (isset($_GET['showCSS'])) {
e94c3830 887 readFileResource('showCSS', TMP_DIR . 'install/files/acp/style/setup/');
7da7f7cc 888}
ec6e78b9
MW
889// show fonts from temp folder
890if (isset($_GET['showFont'])) {
891 readFileResource('showFont', TMP_DIR . 'install/files/font/');
892}
7da7f7cc 893
53e00c6b 894// check whether setup files are already unzipped
158bd3ca
TD
895if (!file_exists(TMP_DIR . 'install/files/lib/system/WCFSetup.class.php')) {
896 // try to unzip all setup files into temp folder
897 $tar = new Tar(SETUP_FILE);
898 $contentList = $tar->getContentList();
15fa2802 899 if (empty($contentList)) {
3a4862d3 900 throw new SystemException("Cannot unpack 'WCFSetup.tar.gz'. File is probably broken.");
158bd3ca
TD
901 }
902
903 foreach ($contentList as $file) {
904 foreach ($neededFilesPattern as $pattern) {
905 if (preg_match($pattern, $file['filename'])) {
906 // create directory if not exists
907 $dir = TMP_DIR . dirname($file['filename']);
908 if (!@is_dir($dir)) {
909 @mkdir($dir, 0777, true);
1232bce2 910 BasicFileUtil::makeWritable($dir);
158bd3ca
TD
911 }
912
913 $tar->extract($file['index'], TMP_DIR . $file['filename']);
914 }
915 }
916 }
917 $tar->close();
918
919 // create cache folders
920 @mkdir(TMP_DIR . 'setup/lang/cache/', 0777);
1232bce2 921 BasicFileUtil::makeWritable(TMP_DIR . 'setup/lang/cache/');
158bd3ca
TD
922
923 @mkdir(TMP_DIR . 'setup/template/compiled/', 0777);
1232bce2 924 BasicFileUtil::makeWritable(TMP_DIR . 'setup/template/compiled/');
158bd3ca
TD
925}
926
927if (!class_exists('wcf\system\WCFSetup')) {
3a4862d3 928 throw new SystemException("Cannot find class 'WCFSetup'");
158bd3ca
TD
929}
930
931// start setup
dcb3a44c 932new \wcf\system\WCFSetup();