MirrorProvider.php 4.74 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\Mirror
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
     */
    protected $mirrorService;

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


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

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

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

        // Get mirror service
56
        $this->mirrorService = $this->objectManager->get(\T3o\TerFe2\Service\Mirror::class);
57
58
59
60
61
62
63
        $this->mirrorService->setRepositoryId($repositoryId);
    }


    /**
     * Returns the url to an extension related file
     *
64
     * @param \T3o\TerFe2\Domain\Model\Version $version Version object
65
66
67
     * @param string $fileType File type
     * @return string Url to file
     */
68
    public function getFileUrl(\T3o\TerFe2\Domain\Model\Version $version, $fileType)
69
70
71
72
73
74
75
76
77
78
    {
        $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
79
80
        if (!empty($localName) && $fileType === 't3x' && \T3o\TerFe2\Utility\FileUtility::fileExists($localName)) {
            return \T3o\TerFe2\Utility\FileUtility::getUrlFromAbsolutePath($localName);
81
82
83
84
        }

        // Get filename on mirror server
        $filename = $this->mirrorService->getUrlToFile($filename);
85
86
        if (\T3o\TerFe2\Utility\FileUtility::isLocalUrl($filename)) {
            $filename = \T3o\TerFe2\Utility\FileUtility::getAbsolutePathFromUrl($filename);
87
88
89
90
        }

        // Copy file to local cache and return it
        if (!empty($localName)) {
91
92
            \T3o\TerFe2\Utility\FileUtility::copyFile($filename, $localName);
            return \T3o\TerFe2\Utility\FileUtility::getUrlFromAbsolutePath($localName);
93
94
95
        }

        // Get local url from absolute path
96
97
        if (\T3o\TerFe2\Utility\FileUtility::isAbsolutePath($filename)) {
            return \T3o\TerFe2\Utility\FileUtility::getUrlFromAbsolutePath($filename);
98
99
100
101
102
103
104
105
106
107
108
        }

        return $filename;
    }


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

        // 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)) {
126
            \T3o\TerFe2\Utility\LogUtility::addMessage('File hash missmatch of file "' . $filename . '"', 'ter_fe2', 2);
127
128
129
130
131
132
            return array();
        }

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

        // Get EM_CONF array
137
        $extension = \T3o\TerFe2\Utility\ArchiveUtility::decompressT3xStream($content);
138
139
140
141
142
143
144
145
146
147
148
        $emConf = array();
        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
149
}