• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

cms/openid/Services/Yadis/ParanoidHTTPFetcher.php

Go to the documentation of this file.
00001 <?php
00002 
00019 require_once "Services/Yadis/HTTPFetcher.php";
00020 
00024 define('Services_Yadis_CURL_PRESENT', function_exists('curl_init'));
00025 
00032 class Services_Yadis_ParanoidHTTPFetcher extends Services_Yadis_HTTPFetcher {
00033     function Services_Yadis_ParanoidHTTPFetcher($timeout)
00034     {
00035         if (!Services_Yadis_CURL_PRESENT) {
00036             trigger_error("Cannot use this class; CURL extension not found",
00037                           E_USER_ERROR);
00038         }
00039 
00040         $this->timeout = $timeout;
00041         $this->headers = array();
00042         $this->data = "";
00043 
00044         $this->reset();
00045     }
00046 
00047     function reset()
00048     {
00049         $this->headers = array();
00050         $this->data = "";
00051     }
00052 
00056     function _writeHeader($ch, $header)
00057     {
00058         array_push($this->headers, rtrim($header));
00059         return strlen($header);
00060     }
00061 
00065     function _writeData($ch, $data)
00066     {
00067         $this->data .= $data;
00068         return strlen($data);
00069     }
00070 
00083     function get($url, $extra_headers = null)
00084     {
00085         $stop = time() + $this->timeout;
00086         $off = $this->timeout;
00087 
00088         $redir = true;
00089 
00090         while ($redir && ($off > 0)) {
00091             $this->reset();
00092 
00093             $c = curl_init();
00094             curl_setopt($c, CURLOPT_NOSIGNAL, true);
00095 
00096             if (!$this->allowedURL($url)) {
00097                 trigger_error(sprintf("Fetching URL not allowed: %s", $url),
00098                               E_USER_WARNING);
00099                 return null;
00100             }
00101 
00102             curl_setopt($c, CURLOPT_WRITEFUNCTION,
00103                         array(&$this, "_writeData"));
00104             curl_setopt($c, CURLOPT_HEADERFUNCTION,
00105                         array(&$this, "_writeHeader"));
00106 
00107             if ($extra_headers) {
00108                 curl_setopt($c, CURLOPT_HTTPHEADER, $extra_headers);
00109             }
00110 
00111             curl_setopt($c, CURLOPT_TIMEOUT, $off);
00112             curl_setopt($c, CURLOPT_URL, $url);
00113             curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
00114 
00115             curl_exec($c);
00116 
00117             $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
00118             $body = $this->data;
00119             $headers = $this->headers;
00120 
00121             if (!$code) {
00122                 trigger_error("No HTTP code returned", E_USER_WARNING);
00123                 return null;
00124             }
00125 
00126             if (in_array($code, array(301, 302, 303, 307))) {
00127                 $url = $this->_findRedirect($headers);
00128                 $redir = true;
00129             } else {
00130                 $redir = false;
00131                 curl_close($c);
00132 
00133                 $new_headers = array();
00134                 foreach ($headers as $header) {
00135                     list($name, $value) = explode(": ", $header, 2);
00136                     $new_headers[$name] = $value;
00137                 }
00138 
00139                 return new Services_Yadis_HTTPResponse($url, $code,
00140                                                        $new_headers, $body);
00141             }
00142 
00143             $off = $stop - time();
00144         }
00145 
00146         trigger_error(sprintf("Timed out fetching: %s", $url),
00147                       E_USER_WARNING);
00148 
00149         return null;
00150     }
00151 }
00152 
00153 ?>

Generated on Sun Jan 2 2011 04:55:32 for Pragyan CMS by  doxygen 1.7.1