1
|
|
|
<?php |
2
|
|
|
namespace AfriCC\EPP; |
3
|
|
|
|
4
|
|
|
use AfriCC\EPP\Frame\ResponseFactory; |
5
|
|
|
use AfriCC\EPP\Frame\Command\Logout as LogoutCommand; |
6
|
|
|
|
7
|
|
|
class HTTPClient extends AbstractClient implements ClientInterface |
8
|
|
|
{ |
9
|
|
|
protected $curl; |
10
|
|
|
protected $cookiejar; |
11
|
|
|
|
12
|
|
|
public function __construct(array $config) |
13
|
|
|
{ |
14
|
|
|
parent::__construct($config); |
15
|
|
|
|
16
|
|
|
if (!empty($config['cookiejar'])) { |
17
|
|
|
$this->cookiejar = $config['cookiejar']; |
18
|
|
|
} else { |
19
|
|
|
$this->cookiejar = tempnam(sys_get_temp_dir(), 'ehc'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if (!is_readable($this->cookiejar) || !is_writable($this->cookiejar)) { |
23
|
|
|
throw new \Exception( |
24
|
|
|
sprintf( |
25
|
|
|
'unable to read/write cookiejar: %s', |
26
|
|
|
$this->cookiejar |
27
|
|
|
) |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function __destruct() |
33
|
|
|
{ |
34
|
|
|
$this->close(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Open a new connection to the EPP server |
39
|
|
|
* |
40
|
|
|
* @param bool|string $newPassword String with new password to set upon login, false if no password |
41
|
|
|
*/ |
42
|
|
|
public function connect($newPassword=false) |
43
|
|
|
{ |
44
|
|
|
$proto = \parse_url($this->host, PHP_URL_SCHEME); |
45
|
|
|
if ($this->ssl || $proto == 'https') { |
46
|
|
|
$this->ssl = true; |
47
|
|
|
|
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
|
|
|
return curl_close($this->curl); |
114
|
|
|
} |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* sends a XML-based frame to the server |
120
|
|
|
* |
121
|
|
|
* @param FrameInterface $frame the frame to send to the server |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function send(FrameInterface $frame) |
125
|
|
|
{ |
126
|
|
|
|
127
|
|
|
$content = (string)$frame; |
128
|
|
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $content); |
129
|
|
|
|
130
|
|
|
return curl_exec($this->curl); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* request via EPP |
135
|
|
|
* |
136
|
|
|
* @param FrameInterface $frame Request frame to server |
137
|
|
|
* @return string|\AfriCC\EPP\Frame\Response\MessageQueue|\AfriCC\EPP\Frame\Response Response from server |
138
|
|
|
*/ |
139
|
|
|
public function request(FrameInterface $frame) |
140
|
|
|
{ |
141
|
|
|
|
142
|
|
|
if ($frame instanceof TransactionAwareInterface) { |
143
|
|
|
$frame->setClientTransactionId( |
144
|
|
|
$this->generateClientTransactionId() |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$return = $this->send($frame); |
149
|
|
|
|
150
|
|
|
if ($return === false) { |
151
|
|
|
$code = curl_errno($this->curl); |
152
|
|
|
$msg = curl_error($this->curl); |
153
|
|
|
throw new \Exception($msg, $code); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return ResponseFactory::build($return); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function log($message) |
160
|
|
|
{ |
161
|
|
|
if($this->debug) { |
162
|
|
|
\error_log($message); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* check if curl session is still active |
168
|
|
|
* @return boolean |
169
|
|
|
*/ |
170
|
|
|
private function active() |
171
|
|
|
{ |
172
|
|
|
return is_resource($this->curl); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.