Completed
Pull Request — master (#64)
by
unknown
01:39
created

HTTPClient::active()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
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
        // get greeting
95
        $greeting = $this->request(new \AfriCC\EPP\Frame\Hello());
96
97
        // login
98
        $this->login($newPassword);
99
100
        // return greeting
101
        return $greeting;
102
    }
103
104
    /**
105
     * Closes a previously opened EPP connection
106
     */
107
    public function close()
108
    {
109
        if ($this->active()) {
110
            // send logout frame
111
            $this->request(new LogoutCommand());
112
113
            return curl_close($this->curl);
114
        }
115
116
        return false;
117
    }
118
119
    /**
120
     * sends a XML-based frame to the server
121
     *
122
     * @param FrameInterface $frame the frame to send to the server
123
     *
124
     * @return string
125
     */
126
    public function send(FrameInterface $frame)
127
    {
128
        $content = (string) $frame;
129
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $content);
130
131
        return curl_exec($this->curl);
132
    }
133
134
    /**
135
     * request via EPP
136
     *
137
     * @param FrameInterface $frame Request frame to server
138
     *
139
     * @return string|\AfriCC\EPP\Frame\Response\MessageQueue|\AfriCC\EPP\Frame\Response Response from server
140
     */
141
    public function request(FrameInterface $frame)
142
    {
143
        if ($frame instanceof TransactionAwareInterface) {
144
            $frame->setClientTransactionId(
145
                $this->generateClientTransactionId()
146
                );
147
        }
148
149
        $return = $this->send($frame);
150
151
        if ($return === false) {
152
            $code = curl_errno($this->curl);
153
            $msg = curl_error($this->curl);
154
            throw new \Exception($msg, $code);
155
        }
156
157
        return ResponseFactory::build($return);
158
    }
159
160
    protected function log($message)
161
    {
162
        if ($this->debug) {
163
            \error_log($message);
164
        }
165
    }
166
167
    /**
168
     * Check if curl session is still active
169
     *
170
     * @return bool
171
     */
172
    private function active()
173
    {
174
        return is_resource($this->curl);
175
    }
176
}
177