Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / package / plugin / FilePackageInstallationPlugin.class.php
1 <?php
2 namespace wcf\system\package\plugin;
3 use wcf\data\application\Application;
4 use wcf\data\package\Package;
5 use wcf\system\package\FilesFileHandler;
6 use wcf\system\package\PackageArchive;
7 use wcf\system\package\PackageInstallationDispatcher;
8 use wcf\system\WCF;
9 use wcf\util\StyleUtil;
10
11 /**
12 * Installs, updates and deletes files.
13 *
14 * @author Matthias Schmidt, Marcel Werk
15 * @copyright 2001-2014 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package com.woltlab.wcf
18 * @subpackage system.package.plugin
19 * @category Community Framework
20 */
21 class FilePackageInstallationPlugin extends AbstractPackageInstallationPlugin {
22 /**
23 * @see \wcf\system\package\plugin\AbstractPackageInstallationPlugin::$tableName
24 */
25 public $tableName = 'package_installation_file_log';
26
27 /**
28 * @see \wcf\system\package\plugin\IPackageInstallationPlugin::install()
29 */
30 public function install() {
31 parent::install();
32
33 $abbreviation = 'wcf';
34 if (isset($this->instruction['attributes']['application'])) {
35 $abbreviation = $this->instruction['attributes']['application'];
36 }
37 else if ($this->installation->getPackage()->isApplication) {
38 $abbreviation = Package::getAbbreviation($this->installation->getPackage()->package);
39 }
40
41 // absolute path to package dir
42 $packageDir = Application::getDirectory($abbreviation);
43
44 // extract files.tar to temp folder
45 $sourceFile = $this->installation->getArchive()->extractTar($this->instruction['value'], 'files_');
46
47 // create file handler
48 $fileHandler = new FilesFileHandler($this->installation, $abbreviation);
49
50 // extract content of files.tar
51 $fileInstaller = $this->installation->extractFiles($packageDir, $sourceFile, $fileHandler);
52
53 // if this a an application, write config.inc.php for this package
54 if ($this->installation->getPackage()->isApplication == 1 && $this->installation->getPackage()->package != 'com.woltlab.wcf' && $this->installation->getAction() == 'install' && $abbreviation != 'wcf') {
55 // touch file
56 $fileInstaller->touchFile(PackageInstallationDispatcher::CONFIG_FILE);
57
58 // create file
59 Package::writeConfigFile($this->installation->getPackageID());
60
61 // log file
62 $sql = "INSERT INTO wcf".WCF_N."_package_installation_file_log
63 (packageID, filename, application)
64 VALUES (?, ?, ?)";
65 $statement = WCF::getDB()->prepareStatement($sql);
66 $statement->execute(array(
67 $this->installation->getPackageID(),
68 'config.inc.php',
69 Package::getAbbreviation($this->installation->getPackage()->package)
70 ));
71
72 // load application
73 WCF::loadRuntimeApplication($this->installation->getPackageID());
74 }
75
76 // delete temporary sourceArchive
77 @unlink($sourceFile);
78
79 // update acp style file
80 StyleUtil::updateStyleFile();
81 }
82
83 /**
84 * @see \wcf\system\package\plugin\IPackageInstallationPlugin::uninstall()
85 */
86 public function uninstall() {
87 // fetch files from log
88 $sql = "SELECT filename, application
89 FROM wcf".WCF_N."_package_installation_file_log
90 WHERE packageID = ?";
91 $statement = WCF::getDB()->prepareStatement($sql);
92 $statement->execute(array($this->installation->getPackageID()));
93
94 $files = array();
95 while ($row = $statement->fetchArray()) {
96 if (!isset($files[$row['application']])) {
97 $files[$row['application']] = array();
98 }
99
100 $files[$row['application']][] = $row['filename'];
101 }
102
103 foreach ($files as $application => $filenames) {
104 $this->installation->deleteFiles(Application::getDirectory($application), $filenames);
105
106 // delete log entries
107 parent::uninstall();
108 }
109 }
110
111 /**
112 * @see \wcf\system\package\plugin\IPackageInstallationPlugin::isValid()
113 */
114 public static function isValid(PackageArchive $archive, $instruction) {
115 if (preg_match('~\.(tar(\.gz)?|tgz)$~', $instruction)) {
116 // check if file actually exists
117 try {
118 if ($archive->getTar()->getIndexByFilename($instruction) === false) {
119 return false;
120 }
121 }
122 catch (\SystemException $e) {
123 return false;
124 }
125
126 return true;
127 }
128
129 return false;
130 }
131 }