Completed
Push — add/support-for-multi-site-res... ( 2212a9...df602e )
by
unknown
21:07 queued 11:04
created

VaultPress_IXR_SSL_Client::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
// don't call the file directly
4
defined( 'ABSPATH' ) or die();
5
6
if ( !class_exists( 'IXR_Client' ) )
7
	include_once( ABSPATH . WPINC . '/class-IXR.php' );
8
9
class VaultPress_IXR_SSL_Client extends IXR_Client {
10
	var $ssl = false;
11
	function __construct( $server, $path = false, $port = 80, $timeout = false, $useragent = false ) {
12
		parent::__construct( $server, $path, $port, $timeout );
13
		if ( ! empty( $useragent ) ) {
14
			$this->useragent = $useragent;
15
		}
16
	}
17
	function ssl( $port=443 ) {
18
		if ( !extension_loaded( 'openssl' ) )
19
			return;
20
21
		$this->ssl = true;
22
		if ( $port )
23
			$this->port = $port;
24
	}
25
	/**
26
	 * Perform the IXR request.
27
	 *
28
	 * @param string[] ...$args IXR args.
29
	 *
30
	 * @return bool True if request succeeded, false otherwise.
31
	 */
0 ignored issues
show
Documentation introduced by
Should the type for parameter $args not be string[][]?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
32
	function query( ...$args ) {
33
		$method = array_shift($args);
34
		$request = new IXR_Request($method, $args);
35
		$length = $request->getLength();
36
		$xml = $request->getXml();
37
		$r = "\r\n";
38
		$request  = "POST {$this->path} HTTP/1.0$r";
39
40
		$this->headers['Host']           = preg_replace( '#^ssl://#', '', $this->server );
41
		$this->headers['Content-Type']   = 'text/xml';
42
		$this->headers['User-Agent']     = $this->useragent;
43
		$this->headers['Content-Length'] = $length;
44
45
		$sslverify = true;
46
		if ( defined( 'VAULTPRESS_NO_SSL' ) && VAULTPRESS_NO_SSL ) {
47
			$sslverify = false;
48
		}
49
		if ( class_exists( 'WP_Http' ) ) {
50
			$args = array(
51
				'method' => 'POST',
52
				'body' => $xml,
53
				'headers' => $this->headers,
54
				'sslverify' => $sslverify,
55
				);
56
			if ( $this->timeout )
57
				$args['timeout'] = $this->timeout;
58
59
			$http = new WP_Http();
60
			if ( $this->ssl )
61
				$url = sprintf( 'https://%s%s', $this->server, $this->path );
62
			else
63
				$url = sprintf( 'http://%s%s', $this->server, $this->path );
64
65
			$result = $http->request( $url, $args );
66
			if ( is_wp_error( $result ) ) {
67
				foreach( $result->errors as $type => $messages ) {
68
					$this->error = new IXR_Error(
69
						-32702,
70
						sprintf( 'WP_Http error: %s, %s', $type, $messages[0] )
71
					);
72
					break;
73
				}
74
				return false;
75
			} else if ( $result['response']['code'] > 299 || $result['response']['code'] < 200 ) {
76
				$this->error = new IXR_Error(
77
					-32701,
78
					sprintf( 'Server rejected request (HTTP response: %s %s)', $result['response']['code'], $result['response']['message'])
79
				);
80
				return false;
81
			}
82
			// Now parse what we've got back
83
			$this->message = new IXR_Message( $result['body'] );
84
		} else {
85
			foreach( $this->headers as $header => $value ) {
86
				$request .= "{$header}: {$value}{$r}";
87
			}
88
			$request .= $r;
89
90
			$request .= $xml;
91
			// Now send the request
92
			if ( $this->ssl )
93
				$host = 'ssl://'.$this->server;
94
			else
95
				$host = $this->server;
96
			if ($this->timeout) {
97
				$fp = @fsockopen( $host, $this->port, $errno, $errstr, $this->timeout );
98
			} else {
99
				$fp = @fsockopen( $host, $this->port, $errno, $errstr );
100
			}
101
			if (!$fp) {
102
				$this->error = new IXR_Error( -32300, "Transport error - could not open socket: $errno $errstr" );
103
				return false;
104
			}
105
			fputs( $fp, $request );
106
107
			$contents = '';
108
			$gotFirstLine = false;
109
			$gettingHeaders = true;
110
111
			while ( !feof($fp) ) {
112
				$line = fgets( $fp, 4096 );
113
				if ( !$gotFirstLine ) {
114
					// Check line for '200'
115
					if ( strstr($line, '200') === false ) {
116
						$this->error = new IXR_Error( -32301, 'transport error - HTTP status code was not 200' );
117
						return false;
118
					}
119
					$gotFirstLine = true;
120
				}
121
				if ( trim($line) == '' ) {
122
					$gettingHeaders = false;
123
				}
124
				if ( !$gettingHeaders ) {
125
					$contents .= trim( $line );
126
				}
127
			}
128
			// Now parse what we've got back
129
			$this->message = new IXR_Message( $contents );
130
		}
131 View Code Duplication
		if ( !$this->message->parse() ) {
132
			// XML error
133
			$this->error = new IXR_Error( -32700, 'parse error. not well formed' );
134
			return false;
135
		}
136
		// Is the message a fault?
137 View Code Duplication
		if ( $this->message->messageType == 'fault' ) {
138
			$this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
139
			return false;
140
		}
141
		// Message must be OK
142
		return true;
143
	}
144
}
145