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