PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/socket.php

https://github.com/hardsshah/bookmarks
PHP | 281 lines | 123 code | 11 blank | 147 comment | 29 complexity | cd5ede8cecebed68399e531262deae0d MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Cake Socket connection class.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  9. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @filesource
  15. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  16. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  17. * @package cake
  18. * @subpackage cake.cake.libs
  19. * @since CakePHP(tm) v 1.2.0
  20. * @version $Revision$
  21. * @modifiedby $LastChangedBy$
  22. * @lastmodified $Date$
  23. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  24. */
  25. App::import('Core', 'Validation');
  26. /**
  27. * Cake network socket connection class.
  28. *
  29. * Core base class for network communication.
  30. *
  31. * @package cake
  32. * @subpackage cake.cake.libs
  33. */
  34. class CakeSocket extends Object {
  35. /**
  36. * Object description
  37. *
  38. * @var string
  39. * @access public
  40. */
  41. var $description = 'Remote DataSource Network Socket Interface';
  42. /**
  43. * Base configuration settings for the socket connection
  44. *
  45. * @var array
  46. * @access protected
  47. */
  48. var $_baseConfig = array(
  49. 'persistent' => false,
  50. 'host' => 'localhost',
  51. 'protocol' => 'tcp',
  52. 'port' => 80,
  53. 'timeout' => 30
  54. );
  55. /**
  56. * Configuration settings for the socket connection
  57. *
  58. * @var array
  59. * @access public
  60. */
  61. var $config = array();
  62. /**
  63. * Reference to socket connection resource
  64. *
  65. * @var resource
  66. * @access public
  67. */
  68. var $connection = null;
  69. /**
  70. * This boolean contains the current state of the CakeSocket class
  71. *
  72. * @var boolean
  73. * @access public
  74. */
  75. var $connected = false;
  76. /**
  77. * This variable contains an array with the last error number (num) and string (str)
  78. *
  79. * @var array
  80. * @access public
  81. */
  82. var $lastError = array();
  83. /**
  84. * Constructor.
  85. *
  86. * @param array $config Socket configuration, which will be merged with the base configuration
  87. */
  88. function __construct($config = array()) {
  89. parent::__construct();
  90. $this->config = array_merge($this->_baseConfig, $config);
  91. if (!is_numeric($this->config['protocol'])) {
  92. $this->config['protocol'] = getprotobyname($this->config['protocol']);
  93. }
  94. }
  95. /**
  96. * Connect the socket to the given host and port.
  97. *
  98. * @return boolean Success
  99. * @access public
  100. */
  101. function connect() {
  102. if ($this->connection != null) {
  103. $this->disconnect();
  104. }
  105. $scheme = null;
  106. if (isset($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https') {
  107. $scheme = 'ssl://';
  108. }
  109. if ($this->config['persistent'] == true) {
  110. $tmp = null;
  111. $this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
  112. } else {
  113. $this->connection = @fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
  114. }
  115. if (!empty($errNum) || !empty($errStr)) {
  116. $this->setLastError($errStr, $errNum);
  117. }
  118. return $this->connected = is_resource($this->connection);
  119. }
  120. /**
  121. * Get the host name of the current connection.
  122. *
  123. * @return string Host name
  124. * @access public
  125. */
  126. function host() {
  127. if (Validation::ip($this->config['host'])) {
  128. return gethostbyaddr($this->config['host']);
  129. } else {
  130. return gethostbyaddr($this->address());
  131. }
  132. }
  133. /**
  134. * Get the IP address of the current connection.
  135. *
  136. * @return string IP address
  137. * @access public
  138. */
  139. function address() {
  140. if (Validation::ip($this->config['host'])) {
  141. return $this->config['host'];
  142. } else {
  143. return gethostbyname($this->config['host']);
  144. }
  145. }
  146. /**
  147. * Get all IP addresses associated with the current connection.
  148. *
  149. * @return array IP addresses
  150. * @access public
  151. */
  152. function addresses() {
  153. if (Validation::ip($this->config['host'])) {
  154. return array($this->config['host']);
  155. } else {
  156. return gethostbynamel($this->config['host']);
  157. }
  158. }
  159. /**
  160. * Get the last error as a string.
  161. *
  162. * @return string Last error
  163. * @access public
  164. */
  165. function lastError() {
  166. if (!empty($this->lastError)) {
  167. return $this->lastError['num'].': '.$this->lastError['str'];
  168. } else {
  169. return null;
  170. }
  171. }
  172. /**
  173. * Set the last error.
  174. *
  175. * @param integer $errNum Error code
  176. * @param string $errStr Error string
  177. * @access public
  178. */
  179. function setLastError($errNum, $errStr) {
  180. $this->lastError = array('num' => $errNum, 'str' => $errStr);
  181. }
  182. /**
  183. * Write data to the socket.
  184. *
  185. * @param string $data The data to write to the socket
  186. * @return boolean Success
  187. * @access public
  188. */
  189. function write($data) {
  190. if (!$this->connected) {
  191. if (!$this->connect()) {
  192. return false;
  193. }
  194. }
  195. return fwrite($this->connection, $data, strlen($data));
  196. }
  197. /**
  198. * Read data from the socket. Returns false if no data is available or no connection could be
  199. * established.
  200. *
  201. * @param integer $length Optional buffer length to read; defaults to 1024
  202. * @return mixed Socket data
  203. * @access public
  204. */
  205. function read($length = 1024) {
  206. if (!$this->connected) {
  207. if (!$this->connect()) {
  208. return false;
  209. }
  210. }
  211. if (!feof($this->connection)) {
  212. return fread($this->connection, $length);
  213. } else {
  214. return false;
  215. }
  216. }
  217. /**
  218. * Abort socket operation.
  219. *
  220. * @return boolean Success
  221. * @access public
  222. */
  223. function abort() {
  224. }
  225. /**
  226. * Disconnect the socket from the current connection.
  227. *
  228. * @return boolean Success
  229. * @access public
  230. */
  231. function disconnect() {
  232. if (!is_resource($this->connection)) {
  233. $this->connected = false;
  234. return true;
  235. }
  236. $this->connected = !fclose($this->connection);
  237. if (!$this->connected) {
  238. $this->connection = null;
  239. }
  240. return !$this->connected;
  241. }
  242. /**
  243. * Destructor, used to disconnect from current connection.
  244. *
  245. * @access private
  246. */
  247. function __destruct() {
  248. $this->disconnect();
  249. }
  250. /**
  251. * Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
  252. *
  253. * @return boolean True on success
  254. * @access public
  255. */
  256. function reset($state = null) {
  257. if (empty($state)) {
  258. static $initalState = array();
  259. if (empty($initalState)) {
  260. $initalState = get_class_vars(__CLASS__);
  261. }
  262. $state = $initalState;
  263. }
  264. foreach ($state as $property => $value) {
  265. $this->{$property} = $value;
  266. }
  267. return true;
  268. }
  269. }
  270. ?>