1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20: App::import('Core', 'Validation');
21:
22: 23: 24: 25: 26: 27: 28: 29:
30: class CakeSocket extends Object {
31:
32: 33: 34: 35: 36: 37:
38: var $description = 'Remote DataSource Network Socket Interface';
39:
40: 41: 42: 43: 44: 45:
46: var $_baseConfig = array(
47: 'persistent' => false,
48: 'host' => 'localhost',
49: 'protocol' => 'tcp',
50: 'port' => 80,
51: 'timeout' => 30
52: );
53:
54: 55: 56: 57: 58: 59:
60: var $config = array();
61:
62: 63: 64: 65: 66: 67:
68: var $connection = null;
69:
70: 71: 72: 73: 74: 75:
76: var $connected = false;
77:
78: 79: 80: 81: 82: 83:
84: var $lastError = array();
85:
86: 87: 88: 89: 90: 91:
92: function __construct($config = array()) {
93: parent::__construct();
94:
95: $this->config = array_merge($this->_baseConfig, $config);
96: if (!is_numeric($this->config['protocol'])) {
97: $this->config['protocol'] = getprotobyname($this->config['protocol']);
98: }
99: }
100:
101: 102: 103: 104: 105: 106:
107: function connect() {
108: if ($this->connection != null) {
109: $this->disconnect();
110: }
111:
112: $scheme = null;
113: if (isset($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https') {
114: $scheme = 'ssl://';
115: }
116:
117: if ($this->config['persistent'] == true) {
118: $tmp = null;
119: $this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
120: } else {
121: $this->connection = @fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
122: }
123:
124: if (!empty($errNum) || !empty($errStr)) {
125: $this->setLastError($errNum, $errStr);
126: }
127:
128: $this->connected = is_resource($this->connection);
129: if ($this->connected) {
130: stream_set_timeout($this->connection, $this->config['timeout']);
131: }
132: return $this->connected;
133: }
134:
135: 136: 137: 138: 139: 140:
141: function host() {
142: if (Validation::ip($this->config['host'])) {
143: return gethostbyaddr($this->config['host']);
144: } else {
145: return gethostbyaddr($this->address());
146: }
147: }
148:
149: 150: 151: 152: 153: 154:
155: function address() {
156: if (Validation::ip($this->config['host'])) {
157: return $this->config['host'];
158: } else {
159: return gethostbyname($this->config['host']);
160: }
161: }
162:
163: 164: 165: 166: 167: 168:
169: function addresses() {
170: if (Validation::ip($this->config['host'])) {
171: return array($this->config['host']);
172: } else {
173: return gethostbynamel($this->config['host']);
174: }
175: }
176:
177: 178: 179: 180: 181: 182:
183: function lastError() {
184: if (!empty($this->lastError)) {
185: return $this->lastError['num'] . ': ' . $this->lastError['str'];
186: } else {
187: return null;
188: }
189: }
190:
191: 192: 193: 194: 195: 196: 197:
198: function setLastError($errNum, $errStr) {
199: $this->lastError = array('num' => $errNum, 'str' => $errStr);
200: }
201:
202: 203: 204: 205: 206: 207: 208:
209: function write($data) {
210: if (!$this->connected) {
211: if (!$this->connect()) {
212: return false;
213: }
214: }
215: $totalBytes = strlen($data);
216: for ($written = 0, $rv = 0; $written < $totalBytes; $written += $rv) {
217: $rv = fwrite($this->connection, substr($data, $written));
218: if ($rv === false || $rv === 0) {
219: return $written;
220: }
221: }
222: return $written;
223: }
224:
225: 226: 227: 228: 229: 230: 231: 232:
233: function read($length = 1024) {
234: if (!$this->connected) {
235: if (!$this->connect()) {
236: return false;
237: }
238: }
239:
240: if (!feof($this->connection)) {
241: $buffer = fread($this->connection, $length);
242: $info = stream_get_meta_data($this->connection);
243: if ($info['timed_out']) {
244: $this->setLastError(E_WARNING, __('Connection timed out', true));
245: return false;
246: }
247: return $buffer;
248: } else {
249: return false;
250: }
251: }
252:
253: 254: 255: 256: 257: 258:
259: function abort() {
260: }
261:
262: 263: 264: 265: 266: 267:
268: function disconnect() {
269: if (!is_resource($this->connection)) {
270: $this->connected = false;
271: return true;
272: }
273: $this->connected = !fclose($this->connection);
274:
275: if (!$this->connected) {
276: $this->connection = null;
277: }
278: return !$this->connected;
279: }
280:
281: 282: 283: 284: 285:
286: function __destruct() {
287: $this->disconnect();
288: }
289:
290: 291: 292: 293: 294: 295:
296: function reset($state = null) {
297: if (empty($state)) {
298: static $initalState = array();
299: if (empty($initalState)) {
300: $initalState = get_class_vars(__CLASS__);
301: }
302: $state = $initalState;
303: }
304:
305: foreach ($state as $property => $value) {
306: $this->{$property} = $value;
307: }
308: return true;
309: }
310: }
311: