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,dirname(__FILE__))).$this->network->execute();
68 /* Merge arrays for advanced view */
69 $this->fix_options();
70 foreach (array("options", "statements") as $type){
71 $this->advanced->$type= $this->$type + $this->network->$type;
72 }
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='".msgPool::saveButton()."'>".
83 " <input type=submit name='cancel_dhcp' value='".msgPool::cancelButton()."'></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()
127 {
128 $message= array();
130 $cache = array();
131 if(isset($this->parent)){
132 $cache = $this->parent->dhcpObjectCache;
133 }
135 /* All required fields are set? */
136 if ($this->cn == ""){
137 $message[]= msgPool::required(_("Name"));
138 }
140 /* cn already used? */
141 if ($this->orig_cn != $this->cn || $this->new){
143 foreach($cache as $dn => $dummy){
144 if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
145 $message[]= msgPool::duplicated(_("Name"));
146 break;
147 }
148 }
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[]= msgPool::invalid(_("Hardware address"));
155 }
157 /* Check external plugins */
158 $net= $this->network->check();
159 $adv= $this->advanced->check();
160 $message= array_merge($message, $net, $adv);
162 return $message;
163 }
166 /* Save to LDAP */
167 function save()
168 {
169 dhcpPlugin::save();
170 if ($this->dhcpHWAddress != ""){
171 $this->attrs['dhcpHWAddress']= array($this->dhcpHWAddress);
172 } else {
173 $this->attrs['dhcpHWAddress']= array();
174 }
176 return ($this->attrs);
177 }
179 }
180 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
181 ?>