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 /* Assign ACLs */
53 $smarty->assign("acl",$this->parent->getacl(""));
55 /* Show main page */
56 $display= $smarty->fetch(get_template_path('dhcp_pool.tpl', TRUE, dirname(__FILE__))).$this->network->execute();
58 /* Merge arrays for advanced view */
59 $this->fix_options();
60 foreach (array("options", "statements") as $type){
61 $this->advanced->$type= $this->$type + $this->network->$type;;
62 }
64 $display.= $this->advanced->execute();
66 /* Merge back for removals */
67 foreach (array("options", "statements") as $type){
68 $this->$type= $this->advanced->$type;
69 $this->network->$type= $this->advanced->$type;
70 }
72 /* Add footer */
73 $display.= "<div style='width:100%;text-align:right;margin-top:5px;'><input type=submit name='save_dhcp' value='".msgPool::saveButton()."'>".
74 " <input type=submit name='cancel_dhcp' value='".msgPool::cancelButton()."'></div>";
76 return ($display);
77 }
79 function remove_from_parent()
80 {
81 }
84 /* Save data to object */
85 function save_object()
86 {
87 if (isset($_POST['cn'])){
88 $this->cn= validate(get_post('cn'));
89 $this->range_start= validate(get_post('range_start'));
90 $this->range_stop= validate(get_post('range_stop'));
91 }
93 dhcpPlugin::save_object();
95 /* Move range to internal variable */
96 $this->dhcpRange= $this->range_start." ".$this->range_stop;
97 }
100 /* Check values */
101 function check()
102 {
103 $message= array();
105 $cache = $this->parent->dhcpObjectCache;
107 /* All required fields are set? */
108 if ($this->cn == ""){
109 $message[]= msgPool::required(_("Name"));
110 }
112 /* cn already used? */
113 if ($this->orig_cn != $this->cn || $this->new){
115 foreach($cache as $dn => $dummy){
116 if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
117 $message[]= msgPool::duplicated(_("Name"));
118 break;
119 }
120 }
121 }
123 if ($this->dhcpRange == ""){
124 $message[]= msgPool::required(_("Range"));
125 }
127 if (!tests::is_ip($this->range_start) || !tests::is_ip($this->range_stop)){
128 $message[]= msgPool::invalid(_("Range"));
129 }
131 if(!tests::is_ip_range($this->range_start,$this->range_stop)){
132 $message[]= msgPool::invalid(_("Range"));
133 }
135 /* Check if range is in the network */
136 $dn= $this->dn;
137 while (preg_match('/,/', $dn)){
138 $type= $this->objectType($cache, $dn);
140 /* Check for subnet */
141 if ($type == 'dhcpSubnet'){
142 $network= $cache[$dn]['cn'][0];
143 $netmask= normalize_netmask($cache[$dn]['dhcpNetMask'][0]);
144 if (!tests::is_in_network($network, $netmask, $this->range_start) ||
145 !tests::is_in_network($network, $netmask, $this->range_stop)){
146 $message[] = _("'Range' is not inside the configured network.");
147 }
148 }
150 /* Stop if we've examined the service base object */
151 if ($type == 'dhcpService'){
152 break;
153 }
154 $dn= preg_replace('/^[^,]+,/', '', $dn);
155 }
157 /* Check external plugins */
158 $net= $this->network->check();
159 $adv= $this->advanced->check();
160 $message= array_merge($message, $net, $adv);
162 return $message;
163 }
165 /* Save to LDAP */
166 function save()
167 {
168 dhcpPlugin::save();
169 $this->attrs['dhcpRange']= array($this->dhcpRange);
171 return ($this->attrs);
172 }
175 function objectType($cache, $dn)
176 {
177 $type= "";
178 $types= array("dhcpService", "dhcpClass", "dhcpSubClass", "dhcpHost",
179 "dhcpGroup", "dhcpPool", "dhcpSubnet", "dhcpSharedNetwork");
181 foreach ($cache[$dn]['objectClass'] as $oc){
182 if (in_array($oc, $types)){
183 $type= $oc;
184 break;
185 }
186 }
188 return ($type);
189 }
192 }
194 ?>