00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 App::import('Core', 'Validation');
00026
00027
00028
00029
00030
00031
00032
00033
00034 class CakeSocket extends Object {
00035
00036
00037
00038
00039
00040
00041 var $description = 'Remote DataSource Network Socket Interface';
00042
00043
00044
00045
00046
00047
00048 var $_baseConfig = array(
00049 'persistent' => false,
00050 'host' => 'localhost',
00051 'protocol' => 'tcp',
00052 'port' => 80,
00053 'timeout' => 30
00054 );
00055
00056
00057
00058
00059
00060
00061 var $config = array();
00062
00063
00064
00065
00066
00067
00068 var $connection = null;
00069
00070
00071
00072
00073
00074
00075 var $connected = false;
00076
00077
00078
00079
00080
00081
00082 var $lastError = array();
00083
00084
00085
00086
00087
00088 function __construct($config = array()) {
00089 parent::__construct();
00090
00091 $this->config = array_merge($this->_baseConfig, $config);
00092 if (!is_numeric($this->config['protocol'])) {
00093 $this->config['protocol'] = getprotobyname($this->config['protocol']);
00094 }
00095 }
00096
00097
00098
00099
00100
00101
00102 function connect() {
00103 if ($this->connection != null) {
00104 $this->disconnect();
00105 }
00106
00107 $scheme = null;
00108 if (isset($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https') {
00109 $scheme = 'ssl:
00110 }
00111
00112 if ($this->config['persistent'] == true) {
00113 $tmp = null;
00114 $this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
00115 } else {
00116 $this->connection = @fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
00117 }
00118
00119 if (!empty($errNum) || !empty($errStr)) {
00120 $this->setLastError($errNum, $errStr);
00121 }
00122
00123 $this->connected = is_resource($this->connection);
00124 if ($this->connected) {
00125 stream_set_timeout($this->connection, $this->config['timeout']);
00126 }
00127 return $this->connected;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136 function host() {
00137 if (Validation::ip($this->config['host'])) {
00138 return gethostbyaddr($this->config['host']);
00139 } else {
00140 return gethostbyaddr($this->address());
00141 }
00142 }
00143
00144
00145
00146
00147
00148
00149 function address() {
00150 if (Validation::ip($this->config['host'])) {
00151 return $this->config['host'];
00152 } else {
00153 return gethostbyname($this->config['host']);
00154 }
00155 }
00156
00157
00158
00159
00160
00161
00162 function addresses() {
00163 if (Validation::ip($this->config['host'])) {
00164 return array($this->config['host']);
00165 } else {
00166 return gethostbynamel($this->config['host']);
00167 }
00168 }
00169
00170
00171
00172
00173
00174
00175 function lastError() {
00176 if (!empty($this->lastError)) {
00177 return $this->lastError['num'].': '.$this->lastError['str'];
00178 } else {
00179 return null;
00180 }
00181 }
00182
00183
00184
00185
00186
00187
00188
00189 function setLastError($errNum, $errStr) {
00190 $this->lastError = array('num' => $errNum, 'str' => $errStr);
00191 }
00192
00193
00194
00195
00196
00197
00198
00199 function write($data) {
00200 if (!$this->connected) {
00201 if (!$this->connect()) {
00202 return false;
00203 }
00204 }
00205
00206 return fwrite($this->connection, $data, strlen($data));
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 function read($length = 1024) {
00218 if (!$this->connected) {
00219 if (!$this->connect()) {
00220 return false;
00221 }
00222 }
00223
00224 if (!feof($this->connection)) {
00225 $buffer = fread($this->connection, $length);
00226 $info = stream_get_meta_data($this->connection);
00227 if ($info['timed_out']) {
00228 $this->setLastError(E_WARNING, __('Connection timed out', true));
00229 return false;
00230 }
00231 return $buffer;
00232 } else {
00233 return false;
00234 }
00235 }
00236
00237
00238
00239
00240
00241
00242 function abort() {
00243 }
00244
00245
00246
00247
00248
00249
00250 function disconnect() {
00251 if (!is_resource($this->connection)) {
00252 $this->connected = false;
00253 return true;
00254 }
00255 $this->connected = !fclose($this->connection);
00256
00257 if (!$this->connected) {
00258 $this->connection = null;
00259 }
00260 return !$this->connected;
00261 }
00262
00263
00264
00265
00266
00267 function __destruct() {
00268 $this->disconnect();
00269 }
00270
00271
00272
00273
00274
00275
00276 function reset($state = null) {
00277 if (empty($state)) {
00278 static $initalState = array();
00279 if (empty($initalState)) {
00280 $initalState = get_class_vars(__CLASS__);
00281 }
00282 $state = $initalState;
00283 }
00284
00285 foreach ($state as $property => $value) {
00286 $this->{$property} = $value;
00287 }
00288 return true;
00289 }
00290 }
00291 ?>