-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebapp_stdio.php
More file actions
209 lines (209 loc) · 4.72 KB
/
webapp_stdio.php
File metadata and controls
209 lines (209 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
require 'webapp.php';
if (PHP_SAPI === 'cli')
{
final class webapp_stdio implements webapp_io
{
function request_ip():string
{
return '127.0.0.1';
}
function request_time():int
{
return $_SERVER['REQUEST_TIME'];
}
function request_scheme():string
{
return 'php';
}
function request_method():string
{
return isset($_SERVER['argv'][1]) && strlen($method = explode('?', $_SERVER['argv'][1], 2)[0]) ? $method : 'cli';
}
function request_query():string
{
return isset($_SERVER['argv'][1]) && is_string($query = strstr($_SERVER['argv'][1], '?')) ? substr($query, 1) : '';
}
function request_into():string
{
return "/{$_SERVER['SCRIPT_NAME']}";
}
function request_header(string $name):?string
{
return $_SERVER[strtoupper($name)] ?? NULL;
}
function request_cookie(string $name):?string
{
return $_COOKIE[$name] ?? NULL;
}
function request_content():string
{
return file_get_contents('php://input');
}
function request_formdata():array
{
return [];
}
function request_uploadedfile():array
{
return [];
}
function response_sent():bool
{
return headers_sent();
}
function response_status(int $code):void
{
http_response_code($code);
}
function response_header(string $value):void
{
header($value);
}
function response_cookie(float|string ...$values):void
{
setcookie(...$values);
}
function response_content(string $data):bool
{
return file_put_contents('php://output', $data) !== FALSE;
}
function response_sendfile(string $filename):bool
{
return copy($filename, 'php://output');
}
}
}
else
{
final class webapp_stdio implements webapp_io
{
function request_ip():string
{
return $_SERVER['REMOTE_ADDR'];
}
function request_time():int
{
return $_SERVER['REQUEST_TIME'];
}
function request_scheme():string
{
return $_SERVER['REQUEST_SCHEME'];
}
function request_method():string
{
return $_SERVER['REQUEST_METHOD'];
}
function request_query():string
{
return $_SERVER['QUERY_STRING'] ?? '';
}
function request_into():string
{
return $_SERVER['SCRIPT_NAME'];
}
function request_header(string $name):?string
{
return array_change_key_case(apache_request_headers(), CASE_UPPER)[$same = strtoupper($name)]
?? $_SERVER[match($alias = strtr($same, '-', '_'))
{
'CONTENT_TYPE', 'CONTENT_LENGTH' => $alias,
default => "HTTP_{$alias}"
}] ?? ($same === 'AUTHORIZATION' ? match(TRUE)
{
array_key_exists('PHP_AUTH_DIGEST', $_SERVER)
=> "Digest {$_SERVER['PHP_AUTH_DIGEST']}",
isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])
=> 'Basic ' . base64_encode("{$_SERVER['PHP_AUTH_USER']}:{$_SERVER['PHP_AUTH_PW']}"),
default => $_SERVER['AUTHORIZATION'] ?? NULL
} : NULL);
}
function request_cookie(string $name):?string
{
return $_COOKIE[$name] ?? NULL;
}
function request_content():string
{
return file_get_contents('php://input');
}
function request_formdata():array
{
if ($this->request_method() === 'POST')
{
return $_POST;
}
//PHP8.4 request_parse_body ??
if (str_starts_with($this->request_header('Content-Type') ?? '', 'application/x-www-form-urlencoded'))
{
parse_str($this->request_content(), $data);
}
else
{
$data = [];
}
return $data;
}
function request_uploadedfile():array
{
$uploadedfiles = [];
foreach ($_FILES as $name => $file)
{
$files = [];
foreach ($file as $type => $info)
{
if (is_array($info))
{
foreach ($info as $index => $value)
{
$files[$index][$type] = $value;
}
continue;
}
$files[0][$type] = $info;
}
$uploadedfiles[$name] = [];
foreach ($files as $file)
{
if ($file['error'] === UPLOAD_ERR_OK)
{
$uploadedfiles[$name][] = [
'path' => strstr($file['full_path'], '/'),
'file' => $file['tmp_name'],
'size' => $file['size'],
'mime' => $file['type'],
'name' => $file['name']
];
}
}
}
return $uploadedfiles;
}
function response_sent():bool
{
return headers_sent();
}
function response_status(int $code):void
{
http_response_code($code);
}
function response_header(string $value):void
{
header($value);
}
function response_cookie(float|string ...$values):void
{
setcookie(...$values);
}
function response_content(string $data):bool
{
return file_put_contents('php://output', $data) !== FALSE;
}
function response_sendfile(string $filename):bool
{
return PHP_SAPI === 'apache2handler'
&& in_array('mod_xsendfile', apache_get_modules(), TRUE)
? $this->response_header("X-Sendfile: {$filename}") === NULL
: copy($filename, 'php://output');
}
}
}