-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·77 lines (74 loc) · 2.89 KB
/
index.php
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
<?php namespace x\form;
function content($content) {
if (!\is_array($form = $_SESSION['form'] ?? [])) {
return;
}
// Convert `foo[bar][baz]` to `foo.bar.baz`
$keys = static function (string $v) {
return \trim(\strtr($v, [
'.' => "\\.",
'][' => '.',
'[' => '.',
']' => '.'
]), '.');
};
if (false !== \strpos($content, '<input ')) {
$content = \preg_replace_callback('/<input(?>\s(?>"[^"]*"|\'[^\']*\'|[^>])*)?>/', static function ($m) use ($form, $keys) {
$input = new \HTML($m[0]);
if (!$name = $input['name']) {
return $m[0];
}
$type = $input['type'];
if ('file' === $type || 'hidden' === $type || 'password' === $type) {
// Disable form session on `file`, `hidden` and `password` input
return $m[0];
}
$name = $keys($name);
$value = \get($form, $name);
if ('checkbox' === $type || 'radio' === $type) {
if (isset($value)) {
$input['checked'] = \s($value) === \s($input['value']);
}
} else {
$input['value'] = $value ?? $input['value'];
}
return $input;
}, $content);
}
if (false !== \strpos($content, '<select ')) {
$content = \preg_replace_callback('/<select(?>\s(?>"[^"]*"|\'[^\']*\'|[^\/>])*)?>[\s\S]*?<\/select>/', static function ($m) use ($form, $keys) {
$select = new \HTML($m[0]);
if (!$name = $select['name']) {
return $m[0];
}
$name = $keys($name);
$value = \get($form, $name);
$select[1] = \preg_replace_callback('/<option(?>\s(?>"[^"]*"|\'[^\']*\'|[^\/>])*)?>[\s\S]*?<\/option>/', static function ($m) use ($value) {
$option = new \HTML($m[0]);
if (isset($value)) {
$option['selected'] = \s($value) === \s($option['value'] ?? $option[1]);
}
return $option;
}, $select[1]);
return $select;
}, $content);
}
if (false !== \strpos($content, '<textarea ')) {
$content = \preg_replace_callback('/<textarea(?>\s(?>"[^"]*"|\'[^\']*\'|[^\/>])*)?>[\s\S]*?<\/textarea>/', static function ($m) use ($form, $keys) {
$textarea = new \HTML($m[0]);
if (!$name = $textarea['name']) {
return $m[0];
}
$name = $keys($name);
$value = \get($form, $name);
$textarea[1] = \is_string($value) ? \htmlspecialchars($value) : $textarea[1];
return $textarea;
}, $content);
}
return $content;
}
function let() {
unset($_SESSION['form']);
}
\Hook::set('content', __NAMESPACE__ . "\\content", 0);
\Hook::set('let', __NAMESPACE__ . "\\let", 20);