1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22:
23: class cHttpRequestSocket extends cHttpRequest {
24:
25: 26: 27: 28: 29:
30: protected $postArray;
31:
32: 33: 34: 35: 36:
37: protected $getArray;
38:
39: 40: 41: 42: 43:
44: protected ;
45:
46: 47: 48: 49: 50:
51: protected $url;
52:
53: 54: 55: 56: 57:
58: protected $boundary;
59:
60: 61: 62: 63: 64:
65: protected ;
66:
67: 68: 69: 70: 71:
72: protected $body;
73:
74: 75: 76: 77: 78: 79: 80: 81:
82: public function __construct($url = '') {
83: $this->url = $url;
84: }
85:
86: 87: 88: 89: 90: 91: 92: 93:
94: public function setURL($url) {
95: $this->url = $url;
96:
97: return $this;
98: }
99:
100: 101: 102: 103: 104: 105: 106: 107:
108: public function setGetParams($array) {
109: $this->getArray = $array;
110:
111: return $this;
112: }
113:
114: 115: 116: 117: 118: 119: 120: 121:
122: public function setPostParams($array) {
123: $this->postArray = $array;
124:
125: return $this;
126: }
127:
128: 129: 130: 131: 132: 133: 134: 135:
136: public function ($array) {
137: $this->headerArray = $array;
138:
139: return $this;
140: }
141:
142: 143: 144:
145: protected function () {
146: $this->header = '';
147: if (!is_array($this->headerArray)) {
148: return;
149: }
150: foreach ($this->headerArray as $key => $value) {
151: $headerString = '';
152: if (is_array($value)) {
153: $headerString .= $value[0] . ': ' . $value[1];
154: } else {
155: $headerString .= $key . ': ' . $value;
156: }
157: $this->header .= $headerString . "\r\n";
158: }
159: }
160:
161: 162: 163:
164: protected function prepareGetRequest() {
165: if (is_array($this->getArray)) {
166: if (!cString::contains($this->url, '?')) {
167: $this->url .= '?';
168: } else {
169: $this->url .= '&';
170: }
171: foreach ($this->getArray as $key => $value) {
172: $this->url .= urlencode($key) . '=' . urlencode($value) . '&';
173: }
174: $this->url = cString::getPartOfString($this->url, 0, cString::getStringLength($this->url) - 1);
175: }
176: }
177:
178: 179: 180:
181: protected function preparePostRequest() {
182: $this->boundary = md5(time()) . md5(time() * rand());
183: $this->headerArray['Content-Type'] = 'multipart/form-data; boundary=' . $this->boundary;
184: $this->boundary = '--' . $this->boundary;
185:
186: $this->body = $this->boundary . "\r\n";
187: foreach ($this->postArray as $key => $value) {
188: $this->body .= 'Content-Disposition: form-data; name="' . $key . "\"\r\n\r\n";
189: $this->body .= $value . "\r\n";
190: $this->body .= $this->boundary . "\r\n";
191: }
192: $this->headerArray['Content-Length'] = cString::getStringLength($this->body);
193: }
194:
195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205:
206: protected function sendRequest($return, $method, $returnHeaders = false) {
207: if (!(cString::findFirstPos($this->url, 'http') === 0)) {
208: $this->url = 'http://' . $this->url;
209: }
210:
211: $urlInfo = @parse_url($this->url);
212: $scheme = '';
213: if ($urlInfo['port'] == '') {
214: if ($urlInfo['scheme'] == 'https') {
215: $urlInfo['port'] = 443;
216: $scheme = 'ssl://';
217: } else {
218: $urlInfo['port'] = 80;
219: }
220: }
221:
222: $this->headerArray['Host'] = ($this->headerArray['Host'] != '') ? $this->headerArray['Host'] : $urlInfo['host'];
223: $this->headerArray['Connection'] = ($this->headerArray['Connection'] != '') ? $this->headerArray['Host'] : 'close';
224: $this->headerArray['Accept'] = ($this->headerArray['Accept'] != '') ? $this->headerArray['Host'] : '*/*';
225:
226: $this->prepareHeaders();
227:
228: $handle = @fsockopen($scheme . $urlInfo['host'], $urlInfo['port']);
229: if (!$handle) {
230: return false;
231: }
232:
233: $request = $method . ' ';
234: $request .= $urlInfo['path'] . '?' . $urlInfo['query'] . ' HTTP/1.1' . "\r\n";
235: $request .= $this->header . "\r\n";
236: $request .= $this->body;
237:
238: fwrite($handle, $request);
239:
240: $ret = '';
241: while (!feof($handle)) {
242: $ret .= fgets($handle);
243: }
244:
245: fclose($handle);
246:
247: if ($return) {
248: if (!$returnHeaders) {
249: $ret = cString::getPartOfString(cString::strstr($ret, "\r\n\r\n"), cString::getStringLength("\r\n\r\n"));
250: }
251: return $ret;
252: } else {
253: return cString::findFirstPos(cString::strstr($ret, '\r\n', true), '200') !== false;
254: }
255: }
256:
257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267:
268: public function postRequest($return = true, $returnHeaders = false) {
269: $this->preparePostRequest();
270:
271: return $this->sendRequest($return, 'POST', $returnHeaders);
272: }
273:
274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284:
285: public function getRequest($return = true, $returnHeaders = false) {
286: $this->prepareGetRequest();
287:
288: return $this->sendRequest($return, 'GET', $returnHeaders);
289: }
290:
291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301:
302: public function request($return = true, $returnHeaders = false) {
303: $this->prepareGetRequest();
304: $this->preparePostRequest();
305:
306: return $this->sendRequest($return, 'POST', $returnHeaders);
307: }
308: }
309: