Creating repository for dokuwiki modifications for sudaraka.org
[sudaraka-org:dokuwiki-mods.git] / inc / HTTPClient.php
1 <?php
2 /**
3  * HTTP Client
4  *
5  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6  * @author     Andreas Goetz <cpuidle@gmx.de>
7  */
8
9
10 define('HTTP_NL',"\r\n");
11
12
13 /**
14  * Adds DokuWiki specific configs to the HTTP client
15  *
16  * @author Andreas Goetz <cpuidle@gmx.de>
17  */
18 class DokuHTTPClient extends HTTPClient {
19
20     /**
21      * Constructor.
22      *
23      * @author Andreas Gohr <andi@splitbrain.org>
24      */
25     function DokuHTTPClient(){
26         global $conf;
27
28         // call parent constructor
29         $this->HTTPClient();
30
31         // set some values from the config
32         $this->proxy_host   = $conf['proxy']['host'];
33         $this->proxy_port   = $conf['proxy']['port'];
34         $this->proxy_user   = $conf['proxy']['user'];
35         $this->proxy_pass   = conf_decodeString($conf['proxy']['pass']);
36         $this->proxy_ssl    = $conf['proxy']['ssl'];
37         $this->proxy_except = $conf['proxy']['except'];
38     }
39
40
41     /**
42      * Wraps an event around the parent function
43      *
44      * @triggers HTTPCLIENT_REQUEST_SEND
45      * @author   Andreas Gohr <andi@splitbrain.org>
46      */
47     function sendRequest($url,$data='',$method='GET'){
48         $httpdata = array('url'    => $url,
49                           'data'   => $data,
50                           'method' => $method);
51         $evt = new Doku_Event('HTTPCLIENT_REQUEST_SEND',$httpdata);
52         if($evt->advise_before()){
53             $url    = $httpdata['url'];
54             $data   = $httpdata['data'];
55             $method = $httpdata['method'];
56         }
57         $evt->advise_after();
58         unset($evt);
59         return parent::sendRequest($url,$data,$method);
60     }
61
62 }
63
64 /**
65  * This class implements a basic HTTP client
66  *
67  * It supports POST and GET, Proxy usage, basic authentication,
68  * handles cookies and referers. It is based upon the httpclient
69  * function from the VideoDB project.
70  *
71  * @link   http://www.splitbrain.org/go/videodb
72  * @author Andreas Goetz <cpuidle@gmx.de>
73  * @author Andreas Gohr <andi@splitbrain.org>
74  * @author Tobias Sarnowski <sarnowski@new-thoughts.org>
75  */
76 class HTTPClient {
77     //set these if you like
78     var $agent;         // User agent
79     var $http;          // HTTP version defaults to 1.0
80     var $timeout;       // read timeout (seconds)
81     var $cookies;
82     var $referer;
83     var $max_redirect;
84     var $max_bodysize;
85     var $max_bodysize_abort = true;  // if set, abort if the response body is bigger than max_bodysize
86     var $header_regexp; // if set this RE must match against the headers, else abort
87     var $headers;
88     var $debug;
89     var $start = 0; // for timings
90     var $keep_alive = true; // keep alive rocks
91
92     // don't set these, read on error
93     var $error;
94     var $redirect_count;
95
96     // read these after a successful request
97     var $status;
98     var $resp_body;
99     var $resp_headers;
100
101     // set these to do basic authentication
102     var $user;
103     var $pass;
104
105     // set these if you need to use a proxy
106     var $proxy_host;
107     var $proxy_port;
108     var $proxy_user;
109     var $proxy_pass;
110     var $proxy_ssl; //boolean set to true if your proxy needs SSL
111     var $proxy_except; // regexp of URLs to exclude from proxy
112
113     // list of kept alive connections
114     static $connections = array();
115
116     // what we use as boundary on multipart/form-data posts
117     var $boundary = '---DokuWikiHTTPClient--4523452351';
118
119     /**
120      * Constructor.
121      *
122      * @author Andreas Gohr <andi@splitbrain.org>
123      */
124     function HTTPClient(){
125         $this->agent        = 'Mozilla/4.0 (compatible; DokuWiki HTTP Client; '.PHP_OS.')';
126         $this->timeout      = 15;
127         $this->cookies      = array();
128         $this->referer      = '';
129         $this->max_redirect = 3;
130         $this->redirect_count = 0;
131         $this->status       = 0;
132         $this->headers      = array();
133         $this->http         = '1.0';
134         $this->debug        = false;
135         $this->max_bodysize = 0;
136         $this->header_regexp= '';
137         if(extension_loaded('zlib')) $this->headers['Accept-encoding'] = 'gzip';
138         $this->headers['Accept'] = 'text/xml,application/xml,application/xhtml+xml,'.
139                                    'text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
140         $this->headers['Accept-Language'] = 'en-us';
141     }
142
143
144     /**
145      * Simple function to do a GET request
146      *
147      * Returns the wanted page or false on an error;
148      *
149      * @param  string $url       The URL to fetch
150      * @param  bool   $sloppy304 Return body on 304 not modified
151      * @author Andreas Gohr <andi@splitbrain.org>
152      */
153     function get($url,$sloppy304=false){
154         if(!$this->sendRequest($url)) return false;
155         if($this->status == 304 && $sloppy304) return $this->resp_body;
156         if($this->status < 200 || $this->status > 206) return false;
157         return $this->resp_body;
158     }
159
160     /**
161      * Simple function to do a GET request with given parameters
162      *
163      * Returns the wanted page or false on an error.
164      *
165      * This is a convenience wrapper around get(). The given parameters
166      * will be correctly encoded and added to the given base URL.
167      *
168      * @param  string $url       The URL to fetch
169      * @param  array  $data      Associative array of parameters
170      * @param  bool   $sloppy304 Return body on 304 not modified
171      * @author Andreas Gohr <andi@splitbrain.org>
172      */
173     function dget($url,$data,$sloppy304=false){
174         if(strpos($url,'?')){
175             $url .= '&';
176         }else{
177             $url .= '?';
178         }
179         $url .= $this->_postEncode($data);
180         return $this->get($url,$sloppy304);
181     }
182
183     /**
184      * Simple function to do a POST request
185      *
186      * Returns the resulting page or false on an error;
187      *
188      * @author Andreas Gohr <andi@splitbrain.org>
189      */
190     function post($url,$data){
191         if(!$this->sendRequest($url,$data,'POST')) return false;
192         if($this->status < 200 || $this->status > 206) return false;
193         return $this->resp_body;
194     }
195
196     /**
197      * Send an HTTP request
198      *
199      * This method handles the whole HTTP communication. It respects set proxy settings,
200      * builds the request headers, follows redirects and parses the response.
201      *
202      * Post data should be passed as associative array. When passed as string it will be
203      * sent as is. You will need to setup your own Content-Type header then.
204      *
205      * @param  string $url    - the complete URL
206      * @param  mixed  $data   - the post data either as array or raw data
207      * @param  string $method - HTTP Method usually GET or POST.
208      * @return bool - true on success
209      * @author Andreas Goetz <cpuidle@gmx.de>
210      * @author Andreas Gohr <andi@splitbrain.org>
211      */
212     function sendRequest($url,$data='',$method='GET'){
213         $this->start  = $this->_time();
214         $this->error  = '';
215         $this->status = 0;
216
217         // don't accept gzip if truncated bodies might occur
218         if($this->max_bodysize &&
219            !$this->max_bodysize_abort &&
220            $this->headers['Accept-encoding'] == 'gzip'){
221             unset($this->headers['Accept-encoding']);
222         }
223
224         // parse URL into bits
225         $uri = parse_url($url);
226         $server = $uri['host'];
227         $path   = $uri['path'];
228         if(empty($path)) $path = '/';
229         if(!empty($uri['query'])) $path .= '?'.$uri['query'];
230         if(isset($uri['port']) && !empty($uri['port'])) $port = $uri['port'];
231         if(isset($uri['user'])) $this->user = $uri['user'];
232         if(isset($uri['pass'])) $this->pass = $uri['pass'];
233
234         // proxy setup
235         if($this->proxy_host && (!$this->proxy_except || !preg_match('/'.$this->proxy_except.'/i',$url)) ){
236             $request_url = $url;
237             $server      = $this->proxy_host;
238             $port        = $this->proxy_port;
239             if (empty($port)) $port = 8080;
240         }else{
241             $request_url = $path;
242             $server      = $server;
243             if (!isset($port)) $port = ($uri['scheme'] == 'https') ? 443 : 80;
244         }
245
246         // add SSL stream prefix if needed - needs SSL support in PHP
247         if($port == 443 || $this->proxy_ssl) $server = 'ssl://'.$server;
248
249         // prepare headers
250         $headers               = $this->headers;
251         $headers['Host']       = $uri['host'];
252         if($uri['port']) $headers['Host'].= ':'.$uri['port'];
253         $headers['User-Agent'] = $this->agent;
254         $headers['Referer']    = $this->referer;
255         if ($this->keep_alive) {
256             $headers['Connection'] = 'Keep-Alive';
257         } else {
258             $headers['Connection'] = 'Close';
259         }
260         if($method == 'POST'){
261             if(is_array($data)){
262                 if($headers['Content-Type'] == 'multipart/form-data'){
263                     $headers['Content-Type']   = 'multipart/form-data; boundary='.$this->boundary;
264                     $data = $this->_postMultipartEncode($data);
265                 }else{
266                     $headers['Content-Type']   = 'application/x-www-form-urlencoded';
267                     $data = $this->_postEncode($data);
268                 }
269             }
270             $headers['Content-Length'] = strlen($data);
271             $rmethod = 'POST';
272         }elseif($method == 'GET'){
273             $data = ''; //no data allowed on GET requests
274         }
275         if($this->user) {
276             $headers['Authorization'] = 'Basic '.base64_encode($this->user.':'.$this->pass);
277         }
278         if($this->proxy_user) {
279             $headers['Proxy-Authorization'] = 'Basic '.base64_encode($this->proxy_user.':'.$this->proxy_pass);
280         }
281
282         // stop time
283         $start = time();
284
285         // already connected?
286         $connectionId = $this->_uniqueConnectionId($server,$port);
287         $this->_debug('connection pool', $this->connections);
288         $socket = null;
289         if (isset($this->connections[$connectionId])) {
290             $this->_debug('reusing connection', $connectionId);
291             $socket = $this->connections[$connectionId];
292         }
293         if (is_null($socket) || feof($socket)) {
294             $this->_debug('opening connection', $connectionId);
295             // open socket
296             $socket = @fsockopen($server,$port,$errno, $errstr, $this->timeout);
297             if (!$socket){
298                 $this->status = -100;
299                 $this->error = "Could not connect to $server:$port\n$errstr ($errno)";
300                 return false;
301             }
302
303             // keep alive?
304             if ($this->keep_alive) {
305                 $this->connections[$connectionId] = $socket;
306             } else {
307                 unset($this->connections[$connectionId]);
308             }
309         }
310
311         //set blocking
312         stream_set_blocking($socket,1);
313
314         // build request
315         $request  = "$method $request_url HTTP/".$this->http.HTTP_NL;
316         $request .= $this->_buildHeaders($headers);
317         $request .= $this->_getCookies();
318         $request .= HTTP_NL;
319         $request .= $data;
320
321         $this->_debug('request',$request);
322
323         // select parameters
324         $sel_r = null;
325         $sel_w = array($socket);
326         $sel_e = null;
327
328         // send request
329         $towrite = strlen($request);
330         $written = 0;
331         while($written < $towrite){
332             // check timeout
333             if(time()-$start > $this->timeout){
334                 $this->status = -100;
335                 $this->error = sprintf('Timeout while sending request (%.3fs)',$this->_time() - $this->start);
336                 unset($this->connections[$connectionId]);
337                 return false;
338             }
339
340             // wait for stream ready or timeout (1sec)
341             if(@stream_select($sel_r,$sel_w,$sel_e,1) === false){
342                 usleep(1000);
343                 continue;
344             }
345
346             // write to stream
347             $ret = fwrite($socket, substr($request,$written,4096));
348             if($ret === false){
349                 $this->status = -100;
350                 $this->error = 'Failed writing to socket';
351                 unset($this->connections[$connectionId]);
352                 return false;
353             }
354             $written += $ret;
355         }
356
357         // continue non-blocking
358         stream_set_blocking($socket,0);
359
360         // read headers from socket
361         $r_headers = '';
362         do{
363             if(time()-$start > $this->timeout){
364                 $this->status = -100;
365                 $this->error = sprintf('Timeout while reading headers (%.3fs)',$this->_time() - $this->start);
366                 unset($this->connections[$connectionId]);
367                 return false;
368             }
369             if(feof($socket)){
370                 $this->error = 'Premature End of File (socket)';
371                 unset($this->connections[$connectionId]);
372                 return false;
373             }
374             usleep(1000);
375             $r_headers .= fgets($socket,1024);
376         }while(!preg_match('/\r?\n\r?\n$/',$r_headers));
377
378         $this->_debug('response headers',$r_headers);
379
380         // check if expected body size exceeds allowance
381         if($this->max_bodysize && preg_match('/\r?\nContent-Length:\s*(\d+)\r?\n/i',$r_headers,$match)){
382             if($match[1] > $this->max_bodysize){
383                 $this->error = 'Reported content length exceeds allowed response size';
384                 if ($this->max_bodysize_abort)
385                     unset($this->connections[$connectionId]);
386                     return false;
387             }
388         }
389
390         // get Status
391         if (!preg_match('/^HTTP\/(\d\.\d)\s*(\d+).*?\n/', $r_headers, $m)) {
392             $this->error = 'Server returned bad answer';
393             unset($this->connections[$connectionId]);
394             return false;
395         }
396         $this->status = $m[2];
397
398         // handle headers and cookies
399         $this->resp_headers = $this->_parseHeaders($r_headers);
400         if(isset($this->resp_headers['set-cookie'])){
401             foreach ((array) $this->resp_headers['set-cookie'] as $cookie){
402                 list($cookie)   = explode(';',$cookie,2);
403                 list($key,$val) = explode('=',$cookie,2);
404                 $key = trim($key);
405                 if($val == 'deleted'){
406                     if(isset($this->cookies[$key])){
407                         unset($this->cookies[$key]);
408                     }
409                 }elseif($key){
410                     $this->cookies[$key] = $val;
411                 }
412             }
413         }
414
415         $this->_debug('Object headers',$this->resp_headers);
416
417         // check server status code to follow redirect
418         if($this->status == 301 || $this->status == 302 ){
419             // close the connection because we don't handle content retrieval here
420             // that's the easiest way to clean up the connection
421             fclose($socket);
422             unset($this->connections[$connectionId]);
423
424             if (empty($this->resp_headers['location'])){
425                 $this->error = 'Redirect but no Location Header found';
426                 return false;
427             }elseif($this->redirect_count == $this->max_redirect){
428                 $this->error = 'Maximum number of redirects exceeded';
429                 return false;
430             }else{
431                 $this->redirect_count++;
432                 $this->referer = $url;
433                 // handle non-RFC-compliant relative redirects
434                 if (!preg_match('/^http/i', $this->resp_headers['location'])){
435                     if($this->resp_headers['location'][0] != '/'){
436                         $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port'].
437                                                           dirname($uri['path']).'/'.$this->resp_headers['location'];
438                     }else{
439                         $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port'].
440                                                           $this->resp_headers['location'];
441                     }
442                 }
443                 // perform redirected request, always via GET (required by RFC)
444                 return $this->sendRequest($this->resp_headers['location'],array(),'GET');
445             }
446         }
447
448         // check if headers are as expected
449         if($this->header_regexp && !preg_match($this->header_regexp,$r_headers)){
450             $this->error = 'The received headers did not match the given regexp';
451             unset($this->connections[$connectionId]);
452             return false;
453         }
454
455         //read body (with chunked encoding if needed)
456         $r_body    = '';
457         if(preg_match('/transfer\-(en)?coding:\s*chunked\r\n/i',$r_headers)){
458             do {
459                 unset($chunk_size);
460                 do {
461                     if(feof($socket)){
462                         $this->error = 'Premature End of File (socket)';
463                         unset($this->connections[$connectionId]);
464                         return false;
465                     }
466                     if(time()-$start > $this->timeout){
467                         $this->status = -100;
468                         $this->error = sprintf('Timeout while reading chunk (%.3fs)',$this->_time() - $this->start);
469                         unset($this->connections[$connectionId]);
470                         return false;
471                     }
472                     $byte = fread($socket,1);
473                     $chunk_size .= $byte;
474                 } while (preg_match('/[a-zA-Z0-9]/',$byte)); // read chunksize including \r
475
476                 $byte = fread($socket,1);     // readtrailing \n
477                 $chunk_size = hexdec($chunk_size);
478                 if ($chunk_size) {
479                     $this_chunk = fread($socket,$chunk_size);
480                     $r_body    .= $this_chunk;
481                     $byte = fread($socket,2); // read trailing \r\n
482                 }
483
484                 if($this->max_bodysize && strlen($r_body) > $this->max_bodysize){
485                     $this->error = 'Allowed response size exceeded';
486                     if ($this->max_bodysize_abort){
487                         unset($this->connections[$connectionId]);
488                         return false;
489                     } else {
490                         break;
491                     }
492                 }
493             } while ($chunk_size);
494         }else{
495             // read entire socket
496             while (!feof($socket)) {
497                 if(time()-$start > $this->timeout){
498                     $this->status = -100;
499                     $this->error = sprintf('Timeout while reading response (%.3fs)',$this->_time() - $this->start);
500                     unset($this->connections[$connectionId]);
501                     return false;
502                 }
503                 $r_body .= fread($socket,4096);
504                 $r_size = strlen($r_body);
505                 if($this->max_bodysize && $r_size > $this->max_bodysize){
506                     $this->error = 'Allowed response size exceeded';
507                     if ($this->max_bodysize_abort) {
508                         unset($this->connections[$connectionId]);
509                         return false;
510                     } else {
511                         break;
512                     }
513                 }
514                 if(isset($this->resp_headers['content-length']) &&
515                    !isset($this->resp_headers['transfer-encoding']) &&
516                    $this->resp_headers['content-length'] == $r_size){
517                     // we read the content-length, finish here
518                     break;
519                 }
520             }
521         }
522
523         if (!$this->keep_alive ||
524                 (isset($this->resp_headers['connection']) && $this->resp_headers['connection'] == 'Close')) {
525             // close socket
526             $status = socket_get_status($socket);
527             fclose($socket);
528             unset($this->connections[$connectionId]);
529         }
530
531         // decode gzip if needed
532         if(isset($this->resp_headers['content-encoding']) &&
533            $this->resp_headers['content-encoding'] == 'gzip' &&
534            strlen($r_body) > 10 && substr($r_body,0,3)=="\x1f\x8b\x08"){
535             $this->resp_body = @gzinflate(substr($r_body, 10));
536             if($this->resp_body === false){
537                 $this->error = 'Failed to decompress gzip encoded content';
538                 $this->resp_body = $r_body;
539             }
540         }else{
541             $this->resp_body = $r_body;
542         }
543
544         $this->_debug('response body',$this->resp_body);
545         $this->redirect_count = 0;
546         return true;
547     }
548
549     /**
550      * print debug info
551      *
552      * @author Andreas Gohr <andi@splitbrain.org>
553      */
554     function _debug($info,$var=null){
555         if(!$this->debug) return;
556         print '<b>'.$info.'</b> '.($this->_time() - $this->start).'s<br />';
557         if(!is_null($var)){
558             ob_start();
559             print_r($var);
560             $content = htmlspecialchars(ob_get_contents());
561             ob_end_clean();
562             print '<pre>'.$content.'</pre>';
563         }
564     }
565
566     /**
567      * Return current timestamp in microsecond resolution
568      */
569     function _time(){
570         list($usec, $sec) = explode(" ", microtime());
571         return ((float)$usec + (float)$sec);
572     }
573
574     /**
575      * convert given header string to Header array
576      *
577      * All Keys are lowercased.
578      *
579      * @author Andreas Gohr <andi@splitbrain.org>
580      */
581     function _parseHeaders($string){
582         $headers = array();
583         if (!preg_match_all('/^\s*([\w-]+)\s*:\s*([\S \t]+)\s*$/m', $string,
584                             $matches, PREG_SET_ORDER)) {
585             return $headers;
586         }
587         foreach($matches as $match){
588             list(, $key, $val) = $match;
589             $key = strtolower($key);
590             if(isset($headers[$key])){
591                 if(is_array($headers[$key])){
592                     $headers[$key][] = $val;
593                 }else{
594                     $headers[$key] = array($headers[$key],$val);
595                 }
596             }else{
597                 $headers[$key] = $val;
598             }
599         }
600         return $headers;
601     }
602
603     /**
604      * convert given header array to header string
605      *
606      * @author Andreas Gohr <andi@splitbrain.org>
607      */
608     function _buildHeaders($headers){
609         $string = '';
610         foreach($headers as $key => $value){
611             if(empty($value)) continue;
612             $string .= $key.': '.$value.HTTP_NL;
613         }
614         return $string;
615     }
616
617     /**
618      * get cookies as http header string
619      *
620      * @author Andreas Goetz <cpuidle@gmx.de>
621      */
622     function _getCookies(){
623         $headers = '';
624         foreach ($this->cookies as $key => $val){
625             $headers .= "$key=$val; ";
626         }
627         $headers = substr($headers, 0, -2);
628         if ($headers !== '') $headers = "Cookie: $headers".HTTP_NL;
629         return $headers;
630     }
631
632     /**
633      * Encode data for posting
634      *
635      * @author Andreas Gohr <andi@splitbrain.org>
636      */
637     function _postEncode($data){
638         $url = '';
639         foreach($data as $key => $val){
640             if($url) $url .= '&';
641             $url .= urlencode($key).'='.urlencode($val);
642         }
643         return $url;
644     }
645
646     /**
647      * Encode data for posting using multipart encoding
648      *
649      * @fixme use of urlencode might be wrong here
650      * @author Andreas Gohr <andi@splitbrain.org>
651      */
652     function _postMultipartEncode($data){
653         $boundary = '--'.$this->boundary;
654         $out = '';
655         foreach($data as $key => $val){
656             $out .= $boundary.HTTP_NL;
657             if(!is_array($val)){
658                 $out .= 'Content-Disposition: form-data; name="'.urlencode($key).'"'.HTTP_NL;
659                 $out .= HTTP_NL; // end of headers
660                 $out .= $val;
661                 $out .= HTTP_NL;
662             }else{
663                 $out .= 'Content-Disposition: form-data; name="'.urlencode($key).'"';
664                 if($val['filename']) $out .= '; filename="'.urlencode($val['filename']).'"';
665                 $out .= HTTP_NL;
666                 if($val['mimetype']) $out .= 'Content-Type: '.$val['mimetype'].HTTP_NL;
667                 $out .= HTTP_NL; // end of headers
668                 $out .= $val['body'];
669                 $out .= HTTP_NL;
670             }
671         }
672         $out .= "$boundary--".HTTP_NL;
673         return $out;
674     }
675
676     /**
677      * Generates a unique identifier for a connection.
678      *
679      * @return string unique identifier
680      */
681     function _uniqueConnectionId($server, $port) {
682         return "$server:$port";
683     }
684 }
685
686 //Setup VIM: ex: et ts=4 :