Code

2587689a670ddaff7f38765506d0a522b4403b55
[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";//ok
12   var $postfix_enable_virus_scan        = "TRUE";//ok
13   var $postfix_relayhost                = "";
14   var $postfix_mxrelayenabled           =  true;
15   var $postfix_allow_unauthenticated     = "false";
16   var $cyrus_quotawarn                  = "80";//ok
17   var $kolabFreeBusyFuture              = "1";// ok 
18   var $cyrus_admins                     = "TRUE";//<<<<<<<<<<<<<<<<
19   var $cyrus_imap                       = "TRUE";//ok
20   var $cyrus_pop3                       = "TRUE";//ok
21   var $cyrus_imaps                      = "TRUE";//ok
22   var $cyrus_pop3s                      = "TRUE";//ok
23   var $cyrus_sieve                      = "TRUE";//ok
24   var $apache_allow_unauthenticated_fb  = "TRUE";//<<<<<<<<<<<<
25   var $proftpd_ftp                      = "TRUE";//pk
26   var $apache_http                      = "TRUE";//ok
27   var $kolabHost                        = array();
28   var $added                            = false; 
29   var $remove                           = false;
30   var $attributes =  array("postfix_mydomain", "postfix_mydestination",
31         "postfix_mynetworks", "postfix_enable_virus_scan","postfix_relayhost","postfix_allow_unauthenticated",
32          "cyrus_admins", "cyrus_imap","kolabFreeBusyFuture",
33         "cyrus_pop3", "cyrus_imaps", "cyrus_pop3s", "cyrus_sieve",
34         "apache_allow_unauthenticated_fb", "proftpd_ftp",
35         "apache_http", "cyrus_quotawarn", "kolabHost");
37   var $objectclasses = array("top", "kolab");
39   function servkolab($config, $dn = NULL) 
40   {
43     /* Setting the hostname and tell this Plugin that we are the kolab extension*/
44     $this->hostname= preg_replace('/^cn=([^,]+),.*$/', '\1', $dn);
45     $this->dn = "k=kolab,".$config->current['BASE'];
46     
47     /* Load variables, if given*/
48     plugin::plugin($config, $this->dn);
50     /* Copy needed attributes */
51     foreach($this->attributes as $val) {
52       $name = preg_replace('/_/', '-', $val);
53       if (isset($this->attrs["$name"][0])) {
54         $this->$val = $this->attrs["$name"][0];
55       }
56     }
57   
58     /* Is this Server a member of the Kolab extension or not ?*/ 
59     if(in_array($this->hostname,$this->attrs['kolabHost'])) {
60       $this->is_account=true;
61     }    else    {   
62       $this->is_account=false;    } 
63     }
66   function execute() 
67   {
69     /* Fill templating stuff */
70     $smarty = get_smarty();
71     $display = "";
73     /* Lets get all Host that are member of the Kolab extension, but don't catch count */
74     if(is_array($this->attrs['kolabHost']))
75       {
76       unset($this->attrs['kolabHost']['count']);
77       $this->kolabHost = $this->attrs['kolabHost'];
78       }
79     else
80       {
81       $this->attrs['kolabHost']=array();
82       $this->kolabHost = $this->attrs['kolabHost'];
83       }    
84     
85     /* Tell the script the dn, we are using */
86     $this->kolabdn = "k=kolab,".$this->config->current['BASE'];
88     /* The Ldap link is needed to ask ldap some questions */
89     $ldap = $this->config->get_ldap_link();
91     /* Do we need to flip is_account state? */
92     if (isset($_POST['modify_state'])) {
93       $this->is_account = !$this->is_account;
94       
96       /*if we flip to true, we'll have to create a new account, so lets do that*/
97       if($this->is_account==true)
98       {
99         /* Only add the given Hostname to the existing hosts*/
100         $this->kolabHost[]=$this->hostname;
101         $this->attrs['kolabHost'][]=$this->hostname;
102       }      
103       else
104       {
105         /* First we have to find the Host we want to delete, take the index and delete em*/
106         foreach($this->kolabHost as $key=>$host) 
107         {
108           /* Delete only if the Host is the in the array*/
109           if($host == $this->hostname)
110           {
111             unset($this->kolabHost[$key]);
112             unset($this->attrs['kolabHost'][$key]);
113           }
114         }
115       /* We deletet an Entry so we must reorder the index of the array */ 
116       $tmp = $this->kolabHost;
117       $this->kolabHost=array();
118       $this->attrs['kolabHost']=array();
119       
120       /* reorder  reorder ...*/
121       foreach($tmp as $host){
122         $this->kolabHost[]=$host;
123         $this->attrs['kolabHost'][]=$host;
124       }
125       /* Tell Save that we want to delete someone*/
126       $this->remove = true;
127       }
129     }
130     /* Check Relayhost and if we have MX lookup enabled*/
131     if((isset($_POST['NewHost'])) || (isset($_POST['DelHost'])))    
132       {
133       $this->postfix_mxrelayenabled = $_POST['RelayMxSupport'] ;
134       }
135     if($this->postfix_relayhost[0]=="[")
136       {
137       $this->postfix_mxrelayenabled=false;
138       $this->postfix_relayhost = str_replace("[","",$this->postfix_relayhost);
139       $this->postfix_relayhost = str_replace("]","",$this->postfix_relayhost);
140       }
141     if($this->postfix_mxrelayenabled)    {
142       $smarty->assign("RelayMxSupportCheck"," checked ");
143       }      else      {
144       $smarty->assign("RelayMxSupportCheck","");
145       }
146     /* Show tab dialog headers */
147     if ($this->is_account) {
148       /* call Add Acoount to add account */
149       $display = $this->show_header(_("Remove Kolab extension"), _("This server has kolab features enabled. You can disable them by clicking below."));
150     } else {
151       /* call remove Account */
152       $display = $this->show_header(_("Add Kolab service"), _("This server has kolab features disabled. You can enable them by clicking below."));
153       return ($display);
154     }
156     /* Initialize all attributes, that were submitted */
157     foreach($this->attributes as $val) 
158     {
159       /* If theres a button pressed, we need to check all attributes */
160       if ((isset($_POST['NewHost'])) || (isset($_POST['DelHost']))) 
161       {
162         /* Set var to value or to zero */
163         if (isset($_POST[$val])) 
164         {
166           $this->$val = $_POST[$val];
167         } else {
168           $this->$val = 0;
169         }
170       }
171       /* Tell smarty which variables we are useing */
172       $smarty->assign($val, $this->$val);
173       if (($this->$val != "FALSE") && (!empty($this->$val)))
174         $smarty->assign($val."Check", "checked");
175       else
176         $smarty->assign($val."Check", "");
177     }
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[] = _("Free/Busy settings -> Future days must be set.");
201     }elseif((($this->kolabFreeBusyFuture=="")<0)){
202       $message[] = _("Free/Busy settings -> Future days must be a positiv value.");
203     }elseif(strcasecmp($this->kolabFreeBusyFuture,(int)$this->kolabFreeBusyFuture)){
204       $message[] = _("Free/Busy settings -> Future days must be a value.)";
205     }
206   
207     if(!is_int((int)($this->cyrus_quotawarn))) {
208       $message[] = _("Cyrus Quota settings -> The given Quota settings value must be a number.");
209     }elseif(!(($this->cyrus_quotawarn<=100)&&($this->cyrus_quotawarn>=0))){
210       $message[] = _("Cyrus Quota settings -> Please choose a value between 1 and 100 for Quota settings.");
211     }elseif(strcasecmp($this->cyrus_quotawarn,(int)$this->cyrus_quotawarn)){
212       $message[] = _("Cyrus Quota settings -> Future days must be a value.)";
213     }
215    if(empty($this->postfix_mynetworks))
216     { 
217       $message[] = _("SMTP privileged networks -> No value is given.");
218     }
219     
220   if(empty($this->postfix_relayhost))
221     {
222       $message[] = _("SMTP smarthost/relayhost -> No value is given.");
223     }
226     return ($message);
227   }
230   /* Save to LDAP */
231   function save() 
232   {
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 ?>