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