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($parent,$attrs)
34 {
35 dhcpPlugin::dhcpPlugin($parent,$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 /* Assign ACLs */
61 $smarty->assign("acl",$this->parent->getacl(""));
63 /* Prepare range */
64 if ($this->use_range){
65 $smarty->assign("use_range", "checked");
66 $smarty->assign("range_disabled", "");
67 } else {
68 $smarty->assign("use_range", "");
69 $smarty->assign("range_disabled", "disabled");
70 }
71 $smarty->assign("range_start", $this->range_start);
72 $smarty->assign("range_stop", $this->range_stop);
74 /* Show main page */
75 $display= $smarty->fetch(get_template_path('dhcp_subnet.tpl', TRUE, dirname(__FILE__))).$this->network->execute();
77 /* Merge arrays for advanced view */
78 $this->fix_options();
79 foreach (array("options", "statements") as $type){
80 $this->advanced->$type= $this->$type + $this->network->$type;
81 }
83 $display.= $this->advanced->execute();
85 /* Merge back for removals */
86 foreach (array("options", "statements") as $type){
87 $this->$type= $this->advanced->$type;
88 $this->network->$type= $this->advanced->$type;
89 }
91 /* Add footer */
92 $display.= "<div style='width:100%;text-align:right;margin-top:5px;'>";
93 if(preg_match("/w/",$this->parent->getacl(""))){
94 $display.= "<input type=submit name='save_dhcp' value='".msgPool::saveButton()."'> ";
95 }
96 $display.= "<input type=submit name='cancel_dhcp' value='".msgPool::cancelButton()."'>";
97 $display.= "</div>";
100 /* Show main page */
101 return $display;
102 }
105 function remove_from_parent()
106 {
107 }
110 /* Save data to object */
111 function save_object()
112 {
114 /* Skip here if do not have write permissions here
115 Still executed at bottom - dhcpPlugin::save_object();
116 */
117 if(isset($_POST['dhcp_subnet_posted']) && preg_match("/w/",$this->parent->getacl(""))){
118 if (isset($_POST['cn'])){
119 $this->cn= validate(get_post('cn'));
120 }
121 if (isset($_POST['dhcp_netmask'])){
122 $this->dhcpNetMask= validate(get_post('dhcp_netmask'));
123 }
124 if (isset($_POST['use_range'])){
125 $this->use_range= TRUE;
126 $this->range_start= validate(get_post('range_start'));
127 $this->range_stop= validate(get_post('range_stop'));
128 } else {
129 $this->use_range= FALSE;
130 }
132 /* Move range to internal variable */
133 $this->dhcpRange= $this->range_start." ".$this->range_stop;
134 }
135 dhcpPlugin::save_object();
136 }
139 /* Check values */
140 function check()
141 {
142 $message= array();
144 $cache = $this->parent->dhcpObjectCache;
146 /* All required fields are set? */
147 if ($this->cn == ""){
148 $message[]= msgPool::required(_("Network address"));
149 }
150 if ($this->dhcpNetMask == ""){
151 $message[]= msgPool::required(_("Netmask"));
152 }
154 /* cn already used? */
155 if ($this->orig_cn != $this->cn || $this->new){
157 foreach($cache as $dn => $dummy){
158 if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
159 $message[]= msgPool::duplicated(_("Name"));
160 break;
161 }
162 }
163 }
165 /* IP's? */
166 foreach(array(
167 'dhcpNetMask' => _("Netmask"),
168 'cn' => _("Network address"),
169 'range_start' => _("Range"),
170 'range_stop' => _("Range")) as $attr => $str){
171 if ($this->$attr != "" && !tests::is_ip($this->$attr)){
172 $message[]= msgPool::invalid($str,"","","192.168.0.23");
173 }
174 }
176 /* Check ip range */
177 if ($this->use_range){
178 if(!tests::is_ip_range($this->range_start,$this->range_stop)){
179 $message[]= msgPool::invalid(_("Network range"));
180 }
182 /* Check if range is in the network */
183 if (!tests::is_in_network($this->cn, $this->dhcpNetMask, $this->range_start) ||
184 !tests::is_in_network($this->cn, $this->dhcpNetMask, $this->range_stop)){
185 $message[] = _("'Range' is not inside the configured network.");
186 }
187 }
189 /* Check external plugins */
190 $net= $this->network->check();
191 $adv= $this->advanced->check();
192 $message= array_merge($message, $net, $adv);
194 return $message;
195 }
198 /* Save to LDAP */
199 function save()
200 {
201 dhcpPlugin::save();
203 /* Move dn to the result */
204 $this->attrs['dhcpNetMask']= array(netmask_to_bits($this->dhcpNetMask));
205 if ($this->use_range && !empty($this->dhcpRange)){
206 $this->attrs['dhcpRange']= array($this->range_start." ".$this->range_stop);
207 } else {
208 $this->attrs['dhcpRange']= array();
209 }
211 return ($this->attrs);
212 }
214 }
215 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
216 ?>