3 * This script tries to find the temp folder and unzip all setup files into.
6 * @copyright 2001-2018 WoltLab GmbH
7 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
9 // @codingStandardsIgnoreFile
11 define('INSTALL_SCRIPT', __FILE__
);
12 define('INSTALL_SCRIPT_DIR', dirname(__FILE__
).'/');
13 define('SETUP_FILE', INSTALL_SCRIPT_DIR
. 'WCFSetup.tar.gz');
14 define('NO_IMPORTS', 1);
16 // set exception handler
17 set_exception_handler('handleException');
18 // set php error handler
19 set_error_handler('handleError', E_ALL
);
21 // define list of needed file
22 $neededFilesPattern = [
24 '!^install/files/acp/images/woltlabSuite.*!',
25 '!^install/files/acp/style/setup/.*!',
26 '!^install/files/lib/data/.*!',
27 '!^install/files/icon/.*!',
28 '!^install/files/font/.*!',
29 '!^install/files/lib/system/.*!',
30 '!^install/files/lib/util/.*!',
32 '!^install/packages/.*!'];
34 // define needed functions and classes
35 /** @noinspection PhpMultipleClassesDeclarationsInOneFile */
37 * WCF::handleException() calls the show method on exceptions that implement this interface.
39 * @package com.woltlab.wcf
42 interface IPrintableException
{
43 public function show();
46 // define needed classes
48 // SystemException, PrintableException, BasicFileUtil, Tar, File, ZipFile
49 /** @noinspection PhpMultipleClassesDeclarationsInOneFile */
51 * A SystemException is thrown when an unexpected error occurs.
53 * @package com.woltlab.wcf
56 class SystemException
extends \Exception
implements IPrintableException
{
57 protected $description;
58 protected $information = '';
59 protected $functions = '';
62 * Creates a new SystemException.
64 * @param string $message error message
65 * @param integer $code error code
66 * @param string $description description of the error
67 * @param \Exception $previous repacked Exception
69 public function __construct($message = '', $code = 0, $description = '', \Exception
$previous = null) {
70 parent
::__construct((string) $message, (int) $code, $previous);
71 $this->description
= $description;
75 * Returns the description of this exception.
79 public function getDescription() {
80 return $this->description
;
84 * Prints this exception.
85 * This method is called by WCF::handleException().
87 public function show() {
89 * A notice on the HTML used below:
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.
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.
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">
112 .exceptionContainer
{
113 box
-sizing
: border
-box
;
114 font
-family
: 'Segoe UI', 'Lucida Grande', 'Helvetica Neue', Helvetica
, Arial
, sans
-serif
;
116 padding
-bottom
: 20px
;
119 .exceptionContainer
* {
128 background
-color
: rgb(58, 109, 156);
138 .exceptionErrorCode
{
143 .exceptionErrorCode
.exceptionInlineCode
{
144 background
-color
: rgb(43, 79, 113);
147 font
-family
: monospace
;
153 border
-bottom
: 1px solid
rgb(238, 238, 238);
154 color
: rgb(44, 62, 80);
158 padding
-bottom
: 10px
;
161 .exceptionContainer
> .exceptionBoundary
{
165 .exceptionText
.exceptionInlineCodeWrapper
{
166 border
: 1px solid
rgb(169, 169, 169);
171 .exceptionText
.exceptionInlineCode
{
172 font
-family
: monospace
;
176 .exceptionFieldTitle
{
177 color
: rgb(59, 109, 169);
180 .exceptionFieldTitle
.exceptionColon
{
181 /* hide colon in browser, but will be visible after copy & paste */
185 .exceptionFieldValue
{
190 .exceptionSystemInformation
,
191 .exceptionErrorDetails
,
192 .exceptionStacktrace
{
193 list-style
-type
: none
;
196 .exceptionSystemInformation
> li
:not(:first
-child
),
197 .exceptionErrorDetails
> li
:not(:first
-child
) {
201 .exceptionStacktrace
{
205 padding
-bottom
: 20px
;
208 .exceptionStacktraceFile
,
209 .exceptionStacktraceFile span
,
210 .exceptionStacktraceCall
,
211 .exceptionStacktraceCall span
{
212 font
-family
: monospace
!important
;
213 white
-space
: nowrap
!important
;
216 .exceptionStacktraceCall +
.exceptionStacktraceFile
{
220 .exceptionStacktraceCall
{
224 .exceptionStacktraceCall
,
225 .exceptionStacktraceCall span
{
226 color
: rgb(102, 102, 102) !important
;
227 font
-size
: 13px
!important
;
231 @media
(max
-width
: 767px
) {
237 .exceptionText
.exceptionInlineCodeWrapper
{
238 display
: inline
-block
;
242 .exceptionErrorCode
.exceptionInlineCode
{
249 @media
(min
-width
: 768px
) {
257 .exceptionSystemInformation
{
262 .exceptionSystemInformation1
,
263 .exceptionSystemInformation3
,
264 .exceptionSystemInformation5
{
266 margin
: 0 0 10px
0 !important
;
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
);
277 .exceptionSystemInformation1
{ order
: 1; }
278 .exceptionSystemInformation2
{ order
: 2; }
279 .exceptionSystemInformation3
{ order
: 3; }
280 .exceptionSystemInformation4
{ order
: 4; }
281 .exceptionSystemInformation5
{ order
: 5; }
282 .exceptionSystemInformation6
{ order
: 6; }
284 .exceptionSystemInformation
.exceptionFieldValue
{
286 text
-overflow
: ellipsis
;
292 <body
class="exceptionBody">
293 <div
class="exceptionContainer">
294 <div
class="exceptionHeader">
295 <div
class="exceptionBoundary">
296 <p
class="exceptionTitle">An error has occured
</p
>
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
>
307 <li
class="exceptionSystemInformation3">
308 <p
class="exceptionFieldTitle">WoltLab Suite Core
<span
class="exceptionColon">:</span
></p
>
309 <p
class="exceptionFieldValue">3.1</p
>
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
>
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
>
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
>
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
>
334 $trace = $e->getTrace();
335 if (isset($trace[0]['function']) && $trace[0]['function'] === 'handleException') {
336 // ignore repacked exception
341 <div
class="exceptionBoundary">
342 <p
class="exceptionSubtitle"><?php
if (!$e->getPrevious() && !$first) { echo "Original "; } else if ($e->getPrevious() && $first) { echo "Final "; } ?
>Error
</p
>
343 <?php
if ($e instanceof SystemException
&& $e->getDescription()) { ?
>
344 <p
class="exceptionText"><?php
echo $e->getDescription(); ?
></p
>
346 <ul
class="exceptionErrorDetails">
348 <p
class="exceptionFieldTitle">Error Type
<span
class="exceptionColon">:</span
></p
>
349 <p
class="exceptionFieldValue"><?php
echo htmlentities(get_class($e)); ?
></p
>
352 <p
class="exceptionFieldTitle">Error Message
<span
class="exceptionColon">:</span
></p
>
353 <p
class="exceptionFieldValue"><?php
echo htmlentities($e->getMessage()); ?
></p
>
355 <?php
if ($e->getCode()) { ?
>
357 <p
class="exceptionFieldTitle">Error Code
<span
class="exceptionColon">:</span
></p
>
358 <p
class="exceptionFieldValue"><?php
echo intval($e->getCode()); ?
></p
>
362 <p
class="exceptionFieldTitle">File
<span
class="exceptionColon">:</span
></p
>
363 <p
class="exceptionFieldValue" style
="word-break: break-all"><?php
echo htmlentities($e->getFile()); ?
> (<?php
echo $e->getLine(); ?
>)</p
>
367 <p
class="exceptionFieldTitle">Stack Trace
<span
class="exceptionColon">:</span
></p
>
368 <ul
class="exceptionStacktrace">
370 $trace = $e->getTrace();
371 for ($i = 0, $max = count($trace); $i < $max; $i++
) {
373 <li
class="exceptionStacktraceFile"><?php
echo '#'.$i.' '.htmlentities($trace[$i]['file']).' ('.$trace[$i]['line'].')'.':'; ?
></li
>
374 <li
class="exceptionStacktraceCall">
376 echo $trace[$i]['class'].$trace[$i]['type'].$trace[$i]['function'].'(';
377 echo implode(', ', array_map(function ($item) {
378 switch (gettype($item)) {
385 return "'".addcslashes(htmlentities($item), "\\'")."'";
387 return $item ?
'true' : 'false';
389 $keys = array_keys($item);
390 if (count($keys) > 5) return "[ ".count($keys)." items ]";
391 return '[ '.implode(', ', array_map(function ($item) {
395 return get_class($item);
398 throw new \
LogicException('Unreachable');
399 }, $trace[$i]['args']));
409 } while ($e = $e->getPrevious());
421 * Loads the required classes automatically.
423 spl_autoload_register(function($className) {
424 $namespaces = explode('\\', $className);
425 if (count($namespaces) > 1) {
426 // remove 'wcf' component
427 array_shift($namespaces);
429 $className = implode('/', $namespaces);
430 $classPath = TMP_DIR
. 'install/files/lib/' . $className . '.class.php';
431 if (file_exists($classPath)) {
432 require_once($classPath);
438 * Escapes strings for execution in sql queries.
440 * @param string $string
443 function escapeString($string) {
444 return \wcf\system\WCF
::getDB()->escapeString($string);
448 * Helper method to output debug data for all passed variables,
449 * uses `print_r()` for arrays and objects, `var_dump()` otherwise.
451 function wcfDebug() {
454 $args = func_get_args();
455 $length = count($args);
457 echo "ERROR: No arguments provided.<hr>";
460 for ($i = 0; $i < $length; $i++
) {
463 echo "<h2>Argument {$i} (" . gettype($arg) . ")</h2>";
465 if (is_array($arg) ||
is_object($arg)) {
476 $backtrace = debug_backtrace();
478 // output call location to help finding these debug outputs again
479 echo "wcfDebug() called in {$backtrace[0]['file']} on line {$backtrace[0]['line']}";
487 * Calls the show method on the given exception.
491 function handleException($e) {
493 if (!($e instanceof \Exception
)) throw $e;
495 if ($e instanceof IPrintableException ||
$e instanceof \wcf\system\exception\IPrintableException
) {
501 (new SystemException($e->getMessage(), $e->getCode(), '', $e))->show();
504 catch (\Throwable
$exception) {
505 die("<pre>WCF::handleException() Unhandled exception: ".$exception->getMessage()."\n\n".$exception->getTraceAsString());
507 catch (\Exception
$exception) {
508 die("<pre>WCF::handleException() Unhandled exception: ".$exception->getMessage()."\n\n".$exception->getTraceAsString());
513 * Catches php errors and throws instead a system exception.
515 * @param integer $errorNo
516 * @param string $message
517 * @param string $filename
518 * @param integer $lineNo
519 * @throws SystemException
521 function handleError($errorNo, $message, $filename, $lineNo) {
522 if (error_reporting() != 0) {
525 case 2: $type = 'warning';
527 case 8: $type = 'notice';
531 throw new SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);
535 /** @noinspection PhpMultipleClassesDeclarationsInOneFile */
537 * BasicFileUtil contains file-related functions.
539 * @package com.woltlab.wcf
540 * @author Marcel Werk
542 class BasicFileUtil
{
547 protected static $mode = null;
550 * Tries to find the temp folder.
553 * @throws SystemException
555 public static function getTempFolder() {
556 // use tmp folder in document root by default
557 if (!empty($_SERVER['DOCUMENT_ROOT'])) {
558 if (strpos($_SERVER['DOCUMENT_ROOT'], 'strato') !== false) {
560 // create tmp folder in document root automatically
561 if (!@file_exists
($_SERVER['DOCUMENT_ROOT'].'/tmp')) {
562 @mkdir
($_SERVER['DOCUMENT_ROOT'].'/tmp/', 0777);
564 self
::makeWritable($_SERVER['DOCUMENT_ROOT'].'/tmp/');
566 catch (SystemException
$e) {}
569 if (@file_exists
($_SERVER['DOCUMENT_ROOT'].'/tmp') && @is_writable
($_SERVER['DOCUMENT_ROOT'].'/tmp')) {
570 return $_SERVER['DOCUMENT_ROOT'].'/tmp/';
574 if (isset($_ENV['TMP']) && @is_writable
($_ENV['TMP'])) {
575 return $_ENV['TMP'] . '/';
577 if (isset($_ENV['TEMP']) && @is_writable
($_ENV['TEMP'])) {
578 return $_ENV['TEMP'] . '/';
580 if (isset($_ENV['TMPDIR']) && @is_writable
($_ENV['TMPDIR'])) {
581 return $_ENV['TMPDIR'] . '/';
584 if (($path = ini_get('upload_tmp_dir')) && @is_writable
($path)) {
587 if (@file_exists
('/tmp/') && @is_writable
('/tmp/')) {
590 if (function_exists('session_save_path') && ($path = session_save_path()) && @is_writable
($path)) {
594 $path = INSTALL_SCRIPT_DIR
.'tmp/';
595 if (@file_exists
($path) && @is_writable
($path)) {
599 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.');
604 * Returns the temp folder for the installation.
608 public static function getInstallTempFolder() {
609 $dir = self
::getTempFolder() . TMP_FILE_PREFIX
. '/';
611 self
::makeWritable($dir);
617 * Tries to make a file or directory writable. It starts of with the least
618 * permissions and goes up until 0666 for files and 0777 for directories.
620 * @param string $filename
621 * @throws SystemException
623 public static function makeWritable($filename) {
624 if (!file_exists($filename)) {
629 if (self
::$mode === null) {
630 // do not use PHP_OS here, as this represents the system it was built on != running on
631 // php_uname() is forbidden on some strange hosts; PHP_EOL is reliable
632 if (PHP_EOL
== "\r\n") {
637 // anything but Windows
642 $tmpFilename = '__permissions_'.sha1(time()).'.txt';
643 @touch
($tmpFilename);
645 // create a new file and check the file owner, if it is the same
646 // as this file (uploaded through FTP), we can safely grant write
647 // permissions exclusively to the owner rather than everyone
648 if (file_exists($tmpFilename)) {
649 $scriptOwner = fileowner(__FILE__
);
650 $fileOwner = fileowner($tmpFilename);
652 if ($scriptOwner === $fileOwner) {
656 @unlink
($tmpFilename);
661 if (is_dir($filename)) {
662 if (self
::$mode == 0644) {
663 @chmod
($filename, 0755);
666 @chmod
($filename, 0777);
670 @chmod
($filename, self
::$mode);
673 if (!is_writable($filename)) {
674 throw new SystemException("Unable to make '".$filename."' writable. This is a misconfiguration of your server, please contact your system administrator or hosting provider.");
679 /** @noinspection PhpMultipleClassesDeclarationsInOneFile */
681 * Opens tar or tar.gz archives.
685 * $tar = new Tar('archive.tar');
686 * $contentList = $tar->getContentList();
687 * foreach ($contentList as $key => $val) {
688 * $tar->extract($key, DESTINATION);
692 protected $archiveName = '';
693 protected $contentList = [];
694 protected $opened = false;
695 protected $read = false;
696 protected $file = null;
697 protected $isZipped = false;
698 protected $mode = 'rb';
701 * Creates a new Tar object.
702 * archiveName must be tarball or gzipped tarball
704 * @param string $archiveName
705 * @throws SystemException
707 public function __construct($archiveName) {
708 if (!is_file($archiveName)) {
709 throw new SystemException("unable to find tar archive '".$archiveName."'");
712 $this->archiveName
= $archiveName;
714 $this->readContent();
718 * Destructor of this class, closes tar archive.
720 public function __destruct() {
725 * Opens the tar archive and stores filehandle.
727 public function open() {
728 if (!$this->opened
) {
729 if ($this->isZipped
) $this->file
= new ZipFile($this->archiveName
, $this->mode
);
732 $this->file
= new File($this->archiveName
, $this->mode
);
733 if ($this->file
->read(2) == "\37\213") {
734 $this->file
->close();
735 $this->isZipped
= true;
736 $this->file
= new ZipFile($this->archiveName
, $this->mode
);
739 $this->file
->seek(0);
742 $this->opened
= true;
747 * Closes the opened file.
749 public function close() {
751 $this->file
->close();
752 $this->opened
= false;
757 * Returns the table of contents (TOC) list for this tar archive.
759 * @return array list of content
761 public function getContentList() {
764 $this->readContent();
766 return $this->contentList
;
770 * Returns an associative array with information
771 * about a specific file in the archive.
773 * @param mixed $fileIndex index or name of the requested file
775 * @throws SystemException
777 public function getFileInfo($fileIndex) {
778 if (!is_int($fileIndex)) {
779 $fileIndex = $this->getIndexByFilename($fileIndex);
782 if (!isset($this->contentList
[$fileIndex])) {
783 throw new SystemException("Tar: could find file '".$fileIndex."' in archive");
785 return $this->contentList
[$fileIndex];
789 * Searchs a file in the tar archive
790 * and returns the numeric fileindex.
791 * Returns false if not found.
793 * @param string $filename
794 * @return integer index of the requested file
796 public function getIndexByFilename($filename) {
797 foreach ($this->contentList
as $index => $file) {
798 if ($file['filename'] == $filename) {
806 * Extracts a specific file and returns the content as string.
807 * Returns false if extraction failed.
809 * @param mixed $index index or name of the requested file
810 * @return string content of the requested file
812 public function extractToString($index) {
815 $this->readContent();
817 $header = $this->getFileInfo($index);
819 // can not extract a folder
820 if ($header['type'] != 'file') {
825 $this->file
->seek($header['offset']);
829 $n = floor($header['size'] / 512);
830 for($i = 0; $i < $n; $i++
) {
831 $content .= $this->file
->read(512);
833 if(($header['size'] %
512) != 0) {
834 $buffer = $this->file
->read(512);
835 $content .= substr($buffer, 0, $header['size'] %
512);
842 * Extracts a specific file and writes it's content
843 * to the file specified with $destination.
845 * @param mixed $index index or name of the requested file
846 * @param string $destination
848 * @throws SystemException
850 public function extract($index, $destination) {
853 $this->readContent();
855 $header = $this->getFileInfo($index);
857 // can not extract a folder
858 if ($header['type'] != 'file') {
863 $this->file
->seek($header['offset']);
865 $targetFile = new File($destination);
868 $n = floor($header['size'] / 512);
869 for ($i = 0; $i < $n; $i++
) {
870 $content = $this->file
->read(512);
871 $targetFile->write($content, 512);
873 if (($header['size'] %
512) != 0) {
874 $content = $this->file
->read(512);
875 $targetFile->write($content, $header['size'] %
512);
878 $targetFile->close();
879 BasicFileUtil
::makeWritable($destination);
881 if ($header['mtime']) {
882 @$targetFile->touch($header['mtime']);
886 if (filesize($destination) != $header['size']) {
887 throw new SystemException("Could not untar file '".$header['filename']."' to '".$destination."'. Maybe disk quota exceeded in folder '".dirname($destination)."'.");
894 * Reads table of contents (TOC) from tar archive.
895 * This does not get the entire to memory but only parts of it.
897 protected function readContent() {
898 $this->contentList
= [];
902 // Read the 512 bytes header
903 $longFilename = null;
904 while (strlen($binaryData = $this->file
->read(512)) != 0) {
906 $header = $this->readHeader($binaryData);
907 if ($header === false) {
911 // fixes a bug that files with long names aren't correctly
913 if ($longFilename !== null) {
914 $header['filename'] = $longFilename;
915 $longFilename = null;
917 if ($header['typeflag'] == 'L') {
918 $format = 'Z'.$header['size'].'filename';
920 $fileData = unpack($format, $this->file
->read(512));
921 $longFilename = $fileData['filename'];
924 // don't include the @LongLink file in the content list
926 $this->contentList
[$i] = $header;
927 $this->contentList
[$i]['index'] = $i;
931 $this->file
->seek($this->file
->tell() +
(512 * ceil($header['size'] / 512)));
936 * Unpacks file header for one file entry.
938 * @param string $binaryData
939 * @return array|boolean
941 protected function readHeader($binaryData) {
942 if (strlen($binaryData) != 512) {
948 // First part of the header
949 for ($i = 0; $i < 148; $i++
) {
950 $checksum +
= ord(substr($binaryData, $i, 1));
952 // Calculate the checksum
953 // Ignore the checksum value and replace it by ' ' (space)
954 for ($i = 148; $i < 156; $i++
) {
955 $checksum +
= ord(' ');
957 // Last part of the header
958 for ($i = 156; $i < 512; $i++
) {
959 $checksum +
= ord(substr($binaryData, $i, 1));
963 $format = 'Z100filename/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/Z8checksum/Z1typeflag/Z100link/Z6magic/Z2version/Z32uname/Z32gname/Z8devmajor/Z8devminor/Z155prefix';
965 $data = unpack($format, $binaryData);
967 // Extract the properties
968 $header['checksum'] = octdec(trim($data['checksum']));
969 if ($header['checksum'] == $checksum) {
970 $header['filename'] = trim($data['filename']);
971 $header['mode'] = octdec(trim($data['mode']));
972 $header['uid'] = octdec(trim($data['uid']));
973 $header['gid'] = octdec(trim($data['gid']));
974 $header['size'] = octdec(trim($data['size']));
975 $header['mtime'] = octdec(trim($data['mtime']));
976 $header['prefix'] = trim($data['prefix']);
977 if ($header['prefix']) {
978 $header['filename'] = $header['prefix'].'/'.$header['filename'];
980 if (($header['typeflag'] = $data['typeflag']) == '5') {
982 $header['type'] = 'folder';
985 $header['type'] = 'file';
987 $header['offset'] = $this->file
->tell();
997 /** @noinspection PhpMultipleClassesDeclarationsInOneFile */
999 * The File class handles all file operations.
1002 * using php functions:
1003 * $fp = fopen('filename', 'wb');
1004 * fwrite($fp, '...');
1008 * $file = new File('filename');
1009 * $file->write('...');
1012 * @author Marcel Werk
1015 protected $resource = null;
1016 protected $filename;
1021 * @param string $filename
1022 * @param string $mode
1023 * @throws SystemException
1025 public function __construct($filename, $mode = 'wb') {
1026 $this->filename
= $filename;
1027 $this->resource = fopen($filename, $mode);
1028 if ($this->resource === false) {
1029 throw new SystemException('Can not open file ' . $filename);
1034 * Calls the specified function on the open file.
1035 * Do not call this function directly. Use $file->write('') instead.
1037 * @param string $function
1038 * @param array $arguments
1040 * @throws SystemException
1042 public function __call($function, $arguments) {
1043 if (function_exists('f' . $function)) {
1044 array_unshift($arguments, $this->resource);
1045 return call_user_func_array('f' . $function, $arguments);
1047 else if (function_exists($function)) {
1048 array_unshift($arguments, $this->filename
);
1049 return call_user_func_array($function, $arguments);
1052 throw new SystemException('Can not call file method ' . $function);
1057 /** @noinspection PhpMultipleClassesDeclarationsInOneFile */
1059 * The File class handles all file operations on a zipped file.
1061 * @author Marcel Werk
1063 class ZipFile
extends File
{
1065 * checks if gz*64 functions are available instead of gz*
1066 * https://bugs.php.net/bug.php?id=53829
1069 protected static $gzopen64 = null;
1071 /** @noinspection PhpMissingParentConstructorInspection */
1073 * Opens a new zipped file.
1075 * @param string $filename
1076 * @param string $mode
1077 * @throws SystemException
1079 public function __construct($filename, $mode = 'wb') {
1080 if (self
::$gzopen64 === null) {
1081 self
::$gzopen64 = function_exists('gzopen64');
1084 $this->filename
= $filename;
1085 if (!self
::$gzopen64 && !function_exists('gzopen')) {
1086 throw new SystemException('Can not find functions of the zlib extension');
1088 /** @noinspection PhpUndefinedFunctionInspection */
1089 $this->resource = (self
::$gzopen64 ? @gzopen64
($filename, $mode) : @gzopen
($filename, $mode));
1090 if ($this->resource === false) {
1091 throw new SystemException('Can not open file ' . $filename);
1096 * Calls the specified function on the open file.
1098 * @param string $function
1099 * @param array $arguments
1101 * @throws SystemException
1103 public function __call($function, $arguments) {
1104 if (self
::$gzopen64 && function_exists('gz' . $function . '64')) {
1105 array_unshift($arguments, $this->resource);
1106 return call_user_func_array('gz' . $function . '64', $arguments);
1108 else if (function_exists('gz' . $function)) {
1109 array_unshift($arguments, $this->resource);
1110 return call_user_func_array('gz' . $function, $arguments);
1112 else if (function_exists($function)) {
1113 array_unshift($arguments, $this->filename
);
1114 return call_user_func_array($function, $arguments);
1117 throw new SystemException('Can not call method ' . $function);
1122 * Returns the filesize of the unzipped file
1124 public function getFileSize() {
1128 // the correction is for zip files that are too small
1129 // to get in the first while loop
1131 while ($this->seek($eof) == 0) {
1136 while ($byteBlock > 1) {
1138 $eof +
= $byteBlock * ($this->seek($eof) ?
-1 : 1);
1141 if ($this->seek($eof) == -1) $eof -= 1;
1144 return $eof - $correction;
1149 // get temp file prefix
1150 if (isset($_REQUEST['tmpFilePrefix'])) {
1151 $prefix = preg_replace('/[^a-f0-9_]+/', '', $_REQUEST['tmpFilePrefix']);
1154 $prefix = substr(sha1(uniqid(microtime())), 0, 8);
1156 define('TMP_FILE_PREFIX', $prefix);
1158 // try to find the temp folder
1159 define('TMP_DIR', BasicFileUtil
::getInstallTempFolder());
1162 * Reads a file resource from temp folder.
1164 * @param string $key
1165 * @param string $directory
1167 function readFileResource($key, $directory) {
1168 if (preg_match('~[\w\-]+\.(css|jpg|png|svg|eot|woff|ttf)~', $_GET[$key], $match)) {
1169 switch ($match[1]) {
1171 header('Content-Type: text/css');
1175 header('Content-Type: image/jpg');
1179 header('Content-Type: image/png');
1183 header('Content-Type: image/svg+xml');
1187 header('Content-Type: application/vnd.ms-fontobject');
1191 header('Content-Type: application/font-woff');
1195 header('Content-Type: application/octet-stream');
1199 header('Expires: '.gmdate('D, d M Y H:i:s', time() +
3600).' GMT');
1200 header('Last-Modified: Mon, 26 Jul 1997 05:00:00 GMT');
1201 header('Cache-Control: public, max-age=3600');
1203 readfile($directory . $_GET[$key]);
1208 // show image from temp folder
1209 if (isset($_GET['showImage'])) {
1210 readFileResource('showImage', TMP_DIR
. 'install/files/acp/images/');
1212 // show icon from temp folder
1213 if (isset($_GET['showIcon'])) {
1214 readFileResource('showIcon', TMP_DIR
. 'install/files/icon/');
1216 // show css from temp folder
1217 if (isset($_GET['showCSS'])) {
1218 readFileResource('showCSS', TMP_DIR
. 'install/files/acp/style/setup/');
1220 // show fonts from temp folder
1221 if (isset($_GET['showFont'])) {
1222 readFileResource('showFont', TMP_DIR
. 'install/files/font/');
1225 // check whether setup files are already unzipped
1226 if (!file_exists(TMP_DIR
. 'install/files/lib/system/WCFSetup.class.php')) {
1227 // try to unzip all setup files into temp folder
1228 $tar = new Tar(SETUP_FILE
);
1229 $contentList = $tar->getContentList();
1230 if (empty($contentList)) {
1231 throw new SystemException("Cannot unpack 'WCFSetup.tar.gz'. File is probably broken.");
1234 foreach ($contentList as $file) {
1235 foreach ($neededFilesPattern as $pattern) {
1236 if (preg_match($pattern, $file['filename'])) {
1237 // create directory if not exists
1238 $dir = TMP_DIR
. dirname($file['filename']);
1239 if (!@is_dir
($dir)) {
1240 @mkdir
($dir, 0777, true);
1241 BasicFileUtil
::makeWritable($dir);
1244 $tar->extract($file['index'], TMP_DIR
. $file['filename']);
1250 // create cache folders
1251 @mkdir
(TMP_DIR
. 'setup/lang/cache/', 0777);
1252 BasicFileUtil
::makeWritable(TMP_DIR
. 'setup/lang/cache/');
1254 @mkdir
(TMP_DIR
. 'setup/template/compiled/', 0777);
1255 BasicFileUtil
::makeWritable(TMP_DIR
. 'setup/template/compiled/');
1258 if (!class_exists('wcf\system\WCFSetup')) {
1259 throw new SystemException("Cannot find class 'WCFSetup'");
1263 new \wcf\system\
WCFSetup();