1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Composer\Package\Package; |
4
|
|
|
use Composer\Package\PackageInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Downloads an add-on and builds more details information about it. |
8
|
|
|
*/ |
9
|
|
|
class AddonBuilder |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
const ADDONS_DIR = 'addon-downloads'; |
13
|
|
|
|
14
|
|
|
const SCREENSHOTS_DIR = 'screenshots'; |
15
|
|
|
|
16
|
|
|
private $packagist; |
17
|
|
|
|
18
|
|
|
public function __construct(PackagistService $packagist) |
19
|
|
|
{ |
20
|
|
|
$this->packagist = $packagist; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function build(Addon $addon) |
24
|
|
|
{ |
25
|
|
|
putenv("GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no\""); |
26
|
|
|
|
27
|
|
|
$composer = $this->packagist->getComposer(); |
28
|
|
|
$downloader = $composer->getDownloadManager(); |
|
|
|
|
29
|
|
|
$packageVersions = $this->packagist->getPackageVersions($addon->Name); |
|
|
|
|
30
|
|
|
$time = time(); |
31
|
|
|
|
32
|
|
|
if (!$packageVersions) { |
|
|
|
|
33
|
|
|
throw new Exception('Could not find corresponding Packagist versions'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// Get the latest local and packagist version pair. |
37
|
|
|
$version = $addon->Versions()->filter('Development', true)->first(); |
|
|
|
|
38
|
|
|
if (!$version) { |
39
|
|
|
echo "No versions found for " . $addon->Name . "; deleting orphan record.\n"; |
|
|
|
|
40
|
|
|
$addon->delete(); |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($packageVersions as $packageVersion) { |
45
|
|
|
if ($packageVersion->getVersionNormalized() != $version->Version) { |
|
|
|
|
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (defined('SS_ADDONS_DOWNLOAD_PATH')) { |
50
|
|
|
$path = SS_ADDONS_DOWNLOAD_PATH . '/' . $addon->Name; |
|
|
|
|
51
|
|
|
} else { |
52
|
|
|
$path = implode('/', array( |
53
|
|
|
TEMP_FOLDER, self::ADDONS_DIR, $addon->Name |
|
|
|
|
54
|
|
|
)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Convert PackagistAPI result into class compatible with Composer logic |
58
|
|
|
$package = new Package( |
59
|
|
|
$addon->Name, |
|
|
|
|
60
|
|
|
$packageVersion->getVersionNormalized(), |
|
|
|
|
61
|
|
|
$packageVersion->getVersion() |
62
|
|
|
); |
63
|
|
|
$package->setExtra((array)$packageVersion->getExtra()); |
64
|
|
|
if ($source = $packageVersion->getSource()) { |
|
|
|
|
65
|
|
|
$package->setSourceUrl($source->getUrl()); |
66
|
|
|
$package->setSourceType($source->getType()); |
67
|
|
|
$package->setSourceReference($source->getReference()); |
68
|
|
|
} |
69
|
|
|
if ($dist = $packageVersion->getDist()) { |
|
|
|
|
70
|
|
|
$package->setDistUrl($dist->getUrl()); |
71
|
|
|
$package->setDistType($dist->getType()); |
72
|
|
|
$package->setDistReference($dist->getReference()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
$this->download($package, $path); |
77
|
|
|
|
78
|
|
|
// If there's an error, mark this version as bad |
79
|
|
|
} catch (RuntimeException $e) { |
80
|
|
|
echo "Add-on " . $addon->Name . " couldn't be downloaded; deleting from database.\n"; |
|
|
|
|
81
|
|
|
echo "Error message: " . $e->getMessage() . "\n"; |
82
|
|
|
$addon->delete(); |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->buildReadme($addon, $path); |
87
|
|
|
$this->buildScreenshots($addon, $package, $path); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$addon->LastBuilt = $time; |
|
|
|
|
91
|
|
|
$addon->write(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function download(PackageInterface $package, $path) |
95
|
|
|
{ |
96
|
|
|
$this->packagist |
97
|
|
|
->getComposer() |
98
|
|
|
->getDownloadManager() |
99
|
|
|
->download($package, $path); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Parses a readme file from markdown to HTML, then purifies it |
104
|
|
|
* @param Addon $addon |
105
|
|
|
* @param string $path |
106
|
|
|
*/ |
107
|
|
|
protected function buildReadme(Addon $addon, $path) |
108
|
|
|
{ |
109
|
|
|
$candidates = array( |
110
|
|
|
'README.md', |
111
|
|
|
'README.markdown', |
112
|
|
|
'README.mdown', |
113
|
|
|
'docs/en/index.md' |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
foreach ($candidates as $candidate) { |
117
|
|
|
$lower = strtolower($candidate); |
118
|
|
|
$paths = array("$path/$candidate", "$path/$lower"); |
119
|
|
|
|
120
|
|
|
foreach ($paths as $path) { |
121
|
|
|
if (!file_exists($path)) { |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$parser = GitHubMarkdownService::create(); |
126
|
|
|
if ($context = $this->getGitHubContext($addon)) { |
127
|
|
|
$parser->setContext($context); |
128
|
|
|
} |
129
|
|
|
$readme = $parser->toHtml(file_get_contents($path)); |
130
|
|
|
|
131
|
|
|
if (empty($readme)) { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$readme = $parser->toHtml(file_get_contents($path)); |
136
|
|
|
|
137
|
|
|
$purifier = new HTMLPurifier(); |
138
|
|
|
$readme = $purifier->purify($readme, array( |
139
|
|
|
'Cache.SerializerPath' => TEMP_FOLDER |
140
|
|
|
)); |
141
|
|
|
|
142
|
|
|
$readme = $this->replaceRelativeLinks($addon, $readme); |
143
|
|
|
$addon->Readme = $readme; |
|
|
|
|
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Determine if the repository is from GitHub, and if so then return the "context" (vendor/module) from the path |
151
|
|
|
* |
152
|
|
|
* @param Addon $addon |
153
|
|
|
* @return string|false |
154
|
|
|
*/ |
155
|
|
|
public function getGitHubContext(Addon $addon) |
156
|
|
|
{ |
157
|
|
|
$repository = $addon->Repository; |
|
|
|
|
158
|
|
|
if (stripos($repository, '://github.com/') === false) { |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
preg_match('/^http(?:s?):\/\/github\.com\/(?<module>.*)(\.git)?$/U', $repository, $matches); |
163
|
|
|
|
164
|
|
|
if (isset($matches['module'])) { |
165
|
|
|
return $matches['module']; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Given an addon and a parsed HTML readme string, find and replace relative links with absolute |
173
|
|
|
* repository path links. This method applies to GitHub repositories only. |
174
|
|
|
* |
175
|
|
|
* @param Addon $addon |
176
|
|
|
* @param string $readme |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function replaceRelativeLinks(Addon $addon, $readme) |
180
|
|
|
{ |
181
|
|
|
if (!$this->hasGitHubRepository($addon)) { |
182
|
|
|
return $readme; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$dom = new DOMDocument; |
186
|
|
|
// LibXML needs a wrapper element... |
187
|
|
|
$dom->loadHTML('<div>' . $readme . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); |
188
|
|
|
$xpath = new DOMXPath($dom); |
189
|
|
|
|
190
|
|
|
// Select all anchors and images in the readme document |
191
|
|
|
$query = $xpath->query('//*[self::a or self::img]'); |
192
|
|
|
|
193
|
|
|
foreach ($query as $element) { /** @var DOMElement $element */ |
194
|
|
|
$attribute = ($element->nodeName === 'a') ? 'href' : 'src'; |
195
|
|
|
$path = $element->getAttribute($attribute); |
196
|
|
|
if (!$this->isRelativeUri($path)) { |
197
|
|
|
continue; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
// See GitHub readmes for example |
201
|
|
|
$folder = ($attribute === 'href') ? 'blob' : 'raw'; |
202
|
|
|
$defaultBranch = 'master'; // Is this safe to assume? |
203
|
|
|
|
204
|
|
|
$element->setAttribute( |
205
|
|
|
$attribute, |
206
|
|
|
implode('/', array($addon->Repository, $folder, $defaultBranch, $path)) |
|
|
|
|
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// Return the inner HTML of the wrapper div... Reference: stackoverflow.com/a/39193507/2812842 |
211
|
|
|
$node = $dom->getElementsByTagName('div')->item(0); |
212
|
|
|
return implode(array_map([$node->ownerDocument, 'saveHTML'], iterator_to_array($node->childNodes))); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Decide whether a URI path is relative or not. The regex pattern matches prefixes that start with |
217
|
|
|
* a protocol, a slash or a hash. If they don't start with those things, then they are deemed to be |
218
|
|
|
* relative paths. |
219
|
|
|
* |
220
|
|
|
* @param string $path |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
public function isRelativeUri($path) |
224
|
|
|
{ |
225
|
|
|
return !preg_match('/(^(?:https?:\/\/|\/|#).*$)/', $path); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Determine whether an addon is hosted on GitHub |
230
|
|
|
* |
231
|
|
|
* @param Addon $addon |
232
|
|
|
* @return bool |
233
|
|
|
*/ |
234
|
|
|
public function hasGitHubRepository(Addon $addon) |
235
|
|
|
{ |
236
|
|
|
return (strpos($addon->Repository, 'github.com') !== false); |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
private function buildScreenshots(Addon $addon, PackageInterface $package, $path) |
240
|
|
|
{ |
241
|
|
|
$extra = $package->getExtra(); |
242
|
|
|
$screenshots = array(); |
243
|
|
|
$target = self::SCREENSHOTS_DIR . '/' . $addon->Name; |
|
|
|
|
244
|
|
|
|
245
|
|
|
if (isset($extra['screenshots'])) { |
246
|
|
|
$screenshots = (array) $extra['screenshots']; |
247
|
|
|
} elseif (isset($extra['screenshot'])) { |
248
|
|
|
$screenshots = (array) $extra['screenshot']; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
// Delete existing screenshots. |
252
|
|
|
foreach ($addon->Screenshots() as $screenshot) { |
|
|
|
|
253
|
|
|
$screenshot->delete(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
$addon->Screenshots()->removeAll(); |
|
|
|
|
257
|
|
|
|
258
|
|
|
foreach ($screenshots as $screenshot) { |
259
|
|
|
if (!is_string($screenshot)) { |
260
|
|
|
continue; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$scheme = parse_url($screenshot, PHP_URL_SCHEME); |
264
|
|
|
|
265
|
|
|
// Handle absolute image URLs. |
266
|
|
|
if ($scheme == 'http' || $scheme == 'https') { |
267
|
|
|
$temp = TEMP_FOLDER . '/' . md5($screenshot); |
268
|
|
|
|
269
|
|
|
if (!copy($screenshot, $temp)) { |
270
|
|
|
continue; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
$data = array( |
274
|
|
|
'name' => basename($screenshot), |
275
|
|
|
'size' => filesize($temp), |
276
|
|
|
'tmp_name' => $temp, |
277
|
|
|
'error' => 0 |
278
|
|
|
); |
279
|
|
|
} // Handle images that are included in the repository. |
280
|
|
|
else { |
281
|
|
|
$source = $path . '/' . ltrim($screenshot, '/'); |
282
|
|
|
|
283
|
|
|
// Prevent directory traversal. |
284
|
|
|
if ($source != realpath($source)) { |
285
|
|
|
continue; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
if (!file_exists($source)) { |
289
|
|
|
continue; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$data = array( |
293
|
|
|
'name' => basename($source), |
294
|
|
|
'size' => filesize($source), |
295
|
|
|
'tmp_name' => $source, |
296
|
|
|
'error' => 0 |
297
|
|
|
); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$upload = new Upload(); |
301
|
|
|
$upload->setValidator(new AddonBuilderScreenshotValidator()); |
302
|
|
|
$upload->load($data, $target); |
|
|
|
|
303
|
|
|
|
304
|
|
|
if ($file = $upload->getFile()) { |
305
|
|
|
$addon->Screenshots()->add($file); |
|
|
|
|
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.