Code

Updated mailMethods.
[gosa.git] / gosa-plugins / mail / personal / mail / class_mail-methods-cyrus.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2004  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class mailMethodCyrus extends mailMethod
22 {
23   var $mbox= "-";
24   var $config;
25   var $gosaMailServer= "";
26   var $uattrib= "uid";
28   function mailMethodCyrus(&$config)
29   {
30     /* Check if the mail account identification attribute
31        is overridden in the configuration file
32      */
33     if(isset($config->current['MAIL_ATTRIB'])){
34       $this->uattrib = $config->current['MAIL_ATTRIB'];
35     }
36     $this->config= $config->data['SERVERS']['IMAP'];
37   }
39   function connect($gosaMailServer)
40   {
41     $cfg=array();
43     /* Connect to IMAP server. I don't want to see these warning here... */
44     $this->gosaMailServer= $gosaMailServer;
45     if (!isset($this->config[$gosaMailServer])){
46       msg_dialog::display(_("Warning"), _("Mail server for this account is invalid!"), WARNING_DIALOG);
47     } else {
48       $cfg= $this->config[$gosaMailServer];
49     }
50     /* For some reason, hiding errors with @ does not wor here... */
51     if(!isset($cfg['connect']))   $cfg['connect']="";
52     if(!isset($cfg['admin']))     $cfg['admin']="";
53     if(!isset($cfg['password']))  $cfg['password']="";
55     /* Setting connect timeout to 10 seconds,
56         else the GOsa UI may freeze for 60 seconds.
57        (PHP default is 'default_socket_timeout = 60') */
58     imap_timeout(1, 10 );
60     $this->mbox = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
62     /* Mailbox reachable? */
63     if ($this->mbox === FALSE){
64       msg_dialog::display(_("IMAP error"), _("Cannot store mail settings on IMAP server!"), ERROR_DIALOG);
65       return (FALSE);
66     }
67     return (TRUE);
68   }
70   function disconnect()
71   {
72     imap_close ($this->mbox);
73   }
75   function getQuota($folder)
76   {
77     $result= array('quotaUsage' => '', 'gosaMailQuota' => '');
79     /* Load quota settings */
80     error_reporting (0);
81     $quota_value = @imap_get_quota($this->mbox, $folder);
82     if(is_array($quota_value)) {
83       if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])){
84         /* use for PHP >= 4.3 */
85         if($quota_value["STORAGE"]['limit'] == 2147483647){
86           $result['quotaUsage']=    (int) ($quota_value["STORAGE"]['usage'] / 1024);
87           $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] );
88         }else{
89           $result['quotaUsage']=    (int) ($quota_value["STORAGE"]['usage'] / 1024);
90           $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] / 1024);
91         }
92       } else {
93         /* backward icompatible */
94         if($quota_value['usage'] == 2147483647){
95           $result['quotaUsage']=    (int) ($quota_value['usage'] / 1024);
96           $result['gosaMailQuota']= (int) ($quota_value['limit'] );
97         }else{
98           $result['quotaUsage']=    (int) ($quota_value['usage'] / 1024);
99           $result['gosaMailQuota']= (int) ($quota_value['limit'] / 1024);
100         }
101       }
102     }elseif(!$quota_value){
103       /* If there is no quota defined for this account, the function imap_get_quota returns false. */
104       return(array("quotaUsage"=>"","gosaMailQuota"=>""));
105     }
106  
107     error_reporting (E_ALL | E_STRICT);
108     return ($result);
109   }
112   /* return all folders of the users mailbox*/
113   function getMailboxList($folder, $uid)
114   {
115     global $config;
116     $result = array();
118     /* Get domain an mail address if uid is an mail address */
119     $domain = "";
120     if(preg_match("/@/",$folder)){
121       $domain = "@".preg_replace("/^.*@/","",$folder);
122       $folder = preg_replace("/@.*$/","",$folder);
123     }
125     /* Get list of mailboxes for combo box */
126     $cfg= $this->config[$this->gosaMailServer];
128     /* Create search pattern
129          (user/kekse*@domain.de
130           user.kekse*@domain.de
131           user.kekse*  )
132        depending on given folder name) */
133     $q = $folder."*".$domain;
134     $list = imap_listmailbox($this->mbox, $cfg["connect"], $q);
136     /* Create list of returned folder names */
137     if (is_array($list)){
138       foreach ($list as $val){
140         /* Cut domain name */
141         $val = preg_replace("/@.*$/","",$val);
142         $result[]=preg_replace ("/^.*".normalizePreg($folder)."/","INBOX", mb_convert_encoding($val, "UTF-8", "UTF7-IMAP"));
143       }
144     }
146     /* Append "INBOX" to the folder array if result is empty and request comes from user dialog */
147     if(empty($result) && !empty($uid)){
148       $result[] = "INBOX";
149     }
151     return ($result);
152   }
155   function updateMailbox($folder)
156   {
157     /* Check if mailbox exists */
158     $cfg= $this->config[$this->gosaMailServer];
159     $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
160     if ($list === FALSE){
161       if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
162         msg_dialog::display(_("IMAP error"), sprintf(_("Cannot create IMAP mailbox: %s"), '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
163         return;
164       }
165     }
166   }
169   function setQuota($folder, $gosaMailQuota)
170   {
171     /* Workaround for the php imap extension */
172     if (($gosaMailQuota == "") || ($gosaMailQuota== "2147483647")){
173       $gosaMailQuota= "2147483647";
174     }elseif($gosaMailQuota > 0){
175       $gosaMailQuota = $gosaMailQuota *1024;
176     }
177     
178     
179     /* Write mail quota */
180     if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
181       msg_dialog::display(_("IMAP error"), sprintf(_("Cannot modify IMAP mailbox quota: %s"), '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
182       return (FALSE);
183     }
184     return (TRUE);
185   }
188   function setSharedFolderPermissions($folder, $permissions)
189   {
190     /* Get list of subfolders */
191     $folders= $this->getMailboxList($folder, "");
192     $folders[]= $folder;
193     
194     foreach ($folders as $subfolder){
196       /* Set shared folder acl's */
197       if (function_exists('imap_getacl')){
199         /* Remove all acl's for this folder */
200         $users= @imap_getacl ($this->mbox, $subfolder);
201         if(is_array($users)){
202           foreach ($users as $userid => $perms){
203             imap_setacl ($this->mbox, $subfolder, $userid, "");
204           }
205         }
206       } else {
207         msg_dialog::display(_("Internal error"), _("Cannot remove IMAP ACL: imap_getacl not implemented!"), ERROR_DIALOG);
208       }
210       /* Set permissions for this folder */
211       foreach ($permissions as $user => $acl){
212         imap_setacl ($this->mbox, $subfolder, $user, $acl);
213       }
214     }
216   }
219   function getSharedFolderPermissions($folder)
220   {
221     $result= array();
223     /* imap_getacl available? */
224     if (!function_exists('imap_getacl')){
225       msg_dialog::display(_("Internal error"), _("Cannot retrieve IMAP ACL: imap_getacl not implemented!"), ERROR_DIALOG);
226     }
228     /* Get permissions in case of shared folders */
229     else {
230       $users= imap_getacl ($this->mbox, $folder);
232       foreach ($users as $userid => $perms){
233         $result[preg_replace('/^user\./', '', $userid)]= $perms;
234       }
236     }
238     return ($result);
239   }
242   function deleteMailbox($folder)
243   {
244     $cfg= $this->config[$this->gosaMailServer];
245     imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
246     if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
247       msg_dialog::display(_("IMAP error"), sprintf(_('Cannot remove IMAP mailbox: %s'), '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
248       return (FALSE);
249     }
250     return (TRUE);
251   }
254   function configureFilter($user, $gosaMailDeliveryMode,
255       $mail, $gosaMailAlternateAddress,
256       $gosaMailMaxSize,
257       $gosaSpamMailbox, $gosaSpamSortLevel,
258       $gosaVacationMessage)
259   {
260     $cfg= $this->config[$this->gosaMailServer];
262     /* Build spamlevel. Spamassassin tags mails with "*" for each integer
263        point of spam. So a spam level of 5.3 gets "*****" which can be
264        checked easily by spam filters */
265     $spamlevel= "";
266     for ($i= 0; $i<$gosaSpamSortLevel; $i++){
267       $spamlevel .= "*";
268     }
270     /* Log into the mail server */
271     $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
272         $cfg["password"], $cfg["admin"]);
274     if (!$sieve->sieve_login()){
275       msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot log into SIEVE server: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
276       return;
277     }
279     /* Load current script from server and remove everything between the comments
280        "###GOSA" */
281     $script= "";
282     if($sieve->sieve_listscripts()){
283       if (in_array("gosa", $sieve->response)){
285         /* get old GOsa script */
286         if(!$sieve->sieve_getscript("gosa")){
287           msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot retrieve SIEVE script: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
288           return;
289         }
291         foreach ($sieve->response as $line){
292           if (preg_match ("/^###GOSA/", $line)){
293             break;
294           }
295           $line= rtrim($line);
296           if (!preg_match ('/^\s*$/', $line)){
297             $script .= $line."\n";
298           }
299         }
301       }
302     }
304     /* Only create a new one, if it is not empty */
305     if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
306         is_integer(strpos($gosaMailDeliveryMode, "C")) ||
307         !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
308         is_integer(strpos($gosaMailDeliveryMode, "V")) ||
309         is_integer(strpos($gosaMailDeliveryMode, "S"))){
311       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
312       eval ("\$script.=\"$text\";");
313     }
315     /* Add anti-spam code */
316     if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
317       $spambox= $gosaSpamMailbox;
318       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
319       eval ("\$script.=\"$text\";");
320     }
322     /* Add "reject due to mailsize" code, message is currently not
323        adjustable through GOsa. */
324     if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
325       $maxsize= $gosaMailMaxSize;
326       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
327       eval ("\$script.=\"$text\";");
328     }
330     /* Add vacation information */
331     if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
333       /* Sieve wants all destination addresses for the
334          vacation message, so we've to assemble them from
335          mail and mailAlternateAddress */
336       $addrlist= "\"".$mail."\"";
337       foreach ($gosaMailAlternateAddress as $val){
338         $addrlist .= ", \"$val\"";
339       }
340       $vacmsg= $gosaVacationMessage;
341       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
342       eval ("\$script.=\"$text\";");
343     }
345     /* If no local delivery is wanted, tell the script to discard the mail */
346     if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
347       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
348       eval ("\$script.=\"$text\";");
349     }
351     /* Just be aware of null scripts... */
352     if (!isset ($script)){
353       $script= "";
354     }
356     /* Upload script and make it the default one */
357     if (!$sieve->sieve_sendscript("gosa", $script)){
358       msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot store SIEVE script: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
359       return;
360     }
361     if(!$sieve->sieve_setactivescript("gosa")){
362       msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot activate SIEVE script: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
363       return;
364     }
366     $sieve->sieve_logout();
367   }
371 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
372 ?>