Code

edf3d65fd601a0fe37c60893a97744d377feb951
[gosa.git] / plugins / admin / systems / class_servKolab.inc
1 <?php
3 class servkolab extends plugin {
4   /* CLI vars */
5   var $cli_summary = "Manage server basic objects";
6   var $cli_description = "Some longer text\nfor help";
7   var $cli_parameters = array("eins"=>"Eins ist toll", "zwei"=>"Zwei ist noch besser");
9   var $postfix_mydomain                 = "\$domain";
10   var $postfix_mydestination            = "\$domain";
11   var $postfix_mynetworks               = "127.0.0.1/8";
12   var $postfix_enable_virus_scan        = "TRUE";
13   var $postfix_relayhost                = "";
14   var $postfix_mxrelayenabled           =  true;
15   var $postfix_allow_unauthenticated     = "false";
16   var $cyrus_quotawarn                  = "80";
17   var $kolabFreeBusyFuture              = "1";
18   var $cyrus_admins                     = "TRUE";
19   var $cyrus_imap                       = "TRUE";
20   var $cyrus_pop3                       = "TRUE";
21   var $cyrus_imaps                      = "TRUE";
22   var $cyrus_pop3s                      = "TRUE";
23   var $cyrus_sieve                      = "TRUE";
24   var $apache_allow_unauthenticated_fb  = "TRUE";
25   var $proftpd_ftp                      = "TRUE";
26   var $apache_http                      = "TRUE";
27   var $kolabHost                        = array();
28   var $added                            = false; 
29   var $remove                           = false;
30   var $changed                          = false;
31   var $attributes =  array("postfix_mydomain", "postfix_mydestination",
32         "postfix_mynetworks", "postfix_enable_virus_scan","postfix_relayhost","postfix_allow_unauthenticated",
33          "cyrus_admins", "cyrus_imap","kolabFreeBusyFuture",
34         "cyrus_pop3", "cyrus_imaps", "cyrus_pop3s", "cyrus_sieve",
35         "apache_allow_unauthenticated_fb", "proftpd_ftp",
36         "apache_http", "cyrus_quotawarn", "kolabHost");
38   var $objectclasses = array("top", "kolab");
40   function servkolab($config, $dn = NULL) 
41   {
44     /* Setting the hostname and tell this Plugin that we are the kolab extension*/
45     $this->hostname= preg_replace('/^cn=([^,]+),.*$/', '\1', $dn);
46     $this->dn = "k=kolab,".$config->current['BASE'];
47     
48     /* Load variables, if given*/
49     plugin::plugin($config, $this->dn);
51     /* Copy needed attributes */
52     foreach($this->attributes as $val) {
53       $name = preg_replace('/_/', '-', $val);
54       if (isset($this->attrs["$name"][0])) {
55         $this->$val = $this->attrs["$name"][0];
56       }
57     }
58   
59     /* Is this Server a member of the Kolab extension or not ?*/ 
60     if(isset($this->attrs['kolabHost']) && in_array($this->hostname, $this->attrs['kolabHost'])) {
61       $this->is_account=true;
62     }    else    {   
63       $this->is_account=false;    
64     } 
65   }
68   function execute() 
69   {
70     /* Fill templating stuff */
71     $smarty = get_smarty();
72     $display = "";
74     $this->changed =true;
76     /* Lets get all Host that are member of the Kolab extension, but don't catch count */
77     if(is_array($this->attrs['kolabHost']))
78       {
79       unset($this->attrs['kolabHost']['count']);
80       $this->kolabHost = $this->attrs['kolabHost'];
81       }
82     else
83       {
84       $this->attrs['kolabHost']=array();
85       $this->kolabHost = $this->attrs['kolabHost'];
86       }    
87     
88     /* Tell the script the dn, we are using */
89     $this->kolabdn = "k=kolab,".$this->config->current['BASE'];
91     /* The Ldap link is needed to ask ldap some questions */
92     $ldap = $this->config->get_ldap_link();
94     /* Do we need to flip is_account state? */
95     if (isset($_POST['modify_state'])) {
96       $this->is_account = !$this->is_account;
97       
99       /*if we flip to true, we'll have to create a new account, so lets do that*/
100       if($this->is_account==true)
101       {
102         /* Only add the given Hostname to the existing hosts*/
103         $this->kolabHost[]=$this->hostname;
104         $this->attrs['kolabHost'][]=$this->hostname;
105       }      
106       else
107       {
108         /* First we have to find the Host we want to delete, take the index and delete em*/
109         foreach($this->kolabHost as $key=>$host) 
110         {
111           /* Delete only if the Host is the in the array*/
112           if($host == $this->hostname)
113           {
114             unset($this->kolabHost[$key]);
115             unset($this->attrs['kolabHost'][$key]);
116           }
117         }
118       /* We deletet an Entry so we must reorder the index of the array */ 
119       $tmp = $this->kolabHost;
120       $this->kolabHost=array();
121       $this->attrs['kolabHost']=array();
122       
123       /* reorder  reorder ...*/
124       foreach($tmp as $host){
125         $this->kolabHost[]=$host;
126         $this->attrs['kolabHost'][]=$host;
127       }
128       /* Tell Save that we want to delete someone*/
129       $this->remove = true;
130       }
132     }
133     /* Check Relayhost and if we have MX lookup enabled*/
134     if(isset($this->postfix_relayhost[0]) && $this->postfix_relayhost[0]=="[")
135       {
136       $this->postfix_mxrelayenabled=false;
137       $this->postfix_relayhost = str_replace("[","",$this->postfix_relayhost);
138       $this->postfix_relayhost = str_replace("]","",$this->postfix_relayhost);
139       }
140     if($this->postfix_mxrelayenabled)    {
141       $smarty->assign("RelayMxSupportCheck"," checked ");
142       }      else      {
143       $smarty->assign("RelayMxSupportCheck","");
144       }
145     /* Show tab dialog headers */
146     if ($this->is_account) {
147       /* call Add Acoount to add account */
148       $display = $this->show_header(_("Remove Kolab extension"), _("This server has kolab features enabled. You can disable them by clicking below."));
149     } else {
150       /* call remove Account */
151       $display = $this->show_header(_("Add Kolab service"), _("This server has kolab features disabled. You can enable them by clicking below."));
152       return ($display);
153     }
155     /* Initialize all attributes, that were submitted */
156     foreach($this->attributes as $val) 
157     {
158       $smarty->assign($val."ACL",chkacl($this->acl,str_replace("_","-",$val)));
159       /* Tell smarty which variables we are useing */
160       $smarty->assign($val, $this->$val);
161       if (($this->$val != "FALSE") && (!empty($this->$val)))
162         $smarty->assign($val."Check", "checked");
163       else
164         $smarty->assign($val."Check", "");
165     }
167     /* Assemble free/busy string */
168     $edit= sprintf('<input name="kolabFreeBusyFuture" value="%s" %s type="text" maxlength="3" size="4">',
169             $this->kolabFreeBusyFuture, chkacl($this->acl, 'kolabFreeBusyFuture'));
170     $fbfuture= sprintf(_("Include data from %s days in the past when creating free/busy lists"), $edit);
171     $smarty->assign("fbfuture", $fbfuture);
173     /* Assemble quota string */
174     $edit= sprintf('<input name="cyrus_quotawarn" value="%s" type="text" maxlength="3" size="4" %s>',
175           $this->cyrus_quotawarn, chkacl($this->acl, 'cyrus_quotawarn'));
176     $quotastr= sprintf(_("Warn users when using more than %s%% of their mail quota"), $edit);
177     $smarty->assign("quotastr", $quotastr);
179     /* Load Template */
180     $display.=$smarty->fetch(get_template_path('servkolab.tpl', TRUE));
181     return ($display);
182   }
184   function remove_from_parent() 
185   {
186   }
189   function save_object()    
190   {
191     plugin::save_object();
192   }
195   function check() 
196   {
197     error_reporting(E_ALL);
198     $message = array();
199     if(($this->kolabFreeBusyFuture==""))    {
200       $message[] = _("Future days in Free/Busy settings must be set.");
201     }elseif(!is_uid($this->kolabFreeBusyFuture) || $this->kolabFreeBusyFuture < 0){
202       $message[] = _("Future days in Free/Busy settings must be a positive value.");
203     }
205     if(!is_int((int)($this->cyrus_quotawarn))) {
206       $message[] = _("The given Quota settings value must be a number.");
207     }elseif(!(($this->cyrus_quotawarn<=100)&&($this->cyrus_quotawarn>=0))){
208       $message[] = _("Please choose a value between 1 and 100 for Quota settings.");
209     }elseif(strcasecmp($this->cyrus_quotawarn,(int)$this->cyrus_quotawarn)){
210       $message[] = _("Future days must be a value.");
211     }
213     if(empty($this->postfix_mynetworks))
214     { 
215       $message[] = _("No SMTP privileged networks set.");
216     }
218     if(empty($this->postfix_relayhost))
219     {
220       $message[] = _("No SMTP smarthost/relayhost set.");
221     }
224     return ($message);
225   }
228   /* Save to LDAP */
229   function save() 
230   {
231     if(!$this->changed) return;
233     /* Set ldap connection */
234     $ldap = $this->config->get_ldap_link();
235     
236     /* Open current dn*/
237     $ldap->cd($this->dn);
239   
240     /* Setup Attributes to save */
241     $newattrs = array();
243     /* Set vars with correct - _ - */
244     foreach($this->attributes as $key) {
245       $key2 = str_replace("_", "-", $key);
246       $newattrs[$key2] = $this->$key;
247     }
248    
251     /* Set bool vars */
252     if ($newattrs['postfix-enable-virus-scan'] == 0)
253       $newattrs['postfix-enable-virus-scan'] = "FALSE";
254     else
255       $newattrs['postfix-enable-virus-scan'] = "TRUE";
256     if ($newattrs['cyrus-imap'] == 0)
257       $newattrs['cyrus-imap'] = "FALSE";
258     else
259       $newattrs['cyrus-imap'] = "TRUE";
260     if ($newattrs['cyrus-imaps'] == 0)
261       $newattrs['cyrus-imaps'] = "FALSE";
262     else
263       $newattrs['cyrus-imaps'] = "TRUE";
264     if ($newattrs['cyrus-pop3'] == 0)
265       $newattrs['cyrus-pop3'] = "FALSE";
266     else
267       $newattrs['cyrus-pop3'] = "TRUE";
268     if ($newattrs['cyrus-pop3s'] == 0)
269       $newattrs['cyrus-pop3s'] = "FALSE";
270     else
271       $newattrs['cyrus-pop3s'] = "TRUE";
272     if ($newattrs['cyrus-sieve'] == 0)
273       $newattrs['cyrus-sieve'] = "FALSE";
274     else
275       $newattrs['cyrus-sieve'] = "TRUE";
276     if ($newattrs['proftpd-ftp'] == 0)
277       $newattrs['proftpd-ftp'] = "FALSE";
278     else
279       $newattrs['proftpd-ftp'] = "TRUE";
280     if ($newattrs['apache-http'] == 0)
281       $newattrs['apache-http'] = "FALSE";
282     else
283       $newattrs['apache-http'] = "TRUE";
284     if ($newattrs['postfix-allow-unauthenticated'] == 0)
285       $newattrs['postfix-allow-unauthenticated'] = "FALSE";
286     else
287       $newattrs['postfix-allow-unauthenticated'] = "TRUE";
288     if ($newattrs['apache-allow-unauthenticated-fb'] == 0)
289       $newattrs['apache-allow-unauthenticated-fb'] = "FALSE";
290     else
291       $newattrs['apache-allow-unauthenticated-fb'] = "TRUE";
294     /* Reorder Host array, to get constant increasing index */
295     $newattrs['kolabHost'] = array();
297     /* Check Relayhost and if we have MX lookup enabled*/
298     if(isset($_POST['RelayMxSupport']))
299       $this->postfix_mxrelayenabled = true ;
300     else
301       $this->postfix_mxrelayenabled = false;
302     if(!$this->postfix_mxrelayenabled)
303       {
304       $newattrs['postfix-relayhost']="[".$this->postfix_relayhost."]";
305       }
307     /* If we want to add someone or do only changes on the settings ...*/
308     if(!$this->remove)
309     {
310       /* Get all Host known, that are member of the Kolab extension */
311       $this->kolabHost = $this->attrs['kolabHost'];
312       
313       /* So, the Host is already member of the extension, so get all and do nothing */
314       if(in_array($this->hostname,$this->kolabHost))
315         {
316         $newattrs['kolabHost']=$this->kolabHost;
317         }
318       /* So this is the first entry */
319       elseif(empty($this->kolabHost))
320         {
321         /* Create an array an add the Host */  
322         $newattrs['kolabHost']=array();
323         $newattrs['kolabHost'][]=$this->hostname;
324         }
325       /* Theres already an entry, but only in a string, */
326       elseif(is_string($this->kolabHost))
327         {
328         $tmp = $this->kolabHost;
329         $newattrs['kolabHost']=array();
330         $newattrs['kolabHost'][]=$tmp;
331         $newattrs['kolabHost'][]=$this->hostname;
332         }
333       /* Here we have already some entries */
334       elseif(is_array($newattrs['kolabHost']))
335         {
336         /* Insert the new one*/
337         foreach($this->kolabHost as $key=>$val) {
338           if (is_int($key)) {
339             $newattrs['kolabHost'][] = $val;
340             }
341           }
342         $newattrs['kolabHost'][]= $this->hostname;      
343         }
344       }
345       else
346       {
347       /* Here we want to delete an entry*/
348       $newattrs['kolabHost']= $this->attrs['kolabHost'];
349       }
350     /* Save as following object ! */
351     $this->kolabdn = "k=kolab,".$this->config->current['BASE'];
353     /* If we already have an object like this one,
354        we only need to modify the entry
355      */
357     /* is this an empty extension, no host defined for it, than delet it */
358     if(count($newattrs['kolabHost'])==0)
359     {
360       /* Delete the entry*/
361       $ldap->cd ("k=kolab,".$this->config->current['BASE']);
362       $ldap->rmdir("k=kolab,".$this->config->current['BASE']);
365     }elseif ($ldap->dn_exists($this->kolabdn)) {
366     /* Ok there is already an extension, so we only need to add the changes and the new Host, (if it was a new host)*/
367       gosa_log("Modifying");
368       
369       /* unset all attributes we won't change*/
370       unset($newattrs['cyrus-admins']);
371       unset($newattrs['postfix-mydomain']);
372       unset($newattrs['postfix-mydestination']);
373     
374       /* Update changes */
375       $ldap->cd($this->kolabdn);
376       $ldap->modify($newattrs);
377     } else {
379       /* We must create a new Entry */
380       $newattrs['k'] = "kolab";
381       $newattrs['uid'] = "freebusy";
382       $newattrs['postfix-mydomain'] = "gonicus.de";
383       $newattrs['postfix-mydestination'] = "\$mydomain";
384       $newattrs['userPassword'] = "tester";
385       $newattrs['objectClass'] = $this->objectclasses;
387       /* For better reading / Objectclass was added at last */ 
388       $newattrs = array_reverse ($newattrs);  
389       
390       /* And add the entry*/
391       $ldap->cd($this->kolabdn);
392       $ldap->add($newattrs);
393     }
394     /* show any errors */
395     show_ldap_error($ldap->get_error());
396     
397     /* Optionally execute a command after we're done */
398     if ($this->initially_was_account == $this->is_account) {
399       if ($this->is_modified) {
400         $this->handle_post_events("mofify");
401       }
402     } else {
403       $this->handle_post_events("add");
404     }
406   }
410 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
411 ?>