/usr/share/php/Horde/Socket/Client.php is in php-horde-socket-client 2.1.0-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | <?php
namespace Horde\Socket;
/**
* Copyright 2013-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2013-2016 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Socket_Client
*/
/**
* Utility interface for establishing a stream socket client.
*
* @author Michael Slusarz <slusarz@horde.org>
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2013-2016 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Socket_Client
*
* @property-read boolean $connected Is there an active connection?
* @property-read boolean $secure Is the active connection secure?
*/
class Client
{
/**
* Is there an active connection?
*
* @var boolean
*/
protected $_connected = false;
/**
* Configuration parameters.
*
* @var array
*/
protected $_params;
/**
* Is the connection secure?
*
* @var boolean
*/
protected $_secure = false;
/**
* The actual socket.
*
* @var resource
*/
protected $_stream;
/**
* Constructor.
*
* @param string $host Hostname of remote server (can contain
* protocol prefx).
* @param integer $port Port number of remote server.
* @param integer $timeout Connection timeout (in seconds).
* @param mixed $secure Security layer requested. One of:
* - false: (No encryption) [DEFAULT]
* - 'ssl': (Auto-detect SSL version)
* - 'sslv2': (Force SSL version 3)
* - 'sslv3': (Force SSL version 2)
* - 'tls': (TLS; started via protocol-level negotation over unencrypted
* channel)
* - 'tlsv1': (TLS version 1.x connection)
* - true: (TLS if available/necessary)
* @param array $context Any context parameters passed to
* stream_create_context().
* @param array $params Additional options.
*
* @throws Horde\Socket\Client\Exception
*/
public function __construct(
$host, $port = null, $timeout = 30, $secure = false,
$context = array(), array $params = array()
)
{
if ($secure && !extension_loaded('openssl')) {
if ($secure !== true) {
throw new \InvalidArgumentException('Secure connections require the PHP openssl extension.');
}
$secure = false;
}
$context = array_merge_recursive(
array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false
)
),
$context
);
$this->_params = $params;
$this->_connect($host, $port, $timeout, $secure, $context);
}
/**
*/
public function __get($name)
{
switch ($name) {
case 'connected':
return $this->_connected;
case 'secure':
return $this->_secure;
}
}
/**
* This object can not be cloned.
*/
public function __clone()
{
throw new \LogicException('Object cannot be cloned.');
}
/**
* This object can not be serialized.
*/
public function __sleep()
{
throw new \LogicException('Object can not be serialized.');
}
/**
* Start a TLS connection.
*
* @return boolean Whether TLS was successfully started.
*/
public function startTls()
{
if ($this->connected && !$this->secure) {
if (defined('STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT')) {
$mode = STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT
| STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT
| STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
} else {
$mode = STREAM_CRYPTO_METHOD_TLS_CLIENT;
}
if (@stream_socket_enable_crypto($this->_stream, true, $mode) === true) {
$this->_secure = true;
return true;
}
}
return false;
}
/**
* Close the connection.
*/
public function close()
{
if ($this->connected) {
@fclose($this->_stream);
$this->_connected = $this->_secure = false;
$this->_stream = null;
}
}
/**
* Returns information about the connection.
*
* Currently returns four entries in the result array:
* - timed_out (bool): The socket timed out waiting for data
* - blocked (bool): The socket was blocked
* - eof (bool): Indicates EOF event
* - unread_bytes (int): Number of bytes left in the socket buffer
*
* @throws Horde\Socket\Client\Exception
* @return array Information about existing socket resource.
*/
public function getStatus()
{
$this->_checkStream();
return stream_get_meta_data($this->_stream);
}
/**
* Returns a line of data.
*
* @param int $size Reading ends when $size - 1 bytes have been read,
* or a newline or an EOF (whichever comes first).
*
* @throws Horde\Socket\Client\Exception
* @return string $size bytes of data from the socket
*/
public function gets($size)
{
$this->_checkStream();
$data = @fgets($this->_stream, $size);
if ($data === false) {
throw new Client\Exception('Error reading data from socket');
}
return $data;
}
/**
* Returns a specified amount of data.
*
* @param integer $size The number of bytes to read from the socket.
*
* @throws Horde\Socket\Client\Exception
* @return string $size bytes of data from the socket.
*/
public function read($size)
{
$this->_checkStream();
$data = @fread($this->_stream, $size);
if ($data === false) {
throw new Client\Exception('Error reading data from socket');
}
return $data;
}
/**
* Writes data to the stream.
*
* @param string $data Data to write.
*
* @throws Horde\Socket\Client\Exception
*/
public function write($data)
{
$this->_checkStream();
if (!@fwrite($this->_stream, $data)) {
$meta_data = $this->getStatus();
if (!empty($meta_data['timed_out'])) {
throw new Client\Exception('Timed out writing data to socket');
}
throw new Client\Exception('Error writing data to socket');
}
}
/* Internal methods. */
/**
* Connect to the remote server.
*
* @see __construct()
*
* @throws Horde\Socket\Client\Exception
*/
protected function _connect(
$host, $port, $timeout, $secure, $context, $retries = 0
)
{
$conn = '';
if (!strpos($host, '://')) {
switch (strval($secure)) {
case 'ssl':
case 'sslv2':
case 'sslv3':
$conn = $secure . '://';
$this->_secure = true;
break;
case 'tlsv1':
$conn = 'tls://';
$this->_secure = true;
break;
case 'tls':
default:
$conn = 'tcp://';
break;
}
}
$conn .= $host;
if ($port) {
$conn .= ':' . $port;
}
$this->_stream = @stream_socket_client(
$conn,
$error_number,
$error_string,
$timeout,
STREAM_CLIENT_CONNECT,
stream_context_create($context)
);
if ($this->_stream === false) {
/* From stream_socket_client() page: a function return of false,
* with an error code of 0, indicates a "problem initializing the
* socket". These kind of issues are seen on the same server
* (and even the same user account) as sucessful connections, so
* these are likely transient issues. Retry up to 3 times in these
* instances. */
if (!$error_number && ($retries < 3)) {
return $this->_connect($host, $port, $timeout, $secure, ++$retries, $context);
}
$e = new Client\Exception(
'Error connecting to server.'
);
$e->details = sprintf("[%u] %s", $error_number, $error_string);
throw $e;
}
stream_set_timeout($this->_stream, $timeout);
if (function_exists('stream_set_read_buffer')) {
stream_set_read_buffer($this->_stream, 0);
}
stream_set_write_buffer($this->_stream, 0);
$this->_connected = true;
}
/**
* Throws an exception is the stream is not a resource.
*
* @throws Horde\Socket\Client\Exception
*/
protected function _checkStream()
{
if (!is_resource($this->_stream)) {
throw new Client\Exception('Not connected');
}
}
}
|