Code

c4e7f178daa7111e4560c2bbbb0775ab1924148b
[gosa.git] / plugins / admin / systems / class_servDNS.inc
1 <?php
3 class servdns extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage server basic objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account= TRUE;
12   var $attributes       = array(""); 
13   var $objectclasses    = array("whatever");
15   var $RecordTypes      = array();
17   var $Zones  = array();
18   var $dialog = NULL;
20   function servdns ($config, $dn= NULL)
21   {
22     plugin::plugin ($config, $dn);
24     /* All types with required attrs */
25     $this->RecordTypes['aRecord']       = "aRecord";           // ok
26     $this->RecordTypes['mDRecord']      = "mDRecord";          // ok
27     $this->RecordTypes['mXRecord']      = "mXRecord";          // ok
28     $this->RecordTypes['nSRecord']      = "nSRecord";          // ok
29     $this->RecordTypes['pTRRecord']     = "relativeDomainName";// ok
30     $this->RecordTypes['hInfoRecord']   = "hInfoRecord";       // ok
31     $this->RecordTypes['mInfoRecord']   = "mInfoRecord";       // ok
32     $this->RecordTypes['tXTRecord']     = "tXTRecord";         // ok
33     $this->RecordTypes['aFSDBRecord']   = "aFSDBRecord";       // ok
34     $this->RecordTypes['SigRecord']     = "SigRecord";         // ok
35     $this->RecordTypes['KeyRecord']     = "KeyRecord";         // ok
36     $this->RecordTypes['aAAARecord']    = "aAAARecord";        // ok
37     $this->RecordTypes['LocRecord']     = "LocRecord";         // ok
38     $this->RecordTypes['nXTRecord']     = "nXTRecord";         // ok
39     $this->RecordTypes['sRVRecord']     = "sRVRecord";         // ok
40     $this->RecordTypes['nAPTRRecord']   = "nAPTRRecord";       // ok
41     $this->RecordTypes['kXRecord']      = "kXRecord";          // ok
42     $this->RecordTypes['certRecord']    = "certRecord";        // ok
43     $this->RecordTypes['a6Record']      = "a6Record";          // ok
44     $this->RecordTypes['dSRecord']      = "dSRecord";          // ok
45     $this->RecordTypes['sSHFPRecord']   = "sSHFPRecord";       // ok
46     $this->RecordTypes['rRSIGRecord']   = "rRSIGRecord";       // ok
47     $this->RecordTypes['nSECRecord']    = "nSECRecord";        // ok
49     $this->cn = $this->attrs['cn'][0];
51     $types = array();
53     /* Get all records */
54     $ldap = $this->config->get_ldap_link();
55     $ldap->cd($this->dn);
56     $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@))",array("*"));
58     while($attrs = $ldap->fetch()){
59       /* If relative domainname == cn
60        * Try to read dnsclass / TTl / zone
61        */
62       if((isset($attrs['tXTRecord'][0]))&&(preg_match("/zoneName\=/",$attrs['tXTRecord'][0]))){
63         $zoneName= preg_replace("/zoneName\=/","",$attrs['tXTRecord'][0]);  
64         $types[$zoneName]['ReverseZone']  = $attrs['zoneName'][0];
65         $types[$zoneName]['ReverseDN']    = $attrs['dn']; 
66       }else{
68         /* Generate SOA entry */
69         if(isset($attrs['sOARecord'][0])){
70           $tmp = split("\ ",$attrs['sOARecord'][0]) ;
71           $tmp2 = array();
72           $ar = array("0"=>"sOAprimary","1"=>"sOAmail","2"=>"sOAserial","3"=>"sOArefresh","4"=>"sOAretry","5"=>"sOAexpire","6"=>"sOAttl");
74           /* Assign soa vars */
75           foreach($ar as $key => $name){
76             if(isset($tmp[$key])){
77               $types[$attrs['zoneName'][0]][$name] = $tmp[$key];
78             }else{
79               $types[$attrs['zoneName'][0]][$name] = "";
80             }
81           }
82         }
84         /* Set TTL value */
85         if(isset($attrs['dNSTTL'][0])){
86           $types[$attrs['zoneName'][0]]['dNSTTL'] = $attrs['dNSTTL'][0];
87         }
89         /* Set dns Class*/
90         if(isset($attrs['dNSClass'][0])){
91           $types[$attrs['zoneName'][0]]['dNSClass'] = $attrs['dNSClass'][0];
92         }
94         /* Set zone Name */
95         if(isset($attrs['zoneName'][0])){
96           $types[$attrs['zoneName'][0]]['zoneName'] = $attrs['zoneName'][0];
97         }
99         /* Set status */
100         $types[$attrs['zoneName'][0]]['status']   ="edited"; 
101         $types[$attrs['zoneName'][0]]['ReverseDN']= $attrs['dn']; 
103         /* Create list with all used records */
104         foreach($this->RecordTypes as $name => $value){
106           /* If there is a record attribute  */
107           if(isset($attrs[$name])){
109             /* get all entries */
110             for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
111               $types[$attrs['zoneName'][0]]['Records'][] =array("type"      =>$name,
112                   "inittype"  =>$name,
113                   "value"     =>$attrs[$value][$i],
114                   "status"    =>"edited",
115                   "dn"        =>$attrs['dn']);
116             }
117           }
118         }
119       }
120     }
122     /* If there is at least one entry in this -> types, we have DNS enabled */
123     $this->Zones = $types;
124     if(count($this->Zones) == 0){
125       $this->is_account = false;
126     }else{
127       $this->is_account = true;
128     }
130     /* Store initally account settings */
131     $this->DNSinitially_was_account = $this->is_account;
132   }
134   function execute()
135   {
136     /* Call parent execute */
137     plugin::execute();
139     /* Fill templating stuff */
140     $smarty= get_smarty();
141     $display= "";
143     /* Do we need to flip is_account state? */
144     if (isset($_POST['modify_state'])){
145       $this->is_account= !$this->is_account;
146     }
148     /* Show tab dialog headers */
149     if ($this->is_account){
150       $display= $this->show_header(_("Remove DNS service"),
151           _("This server has DNS features enabled. You can disable them by clicking below."));
152     } else {
153       $display= $this->show_header(_("Add DNS service"),
154           _("This server has DNS features disabled. You can enable them by clicking below."));
155       return ($display);
156     }
158     if(isset($_POST['SaveZoneChanges'])){
159       $this->dialog->save_object();
160     
161       if(count($this->dialog->check())){
162         foreach($this->dialog->check() as $msgs){
163           print_red($msgs); 
164         }
165       }else{
166         $ret = $this->dialog->save();
167         if($this->dialog->isNew == true){
168           $this->Zones[$ret['zoneName']] = $ret;
169           $this->Zones[$ret['zoneName']] = "new";
170         }else{
172           if(!$this->dialog->isNew){
174             /* Edited but not renamed */
175             if($ret['zoneName'] == $this->dialog->InitiallyZoneName){
176               $this->Zones[$ret['zoneName']] = $ret;
178             }else{
179               /* Edited and renamed, we have to check if we must delete the old entry from ldap */
181               if($this->Zones[$this->dialog->InitiallyZoneName]['status'] == "new"){
182                 /* this was a new entry (currently not saved to ldap) so we can simply unset the zone */
183                 unset($this->Zones[$this->dialog->InitiallyZoneName]);
184               
185               }elseif($this->Zones[$this->dialog->InitiallyZoneName]['status'] == "edited") {
186                 $this->Zones[$this->dialog->InitiallyZoneName]['status'] = "deleted";
187               } 
188               $this->Zones[$ret['zoneName']] = $ret;
189               $this->Zones[$ret['zoneName']]['status'] = "new";
190             }
191           }else{
192             $this->Zones[$ret['zoneName']] = $ret;
193             $this->Zones[$ret['zoneName']]['status'] = "new";
194           }
195         }
196         $this->dialog = NULL;
197       }
198     }
199     if(isset($_POST['CancelZoneChanges'])){
200       $this->dialog = NULL;
201     }
202     /* Add empty new zone */
203     if(isset($_POST['AddZone'])){
204       $this->dialog = new servdnseditZone($this->config,$this->dn,$this->RecordTypes);
205     }
207     $once = false;
208     foreach( $_POST as $name => $value){
209       if(preg_match("/^editZone_/",$name)&&!$once){
210         $once =true;
211         $tmp = preg_replace("/^editZone_/","",$name);
212         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
213         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->RecordTypes,$this->Zones[$tmp]);
214       }
215     }
217     if($this->dialog!= NULL){
218       $this->dialog->save_object();
219       $this->dialog->parent = $this;
220       return($this->dialog->execute());
221     }
223     /* Create Listbox with existing Zones */
224     $ZoneList = new divSelectBox("dNSZones");
225     $ZoneList -> SetHeight(254);
227     /* Add entries to divlist*/
228     $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>";
229     foreach($this->Zones as $zone => $values ){
230       if($values['status'] == "deleted") continue;
231       $ZoneList->AddEntry(array(array("string" => $zone.str_replace("%s",base64_encode($zone),$editImg))));
232     }    
234     /* Display tempalte */
235     $smarty->assign("ZoneList",$ZoneList->DrawList());
236     $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
237     return($display);
238   }
240   function remove_from_parent()
241   {
242   }
245   /* Save data to object */
246   function save_object()
247   {
248   }
251   /* Check supplied data */
252   function check()
253   {
254     $message= array();
255     return ($message);
256   }
259   /* Save to LDAP */
260   function save()
261   {
262   return(true);
263 //    plugin::save();
264     foreach($this->Zones as $zone){
265       $tmp = array();
266       if($zone['status'] == "deleted"){
267         echo "deleted";
268       }elseif($zone['status'] == "new" ){
269         echo "new";
270       }elseif($zone['status'] == "edited"){  
271         echo "edit";
272       }else{
273         echo "undefined type";
274       }
276       //$tmp = $this->generate_LDAP_entries($tmp,$zone,$delete);
277     }
278   }
280   function generate_LDAP_entries($tmp,$zone,$delete)
281   {
282     $delete  = array();
284     $tmp['objectClass']           = array("top","dNSZone");
285     $tmp['dNSTTL']                = $zone['dNSTTL']; 
286     $tmp['dNSClass']              = $zone['dNSClass']; 
287     $tmp['relativeDomainName']    = $zone['relativeDomainName']; 
289     $str = "";
290     foreach($zone['sOARecord'] as $name => $value){
291       $str .= $value." "; 
292     }
293     $tmp['sOARecord'] = $str;
295     $dn = "zoneName=".$zone['zoneName'].",".$this->dn;
297     /* Generate some attrs  */
298     $arr = array("SigRecord","KeyRecord","aAAARecord","nSRecord","iaFSDBRecord","mInfoRecord","hInfoRecord","mXRecord","mDRecord","tXTRecord",
299         "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
300     $aRecords = array();
301     foreach($arr as $ar){
302       foreach($zone['types'] as $type){
303         if(($type['type'] == $ar)&&($type['status']!="deleted")){
304           $tmp[$ar][] = $type['value'];
305         }
306       }
307     }
308     foreach($zone['types'] as $type){
309       if(isset($type['inittype'])){
310         if($type['type'] != $type['inittype']){
311           $tmp[$type['inittype']] = array();
312         }
313       }
314     }
315     return($tmp);
316   }
322 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
323 ?>