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

flyguide's avatar
flyguide committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*******************************************************************
 *  Copyright notice
 *
 *  (c) 2011 Kai Vogel <kai.vogel@speedprogs.de>, Speedprogs.de
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ******************************************************************/

/**
 * Extension provider using external mirror servers
 */
31
class MirrorProvider extends \T3o\TerFe2\Provider\FileProvider
32
33
34
{

    /**
35
     * @var \T3o\TerFe2\Service\Mirror
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
     */
    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'];
        }
58
        $this->fileCachePath = \T3o\TerFe2\Utility\File::getAbsoluteDirectory($this->fileCachePath);
59
60
61
62
63
64
65
66

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

        // Get mirror service
67
        $this->mirrorService = $this->objectManager->get(\T3o\TerFe2\Service\Mirror::class);
68
69
70
71
72
73
74
        $this->mirrorService->setRepositoryId($repositoryId);
    }


    /**
     * Returns the url to an extension related file
     *
75
     * @param \T3o\TerFe2\Domain\Model\Version $version Version object
76
77
78
     * @param string $fileType File type
     * @return string Url to file
     */
79
    public function getFileUrl(\T3o\TerFe2\Domain\Model\Version $version, $fileType)
80
81
82
83
84
85
86
87
88
89
    {
        $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
90
91
        if (!empty($localName) && $fileType === 't3x' && \T3o\TerFe2\Utility\File::fileExists($localName)) {
            return \T3o\TerFe2\Utility\File::getUrlFromAbsolutePath($localName);
92
93
94
95
        }

        // Get filename on mirror server
        $filename = $this->mirrorService->getUrlToFile($filename);
96
97
        if (\T3o\TerFe2\Utility\File::isLocalUrl($filename)) {
            $filename = \T3o\TerFe2\Utility\File::getAbsolutePathFromUrl($filename);
98
99
100
101
        }

        // Copy file to local cache and return it
        if (!empty($localName)) {
102
103
            \T3o\TerFe2\Utility\File::copyFile($filename, $localName);
            return \T3o\TerFe2\Utility\File::getUrlFromAbsolutePath($localName);
104
105
106
        }

        // Get local url from absolute path
107
108
        if (\T3o\TerFe2\Utility\File::isAbsolutePath($filename)) {
            return \T3o\TerFe2\Utility\File::getUrlFromAbsolutePath($filename);
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
        }

        return $filename;
    }


    /**
     * Returns the content of an ext_emconf.php file
     *
     * @param string $extension Extension key
     * @param string $version Version string
     * @return array Extension info array
     */
    protected function getExtensionInfo($extension, $version, $fileHash)
    {
        if (empty($extension) || empty($version) || empty($fileHash)) {
125
            throw new \Exception('Extension key, version and file hash are required to get extension info');
126
127
128
129
130
131
132
133
134
        }

        // 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)) {
135
            \T3o\TerFe2\Utility\Log::addMessage('File hash missmatch of file "' . $filename . '"', 'ter_fe2', 2);
136
137
138
139
140
141
            return array();
        }

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

        // Get EM_CONF array
146
        $extension = \T3o\TerFe2\Utility\Archive::decompressT3xStream($content);
147
148
149
150
151
152
153
154
155
156
157
        $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
158
159

}