1 <?php
  2 
  3 /**
  4  * Class for getting the superglobals.
  5  *
  6  * @package redaxo\core
  7  */
  8 class rex_request
  9 {
 10     /**
 11      * Returns the variable $varname of $_GET and casts the value.
 12      *
 13      * @param string $varname Variable name
 14      * @param string $vartype Variable type
 15      * @param mixed  $default Default value
 16      *
 17      * @return mixed
 18      */
 19     public static function get($varname, $vartype = '', $default = '')
 20     {
 21         return self::arrayKeyCast($_GET, $varname, $vartype, $default);
 22     }
 23 
 24     /**
 25      * Returns the variable $varname of $_POST and casts the value.
 26      *
 27      * @param string $varname Variable name
 28      * @param string $vartype Variable type
 29      * @param mixed  $default Default value
 30      *
 31      * @return mixed
 32      */
 33     public static function post($varname, $vartype = '', $default = '')
 34     {
 35         return self::arrayKeyCast($_POST, $varname, $vartype, $default);
 36     }
 37 
 38     /**
 39      * Returns the variable $varname of $_REQUEST and casts the value.
 40      *
 41      * @param string $varname Variable name
 42      * @param string $vartype Variable type
 43      * @param mixed  $default Default value
 44      *
 45      * @return mixed
 46      */
 47     public static function request($varname, $vartype = '', $default = '')
 48     {
 49         return self::arrayKeyCast($_REQUEST, $varname, $vartype, $default);
 50     }
 51 
 52     /**
 53      * Returns the variable $varname of $_SERVER and casts the value.
 54      *
 55      * @param string $varname Variable name
 56      * @param string $vartype Variable type
 57      * @param mixed  $default Default value
 58      *
 59      * @return mixed
 60      */
 61     public static function server($varname, $vartype = '', $default = '')
 62     {
 63         return self::arrayKeyCast($_SERVER, $varname, $vartype, $default);
 64     }
 65 
 66     /**
 67      * Returns the variable $varname of $_SESSION and casts the value.
 68      *
 69      * @param string $varname Variable name
 70      * @param string $vartype Variable type
 71      * @param mixed  $default Default value
 72      *
 73      * @throws rex_exception
 74      *
 75      * @return mixed
 76      */
 77     public static function session($varname, $vartype = '', $default = '')
 78     {
 79         if (PHP_SESSION_ACTIVE != session_status()) {
 80             throw new rex_exception('Session not started, call rex_login::startSession() before!');
 81         }
 82 
 83         if (isset($_SESSION[self::getSessionNamespace()][$varname])) {
 84             return rex_type::cast($_SESSION[self::getSessionNamespace()][$varname], $vartype);
 85         }
 86 
 87         if ($default === '') {
 88             return rex_type::cast($default, $vartype);
 89         }
 90         return $default;
 91     }
 92 
 93     /**
 94      * Sets a session variable.
 95      *
 96      * @param string $varname Variable name
 97      * @param mixed  $value   Value
 98      *
 99      * @throws rex_exception
100      */
101     public static function setSession($varname, $value)
102     {
103         if (PHP_SESSION_ACTIVE != session_status()) {
104             throw new rex_exception('Session not started, call rex_login::startSession() before!');
105         }
106 
107         $_SESSION[self::getSessionNamespace()][$varname] = $value;
108     }
109 
110     /**
111      * Deletes a session variable.
112      *
113      * @param string $varname Variable name
114      *
115      * @throws rex_exception
116      */
117     public static function unsetSession($varname)
118     {
119         if (PHP_SESSION_ACTIVE != session_status()) {
120             throw new rex_exception('Session not started, call rex_login::startSession() before!');
121         }
122 
123         unset($_SESSION[self::getSessionNamespace()][$varname]);
124     }
125 
126     /**
127      * clear redaxo session contents within the current namespace (the session itself stays alive).
128      *
129      * @throws rex_exception
130      */
131     public static function clearSession()
132     {
133         if (PHP_SESSION_ACTIVE != session_status()) {
134             throw new rex_exception('Session not started, call rex_login::startSession() before!');
135         }
136 
137         unset($_SESSION[self::getSessionNamespace()]);
138     }
139 
140     /**
141      * Returns the variable $varname of $_COOKIE and casts the value.
142      *
143      * @param string $varname Variable name
144      * @param string $vartype Variable type
145      * @param mixed  $default Default value
146      *
147      * @return mixed
148      */
149     public static function cookie($varname, $vartype = '', $default = '')
150     {
151         return self::arrayKeyCast($_COOKIE, $varname, $vartype, $default);
152     }
153 
154     /**
155      * Returns the variable $varname of $_FILES and casts the value.
156      *
157      * @param string $varname Variable name
158      * @param string $vartype Variable type
159      * @param mixed  $default Default value
160      *
161      * @return mixed
162      */
163     public static function files($varname, $vartype = '', $default = '')
164     {
165         return self::arrayKeyCast($_FILES, $varname, $vartype, $default);
166     }
167 
168     /**
169      * Returns the variable $varname of $_ENV and casts the value.
170      *
171      * @param string $varname Variable name
172      * @param string $vartype Variable type
173      * @param mixed  $default Default value
174      *
175      * @return mixed
176      */
177     public static function env($varname, $vartype = '', $default = '')
178     {
179         return self::arrayKeyCast($_ENV, $varname, $vartype, $default);
180     }
181 
182     /**
183      * Searches the value $needle in array $haystack and returns the casted value.
184      *
185      * @param array      $haystack Array
186      * @param string|int $needle   Value to search
187      * @param string     $vartype  Variable type
188      * @param mixed      $default  Default value
189      *
190      * @throws InvalidArgumentException
191      *
192      * @return mixed
193      */
194     private static function arrayKeyCast(array $haystack, $needle, $vartype, $default = '')
195     {
196         if (!is_scalar($needle)) {
197             throw new InvalidArgumentException('Scalar expected for $needle in arrayKeyCast(), got '. gettype($needle) .'!');
198         }
199 
200         if (array_key_exists($needle, $haystack)) {
201             return rex_type::cast($haystack[$needle], $vartype);
202         }
203 
204         if ($default === '') {
205             return rex_type::cast($default, $vartype);
206         }
207         return $default;
208     }
209 
210     /**
211      * Returns the HTTP method of the current request.
212      *
213      * @return string HTTP method in lowercase (head,get,post,put,delete)
214      */
215     public static function requestMethod()
216     {
217         return isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'get';
218     }
219 
220     /**
221      * Returns true if the request is a XMLHttpRequest.
222      *
223      * This only works if your javaScript library sets an X-Requested-With HTTP header.
224      * This is the case with Prototype, Mootools, jQuery, and perhaps others.
225      *
226      * Inspired by a method of the symfony framework.
227      *
228      * @return bool true if the request is an XMLHttpRequest, false otherwise
229      */
230     public static function isXmlHttpRequest()
231     {
232         return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
233     }
234 
235     /**
236      * Returns true if the request is a PJAX-Request.
237      *
238      * @see http://pjax.heroku.com/
239      */
240     public static function isPJAXRequest()
241     {
242         return isset($_SERVER['HTTP_X_PJAX']) && $_SERVER['HTTP_X_PJAX'] == 'true';
243     }
244 
245     /**
246      * Returns true when the current request is a PJAX-Request and the requested container matches the given $containerId.
247      *
248      * @param string $containerId
249      *
250      * @return bool
251      */
252     public static function isPJAXContainer($containerId)
253     {
254         if (!self::isPJAXRequest()) {
255             return false;
256         }
257 
258         return isset($_SERVER['HTTP_X_PJAX_CONTAINER']) && $_SERVER['HTTP_X_PJAX_CONTAINER'] == $containerId;
259     }
260 
261     /**
262      * Returns whether the current request is served via https/ssl.
263      *
264      * @return bool true when https/ssl, otherwise false.
265      */
266     public static function isHttps()
267     {
268         return !empty($_SERVER['HTTPS']) && 'off' !== strtolower($_SERVER['HTTPS']);
269     }
270 
271     /**
272      * Returns the session namespace for the current http request.
273      *
274      * @return string
275      */
276     public static function getSessionNamespace()
277     {
278         // separate backend from frontend namespace,
279         // so we can e.g. clear the backend session without
280         // logging out the users from the frontend
281         $suffix = rex::isBackend() ? '_backend' : '';
282         return rex::getProperty('instname'). $suffix;
283     }
284 }
285