Code

Completed migration
[gosa.git] / include / 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  */
22 class mailMethodCyrus extends mailMethod
23 {
24   var $mbox= "-";
25   var $config;
26   var $gosaMailServer= "";
28   function mailMethodCyrus($config)
29   {
30     $this->config= $config->data['SERVERS']['IMAP'];
31   }
33   function connect($gosaMailServer)
34   {
35     /* Connect to IMAP server. I don't want to see these warning here... */
36     error_reporting (0);
37     $this->gosaMailServer= $gosaMailServer;
38     $cfg= $this->config[$gosaMailServer];
39     $this->mbox = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
40     error_reporting (E_ALL);
42     /* Mailbox reachable? */
43     if ($this->mbox === FALSE){
44       print_red (_("Warning: IMAP Server cannot be reached! If you save this account, some mail settings will not be stored on your server!"));
45       return (FALSE);
46     }
47     return (TRUE);
48   }
50   function disconnect()
51   {
52     imap_close ($this->mbox);
53   }
55   function getQuota($folder)
56   {
57     $result= array('quotaUsage' => '', 'gosaMailQuota' => '');
59     /* Load quota settings */
60     error_reporting (0);
61     $quota_value = @imap_get_quota($this->mbox, $folder);
62     if(is_array($quota_value)) {
63       if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])){
64         /* use for PHP >= 4.3 */
65         $result['quotaUsage']=    $quota_value["STORAGE"]['usage'];
66         $result['gosaMailQuota']= $quota_value["STORAGE"]['limit'];
67       } else {
68         /* backward compatible */
69         $result['quotaUsage']=    $quota_value['usage'];
70         $result['gosaMailQuota']= $quota_value['limit'];
71       }
72     }
73     error_reporting (E_ALL);
75     return ($result);
76   }
78   function getMailboxList($folder, $uid= "")
79   {
80     /* Initialize depending on group or user mode */
81     if ($uid != ""){
82       $result= array("INBOX");
83     } else {
84       $result= array();
85     }
87     /* Get list of mailboxes for combo box */
88     $cfg= $this->config[$this->gosaMailServer];
89     $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder.".*");
90     if (is_array($list)){
91       foreach ($list as $val){
92         $result[]=preg_replace ("/.*user\.".$uid."\./",
93             "INBOX.", imap_utf7_decode ($val));
94       }
95     }
97     return ($result);
98   }
100   function updateMailbox($folder)
101   {
102     /* Check if mailbox exists */
103     $cfg= $this->config[$this->gosaMailServer];
104     $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
105     if ($list === FALSE){
106       if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
107         print_red(sprintf(_("Can't create IMAP mailbox. Server says '%s'."), imap_last_error()));
108         return;
109       }
110     }
111   }
114   function setQuota($folder, $gosaMailQuota)
115   {
116     /* Workaround for the php imap extension */
117     if ($gosaMailQuota == ""){
118       $gosaMailQuota= "0";
119     }
121     /* Write mail quota */
122     if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
123       print_red(sprintf(_("Can't write IMAP quota. Server says '%s'."), imap_last_error()));
124       return (FALSE);
125     }
126     return (TRUE);
127   }
130   function setSharedFolderPermissions($folder, $permissions)
131   {
132     /* Show warning message in case of missing imap_getacl */
133     if (!function_exists('imap_getacl')){
134       print_red (_("Warning: imap_getacl is not implemented, wouldn't modify acl informations."));
135       return;
136     }
138     /* Get list of subfolders */
139     $folders= $this->getMailboxList($folder, "");
140     $folders[]= $folder;
141     
142     foreach ($folders as $subfolder){
144       /* Set shared folder acl's */
145       if (function_exists('imap_getacl')){
147         /* Remove all acl's for this folder */
148         $users= imap_getacl ($this->mbox, $subfolder);
149         foreach ($users as $userid => $perms){
150           imap_setacl ($this->mbox, $subfolder, $userid, "");
151         }
152       }
154       /* Set permissions for this folder */
155       foreach ($permissions as $user => $acl){
156         imap_setacl ($this->mbox, $subfolder, $user, $acl);
157       }
158     }
160   }
163   function getSharedFolderPermissions($folder)
164   {
165     $result= array();
167     /* imap_getacl available? */
168     if (!function_exists('imap_getacl')){
169       print_red (_("Warning: imap_getacl is not available, can't get imap permissions!"));
170     }
172     /* Get permissions in case of shared folders */
173     else {
174       $users= imap_getacl ($this->mbox, $folder);
176       foreach ($users as $userid => $perms){
177         $result[preg_replace('/^user\./', '', $userid)]= $perms;
178       }
180     }
182     return ($result);
183   }
186   function deleteMailbox($folder)
187   {
188     $cfg= $this->config[$this->gosaMailServer];
189     imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
190     if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
191       print_red(sprintf(_("Can't remove IMAP mailbox. Server says '%s'."), imap_last_error()));
192       return (FALSE);
193     }
194     return (TRUE);
195   }
198   function configureFilter($user, $gosaMailDeliveryMode,
199       $mail, $gosaMailAlternateAddress,
200       $gosaMailMaxSize,
201       $gosaSpamMailbox, $gosaSpamSortLevel,
202       $gosaVacationMessage)
203   {
204     $cfg= $this->config[$this->gosaMailServer];
206     /* Build spamlevel. Spamassassin tags mails with "*" for each integer
207        point of spam. So a spam level of 5.3 gets "*****" which can be
208        checked easily by spam filters */
209     $spamlevel= "";
210     for ($i= 0; $i<$gosaSpamSortLevel; $i++){
211       $spamlevel .= "*";
212     }
214     /* Log into the mail server */
215     $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
216         $cfg["password"], $cfg["admin"]);
218     if (!$sieve->sieve_login()){
219       print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
220             to_string($sieve->error_raw)));
221       return;
222     }
224     /* Load current script from server and remove everything between the comments
225        "###GOSA" */
226     $script= "";
227     if($sieve->sieve_listscripts()){
228       if (in_array("gosa", $sieve->response)){
230         /* get old GOsa script */
231         if(!$sieve->sieve_getscript("gosa")){
232           print_red(sprintf(_("Can't get sieve script. Server says '%s'."), to_string($sieve->error_raw)));
233           return;
234         }
236         foreach ($sieve->response as $line){
237           if (preg_match ("/^###GOSA/", $line)){
238             break;
239           }
240           $line= rtrim($line);
241           if (!preg_match ('/^\s*$/', $line)){
242             $script .= $line."\n";
243           }
244         }
246       }
247     }
249     /* Only create a new one, if it is not empty */
250     if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
251         is_integer(strpos($gosaMailDeliveryMode, "C")) ||
252         !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
253         is_integer(strpos($gosaMailDeliveryMode, "V")) ||
254         is_integer(strpos($gosaMailDeliveryMode, "S"))){
256       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
257       eval ("\$script.=\"$text\";");
258     }
260     /* Add anti-spam code */
261     if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
262       $spambox= $gosaSpamMailbox;
263       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
264       eval ("\$script.=\"$text\";");
265     }
267     /* Add "reject due to mailsize" code, message is currently not
268        adjustable through GOsa. */
269     if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
270       $maxsize= $gosaMailMaxSize;
271       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
272       eval ("\$script.=\"$text\";");
273     }
275     /* Add vacation information */
276     if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
278       /* Sieve wants all destination addresses for the
279          vacation message, so we've to assemble them from
280          mail and mailAlternateAddress */
281       $addrlist= "\"".$mail."\"";
282       foreach ($gosaMailAlternateAddress as $val){
283         $addrlist .= ", \"$val\"";
284       }
285       $vacmsg= $gosaVacationMessage;
286       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
287       eval ("\$script.=\"$text\";");
288     }
290     /* If no local delivery is wanted, tell the script to discard the mail */
291     if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
292       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
293       eval ("\$script.=\"$text\";");
294     }
296     /* Just be aware of null scripts... */
297     if (!isset ($script)){
298       $script= "";
299     }
301     /* Upload script and make it the default one */
302     if (!$sieve->sieve_sendscript("gosa", $script)){
303       print_red(sprintf(_("Can't send sieve script. Server says '%s'."), to_string($sieve->error_raw)));
304       return;
305     }
306     if(!$sieve->sieve_setactivescript("gosa")){
307       print_red(sprintf(_("Can't activate GOsa sieve script. Server says '%s'."), to_string($sieve->error_raw)));
308       return;
309     }
311     $sieve->sieve_logout();
312   }
316 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
317 ?>