Code

Updated workstation - Startup
[gosa.git] / plugins / admin / systems / class_servDNSeditZone.inc
index 82e40e3db773c04270e3d6590aaa5506be5204ef..d5df0871d9bbbb912ec1699716255342ccb2b451 100644 (file)
@@ -13,13 +13,11 @@ class servdnseditZone extends plugin
       "sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl"); 
   var $objectclasses  = array("whatever");
 
-  var $RecordTypes = array();
+  var $RecordTypes              = array();
 
   var $ReverseZone              = "";
-  var $ReverseDN                = "";
   var $zoneName                 = "";
   var $dNSClass                 = "IN";
-  var $status                   = "new";
 
   var $sOAprimary               = "";
   var $sOAmail                  = "";
@@ -32,40 +30,72 @@ class servdnseditZone extends plugin
   var $Records                  = array();
   var $mXRecords                = array();
 
-  var $InitiallyZoneName        = "";
-  var $InitiallyReverseZone     = "";
+  var $OldZoneName              = ""; // To detect changes made with this edit
+  var $OldReverseZone           = "";
+
+  var $InitialReverseZone       = "";
+  var $InitialzoneName          = "";
+  var $NetworkClass                = "A" ; // One out of A,B,C
+
+  var $dialog                   = false;
+
+  var $zoneEditor               = NULL;
+
   var $isNew                    = true;
 
