Code

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