1 | <?php |
||
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() |
||
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) |
||
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) |
||
160 | |||
161 | protected function log($message) |
||
167 | |||
168 | /** |
||
169 | * Check if curl session is still active |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | private function active() |
||
177 | |||
178 | } |
||
179 | |||
180 |