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

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!
 ******************************************************************/

/**
 * Service to handle mirrors
 */
31
class Mirror implements \TYPO3\CMS\Core\SingletonInterface
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
118
119
120
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
151
152
153
154
155
156
157
158
159
160
{

    /**
     * @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
     */
    public function getMirror($refresh = FALSE)
    {
        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
161
            $repository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_em_Repository', $emSettings['selectedRepository']);
162
            if ($repository->getMirrorListUrl()) {
163
                $repositoryUtility = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_em_Repository_Utility', $repository);
164
165
166
                $mirrors = $repositoryUtility->getMirrors(TRUE)->getMirrors();
                unset($repositoryUtility);
                if (!is_array($mirrors)) {
167
                    throw new \Exception('No mirrors found');
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
                }
            }

            // 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
     */
    public function getUrlToFile($filename)
    {
        if (empty($filename)) {
190
            throw new \Exception('No filename given to generate url');
191
192
193
194
195
196
197
198
        }

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

        // Check mirrors if file exits
        $attempts = 1;
        $maxAttempts = $this->getMaxAttempts();
199
        while (!\T3o\TerFe2\Utility\File::fileExists($mirrorUrl . $filename)) {
200
201
            $attempts++;
            if ($attempts > $maxAttempts) {
202
                // throw new \Exception('File "' . $filename . '" could not be found on ' . $maxAttempts . ' mirrors, break');
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
                // break;
                return '';
            }
            $mirrorUrl = $this->getMirror(TRUE);
        }

        return $mirrorUrl . $filename;
    }


    /**
     * Fetch a file from mirror server
     *
     * @param string $filename File name to fetch
     * @return string File content
     */
    public function getFile($filename)
    {
        $url = $this->getUrlToFile($filename);

223
224
        if (\T3o\TerFe2\Utility\File::isLocalUrl($url)) {
            $url = \T3o\TerFe2\Utility\File::getAbsolutePathFromUrl($url);
225
            $content = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($url);
226
        } else {
227
            $content = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($url, 0, array(TYPO3_user_agent));
228
229
230
        }

        if (empty($content)) {
231
            throw new \Exception('Could not fetch file "' . $filename . '" from mirror server');
232
233
234
235
236
237
        }

        return (string)$content;
    }

}