1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12: class Request
13: {
14: 15: 16: 17: 18: 19:
20: private $scheme;
21:
22:
23: 24: 25:
26: private $method = NULL;
27:
28:
29: 30: 31: 32: 33: 34:
35: private $host;
36:
37:
38: 39: 40: 41: 42: 43:
44: private $subdomain;
45:
46:
47: 48: 49: 50: 51: 52:
53: private $base_domain;
54:
55:
56: 57: 58: 59: 60: 61:
62: private $root;
63:
64:
65: 66: 67: 68: 69: 70:
71: private $base_uri;
72:
73:
74: 75: 76: 77: 78: 79:
80: private $base_domain_uri;
81:
82:
83: 84: 85: 86: 87: 88:
89: private $uri;
90:
91:
92: 93: 94: 95: 96: 97:
98: private $resource;
99:
100:
101: 102: 103: 104: 105: 106:
107: private $resource_array;
108:
109:
110: 111: 112:
113: private $ajax;
114:
115:
116: 117: 118: 119: 120:
121: private $post;
122:
123:
124: 125: 126: 127: 128:
129: private $get;
130:
131:
132: 133: 134: 135: 136:
137: private $cookie;
138:
139:
140: 141: 142: 143: 144:
145: private $server;
146:
147:
148: 149: 150: 151: 152:
153: private $files;
154:
155:
156: 157: 158: 159: 160:
161: private $referer;
162:
163:
164: 165: 166: 167: 168:
169: private $anchor;
170:
171:
172: function __construct()
173: {
174: $this->server = $this->get_server();
175: $this->root = $this->get_root();
176: }
177:
178:
179: 180: 181: 182: 183: 184:
185: public function __get($property)
186: {
187: if ($this->$property === NULL) {
188: $method = 'get_' . $property;
189: $this->$method();
190: }
191:
192: return $this->$property;
193: }
194:
195:
196: public function get_post()
197: {
198: if ($this->post === NULL) {
199: $this->post = $this->fix_magic_quotes($_POST);
200: }
201:
202: return $this->post;
203: }
204:
205:
206: public function get_get()
207: {
208: if ($this->get === NULL) {
209: $this->get = $this->fix_magic_quotes($_GET);
210: }
211:
212: return $this->get;
213: }
214:
215:
216: public function get_cookie()
217: {
218: if ($this->cookie === NULL) {
219: $this->cookie = $this->fix_magic_quotes($_COOKIE);
220: }
221:
222: return $this->cookie;
223: }
224:
225:
226: public function get_scheme()
227: {
228: if ($this->scheme === NULL) {
229: if (empty($this->server['HTTPS']) === TRUE
230: OR $this->server['HTTPS'] === 'off') {
231:
232: $this->scheme = 'http';
233:
234: } else {
235: $this->scheme = 'https';
236: }
237: }
238:
239: return $this->scheme;
240: }
241:
242:
243: public function get_method()
244: {
245: if ($this->method === NULL) {
246: if (isset($this->server['REQUEST_METHOD']) === TRUE) {
247: $this->method = strtolower($this->server['REQUEST_METHOD']);
248:
249: } else {
250: $this->method = NULL;
251: }
252: }
253:
254: return $this->method;
255: }
256:
257:
258: public function get_host()
259: {
260: if ($this->host === NULL) {
261: if (isset($this->server['HTTP_HOST']) === TRUE) {
262: $this->host = $this->server['HTTP_HOST'];
263:
264: } else {
265: $this->host = $this->server['SERVER_NAME'];
266: }
267: }
268:
269: return $this->host;
270: }
271:
272:
273: public function get_base_domain()
274: {
275: if ($this->base_domain === NULL) {
276: $parts = explode('.', $this->get_host());
277: $parts = array_splice($parts, -2);
278:
279: $this->base_domain = implode('.', $parts);
280: }
281:
282: return $this->base_domain;
283: }
284:
285:
286: public function get_subdomain()
287: {
288: if ($this->subdomain === NULL) {
289: $parts = explode('.', $this->get_host());
290:
291: if (count($parts) > 2) {
292: $this->subdomain = array_shift($parts);
293: }
294: }
295:
296: return $this->subdomain;
297: }
298:
299:
300: public function get_root()
301: {
302: if ($this->root === NULL) {
303: $this->root = rtrim(pathinfo($this->server['SCRIPT_NAME'], PATHINFO_DIRNAME), '/');
304: }
305:
306: return $this->root;
307: }
308:
309:
310: public function get_base_uri()
311: {
312: if ($this->base_uri === NULL) {
313: $base_uri = $this->get_scheme() . '://';
314: $base_uri .= $this->get_host();
315: $base_uri .= $this->root;
316:
317: $this->base_uri = $base_uri;
318: }
319:
320: return $this->base_uri;
321: }
322:
323:
324: public function get_base_domain_uri()
325: {
326: if ($this->base_domain_uri === NULL) {
327: $base_domain_uri = $this->get_scheme() . '://';
328: $base_domain_uri .= $this->get_base_domain();
329: $base_domain_uri .= $this->root;
330:
331: $this->base_domain_uri = $base_domain_uri;
332: }
333:
334: return $this->base_domain_uri;
335: }
336:
337:
338: public function get_uri()
339: {
340: if ($this->uri === NULL) {
341: $uri = $this->get_base_uri();
342: $uri .= $this->get_resource();
343:
344: $this->uri = $uri;
345: }
346:
347: return $this->uri;
348: }
349:
350:
351: public function get_resource()
352: {
353: if ($this->resource === NULL) {
354: list($resource) = explode('?', $this->server['REQUEST_URI']);
355: $resource = preg_replace('|^' . $this->root . '|', '', $resource);
356: $resource = rtrim($resource, '/');
357:
358: if (empty($resource) === TRUE) {
359: $resource = '/';
360: }
361:
362: $this->resource = urldecode($resource);
363: }
364:
365: return $this->resource;
366: }
367:
368:
369: public function get_resource_array()
370: {
371: if ($this->resource_array === NULL) {
372: $resource = trim($this->get_resource(), '/');
373: $this->resource_array = explode('/', $resource);
374: }
375:
376: return $this->resource_array;
377: }
378:
379:
380: public function is_ajax()
381: {
382: if ($this->is_ajax === NULL) {
383: $this->get_get();
384: if ((empty($this->server['HTTP_X_REQUESTED_WITH']) === FALSE
385: AND strcasecmp($this->server['HTTP_X_REQUESTED_WITH'], 'XMLHttpRequest') === 0)
386: OR isset($this->get['ajax']) === TRUE) {
387:
388: $this->is_ajax = TRUE;
389:
390: } else {
391: $this->is_ajax = FALSE;
392: }
393: }
394:
395: return $this->is_ajax;
396: }
397:
398:
399: public function get_files()
400: {
401: if (empty($_FILES) === TRUE) {
402: $this->files = array();
403: return $this->files;
404: }
405:
406: if ($this->files === NULL) {
407: $files = array();
408: foreach ($_FILES as $scope => $scope_files) {
409: $scope_files = $this->fix_magic_quotes($scope_files);
410:
411: if (is_array($scope_files['name']) === TRUE) {
412: $files[$scope] = array();
413: $file = array();
414:
415: foreach ($scope_files['name'] as $key => $file_name) {
416: if (empty($file_name) === TRUE) {
417: continue;
418: }
419:
420: $info = pathinfo($file_name);
421:
422: $file['name'] = $file_name;
423: $file['original_basename'] = $info['filename'];
424: $file['basename'] = Strings::webalize($info['filename']);
425: $file['extension'] = strtolower($info['extension']);
426: $file['mime'] = $scope_files['type'][$key];
427: $file['tmp_name'] = $scope_files['tmp_name'][$key];
428: $file['size'] = $scope_files['size'][$key];
429: $file['error'] = $scope_files['error'][$key];
430:
431: $files[$scope][] = $file;
432: }
433:
434: } else {
435: if (empty($scope_files['name']) === TRUE) {
436: continue;
437: }
438:
439: $info = pathinfo($scope_files['name']);
440:
441: $file['name'] = $scope_files['name'];
442: $file['original_basename'] = $info['filename'];
443: $file['basename'] = Strings::webalize($info['filename']);
444: $file['extension'] = strtolower($info['extension']);
445: $file['mime'] = $scope_files['type'];
446: $file['tmp_name'] = $scope_files['tmp_name'];
447: $file['size'] = $scope_files['size'];
448: $file['error'] = $scope_files['error'];
449:
450: $files[$scope][] = $file;
451: }
452: }
453:
454: $this->files = $files;
455: }
456:
457: return $this->files;
458: }
459:
460:
461: public function get_current_method_data($name = NULL)
462: {
463: if ($this->method === 'post') {
464: $data = $this->get_post();
465:
466: } elseif ($this->method === 'get') {
467: $data = $this->get_get();
468: }
469:
470: if ($name !== NULL) {
471: return Arrays::get_value($data, $name);
472: }
473:
474: return $data;
475: }
476:
477:
478: public function get_referer()
479: {
480: if ($this->referer === NULL) {
481: $this->referer = FALSE;
482:
483: if (isset($_SERVER['HTTP_REFERER']) === TRUE) {
484: $referer = $_SERVER['HTTP_REFERER'];
485:
486: if (strpos($referer, $this->get_base_uri()) === 0) {
487: $this->referer = $referer;
488: }
489: }
490: }
491:
492: return $this->referer;
493: }
494:
495:
496: public function get_server()
497: {
498: if ($this->server === NULL) {
499: $server = $this->fix_magic_quotes($_SERVER);
500:
501: $this->server = $server;
502: }
503:
504: return $this->server;
505: }
506:
507:
508: public function get_anchor()
509: {
510: if ($this->anchor === NULL) {
511: $this->anchor = parse_url('a', PHP_URL_FRAGMENTPHP_URL_FRAGMENT);
512: }
513:
514: return $this->anchor;
515: }
516:
517:
518: 519: 520: 521: 522: 523:
524: public function fix_magic_quotes($data)
525: {
526: if (get_magic_quotes_gpc() == TRUE) {
527: if (is_array($data) === TRUE) {
528: array_map(array($this, 'fix_magic_quotes'), $data);
529:
530: } else {
531: urldecode($data);
532: stripslashes($data);
533: }
534: }
535:
536: return $data;
537: }
538: }
539: