1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class componentGeneric extends plugin
24 {
25 /* Generic terminal attributes */
26 var $interfaces= array();
27 var $ignore_account= TRUE;
29 /* Needed values and lists */
30 var $base= "";
31 var $cn= "";
32 var $description= "";
33 var $orig_dn= "";
34 var $orig_cn= "";
35 var $orig_base= "";
37 /* attribute list for save action */
38 var $attributes= array("cn", "description");
39 var $objectclasses= array("top", "device", "ipHost", "ieee802Device");
40 var $netConfigDNS;
41 var $view_logged = FALSE;
43 function componentGeneric (&$config, $dn= NULL, $parent= NULL)
44 {
45 plugin::plugin ($config, $dn, $parent);
47 /* Set base */
48 if ($this->dn == "new"){
49 $ui= get_userinfo();
50 $this->base= dn2base($ui->dn);
51 $this->cn= "";
52 } else {
53 $this->base= preg_replace ("/^[^,]+,".normalizePreg(get_ou("componentou"))."/", "", $this->dn);
54 }
55 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses, true);
56 /* Save dn for later references */
57 $this->orig_dn= $this->dn;
59 $this->orig_base = $this->base;
60 $this->orig_cn = $this->cn;
61 }
64 function execute()
65 {
66 /* Call parent execute */
67 plugin::execute();
69 if($this->is_account && !$this->view_logged){
70 $this->view_logged = TRUE;
71 new log("view","component/".get_class($this),$this->dn);
72 }
74 /* Do we represent a valid phone? */
75 if (!$this->is_account && $this->parent === NULL){
76 $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle> <b>".
77 msgPool::noValidExtension(_("component"))."</b>";
78 return($display);
79 }
81 /* Base select dialog */
82 $once = true;
83 foreach($_POST as $name => $value){
84 if(preg_match("/^chooseBase/",$name) && $once){
85 $once = false;
86 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
87 $this->dialog->setCurrentBase($this->base);
88 }
89 }
91 /* Dialog handling */
92 if(is_object($this->dialog)){
93 /* Must be called before save_object */
94 $this->dialog->save_object();
96 if($this->dialog->isClosed()){
97 $this->dialog = false;
98 }elseif($this->dialog->isSelected()){
100 /* A new base was selected, check if it is a valid one */
101 $tmp = $this->get_allowed_bases();
102 if(isset($tmp[$this->dialog->isSelected()])){
103 $this->base = $this->dialog->isSelected();
104 }
106 $this->dialog= false;
107 }else{
108 return($this->dialog->execute());
109 }
110 }
112 /* Fill templating stuff */
113 $smarty= get_smarty();
114 $smarty->assign("bases", $this->config->idepartments);
116 /* Set acls */
117 $tmp = $this->plInfo();
118 foreach($tmp['plProvidedAcls'] as $name => $translation){
119 $smarty->assign($name."ACL",$this->getacl($name));
120 }
122 $smarty->assign("bases", $this->get_allowed_bases());
124 /* Assign attributes */
125 foreach ($this->attributes as $attr){
126 $smarty->assign("$attr", $this->$attr);
127 }
128 $smarty->assign("base_select", $this->base);
130 /* Show main page */
131 $str = $this->netConfigDNS->execute();
132 if(is_object($this->netConfigDNS->dialog)){
133 return($str);
134 }
135 $smarty->assign("netconfig", $str);
136 return($smarty->fetch (get_template_path('component.tpl', TRUE)));
137 }
139 function set_acl_base($base)
140 {
141 plugin::set_acl_base($base);
142 $this->netConfigDNS->set_acl_base($base);
143 }
145 function set_acl_category($cat)
146 {
147 plugin::set_acl_category($cat);
148 $this->netConfigDNS->set_acl_category($cat);
149 }
151 function remove_from_parent()
152 {
153 $ldap= $this->config->get_ldap_link();
154 $this->netConfigDNS->remove_from_parent();
155 $ldap->rmdir($this->dn);
157 new log("remove","component/".get_class($this),$this->dn,$this->attributes,$ldap->get_error());
159 if (!$ldap->success()){
160 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
161 }
163 $this->handle_post_events(array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
165 /* Delete references to object groups */
166 $ldap->cd ($this->config->current['BASE']);
167 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
168 while ($ldap->fetch()){
169 $og= new ogroup($this->config, $ldap->getDN());
170 unset($og->member[$this->dn]);
171 $og->save ();
172 }
173 }
176 /* Save data to object */
177 function save_object()
178 {
180 /* Create a base backup and reset the
181 base directly after calling plugin::save_object();
182 Base will be set seperatly a few lines below */
183 $base_tmp = $this->base;
184 plugin::save_object();
185 $this->base = $base_tmp;
187 $this->netConfigDNS->save_object();
189 /* Set new base if allowed */
190 $tmp = $this->get_allowed_bases();
191 if(isset($_POST['base'])){
192 if(isset($tmp[$_POST['base']])){
193 $this->base= $_POST['base'];
194 }
195 }
196 }
199 /* Check supplied data */
200 function check()
201 {
202 /* Call common method to give check the hook */
203 $message= plugin::check();
204 $message= array_merge($message,$this->netConfigDNS->check());
206 $this->dn= "cn=".$this->cn.",".get_ou('componentou').$this->base;
208 if ($this->cn == "" ){
209 $message[]= msgPool::required(_("Component name"));
210 }
212 /* Check if given name is a valid host/dns name */
213 if(!tests::is_dns_name($this->cn)){
214 $message[]= msgPool::invalid(_("Component name"), $this->cn, "/[a-z0-9\.\-]/i");
215 }
217 /* To check for valid ip*/
218 if($this->netConfigDNS->ipHostNumber == ""){
219 $message[]= msgPool::required(_("IP address"));
220 } else {
221 if (!tests::is_ip($this->netConfigDNS->ipHostNumber)){
222 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.2");
223 }
224 }
226 if ($this->orig_dn != $this->dn){
227 $ldap= $this->config->get_ldap_link();
228 $ldap->cd ($this->base);
229 $ldap->search ("(cn=".$this->cn.")", array("cn"));
230 if ($ldap->count() != 0){
231 while ($attrs= $ldap->fetch()){
232 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
233 continue;
234 }
235 if ($attrs['dn'] != $this->orig_dn){
236 $message[]= msgPool::duplicated(_("Component name"));
237 break;
238 }
239 }
240 }
241 }
243 /* Check if we are allowed to create or move this object
244 */
245 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
246 $message[] = msgPool::permCreate();
247 }elseif($this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
248 $message[] = msgPool::permMove();
249 }
251 return ($message);
252 }
255 /* Save to LDAP */
256 function save()
257 {
258 plugin::save();
260 /* Remove all empty values */
261 if ($this->orig_dn == 'new'){
262 $attrs= array();
263 foreach ($this->attrs as $key => $val){
264 if (is_array($val) && count($val) == 0){
265 continue;
266 }
267 $attrs[$key]= $val;
268 }
269 $this->attrs= $attrs;
270 }
272 /* If this is a new Object IP & Mac aren't set.
273 IP & Mac are msut attributes, so we set this values by here. */
274 if($this->orig_dn == 'new'){
275 $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
276 $this->attrs['macAddress'] = $this->netConfigDNS->macAddress;
277 }
279 /* Write back to ldap */
280 $ldap= $this->config->get_ldap_link();
281 if ($this->orig_dn == 'new'){
282 $ldap->cd($this->config->current['BASE']);
283 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
284 $ldap->cd($this->dn);
285 $ldap->add($this->attrs);
286 new log("create","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
287 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
289 } else {
290 $ldap->cd($this->dn);
291 $this->cleanup();
292 $ldap->modify ($this->attrs);
293 new log("modify","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
294 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
295 }
297 $this->netConfigDNS->cn = $this->cn;
298 $this->netConfigDNS->save();
300 if (!$ldap->success()){
301 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
302 }
303 }
305 /* Return plugin informations for acl handling */
306 static function plInfo()
307 {
308 return (array(
309 "plShortName" => _("Generic"),
310 "plDescription" => _("Component generic"),
311 "plSelfModify" => FALSE,
312 "plDepends" => array(),
313 "plPriority" => 1,
314 "plSection" => array("administration"),
315 "plCategory" => array("component" => array("description" => _("Network device"),
316 "objectClass" => array("device", "ipHost", "ieee802Device"))),
317 "plProvidedAcls"=> array(
318 "cn" => _("Name"),
319 "base" => _("Base"),
320 "description" => _("Description"))
321 ));
322 }
324 /* Display generic part for server copy & paste */
325 function getCopyDialog()
326 {
327 $vars = array("cn");
328 $smarty = get_smarty();
329 $smarty->assign("cn" ,$this->cn);
330 $smarty->assign("object","component");
331 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
332 $ret = array();
333 $ret['string'] = $str;
334 $ret['status'] = "";
335 return($ret);
336 }
339 function saveCopyDialog()
340 {
341 if(isset($_POST['cn'])){
342 $this->cn = $_POST['cn'];
343 }
344 }
347 function PrepareForCopyPaste($source)
348 {
349 plugin::PrepareForCopyPaste($source);
350 if(isset($source['macAddress'][0])){
351 $this->netConfigDNS->macAddress = $source['macAddress'][0];
352 }
353 if(isset($source['ipHostNumber'][0])){
354 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
355 }
356 }
357 }
359 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
360 ?>