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