Code

Some functions were missing.
[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 $orig_cn                          = "";
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   /* Serverservice vars */
39   var $conflicts    = array("goImapServer","goMailServer");
40   var $DisplayName  = "Kolab mail service";
41   var $StatusFlag   = "";
43   function servkolab($config, $dn = NULL) 
44   {
45     /* Setting the hostname and tell this Plugin that we are the kolab extension*/
46     $this->hostname = preg_replace('/^cn=([^,]+),.*$/', '\1', $dn);
47     $this->dn       = "k=kolab,".$config->current['BASE'];
49     /* Load variables, if given*/
50     plugin::plugin($config, $this->dn);
52     /* Copy needed attributes */
53     foreach($this->attributes as $val) {
54       $name = preg_replace('/_/', '-', $val);
55       if (isset($this->attrs["$name"][0])) {
56         $this->$val = $this->attrs["$name"][0];
57       }
58     }
60     /* Toggle relayhost */
61     $this->postfix_mxrelayenabled = preg_match('/^\[/', $this->postfix_relayhost);
62     $this->postfix_relayhost      = preg_replace("/[\[\]]/","",$this->postfix_relayhost);
64     /* Is this Server a member of the Kolab extension or not ?*/ 
65     if(isset($this->attrs['kolabHost'])) {
66       $this->kolabHost= $this->attrs['kolabHost'];
67       unset($this->kolabHost['count']);
68     }
69     $this->is_account            = false;
70     $this->initially_was_account = false;
71     foreach($this->kolabHost as $host){
72       if($this->hostname == $host){
73         $this->is_account            = true;
74         $this->initially_was_account = true;
75       }
76     } 
78   }
81   function execute() 
82   {
83     /* Call parent execute */
84     plugin::execute();
86     /***************  
87       Variable initialisation
88      ***************/  
89     
90     $smarty     = get_smarty();
91     $display    = "";
92     $ldap       = $this->config->get_ldap_link();
95     /* Assemble free/busy string */
96     $edit       = sprintf('<input name="kolabFreeBusyFuture" value="%s" %s type="text" maxlength="3" size="4">',
97                     $this->kolabFreeBusyFuture, 
98                     chkacl($this->acl, 'kolabFreeBusyFuture'));
99     $fbfuture   = sprintf(_("Include data from %s days in the past when creating free/busy lists"), $edit);
101     /* Assemble quota string */
102     $edit       = sprintf('<input name="cyrus_quotawarn" value="%s" type="text" maxlength="3" size="4" %s>',
103                     $this->cyrus_quotawarn, 
104                     chkacl($this->acl, 'cyrus_quotawarn'));
105     $quotastr   = sprintf(_("Warn users when using more than %s%% of their mail quota"), $edit);
107     /***************
108       Assign informations to smarty 
109      ***************/
111     /* Set relayhost and if we have MX lookup enabled*/
112     if($this->postfix_mxrelayenabled) {
113       $smarty->assign("RelayMxSupportCheck"," checked ");
114     } else {
115       $smarty->assign("RelayMxSupportCheck","");
116     }
118     /* Initialize all attributes, that were submitted */
119     foreach($this->attributes as $val) 
120     {
121       $smarty->assign($val."ACL",chkacl($this->acl,str_replace("_","-",$val)));
123       /* Tell smarty which variables we are useing */
124       $smarty->assign($val, $this->$val);
125       if (($this->$val != "FALSE") && !empty($this->$val)){
126         $smarty->assign($val."Check", "checked");
127       } else {
128         $smarty->assign($val."Check", "");
129       }
130     }
132     $smarty->assign("fbfuture", $fbfuture);
133     $smarty->assign("quotastr", $quotastr);
135     /* Load Template */
136     $display.=$smarty->fetch(get_template_path('servkolab.tpl', TRUE));
137     return ($display);
138   }
140   function remove_from_parent() 
141   {
143      /* Remove status flag, it is not a memeber of
144         this->attributes, so ensure that it is deleted too */
145     if(!empty($this->StatusFlag)){
146       $this->attrs[$this->StatusFlag] = array();
147     }
150     /* Only walk through following code, if this host 
151        was a member of the kolab hosts, else skip this */
152     if(!$this->initially_was_account){
153       return;
154     }
156     /* !!! Don't use "cn" in this function 
157        hostname -> the initial name of the host
158        cn       -> is the new name of the host, in case that it was renamed.
159      */
161     $ldap     = $this->config->get_ldap_link();
162     $this->dn = "k=kolab,".$this->config->current['BASE'];
164     /* We can't simply remove the whole entry, it is possible that there are 
165        some other hosts assigned to this object. 
166        So, first of all check if we are the last host entry within the host 
167        : Remove k=kolab entry 
168        if we aren't alone, only remove host name from hosts entry and save */
169     /* Are we alone? Remove complete entry... */
171     /* Check if we are definitly in kolabHosts */
172     if(!in_array_ics($this->hostname,$this->kolabHost)) {
173       return;
174     }
176     /* Integration check, not translatet because they can't pop up at all, only for debug */ 
177     if(count($this->kolabHost) == 0){
178       print_red("Server - Kolab tab : This is not possible, we can't remove an account which doesn't exists.");
179       return;
180     }
181     if(!isset($this->hostname) || (empty($this->hostname))){
182       print_red("The required attribute hostname seams to empty.");
183     }
185     /* Our hostname is in kolabHosts and there is a only one entry 
186        = we are the last host entry, delete k=kolab entry  */
187     if (count($this->kolabHost) == 1){
189       /* Remove complete entry */
190       $ldap->rmdir ($this->dn);
191       show_ldap_error($ldap->get_error(), sprintf(_("Removing host entry from system server/kolab with dn '%s' failed."),$this->dn));
192       gosa_log("k=kolab entry removed, ".$this->hostname." was the last kolabHost entry.");
194     } else {
196       /* Only modify kolabHost */
197       $hosts= array();
198       foreach ($this->kolabHost as $host){
199         if($host != $this->hostname){
200           $hosts[]= $host;
201         }
202       }
203       $attrs= array('kolabHost' => $hosts);
204       $ldap->cd($this->dn);
205       $this->cleanup();
206       $ldap->modify ($attrs); 
207       show_ldap_error($ldap->get_error(), sprintf(_("Removing host entry from system server/kolab with dn '%s' failed."),$this->dn));
208       gosa_log("Removing ".$this->hostname." from list of kolabHosts");
209     }
211     /* Optionally execute a command after we're done */
212     $this->handle_post_events("remove");
213   }
216   function save_object()    
217   {
218     if (isset($_POST['kolabtab'])){
219       plugin::save_object();
221       /* Save checkboxes */
222       foreach (array( "postfix_enable_virus_scan", "postfix_allow_unauthenticated",
223             "cyrus_admins", "cyrus_imap", "cyrus_pop3", "cyrus_imaps",
224             "cyrus_pop3s", "cyrus_sieve", "apache_allow_unauthenticated_fb",
225             "proftpd_ftp", "apache_http") as $cb){
226         if (isset($_POST[$cb])){
227           $this->$cb= "TRUE";
228         } else {
229           $this->$cb= "FALSE";
230         }
231       }
233       /* Toggle relay check */
234       $this->postfix_mxrelayenabled= isset($_POST['RelayMxSupport']);
235     }
237   }
240   function check() 
241   {
242     /* Call common method to give check the hook */
243     $message= plugin::check();
245     if(($this->kolabFreeBusyFuture==""))    {
246       $message[] = _("Future days in Free/Busy settings must be set.");
247     }elseif(!is_uid($this->kolabFreeBusyFuture) || $this->kolabFreeBusyFuture < 0){
248       $message[] = _("Future days in Free/Busy settings must be a positive value.");
249     }
251     if(!is_int((int)($this->cyrus_quotawarn))) {
252       $message[] = _("The given Quota settings value must be a number.");
253     }elseif(!(($this->cyrus_quotawarn<=100)&&($this->cyrus_quotawarn>=0))){
254       $message[] = _("Please choose a value between 1 and 100 for Quota settings.");
255     }elseif(strcasecmp($this->cyrus_quotawarn,(int)$this->cyrus_quotawarn)){
256       $message[] = _("Future days must be a value.");
257     }
259     if(empty($this->postfix_mynetworks)) { 
260       $message[] = _("No SMTP privileged networks set.");
261     }
263     /*if(empty($this->postfix_relayhost)) {
264       $message[] = _("No SMTP smarthost/relayhost set.");
265       }
266      */
268     return ($message);
269   }
272   /* Save to LDAP */
273   function save() 
274   {
275     /* Set ldap connection */
276     $ldap       = $this->config->get_ldap_link();
278     /* Open current dn*/
279     $this->dn   = "k=kolab,".$this->config->current['BASE'];
281     /* Adapt relayhost */
282     $this->postfix_relayhost= preg_replace('/[\[\]]/', '', $this->postfix_relayhost);
283     if (!$this->postfix_mxrelayenabled && $this->postfix_relayhost != ""){
284       $this->postfix_relayhost= "[".$this->postfix_relayhost."]";
285     }
287     /* Check if this server was renamed, in this case we have to remove old cn first*/
288     if($this->hostname != $this->cn){
289       $tmp = array();
290       if(in_array_ics($this->hostname,$this->kolabHost)){
291         foreach($this->kolabHost as $host){
292           if($host != $this->hostname){
293             $tmp[] = $host;
294           }
295         }
296         $this->kolabHost = $tmp;
297       }
298     }
300     /* Add ourselves to the list of kolabHost's if needed */
301     if (!in_array_ics($this->cn,$this->kolabHost)){
302       $this->kolabHost[]= $this->cn;
303     }
305     /* Call parents save to prepare $this->attrs */
306     plugin::save();
308     /* Save or modify? */
309     $ldap->cat($this->dn, array('dn'));
310     if (!$ldap->fetch()){
311       $mode= "add"; 
312     } else {
313       $mode= "modify";
314     }
316     /* Do attribute conversion */
317     foreach ($this->attrs as $key => $value){
318       if (preg_match('/_/', $key)){
319         $old_key= $key;
320         $key= preg_replace('/_/', '-', $key);
321         $this->attrs[$key]= $value;
322         unset($this->attrs[$old_key]);
323       }
324     }
325     /* Add kolab hosts */
326     $this->attrs['kolabHost']= $this->kolabHost;
328     /* Perform LDAP action */
329     $ldap->cd($this->dn);
330     $this->cleanup();;
331     $ldap->$mode($this->attrs);
332     show_ldap_error($ldap->get_error(), sprintf(_("Saving system server/kolab with dn '%s' failed."),$this->dn));
334     /* Optionally execute a command after we're done */
335     if ($this->initially_was_account == $this->is_account) {
336       if ($this->is_modified) {
337         $this->handle_post_events("mofify");
338       }
339     } else {
340       $this->handle_post_events("add");
341     }
342   }
345   function getListEntry()
346   {
347     $this->updateStatusState();
348     $flag = $this->StatusFlag;
349     if(empty($flag)){
350       $fields['Status']       = "";
351     }else{
352       $fields['Status']       = $this->$flag;
353     }
354     $fields['Message']      = _("Kolab mail service");
355     $fields['AllowStart']   = true;
356     $fields['AllowStop']    = true;
357     $fields['AllowRestart'] = true;
358     $fields['AllowRemove']  = true;
359     $fields['AllowEdit']    = true;
360     return($fields);
361   }
363     /* Directly save new status flag */
364   function setStatus($value)
365   { 
366     return;
367   
368     if($value == "none") return;
369     if(!$this->initially_was_account) return;
370     $ldap = $this->config->get_ldap_link();
371     $ldap->cd($this->dn);
372     $ldap->cat($this->dn,array("objectClass"));
373     if($ldap->count()){
375       $tmp = $ldap->fetch();
376       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
377         $attrs['objectClass'][] = $tmp['objectClass'][$i];
378       }
379       $flag = $this->StatusFlag;
380       $attrs[$flag] = $value;
381       $this->$flag = $value;
382       $ldap->modify($attrs);
383       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/kolab with dn '%s' failed."),$this->dn)); 
384       $this->action_hook();
385     }
386   }
389   /* Get updates for status flag */
390   function updateStatusState()
391   {
392     if(empty($this->StatusFlag)) return;
394     $attrs = array();
395     $flag = $this->StatusFlag;
396     $ldap = $this->config->get_ldap_link();
397     $ldap->cd($this->cn);
398     $ldap->cat($this->dn,array($flag));
399     if($ldap->count()){
400       $attrs = $ldap->fetch();
401     }
402     if(isset($attrs[$flag][0])){
403       $this->$flag = $attrs[$flag][0];
404     }
405   }
408 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
409 ?>