Code

Updated mail methods.
[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= "";
26 var $uattrib= "uid";
28   function mailMethodCyrus($config)
29   {
30     /* Check if the mail account identification attribute
31        is overridden in the configuration file
32      */
33     if(isset($config->current['MAIL_ATTRIB'])){
34       $this->uattrib = $config->current['MAIL_ATTRIB'];
35     }
37     $this->config= $config->data['SERVERS']['IMAP'];
38   }
40   function connect($gosaMailServer)
41   {
42     $cfg=array();
44     /* Connect to IMAP server. I don't want to see these warning here... */
45     $this->gosaMailServer= $gosaMailServer;
46     if (!isset($this->config[$gosaMailServer])){
47       print_red(_("Warning: Account has an invalid mailserver string!"));
48     } else {
49       $cfg= $this->config[$gosaMailServer];
50     }
51     /* For some reason, hiding errors with @ does not wor here... */
52     if(!isset($cfg['connect']))   $cfg['connect']="";
53     if(!isset($cfg['admin']))     $cfg['admin']="";
54     if(!isset($cfg['password']))  $cfg['password']="";
56     $this->mbox = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
58     /* Mailbox reachable? */
59     if ($this->mbox === FALSE){
60       print_red (_("Warning: IMAP Server cannot be reached! If you save this account, some mail settings will not be stored on your server!"));
61       return (FALSE);
62     }
63     return (TRUE);
64   }
66   function disconnect()
67   {
68     imap_close ($this->mbox);
69   }
71   function getQuota($folder)
72   {
73     $result= array('quotaUsage' => '', 'gosaMailQuota' => '');
75     /* Load quota settings */
76     error_reporting (0);
77     $quota_value = @imap_get_quota($this->mbox, $folder);
78     if(is_array($quota_value)) {
79       if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])){
80         /* use for PHP >= 4.3 */
81         if($quota_value["STORAGE"]['limit'] == 2147483647){
82           $result['quotaUsage']=    (int) ($quota_value["STORAGE"]['usage'] / 1024);
83           $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] );
84         }else{
85           $result['quotaUsage']=    (int) ($quota_value["STORAGE"]['usage'] / 1024);
86           $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] / 1024);
87         }
88       } else {
89         /* backward icompatible */
90         if($quota_value['usage'] == 2147483647){
91           $result['quotaUsage']=    (int) ($quota_value['usage'] / 1024);
92           $result['gosaMailQuota']= (int) ($quota_value['limit'] );
93         }else{
94           $result['quotaUsage']=    (int) ($quota_value['usage'] / 1024);
95           $result['gosaMailQuota']= (int) ($quota_value['limit'] / 1024);
96         }
97       }
98     }elseif(!$quota_value){
99       /* If there is no quota defined for this account, the function imap_get_quota returns false. */
100       return(array("quotaUsage"=>"","gosaMailQuota"=>""));
101     }
102  
103     error_reporting (E_ALL);
104     return ($result);
105   }
108   /* return all folders of the users mailbox*/
109   function getMailboxList($folder, $uid= "")
110   {
111     global $config;
112     $result = array();
114     /* Get domain an mail address if uid is an mail address */
115     $domain = "";
116     if(preg_match("/@/",$folder)){
117       $domain = "@".preg_replace("/^.*@/","",$folder);
118       $folder = preg_replace("/@.*$/","",$folder);
119     }
121     /* Get list of mailboxes for combo box */
122     $cfg= $this->config[$this->gosaMailServer];
124     /* Create search pattern
125          (user/kekse*@domain.de
126           user.kekse*@domain.de
127           user.kekse*  )
128        depending on given folder name) */
129     $q = $folder."*".$domain;
130     $list = imap_listmailbox($this->mbox, $cfg["connect"], $q);
132     /* Create list of returned folder names */
133     if (is_array($list)){
134       foreach ($list as $val){
136         /* Cut domain name */
137         $val = preg_replace("/@.*$/","",$val);
138         $result[]=preg_replace ("/^.*".normalizePreg($folder)."/","INBOX", mb_convert_encoding($val, "UTF-8", "UTF7-IMAP"));
139       }
140     }
142     /* Append "INBOX" to the folder array if result is empty and request comes from user dialog */
143     if(empty($result) && !empty($uid)){
144       $result[] = "INBOX";
145     }
147     return ($result);
148   }
151   function updateMailbox($folder)
152   {
153     /* Check if mailbox exists */
154     $cfg= $this->config[$this->gosaMailServer];
155     $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
156     if ($list === FALSE){
157       if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
158         print_red(sprintf(_("Can't create IMAP mailbox. Server says '%s'."), imap_last_error()));
159         return;
160       }
161     }
162   }
165   function setQuota($folder, $gosaMailQuota)
166   {
167     /* Workaround for the php imap extension */
168     if (($gosaMailQuota == "") || ($gosaMailQuota== "2147483647")){
169       $gosaMailQuota= "2147483647";
170     }elseif($gosaMailQuota > 0){
171       $gosaMailQuota = $gosaMailQuota *1024;
172     }
173     
174     
175     /* Write mail quota */
176     if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
177       print_red(sprintf(_("Can't write IMAP quota. Server says '%s'."), imap_last_error()));
178       return (FALSE);
179     }
180     return (TRUE);
181   }
184   function setSharedFolderPermissions($folder, $permissions)
185   {
186     /* Get list of subfolders */
187     $folders= $this->getMailboxList($folder, "");
188     $folders[]= $folder;
189     
190     foreach ($folders as $subfolder){
192       /* Set shared folder acl's */
193       if (function_exists('imap_getacl')){
195         /* Remove all acl's for this folder */
196         $users= @imap_getacl ($this->mbox, $subfolder);
197         if(is_array($users)){
198           foreach ($users as $userid => $perms){
199             imap_setacl ($this->mbox, $subfolder, $userid, "");
200           }
201         }
202       } else {
203         print_red (_("Warning: imap_getacl is not implemented, can't remove acl informations."));
204       }
206       /* Set permissions for this folder */
207       foreach ($permissions as $user => $acl){
208         imap_setacl ($this->mbox, $subfolder, $user, $acl);
209       }
210     }
212   }
215   function getSharedFolderPermissions($folder)
216   {
217     $result= array();
219     /* imap_getacl available? */
220     if (!function_exists('imap_getacl')){
221       print_red (_("Warning: imap_getacl is not available, can't get imap permissions!"));
222     }
224     /* Get permissions in case of shared folders */
225     else {
226       $users= imap_getacl ($this->mbox, $folder);
228       foreach ($users as $userid => $perms){
229         $result[preg_replace('/^user\./', '', $userid)]= $perms;
230       }
232     }
234     return ($result);
235   }
238   function deleteMailbox($folder)
239   {
240     $cfg= $this->config[$this->gosaMailServer];
241     imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
242     if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
243       print_red(sprintf(_("Can't remove IMAP mailbox. Server says '%s'."), imap_last_error()));
244       return (FALSE);
245     }
246     return (TRUE);
247   }
250   function configureFilter($user, $gosaMailDeliveryMode,
251       $mail, $gosaMailAlternateAddress,
252       $gosaMailMaxSize,
253       $gosaSpamMailbox, $gosaSpamSortLevel,
254       $gosaVacationMessage)
255   {
256     $cfg= $this->config[$this->gosaMailServer];
258     /* Build spamlevel. Spamassassin tags mails with "*" for each integer
259        point of spam. So a spam level of 5.3 gets "*****" which can be
260        checked easily by spam filters */
261     $spamlevel= "";
262     for ($i= 0; $i<$gosaSpamSortLevel; $i++){
263       $spamlevel .= "*";
264     }
266     /* Log into the mail server */
267     $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
268         $cfg["password"], $cfg["admin"]);
270     if (!$sieve->sieve_login()){
271       print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
272             to_string($sieve->error_raw)));
273       return;
274     }
276     /* Load current script from server and remove everything between the comments
277        "###GOSA" */
278     $script= "";
279     if($sieve->sieve_listscripts()){
280       if (in_array("gosa", $sieve->response)){
282         /* get old GOsa script */
283         if(!$sieve->sieve_getscript("gosa")){
284           print_red(sprintf(_("Can't get sieve script. Server says '%s'."), to_string($sieve->error_raw)));
285           return;
286         }
288         foreach ($sieve->response as $line){
289           if (preg_match ("/^###GOSA/", $line)){
290             break;
291           }
292           $line= rtrim($line);
293           if (!preg_match ('/^\s*$/', $line)){
294             $script .= $line."\n";
295           }
296         }
298       }
299     }
301     /* Only create a new one, if it is not empty */
302     if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
303         is_integer(strpos($gosaMailDeliveryMode, "C")) ||
304         !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
305         is_integer(strpos($gosaMailDeliveryMode, "V")) ||
306         is_integer(strpos($gosaMailDeliveryMode, "S"))){
308       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
309       eval ("\$script.=\"$text\";");
310     }
312     /* Add anti-spam code */
313     if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
314       $spambox= $gosaSpamMailbox;
315       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
316       eval ("\$script.=\"$text\";");
317     }
319     /* Add "reject due to mailsize" code, message is currently not
320        adjustable through GOsa. */
321     if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
322       $maxsize= $gosaMailMaxSize;
323       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
324       eval ("\$script.=\"$text\";");
325     }
327     /* Add vacation information */
328     if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
330       /* Sieve wants all destination addresses for the
331          vacation message, so we've to assemble them from
332          mail and mailAlternateAddress */
333       $addrlist= "\"".$mail."\"";
334       foreach ($gosaMailAlternateAddress as $val){
335         $addrlist .= ", \"$val\"";
336       }
337       $vacmsg= $gosaVacationMessage;
338       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
339       eval ("\$script.=\"$text\";");
340     }
342     /* If no local delivery is wanted, tell the script to discard the mail */
343     if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
344       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
345       eval ("\$script.=\"$text\";");
346     }
348     /* Just be aware of null scripts... */
349     if (!isset ($script)){
350       $script= "";
351     }
353     /* Upload script and make it the default one */
354     if (!$sieve->sieve_sendscript("gosa", $script)){
355       print_red(sprintf(_("Can't send sieve script. Server says '%s'."), to_string($sieve->error_raw)));
356       return;
357     }
358     if(!$sieve->sieve_setactivescript("gosa")){
359       print_red(sprintf(_("Can't activate GOsa sieve script. Server says '%s'."), to_string($sieve->error_raw)));
360       return;
361     }
363     $sieve->sieve_logout();
364   }
368 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
369 ?>