|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AfriCC\EPP; |
|
4
|
|
|
|
|
5
|
|
|
use AfriCC\EPP\Frame\Command\Logout as LogoutCommand; |
|
6
|
|
|
use AfriCC\EPP\Frame\ResponseFactory; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A high level HTTP(S) based client for the Extensible Provisioning Protocol (EPP) |
|
10
|
|
|
* |
|
11
|
|
|
* @see http://tools.ietf.org/html/rfc5734 |
|
12
|
|
|
* |
|
13
|
|
|
* As this class deals directly with cURL it's untestable |
|
14
|
|
|
* @codeCoverageIgnore |
|
15
|
|
|
*/ |
|
16
|
|
|
class HTTPClient extends AbstractClient implements ClientInterface |
|
17
|
|
|
{ |
|
18
|
|
|
protected $curl; |
|
19
|
|
|
protected $cookiejar; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(array $config, ObjectSpec $objectSpec = null) |
|
22
|
|
|
{ |
|
23
|
|
|
parent::__construct($config, $objectSpec); |
|
24
|
|
|
|
|
25
|
|
|
$proto = \parse_url($this->host, PHP_URL_SCHEME); |
|
26
|
|
|
if ($proto == 'https') { |
|
27
|
|
|
$this->ssl = true; |
|
28
|
|
|
} else { |
|
29
|
|
|
$this->ssl = false; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$this->prepareCookieJar($config); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function prepareCookieJar(array $config) |
|
36
|
|
|
{ |
|
37
|
|
|
if (!empty($config['cookiejar'])) { |
|
38
|
|
|
$this->cookiejar = $config['cookiejar']; |
|
39
|
|
|
} else { |
|
40
|
|
|
$this->cookiejar = tempnam(sys_get_temp_dir(), 'ehc'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (!is_readable($this->cookiejar) || !is_writable($this->cookiejar)) { |
|
44
|
|
|
throw new \Exception( |
|
45
|
|
|
sprintf( |
|
46
|
|
|
'unable to read/write cookiejar: %s', |
|
47
|
|
|
$this->cookiejar |
|
48
|
|
|
) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function __destruct() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->close(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function setupCurl() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->curl = curl_init($this->host); |
|
61
|
|
|
|
|
62
|
|
|
if ($this->curl === false) { |
|
63
|
|
|
throw new \Exception('Cannot initialize cURL extension'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->setupCurlOpts(); |
|
67
|
|
|
|
|
68
|
|
|
// certs |
|
69
|
|
|
if ($this->ssl) { |
|
70
|
|
|
$this->setupCurlSSL(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function setupCurlOpts() |
|
75
|
|
|
{ |
|
76
|
|
|
// set stream time out |
|
77
|
|
|
curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->timeout); |
|
78
|
|
|
curl_setopt( |
|
79
|
|
|
$this->curl, |
|
80
|
|
|
CURLOPT_CONNECTTIMEOUT, |
|
81
|
|
|
$this->connect_timeout |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
// set necessary options |
|
85
|
|
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
|
86
|
|
|
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); |
|
87
|
|
|
curl_setopt($this->curl, CURLOPT_HEADER, false); |
|
88
|
|
|
|
|
89
|
|
|
// cookies |
|
90
|
|
|
curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookiejar); |
|
91
|
|
|
curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookiejar); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function setupCurlSSL() |
|
95
|
|
|
{ |
|
96
|
|
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, true); |
|
97
|
|
|
curl_setopt($this->curl, CURLOPT_SSLKEYTYPE, 'PEM'); |
|
98
|
|
|
|
|
99
|
|
|
if ($this->ca_cert) { |
|
100
|
|
|
curl_setopt($this->curl, CURLOPT_CAINFO, $this->ca_cert); |
|
101
|
|
|
} |
|
102
|
|
|
if ($this->pk_cert) { |
|
103
|
|
|
curl_setopt($this->curl, CURLOPT_SSLKEY, $this->pk_cert); |
|
104
|
|
|
} |
|
105
|
|
|
if ($this->local_cert) { |
|
106
|
|
|
curl_setopt($this->curl, CURLOPT_SSLCERT, $this->local_cert); |
|
107
|
|
|
} |
|
108
|
|
|
if ($this->passphrase) { |
|
109
|
|
|
curl_setopt($this->curl, CURLOPT_SSLCERTPASSWD, $this->passphrase); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Open a new connection to the EPP server |
|
115
|
|
|
* |
|
116
|
|
|
* @param bool|string $newPassword String with new password to set upon login, false if no password |
|
117
|
|
|
*/ |
|
118
|
|
|
public function connect($newPassword = false) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->setupCurl(); |
|
121
|
|
|
|
|
122
|
|
|
// get greeting |
|
123
|
|
|
$greeting = $this->request(new \AfriCC\EPP\Frame\Hello($this->objectSpec)); |
|
124
|
|
|
|
|
125
|
|
|
// login |
|
126
|
|
|
$this->login($newPassword); |
|
127
|
|
|
|
|
128
|
|
|
// return greeting |
|
129
|
|
|
return $greeting; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Closes a previously opened EPP connection |
|
134
|
|
|
*/ |
|
135
|
|
|
public function close() |
|
136
|
|
|
{ |
|
137
|
|
|
if ($this->active()) { |
|
138
|
|
|
// send logout frame |
|
139
|
|
|
$this->request(new LogoutCommand($this->objectSpec)); |
|
140
|
|
|
|
|
141
|
|
|
return curl_close($this->curl); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
protected function sendFrame(FrameInterface $frame) |
|
148
|
|
|
{ |
|
149
|
|
|
$content = (string) $frame; |
|
150
|
|
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $content); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
protected function getFrame() |
|
154
|
|
|
{ |
|
155
|
|
|
$return = curl_exec($this->curl); |
|
156
|
|
|
|
|
157
|
|
|
if ($return === false) { |
|
158
|
|
|
$code = curl_errno($this->curl); |
|
159
|
|
|
$msg = curl_error($this->curl); |
|
160
|
|
|
throw new \Exception($msg, $code); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $return; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
protected function log($message) |
|
167
|
|
|
{ |
|
168
|
|
|
if ($this->debug) { |
|
169
|
|
|
\error_log($message); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Check if curl session is still active |
|
175
|
|
|
* |
|
176
|
|
|
* @return bool |
|
177
|
|
|
*/ |
|
178
|
|
|
private function active() |
|
179
|
|
|
{ |
|
180
|
|
|
return is_resource($this->curl); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|