Code

Removed theme due to beeing not up to date
[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       return(false);
92     }
93  
94     error_reporting (E_ALL);
95     return ($result);
96   }
98   function getMailboxList($folder, $uid= "")
99   {
100     /* Initialize depending on group or user mode */
101     if ($uid != ""){
102       $result= array("INBOX");
103     } else {
104       $result= array();
105     }
107     /* Get list of mailboxes for combo box */
108     $cfg= $this->config[$this->gosaMailServer];
109     $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder.".*");
110     if (is_array($list)){
111       foreach ($list as $val){
112         $result[]=preg_replace ("/.*user\.".$uid."\./",
113             "INBOX.", imap_utf7_decode ($val));
114       }
115     }
117     return ($result);
118   }
120   function updateMailbox($folder)
121   {
122     /* Check if mailbox exists */
123     $cfg= $this->config[$this->gosaMailServer];
124     $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
125     if ($list === FALSE){
126       if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
127         print_red(sprintf(_("Can't create IMAP mailbox. Server says '%s'."), imap_last_error()));
128         return;
129       }
130     }
131   }
134   function setQuota($folder, $gosaMailQuota)
135   {
136     /* Workaround for the php imap extension */
137     if (($gosaMailQuota == "") || ($gosaMailQuota== "2147483647")){
138       $gosaMailQuota= "2147483647";
139     }elseif($gosaMailQuota > 0){
140       $gosaMailQuota = $gosaMailQuota *1024;
141     }
142     
143     
144     /* Write mail quota */
145     if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
146       print_red(sprintf(_("Can't write IMAP quota. Server says '%s'."), imap_last_error()));
147       return (FALSE);
148     }
149     return (TRUE);
150   }
153   function setSharedFolderPermissions($folder, $permissions)
154   {
155     /* Get list of subfolders */
156     $folders= $this->getMailboxList($folder, "");
157     $folders[]= $folder;
158     
159     foreach ($folders as $subfolder){
161       /* Set shared folder acl's */
162       if (function_exists('imap_getacl')){
164         /* Remove all acl's for this folder */
165         $users= @imap_getacl ($this->mbox, $subfolder);
166         if(is_array($users)){
167           foreach ($users as $userid => $perms){
168             imap_setacl ($this->mbox, $subfolder, $userid, "");
169           }
170         }
171       } else {
172         print_red (_("Warning: imap_getacl is not implemented, can't remove acl informations."));
173       }
175       /* Set permissions for this folder */
176       foreach ($permissions as $user => $acl){
177         imap_setacl ($this->mbox, $subfolder, $user, $acl);
178       }
179     }
181   }
184   function getSharedFolderPermissions($folder)
185   {
186     $result= array();
188     /* imap_getacl available? */
189     if (!function_exists('imap_getacl')){
190       print_red (_("Warning: imap_getacl is not available, can't get imap permissions!"));
191     }
193     /* Get permissions in case of shared folders */
194     else {
195       $users= imap_getacl ($this->mbox, $folder);
197       foreach ($users as $userid => $perms){
198         $result[preg_replace('/^user\./', '', $userid)]= $perms;
199       }
201     }
203     return ($result);
204   }
207   function deleteMailbox($folder)
208   {
209     $cfg= $this->config[$this->gosaMailServer];
210     imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
211     if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
212       print_red(sprintf(_("Can't remove IMAP mailbox. Server says '%s'."), imap_last_error()));
213       return (FALSE);
214     }
215     return (TRUE);
216   }
219   function configureFilter($user, $gosaMailDeliveryMode,
220       $mail, $gosaMailAlternateAddress,
221       $gosaMailMaxSize,
222       $gosaSpamMailbox, $gosaSpamSortLevel,
223       $gosaVacationMessage)
224   {
225     $cfg= $this->config[$this->gosaMailServer];
227     /* Build spamlevel. Spamassassin tags mails with "*" for each integer
228        point of spam. So a spam level of 5.3 gets "*****" which can be
229        checked easily by spam filters */
230     $spamlevel= "";
231     for ($i= 0; $i<$gosaSpamSortLevel; $i++){
232       $spamlevel .= "*";
233     }
235     /* Log into the mail server */
236     $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
237         $cfg["password"], $cfg["admin"]);
239     if (!$sieve->sieve_login()){
240       print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
241             to_string($sieve->error_raw)));
242       return;
243     }
245     /* Load current script from server and remove everything between the comments
246        "###GOSA" */
247     $script= "";
248     if($sieve->sieve_listscripts()){
249       if (in_array("gosa", $sieve->response)){
251         /* get old GOsa script */
252         if(!$sieve->sieve_getscript("gosa")){
253           print_red(sprintf(_("Can't get sieve script. Server says '%s'."), to_string($sieve->error_raw)));
254           return;
255         }
257         foreach ($sieve->response as $line){
258           if (preg_match ("/^###GOSA/", $line)){
259             break;
260           }
261           $line= rtrim($line);
262           if (!preg_match ('/^\s*$/', $line)){
263             $script .= $line."\n";
264           }
265         }
267       }
268     }
270     /* Only create a new one, if it is not empty */
271     if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
272         is_integer(strpos($gosaMailDeliveryMode, "C")) ||
273         !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
274         is_integer(strpos($gosaMailDeliveryMode, "V")) ||
275         is_integer(strpos($gosaMailDeliveryMode, "S"))){
277       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
278       eval ("\$script.=\"$text\";");
279     }
281     /* Add anti-spam code */
282     if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
283       $spambox= $gosaSpamMailbox;
284       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
285       eval ("\$script.=\"$text\";");
286     }
288     /* Add "reject due to mailsize" code, message is currently not
289        adjustable through GOsa. */
290     if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
291       $maxsize= $gosaMailMaxSize;
292       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
293       eval ("\$script.=\"$text\";");
294     }
296     /* Add vacation information */
297     if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
299       /* Sieve wants all destination addresses for the
300          vacation message, so we've to assemble them from
301          mail and mailAlternateAddress */
302       $addrlist= "\"".$mail."\"";
303       foreach ($gosaMailAlternateAddress as $val){
304         $addrlist .= ", \"$val\"";
305       }
306       $vacmsg= $gosaVacationMessage;
307       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
308       eval ("\$script.=\"$text\";");
309     }
311     /* If no local delivery is wanted, tell the script to discard the mail */
312     if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
313       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
314       eval ("\$script.=\"$text\";");
315     }
317     /* Just be aware of null scripts... */
318     if (!isset ($script)){
319       $script= "";
320     }
322     /* Upload script and make it the default one */
323     if (!$sieve->sieve_sendscript("gosa", $script)){
324       print_red(sprintf(_("Can't send sieve script. Server says '%s'."), to_string($sieve->error_raw)));
325       return;
326     }
327     if(!$sieve->sieve_setactivescript("gosa")){
328       print_red(sprintf(_("Can't activate GOsa sieve script. Server says '%s'."), to_string($sieve->error_raw)));
329       return;
330     }
332     $sieve->sieve_logout();
333   }
337 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
338 ?>