Code

Updated inherit button filter, only display the button if gotoWorkstationTemplate...
[gosa.git] / plugins / admin / systems / class_servDB.inc
1 <?php
3 class servdb extends plugin
4 {
5   /* Object information */
6   var $goImapName= "";
7   var $goImapName_old= "";
8   var $goImapConnect= "";
9   var $goImapAdmin= "";
10   var $goImapPassword= "";
11   var $goImapSieveServer= "";
12   var $goImapSievePort= "";
13   var $goKrbRealm= "";
14   var $goKrbAdmin= "";
15   var $goKrbPassword= "";
16   var $goFaxAdmin= "";
17   var $goFaxPassword= "";
18   var $goLogAdmin= "";
19   var $goLogPassword= "";
20   var $goFonAdmin= "";
21   var $goFonPassword= "";
22   var $goFonAreaCode= "";
23   var $goFonCountryCode= "";
24   var $ignore_account= TRUE;
25   var $goGlpiAdmin="";
26   var $goGlpiDatabase = "";
27   var $goGlpiPassword ="";
28   
30   /* attribute list for save action */
31   var $attributes= array("goImapName", "goImapConnect", "goImapAdmin", "goImapPassword",
32                          "goImapSieveServer", "goImapSievePort", "goKrbRealm", 
33                          "goKrbAdmin", "goKrbPassword", "goFaxAdmin", "goFaxPassword",
34                          "goLogAdmin", "goLogPassword", "goFonAdmin", "goFonPassword",
35                          "goFonAreaCode", "goFonCountryCode","goGlpiAdmin","goGlpiDatabase","goGlpiPassword");
36   var $objectclasses  = array("top", "goServer");
37   var $init_objectclasses = array();
38   var $additionaloc= array("goImapServer" => array("goImapName", "goImapConnect",
39                                                 "goImapAdmin", "goImapPassword",
40                                                 "goImapSieveServer", 
41                                                 "goImapSievePort"),
42                           "goKrbServer" => array("goKrbRealm", "goKrbAdmin",
43                                                 "goKrbPassword"),
44                           "goFaxServer" => array("goFaxAdmin", "goFaxPassword"),
45                           "goLogDBServer" => array("goLogAdmin", "goLogPassword"),
46                           "goGlpiServer" => array("goGlpiAdmin","goGlpiDatabase","goGlpiPassword"),
47                           "goFonServer" => array("goFonAdmin", "goFonPassword",
48                                                 "goFonAreaCode", "goFonCountryCode"));
50   function servdb ($config, $dn= NULL, $parent= NULL)
51   {
52     plugin::plugin ($config, $dn, $parent);
54     /* Make dynamic list of objectClasses */
55     foreach ($this->additionaloc as $oc => $dummy){
56       if (isset($this->attrs['objectClass']) && in_array($oc, $this->attrs['objectClass'])){
57         $this->objectclasses[$oc]= $oc;
58       }
59     }
61     /* Remember used databases */
62     $this->init_objectclasses = $this->objectclasses;
64     /* We need to save the historical state of goImapName for later checking */
65     $this->goImapName_old= $this->goImapName;
67     /* Always is account... */
68     $this->is_account= TRUE;
69   }
71   function execute()
72   {
73     /* Call parent execute */
74     plugin::execute();
76     /* Fill templating stuff */
77     $smarty= get_smarty();
79     /* Attributes... */
80     foreach ($this->attributes as $attr){
81       $smarty->assign("$attr", $this->$attr);
82       $smarty->assign("$attr"."ACL", chkacl($this->acl, $attr));
83     }
85     /* Classes... */
86     foreach ($this->additionaloc as $oc => $dummy){
87       if (isset($this->objectclasses[$oc])){
88         $smarty->assign("$oc", "checked");
89         $smarty->assign("$oc"."State", "");
90         $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
91         
92       } else {
93         $smarty->assign("$oc", "");
94         $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
95         $smarty->assign("$oc"."State", "disabled");
96       }
97     }
99     return($smarty->fetch (get_template_path('servdb.tpl', TRUE)));
100   }
102   function remove_from_parent()
103   {
104     /* This cannot be removed... */
105   }
108   /* Save data to object */
109   function save_object()
110   {
111     if (isset($_POST['dbtab'])){
112       plugin::save_object();
113       
114       /* Save checkbox state */
115       foreach ($this->additionaloc as $oc => $dummy){
117         if(chkacl($this->acl,$oc) == ""){
118           if (isset($_POST[$oc]) && $_POST[$oc] == '1'){
119             $this->objectclasses[$oc]= $oc;
120           } else {
121             unset($this->objectclasses[$oc]);
122           }
123         }
124       }
125     }
126   }
129   /* Check supplied data */
130   function check()
131   {
132     /* Call common method to give check the hook */
133     $message= plugin::check();
135     if(in_array("goGlpiServer",$this->objectclasses)){
136       foreach(array("goGlpiAdmin","goGlpiDatabase") as $attr){
137         if(empty($this->$attr)){
138           $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
139         }
140       }
141     }
143     /* All fields are marked as *must* */
144     if (in_array("goImapServer", $this->objectclasses)){
145       foreach (array("goImapAdmin", "goImapName") as $attr){
146         if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
147           $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
148         }
149       }
152       /* Check connect string */
153       if (!preg_match('/^\{[^:]+:[0-9]+.*\}$/', $this->goImapConnect)){
154         $message[]= sprintf(_("The imap connect string needs to be in the form '%s'."),
155                     '{server-name:port/options}');
156       }
157       if (!preg_match('/^[0-9]+$/', $this->goImapSievePort)){
158         $message[]= _("The sieve port needs to be numeric.");
159       }
160     }
161     if (in_array("goKrbServer", $this->objectclasses)){
162       foreach (array("goKrbAdmin", "goKrbRealm") as $attr){
163         if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
164           $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
165         }
166       }
167       /* goKrbPassword is a must field, if goKrbServer is used as objectClass */
168       if ($this->goKrbPassword == "" ){
169         $message[]= sprintf(_("The specified kerberos password is empty."), $attr);
170       }
171     }
172     if (in_array("goFaxServer", $this->objectclasses)){
173       if ($this->goFaxAdmin == "" || preg_match("/ /", $this->goFaxAdmin)){
174         $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), "goFaxAdmin");
175       }
176     }
177     if (in_array("goLogServer", $this->objectclasses)){
178       if ($this->goLogAdmin == "" || preg_match("/ /", $this->goLogAdmin)){
179         $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), "goLogAdmin");
180       }
181     }
182     if (in_array("goFonServer", $this->objectclasses)){
183       foreach (array("goFonAdmin", "goFonAreaCode", "goFonCountryCode") as $attr){
184         if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
185           $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
186         }
187       }
188     }
189     
190     /* Check if we are able to remove the asterisk database. If the database is still in use skip */
191     if (!in_array("goFonServer", $this->objectclasses) && in_array("goFonServer", $this->init_objectclasses)){
192       $ldap = $this->config->get_ldap_link();
193       $ldap->cd($this->config->current['BASE']);
194       $ldap->search("(&(goFonHomeServer=".$this->dn.")(|(uid=*)(cn=*)))",array("dn","uid","cn","goFonHomeServer"));
195       if($ldap->count()){
197         /* Number of entries shown in warning */
198         $i    = 3;
199         $str  = "";   
200         while(($attrs = $ldap->fetch()) && $i >= 0){
201           $i --; 
202           if(isset($attrs['uid'][0])){
203             $str .= $attrs['uid'][0]." ";
204           }else{
205             $str .= $attrs['cn'][0]." ";
206           }
207         }
209         /* Some entries found */
210         if($i != 3){
211           $message[] = sprintf(_("You can't remove the asterisk database extension, it is still in use by these objects '%s'."),trim($str));
212         }
213       } 
214     }
216     if (in_array("goImapServer", $this->objectclasses) && 
217         $this->goImapName != $this->goImapName_old) {
218       // Attribute has changed
219       if(!preg_match("/^[a-z0-9.-]+$/", $this->goImapName)) {
220         $message[]= sprintf(_("The imap name string needs to be a hostname or an IP-address."));
221       }
222     }
224     return ($message);
225   }
228   /* Save to LDAP */
229   function save()
230   {
231     /* Normalize lazy objectclass arrays */
232     $objectclasses= array();
233     foreach($this->objectclasses as $oc){
234       $objectclasses[]= $oc;
235     }
236     
237     /* Move cn to sieve server */
238     $this->goImapSieveServer= $this->cn;
239     
240     plugin::save();
241   
242     $test = $this->objectclasses;
243     $tmp = array_flip($this->attrs['objectClass']);
245     foreach($this->additionaloc as $key=>$val) {
246       unset($tmp[$key]);
247       }
249     $classes = (array_flip(array_merge(array_flip($test),$tmp)));
251     unset($this->attrs['objectClass']);
253     foreach($classes as $class){
254       $this->attrs['objectClass'][]=$class;
255     }
258     /* Remove unneeded attributes */
259     foreach ($this->additionaloc as $oc => $attrs){
260       if (!in_array($oc, $this->attrs['objectClass'])){
261         foreach ($attrs as $attr){
262           $this->attrs[$attr]= array();
263         }
264       }
265     }
266     $this->attrs = array_reverse($this->attrs);
268     /* Write to LDAP */
269     $ldap= $this->config->get_ldap_link();
270     $ldap->cd($this->dn);
271     $this->cleanup();
272     $ldap->modify ($this->attrs); 
274     show_ldap_error($ldap->get_error(), _("Saving server db settings failed"));
276     /* Optionally execute a command after we're done */
277     if ($this->initially_was_account == $this->is_account){
278       if ($this->is_modified){
279         $this->handle_post_events("modify");
280       }
281     } else {
282       $this->handle_post_events("add");
283     }
284   }
286   function allow_remove()
287   {
288     /* Check if we are able to remove the asterisk database. If the database is still in use skip */
289     if (in_array_ics("goFonServer", $this->init_objectclasses)){
290       $ldap = $this->config->get_ldap_link();
291       $ldap->cd($this->config->current['BASE']);
292       $ldap->search("(&(goFonHomeServer=".$this->dn.")(|(uid=*)(cn=*)))",array("dn","uid","cn","goFonHomeServer"));
293       if($ldap->count()){
295         /* Number of entries shown in warning */
296         $i    = 3;
297         $str  = "";
298         while(($attrs = $ldap->fetch()) && $i >= 0){
299           $i --;
300           if(isset($attrs['uid'][0])){
301             $str .= $attrs['uid'][0]." ";
302           }else{
303             $str .= $attrs['cn'][0]." ";
304           }
305         }
307         /* Some entries found */
308         if($i != 3){
309           return(sprintf(_("You can't remove the asterisk database extension, it is still in use by these objects '%s'."),trim($str)));
310         }
311       }
312     }
313   }
318 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
319 ?>