My favorites | Sign in
Project Logo
                
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<?php
//------------------------------------------------------------------------------
// Valerie
// (c) 2009 Chris Jaure
// license: MIT License
// website: http://code.google.com/p/valerie/
//
// valerieserver.php
//------------------------------------------------------------------------------


/*
Class: ValerieServer

Validates the forms. Any form values are converted to UTF-8 charset.
*/

class ValerieServer {

private $values = array();
private $rules = array();
private $elements = array();
private $errors;
private $ajax;
private $periodical;
private $patterns = array();
private $filters = array();
private $referer;
private $definition;
private $uid;
private $response = array();
private $responseReturned;
private $ns;

/*
Constructor: __construct

Arguments:

- $data - POST data
- $lang - language file to use, filename minus .php

Returns:

- ValerieServer instance
*/

public function __construct($data, $lang = 'en.php'){

@session_start();

$this->ajax = isset($data['_ajax']);
$this->periodical = isset($data['_periodical']);

$this->uid = $data['formid'];
$this->definition = unserialize(
$_SESSION[App::get('config:session_ns')][$this->uid]
);

if (!is_array($this->definition)) {
$error = 'Form definition could not be found at this location: $_SESSION['
. App::get('config:session_ns') . '][' . $this->uid . ']';
trigger_error($error, E_USER_ERROR);
}

require_once "localization/$lang";
require_once "libs/utf8/utf8.php";

$this->ns = App::get('config:session_ns') .
$this->definition['attributes']['id'];
$this->referer = $_SESSION[$this->ns]['referer'];
$this->setValues($this->definition['elements'], $data);

$this->filters = App::get('filters');
$this->patterns = App::get('rules');

App::set('form_id', $this->definition['attributes']['id']);
Valerie::loadFormPlugins(App::get('form_id'));
}

/*
Method: setValues

Matches elements from the form definition with the submitted values.
Recursive.

Arguments:

- $els - array of form elements
- $val - array of submitted values.
*/

private function setValues($els, $vals) {
foreach($els as $element) {
if (isset($element['elements'])) {
$this->setValues($element['elements'], $vals);
}
if (isset($element['name'])) {
$name = $element['name'];
if (substr($name, -2) == '[]') {
$name = substr($name, 0, -2);
}
if (is_array($vals[$name])) {
foreach ($vals[$name] as &$value) {
$value = $this->cleanValue($value);
}
$this->values[$name] = $vals[$name];
}
else {
$this->values[$name] = $this->cleanValue($vals[$name]);
}
if (isset($element['validation'])) {
$this->rules[$name] = (array) $element['validation'];
}
$this->elements[$name] = $element;
}
}
}

/*
Method: cleanValue

This will trim the value and make sure it's a valid UTF-8 string.
*/

private function cleanValue($value) {
$value = trim($value);
if (strtoupper(App::get('config:char_encoding')) != 'UTF-8') {
$value = iconv(
App::get('config:char_encoding'),
'UTF-8//TRANSLIT',
$value
);
}
else {
$value = iconv('UTF-8', 'UTF-8//IGNORE', $value);
}

return $value;
}

/*
Method: validate

Validates the submitted form data.

Returns:

- array of values if the form validates, otherwise false.
*/

public function validate() {
Valerie::fireHooks('beforeValidateForm', array(&$this));

if (!$this->ajax) unset($_SESSION[$this->ns]);

if ($this->periodical === false) {

// validate
foreach($this->rules as $name => $rules) {
$values = $this->values[$name];
foreach ((array) $values as $value) {
foreach($rules as $rule) {
if(!$this->test($rule, $value, $name)) break;
}
}
}

// return any errors
if (isset($this->errors)) {
$elements = array();
foreach ($this->elements as $name => $element) {
$el = array(
'id' => $element['id'],
'name' => $name,
'message' => $this->errors[$name]
);
if (!$this->ajax) {
$el['value'] = $this->values[$name];
}
$elements[$name] = $el;
}
$this->setResponse(array(
'message_type' => 'invalid',
'message' => __('Please correct the errors below.'),
'elements' => $elements
), 'form');
}
else {
$this->setResponse(array(
'message_type' => 'valid',
'message' => __('Your form has been submitted.')
), 'form');
}
}
else {

foreach($this->rules[$this->periodical] as $rule ) {
if(!$this->test(
$rule,
$this->values[$this->periodical],
$this->periodical
)) break;
}

if (isset($this->errors)) {
echo '{"type": "invalid", "content": {"id": "' . key($this->errors) .
'", "message": "' . current($this->errors) . '"}}';
}
else {
echo '{"type": "valid", "content": {"id": "'. $this->periodical . '"}}';
}
exit();

}

Valerie::fireHooks('afterValidateForm', array(&$this, &$this->values, (!isset($this->errors))));

if (!isset($this->errors)) {

// filter
foreach ($this->elements as $name => $element) {
$filters = $element['filters'];
if (isset($filters)) {
foreach ((array) $filters as $filter) {
$this->values[$name] = $this->filter($filter, $this->values[$name]);
}
}
}
Valerie::fireHooks('onFormValidated', array(&$this, &$this->values));
return $this->values;
}
else {
Valerie::fireHooks('onFormInvalidated', array(&$this, &$this->values));
return false;
}
}

/*
Method: register

Registers a pattern to validate values against.

Arguments:

- $patterns - array of key/value pairs. The key is to be used in the form
definition. The value must also be an array containing a regex or function
and the message to send if the value is invalid.

Example:

$form->registerRules(array(
'required' => array('/^./', 'This field is required.')
));
*/

public function registerRules($patterns) {
$this->patterns = array_merge($this->patterns, $patterns);
}

/*
Method: registerFilters

Registers filters to be applied to values AFTER they have been validated.

Arguments:

- $filters - array of key/value pairs.

Example:

$form->registerFilters(array(
'striptags' => 'strip_tags'
));
*/

public function registerFilters($filters) {
$this->filters = array_merge($this->filters, $filters);
}

/*
Method: setResponse

Set a value to be sent back to form.

Arguments:

- $name - array of name/value pairs or string name
- $value - string value if $name is string
- $namespace - if this is set, values will be saved in an array with this
index
*/

public function setResponse($name, $value = null, $namespace = null) {
if (!is_array($name)) {
$name = array($name => $value);
}
else {
$namespace = $value;
}
if ($this->ajax) {
$container = &$this->response;
if (isset($namespace)) {
$container = &$this->response[$namespace];
}
}
else {
$container = &$_SESSION[$this->ns];
if (isset($namespace)) {
$container = &$_SESSION[$this->ns][$namespace];
}
}
if (!is_array($container)) {
$container = array();
}
foreach($name as $key => $value) {
if (strpos($key, ':') !== false) {
list($namespace, $key) = explode(':', $key, 2);
$container[$namespace][$key] = $value;
}
else {
$container[$key] = $value;
}
}
}

/*
Method: printResponse

Sends json back to client if ajax request.
*/

public function printResponse() {
if ($this->ajax) {
echo json_encode($this->response);
}
exit();
}

/*
Method: back

Sends the browser back to the form after submission if javascript is
disabled.
*/

public function back($message = null, $type = null) {
if ($message) {
$this->setResponse('form:message', $message);
}
if ($type) {
$this->setResponse('form:message_type', $type);
}
if (!$this->ajax) {
header("Location: {$this->referer}");
exit();
}
$this->printResponse();
}

/*
Method: goto

Sends the browser to the provided url.

Arguments:

- $url - url to redirect to
*/

public function goto($url) {
Valerie::fireHooks('beforeRedirect', array(&$this));
if ($this->ajax) {
$this->setResponse('goto', $url);
}
else {
header("Location: $url");
exit();
}
$this->printResponse();
}

/*
Method: isAjax

Returns:

- bool, true if ajax request
*/

public function isAjax() {
return $this->ajax;
}

/*
Method: getNameLabel

Returns a two-value array. If an array is passed in, first two values are
returned. Otherwise the array is populated with the string.

Arguments:

- $text - string or array

Returns:

- array
*/

public function getNameLabel($text) {
if (is_array($text)) {
return array($text[0], $text[1]);
}
else {
return array($text, $text);
}
}

/*
Method: getFormId

Returns the form id
*/

public function getFormId() {
return $this->definition['attributes']['id'];
}

/*
Method: getValue

Gets the form value(s).

Arguments:

- $name - name of field

Returns:

- string or array
*/

public function getValue($name) {
return $this->values[$name];
}

/*
Method: getRule

Gets the rule(s) associated with form field.

Arguments:

- $name - name of field

Returns:

- string or array
*/

public function getRule($name) {
return $this->rules[$name];
}

/*
Method: isEmpty

Determines if field value is empty.

Arguments:

- $val - value

Returns:

- bool
*/

public function isEmpty($val) {
return ($val == '' || $val == null);
}

/*
Method: format

Replaces n tokens ({1}, {2}, ...) with values.

Arguments:

- $template - string with tokens to be replaced
- $values - string value or array of values

Returns:

- string
*/

public function format($template, $values) {
if (is_array($values)) {
$replace = array();
foreach(array_values($values) as $index => $value) {
$replace[$index] = '{' . ($index + 1) . '}';
}
}
else {
$replace = '{1}';
}
return str_replace($replace, $values, $template);
}

/*
Method: test

Checks value against its validation rules.

Arguments:

- $rule - rule name
- $value - form value
- $name - form value name

Returns:

- bool, true on success, false on failure
*/

private function test($rule, $value, $name) {
//get rule arguments
if (is_array($rule)) {
$arguments = $rule;
list($rule) = array_keys($rule);
$arguments = $arguments[$rule];
}
else {
$arguments = null;
}

// return true if empty and not required
if ($this->isEmpty($value) && !$this->patterns[$rule][2]) {
return true;
}

$error = $this->patterns[$rule][1];

// check whether it's a function or a regex pattern
if (is_callable($this->patterns[$rule][0])) {
$success = call_user_func($this->patterns[$rule][0], array(
"name" => $name,
"value" => $value,
"arguments" => $arguments,
"error" => $error
), $this);
}
else {
$success = preg_match($this->patterns[$rule][0], $value);
}

if (is_array($success)) {
$error = $success[1];
$success = $success[0];
}

if (!$success) {
$this->errors[$name] = htmlspecialchars(strip_tags($error));
return false;
}
else return true;

}

/*
Method: filter

Filters values.

Arguments:

- $filter - filter name
- $values - value(s) to filter

Returns:

- string or array

*/

private function filter($filter, $values) {
// get arguments
if (is_array($filter)) {
$arguments = $filter;
list($filter) = array_keys($filter);
$arguments = $arguments[$filter];
}
else {
$arguments = null;
}

$fn = $this->filters[$filter];
$values = (array) $values;
foreach ($values as &$value) {
call_user_func($fn, $value, $arguments);
}
return $values;
}
}
?>
Show details Hide details

Change log

r102 by chrisjaure on Jul 06 (5 days ago)   Diff
Moved detecting loaded assets to form
class.
Go to: 
Project members, sign in to write a code review

Older revisions

r92 by chrisjaure on Jul 01, 2009   Diff
Renamed most hooks to be clearer.
r91 by chrisjaure on Jul 01, 2009   Diff
The settings, etc. are no longer
prefixed with valerie:.
r90 by chrisjaure on Jun 30, 2009   Diff
Revamped hooks. Started some plugins.
All revisions of this file

File info

Size: 13520 bytes, 606 lines
Hosted by Google Code