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