public function connect($hostname = null, $port = null)
{
if ($hostname) {
$this->hostname = $hostname;
}
if ($port) {
$this->port = $port;
}
if ($this->local_cert_path) {
$ssl = true;
$target = sprintf('%s://%s:%d', $ssl === true ? 'ssl' : 'tcp', $this->hostname, $this->port);
$errno = '';
$errstr = '';
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'local_cert', $this->local_cert_path);
if (isset($this->local_cert_pwd) && strlen($this->local_cert_pwd) > 0) {
stream_context_set_option($context, 'ssl', 'passphrase', $this->local_cert_pwd);
}
if (isset($this->allow_self_signed)) {
stream_context_set_option($context, 'ssl', 'allow_self_signed', $this->allow_self_signed);
}
if ($this->connection = stream_socket_client($target, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $context)) {
$this->writeLog("Connection made", "CONNECT");
$this->connected = true;
$this->read();
return true;
} else {
throw new eppException("Error connecting to {$target}: {$errstr} (code {$errno})", $errno, null, $errstr);
}
} else {
//We don't want our error handler to kick in at this point...
//putenv('SURPRESS_ERROR_HANDLER=1');
#echo "Connecting: $this->hostname:$this->port\n";
#$this->writeLog("Connecting: $this->hostname:$this->port");
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'verify_peer', false);
stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
$this->connection = stream_socket_client($this->hostname . ':' . $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $context);
//putenv('SURPRESS_ERROR_HANDLER=0');
if (is_resource($this->connection)) {
stream_set_blocking($this->connection, false);
stream_set_timeout($this->connection, $this->timeout);
if ($errno == 0) {
$this->writeLog("Connection made", "CONNECT");
$this->connected = true;
$this->read();
return true;
} else {
return false;
}
} else {
$this->writeLog("Connection could not be opened: {$errno} {$errstr}", "ERROR");
return false;
}
}
}