Code

5605253aa5b91b0dbcf2072c7aba3632332a303d
[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,$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(preg_match("/cn=dhcp,/",$attrs['dn'])){
201             continue;
202           }
203           if ($attrs['dn'] != $this->orig_dn){
204             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
205             break;
206           }
207         }
208       }
209     }
211     return ($message);
212   }
215   /* Save to LDAP */
216   function save()
217   {
218     plugin::save();
220     /* Remove all empty values */
221     if ($this->orig_dn == 'new'){
222       $attrs= array();
223       foreach ($this->attrs as $key => $val){
224         if (is_array($val) && count($val) == 0){
225           continue;
226         }
227         $attrs[$key]= $val;
228       }
229       $this->attrs= $attrs;
230     }
232     /* If this is a new Object IP & Mac aren't set.
233          IP & Mac are msut attributes, so we set this values by here. */
234     if($this->orig_dn == 'new'){
235       $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
236       $this->attrs['macAddress']  = $this->netConfigDNS->macAddress;
237     }
239     /* Write back to ldap */
240     $ldap= $this->config->get_ldap_link();
241     if ($this->orig_dn == 'new'){
242       $ldap->cd($this->config->current['BASE']);
243       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
244       $ldap->cd($this->dn);
245       $ldap->add($this->attrs);
246       new log("create","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
247       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
248       
249     } else {
250       if ($this->orig_dn != $this->dn){
251         $this->move($this->orig_dn, $this->dn);
252       }
254       $ldap->cd($this->dn);
255       $this->cleanup();
256       $ldap->modify ($this->attrs); 
257       new log("modify","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
258       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
259     }
261     $this->netConfigDNS->cn = $this->cn;
262     $this->netConfigDNS->save();
264     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system component/generic with dn '%s' failed."),$this->dn));
265   }
267   /* Return plugin informations for acl handling */
268   function plInfo()
269   {
270     return (array(
271           "plShortName"   => _("Generic"),
272           "plDescription" => _("Component generic"),
273           "plSelfModify"  => FALSE,
274           "plDepends"     => array(),
275           "plPriority"    => 1,
276           "plSection"     => array("administration"),
277           "plCategory"    => array("component" => array("description"  => _("Network device"),
278                                                         "objectClass"  => array("device", "ipHost", "ieee802Device"))),
279           "plProvidedAcls"=> array(
280             "cn"                  => _("Name"),
281             "base"                => _("Base"),
282             "description"         => _("Description"))
283           ));
284   }
286   /* Display generic part for server copy & paste */
287   function getCopyDialog()
288   {
289     $vars = array("cn");
290     $smarty = get_smarty();
291     $smarty->assign("cn" ,$this->cn);
292     $smarty->assign("object","component");
293     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
294     $ret = array();
295     $ret['string'] = $str;
296     $ret['status'] = "";
297     return($ret);
298   }
301   function saveCopyDialog()
302   {
303     if(isset($_POST['cn'])){
304       $this->cn = $_POST['cn'];
305     }
306   }
309   function PrepareForCopyPaste($source)
310   {
311     plugin::PrepareForCopyPaste($source);
312     if(isset($source['macAddress'][0])){
313       $this->netConfigDNS->macAddress = $source['macAddress'][0];
314     }
315     if(isset($source['ipHostNumber'][0])){
316       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
317     }
318   }
321 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
322 ?>