Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
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         /*
31f6ce 105          * Get the master-template for the client
M 106          */
cc7a82 107         $sql = "SELECT template_master, template_additional,limit_client FROM client WHERE client_id = ?";
MC 108         $record = $app->db->queryOneRecord($sql, $clientId);
7fe908 109         $masterTemplateId = $record['template_master'];
f50e0e 110         $is_reseller = ($record['limit_client'] != 0)?true:false;
33e9d6 111
MB 112         include '../client/form/' . ($is_reseller ? 'reseller' : 'client') . '.tform.php';
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);
33e9d6 127             if($is_reseller == true && $limits['limit_client'] == 0) $limits['limit_client'] = -1;
MB 128             elseif($is_reseller == false && $limits['limit_client'] != 0) $limits['limit_client'] = 0;
7fe908 129         } else {
MC 130             // if there is no master template it makes NO SENSE adding sub templates.
131             // adding subtemplates are stored in client limits, so they would add up
132             // on every save action for the client -> too high limits!
133             return;
134         }
31f6ce 135
7fe908 136         /*
33e9d6 137          * Process the additional templates here (add them to the limits
31f6ce 138          * if != -1)
M 139          */
7fe908 140         $addTpl = explode('/', $additionalTemplateStr);
cc7a82 141         $addTpls = $app->db->queryAllRecords('SELECT `client_template_id` FROM `client_template_assigned` WHERE `client_id` = ?', $clientId);
7fe908 142         foreach ($addTpls as $addTpl){
MC 143             $item = $addTpl['client_template_id'];
cc7a82 144             $sql = "SELECT * FROM client_template WHERE template_id = ?";
MC 145             $addLimits = $app->db->queryOneRecord($sql, $item);
7fe908 146             $app->log('Template processing subtemplate ' . $item . ' for client ' . $clientId, LOGLEVEL_DEBUG);
MC 147             /* maybe the template is deleted in the meantime */
148             if (is_array($addLimits)){
149                 foreach($addLimits as $k => $v){
33e9d6 150                     if($k == 'limit_client') {
MB 151                         if($is_reseller == true && $v == 0) continue;
152                         elseif($is_reseller == false && $v != 0) continue;
153                     }
154                     
7fe908 155                     /* we can remove this condition, but it is easier to debug with it (don't add ids and other non-limit values) */
a1db68 156                     if (strpos($k, 'limit') !== false or strpos($k, 'default') !== false or $k == 'ssh_chroot' or $k == 'web_php_options' or $k == 'force_suexec'){
7fe908 157                         $app->log('Template processing key ' . $k . ' for client ' . $clientId, LOGLEVEL_DEBUG);
5d0839 158
7fe908 159                         /* process the numerical limits */
MC 160                         if (is_numeric($v)){
161                             /* switch for special cases */
162                             switch ($k){
163                             case 'limit_cron_frequency':
164                                 if ($v < $limits[$k]) $limits[$k] = $v;
165                                 /* silent adjustment of the minimum cron frequency to 1 minute */
166                                 /* maybe this control test should be done via validator definition in tform.php file, but I don't know how */
167                                 if ($limits[$k] < 1) $limits[$k] = 1;
a1db68 168                                 break;
SC 169
170                             case 'default_mailserver':
171                             case 'default_webserver':
172                             case 'default_dnsserver':
173                             case 'default_slave_dnsserver':
174                             case 'default_dbserver':
175                                 /* additional templates don't override default server from main template */
176                                 if ($limits[$k] == 0) $limits[$k] = $v;
7fe908 177                                 break;
31f6ce 178
7fe908 179                             default:
MC 180                                 if ($limits[$k] > -1){
181                                     if ($v == -1){
182                                         $limits[$k] = -1;
183                                     }
184                                     else {
185                                         $limits[$k] += $v;
186                                     }
187                                 }
188                             }
189                         }
190                         /* process the string limits (CHECKBOXARRAY, SELECT etc.) */
191                         elseif (is_string($v)){
192                             switch ($form["tabs"]["limits"]["fields"][$k]['formtype']){
193                             case 'CHECKBOXARRAY':
194                                 if (!isset($limits[$k])){
195                                     $limits[$k] = array();
196                                 }
7b47c0 197
7fe908 198                                 $limits_values = $limits[$k];
MC 199                                 if (is_string($limits[$k])){
200                                     $limits_values = explode($form["tabs"]["limits"]["fields"][$k]["separator"], $limits[$k]);
201                                 }
202                                 $additional_values = explode($form["tabs"]["limits"]["fields"][$k]["separator"], $v);
203                                 $app->log('Template processing key ' . $k . ' type CHECKBOXARRAY, lim / add: ' . implode(',', $limits_values) . ' / ' . implode(',', $additional_values) . ' for client ' . $clientId, LOGLEVEL_DEBUG);
204                                 /* unification of limits_values (master template) and additional_values (additional template) */
205                                 $limits_unified = array();
206                                 foreach($form["tabs"]["limits"]["fields"][$k]["value"] as $key => $val){
207                                     if (in_array($key, $limits_values) || in_array($key, $additional_values)) $limits_unified[] = $key;
208                                 }
209                                 $limits[$k] = implode($form["tabs"]["limits"]["fields"][$k]["separator"], $limits_unified);
210                                 break;
211                             case 'CHECKBOX':
212                                 if($k == 'force_suexec') {
213                                     // 'n' is less limited than y
214                                     if (!isset($limits[$k])){
215                                         $limits[$k] = 'y';
216                                     }
217                                     if($limits[$k] == 'n' || $v == 'n') $limits[$k] = 'n';
218                                 } else {
219                                     // 'y' is less limited than n
220                                     if (!isset($limits[$k])){
221                                         $limits[$k] = 'n';
222                                     }
223                                     if($limits[$k] == 'y' || $v == 'y') $limits[$k] = 'y';
224                                 }
225                                 break;
226                             case 'SELECT':
227                                 $limit_values = array_keys($form["tabs"]["limits"]["fields"][$k]["value"]);
228                                 /* choose the lower index of the two SELECT items */
229                                 $limits[$k] = $limit_values[min(array_search($limits[$k], $limit_values), array_search($v, $limit_values))];
230                                 break;
231                             }
232                         }
233                     }
234                 }
235             }
236         }
31f6ce 237
7fe908 238         /*
31f6ce 239          * Write all back to the database
M 240          */
7fe908 241         $update = '';
cc7a82 242         $update_values = array();
94c961 243         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 244         foreach($limits as $k => $v){
a1db68 245             if (strpos($k, 'default') !== false and $v == 0) {
SC 246                 continue; // template doesn't define default server, client's default musn't be changed
247             }
248             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 249                 if ($update != '') $update .= ', ';
cc7a82 250                 $update .= '?? = ?';
MC 251                 $update_values[] = $k;
252                 $update_values[] = $v;
7fe908 253             }
MC 254         }
cc7a82 255         $update_values[] = $clientId;
7fe908 256         $app->log('Template processed for client ' . $clientId . ', update string: ' . $update, LOGLEVEL_DEBUG);
MC 257         if($update != '') {
cc7a82 258             $sql = 'UPDATE client SET ' . $update . " WHERE client_id = ?";
MC 259             $app->db->query($sql, true, $update_values);
7fe908 260         }
MC 261         unset($form);
262     }
263
264 }