| 1 |
lars |
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 311547 2011-05-29 14:29:05Z clockwerx $
|
|
|
37 |
* @link http://pear.php.net/package/XML_RPC2
|
|
|
38 |
*/
|
|
|
39 |
|
|
|
40 |
// }}}
|
|
|
41 |
|
|
|
42 |
// dependencies {{{
|
|
|
43 |
require_once 'XML/RPC2/Exception.php';
|
|
|
44 |
require_once 'XML/RPC2/Client.php';
|
|
|
45 |
require_once 'HTTP/Request2.php';
|
|
|
46 |
// }}}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* XML_RPC utility HTTP request class. This class mimics a subset of PEAR's HTTP_Request
|
|
|
50 |
* and is to be refactored out of the package once HTTP_Request releases an E_STRICT version.
|
|
|
51 |
*
|
|
|
52 |
* @category XML
|
|
|
53 |
* @package XML_RPC2
|
|
|
54 |
* @author Sergio Carvalho <sergio.carvalho@portugalmail.com>
|
|
|
55 |
* @copyright 2004-2011 Sergio Carvalho
|
|
|
56 |
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
|
|
57 |
* @link http://pear.php.net/package/XML_RPC2
|
|
|
58 |
*/
|
|
|
59 |
class XML_RPC2_Util_HTTPRequest
|
|
|
60 |
{
|
|
|
61 |
|
|
|
62 |
// {{{ properties
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* proxy field
|
|
|
66 |
*
|
|
|
67 |
* @var string
|
|
|
68 |
*/
|
|
|
69 |
private $_proxy = null;
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* proxyauth field
|
|
|
73 |
*
|
|
|
74 |
* @var string
|
|
|
75 |
*/
|
|
|
76 |
private $_proxyAuth = null;
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* postData field
|
|
|
80 |
*
|
|
|
81 |
* @var string
|
|
|
82 |
*/
|
|
|
83 |
private $_postData;
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* uri field
|
|
|
87 |
*
|
|
|
88 |
* @var array
|
|
|
89 |
*/
|
|
|
90 |
private $_uri;
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* encoding for the request
|
|
|
94 |
*
|
|
|
95 |
* @var string
|
|
|
96 |
*/
|
|
|
97 |
private $_encoding='utf-8';
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* SSL verify flag
|
|
|
101 |
*
|
|
|
102 |
* @var boolean
|
|
|
103 |
*/
|
|
|
104 |
private $_sslverify=true;
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* HTTP timeout length in seconds.
|
|
|
108 |
*
|
|
|
109 |
* @var integer
|
|
|
110 |
*/
|
|
|
111 |
private $_connectionTimeout = null;
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* HTTP_Request2 backend
|
|
|
115 |
*
|
|
|
116 |
* @var integer
|
|
|
117 |
*/
|
|
|
118 |
private $_httpRequest = null;
|
|
|
119 |
|
|
|
120 |
// }}}
|
|
|
121 |
// {{{ getBody()
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* body field getter
|
|
|
125 |
*
|
|
|
126 |
* @return string body value
|
|
|
127 |
*/
|
|
|
128 |
public function getBody()
|
|
|
129 |
{
|
|
|
130 |
return $this->_body;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
// }}}
|
|
|
134 |
// {{{ setPostData()
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* postData field setter
|
|
|
138 |
*
|
|
|
139 |
* @param string postData value
|
|
|
140 |
*/
|
|
|
141 |
public function setPostData($value)
|
|
|
142 |
{
|
|
|
143 |
$this->_postData = $value;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
// }}}
|
|
|
147 |
// {{{ constructor
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Constructor
|
|
|
151 |
*
|
|
|
152 |
* Sets up the object
|
|
|
153 |
* @param string The uri to fetch/access
|
|
|
154 |
* @param array Associative array of parameters which can have the following keys:
|
|
|
155 |
* <ul>
|
|
|
156 |
* <li>proxy - Proxy (string)</li>
|
|
|
157 |
* <li>encoding - The request encoding (string)</li>
|
|
|
158 |
* <li>sslverify</li> - The SSL verify flag (boolean)</li>
|
|
|
159 |
* <li>connectionTimeout</li> - The connection timeout in milliseconds (integer)</li>
|
|
|
160 |
* <li>httpRequest</li> - Preconfigured instance of HTTP_Request2 (optional)
|
|
|
161 |
* </ul>
|
|
|
162 |
* @access public
|
|
|
163 |
*/
|
|
|
164 |
public function __construct($uri = '', $params = array())
|
|
|
165 |
{
|
|
|
166 |
if (!preg_match('/(https?:\/\/)(.*)/', $uri)) throw new XML_RPC2_Exception('Unable to parse URI');
|
|
|
167 |
$this->_uri = $uri;
|
|
|
168 |
if (isset($params['encoding'])) {
|
|
|
169 |
$this->_encoding = $params['encoding'];
|
|
|
170 |
}
|
|
|
171 |
if (isset($params['proxy'])) {
|
|
|
172 |
$proxy = $params['proxy'];
|
|
|
173 |
$elements = parse_url($proxy);
|
|
|
174 |
if (is_array($elements)) {
|
|
|
175 |
if ((isset($elements['scheme'])) and (isset($elements['host']))) {
|
|
|
176 |
$this->_proxy = $elements['scheme'] . '://' . $elements['host'];
|
|
|
177 |
}
|
|
|
178 |
if (isset($elements['port'])) {
|
|
|
179 |
$this->_proxy = $this->_proxy . ':' . $elements['port'];
|
|
|
180 |
}
|
|
|
181 |
if ((isset($elements['user'])) and (isset($elements['pass']))) {
|
|
|
182 |
$this->_proxyAuth = $elements['user'] . ':' . $elements['pass'];
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
if (isset($params['sslverify'])) {
|
|
|
187 |
$this->_sslverify = $params['sslverify'];
|
|
|
188 |
}
|
|
|
189 |
if (isset($params['connectionTimeout'])) {
|
|
|
190 |
$this->_connectionTimeout = $params['connectionTimeout'];
|
|
|
191 |
}
|
|
|
192 |
if (isset($params['httpRequest']) && $params['httpRequest'] instanceof HTTP_Request2) {
|
|
|
193 |
$this->_httpRequest = $params['httpRequest'];
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
// }}}
|
|
|
198 |
// {{{ sendRequest()
|
|
|
199 |
|
|
|
200 |
/**
|
|
|
201 |
* Sends the request
|
|
|
202 |
*
|
|
|
203 |
* @access public
|
|
|
204 |
* @return mixed PEAR error on error, true otherwise
|
|
|
205 |
*/
|
|
|
206 |
public function sendRequest()
|
|
|
207 |
{
|
|
|
208 |
if (is_null($this->_httpRequest)) $this->_httpRequest = new HTTP_Request2($this->_uri, HTTP_Request2::METHOD_POST);
|
|
|
209 |
$request = $this->_httpRequest;
|
|
|
210 |
$request->setUrl($this->_uri);
|
|
|
211 |
$request->setMethod(HTTP_Request2::METHOD_POST);
|
|
|
212 |
if (isset($params['proxy'])) {
|
|
|
213 |
$elements = parse_url($params['proxy']);
|
|
|
214 |
if (is_array($elements)) {
|
|
|
215 |
if ((isset($elements['scheme'])) and (isset($elements['host']))) {
|
|
|
216 |
$request->setConfig('proxy_host', $elements['host']);
|
|
|
217 |
}
|
|
|
218 |
if (isset($elements['port'])) {
|
|
|
219 |
$request->setConfig('proxy_port', $elements['port']);
|
|
|
220 |
}
|
|
|
221 |
if ((isset($elements['user'])) and (isset($elements['pass']))) {
|
|
|
222 |
$request->setConfig('proxy_user', $elements['user']);
|
|
|
223 |
$request->setConfig('proxy_password', $elements['pass']);
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
}
|
|
|
227 |
$request->setConfig('ssl_verify_peer', $this->_sslverify);
|
|
|
228 |
$request->setConfig('ssl_verify_host', $this->_sslverify);
|
|
|
229 |
$request->setHeader('Content-type: text/xml; charset='.$this->_encoding);
|
|
|
230 |
$request->setHeader('User-Agent: PEAR::XML_RPC2/@package_version@');
|
|
|
231 |
$request->setBody($this->_postData);
|
|
|
232 |
if (isset($this->_connectionTimeout)) $request->setConfig('timeout', (int) ($this->_connectionTimeout / 1000));
|
|
|
233 |
try {
|
|
|
234 |
$result = $request->send();
|
|
|
235 |
if ($result->getStatus() != 200) throw new XML_RPC2_ReceivedInvalidStatusCodeException('Received non-200 HTTP Code: ' . $result->getStatus() . '. Response body:' . $result->getBody());
|
|
|
236 |
|
|
|
237 |
} catch (HTTP_Request2_Exception $e) {
|
|
|
238 |
throw new XML_RPC2_CurlException($e);
|
|
|
239 |
}
|
|
|
240 |
$this->_body = $result->getBody();
|
|
|
241 |
return $result->getBody();
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
// }}}
|
|
|
245 |
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
?>
|