Code

Added range checks
[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 dhcpPlugin
22 {
23   /* Used attributes */
24   var $dhcpNetMask= 24;
25   var $dhcpRange= "";
26   var $range_start= "";
27   var $range_stop= "";
28   var $use_range= FALSE;
30   /* attribute list for save action */
31   var $objectclasses= array('top', 'dhcpSubnet', 'dhcpOptions');
33   function dhcpSubnet($attrs)
34   {
35     dhcpPlugin::dhcpPlugin($attrs);
37     if (!$this->new){
38       /* Load attributes */
39       foreach (array("dhcpNetMask", "dhcpRange") as $attr){
40         if (isset($attrs[$attr][0])){
41           $this->$attr= $attrs[$attr][0];
42         }
43       }
44       if (isset($attrs['dhcpRange']) && count($attrs['dhcpRange'])){
45         $this->use_range= TRUE;
46         list($this->range_start, $this->range_stop)= preg_split('/\s+/', $this->dhcpRange);
47       }
48     }
50     $this->dhcpNetMask= normalize_netmask($this->dhcpNetMask);
51   }
54   function execute()
55   {
56     $smarty= get_smarty();
57     $smarty->assign("cn", $this->cn);
58     $smarty->assign("dhcp_netmask", $this->dhcpNetMask);
60     /* Prepare range */
61     if ($this->use_range){
62       $smarty->assign("use_range", "checked");
63       $smarty->assign("range_disabled", "");
64     } else {
65       $smarty->assign("use_range", "");
66       $smarty->assign("range_disabled", "disabled");
67     }
68     $smarty->assign("range_start", $this->range_start);
69     $smarty->assign("range_stop", $this->range_stop);
71     /* Show main page */
72     $display= $smarty->fetch(get_template_path('dhcp_subnet.tpl', TRUE)).$this->network->execute();
74     /* Merge arrays for advanced view */
75     foreach (array("options", "statements") as $type){
76       $this->advanced->$type= $this->$type + $this->network->$type;
77     }
79     $display.= $this->advanced->execute();
81     /* Merge back for removals */
82     foreach (array("options", "statements") as $type){
83       $this->$type= $this->advanced->$type;
84       $this->network->$type= $this->advanced->$type;
85     }
87     /* Add footer */
88     $display.= "<div style='width:100%;text-align:right;margin-top:5px;'><input type=submit name='save_dhcp' value='"._("Save")."'>".
89                "&nbsp;<input type=submit name='cancel_dhcp' value='"._("Cancel")."'></div>";
91     /* Show main page */
92     return $display;
93   }
96   function remove_from_parent()
97   {
98   }
101   /* Save data to object */
102   function save_object()
103   {
104     if(isset($_POST['dhcp_subnet_posted'])){
105       if (isset($_POST['cn'])){
106         $this->cn= validate($_POST['cn']);
107       } 
108       if (isset($_POST['dhcp_netmask'])){
109         $this->dhcpNetMask= validate($_POST['dhcp_netmask']);
110       } 
111       if (isset($_POST['use_range'])){
112         $this->use_range= TRUE;
113         $this->range_start= validate($_POST['range_start']);
114         $this->range_stop= validate($_POST['range_stop']);
115       } else {
116         $this->use_range= FALSE;
117       }
119       /* Move range to internal variable */
120       $this->dhcpRange= $this->range_start." ".$this->range_stop;
121       dhcpPlugin::save_object();
122     }
123   }
126   /* Check values */
127   function check($cache)
128   {
129     $message= array();
131     /* All required fields are set? */
132     if ($this->cn == ""){
133       $message[]= _("Required field 'Network address' is not filled.");
134     }
135     if ($this->dhcpNetMask == ""){
136       $message[]= _("Required field 'Netmask' is not filled.");
137     }
139     /* cn already used? */
140     if ($this->orig_cn != $this->cn || $this->new){
142       foreach($cache as $dn => $dummy){
143         if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
144           $message[]= _("The name for this section is already used!");
145           break;
146         }
147       }
148     }
150     /* IP's? */
151     foreach(array('dhcpNetMask' => _("Netmask"), 'cn' => _("Network address"), 'range_start' => _("Range"), 'range_stop' => _("Range")) as $attr => $str){
152       if ($this->$attr != "" && !is_ip($this->$attr)){
153         $message[]= sprintf(_("The field '%s' contains an invalid IP address"), $str);
154       }
155     }
157     /* Check ip range */
158     if(!is_ip_range($this->range_start,$this->range_stop)){
159         $message[] = _("Field 'Range' contains invalid IP range.");
160     }
162     /* Check if range is in the network */
163     if (!is_in_network($this->cn, $this->dhcpNetMask, $this->range_start) ||
164         !is_in_network($this->cn, $this->dhcpNetMask, $this->range_stop)){
165       $message[] = _("'Range' is not inside the configured network.");
166     }
168     return $message;
169   }
172   /* Save to LDAP */
173   function save()
174   {
175     dhcpPlugin::save();
177     /* Move dn to the result */
178     $this->attrs['dhcpNetMask']= array(netmask_to_bits($this->dhcpNetMask));
179     if ($this->dhcpRange != ""){
180       $this->attrs['dhcpRange']= array($this->range_start." ".$this->range_stop);
181     } else {
182       $this->attrs['dhcpRange']= array();
183     }
185     return ($this->attrs);
186   }
187   
189 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
190 ?>