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/PlainHTTPFetcher.php

Documentation is available at 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 Yadis
  12.  * @author JanRain, Inc. <[email protected]>
  13.  * @copyright 2005 Janrain, Inc.
  14.  * @license http://www.gnu.org/copyleft/lesser.html LGPL
  15.  */
  16.  
  17. /**
  18.  * Interface import
  19.  */
  20. require_once "Services/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 Yadis
  27.  */
  28. class Services_Yadis_PlainHTTPFetcher extends Services_Yadis_HTTPFetcher {
  29.     function get($url$extra_headers null)
  30.     {
  31.         if (!$this->allowedURL($url)) {
  32.             trigger_error("Bad URL scheme in url: " $url,
  33.                           E_USER_WARNING);
  34.             return null;
  35.         }
  36.  
  37.         $redir true;
  38.  
  39.         $stop time($this->timeout;
  40.         $off $this->timeout;
  41.  
  42.         while ($redir && ($off 0)) {
  43.  
  44.             $parts parse_url($url);
  45.  
  46.             $specify_port true;
  47.  
  48.             // Set a default port.
  49.             if (!array_key_exists('port'$parts)) {
  50.                 $specify_port false;
  51.                 if ($parts['scheme'== 'http'{
  52.                     $parts['port'80;
  53.                 elseif ($parts['scheme'== 'https'{
  54.                     $parts['port'443;
  55.                 else {
  56.                     trigger_error("fetcher post method doesn't support " .
  57.                                   " scheme '" $parts['scheme'.
  58.                                   "', no default port available",
  59.                                   E_USER_WARNING);
  60.                     return null;
  61.                 }
  62.             }
  63.  
  64.             $host $parts['host'];
  65.  
  66.             if ($parts['scheme'== 'https'{
  67.                 $host 'ssl://' $host;
  68.             }
  69.  
  70.             $user_agent "PHP Yadis Library Fetcher";
  71.  
  72.             $headers array(
  73.                              "GET ".$parts['path'].
  74.                              (array_key_exists('query'$parts?
  75.                               "?".$parts['query'"").
  76.                                  " HTTP/1.0",
  77.                              "User-Agent$user_agent",
  78.                              "Host: ".$parts['host'].
  79.                                 ($specify_port ":".$parts['port'""),
  80.                              "Port: ".$parts['port']);
  81.  
  82.             $errno 0;
  83.             $errstr '';
  84.  
  85.             if ($extra_headers{
  86.                 foreach ($extra_headers as $h{
  87.                     $headers[$h;
  88.                 }
  89.             }
  90.  
  91.             @$sock fsockopen($host$parts['port']$errno$errstr,
  92.                                $this->timeout);
  93.             if ($sock === false{
  94.                 return false;
  95.             }
  96.  
  97.             stream_set_timeout($sock$this->timeout);
  98.  
  99.             fputs($sockimplode("\r\n"$headers"\r\n\r\n");
  100.  
  101.             $data "";
  102.             while (!feof($sock)) {
  103.                 $data .= fgets($sock1024);
  104.             }
  105.  
  106.             fclose($sock);
  107.  
  108.             // Split response into header and body sections
  109.             list($headers$bodyexplode("\r\n\r\n"$data2);
  110.             $headers explode("\r\n"$headers);
  111.  
  112.             $http_code explode(" "$headers[0]);
  113.             $code $http_code[1];
  114.  
  115.             if (in_array($codearray('301''302'))) {
  116.                 $url $this->_findRedirect($headers);
  117.                 $redir true;
  118.             else {
  119.                 $redir false;
  120.             }
  121.  
  122.             $off $stop time();
  123.         }
  124.  
  125.         $new_headers array();
  126.  
  127.         foreach ($headers as $header{
  128.             if (preg_match("/:/"$header)) {
  129.                 list($name$valueexplode(": "$header2);
  130.                 $new_headers[$name$value;
  131.             }
  132.  
  133.         }
  134.  
  135.         return new Services_Yadis_HTTPResponse($url$code$new_headers$body);
  136.     }
  137.  
  138.     function post($url$body$extra_headers null)
  139.     {
  140.         if (!$this->allowedURL($url)) {
  141.             trigger_error("Bad URL scheme in url: " $url,
  142.                           E_USER_WARNING);
  143.             return null;
  144.         }
  145.  
  146.         $parts parse_url($url);
  147.  
  148.         $headers array();
  149.  
  150.         $headers["POST ".$parts['path']." HTTP/1.0";
  151.         $headers["Host: " $parts['host'];
  152.         $headers["Content-type: application/x-www-form-urlencoded";
  153.         $headers["Content-length: " strval(strlen($body));
  154.  
  155.         if ($extra_headers &&
  156.             is_array($extra_headers)) {
  157.             $headers array_merge($headers$extra_headers);
  158.         }
  159.  
  160.         // Join all headers together.
  161.         $all_headers implode("\r\n"$headers);
  162.  
  163.         // Add headers, two newlines, and request body.
  164.         $request $all_headers "\r\n\r\n" $body;
  165.  
  166.         // Set a default port.
  167.         if (!array_key_exists('port'$parts)) {
  168.             if ($parts['scheme'== 'http'{
  169.                 $parts['port'80;
  170.             elseif ($parts['scheme'== 'https'{
  171.                 $parts['port'443;
  172.             else {
  173.                 trigger_error("fetcher post method doesn't support scheme '" .
  174.                               $parts['scheme'.
  175.                               "', no default port available",
  176.                               E_USER_WARNING);
  177.                 return null;
  178.             }
  179.         }
  180.  
  181.         if ($parts['scheme'== 'https'{
  182.             $parts['host'sprintf("ssl://%s"$parts['host']);
  183.         }
  184.  
  185.         // Connect to the remote server.
  186.         $errno 0;
  187.         $errstr '';
  188.  
  189.         $sock fsockopen($parts['host']$parts['port']$errno$errstr,
  190.                           $this->timeout);
  191.  
  192.         if ($sock === false{
  193.             trigger_error("Could not connect to " $parts['host'.
  194.                           " port " $parts['port'],
  195.                           E_USER_WARNING);
  196.             return null;
  197.         }
  198.  
  199.         stream_set_timeout($sock$this->timeout);
  200.  
  201.         // Write the POST request.
  202.         fputs($sock$request);
  203.  
  204.         // Get the response from the server.
  205.         $response "";
  206.         while (!feof($sock)) {
  207.             if ($data fgets($sock128)) {
  208.                 $response .= $data;
  209.             else {
  210.                 break;
  211.             }
  212.         }
  213.  
  214.         // Split the request into headers and body.
  215.         list($headers$response_bodyexplode("\r\n\r\n"$response2);
  216.  
  217.         $headers explode("\r\n"$headers);
  218.  
  219.         // Expect the first line of the headers data to be something
  220.         // like HTTP/1.1 200 OK.  Split the line on spaces and take
  221.         // the second token, which should be the return code.
  222.         $http_code explode(" "$headers[0]);
  223.         $code $http_code[1];
  224.  
  225.         $new_headers array();
  226.  
  227.         foreach ($headers as $header{
  228.             if (preg_match("/:/"$header)) {
  229.                 list($name$valueexplode(": "$header2);
  230.                 $new_headers[$name$value;
  231.             }
  232.  
  233.         }
  234.  
  235.         return new Services_Yadis_HTTPResponse($url$code,
  236.                                                $headers$response_body);
  237.     }
  238. }
  239.  
  240. ?>

Documentation generated on Mon, 05 Mar 2007 21:18:47 +0000 by phpDocumentor 1.3.1