22
33namespace Modulus \Http ;
44
5- use Modulus \Request \Server ;
6- use Modulus \Request \Cookies ;
7- use Modulus \Request \Headers ;
85use Modulus \Http \Request \Base ;
96use Modulus \Http \Request \HasInput ;
107use Modulus \Http \Request \HasRequest ;
@@ -15,98 +12,4 @@ final class Request extends Base
1512 use HasInput;
1613 use HasRequest;
1714 use HasValidation;
18-
19- /**
20- * Construct
21- *
22- * @param array $data
23- */
24- public function __construct (array $ data = [])
25- {
26- $ this ->data = $ data ;
27-
28- if (
29- isset (getallheaders ()['Content-Type ' ]) &&
30- (
31- str_contains (strtolower (getallheaders ()['Content-Type ' ]), 'json ' ) ||
32- str_contains (strtolower (getallheaders ()['Content-Type ' ]), 'javascript ' )
33- )
34- ) {
35- $ json = json_decode (file_get_contents ("php://input " ), true );
36- $ this ->data = array_merge ($ this ->data , is_array ($ json ) ? $ json : []);
37- }
38-
39- if ($ this ->data !== []) {
40- foreach ($ this ->data as $ key => $ value ) {
41- if (!in_array ($ key , $ this ->protected )) $ this ->{$ key } = $ value ;
42- }
43- }
44-
45- $ files = array_filter ($ data , function ($ file ) {
46- if (isset ($ file ['type ' ]) && isset ($ file ['name ' ]) && isset ($ file ['size ' ])) return $ file ;
47- });
48-
49- $ this ->files = $ files ;
50- $ this ->cookies = new Cookies ();
51- $ this ->headers = new Headers ();
52- $ this ->server = new Server ();
53- $ this ->path = (str_contains ($ _SERVER ['REQUEST_URI ' ], '? ' )) ? explode ('? ' , ($ _SERVER ['REQUEST_URI ' ]))[0 ] : $ _SERVER ['REQUEST_URI ' ];
54- $ this ->url = $ _SERVER ['REQUEST_URI ' ];
55- $ this ->method = $ _SERVER ['REQUEST_METHOD ' ];
56- $ this ->isAjax = (isset ($ _SERVER ['HTTP_X_REQUESTED_WITH ' ]) &&
57- ($ _SERVER ['HTTP_X_REQUESTED_WITH ' ] == 'XMLHttpRequest ' ));
58-
59- if ($ this ->rules == [] && count (is_array ($ this ->rules ()) ? $ this ->rules () : []) > 0 ) {
60- $ this ->rules = $ this ->rules ();
61- }
62-
63- /**
64- * Run the validate method if request has rules, and
65- * the csrf token is present.
66- */
67- if (
68- count (is_array ($ this ->rules ) ? $ this ->rules : []) > 0 &&
69- ($ this ->has ('csrf_token ' ) || $ this ->headers ->has ('X-CSRF-TOKEN ' ))
70- ) {
71- $ this ->validate ();
72- }
73- }
74-
75- /**
76- * Get client ip address
77- *
78- * @return void
79- */
80- public function ip ($ return_type = null , $ ip_addresses = [])
81- {
82- $ ip_elements = array (
83- 'HTTP_X_FORWARDED_FOR ' , 'HTTP_FORWARDED_FOR ' ,
84- 'HTTP_X_FORWARDED ' , 'HTTP_FORWARDED ' ,
85- 'HTTP_X_CLUSTER_CLIENT_IP ' , 'HTTP_CLUSTER_CLIENT_IP ' ,
86- 'HTTP_X_CLIENT_IP ' , 'HTTP_CLIENT_IP ' ,
87- 'REMOTE_ADDR '
88- );
89-
90- foreach ( $ ip_elements as $ element ) {
91- if (isset ($ _SERVER [$ element ])) {
92- if ( !is_string ($ _SERVER [$ element ]) ) {
93- // Log the value somehow, to improve the script!
94- continue ;
95- }
96- $ address_list = explode (', ' , $ _SERVER [$ element ]);
97- $ address_list = array_map ('trim ' , $ address_list );
98- // Not using array_merge in order to preserve order
99- foreach ( $ address_list as $ x ) {
100- $ ip_addresses [] = $ x ;
101- }
102- }
103- }
104- if (count ($ ip_addresses ) == 0 ) {
105- return false ;
106- } elseif ($ return_type === 'array ' ) {
107- return $ ip_addresses ;
108- } elseif ($ return_type === 'single ' || $ return_type === null ) {
109- return $ ip_addresses [0 ];
110- }
111- }
11215}
0 commit comments