Code

Msg Pool
[gosa.git] / gosa-plugins / systems / admin / systems / class_componentGeneric.inc
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= "";
35   /* attribute list for save action */
36   var $attributes= array("cn", "description");
37   var $objectclasses= array("top", "device", "ipHost", "ieee802Device");
38   var $netConfigDNS;
39   var $view_logged  = FALSE;
41   function componentGeneric (&$config, $dn= NULL, $parent= NULL)
42   {
43     plugin::plugin ($config, $dn, $parent);
45     /* Set base */
46     if ($this->dn == "new"){
47       $ui= get_userinfo();
48       $this->base= dn2base($ui->dn);
49       $this->cn= "";
50     } else {
51       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
52     }
53     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses, true);
54     /* Save dn for later references */
55     $this->orig_dn= $this->dn;
56   }
59   function execute()
60   {
61     /* Call parent execute */
62     plugin::execute();
64     if($this->is_account && !$this->view_logged){
65       $this->view_logged = TRUE;
66       new log("view","component/".get_class($this),$this->dn);
67     }
69     /* Do we represent a valid phone? */
70     if (!$this->is_account && $this->parent === NULL){
71       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
72         _("This 'dn' has no network features.")."</b>";
73       return($display);
74     }
76     /* Base select dialog */
77     $once = true;
78     foreach($_POST as $name => $value){
79       if(preg_match("/^chooseBase/",$name) && $once){
80         $once = false;
81         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
82         $this->dialog->setCurrentBase($this->base);
83       }
84     }
86     /* Dialog handling */
87     if(is_object($this->dialog)){
88       /* Must be called before save_object */
89       $this->dialog->save_object();
91       if($this->dialog->isClosed()){
92         $this->dialog = false;
93       }elseif($this->dialog->isSelected()){
95         /* A new base was selected, check if it is a valid one */
96         $tmp = $this->get_allowed_bases();
97         if(isset($tmp[$this->dialog->isSelected()])){
98           $this->base = $this->dialog->isSelected();
99         }
101         $this->dialog= false;
102       }else{
103         return($this->dialog->execute());
104       }
105     }
107     /* Fill templating stuff */
108     $smarty= get_smarty();
109     $smarty->assign("bases", $this->config->idepartments);
111     /* Set acls */
112     $tmp = $this->plInfo();
113     foreach($tmp['plProvidedAcls'] as $name => $translation){
114       $smarty->assign($name."ACL",$this->getacl($name));
115     }
117     $smarty->assign("bases", $this->get_allowed_bases());
119     /* Assign attributes */
120     foreach ($this->attributes as $attr){
121       $smarty->assign("$attr", $this->$attr);
122     }
123     $smarty->assign("base_select", $this->base);
125     /* Show main page */
126     $str = $this->netConfigDNS->execute();
127     if(is_object($this->netConfigDNS->dialog)){
128       return($str);
129     }
130     $smarty->assign("netconfig", $str);
131     return($smarty->fetch (get_template_path('component.tpl', TRUE)));
132   }
134   function set_acl_base($base)
135   {
136     plugin::set_acl_base($base);
137     $this->netConfigDNS->set_acl_base($base);
138   }
140   function set_acl_category($cat)
141   {
142     plugin::set_acl_category($cat);
143     $this->netConfigDNS->set_acl_category($cat);
144   }
146   function remove_from_parent()
147   {
148     $ldap= $this->config->get_ldap_link();
149     $this->netConfigDNS->remove_from_parent();
150     $ldap->rmdir($this->dn);
151     
152     new log("remove","component/".get_class($this),$this->dn,$this->attributes,$ldap->get_error());
154     if (!$ldap->success()){
155       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
156     }
158     $this->handle_post_events(array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
160     /* Delete references to object groups */
161     $ldap->cd ($this->config->current['BASE']);
162     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
163     while ($ldap->fetch()){
164       $og= new ogroup($this->config, $ldap->getDN());
165       unset($og->member[$this->dn]);
166       $og->save ();
167     }
168   }
171   /* Save data to object */
172   function save_object()
173   {
174     
175     /* Create a base backup and reset the
176        base directly after calling plugin::save_object();
177        Base will be set seperatly a few lines below */
178     $base_tmp = $this->base;
179     plugin::save_object();
180     $this->base = $base_tmp;
182     $this->netConfigDNS->save_object();
184     /* Set new base if allowed */
185     $tmp = $this->get_allowed_bases();
186     if(isset($_POST['base'])){
187       if(isset($tmp[$_POST['base']])){
188         $this->base= $_POST['base'];
189       }
190     }
191   }
194   /* Check supplied data */
195   function check()
196   {
197     /* Call common method to give check the hook */
198     $message= plugin::check();
199     $message= array_merge($message,$this->netConfigDNS->check());
201     $this->dn= "cn=".$this->cn.",".get_ou('componentou').$this->base;
203     if ($this->cn == "" ){
204       $message[]= msgPool::required(_("Component name"));
205     }
207     /* Check if given name is a valid host/dns name */
208     if(!tests::is_dns_name($this->cn)){
209       $message[]= msgPool::invalid(_("Component name"), $this->cn, "/[a-z0-9\.\-]/i");
210     }
212     /* To check for valid ip*/
213     if($this->netConfigDNS->ipHostNumber == ""){
214        $message[]= msgPool::required(_("IP address"));
215     } else {
216       if (!tests::is_ip($this->netConfigDNS->ipHostNumber)){
217         $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.2");
218       }
219     }
221     if ($this->orig_dn != $this->dn){
222       $ldap= $this->config->get_ldap_link();
223       $ldap->cd ($this->base);
224       $ldap->search ("(cn=".$this->cn.")", array("cn"));
225       if ($ldap->count() != 0){
226         while ($attrs= $ldap->fetch()){
227           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
228             continue;
229           }
230           if ($attrs['dn'] != $this->orig_dn){
231             $message[]= msgPool::duplicated(_("Component name"));
232             break;
233           }
234         }
235       }
236     }
238     return ($message);
239   }
242   /* Save to LDAP */
243   function save()
244   {
245     plugin::save();
247     /* Remove all empty values */
248     if ($this->orig_dn == 'new'){
249       $attrs= array();
250       foreach ($this->attrs as $key => $val){
251         if (is_array($val) && count($val) == 0){
252           continue;
253         }
254         $attrs[$key]= $val;
255       }
256       $this->attrs= $attrs;
257     }
259     /* If this is a new Object IP & Mac aren't set.
260          IP & Mac are msut attributes, so we set this values by here. */
261     if($this->orig_dn == 'new'){
262       $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
263       $this->attrs['macAddress']  = $this->netConfigDNS->macAddress;
264     }
266     /* Write back to ldap */
267     $ldap= $this->config->get_ldap_link();
268     if ($this->orig_dn == 'new'){
269       $ldap->cd($this->config->current['BASE']);
270       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
271       $ldap->cd($this->dn);
272       $ldap->add($this->attrs);
273       new log("create","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
274       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
275       
276     } else {
277       if ($this->orig_dn != $this->dn){
278         $this->move($this->orig_dn, $this->dn);
279       }
281       $ldap->cd($this->dn);
282       $this->cleanup();
283       $ldap->modify ($this->attrs); 
284       new log("modify","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
285       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
286     }
288     $this->netConfigDNS->cn = $this->cn;
289     $this->netConfigDNS->save();
291     if (!$ldap->success()){
292       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
293     }
294   }
296   /* Return plugin informations for acl handling */
297   static function plInfo()
298   {
299     return (array(
300           "plShortName"   => _("Generic"),
301           "plDescription" => _("Component generic"),
302           "plSelfModify"  => FALSE,
303           "plDepends"     => array(),
304           "plPriority"    => 1,
305           "plSection"     => array("administration"),
306           "plCategory"    => array("component" => array("description"  => _("Network device"),
307                                                         "objectClass"  => array("device", "ipHost", "ieee802Device"))),
308           "plProvidedAcls"=> array(
309             "cn"                  => _("Name"),
310             "base"                => _("Base"),
311             "description"         => _("Description"))
312           ));
313   }
315   /* Display generic part for server copy & paste */
316   function getCopyDialog()
317   {
318     $vars = array("cn");
319     $smarty = get_smarty();
320     $smarty->assign("cn" ,$this->cn);
321     $smarty->assign("object","component");
322     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
323     $ret = array();
324     $ret['string'] = $str;
325     $ret['status'] = "";
326     return($ret);
327   }
330   function saveCopyDialog()
331   {
332     if(isset($_POST['cn'])){
333       $this->cn = $_POST['cn'];
334     }
335   }
338   function PrepareForCopyPaste($source)
339   {
340     plugin::PrepareForCopyPaste($source);
341     if(isset($source['macAddress'][0])){
342       $this->netConfigDNS->macAddress = $source['macAddress'][0];
343     }
344     if(isset($source['ipHostNumber'][0])){
345       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
346     }
347   }
350 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
351 ?>