Code

Updated dns stuff.
[gosa.git] / plugins / admin / systems / class_servDHCP.inc
1 <?php
3 class servdhcp extends plugin
4 {
5   /* attribute list for save action */
6   var $attributes= array("dhcpServiceDN");
7   var $objectclasses= array("dhcpServer");
9   var $dhcpServiceDN= "";
11   /* Section storage */
12   var $dhcpSections= array();
13   var $dhcpObjectCache= array();
14   var $current_object= "";
15   var $types= array();
16   var $serviceDN= "";
18         var $quote_option = array("domain-name");
20   var $orig_dn = "";
22   function servdhcp ($config, $dn= NULL, $parent= NULL)
23   {
24     plugin::plugin ($config, $dn, $parent);
26     $this->serviceDN = "cn=dhcp,".$dn;
27     $this->orig_dn = $dn;
29     $this->types= array(  "dhcpLog" => _("Logging"),
30             "dhcpService" => _("Global options"),
31             "dhcpClass" => _("Class"),
32             "dhcpSubClass" => _("Subclass"),
33             "dhcpHost" => _("Host"),
34             "dhcpGroup" => _("Group"),
35             "dhcpPool" => _("Pool"),
36             "dhcpSubnet" => _("Subnet"),
37             "dhcpFailOverPeer" => _("Failover peer"),
38             "dhcpSharedNetwork" => _("Shared network"));
41     /* Backport: PHP4 compatibility  */
42     foreach($this->types as $type => $translation){
43             $this->types[strtolower($type)] = $translation;
44     }
46     /* Load information about available services */
47     $this->reload(); 
48     if (!count($this->dhcpSections)){
49       $this->is_account= FALSE;
50     }
51                 
52   }
55   function execute()
56   {
57     /* Call parent execute */
58     plugin::execute();
60     /* Fill templating stuff */
61     $smarty= get_smarty();
62     $display= "";
64     /* Section Creation? */
65     if (isset($_POST['create_section']) && isset($_POST['section'])){
66       $section= $_POST['section'];
67       $tmp = new dhcpNewSectionDialog(NULL);
68       if (isset($tmp->sectionMap[$section])){
69         $this->dialog= new $section($this->current_object);
70         $this->current_object= "";
71       } else {
72         $this->dialog= NULL;
73       }
74     }
76     /* Cancel section creation? */
77     if (isset($_POST['cancel_section']) || isset($_POST['cancel_dhcp'])){
78       $this->dialog= NULL;
79     }
81     /* Save changes */
82     if (isset($_POST['save_dhcp'])){
83       $this->dialog->save_object();
84       $messages= $this->dialog->check($this->dhcpObjectCache);
85       if (count($messages)){
86         show_errors($messages);
87       } else {
88         $dn= $this->dialog->dn;
89         $class= get_class($this->dialog);
90         $type= $this->types[$class];
91                 if(empty($this->serviceDN)){
92                         $indent= substr_count(preg_replace("/".$this->dn."/", '', $dn), ",") -1;
93                 }else{
94                         $indent= substr_count(preg_replace("/".$this->serviceDN."/", '', $dn), ",");
95                 }
96                 $spaces= "";
97                 for ($i= 0; $i<$indent; $i++){
98           $spaces.= "&nbsp;&nbsp;&nbsp;&nbsp;";
99         }
100         $data= $this->dialog->save();
101         if ($this->current_object == ""){
102           /* New object */
103           $newsects= array();
104           foreach ($this->dhcpSections as $key => $dsc){
105             $newsects[$key]= $dsc;
106             if ($key == $dn){
107               $spaces.= "&nbsp;&nbsp;&nbsp;&nbsp;";
108               $newsects[$data['dn']]= "$spaces$type '".preg_replace('/^[^=]+=([^,]+),.*$/', '\1', $data['dn'])."'";
109             }
110           }
111           $this->dhcpObjectCache[$data['dn']]= $data;
112           $this->dhcpSections= $newsects;
113         } else {
114           if ($dn != $data['dn']){
115             /* Old object, new name */
116             $this->dhcpObjectCache[$dn]= array();
117             $this->dhcpObjectCache[$data['dn']]= $data;
119             /* If we renamed a section, we've to rename a couple of objects, too */
120             foreach ($this->dhcpObjectCache as $key => $dsc){
121               if (preg_match("/,$dn$/", $key)){
122                 $new_dn= preg_replace("/,$dn$/", ",".$data['dn'], $key);
123                 $dsc['MODIFIED']= TRUE;
124                 $this->dhcpObjectCache[$new_dn]= $dsc;
125                 unset($this->dhcpObjectCache[$key]);
126               }
127             }
128             $newsects= array();
129             foreach ($this->dhcpSections as $key => $dsc){
130               if ($key == $dn){
131                 $newsects[$data['dn']]= "$spaces$type '".preg_replace('/^[^=]+=([^,]+),.*$/', '\1', $data['dn'])."'";
132                 continue;
133               }
134               if (preg_match("/,$dn$/", $key)){
135                 $new_dn= preg_replace("/,$dn$/", ",".$data['dn'], $key);
136                 $newsects[$new_dn]= $dsc;
137               } else {
138                 $newsects[$key]= $dsc;
139               }
140             }
141             $this->dhcpSections= $newsects;
143           } else {
144             /* Old object, old name */
145             $this->dhcpObjectCache[$data['dn']]= $data;
146           }
147         }
148         $this->dialog= NULL;
149       }
150     }
152     /* Remove section? */
153     if (isset($_POST['delete_dhcp_confirm'])){
154       if (chkacl($this->acl, "delete") == ""){
155         unset($this->dhcpSections[$this->current_object]);
156         unset($this->dhcpObjectCache[$this->current_object]);
157         $this->dhcpObjectCache[$this->current_object]= array();
158         foreach ($this->dhcpSections as $key => $value){
159           if (preg_match("/".$this->current_object."$/", $key)){
160             unset($this->dhcpSections[$key]);
161             unset($this->dhcpObjectCache[$key]);
162             $this->dhcpObjectCache[$key]= array();
163           }
164         }
165       } else {
166         print_red(_("You're not allowed to remove DHCP sections!"));
167       }
168       $this->dialog= NULL;
169     }
171     /* Look for post entries */
172     foreach($_POST as $name => $value){
173       
174       /* Insert new section? */
175       if (preg_match('/^insertDhcp_.*_x$/', $name)){
176         $dn= base64_decode(preg_replace('/^insertDhcp_([^_]+)_x$/', '\1', $name));
177         if (isset($this->dhcpObjectCache[$dn])){
178           $this->dialog= new dhcpNewSectionDialog($this->objectType($dn));
179           $this->current_object= $dn;
180           $this->dialog->acl= $this->acl;
181         }
182       }
184       /* Edit section? */
185       if (preg_match('/^editDhcp_.*_x$/', $name)){
186         $dn= base64_decode(preg_replace('/^editDhcp_([^_]+)_x$/', '\1', $name));
187         if (isset($this->dhcpObjectCache[$dn])){
188           $section= $this->objectType($dn);
189           $this->current_object= $dn;
190           $this->dialog= new $section($this->dhcpObjectCache[$dn]);
191         }
192       }
194       /* Remove section? */
195       if (preg_match('/^delDhcp_.*_x$/', $name)){
196         $dn= base64_decode(preg_replace('/^delDhcp_([^_]+)_x$/', '\1', $name));
197         if (isset($this->dhcpObjectCache[$dn])){
198           $this->current_object= $dn;
199           $this->dialog= 1;
200           $smarty->assign("warning", sprintf(_("You're about to delete the DHCP section '%s'."), $dn));
201           return($smarty->fetch(get_template_path('remove_dhcp.tpl', TRUE)));
202         }
203       }
205     }
207     /* Do we need to flip is_account state? */
208     if (isset($_POST['modify_state'])){
209       $this->is_account= !$this->is_account;
210     }
212     /* Show tab dialog headers */
213     if ($this->is_account){
214       $display= $this->show_header(_("Remove DHCP service"),
215           _("This server has DHCP features enabled. You can disable them by clicking below."));
217       if (!count($this->dhcpObjectCache)){
218         $attrs= array();
219         $attrs['dn']= 'cn=dhcp,'.$this->dn;
220         $attrs['cn']= array('dhcp');
221         $attrs['objectClass']= array('top', 'dhcpService');
222         $attrs['dhcpPrimaryDN']= array($this->dn);
223         $attrs['dhcpStatements']= array("default-lease-time 600",
224                                         "max-lease-time 1200",
225                                         "authoritative",
226                                         "ddns-update-style none");
227         $attrs['MODIFIED']= TRUE;
228         $this->dhcpSections['cn=dhcp,'.$this->dn]= _("Global options");
229         $this->dhcpObjectCache['cn=dhcp,'.$this->dn]= $attrs;
230       }
232     } else {
233       $display= $this->show_header(_("Add DHCP service"),
234           _("This server has DHCP features disabled. You can enable them by clicking below."));
235       return ($display);
236     }
239     /* Show dialog
240      */
241     if($this->dialog != NULL && !is_int($this->dialog)){
242       $this->dialog->save_object();
243       $this->dialog->parent = $this;
244       return($this->dialog->execute());
245     }
247     /* Create Listbox with existing Zones
248      */
249     $DhcpList = new divSelectBox("dhcpSections");
250     $DhcpList->SetHeight(400);
252     /* Add entries to divlist
253      */
254     $editImgIns = "<input type='image' src='images/list_new.png' name='insertDhcp_%s' title='"._("Insert new DHCP section")."'>".
255       "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>".
256       "<input type='image' src='images/edittrash.png' name='delDhcp_%s' title='"._("Remove DHCP section")."'>";
257     $editImgInsNoDel = "<input type='image' src='images/list_new.png' name='insertDhcp_%s' title='"._("Insert new DHCP section")."'>".
258       "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>";
259     $editImg = "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>".
260       "<input type='image' src='images/edittrash.png' name='delDhcp_%s' title='"._("Remove DHCP section")."'>";
261         
262     $tmp = new dhcpNewSectionDialog(NULL);
263     foreach($this->dhcpSections as $section => $values ){
264         
265       if (count($tmp->sectionMap[$this->objectType($section)])){
266         if ($this->objectType($section) == "dhcpService"){
267           $DhcpList->AddEntry(array(
268               array("string" => $values),
269               array("string" => str_replace("%s",base64_encode($section),$editImgInsNoDel), "attach" => "style='text-align:right;'")
270               ));
271         } else {
272           $DhcpList->AddEntry(array(
273               array("string" => $values),
274               array("string" => str_replace("%s",base64_encode($section),$editImgIns), "attach" => "style='text-align:right;'")
275               ));
276         }
277       } else {
278         $DhcpList->AddEntry(array(
279               array("string" => $values),
280               array("string" => str_replace("%s",base64_encode($section),$editImg), "attach" => "style='text-align:right;'")
281               ));
282       }
283     }
285     $smarty->assign("dhcpACL",chkacl($this->acl,"servdhcp"));
287     /* Display tempalte */
288     $smarty->assign("DhcpList",$DhcpList->DrawList());
289     $display.= $smarty->fetch(get_template_path('servdhcp.tpl', TRUE));
290     return($display);
291   }
294   function remove_from_parent()
295   {
296     /* Cancel if there's nothing to do here */
297     if (!$this->initially_was_account){
298       return;
299     }
301     /* Remove subtrees */
302     $ldap= $this->config->get_ldap_link();
303     foreach ($this->dhcpObjectCache as $dn => $content){
304       if ($this->objectType($dn) == 'dhcpService'){
305         $ldap->rmdir_recursive($dn);
306         show_ldap_error($ldap->get_error(), _("Removing DHCP entries failed"));
307       }
308     }
310     /* Remove from self */
311     $ldap= $this->config->get_ldap_link();
313     /* Remove and write to LDAP */
314     plugin::remove_from_parent();
315     
316     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
317     $ldap->cd($this->dn);
318     $this->cleanup();
319     $ldap->modify ($this->attrs);
321     show_ldap_error($ldap->get_error(), _("Removing DHCP entries failed"));
323     /* Optionally execute a command after we're done */
324     $this->handle_post_events("remove");
325   }
328   /* Save data to object */
329   function save_object()
330   {
331     plugin::save_object();
332   }
335   /* Check supplied data */
336   function check()
337   {
338     /* Call common method to give check the hook */
339     $message= plugin::check();
340     
341     return ($message);
342   }
345   /* Save to LDAP */
346   function save()
347   {
348     $ldap= $this->config->get_ldap_link();
350     foreach ($this->dhcpObjectCache as $dn => $data){
352       if($this->dn != $this->orig_dn){
353         $dn = preg_replace("/".normalizePreg($this->orig_dn)."$/i",$this->dn,$dn);
354       }
356       /* Remove entry? */
357       if (count($data) == 0){
358         /* Check if exists, then remove... */
359         if($ldap->cat($dn)){
360           $ldap->rmdir_recursive($dn);
361           show_ldap_error($ldap->get_error(), _("Can't remove DHCP object!"));
362         }
363         continue;
364       }
366       /* Modify existing entry? */
367       if (isset($data['MODIFIED']) || $this->orig_dn != $this->dn){
369         if($ldap->cat($dn)){
370           $modify= TRUE;
371         } else {
372           $modify= FALSE;
373         }
375         /* Build new entry */
376         $attrs= array();
377         foreach ($data as $attribute => $values){
378           if ($attribute == "MODIFIED" || $attribute == "dn"){
379             continue;
380           }
381       
382           if(in_array($attribute,array("dhcpPrimaryDN","dhcpSecondaryDN","dhcpServerDN","dhcpFailOverPeerDN"))){
383             foreach($values as $v_key => $value){
384               $values[$v_key] = preg_replace("/".normalizePreg($this->orig_dn)."$/i",$this->dn,$value);
385             }
386           }
388           if (count($values)){
390             if($attribute == "dhcpOption"){
391               foreach($values as $key => $value){
392                 $option_name = trim(preg_replace("/[^ ]*$/","",$value));
393                 $option_value= trim(preg_replace("/^[^ ]*/","",$value));
394                 if(in_array($option_name,$this->quote_option)){
395                   $values[$key] = $option_name." \"".$option_value."\"";
396                 }
397               }
398             }
399             if (count($values) == 1){
400               $attrs[$attribute]= $values[0];
401             } else {
402               $attrs[$attribute]= $values;
403             }
404           } else {
405             if ($modify){
406               $attrs[$attribute]= array();
407             }
408           }
409         }
411         $ldap->cd($dn);
412         if ($modify){
413           $ldap->modify($attrs);
414           show_ldap_error($ldap->get_error(), _("Can't save DHCP object!"));
415         } else {
416           $ldap->add($attrs);
417           show_ldap_error($ldap->get_error(), _("Can't save DHCP object!"));
418         }
419       }
420     }
422     /* Self modify and place service dn entry */
423     $this->dhcpServiceDN= $this->dn;
425     plugin::save();
426     
427     /* Save data to LDAP */
428     $ldap->cd($this->dn);
429     $this->cleanup();
430     $ldap->modify ($this->attrs);
432     show_ldap_error($ldap->get_error(), _("Saving DHCP service failed"));
434     /* Optionally execute a command after we're done */
435     if ($this->initially_was_account == $this->is_account){
436       if ($this->is_modified){
437         $this->handle_post_events("modify");
438       }
439     } else {
440       $this->handle_post_events("add");
441     }
442  
443   }
446   function reload()
447   {
448     /* Init LDAP and load list */
449     $ldap= $this->config->get_ldap_link();
450     $ui= get_userinfo();
451     $me= $this->dn;
453     $list= get_list("(&(objectClass=dhcpService)(|(dhcpPrimaryDN=$me)(dhcpSecondaryDN=$me)(dhcpServerDN=$me)(dhcpFailOverPeerDN=$me)))", $ui->subtreeACL, $this->config->current['BASE'], array("cn"));
454     $final= array();
455     foreach ($list as $value){
457       /* Set header */
458       $sortpart= split(",", $value['dn']);
459       $sortpart= array_reverse($sortpart);
460       $tmp= implode(",", $sortpart);
462       $final[$value['dn']]= $tmp."!"._("Global options");
464       /* Read all sub entries to place here */
465       $ldap->cd($value['dn']);
466       $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpLog)(objectClass=dhcpClass)(objectClass=dhcpSubClass)(objectClass=dhcpHost)(objectClass=dhcpGroup)(objectClass=dhcpPool)(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork)(objectClass=dhcpOptions)(objectClass=dhcpTSigKey)(objectClass=dhcpDnsZone)(objectClass=dhcpFailOverPeer))", array());
467       $this->serviceDN= $value['dn'];
469       while ($attrs= $ldap->fetch()){
470         $sattrs= array();
471         for ($i= 0; $i<$attrs['count']; $i++){
472           $sattrs[$attrs[$i]]= $attrs[$attrs[$i]];
473           unset($sattrs[$attrs[$i]]['count']);
474         }
475         $sattrs['dn']= $ldap->getDN();
477         foreach($sattrs as $name => $values){
478           if($name == "dhcpOption"){
479             foreach($values as $key => $value){
480               $value_name = trim(preg_replace("/[^ ]*$/","",$value));
481               $value_value= trim(preg_replace("/^[^ ]*/","",$value));
482               if(in_array($value_name,$this->quote_option)){
483                 $value_value = preg_replace("/^\"/","",$value_value);
484                 $value_value = preg_replace("/\"$/","",$value_value);
485                 $sattrs[$name][$key] = $value_name." ".$value_value;
486               }
487             }
488           }
489         }
491         $this->dhcpObjectCache[$ldap->getDN()]= $sattrs;
492         $tmp= preg_replace("/".$this->serviceDN."/", "", $ldap->getDN());
493         $indent= substr_count($tmp, ",");
494         $spaces= "";
495         for ($i= 0; $i<$indent; $i++){
496           $spaces.= "&nbsp;&nbsp;&nbsp;&nbsp;";
497         }
499         foreach ($this->types as $key => $val){
500           if (in_array("$key", $attrs['objectClass'])){
501             $type= $val;
502             break;
503           }
504         }
506         /* Prepare for sorting... */
507         $sortpart= split(",", $ldap->getDN());
508         $sortpart= array_reverse($sortpart);
509         $tmp= implode(",", $sortpart);
510         $final[$ldap->getDN()]= $tmp."!".$spaces.$type." '".$attrs['cn'][0]."'";
511       }
512     }
514     /* Sort it... */
515     natsort($final);
516     $this->dhcpSections= array();
517     foreach ($final as $key => $val){
518       $this->dhcpSections[$key]= preg_replace('/^[^!]+!(.*)$/', '\\1', $val);
519     }
521   }
524   function objectType($dn)
525   {
526     $type= "";
527     $types= array("dhcpService", "dhcpClass", "dhcpSubClass", "dhcpHost",
528                   "dhcpGroup", "dhcpPool", "dhcpSubnet", "dhcpSharedNetwork");
530     foreach ($this->dhcpObjectCache[$dn]['objectClass'] as $oc){
531       if (in_array($oc, $types)){
532         $type= $oc;
533         break;
534       }
535     }
537     /* That should not happen... */
538     if ($type == ""){
539       print_red(_("DHCP configuration set is unknown. Please contact your system administrator."));
540     }
541     
542     return ($type);
543   }
547 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
548 ?>