Code

Added conflict with kolab
[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);
108     /***************  
109       Handle Account is_account state
110      ***************/  
111   
112     /* Do we need to flip is_account state? */
113     if (isset($_POST['modify_state'])) {
114       $this->is_account = !$this->is_account;
115     }
117     /* Show tab dialog headers */
118     if ($this->is_account) {
119       /* call Add Acoount to add account */
120       $display = $this->show_header(_("Remove Kolab extension"), _("This server has kolab features enabled. You can disable them by clicking below."));
121     } else {
122       /* call remove Account */
123       $display = $this->show_header(_("Add Kolab service"), _("This server has kolab features disabled. You can enable them by clicking below."));
124       return ($display);
125     }
128     /***************
129       Assign informations to smarty 
130      ***************/
132     /* Set relayhost and if we have MX lookup enabled*/
133     if($this->postfix_mxrelayenabled) {
134       $smarty->assign("RelayMxSupportCheck"," checked ");
135     } else {
136       $smarty->assign("RelayMxSupportCheck","");
137     }
139     /* Initialize all attributes, that were submitted */
140     foreach($this->attributes as $val) 
141     {
142       $smarty->assign($val."ACL",chkacl($this->acl,str_replace("_","-",$val)));
144       /* Tell smarty which variables we are useing */
145       $smarty->assign($val, $this->$val);
146       if (($this->$val != "FALSE") && !empty($this->$val)){
147         $smarty->assign($val."Check", "checked");
148       } else {
149         $smarty->assign($val."Check", "");
150       }
151     }
153     $smarty->assign("fbfuture", $fbfuture);
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     /* !!! Don't use "cn" in this function 
170        hostname -> the initial name of the host
171        cn       -> is the new name of the host, in case that it was renamed.
172      */
174     $ldap     = $this->config->get_ldap_link();
175     $this->dn = "k=kolab,".$this->config->current['BASE'];
177     /* We can't simply remove the whole entry, it is possible that there are 
178        some other hosts assigned to this object. 
179        So, first of all check if we are the last host entry within the host 
180        : Remove k=kolab entry 
181        if we aren't alone, only remove host name from hosts entry and save */
182     /* Are we alone? Remove complete entry... */
184     /* Check if we are definitly in kolabHosts */
185     if(!in_array_ics($this->hostname,$this->kolabHost)) {
186       return;
187     }
189     /* Integration check, not translatet because they can't pop up at all, only for debug */ 
190     if(count($this->kolabHost) == 0){
191       print_red("Server - Kolab tab : This is not possible, we can't remove an account which doesn't exists.");
192       return;
193     }
194     if(!isset($this->hostname) || (empty($this->hostname))){
195       print_red("The required attribute hostname seams to empty.");
196     }
198     /* Our hostname is in kolabHosts and there is a only one entry 
199        = we are the last host entry, delete k=kolab entry  */
200     if (count($this->kolabHost) == 1){
202       /* Remove complete entry */
203       $ldap->rmdir ($this->dn);
204       show_ldap_error($ldap->get_error(), _("Removing kolab host entry failed"));
205       gosa_log("k=kolab entry removed, ".$this->hostname." was the last kolabHost entry.");
207     } else {
209       /* Only modify kolabHost */
210       $hosts= array();
211       foreach ($this->kolabHost as $host){
212         if($host != $this->hostname){
213           $hosts[]= $host;
214         }
215       }
216       $attrs= array('kolabHost' => $hosts);
217       $ldap->cd($this->dn);
218       $this->cleanup();
219       $ldap->modify ($attrs); 
220       show_ldap_error($ldap->get_error(), _("Removing server from kolab object failed"));
221       gosa_log("Removing ".$this->hostname." from list of kolabHosts");
222     }
224     /* Optionally execute a command after we're done */
225     $this->handle_post_events("remove");
226   }
229   function save_object()    
230   {
231     if (isset($_POST['kolabtab'])){
232       plugin::save_object();
234       /* Save checkboxes */
235       foreach (array( "postfix_enable_virus_scan", "postfix_allow_unauthenticated",
236             "cyrus_admins", "cyrus_imap", "cyrus_pop3", "cyrus_imaps",
237             "cyrus_pop3s", "cyrus_sieve", "apache_allow_unauthenticated_fb",
238             "proftpd_ftp", "apache_http") as $cb){
239         if (isset($_POST[$cb])){
240           $this->$cb= "TRUE";
241         } else {
242           $this->$cb= "FALSE";
243         }
244       }
246       /* Toggle relay check */
247       $this->postfix_mxrelayenabled= isset($_POST['RelayMxSupport']);
248     }
250   }
253   function check() 
254   {
255     /* Call common method to give check the hook */
256     $message= plugin::check();
258     if(($this->kolabFreeBusyFuture==""))    {
259       $message[] = _("Future days in Free/Busy settings must be set.");
260     }elseif(!is_uid($this->kolabFreeBusyFuture) || $this->kolabFreeBusyFuture < 0){
261       $message[] = _("Future days in Free/Busy settings must be a positive value.");
262     }
264     if(!is_int((int)($this->cyrus_quotawarn))) {
265       $message[] = _("The given Quota settings value must be a number.");
266     }elseif(!(($this->cyrus_quotawarn<=100)&&($this->cyrus_quotawarn>=0))){
267       $message[] = _("Please choose a value between 1 and 100 for Quota settings.");
268     }elseif(strcasecmp($this->cyrus_quotawarn,(int)$this->cyrus_quotawarn)){
269       $message[] = _("Future days must be a value.");
270     }
272     if(empty($this->postfix_mynetworks)) { 
273       $message[] = _("No SMTP privileged networks set.");
274     }
276     /*if(empty($this->postfix_relayhost)) {
277       $message[] = _("No SMTP smarthost/relayhost set.");
278       }
279      */
281     return ($message);
282   }
285   /* Save to LDAP */
286   function save() 
287   {
288     /* Set ldap connection */
289     $ldap       = $this->config->get_ldap_link();
291     /* Open current dn*/
292     $this->dn   = "k=kolab,".$this->config->current['BASE'];
294     /* Adapt relayhost */
295     $this->postfix_relayhost= preg_replace('/[\[\]]/', '', $this->postfix_relayhost);
296     if (!$this->postfix_mxrelayenabled && $this->postfix_relayhost != ""){
297       $this->postfix_relayhost= "[".$this->postfix_relayhost."]";
298     }
300     /* Check if this server was renamed, in this case we have to remove old cn first*/
301     if($this->hostname != $this->cn){
302       $tmp = array();
303       if(in_array_ics($this->hostname,$this->kolabHost)){
304         foreach($this->kolabHost as $host){
305           if($host != $this->hostname){
306             $tmp[] = $host;
307           }
308         }
309         $this->kolabHost = $tmp;
310       }
311     }
313     /* Add ourselves to the list of kolabHost's if needed */
314     if (!in_array_ics($this->cn,$this->kolabHost)){
315       $this->kolabHost[]= $this->cn;
316     }
318     /* Call parents save to prepare $this->attrs */
319     plugin::save();
321     /* Save or modify? */
322     $ldap->cat($this->dn, array('dn'));
323     if (!$ldap->fetch()){
324       $mode= "add"; 
325     } else {
326       $mode= "modify";
327     }
329     /* Do attribute conversion */
330     foreach ($this->attrs as $key => $value){
331       if (preg_match('/_/', $key)){
332         $old_key= $key;
333         $key= preg_replace('/_/', '-', $key);
334         $this->attrs[$key]= $value;
335         unset($this->attrs[$old_key]);
336       }
337     }
338     /* Add kolab hosts */
339     $this->attrs['kolabHost']= $this->kolabHost;
341     /* Perform LDAP action */
342     $ldap->cd($this->dn);
343     $this->cleanup();;
344     $ldap->$mode($this->attrs);
345     show_ldap_error($ldap->get_error(), _("Saving server to kolab object failed"));
347     /* Optionally execute a command after we're done */
348     if ($this->initially_was_account == $this->is_account) {
349       if ($this->is_modified) {
350         $this->handle_post_events("mofify");
351       }
352     } else {
353       $this->handle_post_events("add");
354     }
355   }
358   function getListEntry()
359   {
360     $flag = $this->StatusFlag;
361     if(empty($flag)){
362       $fields['Status']       = "";
363     }else{
364       $fields['Status']       = $this->$flag;
365     }
366     $fields['Message']      = _("Kolab mail service");
367     $fields['AllowStart']   = true;
368     $fields['AllowStop']    = true;
369     $fields['AllowRestart'] = true;
370     $fields['AllowRemove']  = true;
371     $fields['AllowEdit']    = true;
372     return($fields);
373   }
375     /* Directly save new status flag */
376   function setStatus($value)
377   { 
378     echo "Status flag isn't set .... ";
379     return;
380   
381     if($value == "none") return;
382     if(!$this->initially_was_account) return;
383     $ldap = $this->config->get_ldap_link();
384     $ldap->cd($this->dn);
385     $ldap->cat($this->dn,array("objectClass"));
386     if($ldap->count()){
388       $tmp = $ldap->fetch();
389       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
390         $attrs['objectClass'][] = $tmp['objectClass'][$i];
391       }
392       $flag = $this->StatusFlag;
393       $attrs[$flag] = $value;
394       $this->$flag = $value;
395       $ldap->modify($attrs);
396       show_ldap_error($ldap->get_error());
397       $this->action_hook();
398     }
399   }
403 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
404 ?>