2 namespace wcf\system\package
;
3 use wcf\data\application\Application
;
4 use wcf\data\application\ApplicationEditor
;
5 use wcf\data\language\category\LanguageCategory
;
6 use wcf\data\language\LanguageEditor
;
7 use wcf\data\language\LanguageList
;
8 use wcf\data\option\OptionEditor
;
9 use wcf\data\package\installation\queue\PackageInstallationQueue
;
10 use wcf\data\package\installation\queue\PackageInstallationQueueEditor
;
11 use wcf\data\package\Package
;
12 use wcf\data\package\PackageEditor
;
13 use wcf\system\application\ApplicationHandler
;
14 use wcf\system\cache\CacheHandler
;
15 use wcf\system\database\statement\PreparedStatement
;
16 use wcf\system\database\util\PreparedStatementConditionBuilder
;
17 use wcf\system\exception\SystemException
;
18 use wcf\system\form\container
;
19 use wcf\system\form\element
;
20 use wcf\system\form\FormDocument
;
22 use wcf\system\language\LanguageFactory
;
23 use wcf\system\request\LinkHandler
;
24 use wcf\system\request\RouteHandler
;
26 use wcf\util\FileUtil
;
27 use wcf\util\HeaderUtil
;
28 use wcf\util\StringUtil
;
31 * PackageInstallationDispatcher handles the whole installation process.
33 * @author Alexander Ebert
34 * @copyright 2001-2012 WoltLab GmbH
35 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
36 * @package com.woltlab.wcf
37 * @subpackage system.package
38 * @category Community Framework
40 class PackageInstallationDispatcher
{
42 * current installation type
45 protected $action = '';
48 * instance of PackageArchive
49 * @var wcf\system\package\PackageArchive
51 public $archive = null;
54 * instance of PackageInstallationNodeBuilder
55 * @var wcf\system\package\PackageInstallationNodeBuilder
57 public $nodeBuilder = null;
61 * @var wcf\data\package\Package
63 public $package = null;
66 * instance of PackageInstallationQueue
67 * @var wcf\system\package\PackageInstallationQueue
72 * default name of the config file
75 const CONFIG_FILE
= 'config.inc.php';
78 * Creates a new instance of PackageInstallationDispatcher.
80 * @param wcf\data\package\installation\queue\PackageInstallationQueue $queue
82 public function __construct(PackageInstallationQueue
$queue) {
83 $this->queue
= $queue;
84 $this->nodeBuilder
= new PackageInstallationNodeBuilder($this);
86 $this->action
= $this->queue
->action
;
90 * Installs node components and returns next node.
93 * @return wcf\system\package\PackageInstallationStep
95 public function install($node) {
96 $nodes = $this->nodeBuilder
->getNodeData($node);
98 // invoke node-specific actions
99 foreach ($nodes as $data) {
100 $nodeData = unserialize($data['nodeData']);
102 switch ($data['nodeType']) {
104 $step = $this->installPackage($nodeData);
108 $step = $this->executePIP($nodeData);
111 case 'optionalPackages':
112 $step = $this->selectOptionalPackages($node, $nodeData);
116 die("Unknown node type: '".$data['nodeType']."'");
120 if ($step->splitNode()) {
121 $this->nodeBuilder
->cloneNode($node, $data['sequenceNo']);
126 // mark node as completed
127 $this->nodeBuilder
->completeNode($node);
130 $node = $this->nodeBuilder
->getNextNode($node);
131 $step->setNode($node);
133 // update options.inc.php and save localized package infos
135 OptionEditor
::resetCache();
137 if ($this->action
== 'install') {
138 $this->saveLocalizedPackageInfos();
140 // remove all cache files after WCFSetup
142 CacheHandler
::getInstance()->clear(WCF_DIR
.'cache/', 'cache.*.php');
145 // rebuild application paths
146 ApplicationHandler
::rebuild();
147 ApplicationEditor
::setup();
155 * Returns current package archive.
157 * @return wcf\system\package\PackageArchive
159 public function getArchive() {
160 if ($this->archive
=== null) {
161 $this->archive
= new PackageArchive($this->queue
->archive
, $this->getPackage());
163 if (FileUtil
::isURL($this->archive
->getArchive())) {
164 // get return value and update entry in
165 // package_installation_queue with this value
166 $archive = $this->archive
->downloadArchive();
167 $queueEditor = new PackageInstallationQueueEditor($this->queue
);
168 $queueEditor->update(array(
169 'archive' => $archive
173 $this->archive
->openArchive();
176 return $this->archive
;
180 * Installs current package.
182 * @param array $nodeData
184 protected function installPackage(array $nodeData) {
185 $installationStep = new PackageInstallationStep();
187 // check requirements
188 if (!empty($nodeData['requirements'])) {
189 foreach ($nodeData['requirements'] as $package => $requirementData) {
190 // get existing package
191 if ($requirementData['packageID']) {
192 $sql = "SELECT packageName, packageVersion
193 FROM wcf".WCF_N
."_package
194 WHERE packageID = ?";
195 $statement = WCF
::getDB()->prepareStatement($sql);
196 $statement->execute(array($requirementData['packageID']));
199 // try to find matching package
200 $sql = "SELECT packageName, packageVersion
201 FROM wcf".WCF_N
."_package
203 $statement = WCF
::getDB()->prepareStatement($sql);
204 $statement->execute(array($package));
206 $row = $statement->fetchArray();
208 // package is required but not available
209 if ($row === false) {
210 throw new SystemException("Package '".$package."' is required by '".$nodeData['packageName']."', but is neither installed nor shipped.");
213 // check version requirements
214 if ($requirementData['minVersion']) {
215 if (Package
::compareVersion($row['packageVersion'], $requirementData['minVersion']) < 0) {
216 throw new SystemException("Package '".$nodeData['packageName']."' requires the package '".$row['packageName']."' in version '".$requirementData['minVersion']."', but version '".$row['packageVersion']."'");
221 unset($nodeData['requirements']);
223 if (!$this->queue
->packageID
) {
224 // create package entry
225 $package = PackageEditor
::create($nodeData);
227 // update package id for current queue
228 $queueEditor = new PackageInstallationQueueEditor($this->queue
);
229 $queueEditor->update(array(
230 'packageID' => $package->packageID
233 // save excluded packages
234 if (count($this->getArchive()->getExcludedPackages()) > 0) {
235 $sql = "INSERT INTO wcf".WCF_N
."_package_exclusion
236 (packageID, excludedPackage, excludedPackageVersion)
238 $statement = WCF
::getDB()->prepareStatement($sql);
240 foreach ($this->getArchive()->getExcludedPackages() as $excludedPackage) {
241 $statement->execute(array($package->packageID
, $excludedPackage['name'], (!empty($excludedPackage['version']) ?
$excludedPackage['version'] : '')));
245 // if package is plugin to com.woltlab.wcf it must not have any other requirement
246 $requirements = $this->getArchive()->getRequirements();
248 // insert requirements and dependencies
249 $requirements = $this->getArchive()->getAllExistingRequirements();
250 if (!empty($requirements)) {
251 $sql = "INSERT INTO wcf".WCF_N
."_package_requirement
252 (packageID, requirement)
254 $statement = WCF
::getDB()->prepareStatement($sql);
256 foreach ($requirements as $identifier => $possibleRequirements) {
257 if (count($possibleRequirements) == 1) {
258 $requirement = array_shift($possibleRequirements);
261 $requirement = $possibleRequirements[$this->selectedRequirements
[$identifier]];
264 $statement->execute(array($package->packageID
, $requirement['packageID']));
268 // build requirement map
269 Package
::rebuildPackageRequirementMap($package->packageID
);
272 $this->queue
= new PackageInstallationQueue($this->queue
->queueID
);
273 $this->package
= null;
275 if ($package->isApplication
) {
276 $host = StringUtil
::replace(RouteHandler
::getProtocol(), '', RouteHandler
::getHost());
277 $path = RouteHandler
::getPath(array('acp'));
279 // insert as application
280 ApplicationEditor
::create(array(
281 'domainName' => $host,
282 'domainPath' => $path,
283 'cookieDomain' => $host,
284 'cookiePath' => $path,
285 'packageID' => $package->packageID
290 if ($this->getPackage()->isApplication
&& $this->getPackage()->package
!= 'com.woltlab.wcf' && $this->getAction() == 'install') {
291 if (empty($this->getPackage()->packageDir
)) {
292 $document = $this->promptPackageDir();
293 if ($document !== null && $document instanceof form\FormDocument
) {
294 $installationStep->setDocument($document);
297 $installationStep->setSplitNode();
301 return $installationStep;
305 * Saves the localized package infos.
307 * @todo license and readme
309 protected function saveLocalizedPackageInfos() {
310 $package = new Package($this->queue
->packageID
);
312 // localize package information
313 $sql = "INSERT INTO wcf".WCF_N
."_language_item
314 (languageID, languageItem, languageItemValue, languageCategoryID, packageID)
315 VALUES (?, ?, ?, ?, ?)";
316 $statement = WCF
::getDB()->prepareStatement($sql);
319 $languageList = new LanguageList();
320 $languageList->sqlLimit
= 0;
321 $languageList->readObjects();
323 // workaround for WCFSetup
326 FROM wcf".WCF_N
."_language_category
327 WHERE languageCategory = ?";
328 $statement2 = WCF
::getDB()->prepareStatement($sql);
329 $statement2->execute(array('wcf.acp.package'));
330 $languageCategory = $statement2->fetchObject('wcf\data\language\category\LanguageCategory');
333 $languageCategory = LanguageFactory
::getInstance()->getCategory('wcf.acp.package');
337 $this->saveLocalizedPackageInfo($statement, $languageList, $languageCategory, $package, 'packageName');
339 // save package description
340 $this->saveLocalizedPackageInfo($statement, $languageList, $languageCategory, $package, 'packageDescription');
342 // update description and name
343 $packageEditor = new PackageEditor($package);
344 $packageEditor->update(array(
345 'packageDescription' => 'wcf.acp.package.packageDescription.package'.$this->queue
->packageID
,
346 'packageName' => 'wcf.acp.package.packageName.package'.$this->queue
->packageID
351 * Saves a localized package info.
353 * @param wcf\system\database\statement\PreparedStatement $statement
354 * @param wcf\data\language\LanguageList $languageList
355 * @param wcf\data\language\category\LanguageCategory $languageCategory
356 * @param wcf\data\package\Package $package
357 * @param string $infoName
359 protected function saveLocalizedPackageInfo(PreparedStatement
$statement, $languageList, LanguageCategory
$languageCategory, Package
$package, $infoName) {
360 $infoValues = $this->getArchive()->getPackageInfo($infoName);
362 // get default value for languages without specified information
364 if (isset($infoValues['default'])) {
365 $defaultValue = $infoValues['default'];
367 else if (isset($infoValues['en'])) {
368 // fallback to English
369 $defaultValue = $infoValues['en'];
371 else if (isset($infoValues[WCF
::getLanguage()->getFixedLanguageCode()])) {
372 // fallback to the language of the current user
373 $defaultValue = $infoValues[WCF
::getLanguage()->getFixedLanguageCode()];
375 else if ($infoName == 'packageName') {
376 // fallback to the package identifier for the package name
377 $defaultValue = $this->archive
->getPackageInfo('name');
380 foreach ($languageList as $language) {
381 $value = $defaultValue;
382 if (isset($infoValues[$language->languageCode
])) {
383 $value = $infoValues[$language->languageCode
];
386 $statement->execute(array(
387 $language->languageID
,
388 'wcf.acp.package.'.$infoName.'.package'.$package->packageID
,
390 $languageCategory->languageCategoryID
,
397 * Executes a package installation plugin.
402 protected function executePIP(array $nodeData) {
403 $step = new PackageInstallationStep();
405 // fetch all pips associated with current PACKAGE_ID and include pips
406 // previously installed by current installation queue
407 $sql = "SELECT pluginName, className
408 FROM wcf".WCF_N
."_package_installation_plugin
409 WHERE pluginName = ?";
410 $statement = WCF
::getDB()->prepareStatement($sql);
411 $statement->execute(array(
414 $row = $statement->fetchArray();
417 if (!$row ||
(strcmp($nodeData['pip'], $row['pluginName']) !== 0)) {
418 throw new SystemException("unable to find package installation plugin '".$nodeData['pip']."'");
421 // valdidate class definition
422 $className = $row['className'];
423 if (!class_exists($className)) {
424 throw new SystemException("unable to find class '".$className."'");
427 $plugin = new $className($this, $nodeData);
429 if (!($plugin instanceof \wcf\system\package\plugin\IPackageInstallationPlugin
)) {
430 throw new SystemException("'".$className."' does not implement 'wcf\system\package\plugin\IPackageInstallationPlugin'");
435 $document = $plugin->{$this->action
}();
437 catch (SplitNodeException
$e) {
438 $step->setSplitNode();
441 if ($document !== null && ($document instanceof FormDocument
)) {
442 $step->setDocument($document);
443 $step->setSplitNode();
450 protected function selectOptionalPackages($currentNode, array $nodeData) {
451 $installationStep = new PackageInstallationStep();
453 $document = $this->promptOptionalPackages($nodeData);
454 if ($document !== null && $document instanceof form\FormDocument
) {
455 $installationStep->setDocument($document);
456 $installationStep->setSplitNode();
458 // insert new nodes for each package
459 else if (is_array($document)) {
460 // get target child node
461 $node = $currentNode;
462 $queue = $this->queue
;
465 foreach ($nodeData as $package) {
466 if (in_array($package['package'], $document)) {
468 $this->nodeBuilder
->shiftNodes($currentNode, 'tempNode');
472 $queue = PackageInstallationQueueEditor
::create(array(
473 'parentQueueID' => $queue->queueID
,
474 'processNo' => $this->queue
->processNo
,
475 'userID' => WCF
::getUser()->userID
,
476 'package' => $package['package'],
477 'packageName' => $package['packageName'],
478 'archive' => $package['archive'],
479 'action' => $queue->action
482 $installation = new PackageInstallationDispatcher($queue);
483 $installation->nodeBuilder
->setParentNode($node);
484 $installation->nodeBuilder
->buildNodes();
485 $node = $installation->nodeBuilder
->getCurrentNode();
491 $this->nodeBuilder
->shiftNodes('tempNode', $node);
495 return $installationStep;
499 * Extracts files from .tar (or .tar.gz) archive and installs them
501 * @param string $targetDir
502 * @param string $sourceArchive
503 * @param FileHandler $fileHandler
504 * @return wcf\system\setup\Installer
506 public function extractFiles($targetDir, $sourceArchive, $fileHandler = null) {
507 return new \wcf\system\setup\
Installer($targetDir, $sourceArchive, $fileHandler);
511 * Returns current package.
513 * @return wcf\data\package\Package
515 public function getPackage() {
516 if ($this->package
=== null) {
517 $this->package
= new Package($this->queue
->packageID
);
520 return $this->package
;
524 * Prompts for a text input for package directory (applies for applications only)
526 * @return wcf\system\form\FormDocument
528 protected function promptPackageDir() {
529 if (!PackageInstallationFormManager
::findForm($this->queue
, 'packageDir')) {
531 $container = new container\
GroupFormElementContainer();
532 $packageDir = new element\
TextInputFormElement($container);
533 $packageDir->setName('packageDir');
534 $packageDir->setLabel(WCF
::getLanguage()->get('wcf.acp.package.packageDir.input'));
536 $path = RouteHandler
::getPath(array('wcf', 'acp'));
537 $defaultPath = FileUtil
::addTrailingSlash(FileUtil
::unifyDirSeperator($_SERVER['DOCUMENT_ROOT'] . $path));
538 $packageDir->setValue($defaultPath);
539 $container->appendChild($packageDir);
541 $document = new form\
FormDocument('packageDir');
542 $document->appendContainer($container);
544 PackageInstallationFormManager
::registerForm($this->queue
, $document);
548 $document = PackageInstallationFormManager
::getForm($this->queue
, 'packageDir');
549 $document->handleRequest();
550 $packageDir = $document->getValue('packageDir');
552 if ($packageDir !== null) {
553 // validate package dir
554 if (file_exists(FileUtil
::addTrailingSlash($packageDir) . 'global.php')) {
555 $document->setError('packageDir', WCF
::getLanguage()->get('wcf.acp.package.packageDir.notAvailable'));
560 $packageEditor = new PackageEditor($this->getPackage());
561 $packageEditor->update(array(
562 'packageDir' => FileUtil
::getRelativePath(WCF_DIR
, $packageDir)
566 $domainPath = FileUtil
::getRelativePath(FileUtil
::unifyDirSeperator($_SERVER['DOCUMENT_ROOT']), FileUtil
::unifyDirSeperator($packageDir));
568 // work-around for applications installed in document root
569 if ($domainPath == './') {
573 $domainPath = FileUtil
::addLeadingSlash(FileUtil
::addTrailingSlash($domainPath));
575 // update application path
576 $application = new Application($this->getPackage()->packageID
);
577 $applicationEditor = new ApplicationEditor($application);
578 $applicationEditor->update(array(
579 'domainPath' => $domainPath,
580 'cookiePath' => $domainPath
583 // create directory and set permissions
584 @mkdir
($packageDir, 0777, true);
585 @chmod
($packageDir, 0777);
593 protected function promptOptionalPackages(array $packages) {
594 if (!PackageInstallationFormManager
::findForm($this->queue
, 'optionalPackages')) {
595 $container = new container\
MultipleSelectionFormElementContainer();
596 $container->setName('optionalPackages');
598 foreach ($packages as $package) {
599 $optionalPackage = new element\
MultipleSelectionFormElement($container);
600 $optionalPackage->setName('optionalPackages');
601 $optionalPackage->setLabel($package['packageName']);
602 $optionalPackage->setValue($package['package']);
604 $container->appendChild($optionalPackage);
607 $document = new form\
FormDocument('optionalPackages');
608 $document->appendContainer($container);
610 PackageInstallationFormManager
::registerForm($this->queue
, $document);
614 $document = PackageInstallationFormManager
::getForm($this->queue
, 'optionalPackages');
615 $document->handleRequest();
617 return $document->getValue('optionalPackages');
622 * Returns current package id.
626 public function getPackageID() {
627 return $this->queue
->packageID
;
631 * Returns current package installation type.
635 public function getAction() {
636 return $this->action
;
640 * Opens the package installation queue and
641 * starts the installation, update or uninstallation of the first entry.
643 * @param integer $parentQueueID
644 * @param integer $processNo
646 public static function openQueue($parentQueueID = 0, $processNo = 0) {
647 $conditions = new PreparedStatementConditionBuilder();
648 $conditions->add("userID = ?", array(WCF
::getUser()->userID
));
649 $conditions->add("parentQueueID = ?", array($parentQueueID));
650 if ($processNo != 0) $conditions->add("processNo = ?", array($processNo));
651 $conditions->add("done = ?", array(0));
654 FROM wcf".WCF_N
."_package_installation_queue
656 ORDER BY queueID ASC";
657 $statement = WCF
::getDB()->prepareStatement($sql);
658 $statement->execute($conditions->getParameters());
659 $packageInstallation = $statement->fetchArray();
661 if (!isset($packageInstallation['queueID'])) {
662 $url = LinkHandler
::getInstance()->getLink('PackageList');
663 HeaderUtil
::redirect($url);
667 $url = LinkHandler
::getInstance()->getLink('PackageInstallationConfirm', array(), 'action='.$packageInstallation['action'].'&queueID='.$packageInstallation['queueID']);
668 HeaderUtil
::redirect($url);
674 * Checks the package installation queue for outstanding entries.
678 public static function checkPackageInstallationQueue() {
679 $sql = "SELECT queueID
680 FROM wcf".WCF_N
."_package_installation_queue
682 AND parentQueueID = 0
684 ORDER BY queueID ASC";
685 $statement = WCF
::getDB()->prepareStatement($sql);
686 $statement->execute(array(WCF
::getUser()->userID
));
687 $row = $statement->fetchArray();
693 return $row['queueID'];
697 * Executes post-setup actions.
699 public function completeSetup() {
700 // mark queue as done
701 $queueEditor = new PackageInstallationQueueEditor($this->queue
);
702 $queueEditor->update(array(
707 $this->nodeBuilder
->purgeNodes();
709 // update package version
710 if ($this->action
== 'update') {
711 $packageEditor = new PackageEditor($this->getPackage());
712 $packageEditor->update(array(
713 'updateDate' => TIME_NOW
,
714 'packageVersion' => $this->archive
->getPackageInfo('version')
718 // clear language files once whole installation is completed
719 LanguageEditor
::deleteLanguageFiles();
722 CacheHandler
::getInstance()->clear(WCF_DIR
.'cache/', '*');
726 * Updates queue information.
728 public function updatePackage() {
729 if (empty($this->queue
->packageName
)) {
730 $queueEditor = new PackageInstallationQueueEditor($this->queue
);
731 $queueEditor->update(array(
732 'packageName' => $this->getArchive()->getLocalizedPackageInfo('packageName')
736 $this->queue
= new PackageInstallationQueue($this->queue
->queueID
);
741 * Validates specific php requirements.
743 * @param array $requirements
744 * @return array<array>
746 public static function validatePHPRequirements(array $requirements) {
749 // validate php version
750 if (isset($requirements['version'])) {
752 if (version_compare(PHP_VERSION
, $requirements['version'], '>=')) {
757 $errors['version'] = array(
758 'required' => $requirements['version'],
759 'installed' => PHP_VERSION
764 // validate extensions
765 if (isset($requirements['extensions'])) {
766 foreach ($requirements['extensions'] as $extension) {
767 $passed = (extension_loaded($extension)) ?
true : false;
770 $errors['extension'][] = array(
771 'extension' => $extension
778 if (isset($requirements['settings'])) {
779 foreach ($requirements['settings'] as $setting => $value) {
780 $iniValue = ini_get($setting);
782 $passed = self
::compareSetting($setting, $value, $iniValue);
784 $errors['setting'][] = array(
785 'setting' => $setting,
786 'required' => $value,
787 'installed' => ($iniValue === false) ?
'(unknown)' : $iniValue
793 // validate functions
794 if (isset($requirements['functions'])) {
795 foreach ($requirements['functions'] as $function) {
796 $function = StringUtil
::toLowerCase($function);
798 $passed = self
::functionExists($function);
800 $errors['function'][] = array(
801 'function' => $function
808 if (isset($requirements['classes'])) {
809 foreach ($requirements['classes'] as $class) {
812 // see: http://de.php.net/manual/en/language.oop5.basic.php
813 if (preg_match('~[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.~', $class)) {
814 $globalClass = '\\'.$class;
816 if (class_exists($globalClass, false)) {
822 $errors['class'][] = array(
834 * Validates if an function exists and is not blacklisted by suhosin extension.
836 * @param string $function
838 * @see http://de.php.net/manual/en/function.function-exists.php#77980
840 protected static function functionExists($function) {
841 if (extension_loaded('suhosin')) {
842 $blacklist = @ini_get
('suhosin.executor.func.blacklist');
843 if (!empty($blacklist)) {
844 $blacklist = explode(',', $blacklist);
845 foreach ($blacklist as $disabledFunction) {
846 $disabledFunction = StringUtil
::toLowerCase(StringUtil
::trim($disabledFunction));
848 if ($function == $disabledFunction) {
855 return function_exists($function);
859 * Compares settings, converting values into compareable ones.
861 * @param string $setting
862 * @param string $value
863 * @param mixed $compareValue
866 protected static function compareSetting($setting, $value, $compareValue) {
867 if ($compareValue === false) return false;
869 $value = StringUtil
::toLowerCase($value);
870 $trueValues = array('1', 'on', 'true');
871 $falseValues = array('0', 'off', 'false');
873 // handle values considered as 'true'
874 if (in_array($value, $trueValues)) {
875 return ($compareValue) ?
true : false;
877 // handle values considered as 'false'
878 else if (in_array($value, $falseValues)) {
879 return (!$compareValue) ?
true : false;
881 else if (!is_numeric($value)) {
882 $compareValue = self
::convertShorthandByteValue($compareValue);
883 $value = self
::convertShorthandByteValue($value);
886 return ($compareValue >= $value) ?
true : false;
890 * Converts shorthand byte values into an integer representing bytes.
892 * @param string $value
894 * @see http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
896 protected static function convertShorthandByteValue($value) {
897 // convert into bytes
898 $lastCharacter = StringUtil
::substring($value, -1);
899 switch ($lastCharacter) {
902 return (int)$value * 1073741824;
907 return (int)$value * 1048576;
912 return (int)$value * 1024;