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 foreach (array("options", "statements") as $type){
57 $this->advanced->$type= $this->$type + $this->network->$type;;
58 }
60 $display.= $this->advanced->execute();
62 /* Merge back for removals */
63 foreach (array("options", "statements") as $type){
64 $this->$type= $this->advanced->$type;
65 $this->network->$type= $this->advanced->$type;
66 }
68 /* Add footer */
69 $display.= "<div style='width:100%;text-align:right;margin-top:5px;'><input type=submit name='save_dhcp' value='"._("Save")."'>".
70 " <input type=submit name='cancel_dhcp' value='"._("Cancel")."'></div>";
72 return ($display);
73 }
75 function remove_from_parent()
76 {
77 }
80 /* Save data to object */
81 function save_object()
82 {
83 if (isset($_POST['cn'])){
84 $this->cn= validate(get_post('cn'));
85 $this->range_start= validate(get_post('range_start'));
86 $this->range_stop= validate(get_post('range_stop'));
87 }
89 dhcpPlugin::save_object();
91 /* Move range to internal variable */
92 $this->dhcpRange= $this->range_start." ".$this->range_stop;
93 }
96 /* Check values */
97 function check($cache)
98 {
99 $message= array();
101 /* All required fields are set? */
102 if ($this->cn == ""){
103 $message[]= _("Required field 'Name' is not filled.");
104 }
106 /* cn already used? */
107 if ($this->orig_cn != $this->cn || $this->new){
109 foreach($cache as $dn => $dummy){
110 if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
111 $message[]= _("The name for this section is already used!");
112 break;
113 }
114 }
115 }
117 if ($this->dhcpRange == ""){
118 $message[]= _("Required field 'Range' is not filled.");
119 }
121 if (!is_ip($this->range_start) || !is_ip($this->range_stop)){
122 $message[]= _("Field 'Range' contains invalid IP addresses.");
123 }
125 if(!is_ip_range($this->range_start,$this->range_stop)){
126 $message[] = _("Field 'Range' contains invalid IP range.");
127 }
129 /* Check if range is in the network */
130 $dn= $this->dn;
131 while (preg_match('/,/', $dn)){
132 $type= $this->objectType($cache, $dn);
134 /* Check for subnet */
135 if ($type == 'dhcpSubnet'){
136 $network= $cache[$dn]['cn'][0];
137 $netmask= normalize_netmask($cache[$dn]['dhcpNetMask'][0]);
138 if (!is_in_network($network, $netmask, $this->range_start) ||
139 !is_in_network($network, $netmask, $this->range_stop)){
140 $message[] = _("'Range' is not inside the configured network.");
141 }
142 }
144 /* Stop if we've examined the service base object */
145 if ($type == 'dhcpService'){
146 break;
147 }
148 $dn= preg_replace('/^[^,]+,/', '', $dn);
149 }
151 /* Check external plugins */
152 $net= $this->network->check();
153 $adv= $this->advanced->check();
154 $message= array_merge($message, $net, $adv);
156 return $message;
157 }
159 /* Save to LDAP */
160 function save()
161 {
162 dhcpPlugin::save();
163 $this->attrs['dhcpRange']= array($this->dhcpRange);
165 return ($this->attrs);
166 }
169 function objectType($cache, $dn)
170 {
171 $type= "";
172 $types= array("dhcpService", "dhcpClass", "dhcpSubClass", "dhcpHost",
173 "dhcpGroup", "dhcpPool", "dhcpSubnet", "dhcpSharedNetwork");
175 foreach ($cache[$dn]['objectClass'] as $oc){
176 if (in_array($oc, $types)){
177 $type= $oc;
178 break;
179 }
180 }
182 return ($type);
183 }
186 }
188 ?>