/
install.php
796 lines (704 loc) · 20 KB
/
install.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
<?php
/**
* This script tries to find the temp folder and unzip all setup files into.
*
* @author Marcel Werk
* @copyright 2001-2011 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
// define constants
define('INSTALL_SCRIPT_DIR', dirname(__FILE__).'/');
define('SETUP_FILE', INSTALL_SCRIPT_DIR . 'WCFSetup.tar.gz');
define('NO_IMPORTS', 1);
// set exception handler
set_exception_handler('handleException');
// set php error handler
set_error_handler('handleError', E_ALL);
// define list of needed file
$neededFilesPattern = array(
'!^setup/.*!',
'!^install/files/acp/images/wcfLogo.*!',
'!^install/files/acp/style/setup/.*!',
'!^install/files/lib/data/.*!',
'!^install/files/icon/.*!',
'!^install/files/lib/system/.*!',
'!^install/files/lib/util/.*!',
'!^install/lang/.*!',
'!^install/packages/.*!');
// define needed functions and classes
/**
* WCF::handleException() calls the show method on exceptions that implement this interface.
*
* @package com.woltlab.wcf.system.exception
* @author Marcel Werk
*/
interface IPrintableException {
public function show();
}
// define needed classes
// needed are:
// SystemException, PrintableException, BasicFileUtil, Tar, File, ZipFile
/**
* A SystemException is thrown when an unexpected error occurs.
*
* @package com.woltlab.wcf.system.exception
* @author Marcel Werk
*/
class SystemException extends \Exception implements IPrintableException {
protected $description;
protected $information = '';
protected $functions = '';
/**
* Creates a new SystemException.
*
* @param message string error message
* @param code integer error code
* @param description string description of the error
*/
public function __construct($message = '', $code = 0, $description = '') {
parent::__construct($message, $code);
$this->description = $description;
}
/**
* Returns the description of this exception.
*
* @return string
*/
public function getDescription() {
return $this->description;
}
/**
* Prints this exception.
* This method is called by WCF::handleException().
*/
public function show() {
?>
<html>
<head>
<title>Fatal error: <?php echo htmlspecialchars($this->getMessage()); ?></title>
<style type="text/css">
body {
font-family: Verdana, Helvetica, sans-serif;
font-size: 0.8em;
}
div {
border: 1px outset lightgrey;
padding: 3px;
background-color: lightgrey;
}
div div {
border: 1px inset lightgrey;
padding: 4px;
}
h1 {
background-color: #154268;
padding: 4px;
color: #fff;
margin: 0 0 3px 0;
font-size: 1.15em;
}
h2 {
font-size: 1.1em;
margin-bottom: 0;
}
pre, p {
margin: 0;
}
</style>
</head>
<body>
<div>
<h1>Fatal error: <?php echo htmlspecialchars($this->getMessage()); ?></h1>
<div>
<p><?php echo $this->getDescription(); ?></p>
<?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 } ?>
<h2>Information:</h2>
<p>
<b>error message:</b> <?php echo htmlspecialchars($this->getMessage()); ?><br />
<b>error code:</b> <?php echo intval($this->getCode()); ?><br />
<?php echo $this->information; ?>
<b>file:</b> <?php echo htmlspecialchars($this->getFile()); ?> (<?php echo $this->getLine(); ?>)<br />
<b>php version:</b> <?php echo htmlspecialchars(phpversion()); ?><br />
<b>wcf version:</b> <?php if (defined('WCF_VERSION')) echo WCF_VERSION; ?><br />
<b>date:</b> <?php echo gmdate('r'); ?><br />
<b>request:</b> <?php if (isset($_SERVER['REQUEST_URI'])) echo htmlspecialchars($_SERVER['REQUEST_URI']); ?><br />
<b>referer:</b> <?php if (isset($_SERVER['HTTP_REFERER'])) echo htmlspecialchars($_SERVER['HTTP_REFERER']); ?><br />
</p>
<h2>Stacktrace:</h2>
<pre><?php echo htmlspecialchars($this->getTraceAsString()); ?></pre>
</div>
<?php echo $this->functions; ?>
</div>
</body>
</html>
<?php
}
}
/**
* Loads the required classes automatically.
*/
function __autoload($className) {
$namespaces = explode('\\', $className);
if (count($namespaces) > 1) {
// remove 'wcf' component
array_shift($namespaces);
$className = implode('/', $namespaces);
$classPath = TMP_DIR . 'install/files/lib/' . $className . '.class.php';
if (file_exists($classPath)) {
require_once($classPath);
}
}
}
/**
* Escapes strings for execution in sql queries.
*/
function escapeString($string) {
return \wcf\system\WCF::getDB()->escapeString($string);
}
/**
* Calls the show method on the given exception.
*
* @param Exception $e
*/
function handleException(\Exception $e) {
if ($e instanceof IPrintableException || $e instanceof \wcf\system\exception\IPrintableException) {
$e->show();
exit;
}
print $e;
}
/**
* Catches php errors and throws instead a system exception.
*
* @param integer $errorNo
* @param string $message
* @param string $filename
* @param integer $lineNo
*/
function handleError($errorNo, $message, $filename, $lineNo) {
if (error_reporting() != 0) {
$type = 'error';
switch ($errorNo) {
case 2: $type = 'warning';
break;
case 8: $type = 'notice';
break;
}
throw new SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);
}
}
/**
* BasicFileUtil contains file-related functions.
*
* @package com.woltlab.wcf.util
* @author Marcel Werk
*/
class BasicFileUtil {
/**
* Tries to find the temp folder.
*
* @return string
*/
public static function getTempFolder() {
$tmpDirName = TMP_FILE_PREFIX.'/';
// use tmp folder in document root by default
if (!empty($_SERVER['DOCUMENT_ROOT'])) {
if (!@file_exists($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName)) {
@mkdir($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName, 0777, true);
@chmod($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName, 0777);
}
if (@file_exists($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName) && @is_writable($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName)) {
return $_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName;
}
}
foreach (array('TMP', 'TEMP', 'TMPDIR') as $tmpDir) {
if (isset($_ENV[$tmpDir]) && @is_writable($_ENV[$tmpDir])) {
$dir = $_ENV[$tmpDir] . '/' . $tmpDirName;
@mkdir($dir, 0777);
@chmod($dir, 0777);
if (@file_exists($dir) && @is_writable($dir)) {
return $dir;
}
}
}
$dir = INSTALL_SCRIPT_DIR . 'tmp/' . $tmpDirName;
@mkdir($dir, 0777);
@chmod($dir, 0777);
if (!@file_exists($dir) || !@is_writable($dir)) {
$tmpDir = explode('/', $dir);
array_pop($tmpDir);
$dir = implode('/', $tmpDir);
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.');
}
return $dir;
}
}
/**
* Opens tar or tar.gz archives.
*
* Usage:
* ------
* $tar = new Tar('archive.tar');
* $contentList = $tar->getContentList();
* foreach ($contentList as $key => $val) {
* $tar->extract($key, DESTINATION);
* }
*/
class Tar {
protected $archiveName = '';
protected $contentList = array();
protected $opened = false;
protected $read = false;
protected $file = null;
protected $isZipped = false;
protected $mode = 'rb';
/**
* Creates a new Tar object.
* archiveName must be tarball or gzipped tarball
*
* @param string $archiveName
*/
public function __construct($archiveName) {
if (!is_file($archiveName)) {
throw new SystemException("unable to find tar archive '".$archiveName."'");
}
$this->archiveName = $archiveName;
$this->open();
$this->readContent();
}
/**
* Destructor of this class, closes tar archive.
*/
public function __destruct() {
$this->close();
}
/**
* Opens the tar archive and stores filehandle.
*/
public function open() {
if (!$this->opened) {
if ($this->isZipped) $this->file = new ZipFile($this->archiveName, $this->mode);
else {
// test compression
$this->file = new File($this->archiveName, $this->mode);
if ($this->file->read(2) == "\37\213") {
$this->file->close();
$this->isZipped = true;
$this->file = new ZipFile($this->archiveName, $this->mode);
}
else {
$this->file->seek(0);
}
}
$this->opened = true;
}
}
/**
* Closes the opened file.
*/
public function close() {
if ($this->opened) {
$this->file->close();
$this->opened = false;
}
}
/**
* Returns the table of contents (TOC) list for this tar archive.
*
* @return array list of content
*/
public function getContentList() {
if (!$this->read) {
$this->open();
$this->readContent();
}
return $this->contentList;
}
/**
* Returns an associative array with information
* about a specific file in the archive.
*
* @param mixed $fileindex index or name of the requested file
* @return array $fileInfo
*/
public function getFileInfo($fileIndex) {
if (!is_int($fileIndex)) {
$fileIndex = $this->getIndexByFilename($fileIndex);
}
if (!isset($this->contentList[$fileIndex])) {
throw new SystemException("Tar: could find file '".$fileIndex."' in archive");
}
return $this->contentList[$fileIndex];
}
/**
* Searchs a file in the tar archive
* and returns the numeric fileindex.
* Returns false if not found.
*
* @param string $filename
* @return integer index of the requested file
*/
public function getIndexByFilename($filename) {
foreach ($this->contentList as $index => $file) {
if ($file['filename'] == $filename) {
return $index;
}
}
return false;
}
/**
* Extracts a specific file and returns the content as string.
* Returns false if extraction failed.
*
* @param mixed $index index or name of the requested file
* @return string content of the requested file
*/
public function extractToString($index) {
if (!$this->read) {
$this->open();
$this->readContent();
}
$header = $this->getFileInfo($index);
// can not extract a folder
if ($header['type'] != 'file') {
return false;
}
// seek to offset
$this->file->seek($header['offset']);
// read data
$content = '';
$n = floor($header['size'] / 512);
for($i = 0; $i < $n; $i++) {
$content .= $this->file->read(512);
}
if(($header['size'] % 512) != 0) {
$buffer = $this->file->read(512);
$content .= substr($buffer, 0, ($header['size'] % 512));
}
return $content;
}
/**
* Extracts a specific file and writes it's content
* to the file specified with $destination.
*
* @param mixed $index index or name of the requested file
* @param string $destination
* @return boolean $success
*/
public function extract($index, $destination) {
if (!$this->read) {
$this->open();
$this->readContent();
}
$header = $this->getFileInfo($index);
// can not extract a folder
if ($header['type'] != 'file') {
return false;
}
// seek to offset
$this->file->seek($header['offset']);
$targetFile = new File($destination);
// read data
$n = floor($header['size'] / 512);
for ($i = 0; $i < $n; $i++) {
$content = $this->file->read(512);
$targetFile->write($content, 512);
}
if (($header['size'] % 512) != 0) {
$content = $this->file->read(512);
$targetFile->write($content, ($header['size'] % 512));
}
$targetFile->close();
if (function_exists('apache_get_version') || !@$targetFile->is_writable()) {
@$targetFile->chmod(0777);
}
else {
@$targetFile->chmod(0755);
}
if ($header['mtime']) {
@$targetFile->touch($header['mtime']);
}
// check filesize
if (filesize($destination) != $header['size']) {
throw new SystemException("Could not untar file '".$header['filename']."' to '".$destination."'. Maybe disk quota exceeded in folder '".dirname($destination)."'.");
}
return true;
}
/**
* Reads table of contents (TOC) from tar archive.
* This does not get the entire to memory but only parts of it.
*/
protected function readContent() {
$this->contentList = array();
$this->read = true;
$i = 0;
// Read the 512 bytes header
while (strlen($binaryData = $this->file->read(512)) != 0) {
// read header
$header = $this->readHeader($binaryData);
if ($header === false) {
continue;
}
$this->contentList[$i] = $header;
$this->contentList[$i]['index'] = $i;
$i++;
$this->file->seek($this->file->tell() + (512 * ceil(($header['size'] / 512))));
}
}
/**
* Unpacks file header for one file entry.
*
* @param string $binaryData
* @return array $fileheader
*/
protected function readHeader($binaryData) {
if (strlen($binaryData) != 512) {
return false;
}
$header = array();
$checksum = 0;
// First part of the header
for ($i = 0; $i < 148; $i++) {
$checksum += ord(substr($binaryData, $i, 1));
}
// Calculate the checksum
// Ignore the checksum value and replace it by ' ' (space)
for ($i = 148; $i < 156; $i++) {
$checksum += ord(' ');
}
// Last part of the header
for ($i = 156; $i < 512; $i++) {
$checksum += ord(substr($binaryData, $i, 1));
}
// Extract the values
//$data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $binaryData);
$data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix", $binaryData);
// Extract the properties
$header['checksum'] = octDec(trim($data['checksum']));
if ($header['checksum'] == $checksum) {
$header['filename'] = trim($data['filename']);
$header['mode'] = octDec(trim($data['mode']));
$header['uid'] = octDec(trim($data['uid']));
$header['gid'] = octDec(trim($data['gid']));
$header['size'] = octDec(trim($data['size']));
$header['mtime'] = octDec(trim($data['mtime']));
$header['prefix'] = trim($data['prefix']);
if ($header['prefix']) {
$header['filename'] = $header['prefix'].'/'.$header['filename'];
}
if (($header['typeflag'] = $data['typeflag']) == '5') {
$header['size'] = 0;
$header['type'] = 'folder';
}
else {
$header['type'] = 'file';
}
$header['offset'] = $this->file->tell();
return $header;
}
else {
return false;
}
}
}
/**
* The File class handles all file operations.
*
* Example:
* using php functions:
* $fp = fopen('filename', 'wb');
* fwrite($fp, '...');
* fclose($fp);
*
* using this class:
* $file = new File('filename');
* $file->write('...');
* $file->close();
*
* @author Marcel Werk
*/
class File {
protected $resource = null;
protected $filename;
/**
* Opens a new file.
*
* @param string $filename
* @param string $mode
*/
public function __construct($filename, $mode = 'wb') {
$this->filename = $filename;
$this->resource = fopen($filename, $mode);
if ($this->resource === false) {
throw new SystemException('Can not open file ' . $filename);
}
}
/**
* Calls the specified function on the open file.
* Do not call this function directly. Use $file->write('') instead.
*
* @param string $function
* @param array $arguments
*/
public function __call($function, $arguments) {
if (function_exists('f' . $function)) {
array_unshift($arguments, $this->resource);
return call_user_func_array('f' . $function, $arguments);
}
else if (function_exists($function)) {
array_unshift($arguments, $this->filename);
return call_user_func_array($function, $arguments);
}
else {
throw new SystemException('Can not call file method ' . $function);
}
}
}
/**
* The File class handles all file operations on a zipped file.
*
* @author Marcel Werk
*/
class ZipFile extends File {
/**
* Opens a new zipped file.
*
* @param string $filename
* @param string $mode
*/
public function __construct($filename, $mode = 'wb') {
$this->filename = $filename;
if (!function_exists('gzopen')) {
throw new SystemException('Can not find functions of the zlib extension');
}
$this->resource = @gzopen($filename, $mode);
if ($this->resource === false) {
throw new SystemException('Can not open file ' . $filename);
}
}
/**
* Calls the specified function on the open file.
*
* @param string $function
* @param array $arguments
*/
public function __call($function, $arguments) {
if (function_exists('gz' . $function)) {
array_unshift($arguments, $this->resource);
return call_user_func_array('gz' . $function, $arguments);
}
else if (function_exists($function)) {
array_unshift($arguments, $this->filename);
return call_user_func_array($function, $arguments);
}
else {
throw new SystemException('Can not call method ' . $function);
}
}
/**
* Returns the filesize of the unzipped file
*/
public function getFileSize() {
$byteBlock = 1<<14;
$eof = $byteBlock;
// the correction is for zip files that are too small
// to get in the first while loop
$correction = 1;
while ($this->seek($eof) == 0) {
$eof += $byteBlock;
$correction = 0;
}
while ($byteBlock > 1) {
$byteBlock >>= 1;
$eof += $byteBlock * ($this->seek($eof) ? -1 : 1);
}
if ($this->seek($eof) == -1) $eof -= 1;
$this->rewind();
return $eof - $correction;
}
}
// let's go
// get temp file prefix
if (isset($_REQUEST['tmpFilePrefix'])) {
$prefix = preg_replace('/[^a-f0-9_]+/', '', $_REQUEST['tmpFilePrefix']);
}
else {
$prefix = substr(sha1(uniqid(microtime())), 0, 8);
}
define('TMP_FILE_PREFIX', $prefix);
// try to find the temp folder
define('TMP_DIR', BasicFileUtil::getTempFolder());
/**
* Reads a file resource from temp folder.
*
* @param string $key
* @param string $directory
*/
function readFileResource($key, $directory) {
if (preg_match('~[\w\-]+\.(css|jpg|png|svg)~', $_GET[$key], $match)) {
switch ($match[1]) {
case 'css':
header('Content-Type: text/css');
break;
case 'jpg':
header('Content-Type: image/jpg');
break;
case 'png':
header('Content-Type: image/png');
break;
case 'svg':
header('Content-Type: image/svg+xml');
break;
}
header('Expires: '.gmdate('D, d M Y H:i:s', time() + 3600).' GMT');
header('Last-Modified: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: public, max-age=3600');
readfile($directory . $_GET[$key]);
}
exit;
}
// show image from temp folder
if (isset($_GET['showImage'])) {
readFileResource('showImage', TMP_DIR . 'install/files/acp/images/');
}
// show icon from temp folder
if (isset($_GET['showIcon'])) {
readFileResource('showIcon', TMP_DIR . 'install/files/icon/');
}
// show css from temp folder
if (isset($_GET['showCSS'])) {
readFileResource('showCSS', TMP_DIR . 'install/files/acp/style/setup/');
}
// check whether setup files are already unzipped
if (!file_exists(TMP_DIR . 'install/files/lib/system/WCFSetup.class.php')) {
// try to unzip all setup files into temp folder
$tar = new Tar(SETUP_FILE);
$contentList = $tar->getContentList();
if (!count($contentList)) {
throw new SystemException("Can not unpack 'WCFSetup.tar.gz'. File is probably broken.");
}
foreach ($contentList as $file) {
foreach ($neededFilesPattern as $pattern) {
if (preg_match($pattern, $file['filename'])) {
// create directory if not exists
$dir = TMP_DIR . dirname($file['filename']);
if (!@is_dir($dir)) {
@mkdir($dir, 0777, true);
@chmod($dir, 0777);
}
$tar->extract($file['index'], TMP_DIR . $file['filename']);
}
}
}
$tar->close();
// create cache folders
@mkdir(TMP_DIR . 'setup/lang/cache/', 0777);
@chmod(TMP_DIR . 'setup/lang/cache/', 0777);
@mkdir(TMP_DIR . 'setup/template/compiled/', 0777);
@chmod(TMP_DIR . 'setup/template/compiled/', 0777);
}
if (!class_exists('wcf\system\WCFSetup')) {
throw new SystemException("Can not find class 'WCFSetup'");
}
// start setup
new \wcf\system\WCFSetup();