Code

* Avoid removing of the service
[gosa.git] / plugins / admin / systems / class_dhcpService.inc
1 <?php
2 /*
3   This code is part of GOsa (https://gosa.gonicus.de)
4   Copyright (C) 2003-2007  Cajus Pollmeier
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or
9   (at your option) any later version.
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
21 class dhcpService extends plugin
22 {
23   /* Used attributes */
24   var $cn= "";
25   var $orig_cn= "";
26   var $dhcpPrimaryDN= "";
27   var $orig_dhcpPrimaryDN= "";
28   var $options= array();
29   var $statements= array();
30   var $default_lease_time= 600;
31   var $max_lease_time= 1700;
32   var $authoritative= TRUE;
33   var $get_lease_hostnames= TRUE;
34   var $ddns_update_style= "none";
35   var $ddns_styles= array('none', 'interim', 'ad-hoc');
37   /* Subobjects */
38   var $network;
39   var $advanced;
41   /* attribute list for save action */
42   var $attributes= array();
43   var $objectclasses= array();
46   function dhcpService($attrs)
47   {
48     /* Load statements / options */
49     if (is_array($attrs)){
50       $this->dn= $attrs['dn'];
51       $this->new= FALSE;
53       /* Load attributes */
54       foreach (array("cn", "dhcpPrimaryDN") as $attr){
55         if (isset($attrs[$attr][0])){
56           $this->$attr= $attrs[$attr][0];
57         }
58       }
60       /* Load options */
61       if (isset($attrs['dhcpOption'])){
62         foreach ($attrs['dhcpOption'] as $opt){
63           $idx= preg_replace('/\s.+$/', '', $opt);
64           $value= preg_replace('/^[^\s]+\s/', '', $opt);
65           $this->options[$idx]= $value;
66         }
67       }
69       /* Load statements */
70       if (isset($attrs['dhcpStatements'])){
71         foreach ($attrs['dhcpStatements'] as $opt){
72           $idx= preg_replace('/\s.+$/', '', $opt);
73           $value= preg_replace('/^[^\s]+\s/', '', $opt);
74           $this->statements[$idx]= $value;
75         }
76       }
78     } else {
79       /* We keep the parent dn here if it's new */
80       $this->dn= $attrs;
81       $this->new= TRUE;
82     }
84     /* Load network module */
85     $this->network= new dhcpNetwork();
86     $this->network->options= $this->options;
87     $this->network->statements= $this->statements;
88     $this->advanced= new dhcpAdvanced();
89     $this->advanced->options= $this->options;
90     $this->advanced->statements= $this->statements;
91     $this->advanced->setAutoStatements(array("default-lease-time", "max-lease-time", "authoritative", "server-name", "get-lease-hostnames", "server-identifier", "ddns-update-style"));
93     /* Save for later action */
94     $this->orig_dhcpPrimaryDN= $this->dhcpPrimaryDN;
95     $this->orig_cn= $this->cn;
96   }
98   function execute()
99   {
100     /* Show main page */
101     $smarty= get_smarty();
103     $smarty->assign('ddns_styles', $this->ddns_styles);
105     /* Show main page */
106     $display= $smarty->fetch(get_template_path('dhcp_service.tpl', TRUE)).$this->network->execute();
108     /* Merge arrays for advanced view */
109     foreach (array("options", "statements") as $type){
110       $tmp= array_merge($this->$type, $this->network->$type);
111       $this->advanced->$type= $tmp;
112     }
114     $display.= $this->advanced->execute();
116     /* Merge back for removals */
117     foreach (array("options", "statements") as $type){
118       $this->$type= $this->advanced->$type;
119       $this->network->$type= $this->advanced->$type;
120     }
122     /* Add footer */
123     $display.= "<div style='width:100%;text-align:right;margin-top:5px;'><input type=submit name='save_dhcp' value='"._("Save")."'>".
124       "&nbsp;<input type=submit name='cancel_dhcp' value='"._("Cancel")."'></div>";
127     return ($display);
129   }
131   function remove_from_parent()
132   {
133         /* Just remove the dn from the ldap, then we're done. Host
134            entries do not have any entries below themselfes. */
135         $ldap= $this->config->get_ldap_link();
136         $ldap->cd($this->dn);
137         $ldap->recursive_remove();
138         show_ldap_error($ldap->get_error());
140         $ldap->cat($this->dhcpPrimaryDN);
141         $attrs= $ldap->fetch();
142         $objectclasses= array();
143         for($i= 0; $i<$attrs['objectClass']['count']; $i++){
144                 if ($attrs['objectClass'][$i] != "dhcpServer"){
145                         $objectclasses[]= $attrs['objectClass'][$i];
146                 }
147         }
148         $attrs= array();
149         $attrs['dhcpServiceDN']= array();
150         $attrs['objectClass']= $objectclasses;
151         $ldap->cd($this->dhcpPrimaryDN);
152         $ldap->modify($attrs);
153         show_ldap_error($ldap->get_error());
156         /* Optionally execute a command after we're done */
157         $this->postremove();
158   }
161   /* Save data to object */
162   function save_object()
163   {
164         plugin::save_object();
166         /* Save base */
167         if (isset($_POST['base'])){
168                 $this->base= $_POST['base'];
169         }
170   }
173   /* Check values */
174   function check()
175   {
176         $message= array();
177         /* All required fields are set? */
178         if ($this->cn == ""){
179                 $message[]= _("Required field 'Name' is not filled.");
180         }
181         if ($this->dhcpPrimaryDN == ""){
182                 $message[]= _("No server specified to host dhcp service!");
183         }
185         return $message;
186   }
189   /* Save to LDAP */
190   function save()
191   {
192 echo "server-name";
193 echo "server-identifier";
194         plugin::save();
196         /* Get ldap mode */
197         if ($this->dn == "new"){
198                 $mode= "add";
199                 $this->dn= "cn=".$this->cn.",ou=dhcp,ou=configs,ou=systems,".$this->base;
200         } else {
201                 $mode= "modify";
202         }
204         /* Assemble new entry - options */
205         if (isset ($this->options) && count ($this->options)){
206                 $this->attrs['dhcpOption']= array();
207                 foreach ($this->options as $key => $val){
208                         $this->attrs['dhcpOption'][]= "$key $val";
209                 }
210         } else {
211                 if ($mode == "modify"){
212                         $this->attrs['dhcpOption']= array();
213                 }
214         }
216         /* Assemble new entry - statements */
217         if (isset ($this->statements) && count ($this->statements)){
218                 $this->attrs['dhcpStatements']= array();
219                 foreach ($this->statements as $key => $val){
220                         $this->attrs['dhcpStatements'][]= "$key $val";
221                 }
222         } else {
223                 if ($mode == "modify"){
224                         $this->attrs['dhcpStatements']= array();
225                 }
226         }
228         /* Do LDAP action */
229         $ldap= $this->config->get_ldap_link();
230         if ($mode == "add"){
231                 $ldap->cd($this->config->current['BASE']);
232                 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
233         }
234         $ldap->cd($this->dn);
235         $ldap->$mode($this->attrs);
236         show_ldap_error($ldap->get_error());
238         /* Modify server entry to keep our 'dn' */
239         $ldap->cat($this->dhcpPrimaryDN);
240         $attrs= $ldap->fetch();
241         $objectclasses= array();
242         for($i= 0; $i<$attrs['objectClass']['count']; $i++){
243                 if ($attrs['objectClass'][$i] != "dhcpServer"){
244                         $objectclasses[]= $attrs['objectClass'][$i];
245                 }
246         }
247         $objectclasses[]= "dhcpServer";
248         $attrs= array();
249         $attrs['dhcpServiceDN']= $this->dn;
250         $attrs['objectClass']= $objectclasses;
251         $ldap->cd($this->dhcpPrimaryDN);
252         $ldap->modify($attrs);
253         show_ldap_error($ldap->get_error());
255         /* Modify old server entry */
256         if ( ($this->orig_dhcpPrimaryDN != "")
257                 && ($this->orig_dhcpPrimaryDN != $this->dhcpPrimaryDN)){
259                 $ldap->cat($this->orig_dhcpPrimaryDN);
260                 $attrs= $ldap->fetch();
261                 if (isset($attrs['dhcpServiceDN'])){
262                         $objectclasses= array();
263                         for($i= 0; $i<$attrs['objectClass']['count']; $i++){
264                                 if ($attrs['objectClass'][$i] != "dhcpServer"){
265                                         $objectclasses[]= $attrs['objectClass'][$i];
266                                 }
267                         }
268                         $attrs= array();
269                         $attrs['dhcpServiceDN']= array();
270                         $attrs['objectClass']= $objectclasses;
271                         $ldap->cd($this->orig_dhcpPrimaryDN);
272                         $ldap->modify($attrs);
273                         show_ldap_error($ldap->get_error());
274                 }
275         }
276   }
277   
280 ?>