本文整理汇总了PHP中stream_socket_enable_crypto函数的典型用法代码示例。如果您正苦于以下问题:PHP stream_socket_enable_crypto函数的具体用法?PHP stream_socket_enable_crypto怎么用?PHP stream_socket_enable_crypto使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了stream_socket_enable_crypto函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: connect
public function connect()
{
// try open socket
if ($this->getOption('tls')) {
$this->handler = @stream_socket_client('tcp://' . $this->server . ':' . $this->port, $errno, $errstr, $this->getOption('timeout', 10));
if ($this->handler) {
stream_socket_enable_crypto($this->handler, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
}
} else {
$this->handler = @fsockopen($this->server, $this->port, $errno, $errstr, $this->getOption('timeout', 10));
}
if ($this->handler) {
// read welcome
$this->read();
// auth
$this->auth();
} else {
if (!preg_match('//u', $errstr)) {
$tmp = @iconv('windows-1251', 'utf-8//ignore', $errstr);
if ($tmp) {
$errstr = $tmp;
}
}
throw new waException($errstr . ' (' . $errno . ')', $errno);
}
}
示例2: StartTLS
public function StartTLS()
{
$this->error = null;
# to avoid confusion
if (!$this->connected()) {
$this->error = array("error" => "Called StartTLS() without being connected");
return false;
}
fputs($this->smtp_conn, "STARTTLS" . $extra . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply, 0, 3);
if ($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if ($code != 220) {
$this->error = array("error" => "STARTTLS not accepted from server", "smtp_code" => $code, "smtp_msg" => substr($rply, 4));
if ($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF;
}
return false;
}
//Begin encrypted connection
if (!stream_socket_enable_crypto($this->smtp_conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
return false;
}
return true;
}
示例3: enableSocketEncryption
protected function enableSocketEncryption($socket, int $modes, float $timeout = 0) : \Generator
{
if ($modes === 0) {
throw new SocketEncryptionException('No socket client encryption modes available');
}
$timed = $timeout;
try {
while (true) {
(yield new AwaitWrite($socket, $timeout));
$enabled = @\stream_socket_enable_crypto($socket, true, $modes);
if ($enabled === false) {
throw new SocketEncryptionException(\sprintf('Failed to enable socket encryption: %s', \error_get_last()['message'] ?? ''));
}
if ($enabled !== 0) {
break;
}
$timeout = max(0, $timeout - (yield new Pause(0.005, 0.005)));
if ($timed > 0 && $timeout == 0) {
throw new TimeoutException(\sprintf('Socket encryption activation timed out after %.3f seconds', $timed));
}
}
} catch (SocketEncryptionException $e) {
throw $e;
} catch (\Throwable $e) {
throw new SocketEncryptionException('Failed to enable socket encryption', 0, $e);
}
}
示例4: handleConnectedSocket
public function handleConnectedSocket($callback, $socket)
{
$loop = $this->loop;
$enableCrypto = function () use($callback, $socket, $loop) {
$result = stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
if (true === $result) {
// crypto was successfully enabled
$loop->removeWriteStream($socket);
$loop->removeReadStream($socket);
call_user_func($callback, new Stream($socket, $loop));
} else {
if (false === $result) {
// an error occured
$loop->removeWriteStream($socket);
$loop->removeReadStream($socket);
call_user_func($callback, null);
} else {
// need more data, will retry
}
}
};
$this->loop->addWriteStream($socket, $enableCrypto);
$this->loop->addReadStream($socket, $enableCrypto);
$enableCrypto();
}
示例5: enableCrypto
/**
* {@inheritdoc}
*/
public function enableCrypto($enable, $cryptoType = STREAM_CRYPTO_METHOD_TLS_CLIENT)
{
if (!stream_socket_enable_crypto($this->stream, $enable, $cryptoType)) {
throw new SocketException();
}
return $this;
}
示例6: connect
/**
* Open connection to IMAP server
*
* @param string $host hostname or IP address of IMAP server
* @param int|null $port of IMAP server, default is 143 (993 for ssl)
* @param string|bool $ssl use 'SSL', 'TLS' or false
* @throws Exception\RuntimeException
* @return string welcome message
*/
public function connect($host, $port = null, $ssl = false)
{
if ($ssl == 'SSL') {
$host = 'ssl://' . $host;
}
if ($port === null) {
$port = $ssl === 'SSL' ? 993 : 143;
}
$errno = 0;
$errstr = '';
$this->_socket = @fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION);
if (!$this->_socket) {
throw new Exception\RuntimeException('cannot connect to host; error = ' . $errstr . ' (errno = ' . $errno . ' )');
}
if (!$this->_assumedNextLine('* OK')) {
throw new Exception\RuntimeException('host doesn\'t allow connection');
}
if ($ssl === 'TLS') {
$result = $this->requestAndResponse('STARTTLS');
$result = $result && stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
if (!$result) {
throw new Exception\RuntimeException('cannot enable TLS');
}
}
}
示例7: connect
/**
* Open connection to IMAP server
*
* @param string $host hostname or IP address of IMAP server
* @param int|null $port of IMAP server, default is 143 (993 for ssl)
* @param string|bool $ssl use 'SSL', 'TLS' or false
* @throws Exception\RuntimeException
* @return string welcome message
*/
public function connect($host, $port = null, $ssl = false)
{
if ($ssl == 'SSL') {
$host = 'ssl://' . $host;
}
if ($port === null) {
$port = $ssl === 'SSL' ? 993 : 143;
}
ErrorHandler::start();
$this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION);
$error = ErrorHandler::stop();
if (!$this->socket) {
throw new Exception\RuntimeException(sprintf('cannot connect to host%s', $error ? sprintf('; error = %s (errno = %d )', $error->getMessage(), $error->getCode()) : ''), 0, $error);
}
if (!$this->_assumedNextLine('* OK')) {
throw new Exception\RuntimeException('host doesn\'t allow connection');
}
if ($ssl === 'TLS') {
$result = $this->requestAndResponse('STARTTLS');
$result = $result && stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
if (!$result) {
throw new Exception\RuntimeException('cannot enable TLS');
}
}
}
示例8: enableCrypto
/**
* {@inheritdoc}
*/
public function enableCrypto(int $method, float $timeout = 0) : \Generator
{
$resource = $this->getResource();
if ($method & 1 || 0 === $method) {
yield from $this->await($timeout);
} else {
yield from $this->poll($timeout);
$raw = stream_socket_recvfrom($resource, 11, STREAM_PEEK);
if (11 > strlen($raw)) {
throw new FailureException('Failed to read crypto handshake.');
}
$data = unpack('ctype/nversion/nlength/Nembed/nmax-version', $raw);
if (0x16 !== $data['type']) {
throw new FailureException('Invalid crypto handshake.');
}
$version = $this->selectCryptoVersion($data['max-version']);
if ($method & $version) {
// Check if version was available in $method.
$method = $version;
}
}
do {
// Error reporting suppressed since stream_socket_enable_crypto() emits E_WARNING on failure.
$result = @stream_socket_enable_crypto($resource, (bool) $method, $method);
} while (0 === $result && !(yield from $this->poll($timeout)));
if ($result) {
$this->crypto = $method;
return;
}
$message = 'Failed to enable crypto.';
if ($error = error_get_last()) {
$message .= sprintf(' Errno: %d; %s', $error['type'], $error['message']);
}
throw new FailureException($message);
}
示例9: _auth
/**
* Аутентификация пользователя
*
* @param String $login
* @param String $password
* @param Bool $admin
* @return Bool
*/
protected function _auth($login, $password, $admin)
{
$packet = $this->packet();
while (!feof($this->_socket)) {
$packet->clean();
$this->read($packet);
switch ($this->_code) {
case 192:
$digest = $packet->_attr[6]['data'];
$ctx = hash_init('md5');
hash_update($ctx, $digest);
hash_update($ctx, $password);
$hash = hash_final($ctx, true);
$packet->clean();
$this->_code = 193;
$packet->set_attr_string($login, 2);
$packet->set_attr_string($digest, 8);
$packet->set_attr_string($hash, 9);
$packet->set_attr_int($admin ? 4 : 2, 10);
$packet->set_attr_int(2, 1);
$this->write($packet);
break;
case 194:
if ($packet->get_attr_int(10)) {
stream_socket_enable_crypto($this->_socket, TRUE, STREAM_CRYPTO_METHOD_SSLv3_CLIENT);
}
return TRUE;
case 195:
return FALSE;
}
}
}
示例10: _smtp_connect
/**
* SMTP Connect
*
* @access protected
* @return string
*/
protected function _smtp_connect()
{
$ssl = NULL;
if ($this->smtp_crypto == 'ssl') {
$ssl = 'ssl://';
}
$CI =& get_instance();
if (!empty($this->proxy)) {
$context = stream_context_create(['http' => ['proxy' => 'tcp://' . $this->proxy]]);
$this->_smtp_connect = stream_socket_client($ssl . $this->smtp_host . ':' . $this->smtp_port, $errno, $errstr, $this->smtp_timeout, STREAM_CLIENT_CONNECT, $context);
} else {
$this->_smtp_connect = fsockopen($ssl . $this->smtp_host, $this->smtp_port, $errno, $errstr, $this->smtp_timeout);
}
if (!is_resource($this->_smtp_connect)) {
$this->_set_error_message('lang:email_smtp_error', $errno . " " . $errstr);
return FALSE;
}
$this->_set_error_message($this->_get_smtp_data());
if ($this->smtp_crypto == 'tls') {
$this->_send_command('hello');
$this->_send_command('starttls');
stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
}
return $this->_send_command('hello');
}
示例11: auth
protected function auth()
{
fputs($this->conn, 'HELO ' . $this->localhost . $this->newline);
$this->getServerResponse();
if ($this->secure == 'tls') {
fputs($this->conn, 'STARTTLS' . $this->newline);
if (substr($this->getServerResponse(), 0, 3) != '220') {
return false;
}
stream_socket_enable_crypto($this->conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
fputs($this->conn, 'HELO ' . $this->localhost . $this->newline);
if (substr($this->getServerResponse(), 0, 3) != '250') {
return false;
}
}
if ($this->server != 'localhost') {
fputs($this->conn, 'AUTH LOGIN' . $this->newline);
if (substr($this->getServerResponse(), 0, 3) != '334') {
return false;
}
fputs($this->conn, base64_encode($this->username) . $this->newline);
if (substr($this->getServerResponse(), 0, 3) != '334') {
return false;
}
fputs($this->conn, base64_encode($this->password) . $this->newline);
if (substr($this->getServerResponse(), 0, 3) != '235') {
return false;
}
}
return true;
}
示例12: handleData
public function handleData($stream)
{
if (!$this->isSecure) {
$enabled = @stream_socket_enable_crypto($stream, true, $this->protocolNumber);
if ($enabled === false) {
$this->err('Failed to complete a secure handshake with the client.')->end();
return;
} elseif ($enabled === 0) {
return;
}
$this->isSecure = true;
$this->emit('connection', array($this));
$scope = $this;
$this->on('close', function () use($scope, $stream) {
if (false === stream_socket_enable_crypto($stream, false)) {
$scope->err('Failed to gracefully shutdown a secure connection.');
}
});
}
$data = fread($stream, $this->bufferSize);
if ('' !== $data && false !== $data) {
$this->emit('data', array($data, $this));
}
if (false === $data || !is_resource($stream) || feof($stream)) {
$this->end();
}
}
示例13: connect
/**
* Connect, then enable crypto
*
* @param float $timeout
* @return bool
* @throws peer.SSLUnverifiedPeerException if peer verification fails
* @throws peer.SSLHandshakeException if handshake fails for any other reasons
* @throws peer.ConnectException for all other reasons
*/
public function connect($timeout = 2.0)
{
if ($this->isConnected()) {
return true;
}
parent::connect($timeout);
if (stream_socket_enable_crypto($this->_sock, true, $this->cryptoImpl)) {
return true;
}
// Parse OpenSSL errors:
if (preg_match('/error:(\\d+):(.+)/', key(end(\xp::$errors[__FILE__])), $matches)) {
switch ($matches[1]) {
case '14090086':
$e = new SSLUnverifiedPeerException($matches[2]);
break;
default:
$e = new SSLHandshakeException($matches[2]);
break;
}
} else {
$e = new SSLHandshakeException('Unable to enable crypto.');
}
$this->close();
throw $e;
}
示例14: startTLS
/**
* Start TLS encryption on the buffer.
*/
public function startTLS()
{
if (isset($this->_stream)) {
return stream_socket_enable_crypto($this->_stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
} else {
return false;
}
}
示例15: do_enable_crypto_test
function do_enable_crypto_test($url, $context)
{
$fh = stream_socket_client($url, $errno, $errstr, ini_get("default_socket_timeout"), STREAM_CLIENT_CONNECT, $context);
assert($fh);
$r = stream_socket_enable_crypto($fh, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
assert($r);
var_dump(get_CN($context));
}