b497200748a8a4b0136210458713fcdd59435699
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 function fake_handler($log_level, $log_text, $error_file, $error_line)
22 {
23 }
25 class mailMethodCyrus extends mailMethod
26 {
27 var $mbox= "-";
28 var $config;
29 var $gosaMailServer= "";
31 function mailMethodCyrus($config)
32 {
33 $this->config= $config->data['SERVERS']['IMAP'];
34 }
36 function connect($gosaMailServer)
37 {
38 /* Connect to IMAP server. I don't want to see these warning here... */
39 $this->gosaMailServer= $gosaMailServer;
40 $cfg= $this->config[$gosaMailServer];
42 /* For some reason, hiding errors with @ does not wor here... */
43 $tmp= set_error_handler('fake_handler');
44 $this->mbox = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
45 set_error_handler($tmp);
47 /* Mailbox reachable? */
48 if ($this->mbox === FALSE){
49 print_red (_("Warning: IMAP Server cannot be reached! If you save this account, some mail settings will not be stored on your server!"));
50 return (FALSE);
51 }
52 return (TRUE);
53 }
55 function disconnect()
56 {
57 imap_close ($this->mbox);
58 }
60 function getQuota($folder)
61 {
62 $result= array('quotaUsage' => '', 'gosaMailQuota' => '');
64 /* Load quota settings */
65 error_reporting (0);
66 $quota_value = @imap_get_quota($this->mbox, $folder);
67 if(is_array($quota_value)) {
68 if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])){
69 /* use for PHP >= 4.3 */
70 $result['quotaUsage']= $quota_value["STORAGE"]['usage'];
71 $result['gosaMailQuota']= $quota_value["STORAGE"]['limit'];
72 } else {
73 /* backward compatible */
74 $result['quotaUsage']= $quota_value['usage'];
75 $result['gosaMailQuota']= $quota_value['limit'];
76 }
77 }
78 error_reporting (E_ALL);
80 return ($result);
81 }
83 function getMailboxList($folder, $uid= "")
84 {
85 /* Initialize depending on group or user mode */
86 if ($uid != ""){
87 $result= array("INBOX");
88 } else {
89 $result= array();
90 }
92 /* Get list of mailboxes for combo box */
93 $cfg= $this->config[$this->gosaMailServer];
94 $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder.".*");
95 if (is_array($list)){
96 foreach ($list as $val){
97 $result[]=preg_replace ("/.*user\.".$uid."\./",
98 "INBOX.", imap_utf7_decode ($val));
99 }
100 }
102 return ($result);
103 }
105 function updateMailbox($folder)
106 {
107 /* Check if mailbox exists */
108 $cfg= $this->config[$this->gosaMailServer];
109 $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
110 if ($list === FALSE){
111 if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
112 print_red(sprintf(_("Can't create IMAP mailbox. Server says '%s'."), imap_last_error()));
113 return;
114 }
115 }
116 }
119 function setQuota($folder, $gosaMailQuota)
120 {
121 /* Workaround for the php imap extension */
122 if ($gosaMailQuota == ""){
123 $gosaMailQuota= "0";
124 }
126 /* Write mail quota */
127 if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
128 print_red(sprintf(_("Can't write IMAP quota. Server says '%s'."), imap_last_error()));
129 return (FALSE);
130 }
131 return (TRUE);
132 }
135 function setSharedFolderPermissions($folder, $permissions)
136 {
137 /* Show warning message in case of missing imap_getacl */
138 if (!function_exists('imap_getacl')){
139 print_red (_("Warning: imap_getacl is not implemented, wouldn't modify acl informations."));
140 return;
141 }
143 /* Get list of subfolders */
144 $folders= $this->getMailboxList($folder, "");
145 $folders[]= $folder;
147 foreach ($folders as $subfolder){
149 /* Set shared folder acl's */
150 if (function_exists('imap_getacl')){
152 /* Remove all acl's for this folder */
153 $users= imap_getacl ($this->mbox, $subfolder);
154 foreach ($users as $userid => $perms){
155 imap_setacl ($this->mbox, $subfolder, $userid, "");
156 }
157 }
159 /* Set permissions for this folder */
160 foreach ($permissions as $user => $acl){
161 imap_setacl ($this->mbox, $subfolder, $user, $acl);
162 }
163 }
165 }
168 function getSharedFolderPermissions($folder)
169 {
170 $result= array();
172 /* imap_getacl available? */
173 if (!function_exists('imap_getacl')){
174 print_red (_("Warning: imap_getacl is not available, can't get imap permissions!"));
175 }
177 /* Get permissions in case of shared folders */
178 else {
179 $users= imap_getacl ($this->mbox, $folder);
181 foreach ($users as $userid => $perms){
182 $result[preg_replace('/^user\./', '', $userid)]= $perms;
183 }
185 }
187 return ($result);
188 }
191 function deleteMailbox($folder)
192 {
193 $cfg= $this->config[$this->gosaMailServer];
194 imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
195 if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
196 print_red(sprintf(_("Can't remove IMAP mailbox. Server says '%s'."), imap_last_error()));
197 return (FALSE);
198 }
199 return (TRUE);
200 }
203 function configureFilter($user, $gosaMailDeliveryMode,
204 $mail, $gosaMailAlternateAddress,
205 $gosaMailMaxSize,
206 $gosaSpamMailbox, $gosaSpamSortLevel,
207 $gosaVacationMessage)
208 {
209 $cfg= $this->config[$this->gosaMailServer];
211 /* Build spamlevel. Spamassassin tags mails with "*" for each integer
212 point of spam. So a spam level of 5.3 gets "*****" which can be
213 checked easily by spam filters */
214 $spamlevel= "";
215 for ($i= 0; $i<$gosaSpamSortLevel; $i++){
216 $spamlevel .= "*";
217 }
219 /* Log into the mail server */
220 $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
221 $cfg["password"], $cfg["admin"]);
223 if (!$sieve->sieve_login()){
224 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
225 to_string($sieve->error_raw)));
226 return;
227 }
229 /* Load current script from server and remove everything between the comments
230 "###GOSA" */
231 $script= "";
232 if($sieve->sieve_listscripts()){
233 if (in_array("gosa", $sieve->response)){
235 /* get old GOsa script */
236 if(!$sieve->sieve_getscript("gosa")){
237 print_red(sprintf(_("Can't get sieve script. Server says '%s'."), to_string($sieve->error_raw)));
238 return;
239 }
241 foreach ($sieve->response as $line){
242 if (preg_match ("/^###GOSA/", $line)){
243 break;
244 }
245 $line= rtrim($line);
246 if (!preg_match ('/^\s*$/', $line)){
247 $script .= $line."\n";
248 }
249 }
251 }
252 }
254 /* Only create a new one, if it is not empty */
255 if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
256 is_integer(strpos($gosaMailDeliveryMode, "C")) ||
257 !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
258 is_integer(strpos($gosaMailDeliveryMode, "V")) ||
259 is_integer(strpos($gosaMailDeliveryMode, "S"))){
261 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
262 eval ("\$script.=\"$text\";");
263 }
265 /* Add anti-spam code */
266 if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
267 $spambox= $gosaSpamMailbox;
268 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
269 eval ("\$script.=\"$text\";");
270 }
272 /* Add "reject due to mailsize" code, message is currently not
273 adjustable through GOsa. */
274 if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
275 $maxsize= $gosaMailMaxSize;
276 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
277 eval ("\$script.=\"$text\";");
278 }
280 /* Add vacation information */
281 if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
283 /* Sieve wants all destination addresses for the
284 vacation message, so we've to assemble them from
285 mail and mailAlternateAddress */
286 $addrlist= "\"".$mail."\"";
287 foreach ($gosaMailAlternateAddress as $val){
288 $addrlist .= ", \"$val\"";
289 }
290 $vacmsg= $gosaVacationMessage;
291 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
292 eval ("\$script.=\"$text\";");
293 }
295 /* If no local delivery is wanted, tell the script to discard the mail */
296 if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
297 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
298 eval ("\$script.=\"$text\";");
299 }
301 /* Just be aware of null scripts... */
302 if (!isset ($script)){
303 $script= "";
304 }
306 /* Upload script and make it the default one */
307 if (!$sieve->sieve_sendscript("gosa", $script)){
308 print_red(sprintf(_("Can't send sieve script. Server says '%s'."), to_string($sieve->error_raw)));
309 return;
310 }
311 if(!$sieve->sieve_setactivescript("gosa")){
312 print_red(sprintf(_("Can't activate GOsa sieve script. Server says '%s'."), to_string($sieve->error_raw)));
313 return;
314 }
316 $sieve->sieve_logout();
317 }
319 }
321 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
322 ?>