Code

Removed error message if ne quota was defined
[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         if($quota_value["STORAGE"]['limit'] == 2147483647){
74           $result['quotaUsage']=    (int) ($quota_value["STORAGE"]['usage'] / 1024);
75           $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] );
76         }else{
77           $result['quotaUsage']=    (int) ($quota_value["STORAGE"]['usage'] / 1024);
78           $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] / 1024);
79         }
80       } else {
81         /* backward icompatible */
82         if($quota_value['usage'] == 2147483647){
83           $result['quotaUsage']=    (int) ($quota_value['usage'] / 1024);
84           $result['gosaMailQuota']= (int) ($quota_value['limit'] );
85         }else{
86           $result['quotaUsage']=    (int) ($quota_value['usage'] / 1024);
87           $result['gosaMailQuota']= (int) ($quota_value['limit'] / 1024);
88         }
89       }
90     }elseif(!$quota_value){
91       /* If there is no quota defined for this account, the function imap_get_quota returns false. */
92       return(array());
93     }
94  
95     error_reporting (E_ALL);
96     return ($result);
97   }
99   function getMailboxList($folder, $uid= "")
100   {
101     /* Initialize depending on group or user mode */
102     if ($uid != ""){
103       $result= array("INBOX");
104     } else {
105       $result= array();
106     }
108     /* Get list of mailboxes for combo box */
109     $cfg= $this->config[$this->gosaMailServer];
110     $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder.".*");
111     if (is_array($list)){
112       foreach ($list as $val){
113         $result[]=preg_replace ("/.*user\.".$uid."\./",
114             "INBOX.", imap_utf7_decode ($val));
115       }
116     }
118     return ($result);
119   }
121   function updateMailbox($folder)
122   {
123     /* Check if mailbox exists */
124     $cfg= $this->config[$this->gosaMailServer];
125     $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
126     if ($list === FALSE){
127       if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
128         print_red(sprintf(_("Can't create IMAP mailbox. Server says '%s'."), imap_last_error()));
129         return;
130       }
131     }
132   }
135   function setQuota($folder, $gosaMailQuota)
136   {
137     /* Workaround for the php imap extension */
138     if (($gosaMailQuota == "") || ($gosaMailQuota== "2147483647")){
139       $gosaMailQuota= "2147483647";
140     }elseif($gosaMailQuota > 0){
141       $gosaMailQuota = $gosaMailQuota *1024;
142     }
143     
144     
145     /* Write mail quota */
146     if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
147       print_red(sprintf(_("Can't write IMAP quota. Server says '%s'."), imap_last_error()));
148       return (FALSE);
149     }
150     return (TRUE);
151   }
154   function setSharedFolderPermissions($folder, $permissions)
155   {
156     /* Get list of subfolders */
157     $folders= $this->getMailboxList($folder, "");
158     $folders[]= $folder;
159     
160     foreach ($folders as $subfolder){
162       /* Set shared folder acl's */
163       if (function_exists('imap_getacl')){
165         /* Remove all acl's for this folder */
166         $users= @imap_getacl ($this->mbox, $subfolder);
167         if(is_array($users)){
168           foreach ($users as $userid => $perms){
169             imap_setacl ($this->mbox, $subfolder, $userid, "");
170           }
171         }
172       } else {
173         print_red (_("Warning: imap_getacl is not implemented, can't remove acl informations."));
174       }
176       /* Set permissions for this folder */
177       foreach ($permissions as $user => $acl){
178         imap_setacl ($this->mbox, $subfolder, $user, $acl);
179       }
180     }
182   }
185   function getSharedFolderPermissions($folder)
186   {
187     $result= array();
189     /* imap_getacl available? */
190     if (!function_exists('imap_getacl')){
191       print_red (_("Warning: imap_getacl is not available, can't get imap permissions!"));
192     }
194     /* Get permissions in case of shared folders */
195     else {
196       $users= imap_getacl ($this->mbox, $folder);
198       foreach ($users as $userid => $perms){
199         $result[preg_replace('/^user\./', '', $userid)]= $perms;
200       }
202     }
204     return ($result);
205   }
208   function deleteMailbox($folder)
209   {
210     $cfg= $this->config[$this->gosaMailServer];
211     imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
212     if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
213       print_red(sprintf(_("Can't remove IMAP mailbox. Server says '%s'."), imap_last_error()));
214       return (FALSE);
215     }
216     return (TRUE);
217   }
220   function configureFilter($user, $gosaMailDeliveryMode,
221       $mail, $gosaMailAlternateAddress,
222       $gosaMailMaxSize,
223       $gosaSpamMailbox, $gosaSpamSortLevel,
224       $gosaVacationMessage)
225   {
226     $cfg= $this->config[$this->gosaMailServer];
228     /* Build spamlevel. Spamassassin tags mails with "*" for each integer
229        point of spam. So a spam level of 5.3 gets "*****" which can be
230        checked easily by spam filters */
231     $spamlevel= "";
232     for ($i= 0; $i<$gosaSpamSortLevel; $i++){
233       $spamlevel .= "*";
234     }
236     /* Log into the mail server */
237     $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
238         $cfg["password"], $cfg["admin"]);
240     if (!$sieve->sieve_login()){
241       print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
242             to_string($sieve->error_raw)));
243       return;
244     }
246     /* Load current script from server and remove everything between the comments
247        "###GOSA" */
248     $script= "";
249     if($sieve->sieve_listscripts()){
250       if (in_array("gosa", $sieve->response)){
252         /* get old GOsa script */
253         if(!$sieve->sieve_getscript("gosa")){
254           print_red(sprintf(_("Can't get sieve script. Server says '%s'."), to_string($sieve->error_raw)));
255           return;
256         }
258         foreach ($sieve->response as $line){
259           if (preg_match ("/^###GOSA/", $line)){
260             break;
261           }
262           $line= rtrim($line);
263           if (!preg_match ('/^\s*$/', $line)){
264             $script .= $line."\n";
265           }
266         }
268       }
269     }
271     /* Only create a new one, if it is not empty */
272     if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
273         is_integer(strpos($gosaMailDeliveryMode, "C")) ||
274         !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
275         is_integer(strpos($gosaMailDeliveryMode, "V")) ||
276         is_integer(strpos($gosaMailDeliveryMode, "S"))){
278       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
279       eval ("\$script.=\"$text\";");
280     }
282     /* Add anti-spam code */
283     if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
284       $spambox= $gosaSpamMailbox;
285       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
286       eval ("\$script.=\"$text\";");
287     }
289     /* Add "reject due to mailsize" code, message is currently not
290        adjustable through GOsa. */
291     if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
292       $maxsize= $gosaMailMaxSize;
293       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
294       eval ("\$script.=\"$text\";");
295     }
297     /* Add vacation information */
298     if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
300       /* Sieve wants all destination addresses for the
301          vacation message, so we've to assemble them from
302          mail and mailAlternateAddress */
303       $addrlist= "\"".$mail."\"";
304       foreach ($gosaMailAlternateAddress as $val){
305         $addrlist .= ", \"$val\"";
306       }
307       $vacmsg= $gosaVacationMessage;
308       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
309       eval ("\$script.=\"$text\";");
310     }
312     /* If no local delivery is wanted, tell the script to discard the mail */
313     if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
314       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
315       eval ("\$script.=\"$text\";");
316     }
318     /* Just be aware of null scripts... */
319     if (!isset ($script)){
320       $script= "";
321     }
323     /* Upload script and make it the default one */
324     if (!$sieve->sieve_sendscript("gosa", $script)){
325       print_red(sprintf(_("Can't send sieve script. Server says '%s'."), to_string($sieve->error_raw)));
326       return;
327     }
328     if(!$sieve->sieve_setactivescript("gosa")){
329       print_red(sprintf(_("Can't activate GOsa sieve script. Server says '%s'."), to_string($sieve->error_raw)));
330       return;
331     }
333     $sieve->sieve_logout();
334   }
338 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
339 ?>