This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostForm.class.php
214 lines (171 loc) · 4.55 KB
/
PostForm.class.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
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
210
211
212
213
<?php
// reviewed submission process:
// * initialize
// * if request method, form id and signature are valid (throw exception):
// * choose form handler (may be default: Form::handle())
// * import values (control errors)
// * process (form errors)
class PostForm extends Form
{
/**
* @var callback[]
*/
private $buttons = array();
/**
* @var bool
*/
private $signed;
/**
* @var IFormControl[]
*/
private $fieldsToSign = array();
/**
* @var array
*/
private $privateValues = array();
/**
* @var XorCipher
*/
protected $signer;
/**
* @param string $id Form identifier
* @param HttpUrl $action form action (url to which it will be sumbitted)
* @param string $sign form signature to protect it from vulnerabilities;
*/
function __construct($id, HttpUrl $action, $sign)
{
$this->signer = new XorCipherer($sign);
parent::__construct($id, $action, RequestMethod::post());
}
/**
* Whether form is already signed
* @return bool
*/
function isSigned()
{
return $this->signed;
}
/**
* @return Form itself
*/
function addButton($name, $label, $callback = null)
{
if (is_string($callback)) {
Assert::isTrue(method_exists($this, $callback), 'unknown method %s::%s', get_class($this), $callback);
$callback = array($this, $callback);
}
$this->addControl(FormControl::button($name, $label));
$this->buttons[$name] = $callback;
return $this;
}
/**
* @return Form itself
*/
function setHiddenValue($name, $value)
{
if (isset($this->fieldsToSign[$name])) {
$this->fieldsToSign[$name]->importValue($value);
}
else {
$field = FormControl::hidden($name, $value);
$this->addControl($field);
$this->fieldsToSign[$name] = $field;
}
return $this;
}
/**
* Handles request
* @return mixed true on success, array of errors on handle failure
* @throws PostFormException
*/
function handle(WebRequest $request)
{
if (!$request->getRequestMethod()->equals($this->getMethod()))
throw new PostFormException("Wrong request method");
$variables = array_replace(
$request->getPostVars(),
$request->getFilesVars()
);
$signName = $this->getSignatureFieldName();
if (!isset($variables[$signName]))
throw new PostFormException('Missing signature');
if (!$this->importSignature($variables[$signName]))
throw new PostFormException('Malformed signature');
if (isset($this->privateValues['referrer'])) {
if ($this->privateValues['referrer'] != (string)$request->getHttpReferer()) {
throw new PostFormException(
'Unexpected referrer `'.$this->privateValues['referrer'].'`, expected `'.$request->getHttpReferer().'`'
);
}
}
foreach ($this->buttons as $id => $callback) {
if (isset($variables[$id]) && is_callable($callback)) {
call_user_func($callback, $variables);
return;
}
}
$this->process($variables);
}
/**
* Signs the form. This method may be overridden to import fields to be signed, just call Form::setHiddenValue()
* @return Form
*/
function sign(WebRequest $request)
{
Assert::isFalse($this->isSigned(), 'form already signed');
$this->privateValues['referrer'] = (string) $request->getHttpUrl();
$this->addControl(FormControl::hidden($this->getSignatureFieldName(), $this->exportSignature()));
$this->signed = true;
return $this;
}
/**
* Overridden. Called when form is submitted
*/
protected function process(array $variables)
{
if ($this->import($variables)) {
// process me
}
}
/**
* @return string
*/
protected function dumpHidden()
{
Assert::isTrue($this->isSigned(), 'sign me please me');
return parent::dumpHidden();
}
protected function importSignature($string)
{
$decrypted = $this->signer->decrypt($string);
if (!$decrypted)
return false;
try {
$data = unserialize($decrypted);
}
catch (ErrorException $e) {
return false;
}
foreach ($data['fields'] as $key => $value)
$this->setHiddenValue($key, $value);
foreach ($data['values'] as $key => $value)
$this->privateValues[$key] = $value;
return true;
}
protected function exportSignature()
{
$fields = array();
foreach ($this->fieldsToSign as $name => $control) {
$fields[$name] = $control->getValue();
}
$values = $this->privateValues;
return $this->signer->encrypt(serialize(array(
'fields' => $fields,
'values' => $values
)));
}
protected function getSignatureFieldName()
{
return 'signature:' . sha1($this->signer->encrypt($this->getId()));
}
}