Skip to content

Commit

Permalink
Could as well implement those methods directly in Socket\Client.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Jan 28, 2015
1 parent 913efe4 commit c1e2d8d
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 155 deletions.
7 changes: 4 additions & 3 deletions framework/ManageSieve/lib/Horde/ManageSieve.php
Expand Up @@ -16,6 +16,7 @@
*/

namespace Horde;
use Horde\Socket\Client;
use Horde\ManageSieve;
use Horde\ManageSieve\Exception;

Expand Down Expand Up @@ -109,7 +110,7 @@ class ManageSieve
/**
* The socket client.
*
* @var \Horde\ManageSieve\Connection
* @var \Horde\Socket\Client
*/
protected $_sock;

Expand Down Expand Up @@ -274,14 +275,14 @@ public function connect(
}

try {
$this->_sock = new ManageSieve\Connection(
$this->_sock = new Client(
$this->_params['host'],
$this->_params['port'],
5,
$this->_params['usetls'],
$this->_params['context']
);
} catch (Socket\Client\Exception $e) {
} catch (Client\Exception $e) {
throw new Exception($e);
}

Expand Down
112 changes: 0 additions & 112 deletions framework/ManageSieve/lib/Horde/ManageSieve/Connection.php

This file was deleted.

6 changes: 2 additions & 4 deletions framework/ManageSieve/package.xml
Expand Up @@ -10,7 +10,7 @@
<email>jan@horde.org</email>
<active>yes</active>
</lead>
<date>2015-01-26</date>
<date>2015-01-28</date>
<version>
<release>1.0.0alpha1</release>
<api>1.0.0alpha1</api>
Expand Down Expand Up @@ -43,7 +43,6 @@
<file name="NotDisconnected.php" role="php" />
<file name="Referral.php" role="php" />
</dir> <!-- /lib/Horde/ManageSieve/Exception -->
<file name="Connection.php" role="php" />
<file name="Exception.php" role="php" />
</dir> <!-- /lib/Horde/ManageSieve -->
<file name="ManageSieve.php" role="php" />
Expand Down Expand Up @@ -78,7 +77,6 @@
<install as="LICENSE" name="doc/Horde/ManageSieve/LICENSE" />
<install as="UPGRADING" name="doc/Horde/ManageSieve/UPGRADING" />
<install as="Horde/ManageSieve.php" name="lib/Horde/ManageSieve.php" />
<install as="Horde/ManageSieve/Connection.php" name="lib/Horde/ManageSieve/Connection.php" />
<install as="Horde/ManageSieve/Exception.php" name="lib/Horde/ManageSieve/Exception.php" />
<install as="Horde/ManageSieve/Exception/ConnectionFailed.php" name="lib/Horde/ManageSieve/Exception/ConnectionFailed.php" />
<install as="Horde/ManageSieve/Exception/NotAuthenticated.php" name="lib/Horde/ManageSieve/Exception/NotAuthenticated.php" />
Expand All @@ -103,7 +101,7 @@
<release>alpha</release>
<api>alpha</api>
</stability>
<date>2015-01-26</date>
<date>2015-01-28</date>
<license uri="http://www.horde.org/licenses/bsd">BSD</license>
<notes>
* Initial release.
Expand Down
12 changes: 12 additions & 0 deletions framework/Socket_Client/doc/Horde/Socket/Client/UPGRADING
Expand Up @@ -11,6 +11,18 @@
This lists the API changes between releases of the package.


Upgrading to 1.2.0
===================

- Horde\Socket\Client

- Constructor

- The $host parameter accepts a protocol scheme prefix like 'unix://'.

- The $port parameter is optional.


Upgrading to 1.1.0
===================

Expand Down
150 changes: 122 additions & 28 deletions framework/Socket_Client/lib/Horde/Socket/Client.php
Expand Up @@ -59,26 +59,27 @@ class Client
/**
* Constructor.
*
* @param string $host Hostname of remote server.
* @param integer $port Port number of remote server.
* @param string $host Hostname of remote server. Optionally with
* protocol prefix (@since 1.2.0).
* @param integer $port Port number of remote server (optional
* @since 1.2.0).
* @param integer $timeout Connection timeout (in seconds).
* @param mixed $secure Security layer requested. One of:
* <pre>
* - 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
* - 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) (@since 1.1.0)
* - true (TLS if available/necessary)
* </pre>
* - 'tlsv1': (TLS version 1.x connection) (@since 1.1.0)
* - true: (TLS if available/necessary)
* @param array $params Additional options.
*
* @throws Horde\Socket\Client\Exception
*/
public function __construct(
$host, $port, $timeout = 30, $secure = false, array $params = array()
$host, $port = null, $timeout = 30, $secure = false,
array $params = array()
)
{
if ($secure && !extension_loaded('openssl')) {
Expand Down Expand Up @@ -151,6 +152,80 @@ public function close()
}
}

/**
* 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. */

/**
Expand All @@ -162,27 +237,34 @@ public function close()
*/
protected function _connect($host, $port, $timeout, $secure, $retries = 0)
{
switch (strval($secure)) {
case 'ssl':
case 'sslv2':
case 'sslv3':
$conn = $secure . '://';
$this->_secure = true;
break;
$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 'tlsv1':
$conn = 'tls://';
$this->_secure = true;
break;

case 'tls':
default:
$conn = 'tcp://';
break;
case 'tls':
default:
$conn = 'tcp://';
break;
}
}
$conn .= $host;
if ($port) {
$conn .= ':' . $port;
}

$this->_stream = @stream_socket_client(
$conn . $host . ':' . $port,
$conn,
$error_number,
$error_string,
$timeout,
Expand Down Expand Up @@ -226,4 +308,16 @@ protected function _connect($host, $port, $timeout, $secure, $retries = 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');
}
}

}

0 comments on commit c1e2d8d

Please sign in to comment.