Skip to content

Commit 0ff32a4

Browse files
committed
XML-RPC: break up class-IXR.php into individual class files.
See #37827. git-svn-id: https://develop.svn.wordpress.org/trunk@38389 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 55f5577 commit 0ff32a4

11 files changed

Lines changed: 1199 additions & 1172 deletions
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* IXR_Base64
5+
*
6+
* @package IXR
7+
* @since 1.5.0
8+
*/
9+
class IXR_Base64
10+
{
11+
var $data;
12+
13+
/**
14+
* PHP5 constructor.
15+
*/
16+
function __construct( $data )
17+
{
18+
$this->data = $data;
19+
}
20+
21+
/**
22+
* PHP4 constructor.
23+
*/
24+
public function IXR_Base64( $data ) {
25+
self::__construct( $data );
26+
}
27+
28+
function getXml()
29+
{
30+
return '<base64>'.base64_encode($this->data).'</base64>';
31+
}
32+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
/**
4+
* IXR_Client
5+
*
6+
* @package IXR
7+
* @since 1.5.0
8+
*
9+
*/
10+
class IXR_Client
11+
{
12+
var $server;
13+
var $port;
14+
var $path;
15+
var $useragent;
16+
var $response;
17+
var $message = false;
18+
var $debug = false;
19+
var $timeout;
20+
var $headers = array();
21+
22+
// Storage place for an error message
23+
var $error = false;
24+
25+
/**
26+
* PHP5 constructor.
27+
*/
28+
function __construct( $server, $path = false, $port = 80, $timeout = 15 )
29+
{
30+
if (!$path) {
31+
// Assume we have been given a URL instead
32+
$bits = parse_url($server);
33+
$this->server = $bits['host'];
34+
$this->port = isset($bits['port']) ? $bits['port'] : 80;
35+
$this->path = isset($bits['path']) ? $bits['path'] : '/';
36+
37+
// Make absolutely sure we have a path
38+
if (!$this->path) {
39+
$this->path = '/';
40+
}
41+
42+
if ( ! empty( $bits['query'] ) ) {
43+
$this->path .= '?' . $bits['query'];
44+
}
45+
} else {
46+
$this->server = $server;
47+
$this->path = $path;
48+
$this->port = $port;
49+
}
50+
$this->useragent = 'The Incutio XML-RPC PHP Library';
51+
$this->timeout = $timeout;
52+
}
53+
54+
/**
55+
* PHP4 constructor.
56+
*/
57+
public function IXR_Client( $server, $path = false, $port = 80, $timeout = 15 ) {
58+
self::__construct( $server, $path, $port, $timeout );
59+
}
60+
61+
function query()
62+
{
63+
$args = func_get_args();
64+
$method = array_shift($args);
65+
$request = new IXR_Request($method, $args);
66+
$length = $request->getLength();
67+
$xml = $request->getXml();
68+
$r = "\r\n";
69+
$request = "POST {$this->path} HTTP/1.0$r";
70+
71+
// Merged from WP #8145 - allow custom headers
72+
$this->headers['Host'] = $this->server;
73+
$this->headers['Content-Type'] = 'text/xml';
74+
$this->headers['User-Agent'] = $this->useragent;
75+
$this->headers['Content-Length']= $length;
76+
77+
foreach( $this->headers as $header => $value ) {
78+
$request .= "{$header}: {$value}{$r}";
79+
}
80+
$request .= $r;
81+
82+
$request .= $xml;
83+
84+
// Now send the request
85+
if ($this->debug) {
86+
echo '<pre class="ixr_request">'.htmlspecialchars($request)."\n</pre>\n\n";
87+
}
88+
89+
if ($this->timeout) {
90+
$fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);
91+
} else {
92+
$fp = @fsockopen($this->server, $this->port, $errno, $errstr);
93+
}
94+
if (!$fp) {
95+
$this->error = new IXR_Error(-32300, 'transport error - could not open socket');
96+
return false;
97+
}
98+
fputs($fp, $request);
99+
$contents = '';
100+
$debugContents = '';
101+
$gotFirstLine = false;
102+
$gettingHeaders = true;
103+
while (!feof($fp)) {
104+
$line = fgets($fp, 4096);
105+
if (!$gotFirstLine) {
106+
// Check line for '200'
107+
if (strstr($line, '200') === false) {
108+
$this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200');
109+
return false;
110+
}
111+
$gotFirstLine = true;
112+
}
113+
if (trim($line) == '') {
114+
$gettingHeaders = false;
115+
}
116+
if (!$gettingHeaders) {
117+
// merged from WP #12559 - remove trim
118+
$contents .= $line;
119+
}
120+
if ($this->debug) {
121+
$debugContents .= $line;
122+
}
123+
}
124+
if ($this->debug) {
125+
echo '<pre class="ixr_response">'.htmlspecialchars($debugContents)."\n</pre>\n\n";
126+
}
127+
128+
// Now parse what we've got back
129+
$this->message = new IXR_Message($contents);
130+
if (!$this->message->parse()) {
131+
// XML error
132+
$this->error = new IXR_Error(-32700, 'parse error. not well formed');
133+
return false;
134+
}
135+
136+
// Is the message a fault?
137+
if ($this->message->messageType == 'fault') {
138+
$this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
139+
return false;
140+
}
141+
142+
// Message must be OK
143+
return true;
144+
}
145+
146+
function getResponse()
147+
{
148+
// methodResponses can only have one param - return that
149+
return $this->message->params[0];
150+
}
151+
152+
function isError()
153+
{
154+
return (is_object($this->error));
155+
}
156+
157+
function getErrorCode()
158+
{
159+
return $this->error->code;
160+
}
161+
162+
function getErrorMessage()
163+
{
164+
return $this->error->message;
165+
}
166+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* IXR_ClientMulticall
4+
*
5+
* @package IXR
6+
* @since 1.5.0
7+
*/
8+
class IXR_ClientMulticall extends IXR_Client
9+
{
10+
var $calls = array();
11+
12+
/**
13+
* PHP5 constructor.
14+
*/
15+
function __construct( $server, $path = false, $port = 80 )
16+
{
17+
parent::IXR_Client($server, $path, $port);
18+
$this->useragent = 'The Incutio XML-RPC PHP Library (multicall client)';
19+
}
20+
21+
/**
22+
* PHP4 constructor.
23+
*/
24+
public function IXR_ClientMulticall( $server, $path = false, $port = 80 ) {
25+
self::__construct( $server, $path, $port );
26+
}
27+
28+
function addCall()
29+
{
30+
$args = func_get_args();
31+
$methodName = array_shift($args);
32+
$struct = array(
33+
'methodName' => $methodName,
34+
'params' => $args
35+
);
36+
$this->calls[] = $struct;
37+
}
38+
39+
function query()
40+
{
41+
// Prepare multicall, then call the parent::query() method
42+
return parent::query('system.multicall', $this->calls);
43+
}
44+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
* IXR_Date
5+
*
6+
* @package IXR
7+
* @since 1.5.0
8+
*/
9+
class IXR_Date {
10+
var $year;
11+
var $month;
12+
var $day;
13+
var $hour;
14+
var $minute;
15+
var $second;
16+
var $timezone;
17+
18+
/**
19+
* PHP5 constructor.
20+
*/
21+
function __construct( $time )
22+
{
23+
// $time can be a PHP timestamp or an ISO one
24+
if (is_numeric($time)) {
25+
$this->parseTimestamp($time);
26+
} else {
27+
$this->parseIso($time);
28+
}
29+
}
30+
31+
/**
32+
* PHP4 constructor.
33+
*/
34+
public function IXR_Date( $time ) {
35+
self::__construct( $time );
36+
}
37+
38+
function parseTimestamp($timestamp)
39+
{
40+
$this->year = date('Y', $timestamp);
41+
$this->month = date('m', $timestamp);
42+
$this->day = date('d', $timestamp);
43+
$this->hour = date('H', $timestamp);
44+
$this->minute = date('i', $timestamp);
45+
$this->second = date('s', $timestamp);
46+
$this->timezone = '';
47+
}
48+
49+
function parseIso($iso)
50+
{
51+
$this->year = substr($iso, 0, 4);
52+
$this->month = substr($iso, 4, 2);
53+
$this->day = substr($iso, 6, 2);
54+
$this->hour = substr($iso, 9, 2);
55+
$this->minute = substr($iso, 12, 2);
56+
$this->second = substr($iso, 15, 2);
57+
$this->timezone = substr($iso, 17);
58+
}
59+
60+
function getIso()
61+
{
62+
return $this->year.$this->month.$this->day.'T'.$this->hour.':'.$this->minute.':'.$this->second.$this->timezone;
63+
}
64+
65+
function getXml()
66+
{
67+
return '<dateTime.iso8601>'.$this->getIso().'</dateTime.iso8601>';
68+
}
69+
70+
function getTimestamp()
71+
{
72+
return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
73+
}
74+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/**
4+
* IXR_Error
5+
*
6+
* @package IXR
7+
* @since 1.5.0
8+
*/
9+
class IXR_Error
10+
{
11+
var $code;
12+
var $message;
13+
14+
/**
15+
* PHP5 constructor.
16+
*/
17+
function __construct( $code, $message )
18+
{
19+
$this->code = $code;
20+
$this->message = htmlspecialchars($message);
21+
}
22+
23+
/**
24+
* PHP4 constructor.
25+
*/
26+
public function IXR_Error( $code, $message ) {
27+
self::__construct( $code, $message );
28+
}
29+
30+
function getXml()
31+
{
32+
$xml = <<<EOD
33+
<methodResponse>
34+
<fault>
35+
<value>
36+
<struct>
37+
<member>
38+
<name>faultCode</name>
39+
<value><int>{$this->code}</int></value>
40+
</member>
41+
<member>
42+
<name>faultString</name>
43+
<value><string>{$this->message}</string></value>
44+
</member>
45+
</struct>
46+
</value>
47+
</fault>
48+
</methodResponse>
49+
50+
EOD;
51+
return $xml;
52+
}
53+
}

0 commit comments

Comments
 (0)