commit | author | age
|
31f6ce
|
1 |
<?php |
M |
2 |
/** |
|
3 |
* client_templates |
7fe908
|
4 |
* |
31f6ce
|
5 |
* @author Marius Cramer <m.cramer@pixcept.de> pixcept KG |
M |
6 |
* @author (original tools.inc.php) Till Brehm, projektfarm Gmbh |
|
7 |
* @author (original tools.inc.php) Oliver Vogel www.muv.com |
|
8 |
*/ |
7fe908
|
9 |
|
MC |
10 |
|
31f6ce
|
11 |
class client_templates { |
M |
12 |
|
7fe908
|
13 |
|
MC |
14 |
/** |
|
15 |
* - check for old-style templates and change to new style |
|
16 |
* - update assigned templates |
|
17 |
*/ |
7b47c0
|
18 |
function update_client_templates($clientId, $templates = array()) { |
7fe908
|
19 |
global $app, $conf; |
MC |
20 |
|
|
21 |
if(!is_array($templates)) return false; |
|
22 |
|
|
23 |
$new_tpl = array(); |
|
24 |
$used_assigned = array(); |
|
25 |
$needed_types = array(); |
|
26 |
$old_style = true; |
|
27 |
foreach($templates as $item) { |
|
28 |
$item = trim($item); |
|
29 |
if($item == '') continue; |
|
30 |
|
|
31 |
$tpl_id = 0; |
|
32 |
$assigned_id = 0; |
|
33 |
if(strpos($item, ':') === false) { |
|
34 |
$tpl_id = $item; |
|
35 |
} else { |
|
36 |
$old_style = false; // has new-style assigns |
|
37 |
list($assigned_id, $tpl_id) = explode(':', $item, 2); |
|
38 |
if(substr($assigned_id, 0, 1) === 'n') $assigned_id = 0; // newly inserted items |
|
39 |
} |
|
40 |
if(array_key_exists($tpl_id, $needed_types) == false) $needed_types[$tpl_id] = 0; |
|
41 |
$needed_types[$tpl_id]++; |
|
42 |
|
|
43 |
if($assigned_id > 0) { |
|
44 |
$used_assigned[] = $assigned_id; // for comparison with database |
|
45 |
} else { |
|
46 |
$new_tpl[] = $tpl_id; |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
if($old_style == true) { |
|
51 |
// we have to take care of this in an other way |
cc7a82
|
52 |
$in_db = $app->db->queryAllRecords('SELECT `assigned_template_id`, `client_template_id` FROM `client_template_assigned` WHERE `client_id` = ?', $clientId); |
7fe908
|
53 |
if(is_array($in_db) && count($in_db) > 0) { |
MC |
54 |
foreach($in_db as $item) { |
|
55 |
if(array_key_exists($item['client_template_id'], $needed_types) == false) $needed_types[$item['client_template_id']] = 0; |
|
56 |
$needed_types[$item['client_template_id']]--; |
|
57 |
} |
|
58 |
} |
|
59 |
|
|
60 |
foreach($needed_types as $tpl_id => $count) { |
|
61 |
if($count > 0) { |
|
62 |
// add new template to client (includes those from old-style without assigned_template_id) |
|
63 |
for($i = $count; $i > 0; $i--) { |
cc7a82
|
64 |
$app->db->query('INSERT INTO `client_template_assigned` (`client_id`, `client_template_id`) VALUES (?, ?)', $clientId, $tpl_id); |
7fe908
|
65 |
} |
MC |
66 |
} elseif($count < 0) { |
|
67 |
// remove old ones |
|
68 |
for($i = $count; $i < 0; $i++) { |
cc7a82
|
69 |
$app->db->query('DELETE FROM `client_template_assigned` WHERE client_id = ? AND client_template_id = ? LIMIT 1', $clientId, $tpl_id); |
7fe908
|
70 |
} |
MC |
71 |
} |
|
72 |
} |
|
73 |
} else { |
|
74 |
// we have to take care of this in an other way |
cc7a82
|
75 |
$in_db = $app->db->queryAllRecords('SELECT `assigned_template_id`, `client_template_id` FROM `client_template_assigned` WHERE `client_id` = ?', $clientId); |
7fe908
|
76 |
if(is_array($in_db) && count($in_db) > 0) { |
MC |
77 |
// check which templates were removed from this client |
|
78 |
foreach($in_db as $item) { |
|
79 |
if(in_array($item['assigned_template_id'], $used_assigned) == false) { |
|
80 |
// delete this one |
cc7a82
|
81 |
$app->db->query('DELETE FROM `client_template_assigned` WHERE `assigned_template_id` = ?', $item['assigned_template_id']); |
7fe908
|
82 |
} |
MC |
83 |
} |
|
84 |
} |
|
85 |
|
|
86 |
if(count($new_tpl) > 0) { |
|
87 |
foreach($new_tpl as $item) { |
|
88 |
// add new template to client (includes those from old-style without assigned_template_id) |
cc7a82
|
89 |
$app->db->query('INSERT INTO `client_template_assigned` (`client_id`, `client_template_id`) VALUES (?, ?)', $clientId, $item); |
7fe908
|
90 |
} |
MC |
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
unset($new_tpl); |
|
95 |
unset($in_db); |
|
96 |
unset($templates); |
|
97 |
unset($used_assigned); |
|
98 |
return true; |
|
99 |
} |
|
100 |
|
|
101 |
function apply_client_templates($clientId) { |
|
102 |
global $app; |
|
103 |
|
|
104 |
include '../client/form/client.tform.php'; |
|
105 |
|
|
106 |
/* |
31f6ce
|
107 |
* Get the master-template for the client |
M |
108 |
*/ |
cc7a82
|
109 |
$sql = "SELECT template_master, template_additional,limit_client FROM client WHERE client_id = ?"; |
MC |
110 |
$record = $app->db->queryOneRecord($sql, $clientId); |
7fe908
|
111 |
$masterTemplateId = $record['template_master']; |
f50e0e
|
112 |
$is_reseller = ($record['limit_client'] != 0)?true:false; |
7fe908
|
113 |
|
MC |
114 |
if($record['template_additional'] != '') { |
|
115 |
// we have to call the update_client_templates function |
|
116 |
$templates = explode('/', $record['template_additional']); |
|
117 |
$this->update_client_templates($clientId, $templates); |
cc7a82
|
118 |
$app->db->query('UPDATE `client` SET `template_additional` = \'\' WHERE `client_id` = ?', $clientId); |
7fe908
|
119 |
} |
MC |
120 |
|
|
121 |
/* |
31f6ce
|
122 |
* if the master-Template is custom there is NO changing |
M |
123 |
*/ |
7fe908
|
124 |
if ($masterTemplateId > 0){ |
cc7a82
|
125 |
$sql = "SELECT * FROM client_template WHERE template_id = ?"; |
MC |
126 |
$limits = $app->db->queryOneRecord($sql, $masterTemplateId); |
7fe908
|
127 |
} else { |
MC |
128 |
// if there is no master template it makes NO SENSE adding sub templates. |
|
129 |
// adding subtemplates are stored in client limits, so they would add up |
|
130 |
// on every save action for the client -> too high limits! |
|
131 |
return; |
|
132 |
} |
31f6ce
|
133 |
|
7fe908
|
134 |
/* |
31f6ce
|
135 |
* Process the additional tempaltes here (add them to the limits |
M |
136 |
* if != -1) |
|
137 |
*/ |
7fe908
|
138 |
$addTpl = explode('/', $additionalTemplateStr); |
cc7a82
|
139 |
$addTpls = $app->db->queryAllRecords('SELECT `client_template_id` FROM `client_template_assigned` WHERE `client_id` = ?', $clientId); |
7fe908
|
140 |
foreach ($addTpls as $addTpl){ |
MC |
141 |
$item = $addTpl['client_template_id']; |
cc7a82
|
142 |
$sql = "SELECT * FROM client_template WHERE template_id = ?"; |
MC |
143 |
$addLimits = $app->db->queryOneRecord($sql, $item); |
7fe908
|
144 |
$app->log('Template processing subtemplate ' . $item . ' for client ' . $clientId, LOGLEVEL_DEBUG); |
MC |
145 |
/* maybe the template is deleted in the meantime */ |
|
146 |
if (is_array($addLimits)){ |
|
147 |
foreach($addLimits as $k => $v){ |
|
148 |
/* we can remove this condition, but it is easier to debug with it (don't add ids and other non-limit values) */ |
a1db68
|
149 |
if (strpos($k, 'limit') !== false or strpos($k, 'default') !== false or $k == 'ssh_chroot' or $k == 'web_php_options' or $k == 'force_suexec'){ |
7fe908
|
150 |
$app->log('Template processing key ' . $k . ' for client ' . $clientId, LOGLEVEL_DEBUG); |
5d0839
|
151 |
|
7fe908
|
152 |
/* process the numerical limits */ |
MC |
153 |
if (is_numeric($v)){ |
|
154 |
/* switch for special cases */ |
|
155 |
switch ($k){ |
|
156 |
case 'limit_cron_frequency': |
|
157 |
if ($v < $limits[$k]) $limits[$k] = $v; |
|
158 |
/* silent adjustment of the minimum cron frequency to 1 minute */ |
|
159 |
/* maybe this control test should be done via validator definition in tform.php file, but I don't know how */ |
|
160 |
if ($limits[$k] < 1) $limits[$k] = 1; |
a1db68
|
161 |
break; |
SC |
162 |
|
|
163 |
case 'default_mailserver': |
|
164 |
case 'default_webserver': |
|
165 |
case 'default_dnsserver': |
|
166 |
case 'default_slave_dnsserver': |
|
167 |
case 'default_dbserver': |
|
168 |
/* additional templates don't override default server from main template */ |
|
169 |
if ($limits[$k] == 0) $limits[$k] = $v; |
7fe908
|
170 |
break; |
31f6ce
|
171 |
|
7fe908
|
172 |
default: |
MC |
173 |
if ($limits[$k] > -1){ |
|
174 |
if ($v == -1){ |
|
175 |
$limits[$k] = -1; |
|
176 |
} |
|
177 |
else { |
|
178 |
$limits[$k] += $v; |
|
179 |
} |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
/* process the string limits (CHECKBOXARRAY, SELECT etc.) */ |
|
184 |
elseif (is_string($v)){ |
|
185 |
switch ($form["tabs"]["limits"]["fields"][$k]['formtype']){ |
|
186 |
case 'CHECKBOXARRAY': |
|
187 |
if (!isset($limits[$k])){ |
|
188 |
$limits[$k] = array(); |
|
189 |
} |
7b47c0
|
190 |
|
7fe908
|
191 |
$limits_values = $limits[$k]; |
MC |
192 |
if (is_string($limits[$k])){ |
|
193 |
$limits_values = explode($form["tabs"]["limits"]["fields"][$k]["separator"], $limits[$k]); |
|
194 |
} |
|
195 |
$additional_values = explode($form["tabs"]["limits"]["fields"][$k]["separator"], $v); |
|
196 |
$app->log('Template processing key ' . $k . ' type CHECKBOXARRAY, lim / add: ' . implode(',', $limits_values) . ' / ' . implode(',', $additional_values) . ' for client ' . $clientId, LOGLEVEL_DEBUG); |
|
197 |
/* unification of limits_values (master template) and additional_values (additional template) */ |
|
198 |
$limits_unified = array(); |
|
199 |
foreach($form["tabs"]["limits"]["fields"][$k]["value"] as $key => $val){ |
|
200 |
if (in_array($key, $limits_values) || in_array($key, $additional_values)) $limits_unified[] = $key; |
|
201 |
} |
|
202 |
$limits[$k] = implode($form["tabs"]["limits"]["fields"][$k]["separator"], $limits_unified); |
|
203 |
break; |
|
204 |
case 'CHECKBOX': |
|
205 |
if($k == 'force_suexec') { |
|
206 |
// 'n' is less limited than y |
|
207 |
if (!isset($limits[$k])){ |
|
208 |
$limits[$k] = 'y'; |
|
209 |
} |
|
210 |
if($limits[$k] == 'n' || $v == 'n') $limits[$k] = 'n'; |
|
211 |
} else { |
|
212 |
// 'y' is less limited than n |
|
213 |
if (!isset($limits[$k])){ |
|
214 |
$limits[$k] = 'n'; |
|
215 |
} |
|
216 |
if($limits[$k] == 'y' || $v == 'y') $limits[$k] = 'y'; |
|
217 |
} |
|
218 |
break; |
|
219 |
case 'SELECT': |
|
220 |
$limit_values = array_keys($form["tabs"]["limits"]["fields"][$k]["value"]); |
|
221 |
/* choose the lower index of the two SELECT items */ |
|
222 |
$limits[$k] = $limit_values[min(array_search($limits[$k], $limit_values), array_search($v, $limit_values))]; |
|
223 |
break; |
|
224 |
} |
|
225 |
} |
|
226 |
} |
|
227 |
} |
|
228 |
} |
|
229 |
} |
31f6ce
|
230 |
|
7fe908
|
231 |
/* |
31f6ce
|
232 |
* Write all back to the database |
M |
233 |
*/ |
7fe908
|
234 |
$update = ''; |
cc7a82
|
235 |
$update_values = array(); |
94c961
|
236 |
if(!$is_reseller) unset($limits['limit_client']); // Only Resellers may have limit_client set in template to ensure that we do not convert a client to reseller accidently. |
7fe908
|
237 |
foreach($limits as $k => $v){ |
a1db68
|
238 |
if (strpos($k, 'default') !== false and $v == 0) { |
SC |
239 |
continue; // template doesn't define default server, client's default musn't be changed |
|
240 |
} |
|
241 |
if ((strpos($k, 'limit') !== false or strpos($k, 'default') !== false or $k == 'ssh_chroot' or $k == 'web_php_options' or $k == 'force_suexec') && !is_array($v)){ |
7fe908
|
242 |
if ($update != '') $update .= ', '; |
cc7a82
|
243 |
$update .= '?? = ?'; |
MC |
244 |
$update_values[] = $k; |
|
245 |
$update_values[] = $v; |
7fe908
|
246 |
} |
MC |
247 |
} |
cc7a82
|
248 |
$update_values[] = $clientId; |
7fe908
|
249 |
$app->log('Template processed for client ' . $clientId . ', update string: ' . $update, LOGLEVEL_DEBUG); |
MC |
250 |
if($update != '') { |
cc7a82
|
251 |
$sql = 'UPDATE client SET ' . $update . " WHERE client_id = ?"; |
MC |
252 |
$app->db->query($sql, true, $update_values); |
7fe908
|
253 |
} |
MC |
254 |
unset($form); |
|
255 |
} |
|
256 |
|
|
257 |
} |