-  function servdnseditZone ($config, $dn= NULL,$recordtypes,$attrs = array())
+  var $ZoneObject               = array();
+
+  function servdnseditZone (&$config, $dn= NULL,$attrs = array())
   {
     plugin::plugin ($config, $dn);
 
     /* All types with required attrs */
-    $this->RecordTypes = $recordtypes
+    $this->RecordTypes = getDnsRecordTypes(true)
 
     if(!count($attrs)){
-      $this->InitiallyZoneName      = "";
-      $this->InitiallyReverseZone   = "";
-      $this->isNew                  = true;
-      $this->sOAserial = date("Ymd")."1";
+      $this->OldZoneName        = "";
+      $this->OldReverseZone     = "";
+      $this->isNew              = true;
+      $this->sOAserial          = date("Ymd")."1";
+      
+      $this->InitialzoneName    = "";//$attrs['InitialzoneName'];
+      $this->InitialReverseZone = "";//$attrs['InitialReverseZone'];
     }else{
-      $this->InitiallyZoneName      = $attrs['zoneName'];
-      $this->InitiallyReverseZone   = $attrs['ReverseZone'];
+      $this->ZoneObject         = $attrs;
+
+      if(isset($attrs['zoneEditor'])){
+        $this->zoneEditor         = $attrs['zoneEditor'];
+      }
+      $this->OldZoneName        = $attrs['zoneName'];
+      $this->OldReverseZone     = $attrs['ReverseZone'];
+
+      $this->InitialzoneName    = $attrs['InitialzoneName'];
+      $this->InitialReverseZone = $attrs['InitialReverseZone'];
+
       $this->isNew                  = false;
 
       foreach($this->attributes as $value){
         $this->$value = $attrs[$value];
       }
-      if(isset($attrs['Records'])){
-        $this->Records = $attrs['Records']; 
+
+      $this->sOAmail            = preg_replace("/\./","@",$this->sOAmail,1);
+      $this->sOAmail            = preg_replace("/\.$/","",$this->sOAmail);
+      $this->sOAprimary         = preg_replace("/\.$/","",$this->sOAprimary);
+      $this->zoneName           = preg_replace("/\.$/","",$this->zoneName);
+
+      if(isset($attrs['RECORDS'])){
+        $this->Records = $attrs['RECORDS']; 
 
         $tmp2 = array();
         $usedPrio = array();
         foreach($this->Records as $key => $rec){
           if($rec['type'] == "mXRecord"){
             $tmp = split(" ",$rec['value']);
-            $rec['value'] = $tmp[0];
-            $tmp2[$tmp[1]] = $rec;
+            $rec['value'] = $tmp[1];
+            $tmp2[$tmp[0]] = $rec;
+            unset($this->Records[$key]);
+          }
+          if($rec['type'] == "nSRecord"){
             unset($this->Records[$key]);
           }
         }
@@ -78,6 +108,7 @@ class servdnseditZone extends plugin
         $this->mXRecords  = array();
         $this->Records    = array();
       }
+
       $str = date("Ymd");
       if(preg_match("/^".$str."/",$this->sOAserial)){
         $this->sOAserial = $this->sOAserial + 1;
@@ -85,6 +116,22 @@ class servdnseditZone extends plugin
         $this->sOAserial = date("Ymd")."01";
       }
     }
+
+    /* Detect Network class */
+    if(!empty($this->ReverseZone)){
+
+      $dots = count(split("\.",$this->ReverseZone));
+      if($dots == 1){
+        $this->NetworkClass = "A";  
+        $this->ReverseZone .= ".0.0.0"; 
+      }elseif($dots == 2){
+        $this->NetworkClass = "B";  
+        $this->ReverseZone .= ".0.0"; 
+      }else{
+        $this->NetworkClass = "C";  
+        $this->ReverseZone .= ".0"; 
+      }
+    }
   }
 
   /* TRansports the geiven Arraykey one position up*/
@@ -158,10 +205,62 @@ class servdnseditZone extends plugin
     plugin::execute();
 
 
+
     /* Fill templating stuff */
     $smarty= get_smarty();
+    $ui = get_userinfo();
+
+    $smarty->assign("ACLs",$this->parent->getacl(""));
     $display= "";
 
+    /* Open Zone Entry Edit Dialog
+     */
+    if(!count($this->ZoneObject)){
+      $smarty->assign("AllowZoneEdit" , false);
+    }else{
+      $smarty->assign("AllowZoneEdit" , true);
+      if(isset($_POST['EditZoneEntries'])){
+        if($this->zoneEditor == NULL){
+          $this->zoneEditor= new servDNSeditZoneEntries($this->config,$this->dn,$this->ZoneObject);
+          $this->zoneEditor->parent = $this;
+        }
+        $this->dialog = $this->zoneEditor;
+      }
+    }
+
+    /* Save Zone Entry Edit Dialog
+     */
+    if(isset($_POST['SaveZoneEntryChanges'])){
+      $this->dialog->save_object();
+      if(count($this->dialog->check())){
+        $msgs = $this->dialog->check();
+        foreach($msgs as $msg){
+          print_red($msg);
+        }
+      }else{
+        $this->zoneEditor = clone $this->dialog;
+        $this->dialog = FALSE;
+#       $rev = FlipIp(getNameFromMix($this->InitialReverseZone)).".in-addr.arpa";
+#       $for = getNameFromMix($this->InitialzoneName);
+#      
+#       $this->parent->handle_post_events("modify",array("dn" => $this->dn,"zoneName" => $rev));
+#       $this->parent->handle_post_events("modify",array("dn" => $this->dn,"zoneName" => $for));
+#       $this->dialog = false;
+      }
+    }
+
+    /* Cancel Zone Entrie Edit Dialog
+    */
+    if(isset($_POST['CancelZoneEntryChanges'])){
+      $this->dialog = false;
+    }
+
+    /* Display any type of open dialogs 
+     */
+    if(is_object($this->dialog)){
+      $this->dialog->save_object();
+      return($this->dialog->execute());
+    }
 
     $once =true;
     foreach($_POST as $name => $value){
@@ -202,7 +301,7 @@ class servdnseditZone extends plugin
     }
 
     if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
-      $this->mXRecords[] = array("type"=>"mXRecord","inittype"=>"","value"=>trim($_POST['StrMXRecord']),"status"=>"new");      
+      $this->mXRecords[] = array("type"=>"mXRecord","value"=>trim($_POST['StrMXRecord']));      
     }
 
     /* Handle Post events */
