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 /* Connect to IMAP server. I don't want to see these warning here... */
35 $this->gosaMailServer= $gosaMailServer;
36 if (!isset($this->config[$gosaMailServer])){
37 print_red(_("Warning: Account has an invalid mailserver string! Please check the mail server settings in the mail tab."));
38 } else {
39 $cfg= $this->config[$gosaMailServer];
40 }
42 /* For some reason, hiding errors with @ does not wor here... */
43 $this->mbox = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
45 /* Mailbox reachable? */
46 if ($this->mbox === FALSE){
47 print_red (_("Warning: IMAP Server cannot be reached! If you save this account, some mail settings will not be stored on your server!"));
48 return (FALSE);
49 }
50 return (TRUE);
51 }
53 function disconnect()
54 {
55 imap_close ($this->mbox);
56 }
58 function getQuota($folder)
59 {
60 $result= array('quotaUsage' => '', 'gosaMailQuota' => '');
62 /* Load quota settings */
63 error_reporting (0);
64 $quota_value = @imap_get_quota($this->mbox, $folder);
65 if(is_array($quota_value)) {
66 if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])){
67 /* use for PHP >= 4.3 */
68 $result['quotaUsage']= $quota_value["STORAGE"]['usage'];
69 $result['gosaMailQuota']= $quota_value["STORAGE"]['limit'];
70 } else {
71 /* backward compatible */
72 $result['quotaUsage']= $quota_value['usage'];
73 $result['gosaMailQuota']= $quota_value['limit'];
74 }
75 }
76 error_reporting (E_ALL);
78 return ($result);
79 }
81 function getMailboxList($folder, $uid= "")
82 {
83 /* Initialize depending on group or user mode */
84 if ($uid != ""){
85 $result= array("INBOX");
86 } else {
87 $result= array();
88 }
90 /* Get list of mailboxes for combo box */
91 $cfg= $this->config[$this->gosaMailServer];
92 $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder.".*");
93 if (is_array($list)){
94 foreach ($list as $val){
95 $result[]=preg_replace ("/.*user\.".$uid."\./",
96 "INBOX.", imap_utf7_decode ($val));
97 }
98 }
100 return ($result);
101 }
103 function updateMailbox($folder)
104 {
105 /* Check if mailbox exists */
106 $cfg= $this->config[$this->gosaMailServer];
107 $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
108 if ($list === FALSE){
109 if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
110 print_red(sprintf(_("Can't create IMAP mailbox. Server says '%s'."), imap_last_error()));
111 return;
112 }
113 }
114 }
117 function setQuota($folder, $gosaMailQuota)
118 {
119 /* Workaround for the php imap extension */
120 if ($gosaMailQuota == ""){
121 $gosaMailQuota= "0";
122 }
124 /* Write mail quota */
125 if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
126 print_red(sprintf(_("Can't write IMAP quota. Server says '%s'."), imap_last_error()));
127 return (FALSE);
128 }
129 return (TRUE);
130 }
133 function setSharedFolderPermissions($folder, $permissions)
134 {
135 /* Show warning message in case of missing imap_getacl */
136 if (!function_exists('imap_getacl')){
137 print_red (_("Warning: imap_getacl is not implemented, wouldn't modify acl informations."));
138 return;
139 }
141 /* Get list of subfolders */
142 $folders= $this->getMailboxList($folder, "");
143 $folders[]= $folder;
145 foreach ($folders as $subfolder){
147 /* Set shared folder acl's */
148 if (function_exists('imap_getacl')){
150 /* Remove all acl's for this folder */
151 $users= imap_getacl ($this->mbox, $subfolder);
152 foreach ($users as $userid => $perms){
153 imap_setacl ($this->mbox, $subfolder, $userid, "");
154 }
155 }
157 /* Set permissions for this folder */
158 foreach ($permissions as $user => $acl){
159 imap_setacl ($this->mbox, $subfolder, $user, $acl);
160 }
161 }
163 }
166 function getSharedFolderPermissions($folder)
167 {
168 $result= array();
170 /* imap_getacl available? */
171 if (!function_exists('imap_getacl')){
172 print_red (_("Warning: imap_getacl is not available, can't get imap permissions!"));
173 }
175 /* Get permissions in case of shared folders */
176 else {
177 $users= imap_getacl ($this->mbox, $folder);
179 foreach ($users as $userid => $perms){
180 $result[preg_replace('/^user\./', '', $userid)]= $perms;
181 }
183 }
185 return ($result);
186 }
189 function deleteMailbox($folder)
190 {
191 $cfg= $this->config[$this->gosaMailServer];
192 imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
193 if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
194 print_red(sprintf(_("Can't remove IMAP mailbox. Server says '%s'."), imap_last_error()));
195 return (FALSE);
196 }
197 return (TRUE);
198 }
201 function configureFilter($user, $gosaMailDeliveryMode,
202 $mail, $gosaMailAlternateAddress,
203 $gosaMailMaxSize,
204 $gosaSpamMailbox, $gosaSpamSortLevel,
205 $gosaVacationMessage)
206 {
207 $cfg= $this->config[$this->gosaMailServer];
209 /* Build spamlevel. Spamassassin tags mails with "*" for each integer
210 point of spam. So a spam level of 5.3 gets "*****" which can be
211 checked easily by spam filters */
212 $spamlevel= "";
213 for ($i= 0; $i<$gosaSpamSortLevel; $i++){
214 $spamlevel .= "*";
215 }
217 /* Log into the mail server */
218 $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
219 $cfg["password"], $cfg["admin"]);
221 if (!$sieve->sieve_login()){
222 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
223 to_string($sieve->error_raw)));
224 return;
225 }
227 /* Load current script from server and remove everything between the comments
228 "###GOSA" */
229 $script= "";
230 if($sieve->sieve_listscripts()){
231 if (in_array("gosa", $sieve->response)){
233 /* get old GOsa script */
234 if(!$sieve->sieve_getscript("gosa")){
235 print_red(sprintf(_("Can't get sieve script. Server says '%s'."), to_string($sieve->error_raw)));
236 return;
237 }
239 foreach ($sieve->response as $line){
240 if (preg_match ("/^###GOSA/", $line)){
241 break;
242 }
243 $line= rtrim($line);
244 if (!preg_match ('/^\s*$/', $line)){
245 $script .= $line."\n";
246 }
247 }
249 }
250 }
252 /* Only create a new one, if it is not empty */
253 if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
254 is_integer(strpos($gosaMailDeliveryMode, "C")) ||
255 !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
256 is_integer(strpos($gosaMailDeliveryMode, "V")) ||
257 is_integer(strpos($gosaMailDeliveryMode, "S"))){
259 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
260 eval ("\$script.=\"$text\";");
261 }
263 /* Add anti-spam code */
264 if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
265 $spambox= $gosaSpamMailbox;
266 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
267 eval ("\$script.=\"$text\";");
268 }
270 /* Add "reject due to mailsize" code, message is currently not
271 adjustable through GOsa. */
272 if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
273 $maxsize= $gosaMailMaxSize;
274 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
275 eval ("\$script.=\"$text\";");
276 }
278 /* Add vacation information */
279 if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
281 /* Sieve wants all destination addresses for the
282 vacation message, so we've to assemble them from
283 mail and mailAlternateAddress */
284 $addrlist= "\"".$mail."\"";
285 foreach ($gosaMailAlternateAddress as $val){
286 $addrlist .= ", \"$val\"";
287 }
288 $vacmsg= $gosaVacationMessage;
289 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
290 eval ("\$script.=\"$text\";");
291 }
293 /* If no local delivery is wanted, tell the script to discard the mail */
294 if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
295 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
296 eval ("\$script.=\"$text\";");
297 }
299 /* Just be aware of null scripts... */
300 if (!isset ($script)){
301 $script= "";
302 }
304 /* Upload script and make it the default one */
305 if (!$sieve->sieve_sendscript("gosa", $script)){
306 print_red(sprintf(_("Can't send sieve script. Server says '%s'."), to_string($sieve->error_raw)));
307 return;
308 }
309 if(!$sieve->sieve_setactivescript("gosa")){
310 print_red(sprintf(_("Can't activate GOsa sieve script. Server says '%s'."), to_string($sieve->error_raw)));
311 return;
312 }
314 $sieve->sieve_logout();
315 }
317 }
319 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
320 ?>