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