@@ -213,17 +312,13 @@ class servdnseditZone extends plugin
       if((preg_match("/RemoveRecord_/",$name))&&($once)){
         $once = false;
         $id= preg_replace("/RemoveRecord_/","",$name);
-        if($this->Records[$id]['status']!= "new"){
-          $this->Records[$id]['status']= "deleted";
-        }else{
-          unset($this->Records[$id]);
-        }
+        unset($this->Records[$id]);
       }
     }
 
     /* Add new Zonerecord */
     if(isset($_POST['AddNewRecord'])){
-      $this->Records[] = array("type"=>"aRecord","inittype"=>"","value"=>"","status"=>"new");
+      $this->Records[] = array("type"=>"aRecord","value"=>"");
     }
 
     /* Fill in values */
@@ -232,7 +327,7 @@ class servdnseditZone extends plugin
     }
 
 
-    $div = new DivSelectBox("MxRecords");
+    $div = new divSelectBox("MxRecords");
     $div->setHeight(120);
     $recs = $this->mXRecords;
 
@@ -252,9 +347,11 @@ class servdnseditZone extends plugin
     }
 
     /* Assign records list */
-
+    $smarty->assign("NotNew", false);
     $smarty->assign("Mxrecords",  $div->DrawList());
     $smarty->assign("records"  ,  $this->generateRecordsList());
+    $smarty->assign("NetworkClass",  $this->NetworkClass);
+    $smarty->assign("NetworkClasses",  array("A"=>"255.0.0.0 (Class A)","B"=>"255.255.0.0 (Class B)","C"=>"255.255.255.0 (Class C)"));
 
     /* Display tempalte */
     $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
@@ -283,38 +380,37 @@ class servdnseditZone extends plugin
         $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
       }
     }
