TYPO3 CMS  TYPO3_7-6
Helper.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 {
53  protected $repository = null;
54 
59 
64 
69 
75  public function __construct()
76  {
78  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
80  $repositoryRepository = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Domain\Repository\RepositoryRepository::class);
81  $this->extensionRepository = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository::class);
82  $this->configurationUtility = $this->objectManager->get(ConfigurationUtility::class);
84  $repository = $repositoryRepository->findByUid(1);
85  if (is_object($repository)) {
86  $this->setRepository($repository);
87  }
88  }
89 
100  public function setRepository(\TYPO3\CMS\Extensionmanager\Domain\Model\Repository $repository)
101  {
102  $this->repository = $repository;
103  }
104 
115  public function fetchExtListFile()
116  {
117  $this->fetchFile($this->getRemoteExtListFile(), $this->getLocalExtListFile());
118  }
119 
130  public function fetchMirrorListFile()
131  {
132  $this->fetchFile($this->getRemoteMirrorListFile(), $this->getLocalMirrorListFile());
133  }
134 
145  protected function fetchFile($remoteResource, $localResource)
146  {
147  if ($this->configurationUtility->getCurrentConfiguration('extensionmanager')['offlineMode']['value']) {
148  throw new ExtensionManagerException('Extension Manager is in offline mode. No TER connection available.', 1437078780);
149  }
150  if (is_string($remoteResource) && is_string($localResource) && !empty($remoteResource) && !empty($localResource)) {
151  $fileContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($remoteResource, 0, [TYPO3_user_agent]);
152  if ($fileContent !== false) {
153  if (\TYPO3\CMS\Core\Utility\GeneralUtility::writeFileToTypo3tempDir($localResource, $fileContent) !== null) {
154  throw new ExtensionManagerException(sprintf('Could not write to file %s.', $localResource), 1342635378);
155  }
156  } else {
157  throw new ExtensionManagerException(sprintf('Could not access remote resource %s.', $remoteResource), 1342635425);
158  }
159  }
160  }
161 
169  public function getLocalExtListFile()
170  {
171  $absFilePath = PATH_site . 'typo3temp/ExtensionManager/' . (int)$this->repository->getUid() . '.extensions.xml.gz';
172  return $absFilePath;
173  }
174 
182  public function getRemoteExtListFile()
183  {
184  $mirror = $this->getMirrors(true)->getMirror();
185  $filePath = 'https://' . $mirror['host'] . $mirror['path'] . 'extensions.xml.gz';
186  return $filePath;
187  }
188 
196  public function getRemoteExtHashFile()
197  {
198  $mirror = $this->getMirrors(true)->getMirror();
199  $filePath = 'https://' . $mirror['host'] . $mirror['path'] . 'extensions.md5';
200  return $filePath;
201  }
202 
210  public function getLocalMirrorListFile()
211  {
212  $absFilePath = PATH_site . 'typo3temp/ExtensionManager/' . (int)$this->repository->getUid() . '.mirrors.xml.gz';
213  return $absFilePath;
214  }
215 
223  public function getRemoteMirrorListFile()
224  {
225  $filePath = $this->repository->getMirrorListUrl();
226  return $filePath;
227  }
228 
241  public function getMirrors($forcedUpdateFromRemote = true)
242  {
243  $assignedMirror = $this->repository->getMirrors();
244  if ($forcedUpdateFromRemote || is_null($assignedMirror) || !is_object($assignedMirror)) {
245  if ($forcedUpdateFromRemote || !is_file($this->getLocalMirrorListFile())) {
246  $this->fetchMirrorListFile();
247  }
249  $objMirrorListImporter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extensionmanager\Utility\Importer\MirrorListUtility::class);
250  $this->repository->addMirrors($objMirrorListImporter->getMirrors($this->getLocalMirrorListFile()));
251  }
252  return $this->repository->getMirrors();
253  }
254 
264  public function isExtListUpdateNecessary()
265  {
266  $updateNecessity = 0;
267  if ($this->extensionRepository->countByRepository($this->repository->getUid()) <= 0) {
268  $updateNecessity |= self::PROBLEM_NO_VERSIONS_IN_DATABASE;
269  }
270  if (!is_file($this->getLocalExtListFile())) {
271  $updateNecessity |= self::PROBLEM_EXTENSION_FILE_NOT_EXISTING;
272  } else {
273  $remotemd5 = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($this->getRemoteExtHashFile(), 0, [TYPO3_user_agent]);
274  if ($remotemd5 !== false) {
275  $localmd5 = md5_file($this->getLocalExtListFile());
276  if ($remotemd5 !== $localmd5) {
277  $updateNecessity |= self::PROBLEM_EXTENSION_HASH_CHANGED;
278  }
279  } else {
280  throw new ExtensionManagerException('Could not retrieve extension hash file from remote server.', 1342635016);
281  }
282  }
283  return $updateNecessity;
284  }
285 
294  public function updateExtList()
295  {
296  $updated = false;
297  $updateNecessity = $this->isExtListUpdateNecessary();
298  if ($updateNecessity !== 0) {
299  // retrieval of file necessary
300  $tmpBitmask = self::PROBLEM_EXTENSION_FILE_NOT_EXISTING | self::PROBLEM_EXTENSION_HASH_CHANGED;
301  if (($tmpBitmask & $updateNecessity) > 0) {
302  $this->fetchExtListFile();
303  $updateNecessity &= ~$tmpBitmask;
304  }
305  // database table cleanup
306  if ($updateNecessity & self::PROBLEM_NO_VERSIONS_IN_DATABASE) {
307  $updateNecessity &= ~self::PROBLEM_NO_VERSIONS_IN_DATABASE;
308  } else {
309  // Use straight query as extbase "remove" is too slow here
310  // This truncates the whole table. It would be more correct to remove only rows of a specific
311  // repository, but multiple repository handling is not implemented, and truncate is quicker.
312  $this->getDatabaseConnection()->exec_TRUNCATEquery('tx_extensionmanager_domain_model_extension');
313  }
314  // no further problems - start of import process
315  if ($updateNecessity === 0) {
316  $uid = $this->repository->getUid();
317  /* @var $objExtListImporter \TYPO3\CMS\Extensionmanager\Utility\Importer\ExtensionListUtility */
318  $objExtListImporter = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Utility\Importer\ExtensionListUtility::class);
319  $objExtListImporter->import($this->getLocalExtListFile(), $uid);
320  $updated = true;
321  }
322  }
323  return $updated;
324  }
325 
331  protected function getDatabaseConnection()
332  {
333  return $GLOBALS['TYPO3_DB'];
334  }
335 }
fetchFile($remoteResource, $localResource)
Definition: Helper.php:145
static writeFileToTypo3tempDir($filepath, $content)
static getUrl($url, $includeHeader=0, $requestHeaders=false, &$report=null)
$uid
Definition: server.php:38
setRepository(\TYPO3\CMS\Extensionmanager\Domain\Model\Repository $repository)
Definition: Helper.php:100
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']