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 dhcpPlugin
22 {
23 /* Used attributes */
24 var $dhcpHWAddress= "";
25 var $realGosaHost = FALSE;
27 /* attribute list for save action */
28 var $objectclasses= array("top", "dhcpHost");
30 function dhcpHost($attrs,$host_exists_in_gosa = FALSE)
31 {
32 dhcpPlugin::dhcpPlugin($attrs);
34 /* Load attributes */
35 if (!$this->new){
36 $this->dhcpHWAddress= $attrs['dhcpHWAddress'][0];
37 }
39 $this->advanced->setAutoOptions(array("host-name"));
40 $this->advanced->setAutoStatements(array("fixed-address"));
42 $this->realGosaHost = $host_exists_in_gosa;
43 }
45 function execute()
46 {
47 $smarty= get_smarty();
48 $smarty->assign("cn", $this->cn);
49 $smarty->assign("dhcpHWAddress", preg_replace('/^[^ ]+ /', '', $this->dhcpHWAddress));
50 $smarty->assign("realGosaHost",$this->realGosaHost);
52 /* Create fixed address */
53 if (isset($this->statements['fixed-address'])){
54 $smarty->assign("fixedaddr", $this->statements['fixed-address']);
55 } else {
56 $smarty->assign("fixedaddr", "");
57 }
59 /* Prepare hw type selector */
60 $hwtype= preg_replace('/\s.*$/', '', $this->dhcpHWAddress);
61 $smarty->assign("hwtype", $hwtype);
62 $smarty->assign("hwtypes", array("ethernet" => _("Ethernet"),
63 "fddi" => _("FDDI"),
64 "token-ring" => _("Token Ring")));
65 /* Show main page */
66 $display= $smarty->fetch(get_template_path('dhcp_host.tpl', TRUE)).$this->network->execute();
68 /* Merge arrays for advanced view */
69 foreach (array("options", "statements") as $type){
70 $this->advanced->$type= $this->$type + $this->network->$type;
71 }
73 $display.= $this->advanced->execute();
75 /* Merge back for removals */
76 foreach (array("options", "statements") as $type){
77 $this->$type= $this->advanced->$type;
78 $this->network->$type= $this->advanced->$type;
79 }
81 /* Add footer */
82 $display.= "<div style='width:100%;text-align:right;margin-top:5px;'><input type=submit name='save_dhcp' value='"._("Save")."'>".
83 " <input type=submit name='cancel_dhcp' value='"._("Cancel")."'></div>";
86 return ($display);
87 }
90 function remove_from_parent()
91 {
92 }
95 /* Save data to object */
96 function save_object()
97 {
98 /* Save remaining attributes */
99 if (isset($_POST['dhcp_host_posted'])){
101 /* Assemble hwAddress */
102 if (isset($_POST['dhcpHWAddress'])){
103 $this->dhcpHWAddress= get_post('hwtype')." ".get_post('dhcpHWAddress');
104 }
106 if(!$this->realGosaHost){
107 $this->cn= validate(get_post('cn'));
108 }
110 /* Save fixed address */
111 if(!$this->realGosaHost){
112 if ($_POST['fixedaddr'] != ""){
113 $this->statements['fixed-address']= get_post('fixedaddr');
114 } else {
115 unset ($this->statements['fixed-address']);
116 }
117 }
118 $this->options['host-name']= $this->cn;
119 }
121 dhcpPlugin::save_object();
122 }
125 /* Check values */
126 function check($cache)
127 {
128 $message= array();
130 /* All required fields are set? */
131 if ($this->cn == ""){
132 $message[]= _("Required field 'Name' is not filled.");
133 }
135 /* cn already used? */
136 if ($this->orig_cn != $this->cn || $this->new){
138 foreach($cache as $dn => $dummy){
139 if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
140 $message[]= _("The name for this host section is already used!");
141 break;
142 }
143 }
144 }
146 /* Check syntax of MAC address */
147 $check= preg_replace('/^[^\s]*\s/', '', $this->dhcpHWAddress);
148 if (!preg_match('/^([0-9a-fA-F]{1,2}:){5}[0-9a-fA-F]{1,2}$/', $check)){
149 $message[]= _("The hardware address specified by you is not valid!");
150 }
152 /* Check external plugins */
153 $net= $this->network->check();
154 $adv= $this->advanced->check();
155 $message= array_merge($message, $net, $adv);
157 return $message;
158 }
161 /* Save to LDAP */
162 function save()
163 {
164 dhcpPlugin::save();
165 if ($this->dhcpHWAddress != ""){
166 $this->attrs['dhcpHWAddress']= array($this->dhcpHWAddress);
167 } else {
168 $this->attrs['dhcpHWAddress']= array();
169 }
171 return ($this->attrs);
172 }
174 }
175 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
176 ?>