[ Index ]
 

Code source de Joomla 1.0.13

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/includes/domit/ -> php_http_client_generic.php (source)

   1  <?php
   2  /**
   3  * PHP HTTP Tools is a library for working with the http protocol
   4  * php_http_client_generic represents a basic http client
   5  * @package php-http-tools
   6  * @version 0.3
   7  * @copyright (C) 2004 John Heinstein. All rights reserved
   8  * @license http://www.gnu.org/copyleft/lesser.html LGPL License
   9  * @author John Heinstein <johnkarl@nbnet.nb.ca>
  10  * @link http://www.engageinteractive.com/php_http_tools/ PHP HTTP Tools Home Page
  11  * PHP HTTP Tools are Free Software
  12  **/
  13  
  14  if (!defined('PHP_HTTP_TOOLS_INCLUDE_PATH')) {
  15      define('PHP_HTTP_TOOLS_INCLUDE_PATH', (dirname(__FILE__) . "/"));
  16  }
  17  
  18  /** end-of-line character sequence as defined in HTTP spec */
  19  define ('CRLF', "\r\n");
  20  /** carriage return character */
  21  define ('CR', "\r");
  22  /** line feed character */
  23  define ('LF', "\n");
  24  
  25  //http read states for client
  26  /** beginning read state */
  27  define('HTTP_READ_STATE_BEGIN', 1);
  28  /** state when reading headers */
  29  define('HTTP_READ_STATE_HEADERS', 2);
  30  /** state when reading body of message */
  31  define('HTTP_READ_STATE_BODY', 3);
  32  
  33  require_once (PHP_HTTP_TOOLS_INCLUDE_PATH . 'php_http_exceptions.php');
  34  
  35  /**
  36  
  37  * An HTTP Request class
  38  *
  39  * @package php-http-tools
  40  * @author John Heinstein <johnkarl@nbnet.nb.ca>
  41  */
  42  class php_http_request {
  43      /** @var object A reference to the headers object */
  44      var $headers = null;
  45      /** @var string The requested method, e.g. GET, POST, HEAD */
  46      var $requestMethod = 'POST';
  47      /** @var string The requested path */
  48      var $requestPath = '';
  49      /** @var string The requested protocol */
  50      var $protocol = 'HTTP';
  51      /** @var string The version of the requested protocol */
  52      var $protocolVersion= '1.1';
  53  
  54      /**
  55      * Returns the headers object
  56      * @return object The headers object
  57      */
  58      function &getHeaders() {
  59          return $this->headers;
  60      } //getHeaders
  61  
  62      /**
  63      * Sets the header to the specified value
  64      * @param string The header name
  65      * @param string The header value
  66      * @param boolean True if multiple headers with the same name are allowed
  67      */
  68  	function setHeader($name, $value, $allowMultipleHeaders = false) {
  69          $this->headers->setHeader($name, $value, $allowMultipleHeaders);
  70      } //setHeader
  71  
  72      /**
  73      * Default method for setting headers; meant to be overridden in subclasses
  74      */
  75  	function setHeaders() {
  76          //you will want to override this method
  77          $this->setHeader('User-Agent', 'PHP-HTTP-Client(Generic)/0.1');
  78          $this->setHeader('Connection', 'Close');
  79      } //setHeaders
  80  
  81      /**
  82      * Sets the request method, e.g., GET
  83      * @param string The name of the request method
  84      * @return boolean True if the version number is valid
  85      */
  86  	function setRequestMethod($method) {
  87          $method = strtoupper($method);
  88  
  89          switch ($method) {
  90              case 'POST':
  91              case 'GET':
  92              case 'HEAD':
  93              case 'PUT':
  94                  $this->requestMethod = $method;
  95                  return true;
  96                  break;
  97          }
  98  
  99          return false;
 100      } //setRequestMethod
 101  
 102      /**
 103      * Sets the request path, e.g., http://www.engageinteractive.com/domit/test.xml
 104      * @param string The request path
 105      */
 106  	function setRequestPath($path) {
 107          $this->requestPath = $path;
 108      } //setRequestPath
 109  
 110      /**
 111      * Sets the version number of the protocol
 112      * @param string The version number
 113      * @return boolean True if the version number is valid
 114      */
 115  	function setProtocolVersion($version) {
 116          if (($version == '1.0') || ($version == '1.1')) {
 117              $this->protocolVersion = $version;
 118              return true;
 119          }
 120  
 121          return false;
 122      } //setProtocolVersion
 123  
 124      /**
 125      * Specifies a user name and password for basic authentication
 126      * @param string The user name
 127      * @param string The password
 128      */
 129  	function setAuthorization($user, $password) {
 130          $encodedChallengeResponse = 'Basic ' . base64_encode($this->user . ':' . $this->password);
 131          $this->setHeader('Authorization', $encodedChallengeResponse);
 132      } //setAuthorization
 133  } //php_http_request
 134  
 135  
 136  class php_http_client_generic extends php_http_request {
 137      /** @var object A reference to the connection object */
 138      var $connection;
 139      /** @var string True if response headers are to be generated as an object */
 140      var $responseHeadersAsObject = false;
 141      /** @var object The http response */
 142      var $response = null;
 143      /** @var string A list of event names that can be fired by the client */
 144      var $events = array('onRequest' => null, 'onRead' => null,
 145                          'onResponse' => null, 'onResponseHeaders' => null,
 146                          'onResponseBody' => null);
 147  
 148      /**
 149      * HTTP Client constructor
 150      * @param string The client connection host name, with or without its protocol prefix
 151      * @param string The client connection path, not including the host name
 152      * @param int The port to establish the client connection on
 153      * @param int The timeout value for the client connection
 154      */
 155  	function php_http_client_generic($host = '', $path = '/', $port = 80, $timeout = 0) {
 156          $this->connection = new php_http_connection($host, $path, $port, $timeout);
 157          $this->headers = new php_http_headers();
 158          $this->requestPath = $path;
 159          $this->response = new php_http_response();
 160          $this->setHeaders();
 161      } //php_http_client_generic
 162  
 163      /**
 164      * Specifies that the response headers array should be generated
 165      * @param boolean True if the response headers array should be built
 166      */
 167  	function generateResponseHeadersAsObject($responseHeadersAsObject) {
 168          $this->responseHeadersAsObject = $responseHeadersAsObject;
 169  
 170          if ($responseHeadersAsObject) {
 171              $this->response->headers = new php_http_headers();
 172          }
 173      } //generateResponseHeadersAsObject
 174  
 175      /**
 176      * Fires an http event that has been registered
 177      * @param string The name of the event, e.g., onRead
 178      * @param string The data to be passed to the event
 179      */
 180  	function fireEvent($target, $data) {
 181          if ($this->events[$target] != null) {
 182              call_user_func($this->events[$target], $data);
 183          }
 184      } //fireEvent
 185  
 186      /**
 187      * Sets which http events are to be fired
 188      * @param string The http event option to be set
 189      * @param string True if the event is to be fired
 190      * @param object A reference to a custom handler for the http event data
 191      */
 192  	function setHTTPEvent($option, $truthVal, $customHandler = null) {
 193          if ($customHandler != null) {
 194              $handler =& $customHandler;
 195          }
 196          else {
 197              $handler = array(&$this, 'defaultHTTPEventHandler');
 198          }
 199  
 200          switch($option) {
 201              case 'onRequest':
 202              case 'onRead':
 203              case 'onResponse':
 204              case 'onResponseHeaders':
 205              case 'onResponseBody':
 206                  $truthVal ? ($this->events[$option] =& $handler) : ($this->events[$option] = null);
 207                  break;
 208          }
 209      } //setHTTPEvent
 210  
 211      /**
 212      * Evaluates whether the specified http event option is active
 213      * @param string The http event option to evaluate
 214      * @return boolean True if the specified option is active
 215      */
 216  	function getHTTPEvent($option) {
 217          switch($option) {
 218              case 'onRequest':
 219              case 'onRead':
 220              case 'onResponse':
 221              case 'onResponseHeaders':
 222              case 'onResponseBody':
 223                  return ($this->events[$option] != null);
 224                  break;
 225          }
 226      } //getHTTPEvent
 227  
 228      /**
 229      * The default http event handler; fired if no custom handler has been registered
 230      * @param string The event data
 231      */
 232  	function defaultHTTPEventHandler($data) {
 233          $this->printHTML($data);
 234      } //defaultHTTPEventHandler
 235  
 236      /**
 237      * Prints the data to the browser as preformatted, htmlentified output
 238      * @param string The data to be printed
 239      */
 240  	function printHTML($html) {
 241          print('<pre>' . htmlentities($html)  . '</pre>');
 242      } //printHTML
 243  
 244      /**
 245      * Establishes a client connection
 246      */
 247  	function connect() {
 248          if (!$this->headers->headerExists('Host')) {
 249              $this->setHeader('Host', $this->connection->host);
 250          }
 251  
 252          return $this->connection->connect();
 253      } //connect
 254  
 255      /**
 256      * Disconnects the current client connection if one exists
 257      */
 258  	function disconnect() {
 259          return $this->connection->disconnect();
 260      } //disconnect
 261  
 262      /**
 263      * Evaluated whether the current client is connected
 264      * @return boolean True if a connection exists
 265      */
 266  	function isConnected() {
 267          return $this->connection->isOpen();
 268      } //isConnected
 269  
 270      /**
 271      * Performs an HTTP GET
 272      * @param string The target url
 273      * @return object An HTTP response object
 274      */
 275      function &get($url) {
 276          $this->setRequestMethod('GET');
 277          $this->setRequestPath($url);
 278  
 279          $this->get_custom($url);
 280  
 281          $this->connect();
 282  
 283          $result = $this->send('');
 284  
 285          return $result;
 286      } //get
 287  
 288      /**
 289      * Handler for customizing the HTTP GET call
 290      * @param string The target url
 291      */
 292  	function get_custom($url) {
 293          //do nothing; meant to be overridden
 294      } //get_custom
 295  
 296      /**
 297      * Performs an HTTP POST
 298      * @param string The posted data
 299      * @return object An HTTP response object
 300      */
 301      function &post($data) {
 302          $this->setRequestMethod('POST');
 303          $this->setHeader('Content-Type', 'text/html');
 304          $this->post_custom($data);
 305  
 306          $this->connect();
 307  
 308          return $this->send($data);
 309      } //post
 310  
 311      /**
 312      * Handler for customizing the HTTP POST call
 313      * @param string The post data
 314      */
 315  	function post_custom($data) {
 316          //do nothing; meant to be overridden
 317      } //post_custom
 318  
 319      /**
 320      * Performs an HTTP HEAD
 321      * @param string The target url
 322      * @return object An HTTP response object
 323      */
 324      function &head($url) {
 325          $this->setRequestMethod('HEAD');
 326          $this->head_custom($url);
 327  
 328          $this->connect();
 329  
 330          return $this->send('');
 331      } //head
 332  
 333      /**
 334      * Handler for customizing the HTTP HEAD call
 335      * @param string The target url
 336      */
 337  	function head_custom($url) {
 338          //do nothing; meant to be overridden
 339      } //head_custom
 340  
 341      /**
 342      * Sends data through the client connection
 343      * @param string The message to be sent
 344      * @return string The http response
 345      */
 346  	function send($message) {
 347          $conn =& $this->connection;
 348  
 349          if ($conn->isOpen()) {
 350              //build header info
 351              $request = $this->requestMethod  . ' '  . $this->requestPath . ' '  . $this->protocol .
 352                              '/' . $this->protocolVersion . CRLF;
 353              $request .= $this->headers->toString() . CRLF;
 354              $request .= $message;
 355  
 356              //init variables
 357              $response = $headers = $body = '';
 358              $readState = HTTP_READ_STATE_BEGIN;
 359  
 360              $this->fireEvent('onRequest', $request);
 361  
 362              //send request
 363              $connResource =& $conn->connection;
 364              fputs ($connResource, $request);
 365  
 366              //read response
 367              while (!feof($connResource)) {
 368                  $data = fgets($connResource, 4096);
 369                  $this->fireEvent('onRead', $data);
 370  
 371                  switch ($readState) {
 372                      case HTTP_READ_STATE_BEGIN:
 373                          $this->response->statusLine = $data;
 374                          $readState = HTTP_READ_STATE_HEADERS;
 375                          break;
 376  
 377                      case HTTP_READ_STATE_HEADERS:
 378                          if (trim($data) == '') { //end of headers is signalled by a blank line
 379                              $readState = HTTP_READ_STATE_BODY;
 380                          }
 381                          else {
 382                              if ($this->responseHeadersAsObject) {
 383                                  $this->response->setUnformattedHeader($data);
 384                              }
 385                              else {
 386                                  $this->response->headers .= $data;
 387                              }
 388                          }
 389                          break;
 390  
 391                      case HTTP_READ_STATE_BODY:
 392                          $this->response->message .= $data;
 393                          break;
 394                  }
 395              }
 396  
 397              $this->normalizeResponseIfChunked();
 398  
 399              $headerString = is_object($this->response->headers) ?
 400                          $this->response->headers->toString() : $this->response->headers;
 401  
 402              $this->fireEvent('onResponseHeaders', $headerString);
 403              $this->fireEvent('onResponseBody', $this->response->message);
 404  
 405              $this->fireEvent('onResponse', $this->response->headers . $this->response->message);
 406  
 407              return $this->response;
 408          }
 409          else {
 410              HTTPExceptions::raiseException(HTTP_SOCKET_CONNECTION_ERR, ('HTTP Transport Error - Unable to establish connection to host ' .
 411                          $conn->host));
 412          }
 413      } //send
 414  
 415      /**
 416      * Determines if response data is transfer encoding chunked, then decodes
 417      */
 418  	function normalizeResponseIfChunked() {
 419          if (($this->protocolVersion = '1.1') && (!$this->response->isResponseChunkDecoded)) {
 420              if ($this->responseHeadersAsObject) {
 421                  if ($this->response->headers->headerExists('Transfer-Encoding') &&
 422                      ($this->response->headers->getHeader('Transfer-Encoding') == 'chunked')) {
 423                      $this->response->message = $this->decodeChunkedData($this->response->getResponse());
 424                      $this->response->isResponseChunkDecoded = true;
 425                  }
 426              }
 427              else {
 428                  if ((strpos($this->response->headers, 'Transfer-Encoding') !== false) &&
 429                      (strpos($this->response->headers, 'chunked') !== false)){
 430                      $this->response->message = $this->decodeChunkedData($this->response->getResponse());
 431                      $this->response->isResponseChunkDecoded = true;
 432                  }
 433              }
 434          }
 435      } //normalizeResponseIfChunked
 436  
 437      /**
 438      * Decodes data if transfer encoding chunked
 439      * @param string The encoded data
 440      * @return string The decoded data
 441      */
 442  	function decodeChunkedData($data) {
 443          $chunkStart = $chunkEnd = strpos($data, CRLF) + 2;
 444          $chunkLengthInHex = substr($data, 0, $chunkEnd);
 445          $chunkLength = hexdec(trim($chunkLengthInHex));
 446  
 447          $decodedData = '';
 448  
 449          while ($chunkLength > 0) {
 450              $chunkEnd = strpos($data, CRLF, ($chunkStart + $chunkLength));
 451  
 452              if (!$chunkEnd) {
 453                  //if the trailing CRLF is missing, return all the remaining data
 454                  $decodedData .= substr($data, $chunkStart);
 455                  break;
 456              }
 457  
 458              $decodedData .= substr($data, $chunkStart, ($chunkEnd - $chunkStart));
 459              $chunkStart = $chunkEnd + 2;
 460              $chunkEnd = strpos($data, CRLF, $chunkStart) + 2;
 461  
 462              if (!$chunkEnd) break;
 463  
 464              $chunkLengthInHex = substr($data, $chunkStart, ($chunkEnd - $chunkStart));
 465              $chunkLength = hexdec(trim($chunkLengthInHex));
 466              $chunkStart = $chunkEnd;
 467          }
 468  
 469          return $decodedData;
 470      } //decodeChunkedData
 471  } //php_http_client_generic
 472  
 473  
 474  /**
 475  * An HTTP Connection class
 476  *
 477  * @package php-http-tools
 478  * @author John Heinstein <johnkarl@nbnet.nb.ca>
 479  */
 480  class php_http_connection {
 481      /** @var object A reference to the current connection */
 482      var $connection = null;
 483      /** @var string The host of the connection */
 484      var $host;
 485      /** @var string The path of the connection */
 486      var $path;
 487      /** @var int The port of the connection */
 488      var $port;
 489      /** @var int The timeout value for the connection */
 490      var $timeout;
 491      /** @var int The error number of the connection */
 492      var $errorNumber = 0;
 493      /** @var string The error string of the connection */
 494      var $errorString = '';
 495  
 496      /**
 497      * HTTP Connection constructor
 498      * @param string The connection host name, with or without its protocol prefix
 499      * @param string The connection path, not including the host name
 500      * @param int The port to establish the client connection on
 501      * @param int The timeout value for the client connection
 502      */
 503  	function php_http_connection($host = '', $path = '/', $port = 80, $timeout = 0) {
 504          $this->host = $this->formatHost($host);
 505          $this->path = $this->formatPath($path);
 506          $this->port = $port;
 507          $this->timeout = $timeout;
 508      } //php_http_connection
 509  
 510      /**
 511      * Formats a host string by stripping off the http:// prefix
 512      * @param string The host name
 513      * @return string The formatted host name
 514      */
 515  	function formatHost($hostString) {
 516          $hasProtocol = (substr(strtoupper($hostString), 0, 7) == 'HTTP://');
 517  
 518          if ($hasProtocol) {
 519              $hostString = substr($hostString, 7);
 520          }
 521  
 522          return $hostString;
 523      } //formatHost
 524  
 525      /**
 526      * Formats a path string
 527      * @param string The path
 528      * @return string The formatted path
 529      */
 530  	function formatPath($pathString) {
 531          if (($pathString == '') || ($pathString == null)) {
 532              $pathString = '/';
 533          }
 534  
 535          return $pathString;
 536      } //formatPath
 537  
 538      /**
 539      * Establishes a socket connection
 540      * @return boolean True if the connection was successful
 541      */
 542  	function connect() {
 543          if ($this->timeout == 0) {
 544              $this->connection = @fsockopen($this->host, $this->port, $errorNumber, $errorString);
 545          }
 546          else {
 547              $this->connection = @fsockopen($this->host, $this->port, $errorNumber, $errorString, $this->timeout);
 548          }
 549  
 550          $this->errorNumber = $errorNumber;
 551          $this->errorString = $errorString;
 552  
 553          return is_resource($this->connection);
 554      } //connect
 555  
 556      /**
 557      * Determines whether the connection is still open
 558      * @return boolean True if the connection is still open
 559      */
 560  	function isOpen() {
 561          return (is_resource($this->connection) && (!feof($this->connection)));
 562      } //isOpen
 563  
 564      /**
 565      * Disconnects the current connection
 566      * @return boolean True if the connection has been disconnected
 567      */
 568  	function disconnect() {
 569          fclose($this->connection);
 570          $this->connection = null;
 571          return true;
 572      } //disconnect
 573  } //php_http_connection
 574  
 575  /**
 576  * An HTTP Headers class
 577  *
 578  * @package php-http-tools
 579  * @author John Heinstein <johnkarl@nbnet.nb.ca>
 580  */
 581  class php_http_headers {
 582      /** @var object An array of headers */
 583      var $headers;
 584  
 585      /**
 586      * HTTP Headers constructor
 587      */
 588  	function php_http_headers() {
 589          $this->headers = array();
 590      } //php_http_headers
 591  
 592      /**
 593      * Returns the specified header value
 594      * @param string The header name
 595      * @return mixed The header value, or an array of header values
 596      */
 597      function &getHeader($name) {
 598          if ($this->headerExists($name))    {
 599              return $this->headers[$name];
 600          }
 601  
 602          return false;
 603      } //getHeader
 604  
 605      /**
 606      * Sets the named header to the specified value
 607      * @param string The header name
 608      * @param string The header value
 609      * @param boolean True if multiple headers with the same name are allowed
 610      */
 611  	function setHeader($name, $value, $allowMultipleHeaders = false) {
 612          if ($allowMultipleHeaders) {
 613              if (isset($this->headers[$name])) {
 614                  if (is_array($this->headers[$name])) {
 615                      $this->headers[$name][count($this->headers)] = $value;
 616                  }
 617                  else {
 618                      $tempVal = $this->headers[$name];
 619                      $this->headers[$name] = array($tempVal, $value);
 620                  }
 621              }
 622              else {
 623                  $this->headers[$name] = array();
 624                  $this->headers[$name][0] = $value;
 625              }
 626          }
 627          else {
 628              $this->headers[$name] = $value;
 629          }
 630      } //setHeader
 631  
 632      /**
 633      * Determines whether the specified header exists
 634      * @param string The header name
 635      * @return boolean True if the specified header exists
 636      */
 637  	function headerExists($name) {
 638          return isset($this->headers[$name]);
 639      } //headerExists
 640  
 641      /**
 642      * Removes the specified header
 643      * @param string The header name
 644      * @return boolean True if the specified header has been removed
 645      */
 646  	function removeHeader($name) {
 647          if ($this->headerExists($name))    {
 648              unset($this->headers[$name]);
 649              return true;
 650          }
 651  
 652          return false;
 653      } //removeHeader
 654  
 655      /**
 656      * Returns a reference to the headers array
 657      * @return array The headers array
 658      */
 659  	function getHeaders() {
 660          return $this->headers;
 661      } //getHeaders
 662  
 663      /**
 664      * Returns a list of existing headers names
 665      * @return array A list of existing header names
 666      */
 667  	function getHeaderList() {
 668          return array_keys($this->headers);
 669      } //getHeaderList
 670  
 671      /**
 672      * Returns a string representation of the headers array
 673      * @return string A string representation of the headers array
 674      */
 675  	function toString() {
 676          $retString = '';
 677  
 678          foreach ($this->headers as $key => $value) {
 679              if (is_array($value)) {
 680                  foreach ($value as $key2 => $value2) {
 681                      $retString .= $key . ': ' . $value2 . CRLF;
 682                  }
 683              }
 684              else {
 685                  $retString .= $key . ': ' . $value . CRLF;
 686              }
 687          }
 688  
 689          return $retString;
 690      } //toString
 691  } //php_http_headers
 692  
 693  /**
 694  * An HTTP Response class
 695  *
 696  * @package php-http-tools
 697  * @author John Heinstein <johnkarl@nbnet.nb.ca>
 698  */
 699  class php_http_response {
 700      /** @var string Response number */
 701      var $statusLine = '';
 702      /** @var mixed Response headers, either as object or string */
 703      var $headers = '';
 704      /** @var string Response message */
 705      var $message = '';
 706      /** @var boolean True if the chunked transfer-encoding of the response has been decoded */
 707      var $isResponseChunkDecoded = false;
 708  
 709      /**
 710      * Returns a reference to the headers array
 711      * @return array The headers array
 712      */
 713  	function getResponse() {
 714          return $this->message;
 715      } //getResponse
 716  
 717      /**
 718      * Returns the response status line
 719      * @return string The response status line
 720      */
 721  	function getStatusLine() {
 722          return $this->statusLine;
 723      } //getStatusLine
 724  
 725      /**
 726      * Returns the response status code
 727      * @return int The response status code
 728      */
 729  	function getStatusCode() {
 730          $statusArray = split(' ', $this->statusLine);
 731  
 732          if (count($statusArray > 1)) {
 733              return intval($statusArray[1], 10);
 734          }
 735  
 736          return -1;
 737      } //getStatusCode
 738  
 739      /**
 740      * Returns a reference to the headers array
 741      * @return array The headers array
 742      */
 743      function &getHeaders() {
 744          return $this->headers;
 745      } //getHeaders
 746  
 747      /**
 748      * Converts a header string into a key value pair and sets header
 749      */
 750  	function setUnformattedHeader($headerString) {
 751          $colonIndex = strpos($headerString, ':');
 752  
 753          if ($colonIndex !== false) {
 754              $key = trim(substr($headerString, 0, $colonIndex));
 755              $value = trim(substr($headerString, ($colonIndex + 1)));
 756              $this->headers->setHeader($key, $value, true);
 757          }
 758      } //setUnformattedHeader
 759  } //php_http_response
 760  
 761  ?>


Généré le : Wed Nov 21 14:43:32 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics