forked from kakserpom/quicky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quicky.form.class.php
393 lines (339 loc) · 8.47 KB
/
Quicky.form.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/**************************************************************************/
/* Quicky: smart and fast templates
/* ver. 0.5.0.0
/* http://code.google.com/p/quicky/
/* ===========================
/*
/* Copyright (c)oded 2007-2008 by WP
/* http://quicky-tpl.net
/*
/* Quicky.form.class.php: Quicky forms
/**************************************************************************/
class Quicky_form {
static $forms = array();
static $_uploads_temp = FALSE;
public $name;
public $elements;
public $properties;
public $_num_of_errors = 0;
public function __construct($name) {
$this->name = $name;
$this->elements = (object)array();
$this->properties = (object)array();
Quicky_form::$forms[$name] = $this;
}
public function getErrors() {
$errors = array();
foreach ($this->elements as $el) {
if (isset($el->_errormsg)) {
$errors[] = $el->_errormsg;
}
}
return $errors;
}
public function complete() {
if ($this->_num_of_errors) {
return FALSE;
}
if (isset($this->elements->submit) && !$this->elements->submit->clicked()) {
return FALSE;
}
return TRUE;
}
public function addElement($name, $properties = array()) {
if (is_object($properties)) {
if (!isset($properties->name)) {
$properties->name = $name;
}
}
else {
if (!isset($properties['name'])) {
$properties['name'] = $name;
}
}
$this->elements->$name = is_object($properties) ? $properties : new Quicky_form_element($properties);
$this->elements->$name->_form = $this;
}
public function removeElement($name) {
unset($this->elements->$name);
}
public function remove() {
unset(Quicky_form::$forms[$this->name]);
}
public function __get($name) {
return isset($this->$name) ? $this->$name : (isset($this->elements->$name) ? $this->elements->$name : NULL);
}
public function __clone() {
trigger_error('Cloning Quicky_form is not allowed', E_USER_ERROR);
}
}
class Quicky_form_filter {
static function email($string) {
return (bool)preg_match('~^[a-z0-9\._\-]+@[a-z0-9\._\-]+\.+[a-z]{2,}$~i', $string);
}
static function url($string) {
return (bool)preg_match('~^\w+://[a-z0-9\._\-]+/?~i', $string);
}
static function format($string, $regexp) {
return (bool)preg_match($regexp, $string);
}
static function length($string, $min = -1, $max = -1) {
return ($min === -1 or strlen($string) >= $min) && ($max === -1 or strlen($string) <= $max);
}
static function digit($string) {
return ctype_digit($string);
}
static function double($string, $abs = TRUE) {
return (bool)preg_match('~^' . (!$abs ? '-?' : '') . '\d+(\.\d+)?$~', $string);
}
static function notempty($string) {
return $string !== '';
}
}
class Quicky_form_element {
public $_errormsg;
public $_form;
public $type;
public $defaultValue;
public function __construct($properties = array()) {
foreach ($properties as $k => $v) {
$this->$k = $v;
}
}
public function error($errmsg = '') {
if (isset($this->_form)) {
++$this->_form->_num_of_errors;
}
if ($errmsg !== '') {
$this->_errormsg = $errmsg;
}
}
public function addFilter($filter, $errmsg = '') {
if (is_array($filter)) {
$name = $filter[0];
$filter[0] = $this->getValue();
}
else {
$name = $filter;
$filter = array($this->getValue());
}
if (!call_user_func_array(array('Quicky_form_filter', $name), $filter)) {
$this->error($errmsg);
return FALSE;
}
else {
return TRUE;
}
}
public function setValue($value) {
$this->value = $value;
}
public function setDefaultValue($value) {
$this->defaultValue = $value;
}
public function getValue() {
if (!isset($this->name)) {
return FALSE;
}
if (isset($this->value)) {
return $this->value;
}
return isset($_REQUEST[$this->name]) ? $_REQUEST[$this->name] : $this->defaultValue;
}
public function getString() {
if (!isset($this->name)) {
return '';
}
return gpcvar_str($_REQUEST[$this->name]);
}
public function getInt() {
if (!isset($this->name)) {
return 0;
}
return gpcvar_int($_REQUEST[$this->name]);
}
public function getFloat() {
if (!isset($this->name)) {
return (float)0;
}
return gpcvar_float($_REQUEST[$this->name]);
}
public function getArray() {
if (!isset($this->name)) {
return array();
}
return gpcvar_array($_REQUEST[$this->name]);
}
public function __toString() {
return strval($this->getValue());
}
}
class QButton extends Quicky_form_element {
public function __construct($properties = array()) {
if (!isset($properties['type'])) {
$properties['type'] = 'button';
}
foreach ($properties as $k => $v) {
$this->$k = $v;
}
}
public function clicked() {
if (!isset($this->name)) {
return FALSE;
}
return isset($_REQUEST[$this->name]);
}
public function notClicked() {
if (!isset($this->name)) {
return TRUE;
}
return !isset($_REQUEST[$this->name]);
}
}
class QSubmit extends Quicky_form_element {
public function __construct($properties = array()) {
if (!isset($properties['type'])) {
$properties['type'] = 'submit';
}
foreach ($properties as $k => $v) {
$this->$k = $v;
}
}
public function clicked() {
if (!isset($this->name)) {
return FALSE;
}
return isset($_REQUEST[$this->name]);
}
}
class QCheckBox extends Quicky_form_element {
public function __construct($properties = array()) {
$properties['type'] = 'checkbox';
foreach ($properties as $k => $v) {
$this->$k = $v;
}
}
public function checked() {
if (!isset($this->name)) {
return FALSE;
}
return isset($_REQUEST[$this->name]);
}
public function clicked() {
return $this->checked();
}
}
class QDropdown extends Quicky_form_element {
public $elements = array();
public function __construct($properties = array(), $elements = array()) {
$properties['type'] = 'select';
unset($properties['elements']);
foreach ($properties as $k => $v) {
$this->$k = $v;
}
$this->elements = $elements;
}
public function addElement($element, $name = '') {
if ($name === '') {
$this->elements[] = $element;
}
else {
$this->elements[$name] = $element;
}
}
public function removeElement($name) {
unset($this->elements[$name]);
}
}
class QTextBox extends Quicky_form_element {
public function __construct($properties = array()) {
if (!isset($properties['type'])) {
$properties['type'] = 'text';
}
foreach ($properties as $k => $v) {
$this->$k = $v;
}
}
public function getText() {
return $this->getValue();
}
}
class QFile extends Quicky_form_element {
public $_uploads_temp;
public $_upload_id;
public function __construct($properties = array()) {
if (!isset($properties['type'])) {
$properties['type'] = 'file';
}
foreach ($properties as $k => $v) {
$this->$k = $v;
}
$this->_uploads_temp = Quicky_form::$_uploads_temp;
}
public function isUploaded() {
return $this->getPath();
}
public function moveFileTo($dest) {
$p = $this->getPath();
if (!$p) {
return FALSE;
}
copy($p, $dest);
chmod($dest, 0644);
return $dest;
}
public function moveTo($dest) {
return $this->moveFileTo($dest);
}
public function getContents($empty = FALSE) {
$p = $this->getPath();
if (!$p) {
return $empty ? '' : FALSE;
}
return file_get_contents($p);
}
public function getPath() {
if ($this->_upload_id !== NULL) {
$p = $this->_uploads_temp . basename($this->_upload_id);
return file_exists($p) ? $p : FALSE;
}
if (!isset($this->name)) {
return FALSE;
}
if (isset($_FILES[$this->name])) {
return is_uploaded_file($_FILES[$this->name]['tmp_name']) ? $_FILES[$this->name]['tmp_name'] : FALSE;
}
if (isset($_REQUEST[$this->name]) && ($_REQUEST[$this->name] !== '')) {
$p = $this->_uploads_temp . basename(strval($_REQUEST[$this->name]));
return file_exists($p) ? $p : FALSE;
}
return FALSE;
}
public function getName() {
return isset($_FILES[$this->name]['name']) ? $_FILES[$this->name]['name'] : '';
}
}
class QBBarea extends Quicky_form_element {
public function __construct($properties = array()) {
if (!isset($properties['type'])) {
$properties['type'] = 'textarea';
}
foreach ($properties as $k => $v) {
$this->$k = $v;
}
require_once QUICKY_DIR . 'Quicky_BBcode.class.php';
$this->_bbcode = new Quicky_BBcode;
}
public function getText() {
return $this->getValue();
}
public function getHTML() {
$this->_bbcode->load($this->getValue());
return $this->_bbcode->getHTML();
}
}
abstract class QCAPTCHA_Abstract extends Quicky_form_element {
public $_imgid;
abstract public function validate();
}