Subversion Repositories Applications.gtt

Rev

Rev 94 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 187
Line 2... Line 2...
2
/**
2
/**
3
 * PEAR_REST
3
 * PEAR_REST
4
 *
4
 *
5
 * PHP versions 4 and 5
5
 * PHP versions 4 and 5
6
 *
6
 *
7
 * LICENSE: This source file is subject to version 3.0 of the PHP license
-
 
8
 * that is available through the world-wide-web at the following URI:
-
 
9
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
-
 
10
 * the PHP License and are unable to obtain it through the web, please
-
 
11
 * send a note to license@php.net so we can mail you a copy immediately.
-
 
12
 *
-
 
13
 * @category   pear
7
 * @category   pear
14
 * @package    PEAR
8
 * @package    PEAR
15
 * @author     Greg Beaver <cellog@php.net>
9
 * @author     Greg Beaver <cellog@php.net>
16
 * @copyright  1997-2006 The PHP Group
10
 * @copyright  1997-2009 The Authors
17
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
11
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
18
 * @version    CVS: $Id: REST.php,v 1.21 2006/03/27 04:33:11 cellog Exp $
-
 
19
 * @link       http://pear.php.net/package/PEAR
12
 * @link       http://pear.php.net/package/PEAR
20
 * @since      File available since Release 1.4.0a1
13
 * @since      File available since Release 1.4.0a1
21
 */
14
 */
Line 22... Line 15...
22
 
15
 
Line 30... Line 23...
30
 * Intelligently retrieve data, following hyperlinks if necessary, and re-directing
23
 * Intelligently retrieve data, following hyperlinks if necessary, and re-directing
31
 * as well
24
 * as well
32
 * @category   pear
25
 * @category   pear
33
 * @package    PEAR
26
 * @package    PEAR
34
 * @author     Greg Beaver <cellog@php.net>
27
 * @author     Greg Beaver <cellog@php.net>
35
 * @copyright  1997-2006 The PHP Group
28
 * @copyright  1997-2009 The Authors
36
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
29
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
37
 * @version    Release: 1.5.1
30
 * @version    Release: 1.10.1
38
 * @link       http://pear.php.net/package/PEAR
31
 * @link       http://pear.php.net/package/PEAR
39
 * @since      Class available since Release 1.4.0a1
32
 * @since      Class available since Release 1.4.0a1
40
 */
33
 */
