1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003 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 dhcpPool extends dhcpPlugin
22 {
23 /* Used attributes */
24 var $dhcpRange= "";
25 var $range_start= "";
26 var $range_stop= "";
28 /* attribute list for save action */
29 var $objectclasses= array('top', 'dhcpPool');
31 function dhcpPool($attrs)
32 {
33 dhcpPlugin::dhcpPlugin($attrs);
35 /* Load attributes */
36 if (!$this->new){
37 $this->dhcpRange= $attrs['dhcpRange'][0];
38 list($this->range_start, $this->range_stop)= preg_split('/\s+/', $this->dhcpRange);
39 }
41 $this->advanced->setAutoOptions(array("host-name"));
42 $this->advanced->setAutoStatements(array("fixed-address"));
43 }
45 function execute()
46 {
47 $smarty= get_smarty();
48 $smarty->assign("cn", $this->cn);
49 $smarty->assign("range_start", $this->range_start);
50 $smarty->assign("range_stop", $this->range_stop);
52 /* Show main page */
53 $display= $smarty->fetch(get_template_path('dhcp_pool.tpl', TRUE)).$this->network->execute();
55 /* Merge arrays for advanced view */
56 $this->fix_options();
57 foreach (array("options", "statements") as $type){
58 $this->advanced->$type= $this->$type + $this->network->$type;;
59 }
61 $display.= $this->advanced->execute();
63 /* Merge back for removals */
64 foreach (array("options", "statements") as $type){
65 $this->$type= $this->advanced->$type;
66 $this->network->$type= $this->advanced->$type;
67 }
69 /* Add footer */
70 $display.= "<div style='width:100%;text-align:right;margin-top:5px;'><input type=submit name='save_dhcp' value='"._("Save")."'>".
71 " <input type=submit name='cancel_dhcp' value='"._("Cancel")."'></div>";
73 return ($display);
74 }
76 function remove_from_parent()
77 {
78 }
81 /* Save data to object */
82 function save_object()
83 {
84 if (isset($_POST['cn'])){
85 $this->cn= validate(get_post('cn'));
86 $this->range_start= validate(get_post('range_start'));
87 $this->range_stop= validate(get_post('range_stop'));
88 }
90 dhcpPlugin::save_object();
92 /* Move range to internal variable */
93 $this->dhcpRange= $this->range_start." ".$this->range_stop;
94 }
97 /* Check values */
98 function check($cache)
99 {
100 $message= array();
102 /* All required fields are set? */
103 if ($this->cn == ""){
104 $message[]= _("Required field 'Name' is not filled.");
105 }
107 /* cn already used? */
108 if ($this->orig_cn != $this->cn || $this->new){
110 foreach($cache as $dn => $dummy){
111 if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
112 $message[]= _("The name for this section is already used!");
113 break;
114 }
115 }
116 }
118 if ($this->dhcpRange == ""){
119 $message[]= _("Required field 'Range' is not filled.");
120 }
122 if (!is_ip($this->range_start) || !is_ip($this->range_stop)){
123 $message[]= _("Field 'Range' contains invalid IP addresses.");
124 }
126 if(!is_ip_range($this->range_start,$this->range_stop)){
127 $message[] = _("Field 'Range' contains invalid IP range.");
128 }
130 /* Check if range is in the network */
131 $dn= $this->dn;
132 while (preg_match('/,/', $dn)){
133 $type= $this->objectType($cache, $dn);
135 /* Check for subnet */
136 if ($type == 'dhcpSubnet'){
137 $network= $cache[$dn]['cn'][0];
138 $netmask= normalize_netmask($cache[$dn]['dhcpNetMask'][0]);
139 if (!is_in_network($network, $netmask, $this->range_start) ||
140 !is_in_network($network, $netmask, $this->range_stop)){
141 $message[] = _("'Range' is not inside the configured network.");
142 }
143 }
145 /* Stop if we've examined the service base object */
146 if ($type == 'dhcpService'){
147 break;
148 }
149 $dn= preg_replace('/^[^,]+,/', '', $dn);
150 }
152 /* Check external plugins */
153 $net= $this->network->check();
154 $adv= $this->advanced->check();
155 $message= array_merge($message, $net, $adv);
157 return $message;
158 }
160 /* Save to LDAP */
161 function save()
162 {
163 dhcpPlugin::save();
164 $this->attrs['dhcpRange']= array($this->dhcpRange);
166 return ($this->attrs);
167 }
170 function objectType($cache, $dn)
171 {
172 $type= "";
173 $types= array("dhcpService", "dhcpClass", "dhcpSubClass", "dhcpHost",
174 "dhcpGroup", "dhcpPool", "dhcpSubnet", "dhcpSharedNetwork");
176 foreach ($cache[$dn]['objectClass'] as $oc){
177 if (in_array($oc, $types)){
178 $type= $oc;
179 break;
180 }
181 }
183 return ($type);
184 }
187 }
189 ?>