Code

Updated FAI script
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 8 Jul 2010 13:30:12 +0000 (13:30 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 8 Jul 2010 13:30:12 +0000 (13:30 +0000)
-ensure not break character encoding

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

gosa-plugins/fai/admin/fai/class_faiScriptEntry.inc
gosa-plugins/fai/admin/fai/faiScriptEntry.tpl

index c61d0c2b4870ab76d03d3b2c7507240cc6f1a032..edafad3cf699257c2a9f54fb1b69c056c91ac8ec 100644 (file)
 
 class faiScriptEntry extends plugin
 {
-  /* attribute list for save action */
-  var $ignore_account= TRUE;
-  var $attributes   = array("cn","description","FAIpriority","FAIscript");
-  var $objectclasses= array();
-
-  var $orig_cn              = "";
-
-  var $dn            = "";
-  var $cn            = "";
-  var $FAIpriority   = "0";
-  var $FAIscript     = "";
-  var $description   = "";
-  var $status        = "new";
-  var $parent        = NULL;
-  var $FAIstate      = "";
-
-  function faiScriptEntry (&$config, $dn= NULL,$object=false)
-  {
-    plugin::plugin ($config, NULL);
-    if($dn != "new"){
-      $this->orig_cn= $object['cn'];
-      $this->dn=$object['dn'];
-      foreach($object as $name=>$value){
-        $this->$name=$value;
-      }
-    }else{
-      if(is_array($object)&&count($object)){
-        $this->orig_cn= $object['cn'];
-        $this->dn=$object['dn'];
-        foreach($object as $name=>$value){
-          $oname = $name;
-          $this->$oname=$value;
-        }
-      }else{
-
-        $this->status = "new";
-        $this->orig_cn = false;
-      }
-    }
-  }
-
-  function execute()
-  {
-    /* Call parent execute */
-    plugin::execute();
-
-    /* Fill templating stuff */
-    $smarty     = get_smarty();
-    $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));
-    $display = "";
-  
-    if(isset($_POST['ImportUpload']) && $this->acl_is_writeable("FAIscript")){
-      if(($_FILES['ImportFile']['error']!=0)){
-        msg_dialog::display(_("Error"), msgPool::incorrectUpload(), ERROR_DIALOG);
-      }else
-      if(($_FILES['ImportFile']['size']==0)){
-        msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("file is empty")), ERROR_DIALOG);
-      }else{
-        $str = file_get_contents($_FILES['ImportFile']['tmp_name']);
-        $this->FAIscript = $str;
-      }
-    }
-    foreach($this->attributes as $attrs){
-      $smarty->assign($attrs, set_post($this->$attrs));
-    }
+    /* attribute list for save action */
+    var $ignore_account= TRUE;
+    var $attributes   = array("cn","description","FAIpriority","FAIscript");
+    var $objectclasses= array();
+
+    var $orig_cn              = "";
+
+    var $dn            = "";
+    var $cn            = "";
+    var $FAIpriority   = "0";
+    var $FAIscript     = "";
+    var $description   = "";
+    var $status        = "new";
+    var $parent        = NULL;
+    var $FAIstate      = "";
+
+    // Encoding validation
+    var $enc_before_edit = "";
+    var $enc_after_edit = "";
+    var $write_protect = FALSE;
+
+    function faiScriptEntry (&$config, $dn= NULL,$object=false)
+    {
+        plugin::plugin ($config, NULL);
+        if($dn != "new"){
+            $this->orig_cn= $object['cn'];
+            $this->dn=$object['dn'];
+            foreach($object as $name=>$value){
+                $this->$name=$value;
+            }
+        }else{
+            if(is_array($object)&&count($object)){
+                $this->orig_cn= $object['cn'];
+                $this->dn=$object['dn'];
+                foreach($object as $name=>$value){
+                    $oname = $name;
+                    $this->$oname=$value;
+                }
+            }else{
+                $this->status = "new";
+                $this->orig_cn = false;
+            }
+        }
 
-    /* File download requested */
-    if(isset($_POST['download'])){
-      send_binary_content($this->FAIscript,$this->cn.".FAIscript");
+        // 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;
+            }
+        }
     }
 
