3 * This script tries to find the temp folder and unzip all setup files into.
6 * @copyright 2001-2011 WoltLab GmbH
7 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
10 define('INSTALL_SCRIPT_DIR', dirname(__FILE__
).'/');
11 define('SETUP_FILE', INSTALL_SCRIPT_DIR
. 'WCFSetup.tar.gz');
12 define('NO_IMPORTS', 1);
14 // set exception handler
15 set_exception_handler('handleException');
16 // set php error handler
17 set_error_handler('handleError', E_ALL
);
19 // define list of needed file
20 $neededFilesPattern = array(
22 '!^install/files/lib/data/.*!',
23 '!^install/files/lib/system/.*!',
24 '!^install/files/lib/util/.*!',
25 '!^install/files/acp/images/setup.*!',
27 '!^install/packages/.*!');
29 // define needed functions and classes
31 * WCF::handleException() calls the show method on exceptions that implement this interface.
33 * @package com.woltlab.wcf.system.exception
36 interface IPrintableException
{
37 public function show();
40 // define needed classes
42 // SystemException, PrintableException, BasicFileUtil, Tar, File, ZipFile
44 * A SystemException is thrown when an unexpected error occurs.
46 * @package com.woltlab.wcf.system.exception
49 class SystemException
extends \Exception
implements IPrintableException
{
50 protected $description;
51 protected $information = '';
52 protected $functions = '';
55 * Creates a new SystemException.
57 * @param message string error message
58 * @param code integer error code
59 * @param description string description of the error
61 public function __construct($message = '', $code = 0, $description = '') {
62 parent
::__construct($message, $code);
63 $this->description
= $description;
67 * Returns the description of this exception.
71 public function getDescription() {
72 return $this->description
;
76 * Prints this exception.
77 * This method is called by WCF::handleException().
79 public function show() {
83 <title
>Fatal error
: <?php
echo htmlspecialchars($this->getMessage()); ?
></title
>
84 <style type
="text/css">
86 font
-family
: Verdana
, Helvetica
, sans
-serif
;
90 border
: 1px outset lightgrey
;
92 background
-color
: lightgrey
;
96 border
: 1px inset lightgrey
;
101 background
-color
: #154268;
119 <h1
>Fatal error
: <?php
echo htmlspecialchars($this->getMessage()); ?
></h1
>
122 <p
><?php
echo $this->getDescription(); ?
></p
>
123 <?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 } ?>
125 <h2
>Information
:</h2
>
127 <b
>error message
:</b
> <?php
echo htmlspecialchars($this->getMessage()); ?
><br
/>
128 <b
>error code
:</b
> <?php
echo intval($this->getCode()); ?
><br
/>
129 <?php
echo $this->information
; ?
>
130 <b
>file
:</b
> <?php
echo htmlspecialchars($this->getFile()); ?
> (<?php
echo $this->getLine(); ?
>)<br
/>
131 <b
>php version
:</b
> <?php
echo htmlspecialchars(phpversion()); ?
><br
/>
132 <b
>wcf version
:</b
> <?php
if (defined('WCF_VERSION')) echo WCF_VERSION
; ?
><br
/>
133 <b
>date
:</b
> <?php
echo gmdate('r'); ?
><br
/>
134 <b
>request
:</b
> <?php
if (isset($_SERVER['REQUEST_URI'])) echo htmlspecialchars($_SERVER['REQUEST_URI']); ?
><br
/>
135 <b
>referer
:</b
> <?php
if (isset($_SERVER['HTTP_REFERER'])) echo htmlspecialchars($_SERVER['HTTP_REFERER']); ?
><br
/>
139 <pre
><?php
echo htmlspecialchars($this->getTraceAsString()); ?
></pre
>
142 <?php
echo $this->functions
; ?
>
153 * Loads the required classes automatically.
155 function __autoload($className) {
156 $namespaces = explode('\\', $className);
157 if (count($namespaces) > 1) {
158 // remove 'wcf' component
159 array_shift($namespaces);
161 $className = implode('/', $namespaces);
162 $classPath = TMP_DIR
. 'install/files/lib/' . $className . '.class.php';
163 if (file_exists($classPath)) {
164 require_once($classPath);
170 * Escapes strings for execution in sql queries.
172 function escapeString($string) {
173 return \wcf\system\WCF
::getDB()->escapeString($string);
177 * Calls the show method on the given exception.
179 * @param Exception $e
181 function handleException(\Exception
$e) {
182 if ($e instanceof IPrintableException ||
$e instanceof \wcf\system\exception\IPrintableException
) {
191 * Catches php errors and throws instead a system exception.
193 * @param integer $errorNo
194 * @param string $message
195 * @param string $filename
196 * @param integer $lineNo
198 function handleError($errorNo, $message, $filename, $lineNo) {
199 if (error_reporting() != 0) {
202 case 2: $type = 'warning';
204 case 8: $type = 'notice';
208 throw new SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);
213 * BasicFileUtil contains file-related functions.
215 * @package com.woltlab.wcf.util
216 * @author Marcel Werk
218 class BasicFileUtil
{
220 * Tries to find the temp folder.
224 public static function getTempFolder() {
225 $tmpDirName = TMP_FILE_PREFIX
.'/';
227 // use tmp folder in document root by default
228 if (!empty($_SERVER['DOCUMENT_ROOT'])) {
229 if (!@file_exists
($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName)) {
230 @mkdir
($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName, 0777, true);
231 @chmod
($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName, 0777);
234 if (@file_exists
($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName) && @is_writable
($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName)) {
235 return $_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName;
239 foreach (array('TMP', 'TEMP', 'TMPDIR') as $tmpDir) {
240 if (isset($_ENV[$tmpDir]) && @is_writable
($_ENV[$tmpDir])) {
241 $dir = $_ENV[$tmpDir] . '/' . $tmpDirName;
245 if (@file_exists
($dir) && @is_writable
($dir)) {
251 $dir = INSTALL_SCRIPT_DIR
. 'tmp/' . $tmpDirName;
255 if (!@file_exists
($dir) ||
!@is_writable
($dir)) {
256 $tmpDir = explode('/', $dir);
258 $dir = implode('/', $tmpDir);
260 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 '.$dir.' using your favorite ftp program, make it writable and then retry this installation.', 10000);
268 * Opens tar or tar.gz archives.
272 * $tar = new Tar('archive.tar');
273 * $contentList = $tar->getContentList();
274 * foreach ($contentList as $key => $val) {
275 * $tar->extract($key, DESTINATION);
279 protected $archiveName = '';
280 protected $contentList = array();
281 protected $opened = false;
282 protected $read = false;
283 protected $file = null;
284 protected $isZipped = false;
285 protected $mode = 'rb';
288 * Creates a new Tar object.
289 * archiveName must be tarball or gzipped tarball
291 * @param string $archiveName
293 public function __construct($archiveName) {
295 if (!is_file($archiveName)) {
296 throw new SystemException("unable to find tar archive '".$archiveName."'", 11002);
299 $this->archiveName
= $archiveName;
301 $this->readContent();
305 * Destructor of this class, closes tar archive.
307 public function __destruct() {
312 * Opens the tar archive and stores filehandle.
314 public function open() {
315 if (!$this->opened
) {
316 if ($this->isZipped
) $this->file
= new ZipFile($this->archiveName
, $this->mode
);
319 $this->file
= new File($this->archiveName
, $this->mode
);
320 if ($this->file
->read(2) == "\37\213") {
321 $this->file
->close();
322 $this->isZipped
= true;
323 $this->file
= new ZipFile($this->archiveName
, $this->mode
);
326 $this->file
->seek(0);
329 $this->opened
= true;
334 * Closes the opened file.
336 public function close() {
338 $this->file
->close();
339 $this->opened
= false;
344 * Returns the table of contents (TOC) list for this tar archive.
346 * @return array list of content
348 public function getContentList() {
351 $this->readContent();
353 return $this->contentList
;
357 * Returns an associative array with information
358 * about a specific file in the archive.
360 * @param mixed $fileindex index or name of the requested file
361 * @return array $fileInfo
363 public function getFileInfo($fileIndex) {
364 if (!is_int($fileIndex)) {
365 $fileIndex = $this->getIndexByFilename($fileIndex);
368 if (!isset($this->contentList
[$fileIndex])) {
369 throw new SystemException("Tar: could find file '$index' in archive", 11013);
371 return $this->contentList
[$fileIndex];
375 * Searchs a file in the tar archive
376 * and returns the numeric fileindex.
377 * Returns false if not found.
379 * @param string $filename
380 * @return integer index of the requested file
382 public function getIndexByFilename($filename) {
383 foreach ($this->contentList
as $index => $file) {
384 if ($file['filename'] == $filename) {
392 * Extracts a specific file and returns the content as string.
393 * Returns false if extraction failed.
395 * @param mixed $index index or name of the requested file
396 * @return string content of the requested file
398 public function extractToString($index) {
401 $this->readContent();
403 $header = $this->getFileInfo($index);
405 // can not extract a folder
406 if ($header['type'] != 'file') {
411 $this->file
->seek($header['offset']);
415 $n = floor($header['size'] / 512);
416 for($i = 0; $i < $n; $i++
) {
417 $content .= $this->file
->read(512);
419 if(($header['size'] %
512) != 0) {
420 $buffer = $this->file
->read(512);
421 $content .= substr($buffer, 0, ($header['size'] %
512));
428 * Extracts a specific file and writes it's content
429 * to the file specified with $destination.
431 * @param mixed $index index or name of the requested file
432 * @param string $destination
433 * @return boolean $success
435 public function extract($index, $destination) {
438 $this->readContent();
440 $header = $this->getFileInfo($index);
442 // can not extract a folder
443 if ($header['type'] != 'file') {
448 $this->file
->seek($header['offset']);
450 $targetFile = new File($destination);
453 $n = floor($header['size'] / 512);
454 for ($i = 0; $i < $n; $i++
) {
455 $content = $this->file
->read(512);
456 $targetFile->write($content, 512);
458 if (($header['size'] %
512) != 0) {
459 $content = $this->file
->read(512);
460 $targetFile->write($content, ($header['size'] %
512));
463 $targetFile->close();
464 if (function_exists('apache_get_version') ||
!@$targetFile->is_writable()) {
465 @$targetFile->chmod(0777);
468 @$targetFile->chmod(0755);
471 if ($header['mtime']) {
472 @$targetFile->touch($header['mtime']);
476 if (filesize($destination) != $header['size']) {
477 throw new SystemException("Could not untar file '".$header['filename']."' to '".$destination."'. Maybe disk quota exceeded in folder '".dirname($destination)."'.", 11015);
484 * Reads table of contents (TOC) from tar archive.
485 * This does not get the entire to memory but only parts of it.
487 protected function readContent() {
488 $this->contentList
= array();
492 // Read the 512 bytes header
493 while (strlen($binaryData = $this->file
->read(512)) != 0) {
495 $header = $this->readHeader($binaryData);
496 if ($header === false) {
499 $this->contentList
[$i] = $header;
500 $this->contentList
[$i]['index'] = $i;
503 $this->file
->seek($this->file
->tell() +
(512 * ceil(($header['size'] / 512))));
508 * Unpacks file header for one file entry.
510 * @param string $binaryData
511 * @return array $fileheader
513 protected function readHeader($binaryData) {
514 if (strlen($binaryData) != 512) {
520 // First part of the header
521 for ($i = 0; $i < 148; $i++
) {
522 $checksum +
= ord(substr($binaryData, $i, 1));
524 // Calculate the checksum
525 // Ignore the checksum value and replace it by ' ' (space)
526 for ($i = 148; $i < 156; $i++
) {
527 $checksum +
= ord(' ');
529 // Last part of the header
530 for ($i = 156; $i < 512; $i++
) {
531 $checksum +
= ord(substr($binaryData, $i, 1));
534 // Extract the values
535 //$data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $binaryData);
536 $data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix", $binaryData);
538 // Extract the properties
539 $header['checksum'] = octDec(trim($data['checksum']));
540 if ($header['checksum'] == $checksum) {
541 $header['filename'] = trim($data['filename']);
542 $header['mode'] = octDec(trim($data['mode']));
543 $header['uid'] = octDec(trim($data['uid']));
544 $header['gid'] = octDec(trim($data['gid']));
545 $header['size'] = octDec(trim($data['size']));
546 $header['mtime'] = octDec(trim($data['mtime']));
547 $header['prefix'] = trim($data['prefix']);
548 if ($header['prefix']) {
549 $header['filename'] = $header['prefix'].'/'.$header['filename'];
551 if (($header['typeflag'] = $data['typeflag']) == '5') {
553 $header['type'] = 'folder';
556 $header['type'] = 'file';
558 $header['offset'] = $this->file
->tell();
569 * The File class handles all file operations.
572 * using php functions:
573 * $fp = fopen('filename', 'wb');
574 * fwrite($fp, '...');
578 * $file = new File('filename');
579 * $file->write('...');
582 * @author Marcel Werk
585 protected $resource = null;
591 * @param string $filename
592 * @param string $mode
594 public function __construct($filename, $mode = 'wb') {
595 $this->filename
= $filename;
596 $this->resource = fopen($filename, $mode);
597 if ($this->resource === false) {
598 throw new SystemException('Can not open file ' . $filename, 11012);
603 * Calls the specified function on the open file.
604 * Do not call this function directly. Use $file->write('') instead.
606 * @param string $function
607 * @param array $arguments
609 public function __call($function, $arguments) {
610 if (function_exists('f' . $function)) {
611 array_unshift($arguments, $this->resource);
612 return call_user_func_array('f' . $function, $arguments);
614 else if (function_exists($function)) {
615 array_unshift($arguments, $this->filename
);
616 return call_user_func_array($function, $arguments);
619 throw new SystemException('Can not call file method ' . $function, 11003);
625 * The File class handles all file operations on a zipped file.
627 * @author Marcel Werk
629 class ZipFile
extends File
{
631 * Opens a new zipped file.
633 * @param string $filename
634 * @param string $mode
636 public function __construct($filename, $mode = 'wb') {
637 $this->filename
= $filename;
638 if (!function_exists('gzopen')) {
639 throw new SystemException('Can not find functions of the zlib extension', 11004);
641 $this->resource = @gzopen
($filename, $mode);
642 if ($this->resource === false) {
643 throw new SystemException('Can not open file ' . $filename, 11012);
648 * Calls the specified function on the open file.
650 * @param string $function
651 * @param array $arguments
653 public function __call($function, $arguments) {
654 if (function_exists('gz' . $function)) {
655 array_unshift($arguments, $this->resource);
656 return call_user_func_array('gz' . $function, $arguments);
658 else if (function_exists($function)) {
659 array_unshift($arguments, $this->filename
);
660 return call_user_func_array($function, $arguments);
663 throw new SystemException('Can not call method ' . $function, 11003);
668 * Returns the filesize of the unzipped file
670 public function getFileSize() {
674 // the correction is for zip files that are too small
675 // to get in the first while loop
677 while ($this->seek($eof) == 0) {
682 while ($byteBlock > 1) {
684 $eof +
= $byteBlock * ($this->seek($eof) ?
-1 : 1);
687 if ($this->seek($eof) == -1) $eof -= 1;
690 return $eof - $correction;
695 // get temp file prefix
696 if (isset($_REQUEST['tmpFilePrefix'])) {
697 $prefix = preg_replace('/[^a-f0-9_]+/', '', $_REQUEST['tmpFilePrefix']);
700 $prefix = substr(sha1(uniqid(microtime())), 0, 8);
702 define('TMP_FILE_PREFIX', $prefix);
704 // try to find the temp folder
705 define('TMP_DIR', BasicFileUtil
::getTempFolder());
707 // show image from temp folder
708 if (isset($_GET['showImage'])) {
709 if (preg_match('~[\w\-]+\.(jpg|png)~', $_GET['showImage'], $match)) {
710 if ($match[1] == 'jpg') header('Content-Type: image/jpg');
711 else header('Content-Type: image/png');
712 readfile(TMP_DIR
. 'install/files/acp/images/' . $_GET['showImage']);
717 // check whether setup files already unzipped
718 if (!file_exists(TMP_DIR
. 'install/files/lib/system/WCFSetup.class.php')) {
719 // try to unzip all setup files into temp folder
720 $tar = new Tar(SETUP_FILE
);
721 $contentList = $tar->getContentList();
722 if (!count($contentList)) {
723 throw new SystemException("Can not unpack 'WCFSetup.tar.gz'. File is probably broken.", 11016);
726 foreach ($contentList as $file) {
727 foreach ($neededFilesPattern as $pattern) {
728 if (preg_match($pattern, $file['filename'])) {
729 // create directory if not exists
730 $dir = TMP_DIR
. dirname($file['filename']);
731 if (!@is_dir
($dir)) {
732 @mkdir
($dir, 0777, true);
736 $tar->extract($file['index'], TMP_DIR
. $file['filename']);
742 // create cache folders
743 @mkdir
(TMP_DIR
. 'setup/lang/cache/', 0777);
744 @chmod
(TMP_DIR
. 'setup/lang/cache/', 0777);
746 @mkdir
(TMP_DIR
. 'setup/template/compiled/', 0777);
747 @chmod
(TMP_DIR
. 'setup/template/compiled/', 0777);
750 if (!class_exists('wcf\system\WCFSetup')) {
751 throw new SystemException("Can not find class 'WCFSetup'", 11006);
755 new \wcf\system\
WCFSetup();