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 $tmp= array_merge($this->$type, $this->network->$type);
118 $this->advanced->$type= $tmp;
119 }
120 }
123 /* Check values */
124 function check($cache)
125 {
126 $message= array();
128 return $message;
129 }
132 /* Save to LDAP */
133 function save()
134 {
135 /* Merge arrays for network and advanced view */
136 foreach (array("options", "statements") as $type){
137 $tmp= array_merge($this->$type, $this->network->$type, $this->advanced->$type);
138 $this->$type= $tmp;
139 }
141 /* Add cn if we're new */
142 if ($this->new){
143 $this->dn= "cn=".$this->cn.",".$this->dn;
144 } else {
145 $this->dn= "cn=".$this->cn.preg_replace('/^cn=[^,]+/', '', $this->dn);
146 }
148 /* Assemble new entry - options */
149 $this->attrs['dhcpOption']= array();
150 if (isset ($this->options) && count ($this->options)){
151 foreach ($this->options as $key => $val){
152 $this->attrs['dhcpOption'][]= "$key $val";
153 }
154 }
156 /* Assemble new entry - statements */
157 $this->attrs['dhcpStatements']= array();
158 if (isset ($this->statements) && count ($this->statements)){
159 foreach ($this->statements as $key => $val){
160 if ($val != ""){
161 $this->attrs['dhcpStatements'][]= "$key $val";
162 } else {
163 $this->attrs['dhcpStatements'][]= "$key";
164 }
165 }
166 }
168 /* Move dn to the result */
169 $this->attrs['dn']= $this->dn;
170 $this->attrs['cn']= array($this->cn);
171 $this->attrs['objectClass']= $this->objectclasses;
172 $this->attrs['MODIFIED']= TRUE;
174 return ($this->attrs);
175 }
178 function removeAttrs($name, $type)
179 {
180 $new= array();
181 foreach ($this->attrs[$type] as $value){
182 if (!preg_match("/^$name /", $value)){
183 $new[]= $value;
184 }
185 }
186 $this->attrs[$type]= $new;
187 }
190 function removeOption($name)
191 {
192 $this->removeAttrs($name, 'dhcpOption');
193 }
196 function removeStatement($name)
197 {
198 $this->removeAttrs($name, 'dhcpStatement');
199 }
201 }
203 ?>