Code

Updated Post handling
[gosa.git] / plugins / admin / systems / 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= "";
26   /* attribute list for save action */
27   var $objectclasses= array("top", "dhcpHost");
29   function dhcpHost($attrs)
30   {
31     dhcpPlugin::dhcpPlugin($attrs);
33     /* Load attributes */
34     if (!$this->new){
35       $this->dhcpHWaddress= $attrs['dhcpHWAddress'][0];
36     }
38         print_a($this);
40     $this->advanced->setAutoOptions(array("host-name"));
41     $this->advanced->setAutoStatements(array("fixed-address"));
42   }
44   function execute()
45   {
46     $smarty= get_smarty();
47     $smarty->assign("cn", $this->cn);
48     $smarty->assign("dhcpHWAddress", preg_replace('/^[^ ]+ /', '', $this->dhcpHWAddress));
50     /* Create fixed address */
51     if (isset($this->statements['fixed-address'])){
52       $smarty->assign("fixedaddr", $this->statements['fixed-address']);
53     } else {
54       $smarty->assign("fixedaddr", "");
55     }
57     /* Prepare hw type selector */
58     $hwtype= preg_replace('/\s.*$/', '', $this->dhcpHWAddress);
59     $smarty->assign("hwtype", $hwtype);
60     $smarty->assign("hwtypes", array("ethernet" => _("Ethernet"),
61           "fddi" => _("FDDI"),
62           "token-ring" => _("Token Ring")));
63     /* Show main page */
64     $display= $smarty->fetch(get_template_path('dhcp_host.tpl', TRUE)).$this->network->execute();
66     /* Merge arrays for advanced view */
67     foreach (array("options", "statements") as $type){
68       $this->advanced->$type= $this->$type + $this->network->$type;
69     }
71     $display.= $this->advanced->execute();
73     /* Merge back for removals */
74     foreach (array("options", "statements") as $type){
75       $this->$type= $this->advanced->$type;
76       $this->network->$type= $this->advanced->$type;
77     }
79     /* Add footer */
80     $display.= "<div style='width:100%;text-align:right;margin-top:5px;'><input type=submit name='save_dhcp' value='"._("Save")."'>".
81                "&nbsp;<input type=submit name='cancel_dhcp' value='"._("Cancel")."'></div>";
84     return ($display);
85   }
88   function remove_from_parent()
89   {
90   }
93   /* Save data to object */
94   function save_object()
95   {
96     /* Save remaining attributes */
97     if (isset($_POST['hwtype'])){
99       /* Assemble hwAddress */
100       $this->dhcpHWAddress= get_post('hwtype')." ".get_post('dhcpHWAddress');
101       $this->cn= validate(get_post('cn'));
103       /* Save fixed address */
104       if ($_POST['fixedaddr'] != ""){
105         $this->statements['fixed-address']= get_post('fixedaddr');
106       } else {
107         unset ($this->statements['fixed-address']);
108       }
109       $this->options['host-name']= $this->cn;
110     }
112     dhcpPlugin::save_object();
113   }
116   /* Check values */
117   function check($cache)
118   {
119     $message= array();
121     /* All required fields are set? */
122     if ($this->cn == ""){
123       $message[]= _("Required field 'Name' is not filled.");
124     }
126     /* cn already used? */
127     if ($this->orig_cn != $this->cn || $this->new){
128       
129       foreach($cache as $dn => $dummy){
130         if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
131           $message[]= _("The name for this host section is already used!");
132           break;
133         }
134       }
135     }
137     /* Check syntax of MAC address */
138     $check= preg_replace('/^[^\s]*\s/', '', $this->dhcpHWAddress);
139     if (!preg_match('/^([0-9a-fA-F]{1,2}:){5}[0-9a-fA-F]{1,2}$/', $check)){
140       $message[]= _("The hardware address specified by you is not valid!");
141     }
143     /* Check external plugins */
144     $net= $this->network->check();
145     $adv= $this->advanced->check();
146     $message= array_merge($message, $net, $adv);
148     return $message;
149   }
152   /* Save to LDAP */
153   function save()
154   {
155     dhcpPlugin::save();
156     if ($this->dhcpHWAddress != ""){
157       $this->attrs['dhcpHWAddress']= array($this->dhcpHWAddress);
158     } else {
159       $this->attrs['dhcpHWAddress']= array();
160     }
162     return ($this->attrs);
163   }
167 ?>