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