[gosa.git] / branches / old / gosa-plugins / dhcp / admin / systems / services / dhcp / class_dhcpPool.inc
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($parent,$attrs)
32 {
33 dhcpPlugin::dhcpPlugin($parent,$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;'>";
74 if(preg_match("/w/",$this->parent->getacl(""))){
75 $display.= "<input type=submit name='save_dhcp' value='".msgPool::saveButton()."'> ";
76 }
77 $display.= "<input type=submit name='cancel_dhcp' value='".msgPool::cancelButton()."'>";
78 $display.= "</div>";
80 return ($display);
81 }
83 function remove_from_parent()
84 {
85 }
88 /* Save data to object */
89 function save_object()
90 {
91 if (isset($_POST['cn'])){
92 $this->cn= validate(get_post('cn'));
93 $this->range_start= validate(get_post('range_start'));
94 $this->range_stop= validate(get_post('range_stop'));
95 }
97 dhcpPlugin::save_object();
99 /* Move range to internal variable */
100 $this->dhcpRange= $this->range_start." ".$this->range_stop;
101 }
104 /* Check values */
105 function check()
106 {
107 $message= array();
109 $cache = $this->parent->dhcpObjectCache;
111 /* All required fields are set? */
112 if ($this->cn == ""){
113 $message[]= msgPool::required(_("Name"));
114 }
116 /* cn already used? */
117 if ($this->orig_cn != $this->cn || $this->new){
119 foreach($cache as $dn => $dummy){
120 if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
121 $message[]= msgPool::duplicated(_("Name"));
122 break;
123 }
124 }
125 }
127 if ($this->dhcpRange == ""){
128 $message[]= msgPool::required(_("Range"));
129 }
131 if (!tests::is_ip($this->range_start) || !tests::is_ip($this->range_stop)){
132 $message[]= msgPool::invalid(_("Range"));
133 }
135 if(!tests::is_ip_range($this->range_start,$this->range_stop)){
136 $message[]= msgPool::invalid(_("Range"));
137 }
139 /* Check if range is in the network */
140 $dn= $this->dn;
141 while (preg_match('/,/', $dn)){
142 $type= $this->objectType($cache, $dn);
144 /* Check for subnet */
145 if ($type == 'dhcpSubnet'){
146 $network= $cache[$dn]['cn'][0];
147 $netmask= normalize_netmask($cache[$dn]['dhcpNetMask'][0]);
148 if (!tests::is_in_network($network, $netmask, $this->range_start) ||
149 !tests::is_in_network($network, $netmask, $this->range_stop)){
150 $message[] = _("'Range' is not inside the configured network.");
151 }
152 }
154 /* Stop if we've examined the service base object */
155 if ($type == 'dhcpService'){
156 break;
157 }
158 $dn= preg_replace('/^[^,]+,/', '', $dn);
159 }
161 /* Check external plugins */
162 $net= $this->network->check();
163 $adv= $this->advanced->check();
164 $message= array_merge($message, $net, $adv);
166 return $message;
167 }
169 /* Save to LDAP */
170 function save()
171 {
172 dhcpPlugin::save();
173 $this->attrs['dhcpRange']= array($this->dhcpRange);
175 return ($this->attrs);
176 }
179 function objectType($cache, $dn)
180 {
181 $type= "";
182 $types= array("dhcpService", "dhcpClass", "dhcpSubClass", "dhcpHost",
183 "dhcpGroup", "dhcpPool", "dhcpSubnet", "dhcpSharedNetwork");
185 foreach ($cache[$dn]['objectClass'] as $oc){
186 if (in_array($oc, $types)){
187 $type= $oc;
188 break;
189 }
190 }
192 return ($type);
193 }
196 }
198 ?>