[gosa.git] / branches / old / gosa-plugins / dhcp / admin / systems / services / dhcp / class_dhcpHost.inc
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($parent,$attrs,$host_exists_in_gosa = FALSE)
31 {
32 dhcpPlugin::dhcpPlugin($parent,$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 /* Assign ACLs */
53 $smarty->assign("acl",$this->parent->getacl(""));
55 /* Create fixed address */
56 if (isset($this->statements['fixed-address'])){
57 $smarty->assign("fixedaddr", $this->statements['fixed-address']);
58 } else {
59 $smarty->assign("fixedaddr", "");
60 }
62 /* Prepare hw type selector */
63 $hwtype= preg_replace('/\s.*$/', '', $this->dhcpHWAddress);
64 $smarty->assign("hwtype", $hwtype);
65 $smarty->assign("hwtypes", array("ethernet" => _("Ethernet"),
66 "fddi" => _("FDDI"),
67 "token-ring" => _("Token Ring")));
68 /* Show main page */
69 $display= $smarty->fetch(get_template_path('dhcp_host.tpl', TRUE,dirname(__FILE__))).$this->network->execute();
71 /* Merge arrays for advanced view */
72 $this->fix_options();
73 foreach (array("options", "statements") as $type){
74 $this->advanced->$type= $this->$type + $this->network->$type;
75 }
76 $display.= $this->advanced->execute();
78 /* Merge back for removals */
79 foreach (array("options", "statements") as $type){
80 $this->$type= $this->advanced->$type;
81 $this->network->$type= $this->advanced->$type;
82 }
84 /* Add footer */
85 $display.= "<div style='width:100%;text-align:right;margin-top:5px;'>";
86 # if(preg_match("/w/",$this->parent->getacl(""))){
87 $display.= "<input type=submit name='save_dhcp' value='".msgPool::saveButton()."'> ";
88 # }
89 $display.= "<input type=submit name='cancel_dhcp' value='".msgPool::cancelButton()."'>";
90 $display.= "</div>";
92 return ($display);
93 }
96 function remove_from_parent()
97 {
98 }
101 /* Save data to object */
102 function save_object()
103 {
104 /* Save remaining attributes */
105 if (isset($_POST['dhcp_host_posted']) && preg_match("/w/",$this->parent->getacl(""))){
107 /* Assemble hwAddress */
108 if (isset($_POST['dhcpHWAddress'])){
109 $this->dhcpHWAddress= get_post('hwtype')." ".get_post('dhcpHWAddress');
110 }
112 if(!$this->realGosaHost){
113 $this->cn= validate(get_post('cn'));
114 }
116 /* Save fixed address */
117 if(!$this->realGosaHost){
118 if ($_POST['fixedaddr'] != ""){
119 $this->statements['fixed-address']= get_post('fixedaddr');
120 } else {
121 unset ($this->statements['fixed-address']);
122 }
123 }
124 $this->options['host-name']= $this->cn;
125 }
127 dhcpPlugin::save_object();
128 }
131 /* Check values */
132 function check()
133 {
134 $message= array();
136 $cache = array();
137 if(isset($this->parent)){
138 $cache = $this->parent->dhcpObjectCache;
139 }
141 /* All required fields are set? */
142 if ($this->cn == ""){
143 $message[]= msgPool::required(_("Name"));
144 }
146 /* cn already used? */
147 if ($this->orig_cn != $this->cn || $this->new){
149 foreach($cache as $dn => $dummy){
150 if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
151 $message[]= msgPool::duplicated(_("Name"));
152 break;
153 }
154 }
155 }
157 /* Check syntax of MAC address */
158 $check= preg_replace('/^[^\s]*\s/', '', $this->dhcpHWAddress);
159 if (!preg_match('/^([0-9a-fA-F]{1,2}:){5}[0-9a-fA-F]{1,2}$/', $check)){
160 $message[]= msgPool::invalid(_("Hardware address"));
161 }
163 /* Check external plugins */
164 $net= $this->network->check();
165 $adv= $this->advanced->check();
166 $message= array_merge($message, $net, $adv);
168 return $message;
169 }
172 /* Save to LDAP */
173 function save()
174 {
175 dhcpPlugin::save();
176 if ($this->dhcpHWAddress != ""){
177 $this->attrs['dhcpHWAddress']= array($this->dhcpHWAddress);
178 } else {
179 $this->attrs['dhcpHWAddress']= array();
180 }
182 return ($this->attrs);
183 }
185 }
186 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
187 ?>