commit | author | age
|
0b0dc9
|
1 |
<?php |
M |
2 |
|
|
3 |
/* |
|
4 |
Copyright (c) 2007, Till Brehm, projektfarm Gmbh |
|
5 |
Modified 2009, Marius Cramer, pixcept KG |
|
6 |
All rights reserved. |
|
7 |
|
|
8 |
Redistribution and use in source and binary forms, with or without modification, |
|
9 |
are permitted provided that the following conditions are met: |
|
10 |
|
|
11 |
* Redistributions of source code must retain the above copyright notice, |
|
12 |
this list of conditions and the following disclaimer. |
|
13 |
* Redistributions in binary form must reproduce the above copyright notice, |
|
14 |
this list of conditions and the following disclaimer in the documentation |
|
15 |
and/or other materials provided with the distribution. |
|
16 |
* Neither the name of ISPConfig nor the names of its contributors |
|
17 |
may be used to endorse or promote products derived from this software without |
|
18 |
specific prior written permission. |
|
19 |
|
|
20 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
21 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
22 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
23 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
24 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
25 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
26 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
27 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
29 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 |
*/ |
|
31 |
|
|
32 |
class validate_cron { |
7fe908
|
33 |
|
MC |
34 |
function get_error($errmsg) { |
|
35 |
global $app; |
|
36 |
|
|
37 |
if(isset($app->tform->wordbook[$errmsg])) { |
|
38 |
return $app->tform->wordbook[$errmsg]."<br>\r\n"; |
|
39 |
} else { |
|
40 |
return $errmsg."<br>\r\n"; |
|
41 |
} |
|
42 |
} |
|
43 |
|
|
44 |
/* |
0b0dc9
|
45 |
Validator function to check if a given cron command is in correct form (url only). |
M |
46 |
*/ |
7fe908
|
47 |
function command_format($field_name, $field_value, $validator) { |
MC |
48 |
if(preg_match("'^(\w+):\/\/'", $field_value, $matches)) { |
|
49 |
|
|
50 |
$parsed = parse_url($field_value); |
|
51 |
if($parsed === false) return $this->get_error($validator['errmsg']); |
|
52 |
|
|
53 |
if($parsed["scheme"] != "http" && $parsed["scheme"] != "https") return $this->get_error($validator['errmsg']); |
|
54 |
|
336368
|
55 |
if(preg_match("'^([a-z0-9][a-z0-9\-]{0,62}\.)+([A-Za-z0-9\-]{2,30})$'i", $parsed["host"]) == false) return $this->get_error($validator['errmsg']); |
7fe908
|
56 |
} |
MC |
57 |
} |
|
58 |
|
3fd659
|
59 |
function run_month_format($field_name, $field_value, $validator) { |
T |
60 |
global $app; |
|
61 |
//* allow value @reboot in month field |
|
62 |
if($field_value != '@reboot') { |
|
63 |
return $this->run_time_format($field_name, $field_value, $validator); |
|
64 |
} |
|
65 |
} |
7fe908
|
66 |
|
0b0dc9
|
67 |
/* |
M |
68 |
Validator function to check if a given cron time is in correct form. |
|
69 |
*/ |
|
70 |
function run_time_format($field_name, $field_value, $validator) { |
|
71 |
global $app; |
7fe908
|
72 |
|
MC |
73 |
//* check general form |
|
74 |
$is_ok = true; |
|
75 |
$field_value = str_replace(" ", "", $field_value); // spaces are not needed |
|
76 |
$used_times = array(); |
|
77 |
|
|
78 |
if(preg_match("'^[0-9\-\,\/\*]+$'", $field_value) == false) return $this->get_error($validator['errmsg']); // allowed characters are 0-9, comma, *, -, / |
|
79 |
elseif(preg_match("'[\-\,\/][\-\,\/]'", $field_value) == true) return $this->get_error($validator['errmsg']); // comma, - and / never stand together |
|
80 |
//* now split list and check each entry. store used values in array for later limit-check |
|
81 |
$time_list = explode(",", $field_value); |
|
82 |
if(count($time_list) < 1) return $this->get_error($validator['errmsg']); |
|
83 |
|
|
84 |
$max_entry = 0; |
|
85 |
$min_entry = 0; |
|
86 |
$in_minutes = 1; |
|
87 |
//* get maximum value of entry for each field type (name) |
|
88 |
switch($field_name) { |
|
89 |
case "run_min": |
|
90 |
$max_entry = 59; |
|
91 |
break; |
|
92 |
case "run_hour": |
|
93 |
$max_entry = 23; |
|
94 |
$in_minutes = 60; |
|
95 |
break; |
|
96 |
case "run_mday": |
|
97 |
$max_entry = 31; |
|
98 |
$min_entry = 1; |
|
99 |
$in_minutes = 1440; |
|
100 |
break; |
|
101 |
case "run_month": |
|
102 |
$max_entry = 12; |
|
103 |
$min_entry = 1; |
|
104 |
$in_minutes = 1440 * 28; // not exactly but enough |
|
105 |
break; |
|
106 |
case "run_wday": |
|
107 |
$max_entry = 7; |
|
108 |
$in_minutes = 1440; |
|
109 |
break; |
|
110 |
} |
|
111 |
|
|
112 |
if($max_entry == 0) return $this->get_error('unknown_fieldtype_error'); |
|
113 |
|
|
114 |
foreach($time_list as $entry) { |
|
115 |
//* possible value combinations: |
|
116 |
//* x => ^(\d+)$ |
|
117 |
//* x-y => ^(\d+)\-(\d+)$ |
|
118 |
//* x/y => ^(\d+)\/([1-9]\d*)$ |
|
119 |
//* x-y/z => ^(\d+)\-(\d+)\/([1-9]\d*)$ |
|
120 |
//* */x => ^\*\/([1-9]\d*)$ |
|
121 |
//* combined regex => ^(\d+|\*)(\-(\d+))?(\/([1-9]\d*))?$ |
|
122 |
|
|
123 |
if(preg_match("'^(((\d+)(\-(\d+))?)|\*)(\/([1-9]\d*))?$'", $entry, $matches) == false) { |
|
124 |
return $this->get_error($validator['errmsg']); |
|
125 |
} |
|
126 |
|
|
127 |
//* matches contains: |
|
128 |
//* 1 => * or value or x-y range |
|
129 |
//* 2 => unused |
|
130 |
//* 3 => value if [1] != * |
|
131 |
//* 4 => empty if no range was used |
|
132 |
//* 5 => 2nd value of range if [1] != * and range was used |
|
133 |
//* 6 => empty if step was not used |
|
134 |
//* 7 => step |
|
135 |
|
|
136 |
$loop_step = 1; |
|
137 |
$loop_from = $min_entry; |
|
138 |
$loop_to = $max_entry; |
|
139 |
|
|
140 |
//* calculate used values |
|
141 |
if($matches[1] == "*") { |
|
142 |
//* not to check |
|
143 |
} else { |
|
144 |
if($matches[3] < $min_entry || $matches[3] > $max_entry) { |
|
145 |
//* check if value is in allowed range |
|
146 |
return $this->get_error($validator['errmsg']); |
|
147 |
} elseif($matches[4] && ($matches[5] < $min_entry || $matches[5] > $max_entry || $matches[5] <= $matches[3])) { |
|
148 |
//* check if value is in allowed range and not less or equal to first value |
|
149 |
return $this->get_error($validator['errmsg']); |
|
150 |
} |
|
151 |
|
|
152 |
$loop_from = $matches[3]; |
|
153 |
$loop_to = $matches[3]; |
|
154 |
if($matches[4]) $loop_to = $matches[5]; |
|
155 |
} |
|
156 |
if($matches[6] && ($matches[7] < 2 || $matches[7] > $max_entry - 1)) { |
|
157 |
//* check if step value is valid |
|
158 |
return $this->get_error($validator['errmsg']); |
|
159 |
} |
|
160 |
if($matches[7]) $loop_step = $matches[7]; |
|
161 |
|
|
162 |
//* loop through values to set used times |
|
163 |
for($t = $loop_from; $t <= $loop_to; $t = $t + $loop_step) { |
|
164 |
$used_times[] = $t; |
|
165 |
} |
|
166 |
} //* end foreach entry loop |
|
167 |
|
|
168 |
//* sort used times and erase doubles |
|
169 |
sort($used_times); |
|
170 |
$used_times = array_unique($used_times); |
|
171 |
|
|
172 |
//* get minimum frequency and store it in $app->tform->cron_min_freq for usage in onUpdateSave and onInsertSave! |
|
173 |
$min_freq = -1; |
|
174 |
$prev_time = -1; |
|
175 |
foreach($used_times as $curtime) { |
|
176 |
if($prev_time != -1) { |
|
177 |
$freq = $curtime - $prev_time; |
|
178 |
if($min_freq == -1 || $freq < $min_freq) $min_freq = $freq; |
|
179 |
} |
|
180 |
$prev_time = $curtime; |
|
181 |
} |
|
182 |
|
|
183 |
//* check last against first (needed because e.g. wday 1,4,7 has diff 1 not 3 |
|
184 |
$prev_time = $used_times[0]; |
|
185 |
$freq = ($prev_time - $min_entry) + ($max_entry - $curtime) + 1; |
|
186 |
if($min_freq == -1 || $freq < $min_freq) $min_freq = $freq; |
|
187 |
|
|
188 |
if($min_freq > 0 && $min_freq <= $max_entry) { //* only store if > 1 && < $max_entry! |
|
189 |
$min_freq = $min_freq * $in_minutes; // we have to overwrite $app->tform->cron_min_freq if this is higher value |
|
190 |
if(!$app->tform->cron_min_freq || $app->tform->cron_min_freq > $min_freq) $app->tform->cron_min_freq = $min_freq; |
|
191 |
} |
|
192 |
|
|
193 |
//return "DEBUG: " . $app->tform->cron_min_freq . " ($min_freq) --- " . var_export($used_times, true) . "<br />"; |
0b0dc9
|
194 |
} |
7fe908
|
195 |
|
MC |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
} |