Code

Added size_limit warning message
[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! Please check the mail server settings in the mail tab."));
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         foreach ($users as $userid => $perms){
152           imap_setacl ($this->mbox, $subfolder, $userid, "");
153         }
154       } else {
155         print_red (_("Warning: imap_getacl is not implemented, can't remove acl informations."));
156       }
158       /* Set permissions for this folder */
159       foreach ($permissions as $user => $acl){
160         imap_setacl ($this->mbox, $subfolder, $user, $acl);
161       }
162     }
164   }
167   function getSharedFolderPermissions($folder)
168   {
169     $result= array();
171     /* imap_getacl available? */
172     if (!function_exists('imap_getacl')){
173       print_red (_("Warning: imap_getacl is not available, can't get imap permissions!"));
174     }
176     /* Get permissions in case of shared folders */
177     else {
178       $users= imap_getacl ($this->mbox, $folder);
180       foreach ($users as $userid => $perms){
181         $result[preg_replace('/^user\./', '', $userid)]= $perms;
182       }
184     }
186     return ($result);
187   }
190   function deleteMailbox($folder)
191   {
192     $cfg= $this->config[$this->gosaMailServer];
193     imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
194     if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
195       print_red(sprintf(_("Can't remove IMAP mailbox. Server says '%s'."), imap_last_error()));
196       return (FALSE);
197     }
198     return (TRUE);
199   }
202   function configureFilter($user, $gosaMailDeliveryMode,
203       $mail, $gosaMailAlternateAddress,
204       $gosaMailMaxSize,
205       $gosaSpamMailbox, $gosaSpamSortLevel,
206       $gosaVacationMessage)
207   {
208     $cfg= $this->config[$this->gosaMailServer];
210     /* Build spamlevel. Spamassassin tags mails with "*" for each integer
211        point of spam. So a spam level of 5.3 gets "*****" which can be
212        checked easily by spam filters */
213     $spamlevel= "";
214     for ($i= 0; $i<$gosaSpamSortLevel; $i++){
215       $spamlevel .= "*";
216     }
218     /* Log into the mail server */
219     $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
220         $cfg["password"], $cfg["admin"]);
222     if (!$sieve->sieve_login()){
223       print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
224             to_string($sieve->error_raw)));
225       return;
226     }
228     /* Load current script from server and remove everything between the comments
229        "###GOSA" */
230     $script= "";
231     if($sieve->sieve_listscripts()){
232       if (in_array("gosa", $sieve->response)){
234         /* get old GOsa script */
235         if(!$sieve->sieve_getscript("gosa")){
236           print_red(sprintf(_("Can't get sieve script. Server says '%s'."), to_string($sieve->error_raw)));
237           return;
238         }
240         foreach ($sieve->response as $line){
241           if (preg_match ("/^###GOSA/", $line)){
242             break;
243           }
244           $line= rtrim($line);
245           if (!preg_match ('/^\s*$/', $line)){
246             $script .= $line."\n";
247           }
248         }
250       }
251     }
253     /* Only create a new one, if it is not empty */
254     if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
255         is_integer(strpos($gosaMailDeliveryMode, "C")) ||
256         !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
257         is_integer(strpos($gosaMailDeliveryMode, "V")) ||
258         is_integer(strpos($gosaMailDeliveryMode, "S"))){
260       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
261       eval ("\$script.=\"$text\";");
262     }
264     /* Add anti-spam code */
265     if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
266       $spambox= $gosaSpamMailbox;
267       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
268       eval ("\$script.=\"$text\";");
269     }
271     /* Add "reject due to mailsize" code, message is currently not
272        adjustable through GOsa. */
273     if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
274       $maxsize= $gosaMailMaxSize;
275       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
276       eval ("\$script.=\"$text\";");
277     }
279     /* Add vacation information */
280     if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
282       /* Sieve wants all destination addresses for the
283          vacation message, so we've to assemble them from
284          mail and mailAlternateAddress */
285       $addrlist= "\"".$mail."\"";
286       foreach ($gosaMailAlternateAddress as $val){
287         $addrlist .= ", \"$val\"";
288       }
289       $vacmsg= $gosaVacationMessage;
290       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
291       eval ("\$script.=\"$text\";");
292     }
294     /* If no local delivery is wanted, tell the script to discard the mail */
295     if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
296       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
297       eval ("\$script.=\"$text\";");
298     }
300     /* Just be aware of null scripts... */
301     if (!isset ($script)){
302       $script= "";
303     }
305     /* Upload script and make it the default one */
306     if (!$sieve->sieve_sendscript("gosa", $script)){
307       print_red(sprintf(_("Can't send sieve script. Server says '%s'."), to_string($sieve->error_raw)));
308       return;
309     }
310     if(!$sieve->sieve_setactivescript("gosa")){
311       print_red(sprintf(_("Can't activate GOsa sieve script. Server says '%s'."), to_string($sieve->error_raw)));
312       return;
313     }
315     $sieve->sieve_logout();
316   }
320 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
321 ?>