-    /* Create download button*/
-    if($this->dn != "new" && $this->acl_is_readable("FAIscript")){
-      $smarty->assign("DownMe", image('images/save.png','download',_("Download")));
-    }else{
-      $smarty->assign("DownMe","");
-    }
 
-    for($i =0 ; $i < 100 ; $i++){
-      $FAIprioritys[$i]=$i;
-    }
+    function execute()
+    {
+        /* Call parent execute */
+        plugin::execute();
+
+        /* Fill templating stuff */
+        $smarty     = get_smarty();
+        $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));
+        $display = "";
+
+        if(isset($_POST['ImportUpload']) && $this->acl_is_writeable("FAIscript")){
+            if(($_FILES['ImportFile']['error']!=0)){
+                msg_dialog::display(_("Error"), msgPool::incorrectUpload(), ERROR_DIALOG);
+            }else
+                if(($_FILES['ImportFile']['size']==0)){
+                    msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("file is empty")), ERROR_DIALOG);
+                }else{
+                    $str = file_get_contents($_FILES['ImportFile']['tmp_name']);
+                    $this->FAIscript = $str;
+                }
+        }
 
-    $tmp = $this->plInfo();
-    foreach($tmp['plProvidedAcls'] as $name => $translated){
-      $acl = $this->getacl($name,preg_match("/freeze/i",$this->FAIstate));
-      $smarty->assign($name."ACL",$acl);
-    }
-    $smarty->assign("FAIprioritys",$FAIprioritys);
-    $display.= $smarty->fetch(get_template_path('faiScriptEntry.tpl', TRUE));
-    return($display);
-  }
-
-  /* Save data to object */
-  function save_object()
-  {
-    if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/freeze/", $this->FAIstate)){
-      foreach($this->attributes as $attrs){
-          if($this->acl_is_writeable($attrs)){
-              if(isset($_POST[$attrs])){
-                  $this->$attrs = get_post($attrs);
-              }else{
-                  $this->$attrs = "";
-              }
-          }
-      }
-    }
-  }
-
-  /* Check supplied data */
-  function check()
-  {
-    /* Call common method to give check the hook */
-    $message= plugin::check();
-  
-    if(isset($this->parent->SubObjects[$this->cn]) && $this->cn != $this->orig_cn){
-      $message[] = msgPool::duplicated(_("Name"));
-    }
+        // Assign encoding related variables.
+        $smarty->assign("write_protect",$this->write_protect);
+
+        foreach($this->attributes as $attrs){
+            $smarty->assign($attrs, set_post($this->$attrs));
+        }
+
+        /* File download requested */
+        if(isset($_POST['download'])){
+            send_binary_content($this->FAIscript,$this->cn.".FAIscript");
+        }
 
-    $c = trim($this->cn);
-    if($c == ""){
-      $message[] = msgPool::required(_("Name"));
+        /* Create download button*/
+        if($this->dn != "new" && $this->acl_is_readable("FAIscript")){
+            $smarty->assign("DownMe", image('images/save.png','download',_("Download")));
+        }else{
+            $smarty->assign("DownMe","");
+        }
+
+        for($i =0 ; $i < 100 ; $i++){
+            $FAIprioritys[$i]=$i;
+        }
+
+        $tmp = $this->plInfo();
+        foreach($tmp['plProvidedAcls'] as $name => $translated){
+            $acl = $this->getacl($name,preg_match("/freeze/i",$this->FAIstate));
+            $smarty->assign($name."ACL",$acl);
+        }
+        $smarty->assign("FAIprioritys",$FAIprioritys);
+        $display.= $smarty->fetch(get_template_path('faiScriptEntry.tpl', TRUE));
+        return($display);
     }
-    if(preg_match("/[^a-z0-9_\-]/i",$c)){
-      $message[] = msgPool::invalid(_("Name"),$c,"/[a-z0-9_\-]/i");
+
+    /* Save data to object */
+    function save_object()
+    {
+        if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/freeze/", $this->FAIstate)){
+            foreach($this->attributes as $attrs){
+
+                // Do not touch FAIscriot values here, w'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);
+                    }else{
+                        $this->$attrs = "";
+                    }
+                }
+            }
+
+            // 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);
+            }
+        }
+        if(isset($_POST['editAnyway'])) $this->write_protect = FALSE;
     }
 
