1
|
|
|
<?php |
2
|
|
|
namespace TYPO3\PharStreamWrapper\Resolver; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the TYPO3 project. |
6
|
|
|
* |
7
|
|
|
* It is free software; you can redistribute it and/or modify it under the terms |
8
|
|
|
* of the MIT License (MIT). For the full copyright and license information, |
9
|
|
|
* please read the LICENSE file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* The TYPO3 project - inspiring people to share! |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
use TYPO3\PharStreamWrapper\Helper; |
15
|
|
|
use TYPO3\PharStreamWrapper\Manager; |
16
|
|
|
use TYPO3\PharStreamWrapper\Phar\Reader; |
17
|
|
|
use TYPO3\PharStreamWrapper\Resolvable; |
18
|
|
|
|
19
|
|
|
class PharInvocationResolver implements Resolvable |
20
|
|
|
{ |
21
|
|
|
const RESOLVE_REALPATH = 1; |
22
|
|
|
const RESOLVE_ALIAS = 2; |
23
|
|
|
const ASSERT_INTERNAL_INVOCATION = 32; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string[] |
27
|
|
|
*/ |
28
|
|
|
private $invocationFunctionNames = array( |
29
|
|
|
'include', |
30
|
|
|
'include_once', |
31
|
|
|
'require', |
32
|
|
|
'require_once' |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Resolves PharInvocation value object (baseName and optional alias). |
38
|
|
|
* |
39
|
|
|
* Phar aliases are intended to be used only inside Phar archives, however |
40
|
|
|
* PharStreamWrapper needs this information exposed outside of Phar as well |
41
|
|
|
* It is possible that same alias is used for different $baseName values. |
42
|
|
|
* That's why PharInvocationCollection behaves like a stack when resolving |
43
|
|
|
* base-name for a given alias. On the other hand it is not possible that |
44
|
|
|
* one $baseName is referring to multiple aliases. |
45
|
|
|
* @see https://secure.php.net/manual/en/phar.setalias.php |
46
|
|
|
* @see https://secure.php.net/manual/en/phar.mapphar.php |
47
|
|
|
* |
48
|
|
|
* @param string $path |
49
|
|
|
* @param int|null $flags |
50
|
|
|
* @return null|PharInvocation |
51
|
|
|
*/ |
52
|
|
|
public function resolve($path, $flags = null) |
53
|
|
|
{ |
54
|
|
|
$hasPharPrefix = Helper::hasPharPrefix($path); |
55
|
|
|
if ($flags === null) { |
56
|
|
|
$flags = static::RESOLVE_REALPATH | static::RESOLVE_ALIAS | static::ASSERT_INTERNAL_INVOCATION; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($hasPharPrefix && $flags & static::RESOLVE_ALIAS) { |
60
|
|
|
$invocation = $this->findByAlias($path); |
61
|
|
|
if ($invocation !== null && $this->assertInternalInvocation($invocation, $flags)) { |
62
|
|
|
return $invocation; |
63
|
|
|
} elseif ($invocation !== null) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$baseName = Helper::determineBaseFile($path); |
69
|
|
|
if ($baseName === null) { |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($flags & static::RESOLVE_REALPATH) { |
74
|
|
|
$baseName = realpath($baseName); |
75
|
|
|
} |
76
|
|
|
if ($flags & static::RESOLVE_ALIAS) { |
77
|
|
|
$reader = new Reader($baseName); |
78
|
|
|
$alias = $reader->resolveContainer()->getAlias(); |
79
|
|
|
} else { |
80
|
|
|
$alias = ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return new PharInvocation($baseName, $alias); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $path |
88
|
|
|
* @return null|PharInvocation |
89
|
|
|
*/ |
90
|
|
|
private function findByAlias($path) |
91
|
|
|
{ |
92
|
|
|
$normalizedPath = Helper::normalizePath($path); |
93
|
|
|
$possibleAlias = strstr($normalizedPath, '/', true); |
94
|
|
|
if (empty($possibleAlias)) { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
return Manager::instance()->getCollection()->findByCallback( |
98
|
|
|
function (PharInvocation $candidate) use ($possibleAlias) { |
99
|
|
|
return $candidate->getAlias() === $possibleAlias; |
100
|
|
|
}, |
101
|
|
|
true |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param PharInvocation $invocation |
107
|
|
|
* @param int $flags |
108
|
|
|
* @return bool |
109
|
|
|
* @experimental |
110
|
|
|
*/ |
111
|
|
|
private function assertInternalInvocation(PharInvocation $invocation, $flags) |
112
|
|
|
{ |
113
|
|
|
if (!($flags & static::ASSERT_INTERNAL_INVOCATION)) { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$trace = debug_backtrace(0); |
118
|
|
|
$firstIndex = count($trace) - 1; |
119
|
|
|
// initial invocation, most probably a CLI tool |
120
|
|
|
if (isset($trace[$firstIndex]['file']) && $trace[$firstIndex]['file'] === $invocation->getBaseName()) { |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
// otherwise search for include/require invocations |
124
|
|
|
foreach ($trace as $item) { |
125
|
|
|
if (!isset($item['function']) || !isset($item['args'][0])) { |
126
|
|
|
continue; |
127
|
|
|
} |
128
|
|
|
if ($item['args'][0] === $invocation->getBaseName() |
129
|
|
|
&& in_array($item['function'], $this->invocationFunctionNames, true) |
130
|
|
|
) { |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|