-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
executable file
·205 lines (172 loc) · 5.61 KB
/
Copy pathRequest.php
File metadata and controls
executable file
·205 lines (172 loc) · 5.61 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
<?php
namespace Pet\Request;
use Pet\File\File;
use Pet\File\FileCollection;
use Pet\Router\Header;
use Pet\Router\HTTP;
use Pet\Tools\Tools;
class Request
{
public static array $attribute = [];
public static array $parametr = [];
public static array $levels = [];
public static string $original = '';
public $header = [];
public $path;
/** @var FileCollection|null Коллекция загруженных файлов */
private static ?FileCollection $uploadedFiles = null;
public function __construct()
{
self::$parametr = [];
self::$attribute = $this->input();
$this->path = $this->getURI();
$this->parsingHeaders();
self::$uploadedFiles = null;
}
public function getMethod(): string
{
return $_SERVER['REQUEST_METHOD'];
}
public function getURI()
{
$path = str_contains($_SERVER['REQUEST_URI'], '?') ? explode('?', $_SERVER['REQUEST_URI'])[0] :
$_SERVER['REQUEST_URI'];
self::$original = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? '';
self::$levels = array_values(array_filter(explode('/', trim($path, '/'))));
return $path != '/'? Tools::strRep(strlen($path) - 1, '', $path, '/'): $path;
}
/**
* input
*
* @param string|null $name
* @return array|string|null
*/
public function input(string|null $name = null): array|string|null
{
if(!empty(self::$attribute)){
return key_exists($name, self::$attribute) ? self::$attribute[$name]: self::$attribute;
}
$REQUEST = $this->parsing();
if (!$name) return $REQUEST;
return key_exists($name, $REQUEST) ? $REQUEST[$name] : null;
}
private function parsing()
{
$REQUEST = array_merge($_GET, $_POST, $_FILES);
$decode = [];
$input = file_get_contents('php://input');
$ctype = strtolower($_SERVER['CONTENT_TYPE'] ?? '');
if (str_contains($ctype, 'json') && !empty($input)) $decode = Tools::jsonDe($input);
return array_merge($REQUEST, $decode);
}
/**
* Возвращает загруженный файл как объект File или коллекцию FileCollection.
*
* @param string|null $name Имя поля в $_FILES
* @return File|FileCollection|array|null
*/
public function file(?string $name = null): File|FileCollection|array|null
{
if ($_FILES === []) {
return $name !== null ? null : [];
}
if (self::$uploadedFiles === null) {
self::$uploadedFiles = FileCollection::fromUploadedFiles($_FILES);
}
if ($name === null) {
return self::$uploadedFiles;
}
if (!isset($_FILES[$name])) {
return null;
}
if (is_array($_FILES[$name]['name'] ?? null)) {
return FileCollection::fromUploadedFiles($_FILES[$name]);
}
if (($_FILES[$name]['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_OK) {
return File::fromUpload($_FILES[$name]);
}
return null;
}
/**
* Проверяет, был ли загружен файл с указанным именем.
*
* @param string $name
* @return bool
*/
public function hasFile(string $name): bool
{
return isset($_FILES[$name]) && ($_FILES[$name]['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_NO_FILE;
}
/**
* Возвращает все загруженные файлы как массив сырых $_FILES.
*
* @return array
*/
public function allFiles(): array
{
return $_FILES;
}
private function parsingHeaders(){
$header = getallheaders();
foreach($header as $key => $val) $this->header[strtolower($key)] = strtolower($val);
}
public function ip(): string|false {
$ip_keys = [
'REMOTE_ADDR',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_REAL_IP',
'HTTP_CLIENT_IP',
];
foreach ($ip_keys as $key) {
if (!empty($_SERVER[$key])) {
$ips = explode(',', $_SERVER[$key]);
$ip = trim($ips[0]);
if (filter_var($ip, FILTER_VALIDATE_IP)) {
return $ip;
}
}
}
return false;
}
/**
* Возвращает путь запроса (URI без query-строки).
*
* @return string
*/
public function getPath(): string
{
return $this->path;
}
/**
* Возвращает значение заголовка запроса (регистронезависимо).
*
* @param string $name Имя заголовка
* @return string|null
*/
public function getHeader(string $name): ?string
{
$key = strtolower($name);
return $this->header[$key] ?? null;
}
/**
* Устанавливает параметр маршрута (из flexible- или wildcard-маршрутов).
*
* @param string $name Имя параметра
* @param string $value Значение параметра
* @return void
*/
public static function setParameter(string $name, string $value): void
{
self::$parametr[$name] = $value;
}
/**
* Возвращает параметр маршрута по имени.
*
* @param string $name Имя параметра
* @return string|null
*/
public static function getParameter(string $name): ?string
{
return self::$parametr[$name] ?? null;
}
}