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='".msgPool::saveButton()."'>".
71 " <input type=submit name='cancel_dhcp' value='".msgPool::cancelButton()."'></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()
99 {
100 $message= array();
102 $cache = $this->parent->dhcpObjectCache;
104 /* All required fields are set? */
105 if ($this->cn == ""){
106 $message[]= msgPool::required(_("Name"));
107 }
109 /* cn already used? */
110 if ($this->orig_cn != $this->cn || $this->new){
112 foreach($cache as $dn => $dummy){
113 if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
114 $message[]= msgPool::duplicated(_("Name"));
115 break;
116 }
117 }
118 }
120 if ($this->dhcpRange == ""){
121 $message[]= msgPool::required(_("Range"));
122 }
124 if (!tests::is_ip($this->range_start) || !tests::is_ip($this->range_stop)){
125 $message[]= msgPool::invalid(_("Range"));
126 }
128 if(!tests::is_ip_range($this->range_start,$this->range_stop)){
129 $message[]= msgPool::invalid(_("Range"));
130 }
132 /* Check if range is in the network */
133 $dn= $this->dn;
134 while (preg_match('/,/', $dn)){
135 $type= $this->objectType($cache, $dn);
137 /* Check for subnet */
138 if ($type == 'dhcpSubnet'){
139 $network= $cache[$dn]['cn'][0];
140 $netmask= normalize_netmask($cache[$dn]['dhcpNetMask'][0]);
141 if (!tests::is_in_network($network, $netmask, $this->range_start) ||
142 !tests::is_in_network($network, $netmask, $this->range_stop)){
143 $message[] = _("'Range' is not inside the configured network.");
144 }
145 }
147 /* Stop if we've examined the service base object */
148 if ($type == 'dhcpService'){
149 break;
150 }
151 $dn= preg_replace('/^[^,]+,/', '', $dn);
152 }
154 /* Check external plugins */
155 $net= $this->network->check();
156 $adv= $this->advanced->check();
157 $message= array_merge($message, $net, $adv);
159 return $message;
160 }
162 /* Save to LDAP */
163 function save()
164 {
165 dhcpPlugin::save();
166 $this->attrs['dhcpRange']= array($this->dhcpRange);
168 return ($this->attrs);
169 }
172 function objectType($cache, $dn)
173 {
174 $type= "";
175 $types= array("dhcpService", "dhcpClass", "dhcpSubClass", "dhcpHost",
176 "dhcpGroup", "dhcpPool", "dhcpSubnet", "dhcpSharedNetwork");
178 foreach ($cache[$dn]['objectClass'] as $oc){
179 if (in_array($oc, $types)){
180 $type= $oc;
181 break;
182 }
183 }
185 return ($type);
186 }
189 }
191 ?>