Mirror.php 6.08 KB
Newer Older
1
<?php
2
3
namespace T3o\TerFe2\Service;

4
5
/*
 * This file is part of the TYPO3 CMS project.
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.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16
17
18
19

/**
 * Service to handle mirrors
 */
20
class Mirror implements \TYPO3\CMS\Core\SingletonInterface
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{

    /**
     * @var integer
     */
    protected $maxAttempts = 2;

    /**
     * @var string
     */
    protected $protocol = 'http';

    /**
     * @var integer
     */
    protected $repositoryId = 1;

    /**
     * @var string
     */
    protected $mirrorUrl;


    /**
     * Setter for maxAttempts
     *
     * @param integer $maxAttempts Maximal count of attemts to connect
     * @return void
     */
    public function setMaxAttempts($maxAttempts)
    {
        $this->maxAttempts = (int)$maxAttempts;
    }


    /**
     * Getter for maxAttempts
     *
     * @return integer Maximal count of attemts to connect
     */
    public function getMaxAttempts()
    {
        return (int)$this->maxAttempts;
    }


    /**
     * Setter for protocol
     *
     * @param string $protocol Url protocol
     * @return void
     */
    public function setProtocol($protocol)
    {
        $this->protocol = $protocol;
    }


    /**
     * Getter for protocol
     *
     * @return string Url protocol
     */
    public function getProtocol()
    {
        return $this->protocol;
    }


    /**
     * Setter for repositoryId
     *
     * @param integer $repositoryId Repository id
     * @return void
     */
    public function setRepositoryId($repositoryId)
    {
        $this->repositoryId = (int)$repositoryId;
    }


    /**
     * Getter for repositoryId
     *
     * @return integer Repository id
     */
    public function getRepositoryId()
    {
        return (int)$this->repositoryId;
    }


    /**
     * Returns mirror url from local extension manager
     *
     * @param boolean $refresh Generate new mirror url
     * @return string Mirror url
118
     * @throws \Exception
119
     */
Markus Sommer's avatar
Markus Sommer committed
120
    public function getMirror($refresh = false)
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    {
        if (!$refresh && !empty($this->mirrorUrl)) {
            return $this->mirrorUrl;
        }

        // Get extension manager settings
        $emSettings = array(
            'rep_url' => '',
            'extMirrors' => '',
            'selectedRepository' => $this->getRepositoryId(),
            'selectedMirror' => 0,
        );
        if (!empty($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['em'])) {
            $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['em']);
            $emSettings = array_merge($emSettings, $extConf);
        }

        if (!empty($emSettings['rep_url'])) {
            // Force manually added url
            $mirrorUrl = $emSettings['rep_url'];
        } else {
            // Set selected repository to "1" if no mirrors found
            $mirrors = unserialize($emSettings['extMirrors']);
            if (!is_array($mirrors)) {
                if ($emSettings['selectedRepository'] < 1) {
                    $emSettings['selectedRepository'] = 1;
                }
            }

            // Get mirrors from repository object
151
            $repository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_em_Repository', $emSettings['selectedRepository']);
152
            if ($repository->getMirrorListUrl()) {
153
                $repositoryUtility = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_em_Repository_Utility', $repository);
Markus Sommer's avatar
Markus Sommer committed
154
                $mirrors = $repositoryUtility->getMirrors(true)->getMirrors();
155
156
                unset($repositoryUtility);
                if (!is_array($mirrors)) {
157
                    throw new \Exception('No mirrors found');
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
                }
            }

            // Build url
            $protocol = $this->getProtocol();
            $selectedMirror = (!empty($emSettings['selectedMirror']) ? $emSettings['selectedMirror'] : array_rand($mirrors));
            $mirrorUrl = rtrim($protocol, ':/') . '://' . $mirrors[$selectedMirror]['host'] . $mirrors[$selectedMirror]['path'];
        }

        return $this->mirrorUrl = rtrim($mirrorUrl, '/ ') . '/';
    }


    /**
     * Generate the url to a file on mirror server
     *
     * @param string $filename File name to check
     * @return string Url to file on mirror server
176
     * @throws \Exception
177
178
179
180
     */
    public function getUrlToFile($filename)
    {
        if (empty($filename)) {
181
            throw new \Exception('No filename given to generate url');
182
183
184
185
186
187
188
189
        }

        // Get first mirror url
        $mirrorUrl = $this->getMirror();

        // Check mirrors if file exits
        $attempts = 1;
        $maxAttempts = $this->getMaxAttempts();
190
        while (!\T3o\TerFe2\Utility\FileUtility::fileExists($mirrorUrl . $filename)) {
191
192
            $attempts++;
            if ($attempts > $maxAttempts) {
193
                // throw new \Exception('File "' . $filename . '" could not be found on ' . $maxAttempts . ' mirrors, break');
194
195
196
                // break;
                return '';
            }
Markus Sommer's avatar
Markus Sommer committed
197
            $mirrorUrl = $this->getMirror(true);
198
199
200
201
202
203
204
205
206
207
208
        }

        return $mirrorUrl . $filename;
    }


    /**
     * Fetch a file from mirror server
     *
     * @param string $filename File name to fetch
     * @return string File content
209
     * @throws \Exception
210
211
212
213
214
     */
    public function getFile($filename)
    {
        $url = $this->getUrlToFile($filename);

215
216
        if (\T3o\TerFe2\Utility\FileUtility::isLocalUrl($url)) {
            $url = \T3o\TerFe2\Utility\FileUtility::getAbsolutePathFromUrl($url);
217
            $content = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($url);
218
        } else {
219
            $content = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($url, 0, array(TYPO3_user_agent));
220
221
222
        }

        if (empty($content)) {
223
            throw new \Exception('Could not fetch file "' . $filename . '" from mirror server');
224
225
226
227
228
        }

        return (string)$content;
    }
}