commit | author | age
|
fcc698
|
1 |
<?php |
W |
2 |
/* |
|
3 |
Copyright (c) 2009, Scott Barr <gsbarr@gmail.com> |
|
4 |
All rights reserved. |
|
5 |
|
|
6 |
Redistribution and use in source and binary forms, with or without modification, |
|
7 |
are permitted provided that the following conditions are met: |
|
8 |
|
|
9 |
* Redistributions of source code must retain the above copyright notice, |
|
10 |
this list of conditions and the following disclaimer. |
|
11 |
* Redistributions in binary form must reproduce the above copyright notice, |
|
12 |
this list of conditions and the following disclaimer in the documentation |
|
13 |
and/or other materials provided with the distribution. |
|
14 |
* Neither the name of ISPConfig nor the names of its contributors |
|
15 |
may be used to endorse or promote products derived from this software without |
|
16 |
specific prior written permission. |
|
17 |
|
|
18 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
19 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
20 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
21 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
22 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
23 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
24 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
25 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
26 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
27 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
28 |
*/ |
|
29 |
|
b1a6a5
|
30 |
class validate_datetime |
fcc698
|
31 |
{ |
W |
32 |
/** |
|
33 |
* Check if the parsed datetime selectors are not set |
|
34 |
* to it's default value (zero). |
b1a6a5
|
35 |
* |
fcc698
|
36 |
* @param array $field_value |
W |
37 |
* @return bool |
|
38 |
*/ |
b1a6a5
|
39 |
|
MC |
40 |
|
fcc698
|
41 |
function _datetime_selected($field_value) |
W |
42 |
{ |
b1a6a5
|
43 |
if (is_array($field_value) && count($field_value) >= 5) |
fcc698
|
44 |
{ |
W |
45 |
$result = array_filter($field_value, create_function('$dt_unit', 'return ($dt_unit > 0);')); |
b1a6a5
|
46 |
return count($result) !== 0; |
fcc698
|
47 |
} |
b1a6a5
|
48 |
|
fcc698
|
49 |
return false; |
W |
50 |
} |
b1a6a5
|
51 |
|
MC |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
fcc698
|
56 |
/** |
W |
57 |
* Check wordbook for the existence of an |
|
58 |
* error message. If not found will return |
|
59 |
* the parsed error message with line break. |
b1a6a5
|
60 |
* |
fcc698
|
61 |
* @param $errmsg |
W |
62 |
* @return string |
|
63 |
*/ |
|
64 |
function _get_error($errmsg) |
|
65 |
{ |
|
66 |
global $app; |
b1a6a5
|
67 |
|
fcc698
|
68 |
$errmsg = (isset($app->tform->wordbook[$errmsg])) ? $app->tform->wordbook[$errmsg] : $errmsg; |
W |
69 |
$errmsg .= '<br />' . PHP_EOL; |
b1a6a5
|
70 |
|
fcc698
|
71 |
return $errmsg; |
W |
72 |
} |
b1a6a5
|
73 |
|
MC |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
fcc698
|
78 |
/** |
b1a6a5
|
79 |
* Helper function - filter the contents of the |
fcc698
|
80 |
* selectors and return the resulting unix timestamp. |
b1a6a5
|
81 |
* |
fcc698
|
82 |
* @param $field_value |
W |
83 |
* @return int Unix timestamp |
|
84 |
*/ |
|
85 |
function _get_timestamp_value($field_value) |
|
86 |
{ |
41a74e
|
87 |
if(!is_array($field_value)) return 0; |
b1a6a5
|
88 |
$second = 0; |
MC |
89 |
$filtered_values = array_map(create_function('$item', 'return (int)$item;'), $field_value); |
fcc698
|
90 |
extract($filtered_values, EXTR_OVERWRITE); |
b1a6a5
|
91 |
|
fcc698
|
92 |
return mktime($hour, $minute, $second, $month, $day, $year); |
W |
93 |
} |
b1a6a5
|
94 |
|
MC |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
fcc698
|
99 |
/** |
W |
100 |
* The minimum requirement to submit a datetime field |
|
101 |
* is to set the day, month and year values. Check that |
|
102 |
* these values are not zero (default). |
b1a6a5
|
103 |
* |
fcc698
|
104 |
* @param string $field_name |
W |
105 |
* @param array $field_value |
|
106 |
* @param array $validator |
|
107 |
* @return string|void Error message if found |
|
108 |
*/ |
|
109 |
function not_empty($field_name, $field_value, $validator) |
|
110 |
{ |
b1a6a5
|
111 |
if(!is_array($field_value)) return $this->_get_error($validator['errmsg']); |
fcc698
|
112 |
extract($field_value); |
W |
113 |
if ( !($day > 0 && $month > 0 && $year > 0) ) { |
|
114 |
return $this->_get_error($validator['errmsg']); |
|
115 |
} |
|
116 |
} |
b1a6a5
|
117 |
|
MC |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
fcc698
|
122 |
/** |
W |
123 |
* Check that the selected datetime is in the future. |
b1a6a5
|
124 |
* |
fcc698
|
125 |
* @param string $field_name |
W |
126 |
* @param array $field_value |
|
127 |
* @param array $validator |
|
128 |
* @return string|void Error message if found |
|
129 |
*/ |
b1a6a5
|
130 |
function is_future($field_name, $field_value, $validator) |
fcc698
|
131 |
{ |
b1a6a5
|
132 |
$validator['compare'] = mktime(date('H'), (date('i')-30), 0, date('m'), date('d'), date('Y')); // Turn back the clock 30 minutes for slow posters. |
fcc698
|
133 |
return $this->is_greater($field_name, $field_value, $validator); |
W |
134 |
} |
b1a6a5
|
135 |
|
MC |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
fcc698
|
140 |
/** |
W |
141 |
* Compare the selected datetime to a timestamp |
|
142 |
* parsed via the validator array (key: compare). |
b1a6a5
|
143 |
* |
fcc698
|
144 |
* @param string $field_name |
W |
145 |
* @param array $field_value |
|
146 |
* @param array $validator |
|
147 |
* @return string|void Error message if found |
|
148 |
*/ |
|
149 |
function is_greater($field_name, $field_value, $validator) |
|
150 |
{ |
|
151 |
if ( !isset($validator['compare']) || !is_numeric($validator['compare']) || (date('d/m/Y', $validator['compare']) == '01/01/1970') ) { |
|
152 |
return $this->_get_error('Could not find a unix timestamp to compare datetime with.'); |
|
153 |
} |
b1a6a5
|
154 |
|
fcc698
|
155 |
$dt_stamp = $this->_get_timestamp_value($field_value); |
W |
156 |
if ($dt_stamp < $validator['compare']) { |
|
157 |
return $this->_get_error($validator['errmsg']); |
|
158 |
} |
|
159 |
} |
b1a6a5
|
160 |
|
MC |
161 |
|
|
162 |
} |