34 switch(($type = $_SERVER[
'CONTENT_TYPE'] ??
null) ? strtolower(trim(explode(
';',$type)[0] ??
null)) :
null){
35 case 'application/json':
36 if(is_array(
$data = json_decode(file_get_contents(
'php://input'),
true))) $_POST =
$data;
37 if(json_last_error()) $this->errors[
'post'] = $this->
component(
'trans')->str($this->jsonDecodingError,[
'error' => json_last_error_msg()]);
39 case 'application/xml':
40 if(is_array(
$data = json_decode(json_encode(simplexml_load_file(
'php://input',
null,LIBXML_NOCDATA)),
true))) $_POST =
$data;
43 if(\
Rsi\Http::postExceeded($size,$max)){
45 $this->errors[
'post'] = $this->
component(
'trans')->str($this->postExceededError,[
'size' => $local->formatBytes($size),
'max' => $local->formatBytes($max)]);
50 $this->_fred->externalError($e->getMessage());
64 protected function fixFileKey($values,$prefix =
null,$files =
null){
65 if(!$prefix) $prefix = $files = [];
66 foreach($values as $key => $value){
67 $key = array_merge($prefix,[$key]);
68 if(is_array($value)) $files = $this->
fixFileKey($value,$key,$files);
70 $item = array_shift($key);
71 \Rsi\Record::set($files,array_merge($key,[$item]),$value);
84 if($keys)
foreach(array_values($keys) as $index => $key) $_GET[$key] = $argv[$index + 1] ??
null;
85 else parse_str(implode(
'&',array_slice($argv,1)),$_GET);
92 public function file($key){
93 return \Rsi\Record::get($this->files,$key);
103 public function complex($key,$default =
null){
104 if(($value = \
Rsi\Record::get($_POST,$key,
false)) !==
false)
return $value;
105 if(($value = $this->
file($key)) !==
null)
return $value;
106 return \Rsi\Record::iget($_GET,$key,$default);
115 public function record($key,$default =
null){
116 if(!is_array($value = $this->
complex($key,$default)))
return $default;
117 foreach($value as $sub)
if(is_array($sub) || ($this->encoding && !mb_check_encoding($sub,$this->encoding)))
return $default;
128 public function simple($key,$default =
null,$mask =
null){
129 return (is_array($value = $this->
complex($key,$default)) || ($value && (
130 ($this->encoding && !mb_check_encoding($value,$this->encoding)) ||
131 ($mask && !preg_match($mask ===
true ?
'/^\\w+$/' : $mask,$value))
132 ))) ? $default : $value;
135 public function data($key,$default =
null){
136 return \Rsi\Record::get($this->
data,$key,$default);
147 public function cookie($key,$value =
null,$days = 365,$options =
null){
148 if(func_num_args() > 1) \Rsi\Http::setCookie($key,$value,$days,($options ?: []) + $this->cookieOptions);
149 return $_COOKIE[$key] ??
false;
153 if($this->_files ===
null){
155 foreach($_FILES as $key => $value) $this->_files[$key] = $this->
fixFileKey($value);
161 if($this->_server ===
null){
162 $server = [
'http' => []];
163 foreach($_SERVER as $key => $value)
164 if(substr($key = strtolower($key),0,5) ==
'http_') $server[
'http'][lcfirst(\
Rsi\Str::camel(substr($key,5)))] = $value;
165 else $server[lcfirst(\
Rsi\Str::camel($key))] = $value;
166 $this->_server = new \Rsi\Wrapper\Record($server);
172 return array_key_exists($key,$this->
data) ? $this->
data[$key] : $this->
simple($key);
175 protected function _set($key,$value){
176 $this->
data[$key] = $value;
180 return $this->
data($key,$default);