Code

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