Code

2a91090d2c035c9f1780a23e89213dd2811cd831
[gosa.git] / gosa-plugins / kolab / admin / systems / services / kolab / class_servKolab.inc
1 <?php
3 class servkolab extends goService 
4 {
5     var $postfix_mydomain                 = "";
6     var $cyrus_admins                     = "";
7     var $postfix_mydestination            = array();
8     var $postfix_mynetworks               = "127.0.0.1/8";
9     var $postfix_enable_virus_scan        = "TRUE";
10     var $postfix_relayhost                = "";
11     var $postfix_mxrelayenabled           =  true;
12     var $postfix_allow_unauthenticated     = "FALSE";
13     var $cyrus_quotawarn                  = "80";
14     var $kolabFreeBusyFuture              = "1";
15     var $k                                = "kolab";
16     var $cyrus_imap                       = "TRUE";
17     var $cyrus_pop3                       = "FALSE";
18     var $cyrus_imaps                      = "TRUE";
19     var $cyrus_pop3s                      = "TRUE";
20     var $cyrus_sieve                      = "TRUE";
21     var $apache_allow_unauthenticated_fb  = "TRUE";
22     var $proftpd_ftp                      = "FALSE";
23     var $apache_http                      = "TRUE";
24     var $kolabHost                        = array();
25     var $orig_cn                          = "";
26     var $view_logged                      = FALSE;
28     var $attributes =  array("postfix_mydomain", "postfix_mydestination", "proftpd_ftp", "k",
29             "postfix_mynetworks", "postfix_enable_virus_scan", "postfix_relayhost", "apache_http",
30             "postfix_allow_unauthenticated", "cyrus_admins", "cyrus_imap","kolabFreeBusyFuture",
31             "cyrus_pop3", "cyrus_imaps", "cyrus_pop3s", "cyrus_sieve", "apache_allow_unauthenticated_fb",
32             "cyrus_quotawarn");
33     var $objectclasses = array("top", "kolab");
35     /* Serverservice vars */
36     var $conflicts    = array("goMailServer");
37     var $DisplayName  = "Kolab mail service";
38     var $StatusFlag   = "";
40     function servkolab(&$config, $dn = NULL, $parent= NULL) 
41     {
42         /* Setting the hostname and tell this Plugin that we are the kolab extension*/
43         $this->hostname = preg_replace('/^cn=([^,]+),.*$/', '\1', $dn);
44         $this->dn       = "k=kolab,".$config->current['BASE'];
46         /* Load variables, if given*/
47         plugin::plugin($config, $this->dn);
49         /* Copy needed attributes */
50         foreach($this->attributes as $val) {
51             $name = preg_replace('/_/', '-', $val);
52             if (isset($this->attrs["$name"][0])) {
53                 $this->$val = $this->attrs["$name"][0];
54             }
55         }
57         /* Toggle relayhost */
58         $this->postfix_mxrelayenabled = preg_match('/^\[/', $this->postfix_relayhost);
59         $this->postfix_relayhost      = preg_replace("/[\[\]]/","",$this->postfix_relayhost);
61         /* Is this Server a member of the Kolab extension or not ?*/ 
62         if(isset($this->attrs['kolabHost'])) {
63             $this->kolabHost= $this->attrs['kolabHost'];
64             unset($this->kolabHost['count']);
65         }
66         $this->is_account            = false;
67         $this->initially_was_account = false;
68         foreach($this->kolabHost as $host){
69             if($this->hostname == $host){
70                 $this->is_account            = true;
71                 $this->initially_was_account = true;
72             }
73         }
75         /* Get list of configured domains 
76          */ 
77         $this->postfix_mydestination = array();
78         if(isset($this->attrs['postfix-mydestination'])){
79             for($i=0; $i < $this->attrs['postfix-mydestination']['count']; $i++){
80                 $this->postfix_mydestination[] = $this->attrs['postfix-mydestination'][$i];
81             }
82         }
84         // Prepare lists
85         $this->postfix_mydestinationList = new sortableListing();
86         $this->postfix_mydestinationList->setDeleteable(true);
87         $this->postfix_mydestinationList->setInstantDelete(true);
88         $this->postfix_mydestinationList->setEditable(false);
89         $this->postfix_mydestinationList->setWidth("100%");
90         $this->postfix_mydestinationList->setHeight("100px");
91         $this->postfix_mydestinationList->setColspecs(array('*','20px'));
92         $this->postfix_mydestinationList->setHeader(array(_("Name")));
93         $this->postfix_mydestinationList->setDefaultSortColumn(0);
94     }
97     function execute() 
98     {
99         /* Call parent execute */
100         plugin::execute();
102         if($this->is_account && !$this->view_logged){
103             $this->view_logged = TRUE;
104             new log("view","server/".get_class($this),$this->dn);
105         }
107         /***************  
108           Variable initialisation
109          ***************/  
111         $smarty     = get_smarty();
112         $display    = "";
113         $ldap       = $this->config->get_ldap_link();
116         /* Assemble free/busy string */
117         $edit       = sprintf('<input name="kolabFreeBusyFuture" value="%s" type="text" maxlength="3" size="4">',
118                 $this->kolabFreeBusyFuture);
119         $fbfuture   = sprintf(_("Include data from %s days in the past when creating free/busy lists"), $edit);
121         /* Assemble quota string */
122         $edit       = sprintf('<input name="cyrus_quotawarn" value="%s" type="text" maxlength="3" size="4">',
123                 $this->cyrus_quotawarn);
124         $quotastr   = sprintf(_("Warn users when using more than %s%% of their mail quota"), $edit);
126         /* Domain name added/removed 
127          */
128         if($this->acl_is_writeable("postfixmydestination")){
129             $this->postfix_mydestinationList->save_object();
130             $action = $this->postfix_mydestinationList->getAction();
131             if($action['action'] == 'delete'){
132                 $this->postfix_mydestination = $this->postfix_mydestinationList->getMaintainedData();
133             }
134             if(isset($_POST['add_domain_name']) && isset($_POST['new_domain_name'])){
135                 $new_domain = trim(get_post('new_domain_name'));
136                 if(!empty($new_domain)){
137                     if(tests::is_domain($new_domain)){
138                         $this->postfix_mydestination[] = $new_domain;
139                         $this->postfix_mydestination = array_unique($this->postfix_mydestination);
140                     }else{
141                         msg_dialog::display(_("Info"), msgPool::invalid(_("Mail domain")));
142                     }
143                 }
144             }
145         }
147         /***************
148           Assign informations to smarty 
149          ***************/
151         /* Set relayhost and if we have MX lookup enabled*/ 
152         if($this->postfix_mxrelayenabled) {
153             $smarty->assign("RelayMxSupportCheck"," checked ");
154         } else {
155             $smarty->assign("RelayMxSupportCheck","");
156         }
158         /* Set acls */
159         $tmp = $this->plInfo();
160         foreach($tmp['plProvidedAcls'] as $name => $translation){
161             $smarty->assign($name."ACL",$this->getacl($name));
162         }
164         /* Initialize all attributes, that were submitted */
165         foreach($this->attributes as $val) 
166         {
167             /* Tell smarty which variables we are useing */
168             $smarty->assign($val, set_post($this->$val));
169             if (($this->$val != "FALSE") && !empty($this->$val)){
170                 $smarty->assign($val."Check", "checked");
171             } else {
172                 $smarty->assign($val."Check", "");
173             }
174         }
176         $smarty->assign("fbfuture",($fbfuture));
177         $smarty->assign("quotastr",($quotastr));
179         $this->postfix_mydestinationList->setAcl($this->getacl("postfixmydestination"));
180         $this->postfix_mydestinationList->setListData($this->postfix_mydestination);
181         $this->postfix_mydestinationList->update();
182         $smarty->assign("mdDiv",$this->postfix_mydestinationList->render());
184         /* Load Template */
185         $display.=$smarty->fetch(get_template_path('servkolab.tpl', TRUE,dirname(__FILE__)));
186         return ($display);
187     }
189     function remove_from_parent() 
190     {
192         /* Remove status flag, it is not a memeber of
193            this->attributes, so ensure that it is deleted too */
194         if(!empty($this->StatusFlag)){
195             $this->attrs[$this->StatusFlag] = array();
196         }
199         /* Only walk through following code, if this host 
200            was a member of the kolab hosts, else skip this */
201         if(!$this->initially_was_account){
202             return;
203         }
205         /* !!! Don't use "cn" in this function 
206            hostname -> the initial name of the host
207            cn       -> is the new name of the host, in case that it was renamed.
208          */
210         $ldap     = $this->config->get_ldap_link();
211         $this->dn = "k=kolab,".$this->config->current['BASE'];
213         /* We can't simply remove the whole entry, it is possible that there are 
214            some other hosts assigned to this object. 
215            So, first of all check if we are the last host entry within the host 
216            : Remove k=kolab entry 
217            if we aren't alone, only remove host name from hosts entry and save */
218         /* Are we alone? Remove complete entry... */
220         /* Check if we are definitly in kolabHosts */
221         if(!in_array_ics($this->hostname,$this->kolabHost)) {
222             return;
223         }
225         /* Integration check, not translated because they can't pop up at all, only for debug */ 
226         if(count($this->kolabHost) == 0){
227             msg_dialog::display(_("Internal error"), _("Kolab account does not exist!"), ERROR_DIALOG);
228             return;
229         }
230         if(!isset($this->hostname) || (empty($this->hostname))){
231             msg_dialog::display(_("Error"), msgPool::required(_("host name")), ERROR_DIALOG);
232         }
234         /* Our hostname is in kolabHosts and there is a only one entry 
235            = we are the last host entry, delete k=kolab entry  */
236         if (count($this->kolabHost) == 1){
238             /* Remove complete entry */
239             $ldap->rmdir ($this->dn);
240             if (!$ldap->success()){
241                 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
242             }
243         } else {
245             /* Only modify kolabHost */
246             $hosts= array();
247             foreach ($this->kolabHost as $host){
248                 if($host != $this->hostname){
249                     $hosts[]= $host;
250                 }
251             }
252             $attrs= array('kolabHost' => $hosts);
253             $ldap->cd($this->dn);
254             $this->cleanup();
255             $ldap->modify ($attrs); 
256             if (!$ldap->success()){
257                 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
258             }
259         }
261         new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
263         /* Optionally execute a command after we're done */
264         $this->handle_post_events("remove");
265     }
268     function save_object()    
269     {
270         if (isset($_POST['kolabtab'])){
271             @plugin::save_object();
274             /* Map attrinutes 
275              */
276             foreach($this->attributes as $attr){
277                 if(isset($_POST[$attr]) && ($this->acl_is_writeable(preg_replace("/_/","",$attr)))){
278                     $this->$attr = get_post($attr);
279                 }
280             }
282             /* Save checkboxes */
283             foreach (array( "postfix_enable_virus_scan", "postfix_allow_unauthenticated",
284                         "cyrus_imap", "cyrus_pop3", "cyrus_imaps",
285                         "cyrus_pop3s", "cyrus_sieve", "apache_allow_unauthenticated_fb",
286                         "proftpd_ftp", "apache_http") as $cb){
288                 if($this->acl_is_writeable(preg_replace("/_/","",$cb))){
289                     if (isset($_POST[$cb])){
290                         $this->$cb= "TRUE";
291                     } else {
292                         $this->$cb= "FALSE";
293                     }
294                 }
295             }
297             /* Toggell relay check */
298             if($this->acl_is_writeable("postfixmxrelayenabled")){
299                 $this->postfix_mxrelayenabled= isset($_POST['RelayMxSupport']);
300             }
301         }
303     }
306     function check() 
307     {
308         /* Call common method to give check the hook */
309         $message= plugin::check();
311         if(mailMethod::get_current_method($this->config) != "mailMethodKolab22" && count($this->postfix_mydestination) != 1){
312 #      $message[] = _("Only kolab version >= 2.2 is able to handle multiple mail domains. Please provide only one mail domain.");
313         }
315         if(($this->kolabFreeBusyFuture==""))    {
316             $message[] = msgPool::required(_("Future days"));
317         }elseif(!tests::is_uid($this->kolabFreeBusyFuture) || $this->kolabFreeBusyFuture < 0){
318             $message[] = msgPool::invalid(_("Future days"),$this->kolabFreeBusyFuture,"/[0-9]/");
319         }
321         if(!is_int((int)($this->cyrus_quotawarn))) {
322             $message[] = msgPool::required(_("Quota"));
323         }elseif($this->cyrus_quotawarn>100){
324             $message[] = _("Please choose a value between 1 and 100 for Quota settings.");
325         }elseif($this->cyrus_quotawarn < 1){
326             $message[] = msgPool::toosmall(_("Quota"),1);
327         }
329         if(empty($this->postfix_mynetworks)) { 
330             $message[] = msgPool::required(_("Privileged networks"));
331         }
333         if(!count($this->postfix_mydestination)){
334             $message[] = msgPool::required(_("Mail domains")." (postfix-mydestination)" );
335         }
337         return ($message);
338     }
341     /* Save to LDAP */
342     function save() 
343     {
344         /* Set ldap connection */
345         $ldap       = $this->config->get_ldap_link();
347         /* Open current dn*/
348         $this->dn   = "k=kolab,".$this->config->current['BASE'];
350         /* Adapt relayhost */
351         $this->postfix_relayhost= preg_replace('/[\[\]]/', '', $this->postfix_relayhost);
352         if ($this->postfix_mxrelayenabled && $this->postfix_relayhost != ""){
353             $this->postfix_relayhost= "[".$this->postfix_relayhost."]";
354         }
356         /* Check if this server was renamed, in this case we have to remove old cn first*/
357         if($this->hostname != $this->cn){
358             $tmp = array();
359             if(in_array_ics($this->hostname,$this->kolabHost)){
360                 foreach($this->kolabHost as $host){
361                     if($host != $this->hostname){
362                         $tmp[] = $host;
363                     }
364                 }
365                 $this->kolabHost = $tmp;
366             }
367         }
369         /* Add ourselves to the list of kolabHost's if needed */
370         if (!in_array_ics($this->cn,$this->kolabHost)){
371             $this->kolabHost[]= $this->cn;
372         }
374         /* Call parents save to prepare $this->attrs */
375         plugin::save();
377         /* Save or modify? */
378         $ldap->cat($this->dn, array('dn'));
379         if (!$ldap->fetch()){
380             $mode= "add"; 
381         } else {
382             $mode= "modify";
383         }
385         /* Do attribute conversion */
386         foreach ($this->attrs as $key => $value){
387             if (preg_match('/_/', $key)){
388                 $old_key= $key;
389                 $key= preg_replace('/_/', '-', $key);
390                 $this->attrs[$key]= $value;
391                 unset($this->attrs[$old_key]);
392             }
393         }
394         /* Add kolab hosts */
395         $this->attrs['kolabHost']= $this->kolabHost;
397         /* Perform LDAP action */
398         $ldap->cd($this->dn);
399         $this->cleanup();;
400         $ldap->$mode($this->attrs);
402         if($mode == "add"){
403             new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
404         }else{
405             new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
406         }
408         if (!$ldap->success()){
409             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
410         }
412         /* Optionally execute a command after we're done */
413         if ($this->initially_was_account == $this->is_account) {
414             if ($this->is_modified) {
415                 $this->handle_post_events("modify");
416             }
417         } else {
418             $this->handle_post_events("add");
419         }
420     }
423     function getListEntry()
424     {
425         $fields                 = goService::getListEntry();
426         $fields['Message']      = _("Kolab mail service");
427 #$fields['AllowEdit']    = true;
428         return($fields);
429     }
432     /* Return plugin informations for acl handling */
433     static function plInfo()
434     {
435         return (array(
436                     "plShortName"   => _("Kolab"),
437                     "plDescription" => _("Kolab mail service")." ("._("Services").")",
438                     "plSelfModify"  => FALSE,
439                     "plDepends"     => array(),
440                     "plPriority"    => 85,
441                     "plSection"     => array("administration"),
442                     "plCategory"    => array("server"),
444                     "plProvidedAcls"=> array(
445                         "postfixmydomain"                => _("Postfix mydomain") ,
446                         "postfixmydestination"           => _("My destination") ,
447                         "cyrusadmins"                    => _("Cyrus administrators") ,
449                         "cyruspop3"                      => _("POP3 service") ,
450                         "cyruspop3s"                     => _("POP3/SSL service") ,
452                         "cyrusimap"                      => _("IMAP service") ,
453                         "cyrusimaps"                     => _("IMAP/SSL service") ,
455                         "cyrussieve"                     => _("Sieve service") ,
456                         "proftpdftp"                     => _("FTP FreeBusy service") ,
457                         "apachehttp"                     => _("HTTP FreeBusy service") ,
458                         "postfixenablevirusscan"         => _("Enable virus scan") ,
460                         "cyrusquotawarn"                 => _("Quota settings"), 
462                         "apacheallowunauthenticatedfb"   => _("Allow unauthenticated free busy"),
463                         "kolabFreeBusyFuture"            => _("kolabFreeBusyFuture") ,
465                         "postfixmynetworks"              => _("SMTP privileged networks") ,
466                         "postfixmxrelayenabled"          => _("Enable MX lookup for relay host"),
467                         "postfixrelayhost"               => _("Relay host") ,
468                         "postfixallowunauthenticated"    => _("Accept Internet Mail"))
469                             ));
470     }
473 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
474 ?>