3 * This script tries to find the temp folder and unzip all setup files into.
6 * @copyright 2001-2014 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 = array(
24 '!^install/files/acp/images/wcfLogo.*!',
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
36 * WCF::handleException() calls the show method on exceptions that implement this interface.
38 * @package com.woltlab.wcf
41 interface IPrintableException
{
42 public function show();
45 // define needed classes
47 // SystemException, PrintableException, BasicFileUtil, Tar, File, ZipFile
49 * A SystemException is thrown when an unexpected error occurs.
51 * @package com.woltlab.wcf
54 class SystemException
extends \Exception
implements IPrintableException
{
55 protected $description;
56 protected $information = '';
57 protected $functions = '';
60 * Creates a new SystemException.
62 * @param message string error message
63 * @param code integer error code
64 * @param description string description of the error
66 public function __construct($message = '', $code = 0, $description = '') {
67 parent
::__construct($message, $code);
68 $this->description
= $description;
72 * Returns the description of this exception.
76 public function getDescription() {
77 return $this->description
;
81 * Prints this exception.
82 * This method is called by WCF::handleException().
84 public function show() {
88 <title
>Fatal error
: <?php
echo htmlspecialchars($this->getMessage()); ?
></title
>
90 <style type
="text/css">
92 font
-family
: Verdana
, Helvetica
, sans
-serif
;
96 border
: 1px outset lightgrey
;
98 background
-color
: lightgrey
;
102 border
: 1px inset lightgrey
;
107 background
-color
: #154268;
126 <h1
>Fatal error
: <?php
echo htmlspecialchars($this->getMessage()); ?
></h1
>
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 } ?>
132 <h2
>Information
:</h2
>
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
/>
146 <pre
><?php
echo htmlspecialchars($this->getTraceAsString()); ?
></pre
>
149 <?php
echo $this->functions
; ?
>
159 * Loads the required classes automatically.
161 function __autoload($className) {
162 $namespaces = explode('\\', $className);
163 if (count($namespaces) > 1) {
164 // remove 'wcf' component
165 array_shift($namespaces);
167 $className = implode('/', $namespaces);
168 $classPath = TMP_DIR
. 'install/files/lib/' . $className . '.class.php';
169 if (file_exists($classPath)) {
170 require_once($classPath);
176 * Escapes strings for execution in sql queries.
178 function escapeString($string) {
179 return \wcf\system\WCF
::getDB()->escapeString($string);
183 * Calls the show method on the given exception.
185 * @param Exception $e
187 function handleException(\Exception
$e) {
188 if ($e instanceof IPrintableException ||
$e instanceof \wcf\system\exception\IPrintableException
) {
197 * Catches php errors and throws instead a system exception.
199 * @param integer $errorNo
200 * @param string $message
201 * @param string $filename
202 * @param integer $lineNo
204 function handleError($errorNo, $message, $filename, $lineNo) {
205 if (error_reporting() != 0) {
208 case 2: $type = 'warning';
210 case 8: $type = 'notice';
214 throw new SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);
219 * BasicFileUtil contains file-related functions.
221 * @package com.woltlab.wcf
222 * @author Marcel Werk
224 class BasicFileUtil
{
229 protected static $mode = null;
232 * Tries to find the temp folder.
236 public static function getTempFolder() {
237 // use tmp folder in document root by default
238 if (!empty($_SERVER['DOCUMENT_ROOT'])) {
239 if (strpos($_SERVER['DOCUMENT_ROOT'], 'strato') !== false) {
241 // create tmp folder in document root automatically
242 if (!@file_exists
($_SERVER['DOCUMENT_ROOT'].'/tmp')) {
243 @mkdir
($_SERVER['DOCUMENT_ROOT'].'/tmp/', 0777);
245 self
::makeWritable($_SERVER['DOCUMENT_ROOT'].'/tmp/');
247 catch (SystemException
$e) {}
250 if (@file_exists
($_SERVER['DOCUMENT_ROOT'].'/tmp') && @is_writable
($_SERVER['DOCUMENT_ROOT'].'/tmp')) {
251 return $_SERVER['DOCUMENT_ROOT'].'/tmp/';
255 if (isset($_ENV['TMP']) && @is_writable
($_ENV['TMP'])) {
256 return $_ENV['TMP'] . '/';
258 if (isset($_ENV['TEMP']) && @is_writable
($_ENV['TEMP'])) {
259 return $_ENV['TEMP'] . '/';
261 if (isset($_ENV['TMPDIR']) && @is_writable
($_ENV['TMPDIR'])) {
262 return $_ENV['TMPDIR'] . '/';
265 if (($path = ini_get('upload_tmp_dir')) && @is_writable
($path)) {
268 if (@file_exists
('/tmp/') && @is_writable
('/tmp/')) {
271 if (function_exists('session_save_path') && ($path = session_save_path()) && @is_writable
($path)) {
275 $path = INSTALL_SCRIPT_DIR
.'tmp/';
276 if (@file_exists
($path) && @is_writable
($path)) {
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.');
285 * Returns the temp folder for the installation.
289 public static function getInstallTempFolder() {
290 $dir = self
::getTempFolder() . TMP_FILE_PREFIX
. '/';
292 self
::makeWritable($dir);
298 * Tries to make a file or directory writable. It starts of with the least
299 * permissions and goes up until 0666 for files and 0777 for directories.
301 * @param string $filename
303 public static function makeWritable($filename) {
304 if (!file_exists($filename)) {
309 if (self
::$mode === null) {
310 // do not use PHP_OS here, as this represents the system it was built on != running on
311 // php_uname() is forbidden on some strange hosts; PHP_EOL is reliable
312 if (PHP_EOL
== "\r\n") {
317 // anything but Windows
322 $tmpFilename = '__permissions_'.sha1(time()).'.txt';
323 @touch
($tmpFilename);
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
328 if (file_exists($tmpFilename)) {
329 $scriptOwner = fileowner(__FILE__
);
330 $fileOwner = fileowner($tmpFilename);
332 if ($scriptOwner === $fileOwner) {
336 @unlink
($tmpFilename);
342 if (is_dir($filename)) {
343 if (self
::$mode == 0644) {
344 @chmod
($filename, 0755);
347 @chmod
($filename, 0777);
351 @chmod
($filename, self
::$mode);
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.");
361 * Opens tar or tar.gz archives.
365 * $tar = new Tar('archive.tar');
366 * $contentList = $tar->getContentList();
367 * foreach ($contentList as $key => $val) {
368 * $tar->extract($key, DESTINATION);
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';
381 * Creates a new Tar object.
382 * archiveName must be tarball or gzipped tarball
384 * @param string $archiveName
386 public function __construct($archiveName) {
387 if (!is_file($archiveName)) {
388 throw new SystemException("unable to find tar archive '".$archiveName."'");
391 $this->archiveName
= $archiveName;
393 $this->readContent();
397 * Destructor of this class, closes tar archive.
399 public function __destruct() {
404 * Opens the tar archive and stores filehandle.
406 public function open() {
407 if (!$this->opened
) {
408 if ($this->isZipped
) $this->file
= new ZipFile($this->archiveName
, $this->mode
);
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
);
418 $this->file
->seek(0);
421 $this->opened
= true;
426 * Closes the opened file.
428 public function close() {
430 $this->file
->close();
431 $this->opened
= false;
436 * Returns the table of contents (TOC) list for this tar archive.
438 * @return array list of content
440 public function getContentList() {
443 $this->readContent();
445 return $this->contentList
;
449 * Returns an associative array with information
450 * about a specific file in the archive.
452 * @param mixed $fileindex index or name of the requested file
453 * @return array $fileInfo
455 public function getFileInfo($fileIndex) {
456 if (!is_int($fileIndex)) {
457 $fileIndex = $this->getIndexByFilename($fileIndex);
460 if (!isset($this->contentList
[$fileIndex])) {
461 throw new SystemException("Tar: could find file '".$fileIndex."' in archive");
463 return $this->contentList
[$fileIndex];
467 * Searchs a file in the tar archive
468 * and returns the numeric fileindex.
469 * Returns false if not found.
471 * @param string $filename
472 * @return integer index of the requested file
474 public function getIndexByFilename($filename) {
475 foreach ($this->contentList
as $index => $file) {
476 if ($file['filename'] == $filename) {
484 * Extracts a specific file and returns the content as string.
485 * Returns false if extraction failed.
487 * @param mixed $index index or name of the requested file
488 * @return string content of the requested file
490 public function extractToString($index) {
493 $this->readContent();
495 $header = $this->getFileInfo($index);
497 // can not extract a folder
498 if ($header['type'] != 'file') {
503 $this->file
->seek($header['offset']);
507 $n = floor($header['size'] / 512);
508 for($i = 0; $i < $n; $i++
) {
509 $content .= $this->file
->read(512);
511 if(($header['size'] %
512) != 0) {
512 $buffer = $this->file
->read(512);
513 $content .= substr($buffer, 0, ($header['size'] %
512));
520 * Extracts a specific file and writes it's content
521 * to the file specified with $destination.
523 * @param mixed $index index or name of the requested file
524 * @param string $destination
525 * @return boolean $success
527 public function extract($index, $destination) {
530 $this->readContent();
532 $header = $this->getFileInfo($index);
534 // can not extract a folder
535 if ($header['type'] != 'file') {
540 $this->file
->seek($header['offset']);
542 $targetFile = new File($destination);
545 $n = floor($header['size'] / 512);
546 for ($i = 0; $i < $n; $i++
) {
547 $content = $this->file
->read(512);
548 $targetFile->write($content, 512);
550 if (($header['size'] %
512) != 0) {
551 $content = $this->file
->read(512);
552 $targetFile->write($content, ($header['size'] %
512));
555 $targetFile->close();
556 BasicFileUtil
::makeWritable($destination);
558 if ($header['mtime']) {
559 @$targetFile->touch($header['mtime']);
563 if (filesize($destination) != $header['size']) {
564 throw new SystemException("Could not untar file '".$header['filename']."' to '".$destination."'. Maybe disk quota exceeded in folder '".dirname($destination)."'.");
571 * Reads table of contents (TOC) from tar archive.
572 * This does not get the entire to memory but only parts of it.
574 protected function readContent() {
575 $this->contentList
= array();
579 // Read the 512 bytes header
580 while (strlen($binaryData = $this->file
->read(512)) != 0) {
582 $header = $this->readHeader($binaryData);
583 if ($header === false) {
586 $this->contentList
[$i] = $header;
587 $this->contentList
[$i]['index'] = $i;
590 $this->file
->seek($this->file
->tell() +
(512 * ceil(($header['size'] / 512))));
595 * Unpacks file header for one file entry.
597 * @param string $binaryData
598 * @return array $fileheader
600 protected function readHeader($binaryData) {
601 if (strlen($binaryData) != 512) {
607 // First part of the header
608 for ($i = 0; $i < 148; $i++
) {
609 $checksum +
= ord(substr($binaryData, $i, 1));
611 // Calculate the checksum
612 // Ignore the checksum value and replace it by ' ' (space)
613 for ($i = 148; $i < 156; $i++
) {
614 $checksum +
= ord(' ');
616 // Last part of the header
617 for ($i = 156; $i < 512; $i++
) {
618 $checksum +
= ord(substr($binaryData, $i, 1));
621 // Extract the values
622 //$data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $binaryData);
623 if (version_compare(PHP_VERSION
, '5.5.0-dev', '>=')) {
624 $format = 'Z100filename/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/Z8checksum/Z1typeflag/Z100link/Z6magic/Z2version/Z32uname/Z32gname/Z8devmajor/Z8devminor/Z155prefix';
627 $format = 'a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix';
630 $data = unpack($format, $binaryData);
632 // Extract the properties
633 $header['checksum'] = octDec(trim($data['checksum']));
634 if ($header['checksum'] == $checksum) {
635 $header['filename'] = trim($data['filename']);
636 $header['mode'] = octDec(trim($data['mode']));
637 $header['uid'] = octDec(trim($data['uid']));
638 $header['gid'] = octDec(trim($data['gid']));
639 $header['size'] = octDec(trim($data['size']));
640 $header['mtime'] = octDec(trim($data['mtime']));
641 $header['prefix'] = trim($data['prefix']);
642 if ($header['prefix']) {
643 $header['filename'] = $header['prefix'].'/'.$header['filename'];
645 if (($header['typeflag'] = $data['typeflag']) == '5') {
647 $header['type'] = 'folder';
650 $header['type'] = 'file';
652 $header['offset'] = $this->file
->tell();
663 * The File class handles all file operations.
666 * using php functions:
667 * $fp = fopen('filename', 'wb');
668 * fwrite($fp, '...');
672 * $file = new File('filename');
673 * $file->write('...');
676 * @author Marcel Werk
679 protected $resource = null;
685 * @param string $filename
686 * @param string $mode
688 public function __construct($filename, $mode = 'wb') {
689 $this->filename
= $filename;
690 $this->resource = fopen($filename, $mode);
691 if ($this->resource === false) {
692 throw new SystemException('Can not open file ' . $filename);
697 * Calls the specified function on the open file.
698 * Do not call this function directly. Use $file->write('') instead.
700 * @param string $function
701 * @param array $arguments
703 public function __call($function, $arguments) {
704 if (function_exists('f' . $function)) {
705 array_unshift($arguments, $this->resource);
706 return call_user_func_array('f' . $function, $arguments);
708 else if (function_exists($function)) {
709 array_unshift($arguments, $this->filename
);
710 return call_user_func_array($function, $arguments);
713 throw new SystemException('Can not call file method ' . $function);
719 * The File class handles all file operations on a zipped file.
721 * @author Marcel Werk
723 class ZipFile
extends File
{
725 * Opens a new zipped file.
727 * @param string $filename
728 * @param string $mode
730 public function __construct($filename, $mode = 'wb') {
731 $this->filename
= $filename;
732 if (!function_exists('gzopen')) {
733 throw new SystemException('Can not find functions of the zlib extension');
735 $this->resource = @gzopen
($filename, $mode);
736 if ($this->resource === false) {
737 throw new SystemException('Can not open file ' . $filename);
742 * Calls the specified function on the open file.
744 * @param string $function
745 * @param array $arguments
747 public function __call($function, $arguments) {
748 if (function_exists('gz' . $function)) {
749 array_unshift($arguments, $this->resource);
750 return call_user_func_array('gz' . $function, $arguments);
752 else if (function_exists($function)) {
753 array_unshift($arguments, $this->filename
);
754 return call_user_func_array($function, $arguments);
757 throw new SystemException('Can not call method ' . $function);
762 * Returns the filesize of the unzipped file
764 public function getFileSize() {
768 // the correction is for zip files that are too small
769 // to get in the first while loop
771 while ($this->seek($eof) == 0) {
776 while ($byteBlock > 1) {
778 $eof +
= $byteBlock * ($this->seek($eof) ?
-1 : 1);
781 if ($this->seek($eof) == -1) $eof -= 1;
784 return $eof - $correction;
789 // get temp file prefix
790 if (isset($_REQUEST['tmpFilePrefix'])) {
791 $prefix = preg_replace('/[^a-f0-9_]+/', '', $_REQUEST['tmpFilePrefix']);
794 $prefix = substr(sha1(uniqid(microtime())), 0, 8);
796 define('TMP_FILE_PREFIX', $prefix);
798 // try to find the temp folder
799 define('TMP_DIR', BasicFileUtil
::getInstallTempFolder());
802 * Reads a file resource from temp folder.
805 * @param string $directory
807 function readFileResource($key, $directory) {
808 if (preg_match('~[\w\-]+\.(css|jpg|png|svg|eot|woff|ttf)~', $_GET[$key], $match)) {
811 header('Content-Type: text/css');
815 header('Content-Type: image/jpg');
819 header('Content-Type: image/png');
823 header('Content-Type: image/svg+xml');
827 header('Content-Type: application/vnd.ms-fontobject');
831 header('Content-Type: application/font-woff');
835 header('Content-Type: application/octet-stream');
839 header('Expires: '.gmdate('D, d M Y H:i:s', time() +
3600).' GMT');
840 header('Last-Modified: Mon, 26 Jul 1997 05:00:00 GMT');
841 header('Cache-Control: public, max-age=3600');
843 readfile($directory . $_GET[$key]);
848 // show image from temp folder
849 if (isset($_GET['showImage'])) {
850 readFileResource('showImage', TMP_DIR
. 'install/files/acp/images/');
852 // show icon from temp folder
853 if (isset($_GET['showIcon'])) {
854 readFileResource('showIcon', TMP_DIR
. 'install/files/icon/');
856 // show css from temp folder
857 if (isset($_GET['showCSS'])) {
858 readFileResource('showCSS', TMP_DIR
. 'install/files/acp/style/setup/');
860 // show fonts from temp folder
861 if (isset($_GET['showFont'])) {
862 readFileResource('showFont', TMP_DIR
. 'install/files/font/');
865 // check whether setup files are already unzipped
866 if (!file_exists(TMP_DIR
. 'install/files/lib/system/WCFSetup.class.php')) {
867 // try to unzip all setup files into temp folder
868 $tar = new Tar(SETUP_FILE
);
869 $contentList = $tar->getContentList();
870 if (empty($contentList)) {
871 throw new SystemException("Can not unpack 'WCFSetup.tar.gz'. File is probably broken.");
874 foreach ($contentList as $file) {
875 foreach ($neededFilesPattern as $pattern) {
876 if (preg_match($pattern, $file['filename'])) {
877 // create directory if not exists
878 $dir = TMP_DIR
. dirname($file['filename']);
879 if (!@is_dir
($dir)) {
880 @mkdir
($dir, 0777, true);
881 BasicFileUtil
::makeWritable($dir);
884 $tar->extract($file['index'], TMP_DIR
. $file['filename']);
890 // create cache folders
891 @mkdir
(TMP_DIR
. 'setup/lang/cache/', 0777);
892 BasicFileUtil
::makeWritable(TMP_DIR
. 'setup/lang/cache/');
894 @mkdir
(TMP_DIR
. 'setup/template/compiled/', 0777);
895 BasicFileUtil
::makeWritable(TMP_DIR
. 'setup/template/compiled/');
898 if (!class_exists('wcf\system\WCFSetup')) {
899 throw new SystemException("Can not find class 'WCFSetup'");
903 new \wcf\system\
WCFSetup();