Merge branch '5.3'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / package / FilesFileHandler.class.php
1 <?php
2
3 namespace wcf\system\package;
4
5 use wcf\data\package\Package;
6 use wcf\system\database\util\PreparedStatementConditionBuilder;
7 use wcf\system\exception\SystemException;
8 use wcf\system\WCF;
9
10 /**
11 * File handler implementation for the installation of regular files.
12 *
13 * @author Matthias Schmidt, Marcel Werk
14 * @copyright 2001-2019 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package WoltLabSuite\Core\System\Package
17 */
18 class FilesFileHandler extends PackageInstallationFileHandler
19 {
20 /**
21 * @inheritDoc
22 */
23 public function checkFiles(array $files)
24 {
25 if ($this->packageInstallation->getPackage()->package != 'com.woltlab.wcf') {
26 if (!empty($files)) {
27 // get registered files of other packages for the
28 // same application
29 $conditions = new PreparedStatementConditionBuilder();
30 $conditions->add('packageID <> ?', [$this->packageInstallation->getPackageID()]);
31 $conditions->add('filename IN (?)', [$files]);
32 $conditions->add('application = ?', [$this->application]);
33
34 $sql = "SELECT filename, packageID
35 FROM wcf" . WCF_N . "_package_installation_file_log
36 " . $conditions;
37 $statement = WCF::getDB()->prepareStatement($sql);
38 $statement->execute($conditions->getParameters());
39 $lockedFiles = $statement->fetchMap('filename', 'packageID');
40
41 // check delivered files
42 if (!empty($lockedFiles)) {
43 foreach ($files as $key => $file) {
44 if (isset($lockedFiles[$file])) {
45 $owningPackage = new Package($lockedFiles[$file]);
46
47 throw new SystemException("A package can't overwrite files from other packages. Only an update from the package which owns the file can do that. (Package '" . $this->packageInstallation->getPackage()->package . "' tries to overwrite file '" . $file . "', which is owned by package '" . $owningPackage->package . "')");
48 }
49 }
50 }
51 }
52 }
53 }
54
55 /**
56 * @inheritDoc
57 */
58 public function logFiles(array $files)
59 {
60 if (empty($files)) {
61 return;
62 }
63
64 $sql = "INSERT IGNORE INTO wcf" . WCF_N . "_package_installation_file_log
65 (packageID, filename, application)
66 VALUES (?, ?, ?)";
67 $statement = WCF::getDB()->prepareStatement($sql);
68
69 WCF::getDB()->beginTransaction();
70 foreach ($files as $file) {
71 $statement->execute([
72 $this->packageInstallation->getPackageID(),
73 $file,
74 $this->application,
75 ]);
76 }
77 WCF::getDB()->commitTransaction();
78 }
79 }