66 */
77abstract class HttpRequestBase {
88
9+ /**
10+ * Gets or sets information about the URL of the current request.
11+ *
12+ * @var array
13+ */
14+ protected $ url = null ;
15+
916 /**
1017 * URI of the current request.
1118 *
1219 * @var string
1320 */
14- protected $ requestUri ;
21+ protected $ rawUrl = null ;
1522
1623 /**
1724 * Query string of the current request.
1825 *
1926 * @var QueryString
2027 */
21- protected $ queryString ;
28+ protected $ queryString = null ;
2229
2330 /**
2431 * Server variables.
@@ -88,34 +95,56 @@ protected function __construct(
8895 $ this ->post = $ post ;
8996 $ this ->files = $ files ;
9097
91- $ this ->requestUri = $ serverVariables ['REQUEST_URI ' ];
92-
93- $ this ->queryString = new QueryString ();
94- $ queryString = array ();
98+ $ this ->rawUrl = $ this ->serverVariables ['REQUEST_URI ' ];
99+ }
95100
96- if (($ qsIndex = strpos ($ this ->requestUri , '? ' )) !== false ) {
97- parse_str (substr ($ this ->requestUri , $ qsIndex + 1 ), $ queryString );
98- }
99- else {
100- if (!empty ($ serverVariables ['QUERY_STRING ' ])) {
101- parse_str ($ serverVariables ['QUERY_STRING ' ], $ queryString );
102- }
101+ /**
102+ * Returns URL components.
103+ *
104+ * Potential keys within this array are:
105+ * - scheme - e.g. http
106+ * - host
107+ * - port
108+ * - user
109+ * - pass
110+ * - path
111+ * - query - after the question mark ?
112+ * - fragment - after the hashmark #
113+ *
114+ * @return array
115+ */
116+ public function url () {
117+ // TODO: class for url
118+ if ($ this ->url === null ) {
119+ $ this ->url = parse_url (
120+ 'http ' .
121+ (isset ($ serverVariables ['HTTPS ' ]) ? 's ' : '' ) .
122+ ':// ' .
123+ $ serverVariables ['HTTP_HOST ' ] .
124+ $ serverVariables ['REQUEST_URI ' ]
125+ );
103126 }
104127
105- if (!empty ($ queryString )) {
106- foreach ($ queryString as $ key => $ value ) {
107- $ this ->queryString [$ key ] = $ value ;
108- }
109- }
128+ return $ this ->url ;
129+ }
130+
131+ /**
132+ * Gets the raw URL of the current request.
133+ * For example: /home/example
134+ *
135+ * @return string
136+ */
137+ public function rawUrl () {
138+ return $ this ->rawUrl ;
110139 }
111140
112141 /**
113- * Returns request URI .
142+ * Gets information about the URL of the client's previous request that linked to the current URL .
114143 *
115144 * @return string
116145 */
117- public function requestUri () {
118- return $ this ->requestUri ;
146+ public function urlReferrer () {
147+ return $ this ->serverVariables [ ' HTTP_REFERER ' ] ;
119148 }
120149
121150 /**
@@ -126,9 +155,38 @@ public function requestUri() {
126155 * @return QueryString|string
127156 */
128157 public function queryString ($ key = null ) {
158+ if ($ this ->queryString === null ) {
159+ $ this ->queryString = new QueryString ();
160+ $ queryString = array ();
161+
162+ if (($ qsIndex = strpos ($ this ->rawUrl , '? ' )) !== false ) {
163+ parse_str (substr ($ this ->rawUrl , $ qsIndex + 1 ), $ queryString );
164+ }
165+ else {
166+ if (!empty ($ serverVariables ['QUERY_STRING ' ])) {
167+ parse_str ($ serverVariables ['QUERY_STRING ' ], $ queryString );
168+ }
169+ }
170+
171+ if (!empty ($ queryString )) {
172+ foreach ($ queryString as $ key => $ value ) {
173+ $ this ->queryString [$ key ] = $ value ;
174+ }
175+ }
176+ }
177+
129178 return $ this ->getSingleKeyOrAll ($ this ->queryString , $ key );
130179 }
131180
181+ /**
182+ * Gets the document root directory under which the current script is executing, as defined in the server's configuration file.
183+ *
184+ * @return string
185+ */
186+ public function documentRoot () {
187+ return $ this ->serverVariables ['DOCUMENT_ROOT ' ];
188+ }
189+
132190 /**
133191 * Returns server variables.
134192 *
@@ -240,6 +298,37 @@ public function userHostAddress() {
240298 return $ this ->serverVariables ['REMOTE_ADDR ' ];
241299 }
242300
301+ /**
302+ * Gets a sorted string array of client language preferences.
303+ *
304+ * @return array
305+ */
306+ public function userLanguages () {
307+ if (isset ($ this ->serverVariables ['HTTP_ACCEPT_LANGUAGE ' ])) {
308+ preg_match_all (
309+ '/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i ' ,
310+ $ this ->serverVariables ['HTTP_ACCEPT_LANGUAGE ' ],
311+ $ matches
312+ );
313+
314+ if (count ($ matches [1 ])) {
315+ $ result = array_combine ($ matches [1 ], $ matches [4 ]);
316+
317+ array_walk ($ result , function ($ lang , $ amount ) {
318+ if ($ amount === '' ) {
319+ $ langs [$ lang ] = 1 ;
320+ }
321+ });
322+
323+ arsort ($ result , \SORT_NUMERIC );
324+
325+ return $ result ;
326+ }
327+ }
328+
329+ return array ();
330+ }
331+
243332 /**
244333 * Returns Content-Type of the request or empty string.
245334 *
0 commit comments