1+ <?php
2+ namespace phpcore \core ;
3+
4+ use Exception ;
5+
6+ class Request {
7+ public $ IsApi = false ;
8+ public $ Origin ;
9+ public $ Method ;
10+ public $ Controller ;
11+ public $ Arguments = array ();
12+ public $ Headers = array ();
13+
14+ public function __construct () {
15+ try {
16+ // Requests from the same server don't have a HTTP_ORIGIN header
17+ if (array_key_exists ('HTTP_ORIGIN ' , $ _SERVER ))
18+ $ this ->Origin = $ _SERVER ['HTTP_ORIGIN ' ];
19+ else
20+ $ this ->Origin = $ _SERVER ['SERVER_NAME ' ];
21+ $ this ->Method = $ _SERVER ['REQUEST_METHOD ' ];
22+ if (isset ($ _REQUEST ['q ' ])) {
23+ $ this ->Arguments = explode ('/ ' , ltrim (rtrim ($ _REQUEST ['q ' ], '/ ' ), '/ ' ));
24+ }
25+ $ this ->Controller = array_shift ($ this ->Arguments );
26+ if (preg_match ('/^api$/i ' , $ this ->Controller )) {
27+ $ this ->IsApi = true ;
28+ $ this ->Controller = array_shift ($ this ->Arguments );
29+ }
30+ $ this ->Headers = $ this ->GetRequestHeaders ();
31+ }
32+ catch (Exception $ e ) {
33+ throw $ e ;
34+ }
35+ }
36+
37+ public function getRequestHeaders () {
38+ try {
39+ if (!function_exists ('apache_request_headers ' )) {
40+ foreach ($ _SERVER as $ key => $ value ) {
41+ if (preg_match ('/^HTTP_/ ' , $ key )) {
42+ $ key = str_replace (' ' , '- ' , ucwords (strtolower (str_replace ('_ ' , ' ' , substr ($ key , 5 )))));
43+ $ headers [$ key ] = $ value ;
44+ } else if ($ key == "CONTENT_TYPE " ) {
45+ $ headers ["Content-Type " ] = $ value ;
46+ } else if ($ key == "CONTENT_LENGTH " ) {
47+ $ headers ["Content-Length " ] = $ value ;
48+ }
49+ }
50+ }
51+ else {
52+ $ headers = apache_request_headers ();
53+ }
54+ return $ headers ;
55+ }
56+ catch (Exception $ e ) {
57+ throw $ e ;
58+ }
59+ }
60+ }
61+ ?>
0 commit comments