/
Class-CustomForm.php
351 lines (316 loc) · 8.02 KB
/
Class-CustomForm.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* @package Custom Form mod
* @version 1.0.3
* @author John Rayes <live627@gmail.com>
* @copyright Copyright (c) 2014, John Rayes
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
interface CustomForm
{
/*
* Constructs the field.
*
* @param array $field The field as returned by {@link total_getCustomForm()}.
* @param string $value Field value.
*
* @access public
*/
public function __construct(array $field, string $value);
/*
* Sets the input so the user can enter a value.
* Sets the output.
*
* @access public
*/
public function setHtml();
/*
* Gets the value. This method should handle if a specific field type must be sanitized.
*
* @access public
* @return string
*/
public function getValue(): string;
public function validate(): bool;
}
abstract class CustomFormBase implements CustomForm
{
public string $input_html;
public string $output_html;
protected array $field;
protected array $err = [];
protected bool $exists = false;
protected array $type_vars = array();
protected string $value = '';
protected int $size = 0;
protected string $default = '';
protected bool $required = false;
public function __construct(array $field, string $value)
{
$this->field = $field;
$this->value = $value;
$this->exists = !empty($value);
}
/*
* Gets the error generated by the validation method.
*
* @access public
* @return array
*/
public function getError(): array
{
return $this->err;
}
/**
* Returns the input so the user can enter a value.
*
* @access public
* @return string
*/
public function getInputHtml(): string
{
return $this->input_html;
}
/**
* Returns the output. It's the field's value formatted acccording to its criteria.
*
* @access public
* @return string
*/
public function getOutputHtml(): string
{
return $this->output_html;
}
/**
* @access public
*/
public function setOptions()
{
$temp = !empty($this->field['type_vars'])
? array_map('trim', explode(',', $this->field['type_vars']))
: array();
// Go through all of the type_vars to format them correctly.
if (!empty($temp))
foreach ($temp as $var)
{
if (substr($var, 0, 5) == 'size=')
$this->size = intval(substr($var, 5));
elseif (substr($var, 0, 8) == 'default=')
$this->default = substr($var, 8);
elseif ($var == 'required')
$this->required = true;
elseif ($var != '')
$this->type_vars[] = $var;
}
}
}
class CustomForm_info extends CustomFormBase
{
public function setHtml()
{
$this->input_html = $this->getValue();
$this->output_html = '';
}
public function validate(): bool
{
return true;
}
public function getValue(): string
{
return $this->value;
}
}
class CustomForm_check extends CustomFormBase
{
public function setHtml()
{
global $txt;
$true = (!$this->exists && $this->default) || $this->value;
$this->input_html = sprintf(
'<input type="checkbox" text="%s[$s]"%s>',
'CustomFormField',
$this->field['id_field'],
$true ? ' checked' : ''
);
$this->output_html = $txt[$this->getValue()];
}
public function validate(): bool
{
// Nothing needed here, really. It's just a get out of jail
// free card. "This card may be kept until needed, or sold."
return true;
}
public function getValue(): string
{
return (!$this->exists && $this->default) || $this->value ? 'yes' : 'no';
}
}
class CustomForm_select extends CustomFormBase
{
public function setHtml()
{
$this->input_html = sprintf(
'<select name="%s[$s]" style="width: 90%;">',
'CustomFormField',
$this->field['id_field']
);
foreach ($this->type_vars as $v)
{
$this->input_html .= sprintf(
'<option%s> %s</option>',
(!$this->exists && $this->default == $v) || $this->value == $v
? ' checked="checked"'
: '',
$v
);
if ($true)
$this->output_html = $v;
}
$this->input_html .= '</select>';
}
public function validate(): bool
{
$found = isset(array_flip($this->type_vars)[$this->value]) || !empty($this->default);
if (!$found && $this->required)
$this->err = array('customform_invalid_value', $this->field['text']);
return $found;
}
public function getValue(): string
{
return isset(array_flip($this->type_vars)[$this->value])
? $this->value
: $this->default;
}
}
class CustomForm_radio extends CustomForm_select
{
public function setHtml()
{
$this->input_html = '<fieldset>';
foreach ($this->type_vars as $v)
{
$this->input_html .= sprintf(
'<label><input type="radio" name="%s[$s]"%s> %s</label><br>',
'CustomFormField',
$this->field['id_field'],
(!$this->exists && $this->default == $v) || $this->value == $v
? ' checked="checked"'
: '',
$v
);
if ($true)
$this->output_html = $v;
}
$this->input_html .= '</fieldset>';
}
}
class CustomForm_text extends CustomFormBase
{
public function setHtml()
{
$this->output_html = $this->value;
$this->input_html = sprintf(
'<input type="text" name="%s[$s]" style="width: 90%;" value="%s">',
'CustomFormField',
$this->field['id_field'],
$this->value
);
}
public function getValue(): string
{
global $smcFunc;
if (!empty($this->size))
$this->value = $smcFunc['substr']($this->value, 0, $this->size);
if (!in_array('parse_bbc', $this->type_vars))
$this->value = '[nobbc]' . $this->value . '[/nobbc]';
return $this->value;
}
public function validate(): bool
{
if (!$this->exists && $this->required)
$this->err = array('customform_invalid_value', $this->field['text']);
//~ $class_name = 'CustomFormFieldMask_' . $this->field['mask'];
//~ if (!class_exists($class_name))
//~ fatal_error('Mask "' . $this->field['mask'] . '" not found for field "' . $this->field['name'] . '" at ID #' . $this->field['id_field'] . '.', false);
//~ $mask = new $class_name($this->value, $this->field);
//~ $mask->validate(): bool;
//~ if (false !== ($err = $mask->getError()))
//~ $this->err = $err;
return empty($this->err);
}
}
class CustomForm_textarea extends CustomForm_text
{
public function setHtml()
{
$this->output_html = $this->value;
@list ($rows, $cols) = @explode(',', $this->default);
$this->input_html = '<textarea name="%s[$s]" ' . (!empty($rows) ? 'rows="' . $rows . '"' : '') . ' ' . (!empty($cols) ? 'cols="' . $cols . '"' : '') . '>' . $this->value . '</textarea>';
}
}
interface CustomFormFieldMask
{
public function __construct($value, $field);
public function validate(): bool;
}
abstract class CustomFormFieldMaskBase implements CustomFormFieldMask
{
protected $value;
protected $field;
protected $err;
public function __construct($value, $field)
{
$this->value = $value;
$this->field = $field;
$this->err = false;
}
public function getError()
{
return $this->err;
}
}
class CustomFormFieldMask_email extends CustomFormFieldMaskBase
{
public function validate(): bool
{
if (!preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $this->value))
$this->err = array('customform_invalid_value', $this->field['text']);
}
}
class CustomFormFieldMask_regex extends CustomFormFieldMaskBase
{
public function validate(): bool
{
if (!preg_match($this->field['regex'], $this->value))
if (!empty($this->field['err']))
$this->err = $this->field['err'];
else
$this->err = array('customform_invalid_value', $this->field['text']);
}
}
class CustomFormFieldMask_number extends CustomFormFieldMaskBase
{
public function validate(): bool
{
if (!preg_match('/^\s*([0-9]+)\s*$/', $this->value))
$this->err = array('customform_invalid_value', $this->field['text']);
}
}
class CustomFormFieldMask_float extends CustomFormFieldMaskBase
{
public function validate(): bool
{
if (!preg_match('/^\s*([0-9]+(\.[0-9]+)?)\s*$/', $this->value))
$this->err = array('customform_invalid_value', $this->field['text']);
}
}
class CustomFormFieldMask_nohtml extends CustomFormFieldMaskBase
{
public function validate(): bool
{
if (strip_tags($this->value) != $this->value)
$this->err = array('customform_invalid_value', $this->field['text']);
}
}
?>