home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / XML / RPC2 / Util / HTTPRequest.php
Encoding:
PHP Script  |  2008-07-02  |  7.4 KB  |  223 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  4.  
  5. // LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{ 
  6.  
  7. /**
  8. * +-----------------------------------------------------------------------------+
  9. * | Copyright (c) 2004-2006 Sergio Goncalves Carvalho                                |
  10. * +-----------------------------------------------------------------------------+
  11. * | This file is part of XML_RPC2.                                              |
  12. * |                                                                             |
  13. * | XML_RPC2 is free software; you can redistribute it and/or modify            |
  14. * | it under the terms of the GNU Lesser General Public License as published by |
  15. * | the Free Software Foundation; either version 2.1 of the License, or         |
  16. * | (at your option) any later version.                                         |
  17. * |                                                                             |
  18. * | XML_RPC2 is distributed in the hope that it will be useful,                 |
  19. * | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
  20. * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
  21. * | GNU Lesser General Public License for more details.                         |
  22. * |                                                                             |
  23. * | You should have received a copy of the GNU Lesser General Public License    |
  24. * | along with XML_RPC2; if not, write to the Free Software                     |
  25. * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                    |
  26. * | 02111-1307 USA                                                              |
  27. * +-----------------------------------------------------------------------------+
  28. * | Author: Sergio Carvalho <sergio.carvalho@portugalmail.com>                  |
  29. * +-----------------------------------------------------------------------------+
  30. *
  31. * @category   XML
  32. * @package    XML_RPC2
  33. * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  34. * @copyright  2004-2006 Sergio Carvalho
  35. * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  36. * @version    CVS: $Id: HTTPRequest.php,v 1.7 2007/07/30 08:47:11 sergiosgc Exp $
  37. * @link       http://pear.php.net/package/XML_RPC2
  38. */
  39.  
  40. // }}}
  41.  
  42. // dependencies {{{
  43. require_once 'XML/RPC2/Exception.php';
  44. // }}}
  45.  
  46. /**
  47.  * XML_RPC utility HTTP request class. This class mimics a subset of PEAR's HTTP_Request
  48.  * and is to be refactored out of the package once HTTP_Request releases an E_STRICT version.
  49.  * 
  50.  * @category   XML
  51.  * @package    XML_RPC2
  52.  * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  53.  * @copyright  2004-2006 Sergio Carvalho
  54.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  55.  * @link       http://pear.php.net/package/XML_RPC2
  56.  */
  57. class XML_RPC2_Util_HTTPRequest
  58. {
  59.  
  60.     // {{{ properties
  61.     
  62.     /**
  63.      * proxy field
  64.      *
  65.      * @var string
  66.      */
  67.     private $_proxy = null;
  68.     
  69.     /**
  70.      * proxyauth field
  71.      *
  72.      * @var string
  73.      */
  74.     private $_proxyAuth = null;
  75.     
  76.     /**
  77.      * postData field 
  78.      *
  79.      * @var string
  80.      */
  81.     private $_postData;
  82.                
  83.     /**
  84.      * uri field 
  85.      *
  86.      * @var array
  87.      */
  88.     private $_uri;
  89.     
  90.     /**
  91.      * encoding for the request
  92.      *
  93.      * @var string
  94.      */
  95.     private $_encoding='iso-8859-1';
  96.     
  97.     /**
  98.      * SSL verify flag
  99.      *
  100.      * @var boolean
  101.      */
  102.     private $_sslverify=true;
  103.     
  104.     // }}}
  105.     // {{{ getBody()
  106.  
  107.     /**
  108.      * body field getter
  109.      *
  110.      * @return string body value
  111.      */
  112.     public function getBody() 
  113.     {
  114.         return $this->_body;
  115.     }
  116.             
  117.     // }}}
  118.     // {{{ setPostData()
  119.     
  120.     /**
  121.      * postData field setter
  122.      *
  123.      * @param string postData value
  124.      */
  125.     public function setPostData($value) 
  126.     {
  127.         $this->_postData = $value;
  128.     }
  129.     
  130.     // }}}
  131.     // {{{ constructor
  132.     
  133.     /**
  134.     * Constructor
  135.     *
  136.     * Sets up the object
  137.     * @param    string  The uri to fetch/access
  138.     * @param    array   Associative array of parameters which can have the following keys:
  139.     * <ul>
  140.     *   <li>proxy          - Proxy (string)</li>
  141.     *   <li>encoding       - The request encoding (string)</li>
  142.     * </ul>
  143.     * @access public
  144.     */
  145.     public function __construct($uri = '', $params = array())
  146.     {
  147.         if (!preg_match('/(https?:\/\/)(.*)/', $uri)) throw new XML_RPC2_Exception('Unable to parse URI');
  148.         $this->_uri = $uri;
  149.         if (isset($params['encoding'])) {
  150.             $this->_encoding = $params['encoding'];
  151.         }
  152.         if (isset($params['proxy'])) {
  153.             $proxy = $params['proxy'];
  154.             $elements = parse_url($proxy);
  155.             if (is_array($elements)) {
  156.                 if ((isset($elements['scheme'])) and (isset($elements['host']))) { 
  157.                     $this->_proxy = $elements['scheme'] . '://' . $elements['host'];
  158.                 }
  159.                 if (isset($elements['port'])) {
  160.                     $this->_proxy = $this->_proxy . ':' . $elements['port'];
  161.                 }
  162.                 if ((isset($elements['user'])) and (isset($elements['pass']))) {
  163.                     $this->_proxyAuth = $elements['user'] . ':' . $elements['pass'];
  164.                 }
  165.             }
  166.         }
  167.         if (isset($params['sslverify'])) {
  168.             $this->_sslverify = $params['sslverify'];
  169.         }
  170.     }
  171.     
  172.     // }}}
  173.     // {{{ sendRequest()
  174.     
  175.     /**
  176.     * Sends the request
  177.     *
  178.     * @access public
  179.     * @return mixed  PEAR error on error, true otherwise
  180.     */
  181.     public function sendRequest()
  182.     {
  183.         if (!function_exists('curl_init') &&
  184.             !( // TODO Use PEAR::loadExtension once PEAR passes PHP5 unit tests (E_STRICT compliance, namely)
  185.               @dl('php_curl' . PHP_SHLIB_SUFFIX)    || @dl('curl' . PHP_SHLIB_SUFFIX)
  186.              )) {
  187.             throw new XML_RPC2_CurlException('cURI extension is not present and load failed');
  188.         }
  189.         if ($ch = curl_init()) {
  190.             if (
  191.                 (is_null($this->_proxy)     || curl_setopt($ch, CURLOPT_PROXY, $this->_proxy)) &&
  192.                 (is_null($this->_proxyAuth) || curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->_proxyAuth)) &&
  193.                 curl_setopt($ch, CURLOPT_URL, $this->_uri) &&
  194.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE) &&
  195.                 curl_setopt($ch, CURLOPT_POST, 1) &&
  196.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->_sslverify) &&
  197.                 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml; charset='.$this->_encoding, 'User-Agent: PEAR_XML_RCP2/0.0.x')) &&
  198.                 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_postData)
  199.             ) {
  200.                 $result = curl_exec($ch);
  201.                 if (($errno = curl_errno($ch)) != 0) {
  202.                     throw new XML_RPC2_CurlException("Curl returned non-null errno $errno:" . curl_error($ch));
  203.                 }
  204.                 $info = curl_getinfo($ch);
  205.                 if ($info['http_code'] != 200) {
  206.                     throw new XML_RPC2_ReceivedInvalidStatusCodeException('Curl returned non 200 HTTP code: ' . $info['http_code'] . '. Response body:' . $result);
  207.                 }
  208.             } else {
  209.                 throw new XML_RPC2_CurlException('Unable to setup curl');
  210.             }
  211.         } else {
  212.             throw new XML_RPC2_CurlException('Unable to init curl');
  213.         }
  214.         $this->_body = $result;        
  215.         return true;
  216.     }
  217.     
  218.     // }}}
  219.  
  220. }
  221.  
  222. ?>
  223.