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
>
85 <style type
="text/css">
87 font
-family
: Verdana
, Helvetica
, sans
-serif
;
91 border
: 1px outset lightgrey
;
93 background
-color
: lightgrey
;
97 border
: 1px inset lightgrey
;
102 background
-color
: #154268;
121 <h1
>Fatal error
: <?php
echo htmlspecialchars($this->getMessage()); ?
></h1
>
124 <p
><?php
echo $this->getDescription(); ?
></p
>
125 <?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 } ?>
127 <h2
>Information
:</h2
>
129 <b
>error message
:</b
> <?php
echo htmlspecialchars($this->getMessage()); ?
><br
/>
130 <b
>error code
:</b
> <?php
echo intval($this->getCode()); ?
><br
/>
131 <?php
echo $this->information
; ?
>
132 <b
>file
:</b
> <?php
echo htmlspecialchars($this->getFile()); ?
> (<?php
echo $this->getLine(); ?
>)<br
/>
133 <b
>php version
:</b
> <?php
echo htmlspecialchars(phpversion()); ?
><br
/>
134 <b
>wcf version
:</b
> <?php
if (defined('WCF_VERSION')) echo WCF_VERSION
; ?
><br
/>
135 <b
>date
:</b
> <?php
echo gmdate('r'); ?
><br
/>
136 <b
>request
:</b
> <?php
if (isset($_SERVER['REQUEST_URI'])) echo htmlspecialchars($_SERVER['REQUEST_URI']); ?
><br
/>
137 <b
>referer
:</b
> <?php
if (isset($_SERVER['HTTP_REFERER'])) echo htmlspecialchars($_SERVER['HTTP_REFERER']); ?
><br
/>
141 <pre
><?php
echo htmlspecialchars($this->getTraceAsString()); ?
></pre
>
144 <?php
echo $this->functions
; ?
>
155 * Loads the required classes automatically.
157 function __autoload($className) {
158 $namespaces = explode('\\', $className);
159 if (count($namespaces) > 1) {
160 // remove 'wcf' component
161 array_shift($namespaces);
163 $className = implode('/', $namespaces);
164 $classPath = TMP_DIR
. 'install/files/lib/' . $className . '.class.php';
165 if (file_exists($classPath)) {
166 require_once($classPath);
172 * Escapes strings for execution in sql queries.
174 function escapeString($string) {
175 return \wcf\system\WCF
::getDB()->escapeString($string);
179 * Calls the show method on the given exception.
181 * @param Exception $e
183 function handleException(\Exception
$e) {
184 if ($e instanceof IPrintableException ||
$e instanceof \wcf\system\exception\IPrintableException
) {
193 * Catches php errors and throws instead a system exception.
195 * @param integer $errorNo
196 * @param string $message
197 * @param string $filename
198 * @param integer $lineNo
200 function handleError($errorNo, $message, $filename, $lineNo) {
201 if (error_reporting() != 0) {
204 case 2: $type = 'warning';
206 case 8: $type = 'notice';
210 throw new SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);
215 * BasicFileUtil contains file-related functions.
217 * @package com.woltlab.wcf.util
218 * @author Marcel Werk
220 class BasicFileUtil
{
222 * Tries to find the temp folder.
226 public static function getTempFolder() {
227 $tmpDirName = TMP_FILE_PREFIX
.'/';
229 // use tmp folder in document root by default
230 if (!empty($_SERVER['DOCUMENT_ROOT'])) {
231 if (!@file_exists
($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName)) {
232 @mkdir
($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName, 0777, true);
233 @chmod
($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName, 0777);
236 if (@file_exists
($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName) && @is_writable
($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName)) {
237 return $_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName;
241 foreach (array('TMP', 'TEMP', 'TMPDIR') as $tmpDir) {
242 if (isset($_ENV[$tmpDir]) && @is_writable
($_ENV[$tmpDir])) {
243 $dir = $_ENV[$tmpDir] . '/' . $tmpDirName;
247 if (@file_exists
($dir) && @is_writable
($dir)) {
253 $dir = INSTALL_SCRIPT_DIR
. 'tmp/' . $tmpDirName;
257 if (!@file_exists
($dir) ||
!@is_writable
($dir)) {
258 $tmpDir = explode('/', $dir);
260 $dir = implode('/', $tmpDir);
262 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.');
270 * Opens tar or tar.gz archives.
274 * $tar = new Tar('archive.tar');
275 * $contentList = $tar->getContentList();
276 * foreach ($contentList as $key => $val) {
277 * $tar->extract($key, DESTINATION);
281 protected $archiveName = '';
282 protected $contentList = array();
283 protected $opened = false;
284 protected $read = false;
285 protected $file = null;
286 protected $isZipped = false;
287 protected $mode = 'rb';
290 * Creates a new Tar object.
291 * archiveName must be tarball or gzipped tarball
293 * @param string $archiveName
295 public function __construct($archiveName) {
297 if (!is_file($archiveName)) {
298 throw new SystemException("unable to find tar archive '".$archiveName."'");
301 $this->archiveName
= $archiveName;
303 $this->readContent();
307 * Destructor of this class, closes tar archive.
309 public function __destruct() {
314 * Opens the tar archive and stores filehandle.
316 public function open() {
317 if (!$this->opened
) {
318 if ($this->isZipped
) $this->file
= new ZipFile($this->archiveName
, $this->mode
);
321 $this->file
= new File($this->archiveName
, $this->mode
);
322 if ($this->file
->read(2) == "\37\213") {
323 $this->file
->close();
324 $this->isZipped
= true;
325 $this->file
= new ZipFile($this->archiveName
, $this->mode
);
328 $this->file
->seek(0);
331 $this->opened
= true;
336 * Closes the opened file.
338 public function close() {
340 $this->file
->close();
341 $this->opened
= false;
346 * Returns the table of contents (TOC) list for this tar archive.
348 * @return array list of content
350 public function getContentList() {
353 $this->readContent();
355 return $this->contentList
;
359 * Returns an associative array with information
360 * about a specific file in the archive.
362 * @param mixed $fileindex index or name of the requested file
363 * @return array $fileInfo
365 public function getFileInfo($fileIndex) {
366 if (!is_int($fileIndex)) {
367 $fileIndex = $this->getIndexByFilename($fileIndex);
370 if (!isset($this->contentList
[$fileIndex])) {
371 throw new SystemException("Tar: could find file '$index' in archive");
373 return $this->contentList
[$fileIndex];
377 * Searchs a file in the tar archive
378 * and returns the numeric fileindex.
379 * Returns false if not found.
381 * @param string $filename
382 * @return integer index of the requested file
384 public function getIndexByFilename($filename) {
385 foreach ($this->contentList
as $index => $file) {
386 if ($file['filename'] == $filename) {
394 * Extracts a specific file and returns the content as string.
395 * Returns false if extraction failed.
397 * @param mixed $index index or name of the requested file
398 * @return string content of the requested file
400 public function extractToString($index) {
403 $this->readContent();
405 $header = $this->getFileInfo($index);
407 // can not extract a folder
408 if ($header['type'] != 'file') {
413 $this->file
->seek($header['offset']);
417 $n = floor($header['size'] / 512);
418 for($i = 0; $i < $n; $i++
) {
419 $content .= $this->file
->read(512);
421 if(($header['size'] %
512) != 0) {
422 $buffer = $this->file
->read(512);
423 $content .= substr($buffer, 0, ($header['size'] %
512));
430 * Extracts a specific file and writes it's content
431 * to the file specified with $destination.
433 * @param mixed $index index or name of the requested file
434 * @param string $destination
435 * @return boolean $success
437 public function extract($index, $destination) {
440 $this->readContent();
442 $header = $this->getFileInfo($index);
444 // can not extract a folder
445 if ($header['type'] != 'file') {
450 $this->file
->seek($header['offset']);
452 $targetFile = new File($destination);
455 $n = floor($header['size'] / 512);
456 for ($i = 0; $i < $n; $i++
) {
457 $content = $this->file
->read(512);
458 $targetFile->write($content, 512);
460 if (($header['size'] %
512) != 0) {
461 $content = $this->file
->read(512);
462 $targetFile->write($content, ($header['size'] %
512));
465 $targetFile->close();
466 if (function_exists('apache_get_version') ||
!@$targetFile->is_writable()) {
467 @$targetFile->chmod(0777);
470 @$targetFile->chmod(0755);
473 if ($header['mtime']) {
474 @$targetFile->touch($header['mtime']);
478 if (filesize($destination) != $header['size']) {
479 throw new SystemException("Could not untar file '".$header['filename']."' to '".$destination."'. Maybe disk quota exceeded in folder '".dirname($destination)."'.");
486 * Reads table of contents (TOC) from tar archive.
487 * This does not get the entire to memory but only parts of it.
489 protected function readContent() {
490 $this->contentList
= array();
494 // Read the 512 bytes header
495 while (strlen($binaryData = $this->file
->read(512)) != 0) {
497 $header = $this->readHeader($binaryData);
498 if ($header === false) {
501 $this->contentList
[$i] = $header;
502 $this->contentList
[$i]['index'] = $i;
505 $this->file
->seek($this->file
->tell() +
(512 * ceil(($header['size'] / 512))));
510 * Unpacks file header for one file entry.
512 * @param string $binaryData
513 * @return array $fileheader
515 protected function readHeader($binaryData) {
516 if (strlen($binaryData) != 512) {
522 // First part of the header
523 for ($i = 0; $i < 148; $i++
) {
524 $checksum +
= ord(substr($binaryData, $i, 1));
526 // Calculate the checksum
527 // Ignore the checksum value and replace it by ' ' (space)
528 for ($i = 148; $i < 156; $i++
) {
529 $checksum +
= ord(' ');
531 // Last part of the header
532 for ($i = 156; $i < 512; $i++
) {
533 $checksum +
= ord(substr($binaryData, $i, 1));
536 // Extract the values
537 //$data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $binaryData);
538 $data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix", $binaryData);
540 // Extract the properties
541 $header['checksum'] = octDec(trim($data['checksum']));
542 if ($header['checksum'] == $checksum) {
543 $header['filename'] = trim($data['filename']);
544 $header['mode'] = octDec(trim($data['mode']));
545 $header['uid'] = octDec(trim($data['uid']));
546 $header['gid'] = octDec(trim($data['gid']));
547 $header['size'] = octDec(trim($data['size']));
548 $header['mtime'] = octDec(trim($data['mtime']));
549 $header['prefix'] = trim($data['prefix']);
550 if ($header['prefix']) {
551 $header['filename'] = $header['prefix'].'/'.$header['filename'];
553 if (($header['typeflag'] = $data['typeflag']) == '5') {
555 $header['type'] = 'folder';
558 $header['type'] = 'file';
560 $header['offset'] = $this->file
->tell();
571 * The File class handles all file operations.
574 * using php functions:
575 * $fp = fopen('filename', 'wb');
576 * fwrite($fp, '...');
580 * $file = new File('filename');
581 * $file->write('...');
584 * @author Marcel Werk
587 protected $resource = null;
593 * @param string $filename
594 * @param string $mode
596 public function __construct($filename, $mode = 'wb') {
597 $this->filename
= $filename;
598 $this->resource = fopen($filename, $mode);
599 if ($this->resource === false) {
600 throw new SystemException('Can not open file ' . $filename);
605 * Calls the specified function on the open file.
606 * Do not call this function directly. Use $file->write('') instead.
608 * @param string $function
609 * @param array $arguments
611 public function __call($function, $arguments) {
612 if (function_exists('f' . $function)) {
613 array_unshift($arguments, $this->resource);
614 return call_user_func_array('f' . $function, $arguments);
616 else if (function_exists($function)) {
617 array_unshift($arguments, $this->filename
);
618 return call_user_func_array($function, $arguments);
621 throw new SystemException('Can not call file method ' . $function);
627 * The File class handles all file operations on a zipped file.
629 * @author Marcel Werk
631 class ZipFile
extends File
{
633 * Opens a new zipped file.
635 * @param string $filename
636 * @param string $mode
638 public function __construct($filename, $mode = 'wb') {
639 $this->filename
= $filename;
640 if (!function_exists('gzopen')) {
641 throw new SystemException('Can not find functions of the zlib extension');
643 $this->resource = @gzopen
($filename, $mode);
644 if ($this->resource === false) {
645 throw new SystemException('Can not open file ' . $filename);
650 * Calls the specified function on the open file.
652 * @param string $function
653 * @param array $arguments
655 public function __call($function, $arguments) {
656 if (function_exists('gz' . $function)) {
657 array_unshift($arguments, $this->resource);
658 return call_user_func_array('gz' . $function, $arguments);
660 else if (function_exists($function)) {
661 array_unshift($arguments, $this->filename
);
662 return call_user_func_array($function, $arguments);
665 throw new SystemException('Can not call method ' . $function);
670 * Returns the filesize of the unzipped file
672 public function getFileSize() {
676 // the correction is for zip files that are too small
677 // to get in the first while loop
679 while ($this->seek($eof) == 0) {
684 while ($byteBlock > 1) {
686 $eof +
= $byteBlock * ($this->seek($eof) ?
-1 : 1);
689 if ($this->seek($eof) == -1) $eof -= 1;
692 return $eof - $correction;
697 // get temp file prefix
698 if (isset($_REQUEST['tmpFilePrefix'])) {
699 $prefix = preg_replace('/[^a-f0-9_]+/', '', $_REQUEST['tmpFilePrefix']);
702 $prefix = substr(sha1(uniqid(microtime())), 0, 8);
704 define('TMP_FILE_PREFIX', $prefix);
706 // try to find the temp folder
707 define('TMP_DIR', BasicFileUtil
::getTempFolder());
709 // show image from temp folder
710 if (isset($_GET['showImage'])) {
711 if (preg_match('~[\w\-]+\.(jpg|png)~', $_GET['showImage'], $match)) {
712 if ($match[1] == 'jpg') header('Content-Type: image/jpg');
713 else header('Content-Type: image/png');
714 readfile(TMP_DIR
. 'install/files/acp/images/' . $_GET['showImage']);
719 // check whether setup files are already unzipped
720 if (!file_exists(TMP_DIR
. 'install/files/lib/system/WCFSetup.class.php')) {
721 // try to unzip all setup files into temp folder
722 $tar = new Tar(SETUP_FILE
);
723 $contentList = $tar->getContentList();
724 if (!count($contentList)) {
725 throw new SystemException("Can not unpack 'WCFSetup.tar.gz'. File is probably broken.");
728 foreach ($contentList as $file) {
729 foreach ($neededFilesPattern as $pattern) {
730 if (preg_match($pattern, $file['filename'])) {
731 // create directory if not exists
732 $dir = TMP_DIR
. dirname($file['filename']);
733 if (!@is_dir
($dir)) {
734 @mkdir
($dir, 0777, true);
738 $tar->extract($file['index'], TMP_DIR
. $file['filename']);
744 // create cache folders
745 @mkdir
(TMP_DIR
. 'setup/lang/cache/', 0777);
746 @chmod
(TMP_DIR
. 'setup/lang/cache/', 0777);
748 @mkdir
(TMP_DIR
. 'setup/template/compiled/', 0777);
749 @chmod
(TMP_DIR
. 'setup/template/compiled/', 0777);
752 if (!class_exists('wcf\system\WCFSetup')) {
753 throw new SystemException("Can not find class 'WCFSetup'");
757 new \wcf\system\
WCFSetup();