Code

2e65dccb06dd49077e326a59ea1ad929d0ae1ffd
[gosa.git] / plugins / admin / systems / class_componentGeneric.inc
1 <?php
3 class componentGeneric extends plugin
4 {
5   /* Generic terminal attributes */
6   var $interfaces= array();
7   var $ignore_account= TRUE;
9   /* Needed values and lists */
10   var $base= "";
11   var $cn= "";
12   var $description= "";
13   var $orig_dn= "";
15   /* attribute list for save action */
16   var $attributes= array("cn", "description");
17   var $objectclasses= array("top", "device", "ipHost", "ieee802Device");
18   var $netConfigDNS;
20   function componentgeneric ($config, $dn= NULL, $parent= NULL)
21   {
22     plugin::plugin ($config, $dn, $parent);
24     /* Set base */
25     if ($this->dn == "new"){
26       $ui= get_userinfo();
27       $this->base= dn2base($ui->dn);
28       $this->cn= "";
29     } else {
30       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
31     }
32     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses, true);
33     $this->netConfigDNS->acl = $this->acl;
35     /* Save dn for later references */
36     $this->orig_dn= $this->dn;
37   }
39   function execute()
40   {
41     $this->netConfigDNS->acl = $this->acl;
43     /* Call parent execute */
44     plugin::execute();
46     /* Do we represent a valid phone? */
47     if (!$this->is_account && $this->parent == NULL){
48       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
49         _("This 'dn' has no network features.")."</b>";
50       return($display);
51     }
53     /* Base select dialog */
54     $once = true;
55     foreach($_POST as $name => $value){
56       if(preg_match("/^chooseBase/",$name) && $once){
57         $once = false;
58         $this->dialog = new baseSelectDialog($this->config);
59         $this->dialog->setCurrentBase($this->base);
60       }
61     }
63     /* Dialog handling */
64     if(is_object($this->dialog)){
65       /* Must be called before save_object */
66       $this->dialog->save_object();
68       if($this->dialog->isClosed()){
69         $this->dialog = false;
70       }elseif($this->dialog->isSelected()){
71         $this->base = $this->dialog->isSelected();
72         $this->dialog= false;
73       }else{
74         return($this->dialog->execute());
75       }
76     }
78     /* Fill templating stuff */
79     $smarty= get_smarty();
80     $smarty->assign("bases", $this->config->idepartments);
82     /* Assign attributes */
83     foreach ($this->attributes as $attr){
84       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
85       $smarty->assign("$attr", $this->$attr);
86     }
87     $smarty->assign("base_select", $this->base);
88     $smarty->assign("baseACL", chkacl($this->acl,"base"));
89     $this->netConfigDNS->cn= $this->cn;
90     $smarty->assign("netconfig", $this->netConfigDNS->execute());
92     /* Display sub dialog from network settings */
93     if($this->netConfigDNS->dialog){
94       $this->dialog = TRUE;
95       return($this->netConfigDNS->execute());
96     }else{ 
97       $this->dialog = FALSE;
98     }
100     /* Show main page */
101     return($smarty->fetch (get_template_path('component.tpl', TRUE)));
102   }
104   function remove_from_parent()
105   {
106     $this->netConfigDNS->acl = $this->acl;
107     $ldap= $this->config->get_ldap_link();
108     $this->netConfigDNS->remove_from_parent();
109     $ldap->rmdir($this->dn);
110     show_ldap_error($ldap->get_error(), _("Removing generic component failed"));
111     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
113     /* Delete references to object groups */
114     $ldap->cd ($this->config->current['BASE']);
115     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
116     while ($ldap->fetch()){
117       $og= new ogroup($this->config, $ldap->getDN());
118       unset($og->member[$this->dn]);
119       $og->save ();
120     }
121   }
124   /* Save data to object */
125   function save_object()
126   {
127     plugin::save_object();
129     $this->netConfigDNS->save_object();
131     /* Save base, since this is no LDAP attribute */
132     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
133       $this->base= $_POST['base'];
134     }
135   }
138   /* Check supplied data */
139   function check()
140   {
141     /* Call common method to give check the hook */
142     $message= plugin::check();
143     $message= array_merge($message,$this->netConfigDNS->check());
145     $this->dn= "cn=".$this->cn.",ou=netdevices,ou=systems,".$this->base;
147     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
148       $message[]= _("The required field 'Component name' is not set.");
149     }
151     /* To check for valid ip*/
152     if($this->netConfigDNS->ipHostNumber == ""){
153        $message[]= _("The required field IP address is empty.");
154     } else {
155       if (!is_ip($this->netConfigDNS->ipHostNumber)){
156         $message[]= _("The field IP address contains an invalid address.");
157       }
158     }
160     $ui= get_userinfo();
161     $acl= get_permissions ($this->dn, $ui->subtreeACL);
162     $acl= get_module_permission($acl, "component", $this->dn);
163     if (chkacl($acl, "create") != ""){
164       $message[]= _("You have no permissions to create a component on this 'Base'.");
165     }
167     if ($this->orig_dn != $this->dn){
168       $ldap= $this->config->get_ldap_link();
169       $ldap->cd ($this->base);
170       $ldap->search ("(cn=".$this->cn.")", array("cn"));
171       if ($ldap->count() != 0){
172         while ($attrs= $ldap->fetch()){
173           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
174             continue;
175           }
176           if ($attrs['dn'] != $this->orig_dn){
177             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
178             break;
179           }
180         }
181       }
182     }
184     return ($message);
185   }
188   /* Save to LDAP */
189   function save()
190   {
191     $this->netConfigDNS->acl = $this->acl;
192     plugin::save();
194     /* Remove all empty values */
195     if ($this->orig_dn == 'new'){
196       $attrs= array();
197       foreach ($this->attrs as $key => $val){
198         if (is_array($val) && count($val) == 0){
199           continue;
200         }
201         $attrs[$key]= $val;
202       }
203       $this->attrs= $attrs;
204     }
206     /* If this is a new Object IP & Mac aren't set.
207          IP & Mac are msut attributes, so we set this values by here. */
208     if($this->orig_dn == 'new'){
209       $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
210       $this->attrs['macAddress']  = $this->netConfigDNS->macAddress;
211     }
213     /* Write back to ldap */
214     $ldap= $this->config->get_ldap_link();
215     if ($this->orig_dn == 'new'){
216       $ldap->cd($this->config->current['BASE']);
217       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
218       $ldap->cd($this->dn);
219       $ldap->add($this->attrs);
220       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
221     } else {
222       if ($this->orig_dn != $this->dn){
223         $this->move($this->orig_dn, $this->dn);
224       }
226       $ldap->cd($this->dn);
227       $this->cleanup();
228       $ldap->modify ($this->attrs); 
230       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
231     }
233     $this->netConfigDNS->cn = $this->cn;
234     $this->netConfigDNS->save($this->dn);
236     show_ldap_error($ldap->get_error(), _("Saving generic component failed"));
237   }
240 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
241 ?>