Code

Updated dhcp stuff
[gosa.git] / plugins / admin / systems / class_dhcpSubnet.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 dhcpSubnet extends plugin
22 {
23   /* Used attributes */
24   var $cn= "";
25   var $orig_cn= "";
26   var $dhcpNetMask= 24;
27   var $dhcpRange= "";
28   var $range_start= "";
29   var $range_stop= "";
30   var $options= array();
31   var $statements= array();
32   var $use_range= FALSE;
34   /* Subobjects */
35   var $network;
36   var $advanced;
38   /* attribute list for save action */
39   var $attributes= array();
40   var $objectclasses= array();
42   function dhcpSubnet($attrs)
43   {
44     if (is_array($attrs)){
45       $this->dn= $attrs['dn'];
48       /* Load attributes */
49       foreach (array("cn", "dhcpNetMask", "dhcpRange") as $attr){
50         if (isset($attrs[$attr][0])){
51           $this->$attr= $attrs[$attr][0];
52         }
53       }
54       if (isset($attrs['dhcpRange'])){
55         $this->use_range= TRUE;
56         list($this->range_start, $this->range_stop)= preg_split('/\s+/', $this->dhcpRange);
57       }
59       /* Load options */
60       if (isset($attrs['dhcpOption'])){
61         foreach ($attrs['dhcpOption'] as $opt){
62           $idx= preg_replace('/\s.+$/', '', $opt);
63           $value= preg_replace('/^[^\s]+\s/', '', $opt);
64           $this->options[$idx]= $value;
65         }
66       }
68       /* Load statements */
69       if (isset($attrs['dhcpStatements'])){
70         foreach ($attrs['dhcpStatements'] as $opt){
71           $idx= preg_replace('/\s.+$/', '', $opt);
72           $value= preg_replace('/^[^\s]+\s/', '', $opt);
73           $this->statements[$idx]= $value;
74         }
75       }
77     } else {
78       /* We keep the parent dn here if it's new */
79       $this->dn= $attrs;
80       $this->new= TRUE;
81     }
83     /* Load network module */
84     $this->network= new dhcpNetwork();
85     $this->network->options= $this->options;
86     $this->network->statements= $this->statements;
87     $this->advanced= new dhcpAdvanced();
88     $this->advanced->options= $this->options;
89     $this->advanced->statements= $this->statements;
90     $this->advanced->setAutoStatements(array("fixed-address"));
92     /* Save CN for later reference */
93     $this->orig_cn= $this->cn;
94     $this->dhcpNetMask= normalize_netmask($this->dhcpNetMask);
95   }
98   function execute()
99   {
100     $smarty= get_smarty();
101     $smarty->assign("cn", $this->cn);
102     $smarty->assign("dhcp_netmask", $this->dhcpNetMask);
104     /* Prepare range */
105     if ($this->use_range){
106       $smarty->assign("use_range", "checked");
107       $smarty->assign("range_disabled", "");
108     } else {
109       $smarty->assign("use_range", "");
110       $smarty->assign("range_disabled", "disabled");
111     }
112     $smarty->assign("range_start", $this->range_start);
113     $smarty->assign("range_stop", $this->range_stop);
115     /* Show main page */
116     $display= $smarty->fetch(get_template_path('dhcp_subnet.tpl', TRUE)).$this->network->execute();
118     /* Merge arrays for advanced view */
119     foreach (array("options", "statements") as $type){
120       $tmp= array_merge($this->$type, $this->network->$type);
121       $this->advanced->$type= $tmp;
122     }
124     $display.= $this->advanced->execute();
126     /* Merge back for removals */
127     foreach (array("options", "statements") as $type){
128       $this->$type= $this->advanced->$type;
129       $this->network->$type= $this->advanced->$type;
130     }
132     /* Add footer */
133     $display.= "<div style='width:100%;text-align:right;margin-top:5px;'><input type=submit name='save_dhcp' value='"._("Save")."'>".
134                "&nbsp;<input type=submit name='cancel_dhcp' value='"._("Cancel")."'></div>";
136     /* Show main page */
137     return $display;
138   }
141   function remove_from_parent()
142   {
143   }
146   /* Save data to object */
147   function save_object()
148   {
149     if (isset($_POST['cn'])){
150       $this->cn= validate($_POST['cn']);
151     } 
152     if (isset($_POST['dhcp_netmask'])){
153       $this->dhcpNetMask= validate($_POST['dhcp_netmask']);
154     } 
155     if (isset($_POST['use_range'])){
156       $this->use_range= TRUE;
157       $this->range_start= validate($_POST['range_start']);
158       $this->range_stop= validate($_POST['range_stop']);
159     } else {
160       $this->use_range= FALSE;
161     }
163     /* Save sub-objects */
164     $this->network->save_object();
165     $this->advanced->save_object();
167     /* Merge arrays for advanced view */
168     foreach (array("options", "statements") as $type){
169       $tmp= array_merge($this->$type, $this->network->$type);
170       $this->advanced->$type= $tmp;
171     }
173   }
176   /* Check values */
177   function check($cache)
178   {
179     $message= array();
181     /* All required fields are set? */
182     if ($this->cn == ""){
183       $message[]= _("Required field 'Network address' is not filled.");
184     }
185     if ($this->dhcpNetMask == ""){
186       $message[]= _("Required field 'Netmask' is not filled.");
187     }
189     /* cn already used? */
190     if ($this->orig_cn != $this->cn || $this->new){
192       foreach($cache as $dn => $dummy){
193         if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
194           $message[]= _("The name for this section is already used!");
195           break;
196         }
197       }
198     }
200     /* IP's? */
201     foreach(array('dhcpNetMask' => _("Netmask"), 'cn' => _("Network address"), 'range_start' => _("Range"), 'range_stop' => _("Range")) as $attr => $str){
202       if ($this->$attr != "" && !is_ip($this->$attr)){
203         print_red(sprintf(_("The field '%s' contains an invalid IP address"), $str));
204       }
205     }
207     return $message;
208   }
211   /* Save to LDAP */
212   function save()
213   {
214     /* Merge arrays for network and advanced view */
215     foreach (array("options", "statements") as $type){
216       $tmp= array_merge($this->$type, $this->network->$type, $this->advanced->$type);
217       $this->$type= $tmp;
218     }
220     /* Add cn if we're new */
221     if ($this->new){
222       $this->dn= "cn=".$this->cn.",".$this->dn;
223     } else {
224       $this->dn= "cn=".$this->cn.preg_replace('/^cn=[^,]+/', '', $this->dn);
225     }
227     /* Assemble new entry - options */
228     $this->attrs['dhcpOption']= array();
229     if (isset ($this->options) && count ($this->options)){
230       foreach ($this->options as $key => $val){
231         $this->attrs['dhcpOption'][]= "$key $val";
232       }
233     }
235     /* Assemble new entry - statements */
236     $this->attrs['dhcpStatements']= array();
237     if (isset ($this->statements) && count ($this->statements)){
238       foreach ($this->statements as $key => $val){
239         $this->attrs['dhcpStatements'][]= "$key $val";
240       }
241     }
243     /* Move dn to the result */
244     $this->attrs['dn']= $this->dn;
245     $this->attrs['cn']= array($this->cn);
246     $this->attrs['dhcpNetMask']= array(netmask_to_bits($this->dhcpNetMask));
247     if ($this->dhcpRange != ""){
248       $this->attrs['dhcpRange']= array($this->range_start." ".$this->range_stop);
249     } else {
250       $this->attrs['dhcpRange']= array();
251     }
252     $this->attrs['objectClass']= array('top', 'dhcpSubnet', 'dhcpOptions');
253     $this->attrs['MODIFIED']= TRUE;
255     return ($this->attrs);
256   }
257   
260 ?>