PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/cake_socket.php

https://github.com/cgajardo/repositorium
PHP | 304 lines | 132 code | 29 blank | 143 comment | 31 complexity | 8db2fb8c95260e645b7e901ae606c74b MD5 | raw file
  1. <?php
  2. /**
  3. * Cake Socket connection class.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake
  16. * @subpackage cake.cake.libs
  17. * @since CakePHP(tm) v 1.2.0
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::import('Core', 'Validation');
  21. /**
  22. * Cake network socket connection class.
  23. *
  24. * Core base class for network communication.
  25. *
  26. * @package cake
  27. * @subpackage cake.cake.libs
  28. */
  29. class CakeSocket extends Object {
  30. /**
  31. * Object description
  32. *
  33. * @var string
  34. * @access public
  35. */
  36. var $description = 'Remote DataSource Network Socket Interface';
  37. /**
  38. * Base configuration settings for the socket connection
  39. *
  40. * @var array
  41. * @access protected
  42. */
  43. var $_baseConfig = array(
  44. 'persistent' => false,
  45. 'host' => 'localhost',
  46. 'protocol' => 'tcp',
  47. 'port' => 80,
  48. 'timeout' => 30
  49. );
  50. /**
  51. * Configuration settings for the socket connection
  52. *
  53. * @var array
  54. * @access public
  55. */
  56. var $config = array();
  57. /**
  58. * Reference to socket connection resource
  59. *
  60. * @var resource
  61. * @access public
  62. */
  63. var $connection = null;
  64. /**
  65. * This boolean contains the current state of the CakeSocket class
  66. *
  67. * @var boolean
  68. * @access public
  69. */
  70. var $connected = false;
  71. /**
  72. * This variable contains an array with the last error number (num) and string (str)
  73. *
  74. * @var array
  75. * @access public
  76. */
  77. var $lastError = array();
  78. /**
  79. * Constructor.
  80. *
  81. * @param array $config Socket configuration, which will be merged with the base configuration
  82. * @see CakeSocket::$_baseConfig
  83. */
  84. function __construct($config = array()) {
  85. parent::__construct();
  86. $this->config = array_merge($this->_baseConfig, $config);
  87. if (!is_numeric($this->config['protocol'])) {
  88. $this->config['protocol'] = getprotobyname($this->config['protocol']);
  89. }
  90. }
  91. /**
  92. * Connect the socket to the given host and port.
  93. *
  94. * @return boolean Success
  95. * @access public
  96. */
  97. function connect() {
  98. if ($this->connection != null) {
  99. $this->disconnect();
  100. }
  101. $scheme = null;
  102. if (isset($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https') {
  103. $scheme = 'ssl://';
  104. }
  105. if ($this->config['persistent'] == true) {
  106. $tmp = null;
  107. $this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
  108. } else {
  109. $this->connection = @fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
  110. }
  111. if (!empty($errNum) || !empty($errStr)) {
  112. $this->setLastError($errNum, $errStr);
  113. }
  114. $this->connected = is_resource($this->connection);
  115. if ($this->connected) {
  116. stream_set_timeout($this->connection, $this->config['timeout']);
  117. }
  118. return $this->connected;
  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. $buffer = fread($this->connection, $length);
  213. $info = stream_get_meta_data($this->connection);
  214. if ($info['timed_out']) {
  215. $this->setLastError(E_WARNING, __('Connection timed out', true));
  216. return false;
  217. }
  218. return $buffer;
  219. } else {
  220. return false;
  221. }
  222. }
  223. /**
  224. * Abort socket operation.
  225. *
  226. * @return boolean Success
  227. * @access public
  228. */
  229. function abort() {
  230. }
  231. /**
  232. * Disconnect the socket from the current connection.
  233. *
  234. * @return boolean Success
  235. * @access public
  236. */
  237. function disconnect() {
  238. if (!is_resource($this->connection)) {
  239. $this->connected = false;
  240. return true;
  241. }
  242. $this->connected = !fclose($this->connection);
  243. if (!$this->connected) {
  244. $this->connection = null;
  245. }
  246. return !$this->connected;
  247. }
  248. /**
  249. * Destructor, used to disconnect from current connection.
  250. *
  251. * @access private
  252. */
  253. function __destruct() {
  254. $this->disconnect();
  255. }
  256. /**
  257. * Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
  258. *
  259. * @return boolean True on success
  260. * @access public
  261. */
  262. function reset($state = null) {
  263. if (empty($state)) {
  264. static $initalState = array();
  265. if (empty($initalState)) {
  266. $initalState = get_class_vars(__CLASS__);
  267. }
  268. $state = $initalState;
  269. }
  270. foreach ($state as $property => $value) {
  271. $this->{$property} = $value;
  272. }
  273. return true;
  274. }
  275. }