Code

c3ee2fbb575025d8c48103a840139b5becc8fe5e
[gosa.git] / plugins / admin / systems / class_servDHCP.inc
1 <?php
3 class servdhcp extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account= TRUE;
7   var $attributes= array();
8   var $objectclasses= array();
10   /* Section storage */
11   var $dhcpSections= array();
12   var $dhcpObjectCache= array();
13   var $current_object= "";
14   var $types= array();
15   var $serviceDN= "";
17   function servdhcp ($config, $dn= NULL, $parent= NULL)
18   {
19     plugin::plugin ($config, $dn, $parent);
21     $this->types= array(  "dhcpLog" => _("Logging"),
22             "dhcpService" => _("Global options"),
23             "dhcpClass" => _("Class"),
24             "dhcpSubClass" => _("Subclass"),
25             "dhcpHost" => _("Host"),
26             "dhcpGroup" => _("Group"),
27             "dhcpPool" => _("Pool"),
28             "dhcpSubnet" => _("Subnet"),
29             "dhcpFailOverPeer" => _("Failover peer"),
30             "dhcpSharedNetwork" => _("Shared network"));
32     /* Load information about available services */
33     $this->reload(); 
34   }
37   function execute()
38   {
39     /* Call parent execute */
40     plugin::execute();
42     /* Fill templating stuff */
43     $smarty= get_smarty();
44     $display= "";
46     /* Section Creation? */
47     if (isset($_POST['create_section']) && isset($_POST['section'])){
48       $section= $_POST['section'];
49       if (isset(dhcpNewSectionDialog::$sectionMap[$section])){
50         $this->dialog= new $section($this->current_object);
51         $this->current_object= "";
52       } else {
53         $this->dialog= NULL;
54       }
55     }
57     /* Cancel section creation? */
58     if (isset($_POST['cancel_section']) || isset($_POST['cancel_dhcp'])){
59       $this->dialog= NULL;
60     }
62     /* Save changes */
63     if (isset($_POST['save_dhcp'])){
64       $this->dialog->save_object();
65       $messages= $this->dialog->check($this->dhcpObjectCache);
66       if (count($messages)){
67         show_errors($messages);
68       } else {
69         $dn= $this->dialog->dn;
70         $class= get_class($this->dialog);
71         $type= $this->types[$class];
72         $indent= substr_count(preg_replace("/".$this->serviceDN."/", '', $dn), ",");
73         $spaces= "";
74         for ($i= 0; $i<$indent; $i++){
75           $spaces.= "&nbsp;&nbsp;&nbsp;&nbsp;";
76         }
77         $data= $this->dialog->save();
78         if ($this->current_object == ""){
79           /* New object */
80           $newsects= array();
81           foreach ($this->dhcpSections as $key => $dsc){
82             $newsects[$key]= $dsc;
83             if ($key == $dn){
84               $spaces.= "&nbsp;&nbsp;&nbsp;&nbsp;";
85               $newsects[$data['dn']]= "$spaces$type '".preg_replace('/^[^=]+=([^,]+),.*$/', '\1', $data['dn'])."'";
86             }
87           }
88           $this->dhcpObjectCache[$data['dn']]= $data;
89           $this->dhcpSections= $newsects;
90         } else {
91           if ($dn != $data['dn']){
92             /* Old object, new name */
93             $this->dhcpObjectCache[$dn]= array();
94             $this->dhcpObjectCache[$data['dn']]= $data;
96             /* If we renamed a section, we've to rename a couple of objects, too */
97             foreach ($this->dhcpObjectCache as $key => $dsc){
98               if (preg_match("/,$dn$/", $key)){
99                 $new_dn= preg_replace("/,$dn$/", ",".$data['dn'], $key);
100                 $dsc['MODIFIED']= TRUE;
101                 $this->dhcpObjectCache[$new_dn]= $dsc;
102                 unset($this->dhcpObjectCache[$key]);
103               }
104             }
105             $newsects= array();
106             foreach ($this->dhcpSections as $key => $dsc){
107               if ($key == $dn){
108                 $newsects[$data['dn']]= "$spaces$type '".preg_replace('/^[^=]+=([^,]+),.*$/', '\1', $data['dn'])."'";
109                 continue;
110               }
111               if (preg_match("/,$dn$/", $key)){
112                 $new_dn= preg_replace("/,$dn$/", ",".$data['dn'], $key);
113                 $newsects[$new_dn]= $dsc;
114               } else {
115                 $newsects[$key]= $dsc;
116               }
117             }
118             $this->dhcpSections= $newsects;
120           } else {
121             /* Old object, old name */
122             $this->dhcpObjectCache[$data['dn']]= $data;
123           }
124         }
125         $this->dialog= NULL;
126       }
127     }
129     /* Remove section? */
130     if (isset($_POST['delete_dhcp_confirm'])){
131       if (chkacl($this->acl, "delete") == ""){
132         unset($this->dhcpSections[$this->current_object]);
133         unset($this->dhcpObjectCache[$this->current_object]);
134         $this->dhcpObjectCache[$this->current_object]= array();
135         foreach ($this->dhcpSections as $key => $value){
136           if (preg_match("/".$this->current_object."$/", $key)){
137             unset($this->dhcpSections[$key]);
138             unset($this->dhcpObjectCache[$key]);
139             $this->dhcpObjectCache[$key]= array();
140           }
141         }
142       } else {
143         print_red(_("You're not allowed to remove DHCP sections!"));
144       }
145       $this->dialog= NULL;
146     }
148     /* Look for post entries */
149     foreach($_POST as $name => $value){
150       
151       /* Insert new section? */
152       if (preg_match('/^insertDhcp_.*_x$/', $name)){
153         $dn= base64_decode(preg_replace('/^insertDhcp_([^_]+)_x$/', '\1', $name));
154         if (isset($this->dhcpObjectCache[$dn])){
155           $this->dialog= new dhcpNewSectionDialog($this->objectType($dn));
156           $this->current_object= $dn;
157           $this->dialog->acl= $this->acl;
158         }
159       }
161       /* Edit section? */
162       if (preg_match('/^editDhcp_.*_x$/', $name)){
163         $dn= base64_decode(preg_replace('/^editDhcp_([^_]+)_x$/', '\1', $name));
164         if (isset($this->dhcpObjectCache[$dn])){
165           $section= $this->objectType($dn);
166           $this->current_object= $dn;
167           $this->dialog= new $section($this->dhcpObjectCache[$dn]);
168         }
169       }
171       /* Remove section? */
172       if (preg_match('/^delDhcp_.*_x$/', $name)){
173         $dn= base64_decode(preg_replace('/^delDhcp_([^_]+)_x$/', '\1', $name));
174         if (isset($this->dhcpObjectCache[$dn])){
175           $this->current_object= $dn;
176           $this->dialog= 1;
177           $smarty->assign("warning", sprintf(_("You're about to delete the DHCP section '%s'."), $dn));
178           return($smarty->fetch(get_template_path('remove_dhcp.tpl', TRUE)));
179         }
180       }
182     }
184     /* Do we need to flip is_account state? */
185     if (isset($_POST['modify_state'])){
186       $this->is_account= !$this->is_account;
187     }
189     /* Show tab dialog headers */
190     if ($this->is_account){
191       $display= $this->show_header(_("Remove DHCP service"),
192           _("This server has DHCP features enabled. You can disable them by clicking below."));
193     } else {
194       $display= $this->show_header(_("Add DHCP service"),
195           _("This server has DHCP features disabled. You can enable them by clicking below."));
196       return ($display);
197     }
200     /* Show dialog
201      */
202     if($this->dialog != NULL && !is_int($this->dialog)){
203       $this->dialog->save_object();
204       $this->dialog->parent = $this;
205       return($this->dialog->execute());
206     }
208     /* Create Listbox with existing Zones
209      */
210     $DhcpList = new divSelectBox("dhcpSections");
211     $DhcpList->SetHeight(300);
213     /* Add entries to divlist
214      */
215     $editImgIns = "<input type='image' src='images/list_new.png' name='insertDhcp_%s' title='"._("Insert new DHCP section")."'>".
216       "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>".
217       "<input type='image' src='images/edittrash.png' name='delDhcp_%s' title='"._("Remove DHCP section")."'>";
218     $editImgInsNoDel = "<input type='image' src='images/list_new.png' name='insertDhcp_%s' title='"._("Insert new DHCP section")."'>".
219       "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>";
220     $editImg = "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>".
221       "<input type='image' src='images/edittrash.png' name='delDhcp_%s' title='"._("Remove DHCP section")."'>";
222     foreach($this->dhcpSections as $section => $values ){
223       if (count(dhcpNewSectionDialog::$sectionMap[$this->objectType($section)])){
224         if ($this->objectType($section) == "dhcpService"){
225           $DhcpList->AddEntry(array(
226               array("string" => $values),
227               array("string" => str_replace("%s",base64_encode($section),$editImgInsNoDel), "attach" => "style='text-align:right;'")
228               ));
229         } else {
230           $DhcpList->AddEntry(array(
231               array("string" => $values),
232               array("string" => str_replace("%s",base64_encode($section),$editImgIns), "attach" => "style='text-align:right;'")
233               ));
234         }
235       } else {
236         $DhcpList->AddEntry(array(
237               array("string" => $values),
238               array("string" => str_replace("%s",base64_encode($section),$editImg), "attach" => "style='text-align:right;'")
239               ));
240       }
241     }
243     $smarty->assign("dhcpACL",chkacl($this->acl,"servdhcp"));
245     /* Display tempalte */
246     $smarty->assign("DhcpList",$DhcpList->DrawList());
247     $display.= $smarty->fetch(get_template_path('servdhcp.tpl', TRUE));
248     return($display);
249   }
251   function remove_from_parent()
252   {
253     /* This cannot be removed... */
254   }
257   /* Save data to object */
258   function save_object()
259   {
260     plugin::save_object();
261   }
264   /* Check supplied data */
265   function check()
266   {
267     /* Call common method to give check the hook */
268     $message= plugin::check();
269     
270     return ($message);
271   }
274   /* Save to LDAP */
275   function save()
276   {
277     $ldap= $this->config->get_ldap_link();
279     foreach ($this->dhcpObjectCache as $dn => $data){
281       /* Remove entry? */
282       if (count($data) == 0){
283         /* Check if exists, then remove... */
284         if($ldap->cat($dn)){
285           $ldap->rmdir_recursive($dn);
286           show_ldap_error($ldap->get_error(), _("Can't remove DHCP object!"));
287         }
288         continue;
289       }
291       /* Modify existing entry? */
292       if (isset($data['MODIFIED'])){
294         if($ldap->cat($dn)){
295           $modify= TRUE;
296         } else {
297           $modify= FALSE;
298         }
300         /* Build new entry */
301         $attrs= array();
302         foreach ($data as $attribute => $values){
303           if ($attribute == "MODIFIED" || $attribute == "dn"){
304             continue;
305           }
306           if (count($values)){
307             if (count($values) == 1){
308               $attrs[$attribute]= $values[0];
309             } else {
310               $attrs[$attribute]= $values;
311             }
312           } else {
313             if ($modify){
314               $attrs[$attribute]= array();
315             }
316           }
317         }
319         $ldap->cd($dn);
320         if ($modify){
321           $ldap->modify($attrs);
322           show_ldap_error($ldap->get_error(), _("Can't save DHCP object!"));
323         } else {
324           $ldap->add($attrs);
325           show_ldap_error($ldap->get_error(), _("Can't save DHCP object!"));
326         }
327       }
328     }
329   }
332   function reload()
333   {
334     /* Init LDAP and load list */
335     $ldap= $this->config->get_ldap_link();
336     $ui= get_userinfo();
337     $me= $this->dn;
339     $list= get_list("(&(objectClass=dhcpService)(|(dhcpPrimaryDN=$me)(dhcpSecondaryDN=$me)(dhcpServerDN=$me)(dhcpFailOverPeerDN=$me)))", $ui->subtreeACL, $this->config->current['BASE'], array("cn"));
340     $final= array();
341     foreach ($list as $value){
343       /* Set header */
344       $sortpart= split(",", $value['dn']);
345       $sortpart= array_reverse($sortpart);
346       $tmp= implode(",", $sortpart);
348       $final[$value['dn']]= $tmp."!"._("Global options");
350       /* Read all sub entries to place here */
351       $ldap->cd($value['dn']);
352       $ldap->search("(|(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());
353       $this->serviceDN= $value['dn'];
355       while ($attrs= $ldap->fetch()){
356         $sattrs= array();
357         for ($i= 0; $i<$attrs['count']; $i++){
358           $sattrs[$attrs[$i]]= $attrs[$attrs[$i]];
359           unset($sattrs[$attrs[$i]]['count']);
360         }
361         $sattrs['dn']= $ldap->getDN();
362         $this->dhcpObjectCache[$ldap->getDN()]= $sattrs;
363         $tmp= preg_replace("/".$this->serviceDN."/", "", $ldap->getDN());
364         $indent= substr_count($tmp, ",");
365         $spaces= "";
366         for ($i= 0; $i<$indent; $i++){
367           $spaces.= "&nbsp;&nbsp;&nbsp;&nbsp;";
368         }
370         foreach ($this->types as $key => $val){
371           if (in_array("$key", $attrs['objectClass'])){
372             $type= $val;
373             break;
374           }
375         }
377         /* Prepare for sorting... */
378         $sortpart= split(",", $ldap->getDN());
379         $sortpart= array_reverse($sortpart);
380         $tmp= implode(",", $sortpart);
381         $final[$ldap->getDN()]= $tmp."!".$spaces.$type." '".$attrs['cn'][0]."'";
382       }
383     }
385     /* Sort it... */
386     natsort($final);
387     $this->dhcpSections= array();
388     foreach ($final as $key => $val){
389       $this->dhcpSections[$key]= preg_replace('/^[^!]+!(.*)$/', '\\1', $val);
390     }
392   }
395   function objectType($dn)
396   {
397     $type= "";
398     $types= array("dhcpService", "dhcpClass", "dhcpSubClass", "dhcpHost",
399                   "dhcpGroup", "dhcpPool", "dhcpSubnet", "dhcpSharedNetwork");
401     foreach ($this->dhcpObjectCache[$dn]['objectClass'] as $oc){
402       if (in_array($oc, $types)){
403         $type= $oc;
404         break;
405       }
406     }
408     /* That should not happen... */
409     if ($type == ""){
410       print_red(_("DHCP configuration set is unknown. Please contact your system administrator."));
411     }
412     
413     return ($type);
414   }
418 //vim:tabstop=2:expandtab:softtab=2:shiftwidth=2:filetype=php:syntax:ruler:
419 ?>