41
class PEAR_REST
34
class PEAR_REST
42
{
35
{
43
    var $config;
36
    var $config;
44
    var $_options;
37
    var $_options;
-
 
38
 
45
    function PEAR_REST(&$config, $options = array())
39
    function __construct(&$config, $options = array())
46
    {
40
    {
47
        $this->config = &$config;
41
        $this->config   = &$config;
48
        $this->_options = $options;
42
        $this->_options = $options;
49
    }
43
    }
Line 50... Line 44...
50
 
44
 
51
    /**
45
    /**
Line 57... Line 51...
57
     * @param array|false contents of the accept-encoding header
51
     * @param array|false contents of the accept-encoding header
58
     * @param boolean     if true, xml will be returned as a string, otherwise, xml will be
52
     * @param boolean     if true, xml will be returned as a string, otherwise, xml will be
59
     *                    parsed using PEAR_XMLParser
53
     *                    parsed using PEAR_XMLParser
60
     * @return string|array
54
     * @return string|array
61
     */
55
     */
62
    function retrieveCacheFirst($url, $accept = false, $forcestring = false)
56
    function retrieveCacheFirst($url, $accept = false, $forcestring = false, $channel = false)
63
    {
57
    {
64
        $cachefile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
58
        $cachefile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
65
            md5($url) . 'rest.cachefile';
59
            md5($url) . 'rest.cachefile';
-
 
60
 
66
        if (file_exists($cachefile)) {
61
        if (file_exists($cachefile)) {
67
            return unserialize(implode('', file($cachefile)));
62
            return unserialize(implode('', file($cachefile)));
68
        }
63
        }
-
 
64
 
69
        return $this->retrieveData($url, $accept, $forcestring);
65
        return $this->retrieveData($url, $accept, $forcestring, $channel);
70
    }
66
    }
Line 71... Line 67...
71
 
67
 
72
    /**
68
    /**
73
     * Retrieve a remote REST resource
69
     * Retrieve a remote REST resource
74
     * @param string full URL to this resource
70
     * @param string full URL to this resource
75
     * @param array|false contents of the accept-encoding header
71
     * @param array|false contents of the accept-encoding header
76
     * @param boolean     if true, xml will be returned as a string, otherwise, xml will be
72
     * @param boolean     if true, xml will be returned as a string, otherwise, xml will be
77
     *                    parsed using PEAR_XMLParser
73
     *                    parsed using PEAR_XMLParser
78
     * @return string|array
74
     * @return string|array
79
     */
75
     */
80
    function retrieveData($url, $accept = false, $forcestring = false)
76
    function retrieveData($url, $accept = false, $forcestring = false, $channel = false)
81
    {
77
    {
82
        $cacheId = $this->getCacheId($url);
78
        $cacheId = $this->getCacheId($url);
83
        if ($ret = $this->useLocalCache($url, $cacheId)) {
79
        if ($ret = $this->useLocalCache($url, $cacheId)) {
84
            return $ret;
80
            return $ret;
-
 
81
        }
-
 
82
 
85
        }
83
        $file = $trieddownload = false;
86
        if (!isset($this->_options['offline'])) {
84
        if (!isset($this->_options['offline'])) {
87
            $trieddownload = true;
85
            $trieddownload = true;
88
            $file = $this->downloadHttp($url, $cacheId ? $cacheId['lastChange'] : false, $accept);
-
 
89
        } else {
-
 
90
            $trieddownload = false;
-
 
91
            $file = false;
86
            $file = $this->downloadHttp($url, $cacheId ? $cacheId['lastChange'] : false, $accept, $channel);
-
 
87
        }
92
        }
88
 
93
        if (PEAR::isError($file)) {
89
        if (PEAR::isError($file)) {
94
            if ($file->getCode() == -9276) {
-
 
95
                $trieddownload = false;
-
 
96
                $file = false; // use local copy if available on socket connect error
-
 
97
            } else {
90
            if ($file->getCode() !== -9276) {
98
                return $file;
91
                return $file;
-
 
92
            }
-
 
93
 
-
 
94
            $trieddownload = false;
99
            }
95
            $file = false; // use local copy if available on socket connect error
-
 
96
        }
100
        }
97
 
101
        if (!$file) {
98
        if (!$file) {
102
            $ret = $this->getCache($url);
99
            $ret = $this->getCache($url);
103
            if (!PEAR::isError($ret) && $trieddownload) {
100
            if (!PEAR::isError($ret) && $trieddownload) {
104
                // reset the age of the cache if the server says it was unmodified
101
                // reset the age of the cache if the server says it was unmodified
-
 
102
                $result = $this->saveCache($url, $ret, null, true, $cacheId);
-
 
103
                if (PEAR::isError($result)) {
-
 
104
                    return PEAR::raiseError($result->getMessage());
105
                $this->saveCache($url, $ret, null, true, $cacheId);
105
                }
-
 
106
            }
106
            }
107
 
107
            return $ret;
108
            return $ret;
-
 
109
        }
108
        }
110
 
109
        if (is_array($file)) {
111
        if (is_array($file)) {
110
            $headers = $file[2];
112
            $headers      = $file[2];
111
            $lastmodified = $file[1];
113
            $lastmodified = $file[1];
112
            $content = $file[0];
114
            $content      = $file[0];
113
        } else {
115
        } else {
114
            $content = $file;
116
            $headers      = array();
115
            $lastmodified = false;
117
            $lastmodified = false;
116
            $headers = array();
118
            $content      = $file;
-
 
119
        }
117
        }
120
 
118
        if ($forcestring) {
121
        if ($forcestring) {
-
 
122
            $result = $this->saveCache($url, $content, $lastmodified, false, $cacheId);
-
 
123
            if (PEAR::isError($result)) {
-
 
124
                return PEAR::raiseError($result->getMessage());
-
 
125
            }
119
            $this->saveCache($url, $content, $lastmodified, false, $cacheId);
126
 
120
            return $content;
127
            return $content;
-
 
128
        }
121
        }
129
 
-
 
130
        if (isset($headers['content-type'])) {
-
 
131
            $content_type = explode(";", $headers['content-type']);
122
        if (isset($headers['content-type'])) {
132
            $content_type = $content_type[0];
123
            switch ($headers['content-type']) {
133
            switch ($content_type) {
124
                case 'text/xml' :
134
                case 'text/xml' :
-
 
135
                case 'application/xml' :
-
 
136
                case 'text/plain' :
-
 
137
                    if ($content_type === 'text/plain') {
-
 
138
                        $check = substr($content, 0, 5);
-
 
139
                        if ($check !== '<?xml') {
-
 
140
                            break;
-
 
141
                        }
-
 
142
                    }
125
                case 'application/xml' :
143
 
126
                    $parser = new PEAR_XMLParser;
144
                    $parser = new PEAR_XMLParser;
127
                    PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
145
                    PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
128
                    $err = $parser->parse($content);
146
                    $err = $parser->parse($content);
129
                    PEAR::popErrorHandling();
147
                    PEAR::popErrorHandling();
Line 140... Line 158...
140
            // assume XML
158
            // assume XML
141
            $parser = new PEAR_XMLParser;
159
            $parser = new PEAR_XMLParser;
142
            $parser->parse($content);
160
            $parser->parse($content);
143
            $content = $parser->getData();
161
            $content = $parser->getData();
144
        }
162
        }
-
 
163
 
145
        $this->saveCache($url, $content, $lastmodified, false, $cacheId);
164
        $result = $this->saveCache($url, $content, $lastmodified, false, $cacheId);
-
 
165
        if (PEAR::isError($result)) {
-
 
166
            return PEAR::raiseError($result->getMessage());
-
 
167
        }
-
 
168
 
146
        return $content;
169
        return $content;
147
    }
170
    }
Line 148... Line 171...
148
 
171
 
149
    function useLocalCache($url, $cacheid = null)
172
    function useLocalCache($url, $cacheid = null)
150
    {
173
    {
151
        if ($cacheid === null) {
174
        if ($cacheid === null) {
152
            $cacheidfile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
175
            $cacheidfile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
153
                md5($url) . 'rest.cacheid';
176
                md5($url) . 'rest.cacheid';
154
            if (file_exists($cacheidfile)) {
-
 
155
                $cacheid = unserialize(implode('', file($cacheidfile)));
-
 
156
            } else {
177
            if (!file_exists($cacheidfile)) {
157
                return false;
178
                return false;
-
 
179
            }
-
 
180
 
158
            }
181
            $cacheid = unserialize(implode('', file($cacheidfile)));
-
 
182
        }
159
        }
183
 
160
        $cachettl = $this->config->get('cache_ttl');
184
        $cachettl = $this->config->get('cache_ttl');
161
        // If cache is newer than $cachettl seconds, we use the cache!
185
        // If cache is newer than $cachettl seconds, we use the cache!
162
        if (time() - $cacheid['age'] < $cachettl) {
186
        if (time() - $cacheid['age'] < $cachettl) {
163
            return $this->getCache($url);
187
            return $this->getCache($url);
-
 
188
        }
164
        }
189
 
165
        return false;
190
        return false;
Line 166... Line 191...
166
    }
191
    }
167
 
192
 
168
    function getCacheId($url)
193
    function getCacheId($url)
169
    {
194
    {
-
 
195
        $cacheidfile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
170
        $cacheidfile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
196
            md5($url) . 'rest.cacheid';
171
            md5($url) . 'rest.cacheid';
-
 
172
        if (file_exists($cacheidfile)) {
-
 
173
            $ret = unserialize(implode('', file($cacheidfile)));
-
 
174
            return $ret;
197
 
175
        } else {
198
        if (!file_exists($cacheidfile)) {
-
 
199
            return false;
-
 
200
        }
-
 
201
 
176
            return false;
202
        $ret = unserialize(implode('', file($cacheidfile)));
Line 177... Line 203...
177
        }
203
        return $ret;
178
    }
204
    }
179
 
205
 
180
    function getCache($url)
206
    function getCache($url)
-
 
207
    {
181
    {
208
        $cachefile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
182
        $cachefile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
-
 
183
            md5($url) . 'rest.cachefile';
-
 
184
        if (file_exists($cachefile)) {
209
            md5($url) . 'rest.cachefile';
185
            return unserialize(implode('', file($cachefile)));
210
 
-
 
211
        if (!file_exists($cachefile)) {
-
 
212
            return PEAR::raiseError('No cached content available for "' . $url . '"');
186
        } else {
213
        }
Line 187... Line 214...
187
            return PEAR::raiseError('No cached content available for "' . $url . '"');
214
 
188
        }
215
        return unserialize(implode('', file($cachefile)));
189
    }
216
    }
Line 195... Line 222...
195
     * @param bool   if true, then the cache id file should be regenerated to
222
     * @param bool   if true, then the cache id file should be regenerated to
196
     *               trigger a new time-to-live value
223
     *               trigger a new time-to-live value
197
     */
224
     */
198
    function saveCache($url, $contents, $lastmodified, $nochange = false, $cacheid = null)
225
    function saveCache($url, $contents, $lastmodified, $nochange = false, $cacheid = null)
199
    {
226
    {
200
        $cacheidfile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
227
        $cache_dir   = $this->config->get('cache_dir');
201
            md5($url) . 'rest.cacheid';
-
 
202
        $cachefile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
228
        $d           = $cache_dir . DIRECTORY_SEPARATOR . md5($url);
203
            md5($url) . 'rest.cachefile';
229
        $cacheidfile = $d . 'rest.cacheid';
204
        if ($cacheid === null && $nochange) {
230
        $cachefile   = $d . 'rest.cachefile';
205
            $cacheid = unserialize(implode('', file($cacheidfile)));
-
 
206
        }
-
 
Line 207... Line -...
207
 
-
 
208
        $fp = @fopen($cacheidfile, 'wb');
-
 
209
        if (!$fp) {
-
 
210
            $cache_dir = $this->config->get('cache_dir');
231
 
211
            if (!is_dir($cache_dir)) {
232
        if (!is_dir($cache_dir)) {
212
                System::mkdir(array('-p', $cache_dir));
233
            if (System::mkdir(array('-p', $cache_dir)) === false) {
213
                $fp = @fopen($cacheidfile, 'wb');
-
 
214
                if (!$fp) {
-
 
215
                    return false;
-
 
216
                }
-
 
217
            } else {
-
 
218
                return false;
234
              return PEAR::raiseError("The value of config option cache_dir ($cache_dir) is not a directory and attempts to create the directory failed.");
219
            }
235
            }
Line 220... Line 236...
220
        }
236
        }
221
 
237
 
222
        if ($nochange) {
238
        if (!is_writeable($cache_dir)) {
223
            fwrite($fp, serialize(array(
239
            // If writing to the cache dir is not going to work, silently do nothing.
224
                'age'        => time(),
-
 
225
                'lastChange' => $cacheid['lastChange'],
-
 
226
                )));
240
            // An ugly hack, but retains compat with PEAR 1.9.1 where many commands
227
            fclose($fp);
-
 
228
            return true;
-
 
229
        } else {
-
 
230
            fwrite($fp, serialize(array(
-
 
231
                'age'        => time(),
-
 
232
                'lastChange' => $lastmodified,
241
            // work fine as non-root user (w/out write access to default cache dir).
-
 
242
            return true;
-
 
243
        }
-
 
244
 
-
 
245
        if ($cacheid === null && $nochange) {
-
 
246
            $cacheid = unserialize(implode('', file($cacheidfile)));
-
 
247
        }
-
 
248
 
-
 
249
        $idData = serialize(array(
233
                )));
250
            'age'        => time(),
-
 
251
            'lastChange' => ($nochange ? $cacheid['lastChange'] : $lastmodified),
234
        }
252
        ));
-
 
253
 
-
 
254
        $result = $this->saveCacheFile($cacheidfile, $idData);
235
        fclose($fp);
255
        if (PEAR::isError($result)) {
-
 
256
            return $result;
-
 
257
        } elseif ($nochange) {
-
 
258
            return true;
-
 
259
        }
-
 
260
 
236
        $fp = @fopen($cachefile, 'wb');
261
        $result = $this->saveCacheFile($cachefile, serialize($contents));
237
        if (!$fp) {
262
        if (PEAR::isError($result)) {
238
            if (file_exists($cacheidfile)) {
263
            if (file_exists($cacheidfile)) {
-
 
264
              @unlink($cacheidfile);
239
                @unlink($cacheidfile);
265
            }
240
            }
266
 
-
 
267
            return $result;
-
 
268
        }
-
 
269
 
-
 
270
        return true;
-
 
271
    }
-
 
272
 
241
            return false;
273
    function saveCacheFile($file, $contents)
-
 
274
    {
-
 
275
        $len = strlen($contents);
-
 
276
 
-
 
277
        $cachefile_fp = @fopen($file, 'xb'); // x is the O_CREAT|O_EXCL mode
-
 
278
        if ($cachefile_fp !== false) { // create file
-
 
279
            if (fwrite($cachefile_fp, $contents, $len) < $len) {
-
 
280
                fclose($cachefile_fp);
-
 
281
                return PEAR::raiseError("Could not write $file.");
-
 
282
            }
-
 
283
        } else { // update file
-
 
284
            $cachefile_fp = @fopen($file, 'r+b'); // do not truncate file
-
 
285
            if (!$cachefile_fp) {
-
 
286
                return PEAR::raiseError("Could not open $file for writing.");
-
 
287
            }
-
 
288
 
-
 
289
            if (OS_WINDOWS) {
-
 
290
                $not_symlink     = !is_link($file); // see bug #18834
-
 
291
            } else {
-
 
292
                $cachefile_lstat = lstat($file);
-
 
293
                $cachefile_fstat = fstat($cachefile_fp);
-
 
294
                $not_symlink     = $cachefile_lstat['mode'] == $cachefile_fstat['mode']
-
 
295
                                   && $cachefile_lstat['ino']  == $cachefile_fstat['ino']
-
 
296
                                   && $cachefile_lstat['dev']  == $cachefile_fstat['dev']
-
 
297
                                   && $cachefile_fstat['nlink'] === 1;
-
 
298
            }
-
 
299
 
-
 
300
            if ($not_symlink) {
-
 
301
                ftruncate($cachefile_fp, 0); // NOW truncate
-
 
302
                if (fwrite($cachefile_fp, $contents, $len) < $len) {
-
 
303
                    fclose($cachefile_fp);
-
 
304
                    return PEAR::raiseError("Could not write $file.");
-
 
305
                }
-
 
306
            } else {
-
 
307
                fclose($cachefile_fp);
-
 
308
                $link = function_exists('readlink') ? readlink($file) : $file;
-
 
309
                return PEAR::raiseError('SECURITY ERROR: Will not write to ' . $file . ' as it is symlinked to ' . $link . ' - Possible symlink attack');
-
 
310
            }
242
        }
311
        }
243
        fwrite($fp, serialize($contents));
312
 
244
        fclose($fp);
313
        fclose($cachefile_fp);
Line 245... Line 314...
245
        return true;
314
        return true;
246
    }
315
    }
Line 264... Line 333...
264
     *                       getCode().  If caching is requested, then return the header
333
     *                       getCode().  If caching is requested, then return the header
265
     *                       values.
334
     *                       values.
266
     *
335
     *
267
     * @access public
336
     * @access public
268
     */
337
     */
269
    function downloadHttp($url, $lastmodified = null, $accept = false)
338
    function downloadHttp($url, $lastmodified = null, $accept = false, $channel = false)
270
    {
339
    {
-
 
340
        static $redirect = 0;
-
 
341
        // always reset , so we are clean case of error
-
 
342
        $wasredirect = $redirect;
-
 
343
        $redirect = 0;
-
 
344
 
271
        $info = parse_url($url);
345
        $info = parse_url($url);
272
        if (!isset($info['scheme']) || !in_array($info['scheme'], array('http', 'https'))) {
346
        if (!isset($info['scheme']) || !in_array($info['scheme'], array('http', 'https'))) {
273
            return PEAR::raiseError('Cannot download non-http URL "' . $url . '"');
347
            return PEAR::raiseError('Cannot download non-http URL "' . $url . '"');
274
        }
348
        }
-
 
349
 
275
        if (!isset($info['host'])) {
350
        if (!isset($info['host'])) {
276
            return PEAR::raiseError('Cannot download from non-URL "' . $url . '"');
351
            return PEAR::raiseError('Cannot download from non-URL "' . $url . '"');
277
        } else {
-
 
278
            $host = $info['host'];
-
 
279
            if (!array_key_exists('port', $info)) {
-
 
280
                $info['port'] = null;
-
 
281
            }
-
 
282
            if (!array_key_exists('path', $info)) {
-
 
283
                $info['path'] = null;
-
 
284
            }
-
 
285
            $port = $info['port'];
-
 
286
            $path = $info['path'];
-
 
287
        }
352
        }
-
 
353
 
-
 
354
        $host   = isset($info['host']) ? $info['host'] : null;
-
 
355
        $port   = isset($info['port']) ? $info['port'] : null;
-
 
356
        $path   = isset($info['path']) ? $info['path'] : null;
-
 
357
        $schema = (isset($info['scheme']) && $info['scheme'] == 'https') ? 'https' : 'http';
-
 
358
 
288
        $proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
359
        $proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
289
        if ($this->config->get('http_proxy')&& 
360
        if ($this->config->get('http_proxy')&&
290
              $proxy = parse_url($this->config->get('http_proxy'))) {
361
              $proxy = parse_url($this->config->get('http_proxy'))
-
 
362
        ) {
291
            $proxy_host = isset($proxy['host']) ? $proxy['host'] : null;
363
            $proxy_host = isset($proxy['host']) ? $proxy['host'] : null;
292
            if (isset($proxy['scheme']) && $proxy['scheme'] == 'https') {
364
            if ($schema === 'https') {
293
                $proxy_host = 'ssl://' . $proxy_host;
365
                $proxy_host = 'ssl://' . $proxy_host;
294
            }
366
            }
-
 
367
 
295
            $proxy_port = isset($proxy['port']) ? $proxy['port'] : 8080;
368
            $proxy_port   = isset($proxy['port']) ? $proxy['port'] : 8080;
296
            $proxy_user = isset($proxy['user']) ? urldecode($proxy['user']) : null;
369
            $proxy_user   = isset($proxy['user']) ? urldecode($proxy['user']) : null;
297
            $proxy_pass = isset($proxy['pass']) ? urldecode($proxy['pass']) : null;
370
            $proxy_pass   = isset($proxy['pass']) ? urldecode($proxy['pass']) : null;
-
 
371
            $proxy_schema = (isset($proxy['scheme']) && $proxy['scheme'] == 'https') ? 'https' : 'http';
298
        }
372
        }
-
 
373
 
299
        if (empty($port)) {
374
        if (empty($port)) {
300
            if (isset($info['scheme']) && $info['scheme'] == 'https') {
375
            $port = (isset($info['scheme']) && $info['scheme'] == 'https')  ? 443 : 80;
301
                $port = 443;
-
 
302
            } else {
-
 
303
                $port = 80;
-
 
304
            }
-
 
305
        }
376
        }
-
 
377
 
306
        If (isset($proxy['host'])) {
378
        if (isset($proxy['host'])) {
307
            $request = "GET $url HTTP/1.1\r\n";
379
            $request = "GET $url HTTP/1.1\r\n";
308
        } else {
380
        } else {
309
            $request = "GET $path HTTP/1.1\r\n";
381
            $request = "GET $path HTTP/1.1\r\n";
310
        }
382
        }
Line -... Line 383...
-
 
383
 
311
 
384
        $request .= "Host: $host\r\n";
312
        $ifmodifiedsince = '';
385
        $ifmodifiedsince = '';
313
        if (is_array($lastmodified)) {
386
        if (is_array($lastmodified)) {
314
            if (isset($lastmodified['Last-Modified'])) {
387
            if (isset($lastmodified['Last-Modified'])) {
315
                $ifmodifiedsince = 'If-Modified-Since: ' . $lastmodified['Last-Modified'] . "\r\n";
388
                $ifmodifiedsince = 'If-Modified-Since: ' . $lastmodified['Last-Modified'] . "\r\n";
-
 
389
            }
316
            }
390
 
317
            if (isset($lastmodified['ETag'])) {
391
            if (isset($lastmodified['ETag'])) {
318
                $ifmodifiedsince .= "If-None-Match: $lastmodified[ETag]\r\n";
392
                $ifmodifiedsince .= "If-None-Match: $lastmodified[ETag]\r\n";
319
            }
393
            }
320
        } else {
394
        } else {
321
            $ifmodifiedsince = ($lastmodified ? "If-Modified-Since: $lastmodified\r\n" : '');
395
            $ifmodifiedsince = ($lastmodified ? "If-Modified-Since: $lastmodified\r\n" : '');
-
 
396
        }
322
        }
397
 
323
        $request .= "Host: $host:$port\r\n" . $ifmodifiedsince .
398
        $request .= $ifmodifiedsince .
-
 
399
            "User-Agent: PEAR/1.10.1/PHP/" . PHP_VERSION . "\r\n";
324
            "User-Agent: PEAR/1.5.1/PHP/" . PHP_VERSION . "\r\n";
400
 
325
        $username = $this->config->get('username');
401
        $username = $this->config->get('username', null, $channel);
-
 
402
        $password = $this->config->get('password', null, $channel);
326
        $password = $this->config->get('password');
403
 
327
        if ($username && $password) {
404
        if ($username && $password) {
328
            $tmp = base64_encode("$username:$password");
405
            $tmp = base64_encode("$username:$password");
329
            $request .= "Authorization: Basic $tmp\r\n";
406
            $request .= "Authorization: Basic $tmp\r\n";
-
 
407
        }
330
        }
408
 
331
        if ($proxy_host != '' && $proxy_user != '') {
409
        if ($proxy_host != '' && $proxy_user != '') {
332
            $request .= 'Proxy-Authorization: Basic ' .
410
            $request .= 'Proxy-Authorization: Basic ' .
333
                base64_encode($proxy_user . ':' . $proxy_pass) . "\r\n";
411
                base64_encode($proxy_user . ':' . $proxy_pass) . "\r\n";
-
 
412
        }
334
        }
413
 
335
        if ($accept) {
414
        if ($accept) {
336
            $request .= 'Accept: ' . implode(', ', $accept) . "\r\n";
415
            $request .= 'Accept: ' . implode(', ', $accept) . "\r\n";
-
 
416
        }
-
 
417
 
337
        }
418
        $request .= "Accept-Encoding:\r\n";
338
        $request .= "Connection: close\r\n";
419
        $request .= "Connection: close\r\n";
-
 
420
        $request .= "\r\n";
339
        $request .= "\r\n";
421
 
340
        if ($proxy_host != '') {
422
        if ($proxy_host != '') {
341
            $fp = @fsockopen($proxy_host, $proxy_port, $errno, $errstr, 15);
423
            $fp = @fsockopen($proxy_host, $proxy_port, $errno, $errstr, 15);
342
            if (!$fp) {
424
            if (!$fp) {
343
                return PEAR::raiseError("Connection to `$proxy_host:$proxy_port' failed: $errstr",
-
 
344
                    -9276);
425
                return PEAR::raiseError("Connection to `$proxy_host:$proxy_port' failed: $errstr", -9276);
345
            }
426
            }
346
        } else {
427
        } else {
347
            if (isset($info['scheme']) && $info['scheme'] == 'https') {
428
            if ($schema === 'https') {
348
                $host = 'ssl://' . $host;
429
                $host = 'ssl://' . $host;
-
 
430
            }
349
            }
431
 
350
            $fp = @fsockopen($host, $port, $errno, $errstr);
432
            $fp = @fsockopen($host, $port, $errno, $errstr);
351
            if (!$fp) {
433
            if (!$fp) {
352
                return PEAR::raiseError("Connection to `$host:$port' failed: $errstr", $errno);
434
                return PEAR::raiseError("Connection to `$host:$port' failed: $errstr", $errno);
353
            }
435
            }
-
 
436
        }
354
        }
437
 
-
 
438
        fwrite($fp, $request);
355
        fwrite($fp, $request);
439
 
-
 
440
        $headers = array();
356
        $headers = array();
441
        $reply   = 0;
357
        while (trim($line = fgets($fp, 1024))) {
442
        while ($line = trim(fgets($fp, 1024))) {
358
            if (preg_match('/^([^:]+):\s+(.*)\s*$/', $line, $matches)) {
443
            if (preg_match('/^([^:]+):\s+(.*)\s*\\z/', $line, $matches)) {
359
                $headers[strtolower($matches[1])] = trim($matches[2]);
444
                $headers[strtolower($matches[1])] = trim($matches[2]);
-
 
445
            } elseif (preg_match('|^HTTP/1.[01] ([0-9]{3}) |', $line, $matches)) {
360
            } elseif (preg_match('|^HTTP/1.[01] ([0-9]{3}) |', $line, $matches)) {
446
                $reply = (int)$matches[1];
361
                if ($matches[1] == 304 && ($lastmodified || ($lastmodified === false))) {
447
                if ($reply == 304 && ($lastmodified || ($lastmodified === false))) {
362
                    return false;
448
                    return false;
-
 
449
                }
363
                }
450
 
364
                if ($matches[1] != 200) {
451
                if (!in_array($reply, array(200, 301, 302, 303, 305, 307))) {
365
                    return PEAR::raiseError("File http://$host:$port$path not valid (received: $line)", (int) $matches[1]);
452
                    return PEAR::raiseError("File $schema://$host:$port$path not valid (received: $line)");
366
                }
453
                }
367
            }
454
            }
-
 
455
        }
-
 
456
 
368
        }
457
        if ($reply != 200) {
-
 
458
            if (!isset($headers['location'])) {
-
 
459
                return PEAR::raiseError("File $schema://$host:$port$path not valid (redirected but no location)");
-
 
460
            }
369
        if (isset($headers['content-length'])) {
461
 
-
 
462
            if ($wasredirect > 4) {
370
            $length = $headers['content-length'];
463
                return PEAR::raiseError("File $schema://$host:$port$path not valid (redirection looped more than 5 times)");
-
 
464
            }
371
        } else {
465
 
-
 
466
            $redirect = $wasredirect + 1;
372
            $length = -1;
467
            return $this->downloadHttp($headers['location'], $lastmodified, $accept, $channel);
-
 
468
        }
-
 
469
 
-
 
470
        $length = isset($headers['content-length']) ? $headers['content-length'] : -1;
373
        }
471
 
374
        $data = '';
472
        $data = '';
375
        while ($chunk = @fread($fp, 8192)) {
473
        while ($chunk = @fread($fp, 8192)) {
376
            $data .= $chunk;
474
            $data .= $chunk;
377
        }
475
        }
-
 
476
        fclose($fp);
378
        fclose($fp);
477
 
379
        if ($lastmodified === false || $lastmodified) {
478
        if ($lastmodified === false || $lastmodified) {
380
            if (isset($headers['etag'])) {
479
            if (isset($headers['etag'])) {
381
                $lastmodified = array('ETag' => $headers['etag']);
480
                $lastmodified = array('ETag' => $headers['etag']);
-
 
481
            }
382
            }
482
 
383
            if (isset($headers['last-modified'])) {
483
            if (isset($headers['last-modified'])) {
384
                if (is_array($lastmodified)) {
484
                if (is_array($lastmodified)) {
385
                    $lastmodified['Last-Modified'] = $headers['last-modified'];
485
                    $lastmodified['Last-Modified'] = $headers['last-modified'];
386
                } else {
486
                } else {
387
                    $lastmodified = $headers['last-modified'];
487
                    $lastmodified = $headers['last-modified'];
388
                }
488
                }
-
 
489
            }
389
            }
490
 
390
            return array($data, $lastmodified, $headers);
491
            return array($data, $lastmodified, $headers);
-
 
492
        }
391
        }
493
 
392
        return $data;
494
        return $data;
393
    }
495
    }
394
}
-