MirrorProvider.php 4.71 KB
Newer Older
1
<?php
2 3
namespace T3o\TerFe2\Provider;

4 5
/*
 * This file is part of the TYPO3 CMS project.
flyguide's avatar
flyguide committed
6
 *
7 8 9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
flyguide's avatar
flyguide committed
10
 *
11 12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
flyguide's avatar
flyguide committed
13
 *
14 15
 * The TYPO3 project - inspiring people to share!
 */
flyguide's avatar
flyguide committed
16 17 18 19

/**
 * Extension provider using external mirror servers
 */
20
class MirrorProvider extends \T3o\TerFe2\Provider\FileProvider
21 22 23
{

    /**
24
     * @var \T3o\TerFe2\Service\MirrorService
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
     */
    protected $mirrorService;

    /**
     * @var string
     */
    protected $fileCachePath = 'typo3temp/tx_terfe2/files/';

    /**
     * Initialize provider
     */
    public function initializeProvider()
    {
        parent::initializeProvider();

        // Set local file cache path
        if (!empty($this->configuration['fileCachePath'])) {
            $this->fileCachePath = $this->configuration['fileCachePath'];
        }
44
        $this->fileCachePath = \T3o\TerFe2\Utility\FileUtility::getAbsoluteDirectory($this->fileCachePath);
45 46 47 48 49 50 51 52

        // Get repository id
        $repositoryId = 1;
        if (!empty($this->configuration['repositoryId'])) {
            $repositoryId = (int)$this->configuration['repositoryId'];
        }

        // Get mirror service
53
        $this->mirrorService = $this->objectManager->get(\T3o\TerFe2\Service\MirrorService::class);
54 55 56 57 58 59
        $this->mirrorService->setRepositoryId($repositoryId);
    }

    /**
     * Returns the url to an extension related file
     *
60
     * @param \T3o\TerFe2\Domain\Model\Version $version Version object
61 62 63
     * @param string $fileType File type
     * @return string Url to file
     */
64
    public function getFileUrl(\T3o\TerFe2\Domain\Model\Version $version, $fileType)
65 66 67 68 69 70 71 72 73 74
    {
        $filename = $this->getFileName($version, $fileType);
        $localName = '';

        // Get local filename for t3x files
        if (!empty($this->fileCachePath) && $fileType === 't3x') {
            $localName = $this->fileCachePath . basename($filename);
        }

        // Check local cache first
75 76
        if (!empty($localName) && $fileType === 't3x' && \T3o\TerFe2\Utility\FileUtility::fileExists($localName)) {
            return \T3o\TerFe2\Utility\FileUtility::getUrlFromAbsolutePath($localName);
77 78 79 80
        }

        // Get filename on mirror server
        $filename = $this->mirrorService->getUrlToFile($filename);
81 82
        if (\T3o\TerFe2\Utility\FileUtility::isLocalUrl($filename)) {
            $filename = \T3o\TerFe2\Utility\FileUtility::getAbsolutePathFromUrl($filename);
83 84 85 86
        }

        // Copy file to local cache and return it
        if (!empty($localName)) {
87 88
            \T3o\TerFe2\Utility\FileUtility::copyFile($filename, $localName);
            return \T3o\TerFe2\Utility\FileUtility::getUrlFromAbsolutePath($localName);
89 90 91
        }

        // Get local url from absolute path
92 93
        if (\T3o\TerFe2\Utility\FileUtility::isAbsolutePath($filename)) {
            return \T3o\TerFe2\Utility\FileUtility::getUrlFromAbsolutePath($filename);
94 95 96 97 98 99 100 101 102 103
        }

        return $filename;
    }

    /**
     * Returns the content of an ext_emconf.php file
     *
     * @param string $extension Extension key
     * @param string $version Version string
104
     * @param string $fileHash Hash of file
105
     * @return array Extension info array
106
     * @throws \Exception
107 108 109 110
     */
    protected function getExtensionInfo($extension, $version, $fileHash)
    {
        if (empty($extension) || empty($version) || empty($fileHash)) {
111
            throw new \Exception('Extension key, version and file hash are required to get extension info');
112 113 114 115 116 117 118 119 120
        }

        // Fetch file from server
        $filename = $this->generateFileName($extension, $version, 't3x');
        $content = $this->mirrorService->getFile($filename);
        $filesize = strlen($content);

        // Check file hash
        if ($fileHash !== md5($content)) {
121
            \T3o\TerFe2\Utility\LogUtility::addMessage('File hash missmatch of file "' . $filename . '"', 'ter_fe2', 2);
122
            return [];
123 124 125 126 127
        }

        // Write file to local cache
        if (!empty($this->fileCachePath)) {
            $localName = $this->fileCachePath . basename($filename);
128
            \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($localName, $content);
129 130 131
        }

        // Get EM_CONF array
132
        $extension = \T3o\TerFe2\Utility\ArchiveUtility::decompressT3xStream($content);
133
        $emConf = [];
134 135 136 137 138 139 140 141 142 143
        if (!empty($extension['EM_CONF'])) {
            $emConf = $extension['EM_CONF'];
        }
        unset($extension);

        // Add file size
        $emConf['t3xfilesize'] = (int)$filesize;

        return $emConf;
    }
flyguide's avatar
flyguide committed
144
}