-    $s = trim($this->FAIscript);
-    if($s == ""){
-      $message[]= msgPool::required(_("Script"));
+
+    /* Check supplied data */
+    function check()
+    {
+        /* 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){
+            $message[] = msgPool::duplicated(_("Name"));
+        }
+
+        $c = trim($this->cn);
+        if($c == ""){
+            $message[] = msgPool::required(_("Name"));
+        }
+        if(preg_match("/[^a-z0-9_\-]/i",$c)){
+            $message[] = msgPool::invalid(_("Name"),$c,"/[a-z0-9_\-]/i");
+        }
+
+        $s = trim($this->FAIscript);
+        if($s == ""){
+            $message[]= msgPool::required(_("Script"));
+        }
+
+        return ($message);
     }
 
-    return ($message);
-  }
-  function save()
-  {
-    $tmp=array();
-    foreach($this->attributes as $attrs){ 
-      $tmp[$attrs] = $this->$attrs;
+    function save()
+    {
+        $tmp=array();
+        foreach($this->attributes as $attrs){ 
+            $tmp[$attrs] = $this->$attrs;
+        }
+
+        if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
+            $tmp['remove']['from']  = $this->orig_cn;
+            $tmp['remove']['to']    = $tmp['cn'];
+        }
+
+        $tmp['dn']      = $this->dn;  
+        $tmp['status']  = $this->status; 
+        return($tmp);
     }
 
-    if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
-      $tmp['remove']['from']  = $this->orig_cn;
-      $tmp['remove']['to']    = $tmp['cn'];
+
+    /* Return plugin informations for acl handling */
+    static function plInfo()
+    {
+        return (array(
+                    "plShortName" => _("Script entry"),
+                    "plDescription" => _("FAI script entry"),
+                    "plSelfModify"  => FALSE,
+                    "plDepends"     => array(),
+                    "plPriority"    => 19,
+                    "plSection"     => array("administration"),
+                    "plCategory"    => array("fai"),
+                    "plProvidedAcls" => array(
+                        "cn"              => _("Name"),
+                        "description"     => _("Description"),
+                        "FAIscript"       => _("Script entry"),
+                        "FAIpriority"     => _("Script Priority"))
+                    ));
     }
-  
-    $tmp['dn']      = $this->dn;  
-    $tmp['status']  = $this->status; 
-    return($tmp);
-  }
-
-  
-  /* Return plugin informations for acl handling */
-  static function plInfo()
-  {
-    return (array(
-          "plShortName" => _("Script entry"),
-          "plDescription" => _("FAI script entry"),
-          "plSelfModify"  => FALSE,
-          "plDepends"     => array(),
-          "plPriority"    => 19,
-          "plSection"     => array("administration"),
-          "plCategory"    => array("fai"),
-          "plProvidedAcls" => array(
-            "cn"              => _("Name"),
-            "description"     => _("Description"),
-            "FAIscript"       => _("Script entry"),
-            "FAIpriority"     => _("Script Priority"))
-          ));
-  }
 
 
 }
index 5f28a307ceea759cafcfd3c7be332c085d5cbdfb..052fa37312e2f7cd29923542a6a934b9511ee286 100644 (file)
  </tr>
 </table>
 <hr>
-<h3><LABEL for="FAIscript">{t}Script{/t}</LABEL>
-</h3>
-{render acl=$FAIscriptACL}<textarea name="FAIscript" style="width:100%;height:300px;" id="FAIscript"
+<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 {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>
  {render acl=$FAIscriptACL}