Code

Updated translation, fixed some typo/errors
[gosa.git] / include / class_dhcpPlugin.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 dhcpPlugin extends plugin
22 {
23   /* Used attributes */
24   var $cn= "";
25   var $orig_cn= "";
26   var $options= array();
27   var $statements= array();
29   /* Subobjects */
30   var $network;
31   var $advanced;
33   /* attribute list for save action */
34   var $attributes= array();
35   var $objectclasses= array();
37   function dhcpPlugin($attrs)
38   {
39     /* Load statements / options */
40     if (is_array($attrs)){
41       $this->dn= $attrs['dn'];
42       $this->cn= $attrs['cn'][0];
43       $this->new= FALSE;
45       /* Load options */
46       if (isset($attrs['dhcpOption'])){
47         foreach ($attrs['dhcpOption'] as $opt){
48           $idx= preg_replace('/\s.+$/', '', $opt);
49           if ($idx == $opt){
50             $value= "";
51           } else {
52             $value= preg_replace('/^[^\s]+\s/', '', $opt);
53           }
54           $this->options[$idx]= $value;
55         }
56       }
58       /* Load statements */
59       if (isset($attrs['dhcpStatements'])){
60         foreach ($attrs['dhcpStatements'] as $opt){
61           $idx= preg_replace('/\s.+$/', '', $opt);
62           if ($idx == $opt){
63             $value= "";
64           } else {
65             $value= preg_replace('/^[^\s]+\s/', '', $opt);
66           }
67           $this->statements[$idx]= $value;
68         }
69       }
71     } else {
72       /* We keep the parent dn here if it's new */
73       $this->dn= $attrs;
74       $this->new= TRUE;
75     }
77     /* Load network module */
78     $this->network= new dhcpNetwork();
79     $this->network->options= $this->options;
80     $this->network->statements= $this->statements;
81     $this->advanced= new dhcpAdvanced();
82     $this->advanced->options= $this->options;
83     $this->advanced->statements= $this->statements;
85     /* Save CN for later reference */
86     $this->orig_cn= $this->cn;
87   }
89   function execute()
90   {
91     return ("");
92   }
95   function remove_from_parent()
96   {
97   }
100   /* Save data to object */
101   function save_object()
102   {
103     /* Strip network objects */
104     foreach (array("routers", "domain-name", "subnet-mask", "broadcast-address") as $toberemoved){
105       unset($this->options[$toberemoved]);
106     }
107     foreach (array("filename", "next-server") as $toberemoved){
108       unset($this->statements[$toberemoved]);
109     }
111     /* Save sub-objects */
112     $this->network->save_object();
113     $this->advanced->save_object();
115     /* Merge arrays for advanced view */
116     foreach (array("options", "statements") as $type){
117       $this->advanced->$type= $this->$type + $this->network->$type;;
118     }
119   }
122   /* Check values */
123   function check($cache)
124   {
125     $message= array();
126     return $message;
127   }
130   /* Save to LDAP */
131   function save()
132   {
133     /* Merge arrays for network and advanced view */
134     foreach (array("options", "statements") as $type){
135       $this->$type= $this->$type + $this->network->$type + $this->advanced->$type;
136     }
138     /* Add cn if we're new */
139     if ($this->new){
140       $this->dn= "cn=".$this->cn.",".$this->dn;
141     } else {
142       $this->dn= "cn=".$this->cn.preg_replace('/^cn=[^,]+/', '', $this->dn);
143     }
145     /* Assemble new entry - options */
146     $this->attrs['dhcpOption']= array();
147     if (isset ($this->options) && count ($this->options)){
148       foreach ($this->options as $key => $val){
149         $this->attrs['dhcpOption'][]= "$key $val";
150       }
151     }
153     /* Assemble new entry - statements */
154     $this->attrs['dhcpStatements']= array();
155     if (isset ($this->statements) && count ($this->statements)){
156       foreach ($this->statements as $key => $val){
157         if ($val != ""){
158           $this->attrs['dhcpStatements'][]= "$key $val";
159         } else {
160           $this->attrs['dhcpStatements'][]= "$key";
161         }
162       }
163     }
165     /* Move dn to the result */
166     $this->attrs['dn']= $this->dn;
167     $this->attrs['cn']= array($this->cn);
168     $this->attrs['objectClass']= $this->objectclasses;
169     $this->attrs['MODIFIED']= TRUE;
171     return ($this->attrs);
172   }
175   function removeAttrs($name, $type)
176   {
177     $new= array();
178     foreach ($this->attrs[$type] as $value){
179       if (!preg_match("/^$name /", $value)){
180         $new[]= $value;
181       }
182     }
183     $this->attrs[$type]= $new;
184   }
187   function removeOption($name)
188   {
189     $this->removeAttrs($name, 'dhcpOption');
190   }
193   function removeStatement($name)
194   {
195     $this->removeAttrs($name, 'dhcpStatement');
196   }
199   function fix_options()
200   {
201     foreach (array('domain-name-servers') as $key){
202       unset ($this->options[$key]);
203     }
204   }
208 ?>