TYPO3 CMS  TYPO3_8-7
CoreVersionService.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 
25 {
29  protected $registry;
30 
36  protected $downloadBaseUri;
37 
43  public function __construct(Registry $registry = null)
44  {
45  $this->downloadBaseUri = 'https://get.typo3.org/';
46  $this->registry = $registry ?: GeneralUtility::makeInstance(Registry::class);
47  }
48 
52  public function getDownloadBaseUri()
53  {
55  }
56 
62  public function updateVersionMatrix()
63  {
64  $versionArray = $this->fetchVersionMatrixFromRemote();
65  $installedMajorVersion = (int)$this->getInstalledMajorVersion();
66 
67  foreach ($versionArray as $versionNumber => $versionDetails) {
68  if (is_array($versionDetails) && (int)$this->getMajorVersion($versionNumber) < $installedMajorVersion) {
69  unset($versionArray[$versionNumber]);
70  }
71  }
72 
73  $this->registry->set('TYPO3.CMS.Install', 'coreVersionMatrix', $versionArray);
74  }
75 
83  {
84  $version = $this->getInstalledVersion();
85  return substr($version, -4) !== '-dev';
86  }
87 
95  public function getTarGzSha1OfVersion($version)
96  {
97  $this->ensureVersionExistsInMatrix($version);
98 
99  $majorVersion = $this->getMajorVersion($version);
100  $versionMatrix = $this->getVersionMatrix();
101 
102  if (empty($versionMatrix[$majorVersion]['releases'][$version]['checksums']['tar']['sha1'])) {
103  throw new Exception\CoreVersionServiceException(
104  'Release sha1 of version ' . $version . ' not found in version matrix.'
105  . ' This is probably a bug on get.typo3.org.',
106  1381263173
107  );
108  }
109 
110  return $versionMatrix[$majorVersion]['releases'][$version]['checksums']['tar']['sha1'];
111  }
112 
118  public function getInstalledVersion()
119  {
121  }
122 
128  public function isVersionActivelyMaintained()
129  {
130  $majorVersion = $this->getInstalledMajorVersion();
131  $versionMatrix = $this->getVersionMatrix();
132  return (bool)$versionMatrix[$majorVersion]['active'];
133  }
134 
141  {
142  $version = $this->getInstalledVersion();
143  $youngestVersion = $this->getYoungestPatchRelease();
144  return $youngestVersion !== $version;
145  }
146 
153  {
154  $result = false;
155  $version = $this->getInstalledVersion();
156  $youngestVersion = $this->getYoungestPatchDevelopmentRelease();
157  if ($youngestVersion !== $version) {
158  $result = true;
159  }
160  return $result;
161  }
162 
168  public function isUpdateSecurityRelevant()
169  {
170  $result = false;
171  $version = $this->getInstalledVersion();
172  $youngestVersion = $this->getYoungestReleaseByType(['security']);
173  if ($youngestVersion !== $version) {
174  $result = true;
175  }
176  return $result;
177  }
178 
184  public function getYoungestPatchRelease()
185  {
186  return $this->getYoungestReleaseByType(['release', 'security', 'regular']);
187  }
188 
195  {
196  return $this->getYoungestReleaseByType(['release', 'security', 'regular', 'development']);
197  }
198 
207  protected function getYoungestReleaseByType(array $types)
208  {
209  $version = $this->getInstalledVersion();
210 
211  $majorVersion = $this->getMajorVersion($version);
212  $versionMatrix = $this->getVersionMatrix();
213 
214  $youngestRelease = $version;
215  $versionReleaseTimestamp = $this->getReleaseTimestampOfVersion($version);
216 
217  $patchLevelVersions = $versionMatrix[$majorVersion]['releases'];
218  foreach ($patchLevelVersions as $aVersionNumber => $aVersionDetails) {
219  if (!array_key_exists('type', $aVersionDetails)) {
220  throw new Exception\CoreVersionServiceException(
221  'Release type of version ' . $aVersionNumber . ' not found in version matrix.'
222  . ' This is probably a bug on get.typo3.org.',
223  1380909029
224  );
225  }
226  $type = $aVersionDetails['type'];
227  $aVersionNumberReleaseTimestamp = $this->getReleaseTimestampOfVersion($aVersionNumber);
228  if (
229  $aVersionNumberReleaseTimestamp > $versionReleaseTimestamp
230  && in_array($type, $types)
231  ) {
232  $youngestRelease = $aVersionNumber;
233  $versionReleaseTimestamp = $aVersionNumberReleaseTimestamp;
234  }
235  }
236  return $youngestRelease;
237  }
238 
244  protected function getInstalledMajorVersion()
245  {
246  return $this->getMajorVersion($this->getInstalledVersion());
247  }
248 
255  protected function getMajorVersion($version)
256  {
257  $explodedVersion = explode('.', $version);
258  return $explodedVersion[0];
259  }
260 
267  protected function getVersionMatrix()
268  {
269  $versionMatrix = $this->registry->get('TYPO3.CMS.Install', 'coreVersionMatrix');
270  if (empty($versionMatrix) || !is_array($versionMatrix)) {
271  throw new Exception\CoreVersionServiceException(
272  'No version matrix found in registry, call updateVersionMatrix() first.',
273  1380898792
274  );
275  }
276  return $versionMatrix;
277  }
278 
285  protected function fetchVersionMatrixFromRemote()
286  {
287  $url = $this->downloadBaseUri . 'json';
288  $versionJson = GeneralUtility::getUrl($url);
289  if (!$versionJson) {
290  throw new Exception\RemoteFetchException(
291  'Fetching ' . $url . ' failed. Maybe this instance can not connect to the remote system properly.',
292  1380897593
293  );
294  }
295  return json_decode($versionJson, true);
296  }
297 
305  protected function getReleaseTimestampOfVersion($version)
306  {
307  $majorVersion = $this->getMajorVersion($version);
308  $versionMatrix = $this->getVersionMatrix();
309  $this->ensureVersionExistsInMatrix($version);
310  if (!array_key_exists('date', $versionMatrix[$majorVersion]['releases'][$version])) {
311  throw new Exception\CoreVersionServiceException(
312  'Release date of version ' . $version . ' not found in version matrix. This is probably a bug on get.typo3.org',
313  1380905853
314  );
315  }
316  $dateString = $versionMatrix[$majorVersion]['releases'][$version]['date'];
317  $date = new \DateTime($dateString);
318  return $date->getTimestamp();
319  }
320 
327  protected function ensureVersionExistsInMatrix($version)
328  {
329  $majorVersion = $this->getMajorVersion($version);
330  $versionMatrix = $this->getVersionMatrix();
331  if (!array_key_exists($majorVersion, $versionMatrix)) {
332  throw new Exception\CoreVersionServiceException(
333  'Major release ' . $majorVersion . ' not found in version matrix.',
334  1380905851
335  );
336  }
337  if (!array_key_exists($version, $versionMatrix[$majorVersion]['releases'])) {
338  throw new Exception\CoreVersionServiceException(
339  'Patch level release ' . $version . ' not found in version matrix.',
340  1380905852
341  );
342  }
343  }
344 }
static makeInstance($className,... $constructorArguments)