-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathPackage.class.php
More file actions
481 lines (422 loc) · 14.6 KB
/
Package.class.php
File metadata and controls
481 lines (422 loc) · 14.6 KB
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
<?php
namespace wcf\data\package;
use wcf\acp\page\PackagePage;
use wcf\data\DatabaseObject;
use wcf\data\ILinkableObject;
use wcf\system\application\ApplicationHandler;
use wcf\system\package\PackageInstallationDispatcher;
use wcf\system\request\IRouteController;
use wcf\system\request\LinkHandler;
use wcf\system\WCF;
use wcf\util\FileUtil;
/**
* Represents a package.
*
* @author Alexander Ebert
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @property-read int $packageID unique id of the package
* @property-read string $package unique textual identifier of the package
* @property-read string $packageDir relative directory to Core in which the application is installed or empty if package is no application or Core
* @property-read string $packageName name of the package or name of language item which contains the name
* @property-read string $packageDescription description of the package or name of language item which contains the description
* @property-read string $packageVersion installed version of package
* @property-read int $packageDate timestamp at which the installed package version has been released
* @property-read int $installDate timestamp at which the package has been installed
* @property-read int $updateDate timestamp at which the package has been updated or installed if it has not been updated yet
* @property-read string $packageURL external url to website with more information about the package
* @property-read int $isApplication is `1` if the package delivers an application, otherwise `0`
* @property-read string $author author of the package
* @property-read string $authorURL external url to the website of the package author
*/
class Package extends DatabaseObject implements ILinkableObject, IRouteController
{
/**
* recursive list of packages that were given as required packages during installation
* @var Package[]
* @since 5.2
*/
protected $allRequiredPackages;
/**
* list of packages that this package requires
* @var Package[]
*/
protected $dependencies;
/**
* list of packages that require this package
* @var Package[]
*/
protected $dependentPackages;
/**
* installation directory
* @var string
*/
protected $dir = '';
/**
* list of packages that were given as required packages during installation
* @var Package[]
*/
protected $requiredPackages;
/**
* list of ids of packages which are required by another package
* @var int[]
*/
protected static $requiredPackageIDs;
/**
* package requirements
* @var array<int, int[]>
*/
protected static $requirements;
/**
* @inheritDoc
*/
public function getLink(): string
{
return LinkHandler::getInstance()->getControllerLink(PackagePage::class, [
'object' => $this,
]);
}
/**
* @inheritDoc
*/
public function getTitle(): string
{
return $this->getName();
}
/**
* Returns true if this package is required by other packages.
*
* @return bool
*/
public function isRequired()
{
self::loadRequirements();
return \in_array($this->packageID, self::$requiredPackageIDs);
}
/**
* Returns true if package is a plugin.
*
* @return bool
*/
public function isPlugin()
{
if ($this->isApplication) {
return false;
}
return true;
}
/**
* Returns the name of this package.
*
* @return string
*/
public function getName()
{
return WCF::getLanguage()->get($this->packageName);
}
/**
* @inheritDoc
*/
public function __toString(): string
{
return $this->getName();
}
/**
* Returns the description of this package in the active user's language.
*
* @return string
* @since 5.2
*/
public function getDescription()
{
return WCF::getLanguage()->get($this->packageDescription);
}
/**
* Returns the abbreviation of the package name.
*
* @param string $package
* @return string
*/
public static function getAbbreviation($package)
{
$array = \explode('.', $package);
return \array_pop($array);
}
/**
* Returns the list of packages which are required by this package. The
* returned packages are the packages given in the <requiredpackages> tag
* in the package.xml of this package.
*
* @return Package[]
*/
public function getRequiredPackages()
{
if ($this->requiredPackages === null) {
self::loadRequirements();
$this->requiredPackages = [];
if (isset(self::$requirements[$this->packageID])) {
foreach (self::$requirements[$this->packageID] as $packageID) {
$this->requiredPackages[$packageID] = PackageCache::getInstance()->getPackage($packageID);
}
}
}
return $this->requiredPackages;
}
/**
* Returns the recursive list of packages which are required by this package.
* The returned packages are the packages given in the <requiredpackages> tag
* in the package.xml of this package and recursively repeats that for all of
* those required packages.
*
* @return Package[]
* @since 5.2
*/
public function getAllRequiredPackages()
{
if ($this->allRequiredPackages === null) {
$this->allRequiredPackages = $this->getRequiredPackages();
$packagesToCheck = $this->allRequiredPackages;
while (($checkedPackage = \array_pop($packagesToCheck))) {
$newRequiredPackages = \array_diff($checkedPackage->getRequiredPackages(), $this->allRequiredPackages);
$this->allRequiredPackages += $newRequiredPackages;
$packagesToCheck = \array_merge($packagesToCheck, $newRequiredPackages);
}
}
return $this->allRequiredPackages;
}
/**
* Returns true if current user can uninstall this package.
*
* @return bool
*/
public function canUninstall()
{
if (!WCF::getSession()->getPermission('admin.configuration.package.canInstallPackage')) {
return false;
}
// disallow uninstallation of WCF
if ($this->package == 'com.woltlab.wcf') {
return false;
}
// check if package is required by another package
if ($this->isRequired()) {
return false;
}
return true;
}
/**
* Returns a list of packages dependent from current package.
*
* @return Package[]
*/
public function getDependentPackages()
{
if ($this->dependentPackages === null) {
self::loadRequirements();
$this->dependentPackages = [];
foreach (self::$requirements as $packageID => $requiredPackageIDs) {
if (\in_array($this->packageID, $requiredPackageIDs)) {
$this->dependentPackages[$packageID] = PackageCache::getInstance()->getPackage($packageID);
}
}
}
return $this->dependentPackages;
}
/**
* Overwrites current package version.
*
* DO NOT call this method outside the package installation!
*
* @param string $packageVersion
* @return void
*/
public function setPackageVersion($packageVersion)
{
$this->data['packageVersion'] = $packageVersion;
}
/**
* Returns the absolute path to the package directory with a trailing slash.
*
* @return string
* @since 3.1
*/
public function getAbsolutePackageDir()
{
return FileUtil::addTrailingSlash(FileUtil::getRealPath(WCF_DIR . $this->packageDir));
}
/**
* Loads package requirements.
*
* @return void
*/
protected static function loadRequirements()
{
if (self::$requirements === null) {
$sql = "SELECT packageID, requirement
FROM wcf1_package_requirement";
$statement = WCF::getDB()->prepare($sql);
$statement->execute();
self::$requiredPackageIDs = [];
self::$requirements = [];
while ($row = $statement->fetchArray()) {
if (!isset(self::$requirements[$row['packageID']])) {
self::$requirements[$row['packageID']] = [];
}
self::$requirements[$row['packageID']][] = $row['requirement'];
if (!\in_array($row['requirement'], self::$requiredPackageIDs)) {
self::$requiredPackageIDs[] = $row['requirement'];
}
}
}
}
/**
* Returns true if package identified by $package is already installed.
*
* @param string $package
* @return bool
*/
public static function isAlreadyInstalled($package)
{
$sql = "SELECT COUNT(*)
FROM wcf1_package
WHERE package = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$package]);
return $statement->fetchSingleColumn() > 0;
}
/**
* Checks if a package name is valid.
*
* A valid package name begins with at least one alphanumeric character
* or an underscore, followed by a dot, followed by at least one alphanumeric
* character or an underscore and the same again, possibly repeatedly.
* The package name cannot be any longer than 191 characters in total due to
* internal database character encoding limitations.
* Example:
* com.woltlab.wcf
*
* Reminder: The package name being examined here contains the 'name' attribute
* of the 'package' tag noted in the 'package.xml' file delivered inside
* the respective package.
*/
public static function isValidPackageName(string $packageName): bool
{
if (\mb_strlen($packageName) < 3 || \mb_strlen($packageName) > 191) {
return false;
}
return !!\preg_match('/^[a-zA-Z0-9_-]+\\.[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$/', $packageName);
}
/**
* Returns true if package version is valid.
*
* Examples of valid package versions:
* 1.0.0
* 4.0.0 Alpha 1
* 3.1.7 RC 4
*/
public static function isValidVersion(string $version): bool
{
return !!\preg_match('/^([0-9]+)\\.([0-9]+)\\.([0-9]+)( (dev|Alpha|Beta|RC) ([0-9]+))?$/', $version);
}
/**
* Checks the version number of the installed package against the "fromversion"
* number of the update.
*
* The "fromversion" number may contain wildcards (asterisks) which means
* that the update covers the whole range of release numbers where the asterisk
* wildcards digits from 0 to 9.
* For example, if "fromversion" is "1.1.*" and this package updates to
* version 1.2.0, all releases from 1.1.0 to 1.1.9 may be updated using
* this package.
*/
public static function checkFromversion(string $currentVersion, string $fromVersion): bool
{
if (\str_contains($fromVersion, '*')) {
// from version with wildcard
// use regular expression
$fromVersion = \str_replace('\*', '.*', \preg_quote($fromVersion, '!'));
if (\preg_match('!^' . $fromVersion . '$!i', $currentVersion)) {
return true;
}
} else {
if (self::compareVersion($currentVersion, $fromVersion, '=')) {
return true;
}
}
return false;
}
/**
* Compares two version number strings.
*
* @param string $operator
* @return int|bool result
* @see http://www.php.net/manual/en/function.version-compare.php
*/
public static function compareVersion(string $version1, string $version2, $operator = null)
{
$version1 = self::formatVersionForCompare($version1);
$version2 = self::formatVersionForCompare($version2);
if ($operator === null) {
return \version_compare($version1, $version2);
} else {
return \version_compare($version1, $version2, $operator);
}
}
/**
* Formats a package version string for comparing.
*
* @see http://www.php.net/manual/en/function.version-compare.php
*/
private static function formatVersionForCompare(string $version): string
{
// remove spaces
$version = \str_replace(' ', '', $version);
// correct special version strings
$version = \str_ireplace('dev', 'dev', $version);
$version = \str_ireplace('alpha', 'alpha', $version);
$version = \str_ireplace('beta', 'beta', $version);
$version = \str_ireplace('RC', 'RC', $version);
return \str_ireplace('pl', 'pl', $version);
}
/**
* Writes the config.inc.php for an application.
*
* @param int $packageID
* @return void
*/
public static function writeConfigFile($packageID)
{
$package = new self($packageID);
$packageDir = FileUtil::addTrailingSlash(FileUtil::getRealPath(WCF_DIR . $package->packageDir));
$prefix = \strtoupper(self::getAbbreviation($package->package));
$content = "<?php\n";
$content .= "// {$package->package} (packageID {$packageID})\n";
$content .= "if (!defined('{$prefix}_DIR')) define('{$prefix}_DIR', __DIR__.'/');\n";
$content .= "if (!defined('PACKAGE_ID')) define('PACKAGE_ID', {$packageID});\n";
if ($packageID != 1) {
$content .= "\n";
$content .= "// helper constants for applications\n";
$content .= "if (!defined('RELATIVE_{$prefix}_DIR')) define('RELATIVE_{$prefix}_DIR', '');\n";
$content .= "if (!defined('RELATIVE_WCF_DIR')) define('RELATIVE_WCF_DIR', RELATIVE_{$prefix}_DIR.'" . FileUtil::getRelativePath(
$packageDir,
WCF_DIR
) . "');\n";
}
\file_put_contents($packageDir . PackageInstallationDispatcher::CONFIG_FILE, $content);
}
/**
* @since 6.2
*/
public function isTainted(): bool
{
if (!$this->isApplication) {
return false;
}
$package = ApplicationHandler::getInstance()->getApplicationByID($this->packageID);
if ($package === null) {
return false;
}
return !!$package->isTainted;
}
}