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 $idx= preg_replace('/\s.+$/', '', $opt);
57 $value= preg_replace('/^[^\s]+\s/', '', $opt);
58 $this->options[$idx]= $value;
59 }
60 }
62 /* Load statements */
63 if (isset($attrs['dhcpStatements'])){
64 foreach ($attrs['dhcpStatements'] as $opt){
65 $idx= preg_replace('/\s.+$/', '', $opt);
66 $value= preg_replace('/^[^\s]+\s/', '', $opt);
67 $this->statements[$idx]= $value;
68 }
69 }
71 } else {
72 /* We keep the parent dn here if it's new */
73 $this->dn= $attrs;
74 $this->new= TRUE;
75 }
77 /* Load network module */
78 $this->network= new dhcpNetwork();
79 $this->network->options= $this->options;
80 $this->network->statements= $this->statements;
81 $this->advanced= new dhcpAdvanced();
82 $this->advanced->options= $this->options;
83 $this->advanced->statements= $this->statements;
84 $this->advanced->setAutoStatements(array("fixed-address"));
86 /* Save CN for later reference */
87 $this->orig_cn= $this->cn;
88 }
90 function execute()
91 {
92 $smarty= get_smarty();
93 $smarty->assign("cn", $this->cn);
94 $smarty->assign("dhcpHWAddress", preg_replace('/^[^ ]+ /', '', $this->dhcpHWAddress));
96 /* Create fixed address */
97 if (isset($this->statements['fixed-address'])){
98 $smarty->assign("fixedaddr", $this->statements['fixed-address']);
99 } else {
100 $smarty->assign("fixedaddr", "");
101 }
103 /* Prepare hw type selector */
104 $hwtype= preg_replace('/\s.*$/', '', $this->dhcpHWAddress);
105 $smarty->assign("hwtype", $hwtype);
106 $smarty->assign("hwtypes", array("ethernet" => _("Ethernet"),
107 "fddi" => _("FDDI"),
108 "token-ring" => _("Token Ring")));
109 /* Show main page */
110 $display= $smarty->fetch(get_template_path('dhcp_host.tpl', TRUE)).$this->network->execute();
112 /* Merge arrays for advanced view */
113 foreach (array("options", "statements") as $type){
114 $tmp= array_merge($this->$type, $this->network->$type);
115 $this->advanced->$type= $tmp;
116 }
118 $display.= $this->advanced->execute();
120 /* Merge back for removals */
121 foreach (array("options", "statements") as $type){
122 $this->$type= $this->advanced->$type;
123 $this->network->$type= $this->advanced->$type;
124 }
126 /* Add footer */
127 $display.= "<div style='width:100%;text-align:right;margin-top:5px;'><input type=submit name='save_dhcp' value='"._("Save")."'>".
128 " <input type=submit name='cancel_dhcp' value='"._("Cancel")."'></div>";
131 return ($display);
132 }
135 function remove_from_parent()
136 {
137 }
140 /* Save data to object */
141 function save_object()
142 {
143 plugin::save_object();
145 /* Save remaining attributes */
146 if (isset($_POST['hwtype'])){
148 /* Assemble hwAddress */
149 $this->dhcpHWAddress= $_POST['hwtype']." ".$_POST['dhcpHWAddress'];
150 $this->cn= validate($_POST['cn']);
152 /* Save fixed address */
153 if ($_POST['fixedaddr'] != ""){
154 $this->statements['fixed-address']= $_POST['fixedaddr'];
155 } else {
156 unset ($this->statements['fixed-address']);
157 }
158 }
160 /* Strip network objects */
161 foreach (array("routers", "domain-name", "subnet-mask", "broadcast-address") as $toberemoved){
162 unset($this->options[$toberemoved]);
163 }
164 foreach (array("filename", "next-server") as $toberemoved){
165 unset($this->statements[$toberemoved]);
166 }
168 /* Save sub-objects */
169 $this->network->save_object();
170 $this->advanced->save_object();
172 /* Merge arrays for advanced view */
173 foreach (array("options", "statements") as $type){
174 $tmp= array_merge($this->$type, $this->network->$type);
175 $this->advanced->$type= $tmp;
176 }
177 }
180 /* Check values */
181 function check($cache)
182 {
183 $message= array();
185 /* All required fields are set? */
186 if ($this->cn == ""){
187 $message[]= _("Required field 'Name' is not filled.");
188 }
190 /* cn already used? */
191 if ($this->orig_cn != $this->cn || $this->new){
193 foreach($cache as $dn => $dummy){
194 if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
195 $message[]= _("The name for this host section is already used!");
196 break;
197 }
198 }
199 }
201 /* Check syntax of MAC address */
202 $check= preg_replace('/^[^\s]*\s/', '', $this->dhcpHWAddress);
203 if (!preg_match('/^([0-9a-fA-F]{1,2}:){5}[0-9a-fA-F]{1,2}$/', $check)){
204 $message[]= _("The hardware address specified by you is not valid!");
205 }
207 /* Check external plugins */
208 $net= $this->network->check();
209 $adv= $this->advanced->check();
210 $message= array_merge($message, $net, $adv);
212 return $message;
213 }
216 /* Save to LDAP */
217 function save()
218 {
219 /* Merge arrays for network and advanced view */
220 foreach (array("options", "statements") as $type){
221 $tmp= array_merge($this->$type, $this->network->$type, $this->advanced->$type);
222 $this->$type= $tmp;
223 }
225 /* Add cn if we're new */
226 if ($this->new){
227 $this->dn= "cn=".$this->cn.",".$this->dn;
228 } else {
229 $this->dn= "cn=".$this->cn.preg_replace('/^cn=[^,]+/', '', $this->dn);
230 }
232 /* Assemble new entry - options */
233 $this->attrs['dhcpOption']= array();
234 if (isset ($this->options) && count ($this->options)){
235 foreach ($this->options as $key => $val){
236 $this->attrs['dhcpOption'][]= "$key $val";
237 }
238 }
240 /* Assemble new entry - statements */
241 $this->attrs['dhcpStatements']= array();
242 if (isset ($this->statements) && count ($this->statements)){
243 foreach ($this->statements as $key => $val){
244 $this->attrs['dhcpStatements'][]= "$key $val";
245 }
246 }
248 /* Move dn to the result */
249 $this->attrs['dn']= $this->dn;
250 $this->attrs['cn']= array($this->cn);
251 if ($this->dhcpHWAddress != ""){
252 $this->attrs['dhcpHWAddress']= array($this->dhcpHWAddress);
253 } else {
254 $this->attrs['dhcpHWAddress']= array();
255 }
256 $this->attrs['objectClass']= array('top', 'dhcpHost');
257 $this->attrs['MODIFIED']= TRUE;
259 return ($this->attrs);
260 }
262 }
264 ?>