Code

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