17 $root = $_SERVER[
'DOCUMENT_ROOT'] ?? null;
18 if(!$root && ($filename = $_SERVER[
'SCRIPT_FILENAME'] ?? null))
19 $root = str_replace(
'\\',
'/',\substr($filename,0,-strlen($_SERVER[
'PHP_SELF'] ?? null)));
20 if(!$root && ($path = $_SERVER[
'PATH_TRANSLATED'] ?? null))
21 $root = str_replace([
'\\',
'\\\\'],
'/',substr($path,0,-strlen($_SERVER[
'PHP_SELF'] ?? null)));
22 $root = rtrim($root,
'/');
31 static $version = null;
32 if($version === null) $version = ($protocol = $_SERVER[
'SERVER_PROTOCOL']) && ($i = strpos($protocol,
'/'))
33 ? (
float)substr($protocol,$i + 1)
42 return strtoupper(preg_replace(
'/\\W+/',
'',$_SERVER[
'REQUEST_METHOD'] ?? null));
49 return !strcasecmp($_SERVER[
'HTTPS'] ?? null,
'on');
56 public static function host($complete =
false){
57 $host = ($_SERVER[
'SERVER_NAME'] ?? null) ?: gethostname();
59 $host = (($secure = self::secure()) ?
'https' :
'http') .
'://' . $host;
60 if(($port = $_SERVER[
'SERVER_PORT'] ?? null) && ($port != ($secure ? 443 : 80))) $host .=
':' . $port;
70 return array_key_exists($code,$codes = require(__DIR__ .
'/../../data/httpcode.php')) ? $codes[$code] : null;
79 return strpos($url,
'://') ? $url : $protocol .
':' . (substr($url,0,2) ==
'//' ?
'' :
'//') . $url;
87 do $url = preg_replace(
'/\/[^\/]*\/\.\.\//',
'/',$url,-1,$count);
99 if(($i = strpos($url,
'#')) !==
false){
100 $hash = substr($url,$i);
101 $url = substr($url,0,$i);
103 return $url . (parse_url($url,PHP_URL_QUERY) ?
'&' :
'?') . (is_array($params) ? http_build_query($params) : $params) . $hash;
116 ($size = $_SERVER[
'CONTENT_LENGTH'] ?? null) &&
117 ($max = \
Rsi\Number::shorthandBytes(ini_get(
'post_max_size'),
false)) &&
130 public static function setCookie($key,$value,$days = 365,$options = null){
131 $_COOKIE[$key] = $value;
132 $options = ($options ?: []) +
133 [
'expires' => $days ? time() + $days * 86400 : 0,
'path' =>
'/',
'domain' =>
'',
'httponly' =>
true];
134 if(!array_key_exists(
'secure',$options)) $options[
'secure'] = self::secure();
135 return \Rsi::version(
'7.3.0')
136 ? setcookie($key,$value,$options)
137 : setcookie($key,$value,$options[
'expires'],$options[
'path'],$options[
'domain'],$options[
'secure'],$options[
'httponly']);
146 return Record::get($_COOKIE,$key,$default);
154 public static function lang($accepted = null,$quality = 0){
157 '/([a-z]{1,8})(-[a-z]{1,8})?\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
158 $_SERVER[
'HTTP_ACCEPT_LANGUAGE'] ?? null,
161 ))
foreach($client_accepted as $client_accept){
162 $client_lang = $client_accept[1] . ($client_accept[2] ?? null);
164 (!$accepted || in_array($client_lang,$accepted) || in_array($client_lang = $client_accept[1],$accepted)) &&
165 (($client_quality = Record::get($client_accept,4,1)) > $quality)
167 $lang = $client_lang;
168 $quality = $client_quality;
179 if(count($groups = explode(
':',$addr)) > 1){
181 foreach($groups as $index => $group)
if(!$index || ($group !==
'')) $addr[] = Str::pad($group,4);
182 else $addr = array_merge($addr,array_fill(0,9 - count($groups),
'0000'));
183 $addr = implode(
':',$addr);
192 static $remote_addr = null;
193 if($remote_addr === null) $remote_addr = self::expandAddr($_SERVER[
'REMOTE_ADDR'] ?? null);
202 public static function inSubnet($subnets,$remote_addr = null){
203 foreach((array)$subnets as $subnet)
switch(count($cidr = explode(
'/',$subnet))){
205 $cidr = [$subnet,strlen(inet_pton($subnet)) << 3];
207 if(strlen($ip = bin2hex(inet_pton($remote_addr ?: self::remoteAddr()))) == strlen($subnet = bin2hex(inet_pton($cidr[0])))){
208 $nibbles = $cidr[1] >> 2;
209 if((substr($ip,0,$nibbles) == substr($subnet,0,$nibbles)) && (
210 ($nibbles == strlen($ip)) ||
211 !($mask = (0xf0 >> ($cidr[1] - ($nibbles << 2)) & 0xf)) ||
212 ((hexdec($ip[$nibbles]) & $mask) == (hexdec($subnet[$nibbles]) & $mask))
224 header(
'Location: ' . $url,
true,$permanent ? 301 : 302);
233 if($push = self::version() >= 2) header(
"Link: <$src>; rel=preload; as=$type",
false);
243 header(
'Content-Type: ' . ($content_type ?: File::mime($filename)));
244 header(
'Content-Transfer-Encoding: binary');
245 header(
'Content-Disposition: attachment; filename="' . basename($filename) .
'"');
246 if(($size !== null) || is_file($filename)) header(
'Content-Length: ' . ($size === null ? filesize($filename) : $size));
247 header(
'Expires: 0');
248 header(
'Cache-Control: must-revalidate, post-check=0, pre-check=0');
249 header(
'Pragma: public');
262 public static function urlGetContents($url,$post = null,$headers = null,$options = null,$timeout = null,$max_redirs = null){
263 $ch = curl_init($url);
264 curl_setopt($ch,CURLOPT_RETURNTRANSFER,
true);
266 curl_setopt($ch,CURLOPT_POST,
true);
267 curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
270 foreach($headers as $key => &$value) $value = $key .
': ' . $value;
272 curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
274 if($options) curl_setopt_array($ch,$options);
275 curl_setopt($ch,CURLOPT_TIMEOUT,$timeout ?: ini_get(
'default_socket_timeout'));
277 curl_setopt($ch,CURLOPT_FOLLOWLOCATION,
true);
278 curl_setopt($ch,CURLOPT_MAXREDIRS,$max_redirs);
280 $result = curl_exec($ch);
281 $message = ($error = curl_errno($ch)) ? curl_error($ch) : null;
282 $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
284 if($error)
throw new \Exception(
"cURL error: $message ($error)");
286 case 200:
return $result;
287 case 404:
return false;
288 default:
throw new \Exception(
"unexpected response ($code): $result");
static inSubnet($subnets, $remote_addr=null)
Check if an IP-adres lies within a certain subnet.
static redirHeader($url, $permanent=false)
Add a redirection header.
static version()
Protocol version.
static host($complete=false)
Host name.
static postExceeded(&$size=null, &$max=null)
Check if the maximum POST size was exceeded.
static addParams($url, $params)
Add parameters to a URL.
static remoteAddr()
Returns the expanded, remote address (if available).
static codeDescr($code)
Translate a HTTP response code to a description.
static lang($accepted=null, $quality=0)
Get the prefered language from the client.
static docRoot()
Document root.
static urlGetContents($url, $post=null, $headers=null, $options=null, $timeout=null, $max_redirs=null)
Get content from URL.
static pushHeader($src, $type)
Add a push header.
static realUrl($url)
Remove double dots from a URL.
static ensureProtocol($url, $protocol='http')
Make sure a URL starts with a protocol.
static secure()
Check if the request was made over a secure connection.
static getCookie($key, $default=null)
Retrieve a cookie.
static setCookie($key, $value, $days=365, $options=null)
Set a cookie.
static method()
Request method.
static expandAddr($addr)
Expand an abbreviated IPv6 address to its full representation.
static downloadHeaders($filename, $content_type=null, $size=null)
Send download headers.