3 use wcf\data\language\LanguageEditor
;
4 use wcf\data\language\SetupLanguage
;
5 use wcf\data\package\installation\queue\PackageInstallationQueueEditor
;
6 use wcf\data\package\Package
;
7 use wcf\data\user\User
;
8 use wcf\data\user\UserAction
;
9 use wcf\system\cache\builder\LanguageCacheBuilder
;
10 use wcf\system\database\util\SQLParser
;
11 use wcf\system\database\MySQLDatabase
;
12 use wcf\system\exception\SystemException
;
13 use wcf\system\exception\UserInputException
;
14 use wcf\system\io\File
;
15 use wcf\system\io\Tar
;
16 use wcf\system\language\LanguageFactory
;
17 use wcf\system\package\PackageArchive
;
18 use wcf\system\session\ACPSessionFactory
;
19 use wcf\system\session\SessionHandler
;
20 use wcf\system\setup\Installer
;
21 use wcf\system\template\SetupTemplateEngine
;
22 use wcf\util\DirectoryUtil
;
23 use wcf\util\FileUtil
;
24 use wcf\util\StringUtil
;
25 use wcf\util\UserUtil
;
29 define('PACKAGE_ID', 0);
30 define('HTTP_ENABLE_NO_CACHE_HEADERS', 0);
31 define('HTTP_ENABLE_GZIP', 0);
32 define('HTTP_GZIP_LEVEL', 0);
33 define('HTTP_SEND_X_FRAME_OPTIONS', 0);
34 define('CACHE_SOURCE_TYPE', 'disk');
35 define('MODULE_MASTER_PASSWORD', 1);
36 define('ENABLE_DEBUG_MODE', 1);
37 define('ENABLE_BENCHMARK', 0);
40 * Executes the installation of the basic WCF systems.
43 * @copyright 2001-2016 WoltLab GmbH
44 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
45 * @package WoltLabSuite\Core\System
47 class WCFSetup
extends WCF
{
49 * list of available languages
52 protected static $availableLanguages = [];
55 * installation directories
58 protected static $directories = [];
61 * language code of selected installation language
64 protected static $selectedLanguageCode = 'en';
67 * selected languages to be installed
70 protected static $selectedLanguages = [];
73 * list of installed files
76 protected static $installedFiles = [];
79 * indicates if developer mode is used to install
82 protected static $developerMode = 0;
84 /** @noinspection PhpMissingParentConstructorInspection */
86 * Calls all init functions of the WCFSetup class and starts the setup process.
88 public function __construct() {
91 $this->getDeveloperMode();
92 $this->getLanguageSelection();
93 $this->getInstallationDirectories();
94 $this->initLanguage();
96 /** @noinspection PhpUndefinedMethodInspection */
97 self
::getLanguage()->loadLanguage();
98 $this->getPackageNames();
105 * Gets the status of the developer mode.
107 protected static function getDeveloperMode() {
108 if (isset($_GET['dev'])) self
::$developerMode = intval($_GET['dev']);
109 else if (isset($_POST['dev'])) self
::$developerMode = intval($_POST['dev']);
113 * Gets the selected language.
115 protected static function getLanguageSelection() {
116 self
::$availableLanguages = self
::getAvailableLanguages();
118 if (isset($_REQUEST['languageCode']) && isset(self
::$availableLanguages[$_REQUEST['languageCode']])) {
119 self
::$selectedLanguageCode = $_REQUEST['languageCode'];
122 self
::$selectedLanguageCode = LanguageFactory
::getPreferredLanguage(array_keys(self
::$availableLanguages), self
::$selectedLanguageCode);
125 if (isset($_POST['selectedLanguages']) && is_array($_POST['selectedLanguages'])) {
126 self
::$selectedLanguages = $_POST['selectedLanguages'];
131 * Gets the selected wcf dir from request.
135 protected static function getInstallationDirectories() {
136 if (self
::$developerMode && isset($_ENV['WCFSETUP_USEDEFAULTWCFDIR'])) {
137 if (!isset($_REQUEST['directories']) ||
!is_array($_REQUEST['directories'])) $_REQUEST['directories'] = [];
138 $_REQUEST['directories']['wcf'] = FileUtil
::unifyDirSeparator(INSTALL_SCRIPT_DIR
).'wcf/';
141 if (!empty($_REQUEST['directories']) && is_array($_REQUEST['directories'])) {
142 foreach ($_REQUEST['directories'] as $application => $directory) {
143 self
::$directories[$application] = $directory;
145 if ($application === 'wcf' && @file_exists
(self
::$directories['wcf'])) {
146 define('RELATIVE_WCF_DIR', FileUtil
::getRelativePath(INSTALL_SCRIPT_DIR
, self
::$directories['wcf']));
151 define('WCF_DIR', (isset(self
::$directories['wcf']) ? self
::$directories['wcf'] : ''));
155 * Initialises the language engine.
157 protected function initLanguage() {
159 mb_internal_encoding('UTF-8');
160 if (function_exists('mb_regex_encoding')) mb_regex_encoding('UTF-8');
163 // init setup language
164 self
::$languageObj = new SetupLanguage(null, ['languageCode' => self
::$selectedLanguageCode]);
168 * Initialises the template engine.
170 protected function initTPL() {
171 self
::$tplObj = SetupTemplateEngine
::getInstance();
172 self
::getTPL()->setLanguageID((self
::$selectedLanguageCode == 'en' ?
0 : 1));
173 self
::getTPL()->setCompileDir(TMP_DIR
);
174 self
::getTPL()->addApplication('wcf', TMP_DIR
);
175 self
::getTPL()->registerPrefilter(['lang']);
176 self
::getTPL()->assign([
178 'tmpFilePrefix' => TMP_FILE_PREFIX
,
179 'languageCode' => self
::$selectedLanguageCode,
180 'selectedLanguages' => self
::$selectedLanguages,
181 'directories' => self
::$directories,
182 'developerMode' => self
::$developerMode
187 * Returns all languages from WCFSetup.tar.gz.
191 protected static function getAvailableLanguages() {
192 $languages = $match = [];
193 foreach (glob(TMP_DIR
.'setup/lang/*.xml') as $file) {
196 $languageCode = LanguageEditor
::readLanguageCodeFromXML($xml);
197 $languageName = LanguageEditor
::readLanguageNameFromXML($xml);
199 $languages[$languageCode] = $languageName;
202 // sort languages by language name
209 * Calculates the current state of the progress bar.
211 * @param integer $currentStep
213 protected function calcProgress($currentStep) {
214 // calculate progress
215 $progress = round((100 / 18) * ++
$currentStep, 0);
216 self
::getTPL()->assign(['progress' => $progress]);
220 * Executes the setup steps.
222 protected function setup() {
224 if (isset($_REQUEST['step'])) $step = $_REQUEST['step'];
225 else $step = 'selectSetupLanguage';
227 // execute current step
229 /** @noinspection PhpMissingBreakStatementInspection */
230 case 'selectSetupLanguage':
231 if (!self
::$developerMode) {
232 $this->calcProgress(0);
233 $this->selectSetupLanguage();
237 /** @noinspection PhpMissingBreakStatementInspection */
239 if (!self
::$developerMode) {
240 $this->calcProgress(1);
241 $this->showLicense();
245 /** @noinspection PhpMissingBreakStatementInspection */
246 case 'showSystemRequirements':
247 if (!self
::$developerMode) {
248 $this->calcProgress(2);
249 $this->showSystemRequirements();
253 /** @noinspection PhpMissingBreakStatementInspection */
254 case 'configureDirectories':
255 if (!self
::$developerMode ||
!isset($_ENV['WCFSETUP_USEDEFAULTWCFDIR'])) {
256 $this->calcProgress(3);
257 $this->configureDirectories();
262 $this->calcProgress(4);
266 case 'selectLanguages':
267 $this->calcProgress(5);
268 $this->selectLanguages();
272 $this->calcProgress(6);
273 $this->configureDB();
278 if (isset($_POST['offset'])) {
279 $currentStep +
= intval($_POST['offset']);
282 $this->calcProgress($currentStep);
287 $this->calcProgress(14);
291 case 'installLanguage':
292 $this->calcProgress(15);
293 $this->installLanguage();
297 $this->calcProgress(16);
301 case 'installPackages':
302 $this->calcProgress(17);
303 $this->installPackages();
309 * Shows the first setup page.
311 protected function selectSetupLanguage() {
312 WCF
::getTPL()->assign([
313 'availableLanguages' => self
::$availableLanguages,
314 'nextStep' => 'showLicense'
316 WCF
::getTPL()->display('stepSelectSetupLanguage');
320 * Shows the license agreement.
322 protected function showLicense() {
323 if (isset($_POST['send'])) {
324 if (isset($_POST['accepted'])) {
325 $this->gotoNextStep('showSystemRequirements');
329 WCF
::getTPL()->assign(['missingAcception' => true]);
334 if (file_exists(TMP_DIR
.'setup/license/license_'.self
::$selectedLanguageCode.'.txt')) {
335 $license = file_get_contents(TMP_DIR
.'setup/license/license_'.self
::$selectedLanguageCode.'.txt');
338 $license = file_get_contents(TMP_DIR
.'setup/license/license_en.txt');
341 WCF
::getTPL()->assign([
342 'license' => $license,
343 'nextStep' => 'showLicense'
345 WCF
::getTPL()->display('stepShowLicense');
349 * Shows the system requirements.
351 protected function showSystemRequirements() {
355 $system['phpVersion']['value'] = phpversion();
356 $comparePhpVersion = preg_replace('/^(\d+\.\d+\.\d+).*$/', '\\1', $system['phpVersion']['value']);
357 $system['phpVersion']['result'] = (version_compare($comparePhpVersion, '5.5.4') >= 0);
360 $system['sql']['result'] = MySQLDatabase
::isSupported();
362 // upload_max_filesize
363 $system['uploadMaxFilesize']['value'] = min(ini_get('upload_max_filesize'), ini_get('post_max_size'));
364 $system['uploadMaxFilesize']['result'] = (intval($system['uploadMaxFilesize']['value']) > 0);
367 $system['gdLib']['value'] = '0.0.0';
368 if (function_exists('gd_info')) {
371 if (preg_match('!([0-9]+\.[0-9]+(?:\.[0-9]+)?)!', $temp['GD Version'], $match)) {
372 if (preg_match('/^[0-9]+\.[0-9]+$/', $match[1])) $match[1] .= '.0';
373 $system['gdLib']['value'] = $match[1];
376 $system['gdLib']['result'] = (version_compare($system['gdLib']['value'], '2.0.0') >= 0);
379 $system['memoryLimit']['value'] = ini_get('memory_limit');
380 $system['memoryLimit']['result'] = $this->compareMemoryLimit();
382 WCF
::getTPL()->assign([
384 'nextStep' => 'configureDirectories'
386 WCF
::getTPL()->display('stepShowSystemRequirements');
390 * Returns true if memory_limit is set to at least 128 MB
394 protected function compareMemoryLimit() {
395 $memoryLimit = ini_get('memory_limit');
398 if ($memoryLimit == -1) {
402 // completely numeric, PHP assumes byte
403 if (is_numeric($memoryLimit)) {
404 $memoryLimit = $memoryLimit / 1024 / 1024;
405 return ($memoryLimit >= 128);
408 // PHP supports 'K', 'M' and 'G' shorthand notation
409 if (preg_match('~^(\d+)([KMG])$~', $memoryLimit, $matches)) {
410 switch ($matches[2]) {
412 $memoryLimit = $matches[1] * 1024;
413 return ($memoryLimit >= 128);
417 return ($matches[1] >= 128);
421 return ($matches[1] >= 1);
430 * Searches the wcf dir.
434 protected function configureDirectories() {
435 // get available packages
436 $applications = $packages = [];
437 foreach (glob(TMP_DIR
. 'install/packages/*') as $file) {
438 $filename = basename($file);
439 if (preg_match('~\.(?:tar|tar\.gz|tgz)$~', $filename)) {
440 $package = new PackageArchive($file);
441 $package->openArchive();
443 $application = Package
::getAbbreviation($package->getPackageInfo('name'));
445 $applications[] = $application;
446 $packages[$application] = [
447 'directory' => ($package->getPackageInfo('applicationDirectory') ?
: $application),
448 'packageDescription' => $package->getLocalizedPackageInfo('packageDescription'),
449 'packageName' => $package->getLocalizedPackageInfo('packageName')
455 uasort($packages, function($a, $b) {
456 return strcmp($a['packageName'], $b['packageName']);
459 // force cms being shown first
460 $showOrder = ['wcf'];
461 foreach (array_keys($packages) as $application) {
462 if ($application !== 'wcf') $showOrder[] = $application;
465 $documentRoot = FileUtil
::unifyDirSeparator($_SERVER['DOCUMENT_ROOT']);
467 if (!empty(self
::$directories)) {
468 $applicationPaths = $knownPaths = [];
470 // use $showOrder instead of $applications to ensure that the error message for
471 // duplicate directories will trigger in display order rather than the random
472 // sort order returned by glob() above
473 foreach ($showOrder as $application) {
474 $path = FileUtil
::getRealPath($documentRoot . '/' . FileUtil
::addTrailingSlash(FileUtil
::removeLeadingSlash(self
::$directories[$application])));
475 if (strpos($path, $documentRoot) !== 0) {
476 // verify that given path is still within the current document root
477 $errors[$application] = 'outsideDocumentRoot';
479 else if (in_array($path, $knownPaths)) {
480 // prevent the same path for two or more applications
481 $errors[$application] = 'duplicate';
483 else if (@is_file
($path . 'global.php')) {
484 // check if directory is empty (dotfiles are okay)
485 $errors[$application] = 'notEmpty';
488 // try to create directory if it does not exist
489 if (!is_dir($path) && !FileUtil
::makePath($path)) {
490 $errors[$application] = 'makePath';
494 FileUtil
::makeWritable($path);
496 catch (SystemException
$e) {
497 $errors[$application] = 'makeWritable';
501 $applicationPaths[$application] = $path;
502 $knownPaths[] = $path;
505 if (empty($errors)) {
506 // copy over the actual paths
507 self
::$directories = array_merge(self
::$directories, $applicationPaths);
508 WCF
::getTPL()->assign(['directories' => self
::$directories]);
515 // resolve path relative to document root
516 $relativePath = str_replace(FileUtil
::unifyDirSeparator($_SERVER['DOCUMENT_ROOT']), '', FileUtil
::unifyDirSeparator(INSTALL_SCRIPT_DIR
));
517 foreach ($packages as $application => $packageData) {
518 self
::$directories[$application] = $relativePath . ($application === 'wcf' ?
'' : $packageData['directory'] . '/');
522 WCF
::getTPL()->assign([
523 'directories' => self
::$directories,
524 'documentRoot' => $documentRoot,
526 'installScriptDir' => FileUtil
::unifyDirSeparator(INSTALL_SCRIPT_DIR
),
527 'nextStep' => 'configureDirectories', // call this step again to validate paths
528 'packages' => $packages,
529 'showOrder' => $showOrder
532 WCF
::getTPL()->display('stepConfigureDirectories');
536 * Unzips the files of the wcfsetup tar archive.
538 protected function unzipFiles() {
539 // WCF seems to be installed, abort
540 if (@is_file
(self
::$directories['wcf'].'lib/system/WCF.class.php')) {
541 throw new SystemException('Target directory seems to be an existing installation of WCF, unable to continue.');
543 // WCF not yet installed, install files first
545 $this->installFiles();
547 $this->gotoNextStep('selectLanguages');
552 * Shows the page for choosing the installed languages.
554 protected function selectLanguages() {
555 $errorField = $errorType = '';
557 // skip step in developer mode
558 // select all available languages automatically
559 if (self
::$developerMode) {
560 self
::$selectedLanguages = [];
561 foreach (self
::$availableLanguages as $languageCode => $language) {
562 self
::$selectedLanguages[] = $languageCode;
565 self
::getTPL()->assign(['selectedLanguages' => self
::$selectedLanguages]);
566 $this->gotoNextStep('configureDB');
570 // start error handling
571 if (isset($_POST['send'])) {
573 // no languages selected
574 if (empty(self
::$selectedLanguages)) {
575 throw new UserInputException('selectedLanguages');
579 foreach (self
::$selectedLanguages as $language) {
580 if (!isset(self
::$availableLanguages[$language])) {
581 throw new UserInputException('selectedLanguages');
587 $this->gotoNextStep('configureDB');
590 catch (UserInputException
$e) {
591 $errorField = $e->getField();
592 $errorType = $e->getType();
596 self
::$selectedLanguages[] = self
::$selectedLanguageCode;
597 WCF
::getTPL()->assign(['selectedLanguages' => self
::$selectedLanguages]);
600 WCF
::getTPL()->assign([
601 'errorField' => $errorField,
602 'errorType' => $errorType,
603 'availableLanguages' => self
::$availableLanguages,
604 'nextStep' => 'selectLanguages'
606 WCF
::getTPL()->display('stepSelectLanguages');
610 * Shows the page for configuring the database connection.
612 protected function configureDB() {
613 if (self
::$developerMode && isset($_ENV['WCFSETUP_DBHOST'])) {
614 $dbHost = $_ENV['WCFSETUP_DBHOST'];
615 $dbUser = $_ENV['WCFSETUP_DBUSER'];
616 $dbPassword = $_ENV['WCFSETUP_DBPASSWORD'];
617 $dbName = $_ENV['WCFSETUP_DBNAME'];
621 $dbHost = 'localhost';
628 if (isset($_POST['send']) ||
(self
::$developerMode && isset($_ENV['WCFSETUP_DBHOST']))) {
629 if (isset($_POST['dbHost'])) $dbHost = $_POST['dbHost'];
630 if (isset($_POST['dbUser'])) $dbUser = $_POST['dbUser'];
631 if (isset($_POST['dbPassword'])) $dbPassword = $_POST['dbPassword'];
632 if (isset($_POST['dbName'])) $dbName = $_POST['dbName'];
634 // ensure that $dbNumber is zero or a positive integer
635 if (isset($_POST['dbNumber'])) $dbNumber = max(0, intval($_POST['dbNumber']));
639 if (preg_match('/^(.+?):(\d+)$/', $dbHost, $match)) {
641 $dbPort = intval($match[2]);
646 // check connection data
647 /** @var \wcf\system\database\Database $db */
648 $db = new MySQLDatabase($dbHost, $dbUser, $dbPassword, $dbName, $dbPort, true);
652 $sqlVersion = $db->getVersion();
653 $compareSQLVersion = preg_replace('/^(\d+\.\d+\.\d+).*$/', '\\1', $sqlVersion);
654 if (stripos($sqlVersion, 'MariaDB')) {
656 if (!(version_compare($compareSQLVersion, '10.0.22') >= 0)) {
657 throw new SystemException("Insufficient MariaDB version '".$compareSQLVersion."'. Version '10.0.22' or greater is needed.");
662 if (!(version_compare($compareSQLVersion, '5.5.35') >= 0)) {
663 throw new SystemException("Insufficient MySQL version '".$compareSQLVersion."'. Version '5.5.35' or greater is needed.");
667 // check innodb support
668 $sql = "SHOW ENGINES";
669 $statement = $db->prepareStatement($sql);
670 $statement->execute();
672 while ($row = $statement->fetchArray()) {
673 if ($row['Engine'] == 'InnoDB' && in_array($row['Support'], ['DEFAULT', 'YES'])) {
680 throw new SystemException("Support for InnoDB is missing.");
683 // check for table conflicts
684 $conflictedTables = $this->getConflictedTables($db, $dbNumber);
687 if (empty($conflictedTables)) {
688 // connection successfully established
689 // write configuration to config.inc.php
690 $file = new File(WCF_DIR
.'config.inc.php');
691 $file->write("<?php\n");
692 $file->write("\$dbHost = '".str_replace("'", "\\'", $dbHost)."';\n");
693 $file->write("\$dbPort = ".$dbPort.";\n");
694 $file->write("\$dbUser = '".str_replace("'", "\\'", $dbUser)."';\n");
695 $file->write("\$dbPassword = '".str_replace("'", "\\'", $dbPassword)."';\n");
696 $file->write("\$dbName = '".str_replace("'", "\\'", $dbName)."';\n");
697 $file->write("if (!defined('WCF_N')) define('WCF_N', $dbNumber);\n");
701 $this->gotoNextStep('createDB');
704 // show configure template again
706 WCF
::getTPL()->assign(['conflictedTables' => $conflictedTables]);
709 catch (SystemException
$e) {
710 WCF
::getTPL()->assign(['exception' => $e]);
713 WCF
::getTPL()->assign([
716 'dbPassword' => $dbPassword,
718 'dbNumber' => $dbNumber,
719 'nextStep' => 'configureDB'
721 WCF
::getTPL()->display('stepConfigureDB');
725 * Checks if in the chosen database are tables in conflict with the wcf tables
726 * which will be created in the next step.
728 * @param \wcf\system\database\Database $db
729 * @param integer $dbNumber
730 * @return string[] list of already existing tables
732 protected function getConflictedTables($db, $dbNumber) {
733 // get content of the sql structure file
734 $sql = file_get_contents(TMP_DIR
.'setup/db/install.sql');
736 // installation number value 'n' (WCF_N) must be reflected in the executed sql queries
737 $sql = str_replace('wcf1_', 'wcf'.$dbNumber.'_', $sql);
739 // get all tablenames which should be created
740 preg_match_all("%CREATE\s+TABLE\s+(\w+)%", $sql, $matches);
742 // get all installed tables from chosen database
743 $existingTables = $db->getEditor()->getTableNames();
745 // check if existing tables are in conflict with wcf tables
746 $conflictedTables = [];
747 foreach ($existingTables as $existingTableName) {
748 foreach ($matches[1] as $wcfTableName) {
749 if ($existingTableName == $wcfTableName) {
750 $conflictedTables[] = $wcfTableName;
754 return $conflictedTables;
758 * Creates the database structure of the wcf.
760 protected function createDB() {
763 // get content of the sql structure file
764 $sql = file_get_contents(TMP_DIR
.'setup/db/install.sql');
767 $sqlData = explode('/* SQL_PARSER_OFFSET */', $sql);
768 $offset = (isset($_POST['offset'])) ?
intval($_POST['offset']) : 0;
769 if (!isset($sqlData[$offset])) {
770 throw new SystemException("Offset for SQL parser is out of bounds, ".$offset." was requested, but there are only ".count($sqlData)." sections");
772 $sql = $sqlData[$offset];
774 // installation number value 'n' (WCF_N) must be reflected in the executed sql queries
775 $sql = str_replace('wcf1_', 'wcf'.WCF_N
.'_', $sql);
777 // execute sql queries
778 $parser = new SQLParser($sql);
782 preg_match_all("~CREATE\s+TABLE\s+(\w+)~i", $sql, $matches);
784 if (!empty($matches[1])) {
785 $sql = "INSERT INTO wcf".WCF_N
."_package_installation_sql_log
788 $statement = self
::getDB()->prepareStatement($sql);
789 foreach ($matches[1] as $tableName) {
790 $statement->execute([$tableName]);
794 if ($offset < (count($sqlData) - 1)) {
795 WCF
::getTPL()->assign([
796 '__additionalParameters' => [
797 'offset' => $offset +
1
801 $this->gotoNextStep('createDB');
805 * Manually install PIPPackageInstallationPlugin since install.sql content is not escaped resulting
806 * in different behaviour in MySQL and MSSQL. You SHOULD NOT move this into install.sql!
808 $sql = "INSERT INTO wcf".WCF_N
."_package_installation_plugin
809 (pluginName, priority, className)
811 $statement = self
::getDB()->prepareStatement($sql);
812 $statement->execute([
813 'packageInstallationPlugin',
815 'wcf\system\package\plugin\PIPPackageInstallationPlugin'
818 $this->gotoNextStep('logFiles');
823 * Logs the unzipped files.
825 protected function logFiles() {
828 $this->getInstalledFiles(WCF_DIR
);
829 $acpTemplateInserts = $fileInserts = [];
830 foreach (self
::$installedFiles as $file) {
832 if (preg_match('!/acp/templates/([^/]+)\.tpl$!', $file, $match)) {
834 $acpTemplateInserts[] = $match[1];
838 $fileInserts[] = str_replace(WCF_DIR
, '', $file);
842 // save acp template log
843 if (!empty($acpTemplateInserts)) {
844 $sql = "INSERT INTO wcf".WCF_N
."_acp_template
845 (templateName, application)
847 $statement = self
::getDB()->prepareStatement($sql);
849 self
::getDB()->beginTransaction();
850 foreach ($acpTemplateInserts as $acpTemplate) {
851 $statement->execute([$acpTemplate, 'wcf']);
853 self
::getDB()->commitTransaction();
857 if (!empty($fileInserts)) {
858 $sql = "INSERT INTO wcf".WCF_N
."_package_installation_file_log
859 (filename, application)
861 $statement = self
::getDB()->prepareStatement($sql);
863 self
::getDB()->beginTransaction();
864 foreach ($fileInserts as $file) {
865 $statement->execute([$file, 'wcf']);
867 self
::getDB()->commitTransaction();
870 $this->gotoNextStep('installLanguage');
874 * Scans the given dir for installed files.
878 protected function getInstalledFiles($dir) {
879 if ($files = glob($dir.'*')) {
880 foreach ($files as $file) {
882 $this->getInstalledFiles(FileUtil
::addTrailingSlash($file));
885 self
::$installedFiles[] = FileUtil
::unifyDirSeparator($file);
892 * Installs the selected languages.
894 protected function installLanguage() {
897 foreach (self
::$selectedLanguages as $language) {
898 // get language.xml file name
899 $filename = TMP_DIR
.'install/lang/'.$language.'.xml';
902 if (!file_exists($filename)) {
903 throw new SystemException("unable to find language file '".$filename."'");
908 $xml->load($filename);
911 LanguageEditor
::importFromXML($xml, 0);
914 // set default language
915 $language = LanguageFactory
::getInstance()->getLanguageByCode(in_array(self
::$selectedLanguageCode, self
::$selectedLanguages) ? self
::$selectedLanguageCode : self
::$selectedLanguages[0]);
916 LanguageFactory
::getInstance()->makeDefault($language->languageID
);
918 // rebuild language cache
919 LanguageCacheBuilder
::getInstance()->reset();
922 $this->gotoNextStep('createUser');
926 * Shows the page for creating the admin account.
928 protected function createUser() {
929 $errorType = $errorField = $username = $email = $confirmEmail = $password = $confirmPassword = '';
932 $email = $confirmEmail = '';
933 $password = $confirmPassword = '';
935 if (isset($_POST['send']) || self
::$developerMode) {
936 if (isset($_POST['send'])) {
937 if (isset($_POST['username'])) $username = StringUtil
::trim($_POST['username']);
938 if (isset($_POST['email'])) $email = StringUtil
::trim($_POST['email']);
939 if (isset($_POST['confirmEmail'])) $confirmEmail = StringUtil
::trim($_POST['confirmEmail']);
940 if (isset($_POST['password'])) $password = $_POST['password'];
941 if (isset($_POST['confirmPassword'])) $confirmPassword = $_POST['confirmPassword'];
944 $username = $password = $confirmPassword = 'root';
945 $email = $confirmEmail = 'wsc-developer-mode@example.com';
951 if (empty($username)) {
952 throw new UserInputException('username');
954 if (!UserUtil
::isValidUsername($username)) {
955 throw new UserInputException('username', 'notValid');
960 throw new UserInputException('email');
962 if (!UserUtil
::isValidEmail($email)) {
963 throw new UserInputException('email', 'notValid');
966 // confirm e-mail address
967 if ($email != $confirmEmail) {
968 throw new UserInputException('confirmEmail', 'notEqual');
972 if (empty($password)) {
973 throw new UserInputException('password');
976 // confirm e-mail address
977 if ($password != $confirmPassword) {
978 throw new UserInputException('confirmPassword', 'notEqual');
982 // init database connection
987 $sql = "SELECT languageID
988 FROM wcf".WCF_N
."_language
989 WHERE languageCode = ?";
990 $statement = self
::getDB()->prepareStatement($sql);
991 $statement->execute([self
::$selectedLanguageCode]);
992 $row = $statement->fetchArray();
993 if (isset($row['languageID'])) $languageID = $row['languageID'];
996 $languageID = LanguageFactory
::getInstance()->getDefaultLanguageID();
1003 'languageID' => $languageID,
1004 'password' => $password,
1005 'username' => $username
1017 $userAction = new UserAction([], 'create', $data);
1018 $userAction->executeAction();
1021 $this->gotoNextStep('installPackages');
1024 catch (UserInputException
$e) {
1025 $errorField = $e->getField();
1026 $errorType = $e->getType();
1030 WCF
::getTPL()->assign([
1031 'errorField' => $errorField,
1032 'errorType' => $errorType,
1033 'username' => $username,
1035 'confirmEmail' => $confirmEmail,
1036 'password' => $password,
1037 'confirmPassword' => $confirmPassword,
1038 'nextStep' => 'createUser'
1040 WCF
::getTPL()->display('stepCreateUser');
1044 * Registers with wcf setup delivered packages in the package installation queue.
1046 protected function installPackages() {
1047 // init database connection
1050 // get admin account
1051 $admin = new User(1);
1053 // get delivered packages
1054 $wcfPackageFile = '';
1055 $otherPackages = [];
1056 $tar = new Tar(SETUP_FILE
);
1057 foreach ($tar->getContentList() as $file) {
1058 if ($file['type'] != 'folder' && mb_strpos($file['filename'], 'install/packages/') === 0) {
1059 $packageFile = basename($file['filename']);
1061 // ignore any files which aren't an archive
1062 if (preg_match('~\.(tar\.gz|tgz|tar)$~', $packageFile)) {
1063 $packageName = preg_replace('!\.(tar\.gz|tgz|tar)$!', '', $packageFile);
1065 if ($packageName == 'com.woltlab.wcf') {
1066 $wcfPackageFile = $packageFile;
1069 $isStrato = (!empty($_SERVER['DOCUMENT_ROOT']) && (strpos($_SERVER['DOCUMENT_ROOT'], 'strato') !== false));
1070 if (!$isStrato && preg_match('!\.(tar\.gz|tgz)$!', $packageFile)) {
1071 // try to unzip zipped package files
1072 if (FileUtil
::uncompressFile(TMP_DIR
.'install/packages/'.$packageFile, TMP_DIR
.'install/packages/'.$packageName.'.tar')) {
1073 @unlink
(TMP_DIR
.'install/packages/'.$packageFile);
1074 $packageFile = $packageName.'.tar';
1078 $otherPackages[$packageName] = $packageFile;
1085 // register packages in queue
1086 // get new process id
1087 $sql = "SELECT MAX(processNo) AS processNo
1088 FROM wcf".WCF_N
."_package_installation_queue";
1089 $statement = self
::getDB()->prepareStatement($sql);
1090 $statement->execute();
1091 $result = $statement->fetchArray();
1092 $processNo = intval($result['processNo']) +
1;
1094 // search existing wcf package
1095 $sql = "SELECT COUNT(*) AS count
1096 FROM wcf".WCF_N
."_package
1097 WHERE package = 'com.woltlab.wcf'";
1098 $statement = self
::getDB()->prepareStatement($sql);
1099 $statement->execute();
1100 if (!$statement->fetchSingleColumn()) {
1101 if (empty($wcfPackageFile)) {
1102 throw new SystemException('the essential package com.woltlab.wcf is missing.');
1105 // register essential wcf package
1106 $queue = PackageInstallationQueueEditor
::create([
1107 'processNo' => $processNo,
1108 'userID' => $admin->userID
,
1109 'package' => 'com.woltlab.wcf',
1110 'packageName' => 'WoltLab Suite Core',
1111 'archive' => TMP_DIR
.'install/packages/'.$wcfPackageFile,
1112 'isApplication' => 1
1116 // register all other delivered packages
1117 asort($otherPackages);
1118 foreach ($otherPackages as $packageName => $packageFile) {
1119 // extract packageName from archive's package.xml
1120 $archive = new PackageArchive(TMP_DIR
.'install/packages/'.$packageFile);
1122 $archive->openArchive();
1124 catch (\Exception
$e) {
1125 // we've encountered a broken archive, revert everything and then fail
1126 $sql = "SELECT queueID, parentQueueID
1127 FROM wcf".WCF_N
."_package_installation_queue";
1128 $statement = WCF
::getDB()->prepareStatement($sql);
1129 $statement->execute();
1131 while ($row = $statement->fetchArray()) {
1132 $queues[$row['queueID']] = $row['parentQueueID'];
1136 /** @noinspection PhpUndefinedVariableInspection */
1137 $queueID = $queue->queueID
;
1139 $queueIDs[] = $queueID;
1141 $queueID = (isset($queues[$queueID])) ?
$queues[$queueID] : 0;
1144 // remove previously created queues
1145 if (!empty($queueIDs)) {
1146 $sql = "DELETE FROM wcf".WCF_N
."_package_installation_queue
1148 $statement = WCF
::getDB()->prepareStatement($sql);
1149 WCF
::getDB()->beginTransaction();
1150 foreach ($queueIDs as $queueID) {
1151 $statement->execute([$queueID]);
1153 WCF
::getDB()->commitTransaction();
1156 // remove package files
1157 @unlink
(TMP_DIR
.'install/packages/'.$wcfPackageFile);
1158 foreach ($otherPackages as $otherPackageFile) {
1159 @unlink
(TMP_DIR
.'install/packages/'.$otherPackageFile);
1162 // throw exception again
1163 throw new SystemException('', 0, '', $e);
1166 /** @noinspection PhpUndefinedVariableInspection */
1167 $queue = PackageInstallationQueueEditor
::create([
1168 'parentQueueID' => $queue->queueID
,
1169 'processNo' => $processNo,
1170 'userID' => $admin->userID
,
1171 'package' => $packageName,
1172 'packageName' => $archive->getLocalizedPackageInfo('packageName'),
1173 'archive' => TMP_DIR
.'install/packages/'.$packageFile,
1174 'isApplication' => 1
1179 define('COOKIE_PREFIX', 'wcf22_');
1181 $factory = new ACPSessionFactory();
1184 SessionHandler
::getInstance()->changeUser($admin);
1185 SessionHandler
::getInstance()->register('masterPassword', 1);
1186 SessionHandler
::getInstance()->register('__wcfSetup_developerMode', self
::$developerMode);
1187 SessionHandler
::getInstance()->register('__wcfSetup_directories', self
::$directories);
1188 SessionHandler
::getInstance()->update();
1190 $installPhpDeleted = @unlink
('./install.php');
1191 @unlink
('./test.php');
1192 $wcfSetupTarDeleted = @unlink
('./WCFSetup.tar.gz');
1195 WCF
::getTPL()->assign([
1196 'installPhpDeleted' => $installPhpDeleted,
1197 'wcfSetupTarDeleted' => $wcfSetupTarDeleted
1199 WCF
::getTPL()->display('stepInstallPackages');
1202 $directory = TMP_DIR
.'/';
1203 DirectoryUtil
::getInstance($directory)->removePattern(new Regex('\.tar(\.gz)?$'), true);
1207 * Goes to the next step.
1209 * @param string $nextStep
1211 protected function gotoNextStep($nextStep) {
1212 WCF
::getTPL()->assign(['nextStep' => $nextStep]);
1213 WCF
::getTPL()->display('stepNext');
1217 * Installs the files of the tar archive.
1219 protected static function installFiles() {
1220 new Installer(self
::$directories['wcf'], SETUP_FILE
, null, 'install/files/');
1224 * Gets the package names of the bundled applications in WCFSetup.tar.gz.
1226 protected static function getPackageNames() {
1229 $tar = new Tar(SETUP_FILE
);
1230 foreach ($tar->getContentList() as $file) {
1231 if ($file['type'] != 'folder' && mb_strpos($file['filename'], 'install/packages/') === 0) {
1232 $packageFile = basename($file['filename']);
1235 $archive = new PackageArchive(TMP_DIR
.'install/packages/'.$packageFile);
1236 $archive->openArchive();
1237 $packageNames[] = $archive->getLocalizedPackageInfo('packageName');
1238 $archive->getTar()->close();
1240 catch (SystemException
$e) {}
1245 sort($packageNames);
1247 // assign package name
1248 WCF
::getTPL()->assign(['setupPackageNames' => $packageNames]);