/modules/esl-1.1/libraries/FS_Socket.php

https://github.com/robertleeplummerjr/bluebox · PHP · 266 lines · 122 code · 40 blank · 104 comment · 16 complexity · 2fcbae7d1949a7b1c718d22d6fcefd33 MD5 · raw file

  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /**
  3. * FS_Socket.php - This supports ESLconnection when the native ESL
  4. * extension is not avaliable
  5. *
  6. * @author K Anderson
  7. * @license LGPL
  8. * @package Esl
  9. */
  10. class FS_Socket
  11. {
  12. /**
  13. * @var int $timeOut
  14. * @desc The timeout used to open the socket
  15. */
  16. private $timeOut = 10;
  17. /**
  18. * @var resource $connection
  19. * @desc Connection resource
  20. */
  21. private $connection = NULL;
  22. /**
  23. * @var string $connectionState
  24. * @desc
  25. */
  26. private $connectionState = FALSE;
  27. /**
  28. * @var float $defaultTimeout
  29. * @desc Default timeout for connection to a server
  30. */
  31. private $defaultTimeout = 30;
  32. /**
  33. * @var bool $persistentConnection
  34. * @desc Determines wether to use a persistent socket connection or not
  35. */
  36. private $persistentConnection = FALSE;
  37. /**
  38. * If there still was a connection alive, disconnect it
  39. */
  40. public function __destruct()
  41. {
  42. $this->disconnect();
  43. }
  44. /**
  45. * Connects to the socket with the given address and port
  46. *
  47. * @return void
  48. */
  49. protected function connect($host, $port, $options = array())
  50. {
  51. // initialize our defaults
  52. $options += array(
  53. 'persistentConnection' => $this->persistentConnection,
  54. 'timeOut' => $this->defaultTimeout,
  55. 'blocking' => 1
  56. );
  57. extract($options);
  58. // store the timeOut that was actually used
  59. $this->timeOut = $timeOut;
  60. // decided the stream type
  61. $socketFunction = $persistentConnection ? "pfsockopen" : "fsockopen";
  62. // Check if the function parameters are set.
  63. if(empty($host))
  64. {
  65. $this->_throwError("invalid host provided!");
  66. }
  67. if(empty($port))
  68. {
  69. $this->_throwError("invalid port provided!");
  70. }
  71. // attempt to open a socket
  72. $connection = @$socketFunction($host, $port, $errorNumber, $errorString, $timeOut);
  73. $this->connection = $connection;
  74. // if we didnt get a valid socket throw an error
  75. if($connection == false)
  76. {
  77. $this->_throwError("connection to {$host}:{$port} failed: {$errorString}");
  78. }
  79. // initialize our stream blocking setting
  80. stream_set_blocking($this->connection, $blocking);
  81. // set this stream as connected
  82. $this->connectionState = TRUE;
  83. }
  84. /**
  85. * Disconnects from the server
  86. *
  87. * @return bool
  88. */
  89. protected function disconnect()
  90. {
  91. if($this->validateConnection())
  92. {
  93. fclose($this->connection);
  94. $this->connectionState = FALSE;
  95. return TRUE;
  96. }
  97. return FALSE;
  98. }
  99. /**
  100. * Sends a command to the server
  101. *
  102. * @return string
  103. */
  104. protected function sendCmd($command)
  105. {
  106. if($this->validateConnection())
  107. {
  108. $command = trim($command);
  109. kohana::log('debug', 'ESL Command "' .$command .'"');
  110. $command .= "\n\n";
  111. $result = fwrite($this->connection, $command, strlen($command));
  112. return $result;
  113. }
  114. $this->_throwError("sending command \"{$command}\" failed: Not connected");
  115. }
  116. /**
  117. * Gets the content/body of the response
  118. *
  119. * @return string
  120. */
  121. protected function getContent($contentLength = 2048)
  122. {
  123. if($this->validateConnection())
  124. {
  125. return fread($this->connection, (int)$contentLength);
  126. }
  127. $this->_throwError("receiving content from server failed: Not connected");
  128. }
  129. /**
  130. * Reads a line out of buffer
  131. *
  132. * @return string
  133. */
  134. protected function readLine()
  135. {
  136. if($this->validateConnection()) {
  137. return fgets($this->connection, 1024);
  138. }
  139. $this->_throwError("read line failed: Not connected");
  140. }
  141. /**
  142. * Sets the socket to blocking operations
  143. *
  144. * @return bool
  145. */
  146. protected function setBlocking() {
  147. if($this->validateConnection()) {
  148. return stream_set_blocking($this->connection, 1);
  149. }
  150. $this->_throwError("set stream to blocking failed: Not connected");
  151. }
  152. /**
  153. * Sets the socket to non-blocking operations
  154. *
  155. * @return bool
  156. */
  157. protected function setNonBlocking() {
  158. if($this->validateConnection()) {
  159. return stream_set_blocking($this->connection, 0);
  160. }
  161. $this->_throwError("set stream to non-blocking failed: Not connected");
  162. }
  163. /**
  164. * Sets the timeout for this socket to an arbitrary value
  165. *
  166. * @return bool
  167. */
  168. protected function setTimeOut($seconds = 0, $milliseconds = 0) {
  169. if($this->validateConnection()) {
  170. return stream_set_timeout ($this->connection, (int)$seconds, (int)$milliseconds);
  171. }
  172. $this->_throwError("set stream timeout failed: Not connected");
  173. }
  174. /**
  175. * Restores the time out for this socket to the value it was opened with
  176. *
  177. * @return bool
  178. */
  179. protected function restoreTimeOut() {
  180. if($this->validateConnection()) {
  181. return stream_set_timeout ($this->connection, $this->timeOut, 0);
  182. }
  183. $this->_throwError("restore stream timeout failed: Not connected");
  184. }
  185. /**
  186. * Gets the meta data on this socket
  187. *
  188. * @return string
  189. */
  190. protected function getMetaData() {
  191. if($this->validateConnection()) {
  192. return stream_get_meta_data($this->connection);
  193. }
  194. $this->_throwError("get stream meta data failed: Not connected");
  195. }
  196. /**
  197. * Gets the socket status
  198. *
  199. * @return string
  200. */
  201. protected function getStatus() {
  202. if($this->validateConnection())
  203. {
  204. return socket_get_status($this->connection);
  205. }
  206. $this->_throwError("getting socket descriptor failed: Not connected");
  207. }
  208. /**
  209. * Validates the connection state
  210. *
  211. * @return bool
  212. */
  213. protected function validateConnection()
  214. {
  215. return (is_resource($this->connection) && ($this->connectionState != FALSE));
  216. }
  217. /**
  218. * Throws an error
  219. *
  220. * @return void
  221. */
  222. private function _throwError($errorMessage)
  223. {
  224. throw new ESLException("Socket {$errorMessage}");
  225. }
  226. }