From aa370627b211a51dc46891cfa4b6e3d2ef3e52db Mon Sep 17 00:00:00 2001
From: mcramer <m.cramer@pixcept.de>
Date: Tue, 16 Jul 2013 10:45:17 -0400
Subject: [PATCH] - Fixed FS#2924 - the month will not set automatically in the autoresponder by click now   Along with this fixed some display problems with the combo boxes introduced in 3.0.5.   Some fields were not correctly displayed with the predefined values if value and text of the underlying option element differ.

---
 interface/lib/classes/client_templates.inc.php |   54 +++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/interface/lib/classes/client_templates.inc.php b/interface/lib/classes/client_templates.inc.php
index 8631db0..c2ef0bb 100644
--- a/interface/lib/classes/client_templates.inc.php
+++ b/interface/lib/classes/client_templates.inc.php
@@ -9,15 +9,15 @@
  
 class client_templates {
 
-	function apply_client_templates($clientId, $limits = array()) {
+	function apply_client_templates($clientId) {
         global $app;
         
-        if(!is_array($limits)) $limits = array();
+        include('../client/form/client.tform.php');
         
         /*
          * Get the master-template for the client
          */
-        $sql = "SELECT template_master, template_additional FROM client WHERE client_id = " . intval($clientId);
+        $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'];
@@ -26,8 +26,13 @@
          * if the master-Template is custom there is NO changing
          */
         if ($masterTemplateId > 0){
-            $sql = "SELECT * FROM client_template WHERE template_id = " . intval($masterTemplateId);
+            $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;
         }
 
         /*
@@ -37,13 +42,16 @@
         $addTpl = explode('/', $additionalTemplateStr);
         foreach ($addTpl as $item){
             if (trim($item) != ''){
-                $sql = "SELECT * FROM client_template WHERE template_id = " . intval($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){
+                        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 */
@@ -68,7 +76,7 @@
                             }
                             /* process the string limits (CHECKBOXARRAY, SELECT etc.) */
                             elseif (is_string($v)){
-                                switch ($app->tform->formDef["tabs"]["limits"]["fields"][$k]['formtype']){
+                                switch ($form["tabs"]["limits"]["fields"][$k]['formtype']){
                                 case 'CHECKBOXARRAY':
                                     if (!isset($limits[$k])){
                                         $limits[$k] = array();
@@ -76,20 +84,34 @@
 
                                     $limits_values = $limits[$k];
                                     if (is_string($limits[$k])){
-                                        $limits_values = explode($app->tform->formDef["tabs"]["limits"]["fields"][$k]["separator"],$limits[$k]);
+                                        $limits_values = explode($form["tabs"]["limits"]["fields"][$k]["separator"],$limits[$k]);
                                     }
-                                    $additional_values = explode($app->tform->formDef["tabs"]["limits"]["fields"][$k]["separator"],$v);
-
+                                    $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($app->tform->formDef["tabs"]["limits"]["fields"][$k]["value"] as $key => $val){
+                                    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($app->tform->formDef["tabs"]["limits"]["fields"][$k]["separator"],$limits_unified);
+                                    $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($app->tform->formDef["tabs"]["limits"]["fields"][$k]["value"]);
+                                    $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;
@@ -111,9 +133,11 @@
                 $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 = " . intval($clientId);
+            $sql = 'UPDATE client SET ' . $update . " WHERE client_id = " . $app->functions->intval($clientId);
             $app->db->query($sql);
         }
+        unset($form);
     }
 }
\ No newline at end of file

--
Gitblit v1.9.1