Subversion Repositories Applications.annuaire

Rev

Rev 42 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42 aurelien 1
<?php
2
 
3
/**
4
 * This module contains the CURL-based HTTP fetcher implementation.
5
 *
6
 * PHP versions 4 and 5
7
 *
8
 * LICENSE: See the COPYING file included in this distribution.
9
 *
10
 * @package Yadis
11
 * @author JanRain, Inc. <openid@janrain.com>
12
 * @copyright 2005 Janrain, Inc.
13
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
14
 */
15
 
16
/**
17
 * Interface import
18
 */
19
require_once "Services/Yadis/HTTPFetcher.php";
20
 
21
/**
22
 * A paranoid {@link Services_Yadis_HTTPFetcher} class which uses CURL
23
 * for fetching.
24
 *
25
 * @package Yadis
26
 */
27
class Services_Yadis_ParanoidHTTPFetcher extends Services_Yadis_HTTPFetcher {
28
    function Services_Yadis_ParanoidHTTPFetcher()
29
    {
30
        $this->reset();
31
    }
32
 
33
    function reset()
34
    {
35
        $this->headers = array();
36
        $this->data = "";
37
    }
38
 
39
    /**
40
     * @access private
41
     */
42
    function _writeHeader($ch, $header)
43
    {
44
        array_push($this->headers, rtrim($header));
45
        return strlen($header);
46
    }
47
 
48
    /**
49
     * @access private
50
     */
51
    function _writeData($ch, $data)
52
    {
53
        $this->data .= $data;
54
        return strlen($data);
55
    }
56
 
57
    function get($url, $extra_headers = null)
58
    {
59
        $stop = time() + $this->timeout;
60
        $off = $this->timeout;
61
 
62
        $redir = true;
63
 
64
        while ($redir && ($off > 0)) {
65
            $this->reset();
66
 
67
            $c = curl_init();
68
            if (defined('CURLOPT_NOSIGNAL')) {
69
                curl_setopt($c, CURLOPT_NOSIGNAL, true);
70
            }
71
 
72
            if (!$this->allowedURL($url)) {
73
                trigger_error(sprintf("Fetching URL not allowed: %s", $url),
74
                              E_USER_WARNING);
75
                return null;
76
            }
77
 
78
            curl_setopt($c, CURLOPT_WRITEFUNCTION,
79
                        array(&$this, "_writeData"));
80
            curl_setopt($c, CURLOPT_HEADERFUNCTION,
81
                        array(&$this, "_writeHeader"));
82
 
83
            if ($extra_headers) {
84
                curl_setopt($c, CURLOPT_HTTPHEADER, $extra_headers);
85
            }
86
 
87
            curl_setopt($c, CURLOPT_TIMEOUT, $off);
88
            curl_setopt($c, CURLOPT_URL, $url);
89
 
90
            curl_exec($c);
91
 
92
            $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
93
            $body = $this->data;
94
            $headers = $this->headers;
95
 
96
            if (!$code) {
97
                return null;
98
            }
99
 
100
            if (in_array($code, array(301, 302, 303, 307))) {
101
                $url = $this->_findRedirect($headers);
102
                $redir = true;
103
            } else {
104
                $redir = false;
105
                curl_close($c);
106
 
107
                $new_headers = array();
108
 
109
                foreach ($headers as $header) {
110
                    if (preg_match("/:/", $header)) {
111
                        list($name, $value) = explode(": ", $header, 2);
112
                        $new_headers[$name] = $value;
113
                    }
114
                }
115
 
116
                return new Services_Yadis_HTTPResponse($url, $code,
117
                                                    $new_headers, $body);
118
            }
119
 
120
            $off = $stop - time();
121
        }
122
 
123
        trigger_error(sprintf("Timed out fetching: %s", $url),
124
                      E_USER_WARNING);
125
 
126
        return null;
127
    }
128
 
129
    function post($url, $body)
130
    {
131
        $this->reset();
132
 
133
        if (!$this->allowedURL($url)) {
134
            trigger_error(sprintf("Fetching URL not allowed: %s", $url),
135
                          E_USER_WARNING);
136
            return null;
137
        }
138
 
139
        $c = curl_init();
140
 
141
        curl_setopt($c, CURLOPT_NOSIGNAL, true);
142
        curl_setopt($c, CURLOPT_POST, true);
143
        curl_setopt($c, CURLOPT_POSTFIELDS, $body);
144
        curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
145
        curl_setopt($c, CURLOPT_URL, $url);
146
        curl_setopt($c, CURLOPT_WRITEFUNCTION,
147
                    array(&$this, "_writeData"));
148
 
149
        curl_exec($c);
150
 
151
        $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
152
 
153
        if (!$code) {
154
            trigger_error("No HTTP code returned", E_USER_WARNING);
155
            return null;
156
        }
157
 
158
        $body = $this->data;
159
 
160
        curl_close($c);
161
 
162
        $new_headers = array();
163
 
164
        foreach ($this->headers as $header) {
165
            if (preg_match("/:/", $header)) {
166
                list($name, $value) = explode(": ", $header, 2);
167
                $new_headers[$name] = $value;
168
            }
169
 
170
        }
171
 
172
        return new Services_Yadis_HTTPResponse($url, $code,
173
                                               $new_headers, $body);
174
    }
175
}
176
 
177
?>