TYPO3 CMS  TYPO3_6-2
Helper.php
Go to the documentation of this file.
1 <?php
3 
18 
26 
55  protected $repository = NULL;
56 
61 
66 
72  public function __construct() {
74  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
76  $repositoryRepository = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Repository\\RepositoryRepository');
77  $this->extensionRepository = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Repository\\ExtensionRepository');
79  $repository = $repositoryRepository->findByUid(1);
80  if (is_object($repository)) {
81  $this->setRepository($repository);
82  }
83  }
84 
95  public function setRepository(\TYPO3\CMS\Extensionmanager\Domain\Model\Repository $repository) {
96  $this->repository = $repository;
97  }
98 
109  public function fetchExtListFile() {
110  $this->fetchFile($this->getRemoteExtListFile(), $this->getLocalExtListFile());
111  }
112 
123  public function fetchMirrorListFile() {
124  $this->fetchFile($this->getRemoteMirrorListFile(), $this->getLocalMirrorListFile());
125  }
126 
137  protected function fetchFile($remoteResource, $localResource) {
138  if (is_string($remoteResource) && is_string($localResource) && !empty($remoteResource) && !empty($localResource)) {
139  $fileContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($remoteResource, 0, array(TYPO3_user_agent));
140  if ($fileContent !== FALSE) {
141  if (\TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($localResource, $fileContent) === FALSE) {
142  throw new ExtensionManagerException(sprintf('Could not write to file %s.', htmlspecialchars($localResource)), 1342635378);
143  }
144  } else {
145  throw new ExtensionManagerException(sprintf('Could not access remote resource %s.', htmlspecialchars($remoteResource)), 1342635425);
146  }
147  }
148  }
149 
157  public function getLocalExtListFile() {
158  $absFilePath = PATH_site . 'typo3temp/' . (int)$this->repository->getUid() . '.extensions.xml.gz';
159  return $absFilePath;
160  }
161 
169  public function getRemoteExtListFile() {
170  $mirror = $this->getMirrors(TRUE)->getMirror();
171  $filePath = 'https://' . $mirror['host'] . $mirror['path'] . 'extensions.xml.gz';
172  return $filePath;
173  }
174 
182  public function getRemoteExtHashFile() {
183  $mirror = $this->getMirrors(TRUE)->getMirror();
184  $filePath = 'https://' . $mirror['host'] . $mirror['path'] . 'extensions.md5';
185  return $filePath;
186  }
187 
195  public function getLocalMirrorListFile() {
196  $absFilePath = PATH_site . 'typo3temp/' . (int)$this->repository->getUid() . '.mirrors.xml.gz';
197  return $absFilePath;
198  }
199 
207  public function getRemoteMirrorListFile() {
208  $filePath = $this->repository->getMirrorListUrl();
209  return $filePath;
210  }
211 
224  public function getMirrors($forcedUpdateFromRemote = TRUE) {
225  $assignedMirror = $this->repository->getMirrors();
226  if ($forcedUpdateFromRemote || is_null($assignedMirror) || !is_object($assignedMirror)) {
227  if ($forcedUpdateFromRemote || !is_file($this->getLocalMirrorListFile())) {
228  $this->fetchMirrorListFile();
229  }
231  $objMirrorListImporter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extensionmanager\\Utility\\Importer\\MirrorListUtility');
232  $this->repository->addMirrors($objMirrorListImporter->getMirrors($this->getLocalMirrorListFile()));
233  }
234  return $this->repository->getMirrors();
235  }
236 
246  public function isExtListUpdateNecessary() {
247  $updateNecessity = 0;
248  if ($this->extensionRepository->countByRepository($this->repository->getUid()) <= 0) {
249  $updateNecessity |= self::PROBLEM_NO_VERSIONS_IN_DATABASE;
250  }
251  if (!is_file($this->getLocalExtListFile())) {
252  $updateNecessity |= self::PROBLEM_EXTENSION_FILE_NOT_EXISTING;
253  } else {
254  $remotemd5 = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($this->getRemoteExtHashFile(), 0, array(TYPO3_user_agent));
255  if ($remotemd5 !== FALSE) {
256  $localmd5 = md5_file($this->getLocalExtListFile());
257  if ($remotemd5 !== $localmd5) {
258  $updateNecessity |= self::PROBLEM_EXTENSION_HASH_CHANGED;
259  }
260  } else {
261  throw new ExtensionManagerException('Could not retrieve extension hash file from remote server.', 1342635016);
262  }
263  }
264  return $updateNecessity;
265  }
266 
275  public function updateExtList() {
276  $updated = FALSE;
277  $updateNecessity = $this->isExtListUpdateNecessary();
278  if ($updateNecessity !== 0) {
279  // retrieval of file necessary
280  $tmpBitmask = self::PROBLEM_EXTENSION_FILE_NOT_EXISTING | self::PROBLEM_EXTENSION_HASH_CHANGED;
281  if (($tmpBitmask & $updateNecessity) > 0) {
282  $this->fetchExtListFile();
283  $updateNecessity &= ~$tmpBitmask;
284  }
285  // database table cleanup
286  if ($updateNecessity & self::PROBLEM_NO_VERSIONS_IN_DATABASE) {
287  $updateNecessity &= ~self::PROBLEM_NO_VERSIONS_IN_DATABASE;
288  } else {
289  // Use straight query as extbase "remove" is too slow here
290  // This truncates the whole table. It would be more correct to remove only rows of a specific
291  // repository, but multiple repository handling is not implemented, and truncate is quicker.
292  $this->getDatabaseConnection()->exec_TRUNCATEquery('tx_extensionmanager_domain_model_extension');
293  }
294  // no further problems - start of import process
295  if ($updateNecessity === 0) {
296  $uid = $this->repository->getUid();
297  /* @var $objExtListImporter \TYPO3\CMS\Extensionmanager\Utility\Importer\ExtensionListUtility */
298  $objExtListImporter = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\Importer\\ExtensionListUtility');
299  $objExtListImporter->import($this->getLocalExtListFile(), $uid);
300  $updated = TRUE;
301  }
302  }
303  return $updated;
304  }
305 
311  protected function getDatabaseConnection() {
312  return $GLOBALS['TYPO3_DB'];
313  }
314 
315 }
fetchFile($remoteResource, $localResource)
Definition: Helper.php:137
static writeFile($file, $content, $changePermissions=FALSE)
$uid
Definition: server.php:36
static getUrl($url, $includeHeader=0, $requestHeaders=FALSE, &$report=NULL)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
setRepository(\TYPO3\CMS\Extensionmanager\Domain\Model\Repository $repository)
Definition: Helper.php:95