TYPO3 CMS  TYPO3_7-6
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 
19 
24 {
28  protected $objectManager;
29 
33  protected $registry;
34 
40  protected $downloadBaseUri;
41 
45  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
46  {
47  $this->objectManager = $objectManager;
48  }
49 
53  public function injectRegistry(\TYPO3\CMS\Core\Registry $registry)
54  {
55  $this->registry = $registry;
56  }
57 
61  public function getDownloadBaseUri()
62  {
64  }
65 
69  public function __construct()
70  {
71  $this->downloadBaseUri = 'https://get.typo3.org/';
72  }
73 
80  public function updateVersionMatrix()
81  {
82  $versionArray = $this->fetchVersionMatrixFromRemote();
83  $installedMajorVersion = (int)$this->getInstalledMajorVersion();
84 
85  foreach ($versionArray as $versionNumber => $versionDetails) {
86  if (is_array($versionDetails) && (int)$this->getMajorVersion($versionNumber) < $installedMajorVersion) {
87  unset($versionArray[$versionNumber]);
88  }
89  }
90 
91  $this->registry->set('TYPO3.CMS.Install', 'coreVersionMatrix', $versionArray);
92  }
93 
101  {
102  $version = $this->getInstalledVersion();
103  return substr($version, -4) !== '-dev';
104  }
105 
113  public function getTarGzSha1OfVersion($version)
114  {
115  $this->ensureVersionExistsInMatrix($version);
116 
117  $majorVersion = $this->getMajorVersion($version);
118  $versionMatrix = $this->getVersionMatrix();
119 
120  if (empty($versionMatrix[$majorVersion]['releases'][$version]['checksums']['tar']['sha1'])) {
121  throw new Exception\CoreVersionServiceException(
122  'Release sha1 of version ' . $version . ' not found in version matrix.'
123  . ' This is probably a bug on get.typo3.org.',
124  1381263173
125  );
126  }
127 
128  return $versionMatrix[$majorVersion]['releases'][$version]['checksums']['tar']['sha1'];
129  }
130 
136  public function getInstalledVersion()
137  {
139  }
140 
146  public function isVersionActivelyMaintained()
147  {
148  $majorVersion = $this->getInstalledMajorVersion();
149  $versionMatrix = $this->getVersionMatrix();
150  return (bool)$versionMatrix[$majorVersion]['active'];
151  }
152 
159  {
160  $version = $this->getInstalledVersion();
161  $youngestVersion = $this->getYoungestPatchRelease();
162  return $youngestVersion !== $version;
163  }
164 
171  {
172  $result = false;
173  $version = $this->getInstalledVersion();
174  $youngestVersion = $this->getYoungestPatchDevelopmentRelease();
175  if ($youngestVersion !== $version) {
176  $result = true;
177  }
178  return $result;
179  }
180 
186  public function isUpdateSecurityRelevant()
187  {
188  $result = false;
189  $version = $this->getInstalledVersion();
190  $youngestVersion = $this->getYoungestReleaseByType(['security']);
191  if ($youngestVersion !== $version) {
192  $result = true;
193  }
194  return $result;
195  }
196 
202  public function getYoungestPatchRelease()
203  {
204  return $this->getYoungestReleaseByType(['release', 'security', 'regular']);
205  }
206 
213  {
214  return $this->getYoungestReleaseByType(['release', 'security', 'regular', 'development']);
215  }
216 
225  protected function getYoungestReleaseByType(array $types)
226  {
227  $version = $this->getInstalledVersion();
228 
229  $majorVersion = $this->getMajorVersion($version);
230  $versionMatrix = $this->getVersionMatrix();
231 
232  $youngestRelease = $version;
233  $versionReleaseTimestamp = $this->getReleaseTimestampOfVersion($version);
234 
235  $patchLevelVersions = $versionMatrix[$majorVersion]['releases'];
236  foreach ($patchLevelVersions as $aVersionNumber => $aVersionDetails) {
237  if (!array_key_exists('type', $aVersionDetails)) {
238  throw new Exception\CoreVersionServiceException(
239  'Release type of version ' . $aVersionNumber . ' not found in version matrix.'
240  . ' This is probably a bug on get.typo3.org.',
241  1380909029
242  );
243  }
244  $type = $aVersionDetails['type'];
245  $aVersionNumberReleaseTimestamp = $this->getReleaseTimestampOfVersion($aVersionNumber);
246  if (
247  $aVersionNumberReleaseTimestamp > $versionReleaseTimestamp
248  && in_array($type, $types)
249  ) {
250  $youngestRelease = $aVersionNumber;
251  $versionReleaseTimestamp = $aVersionNumberReleaseTimestamp;
252  }
253  }
254  return $youngestRelease;
255  }
256 
262  protected function getInstalledMajorVersion()
263  {
264  return $this->getMajorVersion($this->getInstalledVersion());
265  }
266 
273  protected function getMajorVersion($version)
274  {
275  $explodedVersion = explode('.', $version);
276  return $explodedVersion[0];
277  }
278 
285  protected function getVersionMatrix()
286  {
287  $versionMatrix = $this->registry->get('TYPO3.CMS.Install', 'coreVersionMatrix');
288  if (empty($versionMatrix) || !is_array($versionMatrix)) {
289  throw new Exception\CoreVersionServiceException(
290  'No version matrix found in registry, call updateVersionMatrix() first.',
291  1380898792
292  );
293  }
294  return $versionMatrix;
295  }
296 
303  protected function fetchVersionMatrixFromRemote()
304  {
305  $url = $this->downloadBaseUri . 'json';
306  $versionJson = GeneralUtility::getUrl($url);
307  if (!$versionJson) {
308  throw new Exception\RemoteFetchException(
309  'Fetching ' . $url . ' failed. Maybe this instance can not connect to the remote system properly.',
310  1380897593
311  );
312  }
313  return json_decode($versionJson, true);
314  }
315 
323  protected function getReleaseTimestampOfVersion($version)
324  {
325  $majorVersion = $this->getMajorVersion($version);
326  $versionMatrix = $this->getVersionMatrix();
327  $this->ensureVersionExistsInMatrix($version);
328  if (!array_key_exists('date', $versionMatrix[$majorVersion]['releases'][$version])) {
329  throw new Exception\CoreVersionServiceException(
330  'Release date of version ' . $version . ' not found in version matrix. This is probably a bug on get.typo3.org',
331  1380905853
332  );
333  }
334  $dateString = $versionMatrix[$majorVersion]['releases'][$version]['date'];
335  $date = new \DateTime($dateString);
336  return $date->getTimestamp();
337  }
338 
345  protected function ensureVersionExistsInMatrix($version)
346  {
347  $majorVersion = $this->getMajorVersion($version);
348  $versionMatrix = $this->getVersionMatrix();
349  if (!array_key_exists($majorVersion, $versionMatrix)) {
350  throw new Exception\CoreVersionServiceException(
351  'Major release ' . $majorVersion . ' not found in version matrix.',
352  1380905851
353  );
354  }
355  if (!array_key_exists($version, $versionMatrix[$majorVersion]['releases'])) {
356  throw new Exception\CoreVersionServiceException(
357  'Patch level release ' . $version . ' not found in version matrix.',
358  1380905852
359  );
360  }
361  }
362 }
injectRegistry(\TYPO3\CMS\Core\Registry $registry)
static getUrl($url, $includeHeader=0, $requestHeaders=false, &$report=null)
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)