1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003-2007 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 dhcpHost extends plugin
22 {
23 /* Used attributes */
24 var $cn= "";
25 var $orig_cn= "";
26 var $dhcpHWAddress= "";
27 var $options= array();
28 var $statements= array();
29 var $dn= "";
31 /* Subobjects */
32 var $network;
33 var $advanced;
35 /* attribute list for save action */
36 var $attributes= array();
37 var $objectclasses= array();
39 function dhcpHost($attrs)
40 {
41 /* Load statements / options */
42 if (is_array($attrs)){
43 $this->dn= $attrs['dn'];
44 $this->new= FALSE;
46 /* Load attributes */
47 foreach (array("cn", "dhcpHWAddress") as $attr){
48 if (isset($attrs[$attr][0])){
49 $this->$attr= $attrs[$attr][0];
50 }
51 }
53 /* Load options */
54 if (isset($attrs['dhcpOption'])){
55 foreach ($attrs['dhcpOption'] as $opt){
56 if ($opt == "count"){
57 continue;
58 }
59 $idx= preg_replace('/\s.+$/', '', $opt);
60 $value= preg_replace('/^[^\s]+\s/', '', $opt);
61 $this->options[$idx]= $value;
62 }
63 }
65 /* Load statements */
66 if (isset($attrs['dhcpStatements'])){
67 foreach ($attrs['dhcpStatements'] as $opt){
68 if ($opt == "count"){
69 continue;
70 }
71 $idx= preg_replace('/\s.+$/', '', $opt);
72 $value= preg_replace('/^[^\s]+\s/', '', $opt);
73 $this->statements[$idx]= $value;
74 }
75 }
77 } else {
78 /* We keep the parent dn here if it's new */
79 $this->dn= $attrs;
80 $this->new= TRUE;
81 }
83 /* Load network module */
84 $this->network= new dhcpNetwork();
85 $this->network->options= $this->options;
86 $this->network->statements= $this->statements;
87 $this->advanced= new dhcpAdvanced();
88 $this->advanced->options= $this->options;
89 $this->advanced->statements= $this->statements;
90 $this->advanced->setAutoStatements(array("fixed-address"));
92 /* Save CN for later reference */
93 $this->orig_cn= $this->cn;
94 }
96 function execute()
97 {
98 $smarty= get_smarty();
99 $smarty->assign("cn", $this->cn);
100 $smarty->assign("dhcpHWAddress", preg_replace('/^[^ ]+ /', '', $this->dhcpHWAddress));
102 /* Create fixed address */
103 if (isset($this->statements['fixed-address'])){
104 $smarty->assign("fixedaddr", $this->statements['fixed-address']);
105 } else {
106 $smarty->assign("fixedaddr", "");
107 }
109 /* Prepare hw type selector */
110 $hwtype= preg_replace('/\s.*$/', '', $this->dhcpHWAddress);
111 $smarty->assign("hwtype", $hwtype);
112 $smarty->assign("hwtypes", array("ethernet" => _("Ethernet"),
113 "fddi" => _("FDDI"),
114 "token-ring" => _("Token Ring")));
115 /* Show main page */
116 $display= $smarty->fetch(get_template_path('dhcp_host.tpl', TRUE)).$this->network->execute();
118 /* Merge arrays for advanced view */
119 foreach (array("options", "statements") as $type){
120 $tmp= array_merge($this->$type, $this->network->$type);
121 $this->advanced->$type= $tmp;
122 }
124 $display.= $this->advanced->execute();
126 /* Merge back for removals */
127 foreach (array("options", "statements") as $type){
128 $this->$type= $this->advanced->$type;
129 $this->network->$type= $this->advanced->$type;
130 }
132 /* Add footer */
133 $display.= "<div style='width:100%;text-align:right;margin-top:5px;'><input type=submit name='save_dhcp' value='"._("Save")."'>".
134 " <input type=submit name='cancel_dhcp' value='"._("Cancel")."'></div>";
137 return ($display);
138 }
141 function remove_from_parent()
142 {
143 }
146 /* Save data to object */
147 function save_object()
148 {
149 plugin::save_object();
151 /* Save remaining attributes */
152 if (isset($_POST['hwtype'])){
154 /* Assemble hwAddress */
155 $this->dhcpHWAddress= $_POST['hwtype']." ".$_POST['dhcpHWAddress'];
156 $this->cn= validate($_POST['cn']);
158 /* Save fixed address */
159 if ($_POST['fixedaddr'] != ""){
160 $this->statements['fixed-address']= $_POST['fixedaddr'];
161 } else {
162 unset ($this->statements['fixed-address']);
163 }
164 }
166 /* Strip network objects */
167 foreach (array("routers", "domain-name", "subnet-mask", "broadcast-address") as $toberemoved){
168 unset($this->options[$toberemoved]);
169 }
170 foreach (array("filename", "next-server") as $toberemoved){
171 unset($this->statements[$toberemoved]);
172 }
174 /* Save sub-objects */
175 $this->network->save_object();
176 $this->advanced->save_object();
178 /* Merge arrays for advanced view */
179 foreach (array("options", "statements") as $type){
180 $tmp= array_merge($this->$type, $this->network->$type);
181 $this->advanced->$type= $tmp;
182 }
183 }
186 /* Check values */
187 function check($cache)
188 {
189 $message= array();
191 /* All required fields are set? */
192 if ($this->cn == ""){
193 $message[]= _("Required field 'Name' is not filled.");
194 }
196 /* cn already used? */
197 if ($this->orig_cn != $this->cn || $this->new){
199 foreach($cache as $dn => $dummy){
200 if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
201 $message[]= _("The name for this host section is already used!");
202 break;
203 }
204 }
205 }
207 /* Check syntax of MAC address */
208 $check= preg_replace('/^[^\s]*\s/', '', $this->dhcpHWAddress);
209 if (!preg_match('/^([0-9a-fA-F]{1,2}:){5}[0-9a-fA-F]{1,2}$/', $check)){
210 $message[]= _("The hardware address specified by you is not valid!");
211 }
213 /* Check external plugins */
214 $net= $this->network->check();
215 $adv= $this->advanced->check();
216 $message= array_merge($message, $net, $adv);
218 return $message;
219 }
222 /* Save to LDAP */
223 function save()
224 {
225 /* Merge arrays for network and advanced view */
226 foreach (array("options", "statements") as $type){
227 $tmp= array_merge($this->$type, $this->network->$type, $this->advanced->$type);
228 $this->$type= $tmp;
229 }
231 /* Add cn if we're new */
232 if ($this->new){
233 $this->dn= "cn=".$this->cn.",".$this->dn;
234 } else {
235 $this->dn= "cn=".$this->cn.preg_replace('/^cn=[^,]+/', '', $this->dn);
236 }
238 /* Assemble new entry - options */
239 $this->attrs['dhcpOption']= array();
240 if (isset ($this->options) && count ($this->options)){
241 foreach ($this->options as $key => $val){
242 $this->attrs['dhcpOption'][]= "$key $val";
243 }
244 }
246 /* Assemble new entry - statements */
247 $this->attrs['dhcpStatements']= array();
248 if (isset ($this->statements) && count ($this->statements)){
249 foreach ($this->statements as $key => $val){
250 $this->attrs['dhcpStatements'][]= "$key $val";
251 }
252 }
254 /* Move dn to the result */
255 $this->attrs['dn']= $this->dn;
256 $this->attrs['cn']= array($this->cn);
257 if ($this->dhcpHWAddress != ""){
258 $this->attrs['dhcpHWAddress']= array($this->dhcpHWAddress);
259 } else {
260 $this->attrs['dhcpHWAddress']= array();
261 }
262 $this->attrs['objectClass']= array('top', 'dhcpHost');
263 $this->attrs['MODIFIED']= TRUE;
265 return ($this->attrs);
266 }
268 }
270 ?>