|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* @copyright (c) 2014 phpBB Group |
|
5
|
|
|
* @license http://opensource.org/licenses/gpl-3.0.php GNU General Public License v3 |
|
6
|
|
|
* @author MichaelC |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace AppBundle\Utilities; |
|
11
|
|
|
|
|
12
|
|
|
use Symfony\Component\HttpKernel\Config\FileLocator; |
|
13
|
|
|
|
|
14
|
|
|
class DownloadManager |
|
15
|
|
|
{ |
|
16
|
|
|
protected $branch; |
|
17
|
|
|
protected $selectedVersion; |
|
18
|
|
|
protected $update; |
|
19
|
|
|
protected $packages; |
|
20
|
|
|
protected $fromVersions; |
|
21
|
|
|
protected $kernel; |
|
22
|
|
|
protected $cache; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct($cache, $kernel) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->cache = $cache; |
|
27
|
|
|
$this->kernel = $kernel; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function setBranch($branch) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->branch = $branch; |
|
33
|
|
|
|
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function setUpdate($selectedVersion) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->selectedVersion = $selectedVersion; |
|
40
|
|
|
$this->update = true; |
|
41
|
|
|
|
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getAvailableUpdateFromVersions() |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->fromVersions; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function generatePackages() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->generatePackageList(); |
|
53
|
|
|
|
|
54
|
|
|
return array( |
|
55
|
|
|
'packages' => $this->packages, |
|
56
|
|
|
'caching' => $this->caching, |
|
|
|
|
|
|
57
|
|
|
'updateFromVersions' => $this->fromVersions, |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function getPackagesJsonData() |
|
62
|
|
|
{ |
|
63
|
|
|
if ($this->cache->contains('packages_json_downloads') !== FALSE) |
|
64
|
|
|
{ |
|
65
|
|
|
// If we have it in cache, get the packages.json file |
|
66
|
|
|
$packagesDataJson = $this->cache->fetch('packages_json_downloads'); |
|
67
|
|
|
$cacheStatus = 'Hit'; |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
else |
|
70
|
|
|
{ |
|
71
|
|
|
// If we don't have it in cache, find it & load it |
|
72
|
|
|
$locator = new FileLocator($this->kernel->getRootDir()); |
|
73
|
|
|
$locator->locate('packages.json', null, true); |
|
74
|
|
|
$packagesDataJson = $locator->getContents(); |
|
|
|
|
|
|
75
|
|
|
$this->cache->save('packages_json_downloads', $packagesDataJson, 86400); |
|
76
|
|
|
$cacheStatus = 'Miss'; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Parse JSON response and discard irrelevant branches |
|
80
|
|
|
return json_decode($packagesDataJson, true); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function generatePackageList() |
|
84
|
|
|
{ |
|
85
|
|
|
// Get packages |
|
86
|
|
|
$packagesData = $this->getPackagesJsonData(); |
|
87
|
|
|
|
|
88
|
|
|
// Discard those not on this branch |
|
89
|
|
|
$relevantPackages = $packagesData[$this->branch]; |
|
90
|
|
|
|
|
91
|
|
|
// Latest release in this branch is... |
|
92
|
|
|
$release = $relevantPackages['release']; |
|
93
|
|
|
|
|
94
|
|
|
// Generate from versions |
|
95
|
|
|
$this->fromVersions = explode(',', $relevantPackages['updates']['from']); |
|
96
|
|
|
|
|
97
|
|
|
// Check selected version is a valid version |
|
98
|
|
|
if (!in_array($this->selectedVersion, $this->fromVersions)) |
|
99
|
|
|
{ |
|
100
|
|
|
$update = false; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Link to the packages for this release. Add filenames on here for download urls. |
|
104
|
|
|
$download_base_link = 'https://download.phpbb.com/pub/release/' . $this->branch . '/' . $release . '/'; |
|
105
|
|
|
|
|
106
|
|
|
$hashCaches = 0; |
|
107
|
|
|
$packagesTotal = 0; |
|
108
|
|
|
|
|
109
|
|
|
// If we haven't established it's an update (and have an update from version) |
|
110
|
|
|
if (!$this->update) |
|
111
|
|
|
{ |
|
112
|
|
|
// Discard irrlevant data |
|
113
|
|
|
$packages = array( |
|
114
|
|
|
'package' => $relevantPackages['package']['release'], |
|
115
|
|
|
'patch' => $relevantPackages['updates']['patch'], |
|
116
|
|
|
'changed-files' => $relevantPackages['updates']['changed-files'], |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
foreach ($packages as $package) |
|
120
|
|
|
{ |
|
121
|
|
|
// URL to this specific package |
|
122
|
|
|
$url = $download_base_link . $package['filename']; |
|
123
|
|
|
|
|
124
|
|
|
// Generate sha256/md5 hashes for packages |
|
125
|
|
|
$hash = $this->gethash($packages[$package]['filename'], $url); |
|
126
|
|
|
|
|
127
|
|
|
// Make use of the stuff we just generated by putting it back in ready for templates |
|
128
|
|
|
$packages[$package]['url'] = $url; |
|
129
|
|
|
$packages[$package]['hash'] = $hash['hash']; |
|
130
|
|
|
$packages[$package]['hashType'] = $hash['hashType']; |
|
131
|
|
|
|
|
132
|
|
|
// Counts |
|
133
|
|
|
$packagesTotal++; |
|
134
|
|
|
($hash['hashCacheStatus'] == 'Hit') ? $hashCaches++ : null; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
else |
|
138
|
|
|
{ |
|
139
|
|
|
// Discard irrlevant data |
|
140
|
|
|
$packages = array( |
|
141
|
|
|
'package' => $relevantPackages['package']['release'], |
|
142
|
|
|
); |
|
143
|
|
|
|
|
144
|
|
|
foreach ($relevantPackages['updates']['changed-files'] as $changedFilesPackage) |
|
145
|
|
|
{ |
|
146
|
|
|
if ($changedFilesPackage['from'] == $this->selectedVersion) |
|
147
|
|
|
{ |
|
148
|
|
|
// URL to this specific package |
|
149
|
|
|
$url = $download_base_link . $changedFilesPackage['filename']; |
|
150
|
|
|
|
|
151
|
|
|
// Generate sha256/md5 hashes for packages |
|
152
|
|
|
$hash = $this->gethash($changedFilesPackage['filename'], $url); |
|
153
|
|
|
|
|
154
|
|
|
// Make use of the stuff we just generated by putting it back in ready for templates |
|
155
|
|
|
$changedFilesPackage['url'] = $url; |
|
156
|
|
|
$changedFilesPackage['hash'] = $hash['hash']; |
|
157
|
|
|
$changedFilesPackage['hashType'] = $hash['hashType']; |
|
158
|
|
|
|
|
159
|
|
|
// Counts |
|
160
|
|
|
$packagesTotal++; |
|
161
|
|
|
($hash['hashCacheStatus'] == 'Hit') ? $hashCaches++ : null; |
|
162
|
|
|
|
|
163
|
|
|
$packages['changed_files'] = $changedFilesPackage; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
foreach ($relevantPackages['updates']['patch'] as $patchFilesPackage) |
|
168
|
|
|
{ |
|
169
|
|
|
if ($patchFilesPackage['from'] == $this->selectedVersion) |
|
170
|
|
|
{ |
|
171
|
|
|
// URL to this specific package |
|
172
|
|
|
$url = $download_base_link . $patchFilesPackage['filename']; |
|
173
|
|
|
|
|
174
|
|
|
// Generate sha256/md5 hashes for packages |
|
175
|
|
|
$hash = $this->gethash($patchFilesPackage['filename'], $url); |
|
176
|
|
|
|
|
177
|
|
|
// Make use of the stuff we just generated by putting it back in ready for templates |
|
178
|
|
|
$patchFilesPackage['url'] = $url; |
|
179
|
|
|
$patchFilesPackage['hash'] = $hash['hash']; |
|
180
|
|
|
$patchFilesPackage['hashType'] = $hash['hashType']; |
|
181
|
|
|
|
|
182
|
|
|
// Counts |
|
183
|
|
|
$packagesTotal++; |
|
184
|
|
|
($hash['hashCacheStatus'] == 'Hit') ? $hashCaches++ : null; |
|
185
|
|
|
|
|
186
|
|
|
$packages['patch'] = $patchFilesPackage; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
foreach ($relevantPackages['updates']['code-changes'] as $ccFilesPackage) |
|
191
|
|
|
{ |
|
192
|
|
|
if ($ccFilesPackage['from'] == $this->selectedVersion) |
|
193
|
|
|
{ |
|
194
|
|
|
// URL to this specific package |
|
195
|
|
|
$url = $download_base_link . $ccFilesPackage['filename']; |
|
196
|
|
|
|
|
197
|
|
|
// Generate sha256/md5 hashes for packages |
|
198
|
|
|
$hash = $this->gethash($ccFilesPackage['filename'], $url); |
|
199
|
|
|
|
|
200
|
|
|
// Make use of the stuff we just generated by putting it back in ready for templates |
|
201
|
|
|
$ccFilesPackage['url'] = $url; |
|
202
|
|
|
$ccFilesPackage['hash'] = $hash['hash']; |
|
203
|
|
|
$ccFilesPackage['hashType'] = $hash['hashType']; |
|
204
|
|
|
|
|
205
|
|
|
// Counts |
|
206
|
|
|
$packagesTotal++; |
|
207
|
|
|
($hash['hashCacheStatus'] == 'Hit') ? $hashCaches++ : null; |
|
208
|
|
|
|
|
209
|
|
|
$packages['code-changes'] = $ccFilesPackage; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
foreach ($relevantPackages['updates']['automatic-updaters'] as $automaticFilesPackage) |
|
214
|
|
|
{ |
|
215
|
|
|
if ($automaticFilesPackage['from'] == $this->selectedVersion) |
|
216
|
|
|
{ |
|
217
|
|
|
// URL to this specific package |
|
218
|
|
|
$url = $download_base_link . $automaticFilesPackage['filename']; |
|
219
|
|
|
|
|
220
|
|
|
// Generate sha256/md5 hashes for packages |
|
221
|
|
|
$hash = $this->gethash($automaticFilesPackage['filename'], $url); |
|
222
|
|
|
|
|
223
|
|
|
// Make use of the stuff we just generated by putting it back in ready for templates |
|
224
|
|
|
$automaticFilesPackage['url'] = $url; |
|
225
|
|
|
$automaticFilesPackage['hash'] = $hash['hash']; |
|
226
|
|
|
$automaticFilesPackage['hashType'] = $hash['hashType']; |
|
227
|
|
|
|
|
228
|
|
|
// Counts |
|
229
|
|
|
$packagesTotal++; |
|
230
|
|
|
($hash['hashCacheStatus'] == 'Hit') ? $hashCaches++ : null; |
|
231
|
|
|
|
|
232
|
|
|
$packages['automatic-updater'] = $automaticFilesPackage; |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
$this->packages = $packages; |
|
238
|
|
|
$this->caching = array($cacheStatus, $hashCaches, $packagesTotal); |
|
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
return; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Get the MD5 or SHA256 hash |
|
245
|
|
|
* |
|
246
|
|
|
* @param string $packageName Package Filename |
|
247
|
|
|
* @param string $url Url to the package |
|
248
|
|
|
* @return array hash, hashType (md5 or sha356), hashCacheStatus (Hit or Miss) |
|
249
|
|
|
*/ |
|
250
|
|
|
private function getHash($packageName, $url) |
|
251
|
|
|
{ |
|
252
|
|
|
$cacheName = 'packages_hash' . $packageName; |
|
253
|
|
|
$hashType = ($this->branch == '3.0') ? 'md5' : 'sha256'; |
|
254
|
|
|
|
|
255
|
|
|
if ($this->cache->contains($cacheName) !== FALSE) |
|
256
|
|
|
{ |
|
257
|
|
|
// See if we've cached the hash before grabbing an external file |
|
258
|
|
|
$hash = $this->cache->fetch($cacheName); |
|
259
|
|
|
$hashCacheStatus = 'Hit'; |
|
260
|
|
|
} |
|
261
|
|
|
else |
|
262
|
|
|
{ |
|
263
|
|
|
// It seems we have no choice, grab the file from the external server |
|
264
|
|
|
$hash = @file_get_contents($url . '.' . $hashType); |
|
265
|
|
|
$this->cache->save($cacheName, $hash, 86400); |
|
266
|
|
|
$hashCacheStatus = 'Miss'; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
return array( |
|
270
|
|
|
'hash' => $hash, |
|
271
|
|
|
'hashType' => $hashType, |
|
272
|
|
|
'hashCacheStatus' => $hashCacheStatus |
|
273
|
|
|
); |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: