1+ <?php
2+
3+ namespace Stackify \Log \Entities ;
4+
5+ class WebRequestDetail
6+ {
7+
8+ const HIDDEN_VALUE = 'X-MASKED-X ' ;
9+
10+ /**
11+ * @var string
12+ */
13+ public $ UserIPAddress ;
14+
15+ /**
16+ * @var string
17+ */
18+ public $ HttpMethod ;
19+
20+ /**
21+ * @var string
22+ */
23+ public $ RequestProtocol ;
24+
25+ /**
26+ * @var string
27+ */
28+ public $ RequestUrl ;
29+
30+ /**
31+ * @var string
32+ */
33+ public $ RequestUrlRoot ;
34+
35+ /**
36+ * @var string
37+ */
38+ public $ ReferralUrl ;
39+
40+ /**
41+ * @var array Key-value pairs
42+ */
43+ public $ Headers = array ();
44+
45+ /**
46+ * @var array Key-value pairs
47+ */
48+ public $ Cookies = array ();
49+
50+ /**
51+ * @var array Key-value pairs
52+ */
53+ public $ QueryString = array ();
54+
55+ /**
56+ * @var array Key-value pairs
57+ */
58+ public $ PostData = array ();
59+
60+ /**
61+ * @var array Key-value pairs
62+ */
63+ public $ SessionData = array ();
64+
65+ /**
66+ * @var string
67+ */
68+ public $ PostDataRaw ;
69+
70+ /**
71+ * @var string
72+ */
73+ public $ MVCAction ;
74+
75+ /**
76+ * @var string
77+ */
78+ public $ MVCController ;
79+
80+ /**
81+ * @var string
82+ */
83+ public $ MVCArea ;
84+
85+ public function __construct () {
86+ $ this ->UserIPAddress = $ this ->getIpAddress ();
87+ $ this ->HttpMethod = filter_input (INPUT_SERVER , 'REQUEST_METHOD ' );
88+ $ this ->RequestProtocol = $ this ->getProtocol ();
89+ $ this ->RequestUrl = $ this ->getRequestUrl ();
90+ $ this ->RequestUrlRoot = filter_input (INPUT_SERVER , 'SERVER_NAME ' );
91+ $ this ->ReferralUrl = filter_input (INPUT_SERVER , 'HTTP_REFERER ' );
92+ $ this ->Headers = $ this ->getHeaders ();
93+ $ this ->Cookies = isset ($ _COOKIE ) ? $ this ->getRequestMap ($ _COOKIE , true ) : array ();
94+ $ this ->QueryString = isset ($ _GET ) ? $ this ->getRequestMap ($ _GET ) : array ();
95+ $ this ->PostData = isset ($ _POST ) ? $ this ->getRequestMap ($ _POST ) : array ();
96+ $ this ->SessionData = isset ($ _SESSION ) ? $ this ->getRequestMap ($ _SESSION , true ) : array ();
97+ $ this ->PostDataRaw = file_get_contents ('php://input ' );
98+ }
99+
100+ /**
101+ * Converts $data to key-value pairs, where values are strings
102+ * @param mixed $data
103+ * @param boolean $maskValues Hide request values
104+ * @return array
105+ */
106+ public function getRequestMap ($ data , $ maskValues = false )
107+ {
108+ $ result = array ();
109+ if (is_array ($ data )) {
110+ foreach ($ data as $ key => $ value ) {
111+ $ result [$ key ] = $ maskValues
112+ ? self ::HIDDEN_VALUE
113+ : $ this ->stringify ($ value );
114+ }
115+ }
116+ return $ result ;
117+ }
118+
119+ private function getIpAddress () {
120+ $ keys = array (
121+ 'HTTP_CLIENT_IP ' ,
122+ 'HTTP_X_FORWARDED_FOR ' ,
123+ 'HTTP_X_FORWARDED ' ,
124+ 'HTTP_X_CLUSTER_CLIENT_IP ' ,
125+ 'HTTP_FORWARDED_FOR ' ,
126+ 'HTTP_FORWARDED ' ,
127+ 'REMOTE_ADDR ' ,
128+ );
129+ foreach ($ keys as $ key ) {
130+ $ ip = filter_input (INPUT_SERVER , $ key );
131+ if (null !== $ ip && false !== filter_var ($ ip , FILTER_VALIDATE_IP )) {
132+ return $ ip ;
133+ }
134+ }
135+ return null ;
136+ }
137+
138+ private function getProtocol ()
139+ {
140+ $ protocol = null ;
141+ if ('cli ' === php_sapi_name ()) {
142+ $ protocol = 'CLI ' ;
143+ } else {
144+ $ protocol = filter_input (INPUT_SERVER , 'SERVER_PROTOCOL ' );
145+ }
146+ return $ protocol ;
147+ }
148+
149+ private function getRequestUrl ()
150+ {
151+ $ https = filter_input (INPUT_SERVER , 'HTTPS ' );
152+ $ ssl = null !== $ https && 'off ' !== $ https ;
153+ $ protocol = $ ssl ? 'https ' : 'http ' ;
154+ list ($ url ,) = explode ('? ' , filter_input (INPUT_SERVER , 'REQUEST_URI ' ));
155+ $ serverName = filter_input (INPUT_SERVER , 'SERVER_NAME ' );
156+ if ($ serverName && $ url ) {
157+ return "$ protocol:// $ serverName " . $ url ;
158+ }
159+ return null ;
160+ }
161+
162+ private function getHeaders ()
163+ {
164+ $ headers = array ();
165+ if (function_exists ('getallheaders ' )) {
166+ $ headers = getallheaders ();
167+ if (false === $ headers ) {
168+ // getallheaders() returns false in case of error
169+ $ headers = array ();
170+ }
171+ }
172+ foreach ($ headers as $ name => $ value ) {
173+ if ('cookie ' === strtolower ($ name )) {
174+ $ headers [$ name ] = self ::HIDDEN_VALUE ;
175+ }
176+ }
177+ return $ headers ;
178+ }
179+
180+ /**
181+ * Converts any PHP type to string
182+ * @param mixed $value
183+ * @return string
184+ */
185+ private function stringify ($ value )
186+ {
187+ $ string = '' ;
188+ if (is_scalar ($ value )) {
189+ // integer, float, string, boolean
190+ $ string = (string )$ value ;
191+ } elseif (is_resource ($ value )) {
192+ // resource
193+ $ string = '[resource] ' ;
194+ } else {
195+ // array, object, null, callable
196+ $ string = json_encode ($ value );
197+ }
198+ return $ string ;
199+ }
200+
201+ }
0 commit comments