Code

8c819844268b3bfe87aa82adcc014dd9f883358b
[gosa.git] / plugins / admin / systems / class_servKolab.inc
1 <?php
3 require_once("class_goService.inc");
5 class servkolab extends goService {
6   /* CLI vars */
7   var $cli_summary = "Manage server basic objects";
8   var $cli_description = "Some longer text\nfor help";
9   var $cli_parameters = array("eins"=>"Eins ist toll", "zwei"=>"Zwei ist noch besser");
11   var $postfix_mydomain                 = "";
12   var $cyrus_admins                     = "";
13   var $postfix_mydestination            = "";
14   var $postfix_mynetworks               = "127.0.0.1/8";
15   var $postfix_enable_virus_scan        = "TRUE";
16   var $postfix_relayhost                = "";
17   var $postfix_mxrelayenabled           =  true;
18   var $postfix_allow_unauthenticated     = "FALSE";
19   var $cyrus_quotawarn                  = "80";
20   var $kolabFreeBusyFuture              = "1";
21   var $k                                = "kolab";
22   var $cyrus_imap                       = "TRUE";
23   var $cyrus_pop3                       = "TRUE";
24   var $cyrus_imaps                      = "TRUE";
25   var $cyrus_pop3s                      = "TRUE";
26   var $cyrus_sieve                      = "TRUE";
27   var $apache_allow_unauthenticated_fb  = "TRUE";
28   var $proftpd_ftp                      = "TRUE";
29   var $apache_http                      = "TRUE";
30   var $kolabHost                        = array();
31   var $orig_cn                          = "";
32   var $view_logged                      = FALSE;
34   var $attributes =  array("postfix_mydomain", "postfix_mydestination", "proftpd_ftp", "k",
35       "postfix_mynetworks", "postfix_enable_virus_scan", "postfix_relayhost", "apache_http",
36       "postfix_allow_unauthenticated", "cyrus_admins", "cyrus_imap","kolabFreeBusyFuture",
37       "cyrus_pop3", "cyrus_imaps", "cyrus_pop3s", "cyrus_sieve", "apache_allow_unauthenticated_fb",
38       "cyrus_quotawarn");
39   var $objectclasses = array("top", "kolab");
41   /* Serverservice vars */
42   var $conflicts    = array("goImapServer","goMailServer");
43   var $DisplayName  = "Kolab mail service";
44   var $StatusFlag   = "";
46   function servkolab($config, $dn = NULL, $parent= NULL) 
47   {
48     /* Setting the hostname and tell this Plugin that we are the kolab extension*/
49     $this->hostname = preg_replace('/^cn=([^,]+),.*$/', '\1', $dn);
50     $this->dn       = "k=kolab,".$config->current['BASE'];
52     /* Load variables, if given*/
53     plugin::plugin($config, $this->dn);
55     /* Copy needed attributes */
56     foreach($this->attributes as $val) {
57       $name = preg_replace('/_/', '-', $val);
58       if (isset($this->attrs["$name"][0])) {
59         $this->$val = $this->attrs["$name"][0];
60       }
61     }
63     /* Toggle relayhost */
64     $this->postfix_mxrelayenabled = preg_match('/^\[/', $this->postfix_relayhost);
65     $this->postfix_relayhost      = preg_replace("/[\[\]]/","",$this->postfix_relayhost);
67     /* Is this Server a member of the Kolab extension or not ?*/ 
68     if(isset($this->attrs['kolabHost'])) {
69       $this->kolabHost= $this->attrs['kolabHost'];
70       unset($this->kolabHost['count']);
71     }
72     $this->is_account            = false;
73     $this->initially_was_account = false;
74     foreach($this->kolabHost as $host){
75       if($this->hostname == $host){
76         $this->is_account            = true;
77         $this->initially_was_account = true;
78       }
79     } 
80   }
83   function execute() 
84   {
85     /* Call parent execute */
86     plugin::execute();
88     if($this->is_account && !$this->view_logged){
89       $this->view_logged = TRUE;
90       @log::log("view","server/".get_class($this),$this->dn);
91     }
93     /***************  
94       Variable initialisation
95      ***************/  
96     
97     $smarty     = get_smarty();
98     $display    = "";
99     $ldap       = $this->config->get_ldap_link();
102     /* Assemble free/busy string */
103     $edit       = sprintf('<input name="kolabFreeBusyFuture" value="%s" type="text" maxlength="3" size="4">',
104                     $this->kolabFreeBusyFuture);
105     $fbfuture   = sprintf(_("Include data from %s days in the past when creating free/busy lists"), $edit);
107     /* Assemble quota string */
108     $edit       = sprintf('<input name="cyrus_quotawarn" value="%s" type="text" maxlength="3" size="4">',
109                     $this->cyrus_quotawarn);
110     $quotastr   = sprintf(_("Warn users when using more than %s%% of their mail quota"), $edit);
112     /***************
113       Assign informations to smarty 
114      ***************/
116     /* Set relayhost and if we have MX lookup enabled*/ 
117     if($this->postfix_mxrelayenabled) {
118       $smarty->assign("RelayMxSupportCheck"," checked ");
119     } else {
120       $smarty->assign("RelayMxSupportCheck","");
121     }
123     /* Set acls */
124     $tmp = $this->plInfo();
125     foreach($tmp['plProvidedAcls'] as $name => $translation){
126       $smarty->assign($name."ACL",$this->getacl($name));
127     }
129     /* Initialize all attributes, that were submitted */
130     foreach($this->attributes as $val) 
131     {
132       /* Tell smarty which variables we are useing */
133       $smarty->assign($val, $this->$val);
134       if (($this->$val != "FALSE") && !empty($this->$val)){
135         $smarty->assign($val."Check", "checked");
136       } else {
137         $smarty->assign($val."Check", "");
138       }
139     }
141     $smarty->assign("fbfuture", $fbfuture);
142     $smarty->assign("quotastr", $quotastr);
144     /* Load Template */
145     $display.=$smarty->fetch(get_template_path('servkolab.tpl', TRUE));
146     return ($display);
147   }
149   function remove_from_parent() 
150   {
152      /* Remove status flag, it is not a memeber of
153         this->attributes, so ensure that it is deleted too */
154     if(!empty($this->StatusFlag)){
155       $this->attrs[$this->StatusFlag] = array();
156     }
159     /* Only walk through following code, if this host 
160        was a member of the kolab hosts, else skip this */
161     if(!$this->initially_was_account){
162       return;
163     }
165     /* !!! Don't use "cn" in this function 
166        hostname -> the initial name of the host
167        cn       -> is the new name of the host, in case that it was renamed.
168      */
170     $ldap     = $this->config->get_ldap_link();
171     $this->dn = "k=kolab,".$this->config->current['BASE'];
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->hostname,$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->hostname) || (empty($this->hostname))){
191       print_red("The required attribute hostname 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){
198       /* Remove complete entry */
199       $ldap->rmdir ($this->dn);
200       show_ldap_error($ldap->get_error(), sprintf(_("Removing host entry from system server/kolab with dn '%s' failed."),$this->dn));
201     } else {
203       /* Only modify kolabHost */
204       $hosts= array();
205       foreach ($this->kolabHost as $host){
206         if($host != $this->hostname){
207           $hosts[]= $host;
208         }
209       }
210       $attrs= array('kolabHost' => $hosts);
211       $ldap->cd($this->dn);
212       $this->cleanup();
213       $ldap->modify ($attrs); 
214       show_ldap_error($ldap->get_error(), sprintf(_("Removing host entry from system server/kolab with dn '%s' failed."),$this->dn));
215     }
217     @log::log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
219     /* Optionally execute a command after we're done */
220     $this->handle_post_events("remove");
221   }
224   function save_object()    
225   {
226     if (isset($_POST['kolabtab'])){
227       plugin::save_object();
230       foreach($this->attributes as $attr){
231         if(($this->acl_is_writeable(preg_replace("/_/","",$attr))) && (isset($_POST[$attr]))){
232           $this->$attr = $_POST[$attr];
233         }
234       }
236       /* Save checkboxes */
237       foreach (array( "postfix_enable_virus_scan", "postfix_allow_unauthenticated",
238             "cyrus_imap", "cyrus_pop3", "cyrus_imaps",
239             "cyrus_pop3s", "cyrus_sieve", "apache_allow_unauthenticated_fb",
240             "proftpd_ftp", "apache_http") as $cb){
242         if($this->acl_is_writeable(preg_replace("/_/","",$cb))){
243           if (isset($_POST[$cb])){
244             $this->$cb= "TRUE";
245           } else {
246             $this->$cb= "FALSE";
247           }
248         }
249       }
251       /* Toggell relay check */
252       if($this->acl_is_writeable("postfixmxrelayenabled")){
253         $this->postfix_mxrelayenabled= isset($_POST['RelayMxSupport']);
254       }
255     }
257   }
260   function check() 
261   {
262     /* Call common method to give check the hook */
263     $message= plugin::check();
265     if(($this->kolabFreeBusyFuture==""))    {
266       $message[] = _("Future days in Free/Busy settings must be set.");
267     }elseif(!is_uid($this->kolabFreeBusyFuture) || $this->kolabFreeBusyFuture < 0){
268       $message[] = _("Future days in Free/Busy settings must be a positive value.");
269     }
271     if(!is_int((int)($this->cyrus_quotawarn))) {
272       $message[] = _("The given Quota settings value must be a number.");
273     }elseif(!(($this->cyrus_quotawarn<=100)&&($this->cyrus_quotawarn>=0))){
274       $message[] = _("Please choose a value between 1 and 100 for Quota settings.");
275     }elseif(strcasecmp($this->cyrus_quotawarn,(int)$this->cyrus_quotawarn)){
276       $message[] = _("Future days must be a value.");
277     }
279     if(empty($this->postfix_mynetworks)) { 
280       $message[] = _("No SMTP privileged networks set.");
281     }
283     /*if(empty($this->postfix_relayhost)) {
284       $message[] = _("No SMTP smarthost/relayhost set.");
285       }
286      */
288     return ($message);
289   }
292   /* Save to LDAP */
293   function save() 
294   {
295     /* Set ldap connection */
296     $ldap       = $this->config->get_ldap_link();
298     /* Open current dn*/
299     $this->dn   = "k=kolab,".$this->config->current['BASE'];
301     /* Adapt relayhost */
302     $this->postfix_relayhost= preg_replace('/[\[\]]/', '', $this->postfix_relayhost);
303     if ($this->postfix_mxrelayenabled && $this->postfix_relayhost != ""){
304       $this->postfix_relayhost= "[".$this->postfix_relayhost."]";
305     }
307     /* Check if this server was renamed, in this case we have to remove old cn first*/
308     if($this->hostname != $this->cn){
309       $tmp = array();
310       if(in_array_ics($this->hostname,$this->kolabHost)){
311         foreach($this->kolabHost as $host){
312           if($host != $this->hostname){
313             $tmp[] = $host;
314           }
315         }
316         $this->kolabHost = $tmp;
317       }
318     }
320     /* Add ourselves to the list of kolabHost's if needed */
321     if (!in_array_ics($this->cn,$this->kolabHost)){
322       $this->kolabHost[]= $this->cn;
323     }
325     /* Call parents save to prepare $this->attrs */
326     plugin::save();
328     /* Save or modify? */
329     $ldap->cat($this->dn, array('dn'));
330     if (!$ldap->fetch()){
331       $mode= "add"; 
332     } else {
333       $mode= "modify";
334     }
336     /* Do attribute conversion */
337     foreach ($this->attrs as $key => $value){
338       if (preg_match('/_/', $key)){
339         $old_key= $key;
340         $key= preg_replace('/_/', '-', $key);
341         $this->attrs[$key]= $value;
342         unset($this->attrs[$old_key]);
343       }
344     }
345     /* Add kolab hosts */
346     $this->attrs['kolabHost']= $this->kolabHost;
348     /* Perform LDAP action */
349     $ldap->cd($this->dn);
350     $this->cleanup();;
351     $ldap->$mode($this->attrs);
353     if($mode == "add"){
354       @log::log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
355     }else{
356       @log::log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
357     }
359     show_ldap_error($ldap->get_error(), sprintf(_("Saving system server/kolab with dn '%s' failed."),$this->dn));
361     /* Optionally execute a command after we're done */
362     if ($this->initially_was_account == $this->is_account) {
363       if ($this->is_modified) {
364         $this->handle_post_events("modify");
365       }
366     } else {
367       $this->handle_post_events("add");
368     }
369   }
372   function getListEntry()
373   {
374     $fields                 = goService::getListEntry();
375     $fields['Message']      = _("Kolab mail service");
376     $fields['AllowEdit']    = true;
377     return($fields);
378   }
381   /* Return plugin informations for acl handling */
382   function plInfo()
383   {
384     return (array(
385           "plShortName"   => _("Kolab"),
386           "plDescription" => _("Kolab mail service")." ("._("Services").")",
387           "plSelfModify"  => FALSE,
388           "plDepends"     => array(),
389           "plPriority"    => 85,
390           "plSection"     => array("administration"),
391           "plCategory"    => array("server"),
393           "plProvidedAcls"=> array(
394             "postfixmydomain"                => _("Postfix mydomain") ,
395             "postfixmydestination"           => _("My destination") ,
396             "cyrusadmins"                    => _("Cyrus admins") ,
397             "proftpdftp"                     => _("FTP FreeBusy service") ,
398             "postfixmynetworks"              => _("SMTP privileged networks") ,
399             "postfixenablevirusscan"         => _("Enable virus scan") ,
400             "postfixrelayhost"               => _("Relayhost") ,
401             "postfixmxrelayenabled"          => _("Enable MX lookup for relayhost"),
402             "apachehttp"                     => _("HTTP FreeBusy service") ,
403             "apacheallowunauthenticatedfb"   => _("Allow unauthenticated free busy"),
404             "postfixallowunauthenticated"    => _("Accept Internet Mail") ,
405             "cyrusimap"                      => _("IMAP service") ,
406             "kolabFreeBusyFuture"            => _("kolabFreeBusyFuture") ,
407             "cyruspop3"                      => _("POP3 service") ,
408             "cyrusimaps"                     => _("IMAP/SSL service") ,
409             "cyruspop3s"                     => _("POP3/SSL service") ,
410             "cyrussieve"                     => _("Sieve service") ,
411             "cyrusquotawarn"                 => _("Quota settings")) 
412             ));
413   }
416 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
417 ?>