Code

90614de98bfdf32fd4e2a000fee0e9b6388fe02f
[gosa.git] / plugins / admin / systems / class_componentGeneric.inc
1 <?php
3 class componentGeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage component base objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* Generic terminal attributes */
11   var $interfaces= array();
12   var $ignore_account= TRUE;
14   /* Needed values and lists */
15   var $base= "";
16   var $cn= "";
17   var $description= "";
18   var $orig_dn= "";
20   /* attribute list for save action */
21   var $attributes= array("cn", "description");
22   var $objectclasses= array("top", "device", "ipHost", "ieee802Device");
23   var $netConfigDNS;
24   var $view_logged  = FALSE;
26   function componentgeneric ($config, $dn= NULL, $parent= NULL)
27   {
28     plugin::plugin ($config, $dn, $parent);
30     /* Set base */
31     if ($this->dn == "new"){
32       $ui= get_userinfo();
33       $this->base= dn2base($ui->dn);
34       $this->cn= "";
35     } else {
36       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
37     }
38     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses, true);
39     /* Save dn for later references */
40     $this->orig_dn= $this->dn;
41   }
44   function execute()
45   {
46     /* Call parent execute */
47     plugin::execute();
49     if($this->is_account && !$this->view_logged){
50       $this->view_logged = TRUE;
51       new log("view","component/".get_class($this),$this->dn);
52     }
54     /* Do we represent a valid phone? */
55     if (!$this->is_account && $this->parent == NULL){
56       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
57         _("This 'dn' has no network features.")."</b>";
58       return($display);
59     }
61     /* Base select dialog */
62     $once = true;
63     foreach($_POST as $name => $value){
64       if(preg_match("/^chooseBase/",$name) && $once){
65         $once = false;
66         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
67         $this->dialog->setCurrentBase($this->base);
68       }
69     }
71     /* Dialog handling */
72     if(is_object($this->dialog)){
73       /* Must be called before save_object */
74       $this->dialog->save_object();
76       if($this->dialog->isClosed()){
77         $this->dialog = false;
78       }elseif($this->dialog->isSelected()){
80         /* A new base was selected, check if it is a valid one */
81         $tmp = $this->get_allowed_bases();
82         if(isset($tmp[$this->dialog->isSelected()])){
83           $this->base = $this->dialog->isSelected();
84         }
86         $this->dialog= false;
87       }else{
88         return($this->dialog->execute());
89       }
90     }
92     /* Fill templating stuff */
93     $smarty= get_smarty();
94     $smarty->assign("bases", $this->config->idepartments);
96     /* Set acls */
97     $tmp = $this->plInfo();
98     foreach($tmp['plProvidedAcls'] as $name => $translation){
99       $smarty->assign($name."ACL",$this->getacl($name));
100     }
102     $smarty->assign("bases", $this->get_allowed_bases());
104     /* Assign attributes */
105     foreach ($this->attributes as $attr){
106       $smarty->assign("$attr", $this->$attr);
107     }
108     $smarty->assign("base_select", $this->base);
110     /* Show main page */
111     $smarty->assign("netconfig", $this->netConfigDNS->execute());
112     return($smarty->fetch (get_template_path('component.tpl', TRUE)));
113   }
115   function set_acl_base($base)
116   {
117     plugin::set_acl_base($base);
118     $this->netConfigDNS->set_acl_base($base);
119   }
121   function set_acl_category($cat)
122   {
123     plugin::set_acl_category($cat);
124     $this->netConfigDNS->set_acl_category($cat);
125   }
127   function remove_from_parent()
128   {
129     $ldap= $this->config->get_ldap_link();
130     $this->netConfigDNS->remove_from_parent();
131     $ldap->rmdir($this->dn);
132     
133     new log("remove","component/".get_class($this),$this->dn,$this->attributes,$ldap->get_error());
135     show_ldap_error($ldap->get_error(), sprintf(_("Removing of system component/generic with dn '%s' failed."),$this->dn));
136     $this->handle_post_events(array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
138     /* Delete references to object groups */
139     $ldap->cd ($this->config->current['BASE']);
140     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
141     while ($ldap->fetch()){
142       $og= new ogroup($this->config, $ldap->getDN());
143       unset($og->member[$this->dn]);
144       $og->save ();
145     }
146   }
149   /* Save data to object */
150   function save_object()
151   {
152     
153     /* Create a base backup and reset the
154        base directly after calling plugin::save_object();
155        Base will be set seperatly a few lines below */
156     $base_tmp = $this->base;
157     plugin::save_object();
158     $this->base = $base_tmp;
160     $this->netConfigDNS->save_object();
162     /* Set new base if allowed */
163     $tmp = $this->get_allowed_bases();
164     if(isset($_POST['base'])){
165       if(isset($tmp[$_POST['base']])){
166         $this->base= $_POST['base'];
167       }
168     }
169   }
172   /* Check supplied data */
173   function check()
174   {
175     /* Call common method to give check the hook */
176     $message= plugin::check();
177     $message= array_merge($message,$this->netConfigDNS->check());
179     $this->dn= "cn=".$this->cn.",ou=netdevices,ou=systems,".$this->base;
181     if ($this->cn == "" ){
182       $message[]= _("The required field 'Component name' is not set.");
183     }
185     /* To check for valid ip*/
186     if($this->netConfigDNS->ipHostNumber == ""){
187        $message[]= _("The required field IP address is empty.");
188     } else {
189       if (!is_ip($this->netConfigDNS->ipHostNumber)){
190         $message[]= _("The field IP address contains an invalid address.");
191       }
192     }
194     if ($this->orig_dn != $this->dn){
195       $ldap= $this->config->get_ldap_link();
196       $ldap->cd ($this->base);
197       $ldap->search ("(cn=".$this->cn.")", array("cn"));
198       if ($ldap->count() != 0){
199         while ($attrs= $ldap->fetch()){
200           if ($attrs['dn'] != $this->orig_dn){
201             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
202             break;
203           }
204         }
205       }
206     }
208     return ($message);
209   }
212   /* Save to LDAP */
213   function save()
214   {
215     plugin::save();
217     /* Remove all empty values */
218     if ($this->orig_dn == 'new'){
219       $attrs= array();
220       foreach ($this->attrs as $key => $val){
221         if (is_array($val) && count($val) == 0){
222           continue;
223         }
224         $attrs[$key]= $val;
225       }
226       $this->attrs= $attrs;
227     }
229     /* If this is a new Object IP & Mac aren't set.
230          IP & Mac are msut attributes, so we set this values by here. */
231     if($this->orig_dn == 'new'){
232       $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
233       $this->attrs['macAddress']  = $this->netConfigDNS->macAddress;
234     }
236     /* Write back to ldap */
237     $ldap= $this->config->get_ldap_link();
238     if ($this->orig_dn == 'new'){
239       $ldap->cd($this->config->current['BASE']);
240       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
241       $ldap->cd($this->dn);
242       $ldap->add($this->attrs);
243       new log("create","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
244       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
245       
246     } else {
247       if ($this->orig_dn != $this->dn){
248         $this->move($this->orig_dn, $this->dn);
249       }
251       $ldap->cd($this->dn);
252       $this->cleanup();
253       $ldap->modify ($this->attrs); 
254       new log("modify","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
255       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
256     }
258     $this->netConfigDNS->cn = $this->cn;
259     $this->netConfigDNS->save($this->dn);
261     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system component/generic with dn '%s' failed."),$this->dn));
262   }
264   /* Return plugin informations for acl handling */
265   function plInfo()
266   {
267     return (array(
268           "plShortName"   => _("Generic"),
269           "plDescription" => _("Component generic"),
270           "plSelfModify"  => FALSE,
271           "plDepends"     => array(),
272           "plPriority"    => 1,
273           "plSection"     => array("administration"),
274           "plCategory"    => array("component" => array("description"  => _("Network device"),
275                                                         "objectClass"  => array("device", "ipHost", "ieee802Device"))),
276           "plProvidedAcls"=> array(
277             "cn"                  => _("Name"),
278             "base"                => _("Base"),
279             "description"         => _("Description"))
280           ));
281   }
283   /* Display generic part for server copy & paste */
284   function getCopyDialog()
285   {
286     $vars = array("cn");
287     $smarty = get_smarty();
288     $smarty->assign("cn" ,$this->cn);
289     $smarty->assign("object","component");
290     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
291     $ret = array();
292     $ret['string'] = $str;
293     $ret['status'] = "";
294     return($ret);
295   }
298   function saveCopyDialog()
299   {
300     if(isset($_POST['cn'])){
301       $this->cn = $_POST['cn'];
302     }
303   }
306   function PrepareForCopyPaste($source)
307   {
308     plugin::PrepareForCopyPaste($source);
309     if(isset($source['macAddress'][0])){
310       $this->netConfigDNS->macAddress = $source['macAddress'][0];
311     }
312     if(isset($source['ipHostNumber'][0])){
313       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
314     }
315   }
318 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
319 ?>