Code

78e3ab87759513b375084aa5c71d2220c61fcb35
[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                 = "";
10   var $postfix_mydestination            = "";
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 $k                                = "kolab";
19   var $cyrus_admins                     = "TRUE";
20   var $cyrus_imap                       = "TRUE";
21   var $cyrus_pop3                       = "TRUE";
22   var $cyrus_imaps                      = "TRUE";
23   var $cyrus_pop3s                      = "TRUE";
24   var $cyrus_sieve                      = "TRUE";
25   var $apache_allow_unauthenticated_fb  = "TRUE";
26   var $proftpd_ftp                      = "TRUE";
27   var $apache_http                      = "TRUE";
28   var $kolabHost                        = array();
29   var $is_in_there                      = false;
30   var $exactName                        = false;
31   var $attributes =  array("postfix_mydomain", "postfix_mydestination", "proftpd_ftp", "k",
32         "postfix_mynetworks", "postfix_enable_virus_scan", "postfix_relayhost", "apache_http",
33         "postfix_allow_unauthenticated", "cyrus_admins", "cyrus_imap","kolabFreeBusyFuture",
34         "cyrus_pop3", "cyrus_imaps", "cyrus_pop3s", "cyrus_sieve", "apache_allow_unauthenticated_fb",
35         "cyrus_quotawarn");
36   var $objectclasses = array("top", "kolab");
38   function servkolab($config, $dn = NULL) 
39   {
40     /* Setting the hostname and tell this Plugin that we are the kolab extension*/
41     $this->hostname = preg_replace('/^cn=([^,]+),.*$/', '\1', $dn);
42     $this->dn       = "k=kolab,".$config->current['BASE'];
43     
44     /* Load variables, if given*/
45     plugin::plugin($config, $this->dn);
47     /* Copy needed attributes */
48     foreach($this->attributes as $val) {
49       $name = preg_replace('/_/', '-', $val);
50       if (isset($this->attrs["$name"][0])) {
51         $this->$val = $this->attrs["$name"][0];
52       }
53     }
54   
55     /* Toggle relayhost */
56     $this->postfix_mxrelayenabled = preg_match('/^\[/', $this->postfix_relayhost);
57     $this->postfix_relayhost      = preg_replace("/[\[\]]/","",$this->postfix_relayhost);
59     /* Is this Server a member of the Kolab extension or not ?*/ 
60     if(isset($this->attrs['kolabHost'])) {
61       $this->kolabHost= $this->attrs['kolabHost'];
62       unset($this->kolabHost['count']);
63     }
65     $this->is_in_there           = false;
66     $this->is_account            = false;
67     $this->initially_was_account = false;
68     foreach($this->kolabHost as $host){
69       if(preg_match("/".$this->hostname.".*/i",$host)){
70         $this->is_account            = true;
71         $this->is_in_there           = true;
72         $this->initially_was_account = true;
73       }
74     } 
75   
76     if(in_array($this->hostname, $this->kolabHost)) {
77       $this->exactName=true;
78     } else {   
79       $this->exactName=false;    
80     } 
81     if(isset($this->attrs['postfix-mynetworks'])){
82       if(is_array($this->attrs['postfix-mynetworks'])){  
83         unset($this->attrs['postfix-mynetworks']['count']);
84         $tmp="";
85         foreach($this->attrs['postfix-mynetworks'] as $tm){
86           $tmp.=$tm.";";
87         }
88         $this->postfix_mynetworks = $tmp;
89       }
90     }else{
91       $this->postfix_mynetworks="";
92     }
93   }
96   function execute() 
97   {
98         /* Call parent execute */
99         plugin::execute();
101     /* Fill templating stuff */
102     $smarty = get_smarty();
103     $display = "";
105     /* The Ldap link is needed to ask ldap some questions */
106     $ldap = $this->config->get_ldap_link();
108     /* Do we need to flip is_account state? */
109     if (isset($_POST['modify_state'])) {
110       $this->is_account = !$this->is_account;
111     }
113     /* Show tab dialog headers */
114     if ($this->is_account) {
115       /* call Add Acoount to add account */
116       $display = $this->show_header(_("Remove Kolab extension"), _("This server has kolab features enabled. You can disable them by clicking below."));
117     } else {
118       /* call remove Account */
119       $display = $this->show_header(_("Add Kolab service"), _("This server has kolab features disabled. You can enable them by clicking below."));
120       return ($display);
121     }
123     /* Set relayhost and if we have MX lookup enabled*/
124     if($this->postfix_mxrelayenabled) {
125       $smarty->assign("RelayMxSupportCheck"," checked ");
126     } else {
127       $smarty->assign("RelayMxSupportCheck","");
128     }
129     
130     /* Initialize all attributes, that were submitted */
131     foreach($this->attributes as $val) 
132     {
133       $smarty->assign($val."ACL",chkacl($this->acl,str_replace("_","-",$val)));
135       /* Tell smarty which variables we are useing */
136       $smarty->assign($val, $this->$val);
137       if (($this->$val != "FALSE") && !empty($this->$val)){
138         $smarty->assign($val."Check", "checked");
139       } else {
140         $smarty->assign($val."Check", "");
141       }
142     }
144     /* Assemble free/busy string */
145     $edit= sprintf('<input name="kolabFreeBusyFuture" value="%s" %s type="text" maxlength="3" size="4">',
146             $this->kolabFreeBusyFuture, chkacl($this->acl, 'kolabFreeBusyFuture'));
147     $fbfuture= sprintf(_("Include data from %s days in the past when creating free/busy lists"), $edit);
148     $smarty->assign("fbfuture", $fbfuture);
150     /* Assemble quota string */
151     $edit= sprintf('<input name="cyrus_quotawarn" value="%s" type="text" maxlength="3" size="4" %s>',
152           $this->cyrus_quotawarn, chkacl($this->acl, 'cyrus_quotawarn'));
153     $quotastr= sprintf(_("Warn users when using more than %s%% of their mail quota"), $edit);
154     $smarty->assign("quotastr", $quotastr);
156     /* Load Template */
157     $display.=$smarty->fetch(get_template_path('servkolab.tpl', TRUE));
158     return ($display);
159   }
161   function remove_from_parent() 
162   {
163     /* Only walk through following code, if this host 
164         was a member of the kolab hosts, else skip this */
165     if(!$this->initially_was_account){
166       return;
167     }
169     $ldap     = $this->config->get_ldap_link();
170     $this->dn = "k=kolab,".$this->config->current['BASE'];
172     
173     /* We can't simply remove the whole entry, it is possible that there are 
174         some other hosts assigned to this object. 
175        So, first of all check if we are the last host entry within the host 
176         : Remove k=kolab entry 
177        if we aren't alone, only remove host name from hosts entry and save */
178     /* Are we alone? Remove complete entry... */
180     /* Check if we are definitly in kolabHosts */
181     if(!in_array_ics($this->cn,$this->kolabHost)) {
182       return;
183     }
185     /* Integration check, not translatet because they can't pop up at all, only for debug */ 
186     if(count($this->kolabHost) == 0){
187       print_red("Server - Kolab tab : This is not possible, we can't remove an account which doesn't exists.");
188       return;
189     }
190     if(!isset($this->cn) || (empty($this->cn))){
191       print_red("The required attribute cn seams to empty.");
192     }
194     /* Our hostname is in kolabHosts and there is a only one entry 
195         = we are the last host entry, delete k=kolab entry  */
196     if (count($this->kolabHost) == 1){
197     
198       /* Remove complete entry */
199       $ldap->rmdir ($this->dn);
200       show_ldap_error($ldap->get_error(), _("Removing kolab host entry failed"));
201       gosa_log("k=kolab entry removed, ".$this->cn." was the last kolabHost entry.");
203     } else {
205       /* Only modify kolabHost */
206       $hosts= array();
207       foreach ($this->kolabHost as $host){
208         if($host != $this->cn){
209           $hosts[]= $host;
210         }
211       }
212       $attrs= array('kolabHost' => $hosts);
213       $ldap->cd($this->dn);
214       $this->cleanup();
215       $ldap->modify ($attrs); 
216       show_ldap_error($ldap->get_error(), _("Removing server from kolab object failed"));
217       gosa_log("Removing ".$this->cn." from list of kolabHosts");
218     }
220     /* Optionally execute a command after we're done */
221     $this->handle_post_events("remove");
222   }
225   function save_object()    
226   {
227     if (isset($_POST['kolabtab'])){
228       plugin::save_object();
230       /* Save checkboxes */
231       foreach (array( "postfix_enable_virus_scan", "postfix_allow_unauthenticated",
232                       "cyrus_admins", "cyrus_imap", "cyrus_pop3", "cyrus_imaps",
233                       "cyrus_pop3s", "cyrus_sieve", "apache_allow_unauthenticated_fb",
234                       "proftpd_ftp", "apache_http") as $cb){
235         if (isset($_POST[$cb])){
236           $this->$cb= "TRUE";
237         } else {
238           $this->$cb= "FALSE";
239         }
240       }
242       /* Toggle relay check */
243       $this->postfix_mxrelayenabled= isset($_POST['RelayMxSupport']);
244     }
246   }
249   function check() 
250   {
251     /* Call common method to give check the hook */
252     $message= plugin::check();
254     if(($this->kolabFreeBusyFuture==""))    {
255       $message[] = _("Future days in Free/Busy settings must be set.");
256     }elseif(!is_uid($this->kolabFreeBusyFuture) || $this->kolabFreeBusyFuture < 0){
257       $message[] = _("Future days in Free/Busy settings must be a positive value.");
258     }
260     if(!is_int((int)($this->cyrus_quotawarn))) {
261       $message[] = _("The given Quota settings value must be a number.");
262     }elseif(!(($this->cyrus_quotawarn<=100)&&($this->cyrus_quotawarn>=0))){
263       $message[] = _("Please choose a value between 1 and 100 for Quota settings.");
264     }elseif(strcasecmp($this->cyrus_quotawarn,(int)$this->cyrus_quotawarn)){
265       $message[] = _("Future days must be a value.");
266     }
268     if(empty($this->postfix_mynetworks)) { 
269       $message[] = _("No SMTP privileged networks set.");
270     }
272     /*if(empty($this->postfix_relayhost)) {
273       $message[] = _("No SMTP smarthost/relayhost set.");
274     }
275     */
277     return ($message);
278   }
281   /* Save to LDAP */
282   function save() 
283   {
284     /* Set ldap connection */
285     $ldap = $this->config->get_ldap_link();
286     
287     /* Open current dn*/
288     $this->dn = "k=kolab,".$this->config->current['BASE'];
289   
290     /* Adapt relayhost */
291     $this->postfix_relayhost= preg_replace('/[\[\]]/', '', $this->postfix_relayhost);
292     if (!$this->postfix_mxrelayenabled && $this->postfix_relayhost != ""){
293       $this->postfix_relayhost= "[".$this->postfix_relayhost."]";
294     }
296     /* Add ourselves to the list of kolabHost's if needed */
297     if ((!$this->is_in_there)){
298       $this->kolabHost[]= $this->cn;
299     }
300     
301  
302   
303     $tmp = split(";",$this->postfix_mynetworks);
304     $this->postfix_mynetworks = array();
305     foreach($tmp as $tm){
306       trim($tm);
307       if(!empty($tm)){
308         $this->postfix_mynetworks[]=$tm;
309       }
310     }
311   
312     $this->attrs['postfix_mynetworks']=$this->postfix_mynetworks;
314     /* Call parents save to prepare $this->attrs */
315     plugin::save();
317     /* Save or modify? */
318     $ldap->cat($this->dn, array('dn'));
319     if (!$ldap->fetch()){
320       $mode= "add"; 
321     } else {
322       $mode= "modify";
323     }
325     /* Do attribute conversion */
326     foreach ($this->attrs as $key => $value){
327       if (preg_match('/_/', $key)){
328         $old_key= $key;
329         $key= preg_replace('/_/', '-', $key);
330         $this->attrs[$key]= $value;
331         unset($this->attrs[$old_key]);
332       }
333     }
335     /* Add kolab hosts */
336     $this->attrs['kolabHost']= $this->kolabHost;
337   
338     if(($this->is_in_there)&&(!$this->exactName)){
339       unset($this->attrs['kolabHost']);
340     }
342     /* Perform LDAP action */
343     $ldap->cd($this->dn);
344     $this->cleanup();;
345     $ldap->$mode($this->attrs);
346     show_ldap_error($ldap->get_error(), _("Saving server to kolab object failed"));
347     
348     /* Optionally execute a command after we're done */
349     if ($this->initially_was_account == $this->is_account) {
350       if ($this->is_modified) {
351         $this->handle_post_events("mofify");
352       }
353     } else {
354       $this->handle_post_events("add");
355     }
357   }
361 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
362 ?>