Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Yadis

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /openid/Services/Yadis/ParanoidHTTPFetcher.php

Documentation is available at ParanoidHTTPFetcher.php

  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. <[email protected]>
  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.     {
  29.         $this->reset();
  30.     }
  31.  
  32.     function reset()
  33.     {
  34.         $this->headers array();
  35.         $this->data "";
  36.     }
  37.  
  38.     /**
  39.      * @access private
  40.      */
  41.     function _writeHeader($ch$header)
  42.     {
  43.         array_push($this->headersrtrim($header));
  44.         return strlen($header);
  45.     }
  46.  
  47.     /**
  48.      * @access private
  49.      */
  50.     function _writeData($ch$data)
  51.     {
  52.         $this->data .= $data;
  53.         return strlen($data);
  54.     }
  55.  
  56.     function get($url$extra_headers null)
  57.     {
  58.         $stop time($this->timeout;
  59.         $off $this->timeout;
  60.  
  61.         $redir true;
  62.  
  63.         while ($redir && ($off 0)) {
  64.             $this->reset();
  65.  
  66.             $c curl_init();
  67.             if (defined('CURLOPT_NOSIGNAL')) {
  68.                 curl_setopt($cCURLOPT_NOSIGNALtrue);
  69.             }
  70.  
  71.             if (!$this->allowedURL($url)) {
  72.                 trigger_error(sprintf("Fetching URL not allowed: %s"$url),
  73.                               E_USER_WARNING);
  74.                 return null;
  75.             }
  76.  
  77.             curl_setopt($cCURLOPT_WRITEFUNCTION,
  78.                         array(&$this"_writeData"));
  79.             curl_setopt($cCURLOPT_HEADERFUNCTION,
  80.                         array(&$this"_writeHeader"));
  81.  
  82.             if ($extra_headers{
  83.                 curl_setopt($cCURLOPT_HTTPHEADER$extra_headers);
  84.             }
  85.  
  86.             curl_setopt($cCURLOPT_TIMEOUT$off);
  87.             curl_setopt($cCURLOPT_URL$url);
  88.  
  89.             curl_exec($c);
  90.  
  91.             $code curl_getinfo($cCURLINFO_HTTP_CODE);
  92.             $body $this->data;
  93.             $headers $this->headers;
  94.  
  95.             if (!$code{
  96.                 return null;
  97.             }
  98.  
  99.             if (in_array($codearray(301302303307))) {
  100.                 $url $this->_findRedirect($headers);
  101.                 $redir true;
  102.             else {
  103.                 $redir false;
  104.                 curl_close($c);
  105.  
  106.                 $new_headers array();
  107.  
  108.                 foreach ($headers as $header{
  109.                     if (preg_match("/:/"$header)) {
  110.                         list($name$valueexplode(": "$header2);
  111.                         $new_headers[$name$value;
  112.                     }
  113.                 }
  114.  
  115.                 return new Services_Yadis_HTTPResponse($url$code,
  116.                                                     $new_headers$body);
  117.             }
  118.  
  119.             $off $stop time();
  120.         }
  121.  
  122.         trigger_error(sprintf("Timed out fetching: %s"$url),
  123.                       E_USER_WARNING);
  124.  
  125.         return null;
  126.     }
  127.  
  128.     function post($url$body)
  129.     {
  130.         $this->reset();
  131.  
  132.         if (!$this->allowedURL($url)) {
  133.             trigger_error(sprintf("Fetching URL not allowed: %s"$url),
  134.                           E_USER_WARNING);
  135.             return null;
  136.         }
  137.  
  138.         $c curl_init();
  139.  
  140.         curl_setopt($cCURLOPT_NOSIGNALtrue);
  141.         curl_setopt($cCURLOPT_POSTtrue);
  142.         curl_setopt($cCURLOPT_POSTFIELDS$body);
  143.         curl_setopt($cCURLOPT_TIMEOUT$this->timeout);
  144.         curl_setopt($cCURLOPT_URL$url);
  145.         curl_setopt($cCURLOPT_WRITEFUNCTION,
  146.                     array(&$this"_writeData"));
  147.  
  148.         curl_exec($c);
  149.  
  150.         $code curl_getinfo($cCURLINFO_HTTP_CODE);
  151.  
  152.         if (!$code{
  153.             trigger_error("No HTTP code returned"E_USER_WARNING);
  154.             return null;
  155.         }
  156.  
  157.         $body $this->data;
  158.  
  159.         curl_close($c);
  160.  
  161.         $new_headers array();
  162.  
  163.         foreach ($this->headers as $header{
  164.             if (preg_match("/:/"$header)) {
  165.                 list($name$valueexplode(": "$header2);
  166.                 $new_headers[$name$value;
  167.             }
  168.  
  169.         }
  170.  
  171.         return new Services_Yadis_HTTPResponse($url$code,
  172.                                                $new_headers$body);
  173.     }
  174. }
  175.  
  176. ?>

Documentation generated on Mon, 05 Mar 2007 21:13:26 +0000 by phpDocumentor 1.3.1