Code

Udpated gotomasses
[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     $str = $this->netConfigDNS->execute();
112     if(is_object($this->netConfigDNS->dialog)){
113       return($str);
114     }
115     $smarty->assign("netconfig", $str);
116     return($smarty->fetch (get_template_path('component.tpl', TRUE)));
117   }
119   function set_acl_base($base)
120   {
121     plugin::set_acl_base($base);
122     $this->netConfigDNS->set_acl_base($base);
123   }
125   function set_acl_category($cat)
126   {
127     plugin::set_acl_category($cat);
128     $this->netConfigDNS->set_acl_category($cat);
129   }
131   function remove_from_parent()
132   {
133     $ldap= $this->config->get_ldap_link();
134     $this->netConfigDNS->remove_from_parent();
135     $ldap->rmdir($this->dn);
136     
137     new log("remove","component/".get_class($this),$this->dn,$this->attributes,$ldap->get_error());
139     show_ldap_error($ldap->get_error(), sprintf(_("Removing of system component/generic with dn '%s' failed."),$this->dn));
140     $this->handle_post_events(array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
142     /* Delete references to object groups */
143     $ldap->cd ($this->config->current['BASE']);
144     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
145     while ($ldap->fetch()){
146       $og= new ogroup($this->config, $ldap->getDN());
147       unset($og->member[$this->dn]);
148       $og->save ();
149     }
150   }
153   /* Save data to object */
154   function save_object()
155   {
156     
157     /* Create a base backup and reset the
158        base directly after calling plugin::save_object();
159        Base will be set seperatly a few lines below */
160     $base_tmp = $this->base;
161     plugin::save_object();
162     $this->base = $base_tmp;
164     $this->netConfigDNS->save_object();
166     /* Set new base if allowed */
167     $tmp = $this->get_allowed_bases();
168     if(isset($_POST['base'])){
169       if(isset($tmp[$_POST['base']])){
170         $this->base= $_POST['base'];
171       }
172     }
173   }
176   /* Check supplied data */
177   function check()
178   {
179     /* Call common method to give check the hook */
180     $message= plugin::check();
181     $message= array_merge($message,$this->netConfigDNS->check());
183     $this->dn= "cn=".$this->cn.",ou=netdevices,ou=systems,".$this->base;
185     if ($this->cn == "" ){
186       $message[]= _("The required field 'Component name' is not set.");
187     }
189     /* To check for valid ip*/
190     if($this->netConfigDNS->ipHostNumber == ""){
191        $message[]= _("The required field IP address is empty.");
192     } else {
193       if (!is_ip($this->netConfigDNS->ipHostNumber)){
194         $message[]= _("The field IP address contains an invalid address.");
195       }
196     }
198     if ($this->orig_dn != $this->dn){
199       $ldap= $this->config->get_ldap_link();
200       $ldap->cd ($this->base);
201       $ldap->search ("(cn=".$this->cn.")", array("cn"));
202       if ($ldap->count() != 0){
203         while ($attrs= $ldap->fetch()){
204           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
205             continue;
206           }
207           if ($attrs['dn'] != $this->orig_dn){
208             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
209             break;
210           }
211         }
212       }
213     }
215     return ($message);
216   }
219   /* Save to LDAP */
220   function save()
221   {
222     plugin::save();
224     /* Remove all empty values */
225     if ($this->orig_dn == 'new'){
226       $attrs= array();
227       foreach ($this->attrs as $key => $val){
228         if (is_array($val) && count($val) == 0){
229           continue;
230         }
231         $attrs[$key]= $val;
232       }
233       $this->attrs= $attrs;
234     }
236     /* If this is a new Object IP & Mac aren't set.
237          IP & Mac are msut attributes, so we set this values by here. */
238     if($this->orig_dn == 'new'){
239       $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
240       $this->attrs['macAddress']  = $this->netConfigDNS->macAddress;
241     }
243     /* Write back to ldap */
244     $ldap= $this->config->get_ldap_link();
245     if ($this->orig_dn == 'new'){
246       $ldap->cd($this->config->current['BASE']);
247       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
248       $ldap->cd($this->dn);
249       $ldap->add($this->attrs);
250       new log("create","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
251       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
252       
253     } else {
254       if ($this->orig_dn != $this->dn){
255         $this->move($this->orig_dn, $this->dn);
256       }
258       $ldap->cd($this->dn);
259       $this->cleanup();
260       $ldap->modify ($this->attrs); 
261       new log("modify","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
262       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
263     }
265     $this->netConfigDNS->cn = $this->cn;
266     $this->netConfigDNS->save();
268     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system component/generic with dn '%s' failed."),$this->dn));
269   }
271   /* Return plugin informations for acl handling */
272   function plInfo()
273   {
274     return (array(
275           "plShortName"   => _("Generic"),
276           "plDescription" => _("Component generic"),
277           "plSelfModify"  => FALSE,
278           "plDepends"     => array(),
279           "plPriority"    => 1,
280           "plSection"     => array("administration"),
281           "plCategory"    => array("component" => array("description"  => _("Network device"),
282                                                         "objectClass"  => array("device", "ipHost", "ieee802Device"))),
283           "plProvidedAcls"=> array(
284             "cn"                  => _("Name"),
285             "base"                => _("Base"),
286             "description"         => _("Description"))
287           ));
288   }
290   /* Display generic part for server copy & paste */
291   function getCopyDialog()
292   {
293     $vars = array("cn");
294     $smarty = get_smarty();
295     $smarty->assign("cn" ,$this->cn);
296     $smarty->assign("object","component");
297     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
298     $ret = array();
299     $ret['string'] = $str;
300     $ret['status'] = "";
301     return($ret);
302   }
305   function saveCopyDialog()
306   {
307     if(isset($_POST['cn'])){
308       $this->cn = $_POST['cn'];
309     }
310   }
313   function PrepareForCopyPaste($source)
314   {
315     plugin::PrepareForCopyPaste($source);
316     if(isset($source['macAddress'][0])){
317       $this->netConfigDNS->macAddress = $source['macAddress'][0];
318     }
319     if(isset($source['ipHostNumber'][0])){
320       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
321     }
322   }
325 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
326 ?>