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 $this->fix_options();
76 foreach (array("options", "statements") as $type){
77 $this->advanced->$type= $this->$type + $this->network->$type;
78 }
80 $display.= $this->advanced->execute();
82 /* Merge back for removals */
83 foreach (array("options", "statements") as $type){
84 $this->$type= $this->advanced->$type;
85 $this->network->$type= $this->advanced->$type;
86 }
88 /* Add footer */
89 $display.= "<div style='width:100%;text-align:right;margin-top:5px;'><input type=submit name='save_dhcp' value='".msgPool::saveButton()."'>".
90 " <input type=submit name='cancel_dhcp' value='".msgPool::cancelButton()."'></div>";
92 /* Show main page */
93 return $display;
94 }
97 function remove_from_parent()
98 {
99 }
102 /* Save data to object */
103 function save_object()
104 {
105 if(isset($_POST['dhcp_subnet_posted'])){
106 if (isset($_POST['cn'])){
107 $this->cn= validate(get_post('cn'));
108 }
109 if (isset($_POST['dhcp_netmask'])){
110 $this->dhcpNetMask= validate(get_post('dhcp_netmask'));
111 }
112 if (isset($_POST['use_range'])){
113 $this->use_range= TRUE;
114 $this->range_start= validate(get_post('range_start'));
115 $this->range_stop= validate(get_post('range_stop'));
116 } else {
117 $this->use_range= FALSE;
118 }
120 /* Move range to internal variable */
121 $this->dhcpRange= $this->range_start." ".$this->range_stop;
122 dhcpPlugin::save_object();
123 }
124 }
127 /* Check values */
128 function check()
129 {
130 $message= array();
132 $cache = $this->parent->dhcpObjectCache;
134 /* All required fields are set? */
135 if ($this->cn == ""){
136 $message[]= msgPool::required(_("Network address"));
137 }
138 if ($this->dhcpNetMask == ""){
139 $message[]= msgPool::required(_("Netmask"));
140 }
142 /* cn already used? */
143 if ($this->orig_cn != $this->cn || $this->new){
145 foreach($cache as $dn => $dummy){
146 if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
147 $message[]= msgPool::duplicated(_("Name"));
148 break;
149 }
150 }
151 }
153 /* IP's? */
154 foreach(array('dhcpNetMask' => _("Netmask"), 'cn' => _("Network address"), 'range_start' => _("Range"), 'range_stop' => _("Range")) as $attr => $str){
155 if ($this->$attr != "" && !tests::is_ip($this->$attr)){
156 $message[]= msgPool::invalid($str,"","","192.168.0.23");
157 }
158 }
160 /* Check ip range */
161 if ($this->use_range){
162 if(!tests::is_ip_range($this->range_start,$this->range_stop)){
163 $message[]= msgPool::invalid(_("Network range"));
164 }
166 /* Check if range is in the network */
167 if (!tests::is_in_network($this->cn, $this->dhcpNetMask, $this->range_start) ||
168 !tests::is_in_network($this->cn, $this->dhcpNetMask, $this->range_stop)){
169 $message[] = _("'Range' is not inside the configured network.");
170 }
171 }
173 /* Check external plugins */
174 $net= $this->network->check();
175 $adv= $this->advanced->check();
176 $message= array_merge($message, $net, $adv);
178 return $message;
179 }
182 /* Save to LDAP */
183 function save()
184 {
185 dhcpPlugin::save();
187 /* Move dn to the result */
188 $this->attrs['dhcpNetMask']= array(netmask_to_bits($this->dhcpNetMask));
189 if ($this->use_range && !empty($this->dhcpRange)){
190 $this->attrs['dhcpRange']= array($this->range_start." ".$this->range_stop);
191 } else {
192 $this->attrs['dhcpRange']= array();
193 }
195 return ($this->attrs);
196 }
198 }
199 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
200 ?>