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