+
+     if(isset($_POST['NetworkClass'])){
+       $this->NetworkClass = $_POST['NetworkClass'];
+     }
+
   }
 
 
   /* Check supplied data */
   function check()
   {
-    $message= array();
+    /* Call common method to give check the hook */
+    $message= plugin::check();
+        
     /* Check if zoneName is already in use */
     $usedZones = $this->getUsedZoneNames();
-    if(($this->isNew == true)||($this->zoneName  != $this->InitiallyZoneName)||($this->ReverseZone != $this->InitiallyReverseZone)){
-      if((isset($usedZones[$this->zoneName]))&&($this->zoneName  != $this->InitiallyZoneName)){
+    if(($this->isNew == true)||($this->zoneName  != $this->InitialzoneName)||($this->ReverseZone != $this->InitialReverseZone)){
+    /*  if((isset($usedZones[$this->zoneName]))&&($this->zoneName  != $this->InitialzoneName)){
         $message[] =_("This zoneName is already in use");
       }
-      if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitiallyReverseZone)){
+      if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitialReverseZone)){
         $message[] =_("This reverse zone is already in use");
-      }
-    }
-
-    if(!preg_match("/\.$/",$this->sOAprimary)){
-      $message[] = _("Primary dns server must end with '.' to be a valid entry.");
+      }*/
     }
 
-    if(!preg_match("/\.$/",$this->sOAmail)){
-      $message[] = _("Your specified mail address must end with '.' to be a valid record.");
+    if(empty($this->zoneName)){
+      $message[] =sprintf(_("Please choose a valid zone name."));
     }
 
-    if(preg_match("/@/",$this->sOAmail)){
-      $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
-    }
-
-    if(preg_match("/@/",$this->sOAmail)){
-      $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
+    if(empty($this->ReverseZone)){
+      $message[] =sprintf(_("Please choose a valid reverse zone name."));
     }
 
     if($this->zoneName != strtolower($this->zoneName)){
@@ -347,6 +443,38 @@ class servdnseditZone extends plugin
         $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
       }
     }
+
+    /* Check class for given Zone Address */
+    $addr = preg_replace("/^[^\/]*+\//","",$this->ReverseZone);
+  
+    /* Check for valid&complete IP address */
+    if(!is_ip($addr)){
+      $message[] = _("The given network address is not a valid, please specify a valid IP address.");
+    }
+    /* Check if given address matches selected network class */
+    switch($this->NetworkClass){
+      case 'A': { 
+                  if(!preg_match("/^[0-9]*\.0\.0\.0$/",$addr)){
+                    $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.0.0.0"));
+                  }
+                }
+                break;
+      case 'B': {
+                  if(!preg_match("/^[0-9]*\.[0-9]*\.0\.0$/",$addr)){
+                    $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.x.0.0"));
+                  }
+                }
+                break;
+      case 'C': {
+                  if(!preg_match("/^[0-9]*\.[0-9]*\.[0-9]*\.0$/",$addr)){
+                    $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.x.x.0"));
+                  }
+                }
+                break;
+      default : $message[] =sprintf(_("The given network class '%s' is not valid."),$this->NetworkClass);
+    }
+
     return ($message);
   }
 
@@ -361,7 +489,7 @@ class servdnseditZone extends plugin
       if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
         if(isset($attr['tXTRecord'][0])){
           $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
-          $ret[$zn] = $this->FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
+          $ret[$zn] =FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
         }
       }else{
         $ret[$attr['zoneName'][0]]="";
@@ -370,22 +498,6 @@ class servdnseditZone extends plugin
     return($ret);
   }
 
-  /* this is used to flip the ip address for example
-      12.3.45  ->  54.3.12
-     Because some entries (like zones) are store like that 54.3.12.in-addr.arpa
-      but we want to display 12.3.45.
-  */
-  function FlipIp($ip)
-  {
-    $tmp = array_reverse(split("\.",$ip));
-    $new = "";
-    foreach($tmp as $section){
-      $new .= $section.".";
-    }
-    return(preg_replace("/.$/","",$new));
-  }
-
-
   /* Save to LDAP */
   function save()
   {
@@ -394,13 +506,39 @@ class servdnseditZone extends plugin
       $ret[$name] = $this->$name;
     }
 
+    /* Create mx records 
+     */
     foreach($this->mXRecords as $key => $rec){
-      $rec['value'].= " ".$key;
+      $rec['value']= $key." ".$rec['value'];
       $this->Records [] = $rec;
     }
 
-    $ret['Records'] = $this->Records; 
+  
+    $ret['RECORDS'] = $this->Records; 
+    switch($this->NetworkClass){
+      case 'C' : $ret['ReverseZone']= preg_replace("/\.[0-9]*$/","",$this->ReverseZone);break;
+      case 'B' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
+      case 'A' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
+      default : trigger_error("Invalid network class given '".$this->NetworkClass."'");
+    }
+
+    $ret['InitialReverseZone']=  $this->InitialReverseZone;
+    $ret['InitialzoneName']   =  $this->InitialzoneName;
+
+    $ret['sOAmail']            = preg_replace("/\@/",".",$this->sOAmail);
+
+    foreach(array("sOAprimary","zoneName","sOAmail") as $attr){
+      if(!preg_match("/\.$/",$ret[$attr])){
+        if(!is_ip($ret[$attr])){
+          $ret[$attr] = $ret[$attr].".";
+        }
+      }
+    }
+
+    $ret['RECORDS'][] = array("type" => "nSRecord","value" => $ret['sOAprimary']) ;
 
+    $ret['zoneEditor'] = $this->zoneEditor;
     return($ret);
   }
 
@@ -419,8 +557,6 @@ class servdnseditZone extends plugin
 
       if($entry['type'] == "mXRecord") continue;
       
-      if($entry['status'] == "deleted") continue;
-
       $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
       $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
       $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";