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