mcramer
2013-01-29 ffcef2d56196aad6a3c508b7ddb58c77daa87cff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
 * client_templates
 * 
 * @author Marius Cramer <m.cramer@pixcept.de> pixcept KG
 * @author (original tools.inc.php) Till Brehm, projektfarm Gmbh
 * @author (original tools.inc.php) Oliver Vogel www.muv.com
 */
 
class client_templates {
 
    function apply_client_templates($clientId) {
        global $app;
        
        include('../client/form/client.tform.php');
        
        /*
         * Get the master-template for the client
         */
        $sql = "SELECT template_master, template_additional FROM client WHERE client_id = " . $app->functions->intval($clientId);
        $record = $app->db->queryOneRecord($sql);
        $masterTemplateId = $record['template_master'];
        $additionalTemplateStr = $record['template_additional'];
 
        /*
         * if the master-Template is custom there is NO changing
         */
        if ($masterTemplateId > 0){
            $sql = "SELECT * FROM client_template WHERE template_id = " . $app->functions->intval($masterTemplateId);
            $limits = $app->db->queryOneRecord($sql);
        } else {
            // if there is no master template it makes NO SENSE adding sub templates.
            // adding subtemplates are stored in client limits, so they would add up
            // on every save action for the client -> too high limits!
            return;
        }
 
        /*
         * Process the additional tempaltes here (add them to the limits
         * if != -1)
         */
        $addTpl = explode('/', $additionalTemplateStr);
        foreach ($addTpl as $item){
            if (trim($item) != ''){
                $sql = "SELECT * FROM client_template WHERE template_id = " . $app->functions->intval($item);
                $addLimits = $app->db->queryOneRecord($sql);
                $app->log('Template processing subtemplate ' . $item . ' for client ' . $clientId, LOGLEVEL_DEBUG);
                /* maybe the template is deleted in the meantime */
                if (is_array($addLimits)){
                    foreach($addLimits as $k => $v){
                        /* we can remove this condition, but it is easier to debug with it (don't add ids and other non-limit values) */
                        if (strpos($k, 'limit') !== false or $k == 'ssh_chroot' or $k == 'web_php_options' or $k == 'force_suexec'){
                            $app->log('Template processing key ' . $k . ' for client ' . $clientId, LOGLEVEL_DEBUG);
 
                            /* process the numerical limits */
                            if (is_numeric($v)){
                                /* switch for special cases */
                                switch ($k){
                                case 'limit_cron_frequency':
                                    if ($v < $limits[$k]) $limits[$k] = $v;
                                    /* silent adjustment of the minimum cron frequency to 1 minute */
                                    /* maybe this control test should be done via validator definition in tform.php file, but I don't know how */
                                    if ($limits[$k] < 1) $limits[$k] = 1;
                                break;
 
                                default:
                                    if ($limits[$k] > -1){
                                        if ($v == -1){
                                            $limits[$k] = -1;
                                        }
                                        else {
                                            $limits[$k] += $v;
                                        }
                                    }
                                }
                            }
                            /* process the string limits (CHECKBOXARRAY, SELECT etc.) */
                            elseif (is_string($v)){
                                switch ($form["tabs"]["limits"]["fields"][$k]['formtype']){
                                case 'CHECKBOXARRAY':
                                    if (!isset($limits[$k])){
                                        $limits[$k] = array();
                                    }
 
                                    $limits_values = $limits[$k];
                                    if (is_string($limits[$k])){
                                        $limits_values = explode($form["tabs"]["limits"]["fields"][$k]["separator"],$limits[$k]);
                                    }
                                    $additional_values = explode($form["tabs"]["limits"]["fields"][$k]["separator"],$v);
                                    $app->log('Template processing key ' . $k . ' type CHECKBOXARRAY, lim / add: ' . implode(',', $limits_values) . ' / ' . implode(',', $additional_values) . ' for client ' . $clientId, LOGLEVEL_DEBUG);
                                    /* unification of limits_values (master template) and additional_values (additional template) */
                                    $limits_unified = array();
                                    foreach($form["tabs"]["limits"]["fields"][$k]["value"] as $key => $val){
                                        if (in_array($key,$limits_values) || in_array($key,$additional_values)) $limits_unified[] = $key;
                                    }
                                    $limits[$k] = implode($form["tabs"]["limits"]["fields"][$k]["separator"],$limits_unified);
                                break;
                                case 'CHECKBOX':
                                    if($k == 'force_suexec') {
                                        // 'n' is less limited than y
                                        if (!isset($limits[$k])){
                                            $limits[$k] = 'y';
                                        }
                                        if($limits[$k] == 'n' || $v == 'n') $limits[$k] = 'n';
                                    } else {
                                        // 'y' is less limited than n
                                        if (!isset($limits[$k])){
                                            $limits[$k] = 'n';
                                        }
                                        if($limits[$k] == 'y' || $v == 'y') $limits[$k] = 'y';
                                    }
                                break;
                                case 'SELECT':
                                    $limit_values = array_keys($form["tabs"]["limits"]["fields"][$k]["value"]);
                                    /* choose the lower index of the two SELECT items */
                                    $limits[$k] = $limit_values[min(array_search($limits[$k], $limit_values), array_search($v, $limit_values))];
                                break;
                                }
                            }
                        }
                    }
                }
            }
        }
 
        /*
         * Write all back to the database
         */
        $update = '';
        foreach($limits as $k => $v){
            if ((strpos($k, 'limit') !== false or $k == 'ssh_chroot' or $k == 'web_php_options' or $k == 'force_suexec') && !is_array($v)){
                if ($update != '') $update .= ', ';
                $update .= '`' . $k . "`='" . $v . "'";
            }
        }
        $app->log('Template processed for client ' . $clientId . ', update string: ' . $update, LOGLEVEL_DEBUG);
        if($update != '') {
            $sql = 'UPDATE client SET ' . $update . " WHERE client_id = " . $app->functions->intval($clientId);
            $app->db->query($sql);
        }
        unset($form);
    }
}