1+ <?php
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+
30+ class validate_datetime
31+ {
32+ /**
33+ * Check if the parsed datetime selectors are not set
34+ * to it's default value (zero).
35+ *
36+ * @param array $field_value
37+ * @return bool
38+ */
39+ function _datetime_selected ($ field_value )
40+ {
41+ if (is_array ($ field_value ) && count ($ field_value ) >= 5 )
42+ {
43+ $ result = array_filter ($ field_value , create_function ('$dt_unit ' , 'return ($dt_unit > 0); ' ));
44+ return (count ($ result ) !== 0 );
45+ }
46+
47+ return false ;
48+ }
49+
50+ /**
51+ * Check wordbook for the existence of an
52+ * error message. If not found will return
53+ * the parsed error message with line break.
54+ *
55+ * @param $errmsg
56+ * @return string
57+ */
58+ function _get_error ($ errmsg )
59+ {
60+ global $ app ;
61+
62+ $ errmsg = (isset ($ app ->tform ->wordbook [$ errmsg ])) ? $ app ->tform ->wordbook [$ errmsg ] : $ errmsg ;
63+ $ errmsg .= '<br /> ' . PHP_EOL ;
64+
65+ return $ errmsg ;
66+ }
67+
68+ /**
69+ * Helper function - filter the contents of the
70+ * selectors and return the resulting unix timestamp.
71+ *
72+ * @param $field_value
73+ * @return int Unix timestamp
74+ */
75+ function _get_timestamp_value ($ field_value )
76+ {
77+ $ second = 0 ;
78+ $ filtered_values = array_map (create_function ('$item ' ,'return (int)$item; ' ), $ field_value );
79+ extract ($ filtered_values , EXTR_OVERWRITE );
80+
81+ return mktime ($ hour , $ minute , $ second , $ month , $ day , $ year );
82+ }
83+
84+ /**
85+ * The minimum requirement to submit a datetime field
86+ * is to set the day, month and year values. Check that
87+ * these values are not zero (default).
88+ *
89+ * @param string $field_name
90+ * @param array $field_value
91+ * @param array $validator
92+ * @return string|void Error message if found
93+ */
94+ function not_empty ($ field_name , $ field_value , $ validator )
95+ {
96+ extract ($ field_value );
97+ if ( !($ day > 0 && $ month > 0 && $ year > 0 ) ) {
98+ return $ this ->_get_error ($ validator ['errmsg ' ]);
99+ }
100+ }
101+
102+ /**
103+ * Check that the selected datetime is in the future.
104+ *
105+ * @param string $field_name
106+ * @param array $field_value
107+ * @param array $validator
108+ * @return string|void Error message if found
109+ */
110+ function is_future ($ field_name , $ field_value , $ validator )
111+ {
112+ $ validator ['compare ' ] = mktime (date ('H ' ), (date ('i ' )-30 ), 0 , date ('m ' ), date ('d ' ), date ('Y ' )); // Turn back the clock 30 minutes for slow posters.
113+ return $ this ->is_greater ($ field_name , $ field_value , $ validator );
114+ }
115+
116+ /**
117+ * Compare the selected datetime to a timestamp
118+ * parsed via the validator array (key: compare).
119+ *
120+ * @param string $field_name
121+ * @param array $field_value
122+ * @param array $validator
123+ * @return string|void Error message if found
124+ */
125+ function is_greater ($ field_name , $ field_value , $ validator )
126+ {
127+ if ( !isset ($ validator ['compare ' ]) || !is_numeric ($ validator ['compare ' ]) || (date ('d/m/Y ' , $ validator ['compare ' ]) == '01/01/1970 ' ) ) {
128+ return $ this ->_get_error ('Could not find a unix timestamp to compare datetime with. ' );
129+ }
130+
131+ $ dt_stamp = $ this ->_get_timestamp_value ($ field_value );
132+ if ($ dt_stamp < $ validator ['compare ' ]) {
133+ return $ this ->_get_error ($ validator ['errmsg ' ]);
134+ }
135+ }
136+
137+
138+ }
0 commit comments