]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/Auth/Yadis/PlainHTTPFetcher.php
include external libs in a subdir to make install easier
[quix0rs-gnu-social.git] / extlib / Auth / Yadis / PlainHTTPFetcher.php
1 <?php
2
3 /**
4  * This module contains the plain non-curl HTTP fetcher
5  * implementation.
6  *
7  * PHP versions 4 and 5
8  *
9  * LICENSE: See the COPYING file included in this distribution.
10  *
11  * @package OpenID
12  * @author JanRain, Inc. <openid@janrain.com>
13  * @copyright 2005-2008 Janrain, Inc.
14  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
15  */
16
17 /**
18  * Interface import
19  */
20 require_once "Auth/Yadis/HTTPFetcher.php";
21
22 /**
23  * This class implements a plain, hand-built socket-based fetcher
24  * which will be used in the event that CURL is unavailable.
25  *
26  * @package OpenID
27  */
28 class Auth_Yadis_PlainHTTPFetcher extends Auth_Yadis_HTTPFetcher {
29     /**
30      * Does this fetcher support SSL URLs?
31      */
32     function supportsSSL()
33     {
34         return function_exists('openssl_open');
35     }
36
37     function get($url, $extra_headers = null)
38     {
39         if (!$this->canFetchURL($url)) {
40             return null;
41         }
42
43         $redir = true;
44
45         $stop = time() + $this->timeout;
46         $off = $this->timeout;
47
48         while ($redir && ($off > 0)) {
49
50             $parts = parse_url($url);
51
52             $specify_port = true;
53
54             // Set a default port.
55             if (!array_key_exists('port', $parts)) {
56                 $specify_port = false;
57                 if ($parts['scheme'] == 'http') {
58                     $parts['port'] = 80;
59                 } elseif ($parts['scheme'] == 'https') {
60                     $parts['port'] = 443;
61                 } else {
62                     return null;
63                 }
64             }
65
66             if (!array_key_exists('path', $parts)) {
67                 $parts['path'] = '/';
68             }
69
70             $host = $parts['host'];
71
72             if ($parts['scheme'] == 'https') {
73                 $host = 'ssl://' . $host;
74             }
75
76             $user_agent = Auth_OpenID_USER_AGENT;
77
78             $headers = array(
79                              "GET ".$parts['path'].
80                              (array_key_exists('query', $parts) ?
81                               "?".$parts['query'] : "").
82                                  " HTTP/1.0",
83                              "User-Agent: $user_agent",
84                              "Host: ".$parts['host'].
85                                 ($specify_port ? ":".$parts['port'] : ""),
86                              "Range: 0-".
87                                 (1024*Auth_OpenID_FETCHER_MAX_RESPONSE_KB),
88                              "Port: ".$parts['port']);
89
90             $errno = 0;
91             $errstr = '';
92
93             if ($extra_headers) {
94                 foreach ($extra_headers as $h) {
95                     $headers[] = $h;
96                 }
97             }
98
99             @$sock = fsockopen($host, $parts['port'], $errno, $errstr,
100                                $this->timeout);
101             if ($sock === false) {
102                 return false;
103             }
104
105             stream_set_timeout($sock, $this->timeout);
106
107             fputs($sock, implode("\r\n", $headers) . "\r\n\r\n");
108
109             $data = "";
110             $kilobytes = 0;
111             while (!feof($sock) &&
112                    $kilobytes < Auth_OpenID_FETCHER_MAX_RESPONSE_KB ) {
113                 $data .= fgets($sock, 1024);
114                 $kilobytes += 1;
115             }
116
117             fclose($sock);
118
119             // Split response into header and body sections
120             list($headers, $body) = explode("\r\n\r\n", $data, 2);
121             $headers = explode("\r\n", $headers);
122
123             $http_code = explode(" ", $headers[0]);
124             $code = $http_code[1];
125
126             if (in_array($code, array('301', '302'))) {
127                 $url = $this->_findRedirect($headers);
128                 $redir = true;
129             } else {
130                 $redir = false;
131             }
132
133             $off = $stop - time();
134         }
135
136         $new_headers = array();
137
138         foreach ($headers as $header) {
139             if (preg_match("/:/", $header)) {
140                 $parts = explode(": ", $header, 2);
141
142                 if (count($parts) == 2) {
143                     list($name, $value) = $parts;
144                     $new_headers[$name] = $value;
145                 }
146             }
147
148         }
149
150         return new Auth_Yadis_HTTPResponse($url, $code, $new_headers, $body);
151     }
152
153     function post($url, $body, $extra_headers = null)
154     {
155         if (!$this->canFetchURL($url)) {
156             return null;
157         }
158
159         $parts = parse_url($url);
160
161         $headers = array();
162
163         $post_path = $parts['path'];
164         if (isset($parts['query'])) {
165             $post_path .= '?' . $parts['query'];
166         }
167
168         $headers[] = "POST ".$post_path." HTTP/1.0";
169         $headers[] = "Host: " . $parts['host'];
170         $headers[] = "Content-type: application/x-www-form-urlencoded";
171         $headers[] = "Content-length: " . strval(strlen($body));
172
173         if ($extra_headers &&
174             is_array($extra_headers)) {
175             $headers = array_merge($headers, $extra_headers);
176         }
177
178         // Join all headers together.
179         $all_headers = implode("\r\n", $headers);
180
181         // Add headers, two newlines, and request body.
182         $request = $all_headers . "\r\n\r\n" . $body;
183
184         // Set a default port.
185         if (!array_key_exists('port', $parts)) {
186             if ($parts['scheme'] == 'http') {
187                 $parts['port'] = 80;
188             } elseif ($parts['scheme'] == 'https') {
189                 $parts['port'] = 443;
190             } else {
191                 return null;
192             }
193         }
194
195         if ($parts['scheme'] == 'https') {
196             $parts['host'] = sprintf("ssl://%s", $parts['host']);
197         }
198
199         // Connect to the remote server.
200         $errno = 0;
201         $errstr = '';
202
203         $sock = fsockopen($parts['host'], $parts['port'], $errno, $errstr,
204                           $this->timeout);
205
206         if ($sock === false) {
207             return null;
208         }
209
210         stream_set_timeout($sock, $this->timeout);
211
212         // Write the POST request.
213         fputs($sock, $request);
214
215         // Get the response from the server.
216         $response = "";
217         while (!feof($sock)) {
218             if ($data = fgets($sock, 128)) {
219                 $response .= $data;
220             } else {
221                 break;
222             }
223         }
224
225         // Split the request into headers and body.
226         list($headers, $response_body) = explode("\r\n\r\n", $response, 2);
227
228         $headers = explode("\r\n", $headers);
229
230         // Expect the first line of the headers data to be something
231         // like HTTP/1.1 200 OK.  Split the line on spaces and take
232         // the second token, which should be the return code.
233         $http_code = explode(" ", $headers[0]);
234         $code = $http_code[1];
235
236         $new_headers = array();
237
238         foreach ($headers as $header) {
239             if (preg_match("/:/", $header)) {
240                 list($name, $value) = explode(": ", $header, 2);
241                 $new_headers[$name] = $value;
242             }
243
244         }
245
246         return new Auth_Yadis_HTTPResponse($url, $code,
247                                            $new_headers, $response_body);
248     }
249 }
250
251 ?>