PageRenderTime 67ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/ASTRA_Demo_Server/udrive/www/astra/interact/includes/pear/Net/Socket.php

https://github.com/shafiqissani/ASTRA-College-Website
PHP | 326 lines | 151 code | 33 blank | 142 comment | 30 complexity | 7f7661fbac1d87b66469996e53f659b3 MD5 | raw file
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no> |
  17. // | Chuck Hagenbuch <chuck@horde.org> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Socket.php,v 1.2 2005/03/21 01:04:56 websterb4 Exp $
  21. //
  22. require_once $CONFIG['INCLUDES_PATH'].'/pear/PEAR.php';
  23. /**
  24. * Generalized Socket class. More docs to be written.
  25. *
  26. * @version 0.2
  27. * @author Stig Bakken <ssb@fast.no>
  28. * @author Chuck Hagenbuch <chuck@horde.org>
  29. */
  30. class Net_Socket extends PEAR {
  31. // {{{ properties
  32. /** Socket file pointer. */
  33. var $fp = null;
  34. /** Whether the socket is blocking. */
  35. var $blocking = true;
  36. /** Whether the socket is persistent. */
  37. var $persistent;
  38. /** The IP address to connect to. */
  39. var $addr = '';
  40. /** The port number to connect to. */
  41. var $port = 0;
  42. /** Number of seconds to wait on socket connections before
  43. assuming there's no more data. */
  44. var $timeout = false;
  45. /** Number of bytes to read at a time in readLine() and
  46. readAll(). */
  47. var $lineLength = 2048;
  48. // }}}
  49. // {{{ constructor
  50. /**
  51. * Constructs a new Net_Socket object.
  52. *
  53. * @access public
  54. */
  55. function Net_Socket() {
  56. $this->PEAR();
  57. }
  58. // }}}
  59. // {{{ connect()
  60. /**
  61. * Connect to the specified port. If called when the socket is
  62. * already connected, it disconnects and connects again.
  63. *
  64. * @param $addr string IP address or host name
  65. * @param $port int TCP port number
  66. * @param $persistent bool (optional) whether the connection is
  67. * persistent (kept open between requests by the web server)
  68. * @param $timeout int (optional) how long to wait for data
  69. * @access public
  70. * @return mixed true on success or error object
  71. */
  72. function connect($addr, $port, $persistent = false, $timeout = false) {
  73. if (is_resource($this->fp)) {
  74. @fclose($this->fp);
  75. $this->fp = null;
  76. }
  77. if (strspn($addr, '.0123456789') == strlen($addr)) {
  78. $this->addr = $addr;
  79. } else {
  80. $this->addr = gethostbyname($addr);
  81. }
  82. $this->port = $port % 65536;
  83. $this->timeout = $timeout;
  84. $openfunc = $persistent ? 'pfsockopen' : 'fsockopen';
  85. $errno = 0;
  86. $errstr = '';
  87. if ($this->timeout) {
  88. $fp = $openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
  89. } else {
  90. $fp = $openfunc($this->addr, $this->port, $errno, $errstr);
  91. }
  92. if (!$fp) {
  93. return new PEAR_Error($errstr, $errno);
  94. }
  95. socket_set_blocking($fp, $this->blocking);
  96. $this->fp = $fp;
  97. $this->persistent = $persistent;
  98. return true;
  99. }
  100. // }}}
  101. // {{{ disconnect()
  102. /**
  103. * Disconnects from the peer, closes the socket.
  104. *
  105. * @access public
  106. * @return mixed true on success or an error object otherwise
  107. */
  108. function disconnect() {
  109. if (is_resource($this->fp)) {
  110. fclose($this->fp);
  111. $this->fp = null;
  112. return true;
  113. }
  114. return new PEAR_Error("not connected");
  115. }
  116. // }}}
  117. // {{{ isBlocking()
  118. /**
  119. * Find out if the socket is in blocking mode.
  120. *
  121. * @access public
  122. * @return bool the current blocking mode.
  123. */
  124. function isBlocking() {
  125. return $this->blocking;
  126. }
  127. // }}}
  128. // {{{ setBlocking()
  129. /**
  130. * Sets whether the socket connection should be blocking or
  131. * not. A read call to a non-blocking socket will return immediately
  132. * if there is no data available, whereas it will block until there
  133. * is data for blocking sockets.
  134. *
  135. * @param $mode bool true for blocking sockets, false for nonblocking
  136. * @access public
  137. */
  138. function setBlocking($mode) {
  139. $this->blocking = $mode;
  140. if (is_resource($this->fp)) {
  141. set_socket_blocking($this->fp, $mode);
  142. }
  143. }
  144. // }}}
  145. // {{{ gets()
  146. function gets($size) {
  147. if (is_resource($this->fp)) {
  148. return fgets($this->fp, $size);
  149. }
  150. return new PEAR_Error("not connected");
  151. }
  152. // }}}
  153. // {{{ read()
  154. /**
  155. * Read a specified amount of data. This is guaranteed to return,
  156. * and has the added benefit of getting everything in one fread()
  157. * chunk; if you know the size of the data you're getting
  158. * beforehand, this is definitely the way to go.
  159. *
  160. * @param $size The number of bytes to read from the socket.
  161. * @access public
  162. * @return $size bytes of data from the socket, or a PEAR_Error if
  163. * not connected.
  164. */
  165. function read($size) {
  166. if (is_resource($this->fp)) {
  167. return fread($this->fp, $size);
  168. }
  169. return new PEAR_Error("not connected");
  170. }
  171. // }}}
  172. // {{{ write()
  173. function write($data) {
  174. if (is_resource($this->fp)) {
  175. return fwrite($this->fp, $data);
  176. }
  177. return new PEAR_Error("not connected");
  178. }
  179. // }}}
  180. // {{{ writeLine()
  181. /**
  182. * Write a line of data to the socket, followed by a trailing "\r\n".
  183. *
  184. * @access public
  185. * @return mixed fputs result, or an error
  186. */
  187. function writeLine ($data) {
  188. if (is_resource($this->fp)) {
  189. return fwrite($this->fp, "$data\r\n");
  190. }
  191. return new PEAR_Error("not connected");
  192. }
  193. // }}}
  194. // {{{ eof()
  195. function eof() {
  196. return (is_resource($this->fp) && feof($this->fp));
  197. }
  198. // }}}
  199. // {{{ readByte()
  200. function readByte() {
  201. if (is_resource($this->fp)) {
  202. return ord(fread($this->fp, 1));
  203. }
  204. return new PEAR_Error("not connected");
  205. }
  206. // }}}
  207. // {{{ readWord()
  208. function readWord() {
  209. if (is_resource($this->fp)) {
  210. $buf = fread($this->fp, 2);
  211. return (ord($buf[0]) + (ord($buf[1]) << 8));
  212. }
  213. return new PEAR_Error("not connected");
  214. }
  215. // }}}
  216. // {{{ readInt()
  217. function readInt() {
  218. if (is_resource($this->fp)) {
  219. $buf = fread($this->fp, 4);
  220. return (ord($buf[0]) + (ord($buf[1]) << 8) +
  221. (ord($buf[2]) << 16) + (ord($buf[3]) << 24));
  222. }
  223. return new PEAR_Error("not connected");
  224. }
  225. // }}}
  226. // {{{ readString()
  227. function readString() {
  228. if (is_resource($this->fp)) {
  229. $string = '';
  230. while (($char = fread($this->fp, 1)) != "\x00") {
  231. $string .= $char;
  232. }
  233. return $string;
  234. }
  235. return new PEAR_Error("not connected");
  236. }
  237. // }}}
  238. // {{{ readIPAddress()
  239. function readIPAddress() {
  240. if (is_resource($this->fp)) {
  241. $buf = fread($this->fp, 4);
  242. return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]),
  243. ord($buf[2]), ord($buf[3]));
  244. }
  245. return new PEAR_Error("not connected");
  246. }
  247. // }}}
  248. // {{{ readLine()
  249. /**
  250. * Read until either the end of the socket or a newline, whichever
  251. * comes first. Strips the trailing newline from the returned data.
  252. *
  253. * @access public
  254. * @return All available data up to a newline, without that
  255. * newline, or until the end of the socket.
  256. */
  257. function readLine() {
  258. if (is_resource($this->fp)) {
  259. $line = '';
  260. $timeout = time() + $this->timeout;
  261. while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) {
  262. $line .= fgets($this->fp, $this->lineLength);
  263. if (strlen($line) >= 2 &&
  264. (substr($line, -2) == "\r\n" ||
  265. substr($line, -1) == "\n")) {
  266. return rtrim($line);
  267. }
  268. }
  269. return $line;
  270. }
  271. return new PEAR_ERROR("not connected");
  272. }
  273. // }}}
  274. // {{{ readAll()
  275. /**
  276. * Read until the socket closes. THIS FUNCTION WILL NOT EXIT if the
  277. * socket is in blocking mode until the socket closes.
  278. *
  279. * @access public
  280. * @return All data until the socket closes.
  281. */
  282. function readAll() {
  283. if (is_resource($this->fp)) {
  284. $data = '';
  285. while (!feof($this->fp))
  286. $data .= fread($this->fp, $this->lineLength);
  287. return $data;
  288. }
  289. return new PEAR_Error("not connected");
  290. }
  291. // }}}
  292. }
  293. ?>