Code

Updated FAIhooks
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 9 Jul 2010 09:27:02 +0000 (09:27 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 9 Jul 2010 09:27:02 +0000 (09:27 +0000)
-Added encoding verification.

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@18973 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-plugins/fai/admin/fai/class_faiHookEntry.inc
gosa-plugins/fai/admin/fai/faiHookEntry.tpl

index 5bfdf5823d6ec248b67b111d6b83a93bc21ae104..501ffaa54e8d669ec5eea5ee44478b64c73a7178 100644 (file)
@@ -20,6 +20,12 @@ class faiHookEntry extends plugin
   var $status        = "new";
   var $parent        = NULL;
   var $FAIstate      = "";
+
+  // Encoding validation
+  var $enc_before_edit = "";
+  var $enc_after_edit = "";
+  var $write_protect = FALSE;
+
   
   function faiHookEntry (&$config, $dn= NULL,$object=false)
   {
@@ -45,6 +51,15 @@ class faiHookEntry extends plugin
         $this->orig_cn       = false;
       }
     }
+
+    // Keep an eye on dangerous encodings, we may break scripts while editing.
+    $this->mb_extension = function_exists("mb_detect_encoding");
+    if($this->mb_extension){
+        $this->enc_before_edit = mb_detect_encoding($this->FAIscript);
+        if($this->enc_before_edit != "ASCII"){
+            $this->write_protect = TRUE;
+        }
+    }
   }
 
 
@@ -66,9 +81,21 @@ class faiHookEntry extends plugin
       }else{
         $str = file_get_contents($_FILES['ImportFile']['tmp_name']);
         $this->FAIscript = $str;
+
+        // Check encoding again
+        if($this->mb_extension){
+            $this->enc_before_edit = mb_detect_encoding($this->FAIscript);
+            if($this->enc_before_edit != "ASCII"){
+                $this->write_protect = TRUE;
+            }
+        }
       }
     }
 
+    // Assign encoding related variables.
+    $smarty->assign("write_protect",$this->write_protect);
+
+
     /* File download requested */
     if(isset($_POST['download'])){
       send_binary_content($this->FAIscript,$this->cn.".FAIhook");
@@ -114,6 +141,10 @@ class faiHookEntry extends plugin
   {
     if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/^freeze/", $this->FAIstate)){
       foreach($this->attributes as $attrs){
+
+        // Do not update the hook content, here, we'll do this a few lines below.
+        if($attrs == 'FAIscript') continue;
+
         if($this->acl_is_writeable($attrs)){
           if(isset($_POST[$attrs])){
             $this->$attrs = get_post($attrs);
@@ -122,6 +153,16 @@ class faiHookEntry extends plugin
           }
         }
       }
+
+      // Get FAIscript changes
+      if(isset($_POST['FAIscript']) &&
+              $this->acl_is_writeable('FAIscript') &&
+              !$this->write_protect){
+
+          // Check encoding again.
+          $this->FAIscript = get_post('FAIscript');
+          $this->enc_after_edit = mb_detect_encoding($this->FAIscript);
+      }
     }
   }
 
@@ -131,6 +172,13 @@ class faiHookEntry extends plugin
     /* Call common method to give check the hook */
     $message= plugin::check();
 
+    if($this->mb_extension && !$this->write_protect && $this->enc_after_edit !== $this->enc_before_edit ){
+        $msg = sprintf(_("The script encodig has changed from '%s' to '%s'. Do you really want to save?"),
+                "<i>".$this->enc_before_edit."</i>","<i>".$this->enc_after_edit."</i>");
+        $message[] = $msg;
+        $this->enc_before_edit = $this->enc_after_edit;
+    }
+
     if(isset($this->parent->SubObjects[$this->cn]) && $this->cn != $this->orig_cn && 
         $this->parent->SubObjects[$this->cn]['status'] != 'delete'){
       $message[]= msgPool::duplicated(_("Name"));
index 591598ede0cc97e37970572f774f1fe484eff025..98c99dda0a6a4187630a47ca7ce96d269635b3b7 100644 (file)
 <hr>
 <h3><LABEL for="FAIscript">{t}Script{/t}</LABEL>
 </h3>
+
+{if $write_protect}
+  {t}This FAI script is write protected, due to its encoding. Editing may break it!{/t}
+  <br>
+  <button type='submit' name='editAnyway'>{t}Edit anyway{/t}</button>
+{/if}
+
+
 {render acl=$FAIscriptACL}
-    <textarea name="FAIscript" style="width:100%;height:300px;" id="FAIscript" rows=20 cols=120>{$FAIscript}</textarea>
+    <textarea {if $write_protect} disabled {/if} {if !$write_protect} name="FAIscript" {/if} 
+        style="width:100%;height:300px;" id="FAIscript" rows=20 cols=120>{$FAIscript}</textarea>
 {/render}
 <br>
 <div>