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 $dhcpHWAddress= "";
26 var $options= array();
27 var $statements= array();
28 var $dn= "new";
30 /* Subobjects */
31 var $network;
32 var $advanced;
34 /* attribute list for save action */
35 var $attributes= array();
36 var $objectclasses= array();
38 function dhcpHost($attrs)
39 {
40 /* Load statements / options */
41 if ($attrs != NULL){
42 $this->dn= "";
44 /* Load attributes */
45 foreach (array("cn", "dhcpHWAddress") as $attr){
46 if (isset($attrs[$attr][0])){
47 $this->$attr= $attrs[$attr][0];
48 }
49 }
51 /* Load options */
52 if (isset($attrs['dhcpOption'])){
53 for($i= 0; $i<$attrs['dhcpOption']['count']; $i++){
54 $tmp= $attrs['dhcpOption'][$i];
55 $idx= preg_replace('/\s.+$/', '', $tmp);
56 $value= preg_replace('/^[^\s]+\s/', '', $tmp);
57 $this->options[$idx]= $value;
58 }
59 }
61 /* Load statements */
62 if (isset($attrs['dhcpStatements'])){
63 for($i= 0; $i<$attrs['dhcpStatements']['count']; $i++){
64 $tmp= $attrs['dhcpStatements'][$i];
65 $idx= preg_replace('/\s.+$/', '', $tmp);
66 $value= preg_replace('/^[^\s]+\s/', '', $tmp);
67 $this->statements[$idx]= $value;
68 }
69 }
71 }
73 /* Load network module */
74 $this->network= new dhcpNetwork();
75 $this->network->options= $this->options;
76 $this->network->statements= $this->statements;
77 $this->advanced= new dhcpAdvanced();
78 $this->advanced->options= $this->options;
79 $this->advanced->statements= $this->statements;
80 }
82 function execute()
83 {
84 $smarty= get_smarty();
85 $smarty->assign("cn", $this->cn);
86 $smarty->assign("dhcpHWAddress", preg_replace('/^[^ ]+ /', '', $this->dhcpHWAddress));
88 /* Create fixed address */
89 if (isset($this->statements['fixed-address'])){
90 $smarty->assign("fixedaddr", $this->statements['fixed-address']);
91 } else {
92 $smarty->assign("fixedaddr", "");
93 }
95 /* Prepare hw type selector */
96 $hwtype= preg_replace('/\s.*$/', '', $this->dhcpHWAddress);
97 $smarty->assign("hwtype", $hwtype);
98 $smarty->assign("hwtypes", array("ethernet" => _("Ethernet"),
99 "fddi" => _("FDDI"),
100 "token-ring" => _("Token Ring")));
102 /* Show main page */
103 return($smarty->fetch(get_template_path('dhcphost.tpl', TRUE)).$this->network->execute().$this->advanced->execute());
104 }
107 function remove_from_parent()
108 {
109 }
112 /* Save data to object */
113 function save_object()
114 {
115 plugin::save_object();
117 /* Save remaining attributes */
118 if (isset($_POST['hwtype'])){
120 /* Assemble hwAddress */
121 $this->dhcpHWAddress= $_POST['hwtype']." ".$_POST['dhcpHWAddress'];
122 $this->cn= validate($_POST['cn']);
124 /* Save fixed address */
125 if ($_POST['fixedaddr'] != ""){
126 $this->statements['fixed-address']= $_POST['fixedaddr'];
127 } else {
128 unset ($this->statements['fixed-address']);
129 }
130 }
131 }
134 /* Check values */
135 function check()
136 {
137 $message= array();
139 /* All required fields are set? */
140 if ($this->cn == ""){
141 $message[]= _("Required field 'Name' is not filled.");
142 }
144 /* cn already used? */
145 if ($this->dn != "new"){
146 # Ask parents container object
147 echo "FIXME: Need to check container object!";
148 $message[]= _("The name for this host section is already used!");
149 }
151 /* Check syntax of MAC address */
152 $check= preg_replace('/^[^\s]*\s/', '', $this->dhcpHWAddress);
153 if (!preg_match('/^([0-9a-fA-F]{1,2}:){5}[0-9a-fA-F]{1,2}$/', $check)){
154 $message[]= _("The hardware address specified by you is not valid!");
155 }
157 return $message;
158 }
161 /* Save to LDAP */
162 function save()
163 {
164 echo "FIXME: Need to implement modified save()!";
165 return;
167 # If this->dn == "new", create new entry
168 # If this->dn != "new", modify existing entry and place a "modifyied" inside to notify
169 # the parent to move/handle it.
170 # If CN has changed from former CN, empty out old entry and
171 # create a new one.
173 #-------------------------------- Not yet modified from this line on -------------------------------
175 /* Generate new dn */
176 if ($this->parent->parent != NULL){
177 $this->dn= "cn=".$this->cn.",".$this->parent->parent;
178 } else {
179 $tmp= preg_replace('/^cn=[^,]+/', '', $this->dn);
180 $this->dn= "cn=".$this->cn.$tmp;
181 }
183 /* Get ldap mode */
184 if ($this->orig_dn != $this->dn){
185 $mode= "add";
186 } else {
187 $mode= "modify";
188 }
190 /* Assemble new entry - options */
191 if (isset ($this->options) && count ($this->options)){
192 $this->attrs['dhcpOption']= array();
193 foreach ($this->options as $key => $val){
194 $this->attrs['dhcpOption'][]= "$key $val";
195 }
196 } else {
197 if ($mode == "modify"){
198 $this->attrs['dhcpOption']= array();
199 }
200 }
202 /* Assemble new entry - statements */
203 if (isset ($this->statements) && count ($this->statements)){
204 $this->attrs['dhcpStatements']= array();
205 foreach ($this->statements as $key => $val){
206 $this->attrs['dhcpStatements'][]= "$key $val";
207 }
208 } else {
209 if ($mode == "modify"){
210 $this->attrs['dhcpStatements']= array();
211 }
212 }
214 /* Do LDAP action */
215 $ldap= $this->config->get_ldap_link();
216 if ($mode == "add"){
217 $ldap->cd($this->config->current['BASE']);
218 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
219 }
220 $ldap->cd($this->dn);
221 $ldap->$mode($this->attrs);
222 show_ldap_error($ldap->get_error());
224 /* Name changed? Remove orig? */
225 if ($this->orig_dn != $this->dn && $this->orig_dn != "new"){
226 echo "Remove old<br>";
227 $ldap->rmdir($this->orig_dn);
228 show_ldap_error($ldap->get_error());
229 }
230 }
232 }
234 ?>