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 $this->config= $config->data['SERVERS']['IMAP'];
31 }
33 function connect($gosaMailServer)
34 {
35 $cfg=array();
37 /* Connect to IMAP server. I don't want to see these warning here... */
38 $this->gosaMailServer= $gosaMailServer;
39 if (!isset($this->config[$gosaMailServer])){
40 msg_dialog::display(_("Warning"), _("Mail server for this account is invalid!"), WARNING_DIALOG);
41 } else {
42 $cfg= $this->config[$gosaMailServer];
43 }
44 /* For some reason, hiding errors with @ does not wor here... */
45 if(!isset($cfg['connect'])) $cfg['connect']="";
46 if(!isset($cfg['admin'])) $cfg['admin']="";
47 if(!isset($cfg['password'])) $cfg['password']="";
49 /* Setting connect timeout to 10 seconds,
50 else the GOsa UI may freeze for 60 seconds.
51 (PHP default is 'default_socket_timeout = 60') */
52 imap_timeout(1, 10 );
54 $this->mbox = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
56 /* Mailbox reachable? */
57 if ($this->mbox === FALSE){
58 msg_dialog::display(_("IMAP error"), _("Cannot store mail settings on IMAP server!"), ERROR_DIALOG);
59 return (FALSE);
60 }
61 return (TRUE);
62 }
64 function disconnect()
65 {
66 imap_close ($this->mbox);
67 }
69 function getQuota($folder)
70 {
71 $result= array('quotaUsage' => '', 'gosaMailQuota' => '');
73 /* Load quota settings */
74 error_reporting (0);
75 $quota_value = @imap_get_quota($this->mbox, $folder);
76 if(is_array($quota_value)) {
77 if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])){
78 /* use for PHP >= 4.3 */
79 if($quota_value["STORAGE"]['limit'] == 2147483647){
80 $result['quotaUsage']= (int) ($quota_value["STORAGE"]['usage'] / 1024);
81 $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] );
82 }else{
83 $result['quotaUsage']= (int) ($quota_value["STORAGE"]['usage'] / 1024);
84 $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] / 1024);
85 }
86 } else {
87 /* backward icompatible */
88 if($quota_value['usage'] == 2147483647){
89 $result['quotaUsage']= (int) ($quota_value['usage'] / 1024);
90 $result['gosaMailQuota']= (int) ($quota_value['limit'] );
91 }else{
92 $result['quotaUsage']= (int) ($quota_value['usage'] / 1024);
93 $result['gosaMailQuota']= (int) ($quota_value['limit'] / 1024);
94 }
95 }
96 }elseif(!$quota_value){
97 /* If there is no quota defined for this account, the function imap_get_quota returns false. */
98 return(array("quotaUsage"=>"","gosaMailQuota"=>""));
99 }
101 error_reporting (E_ALL | E_STRICT);
102 return ($result);
103 }
106 /* return all folders of the users mailbox*/
107 function getMailboxList($folder, $uid)
108 {
109 global $config;
110 $result = array();
112 /* Get domain an mail address if uid is an mail address */
113 $domain = "";
114 if(preg_match("/@/",$folder)){
115 $domain = "@".preg_replace("/^.*@/","",$folder);
116 $folder = preg_replace("/@.*$/","",$folder);
117 }
119 /* Get list of mailboxes for combo box */
120 $cfg= $this->config[$this->gosaMailServer];
122 /* Create search pattern
123 (user/kekse*@domain.de
124 user.kekse*@domain.de
125 user.kekse* )
126 depending on given folder name) */
127 $q = $folder."*".$domain;
128 $list = imap_listmailbox($this->mbox, $cfg["connect"], $q);
130 /* Create list of returned folder names */
131 if (is_array($list)){
132 foreach ($list as $val){
134 /* Cut domain name */
135 $val = preg_replace("/@.*$/","",$val);
136 $result[]=preg_replace ("/^.*".normalizePreg($folder)."/","INBOX", mb_convert_encoding($val, "UTF-8", "UTF7-IMAP"));
137 }
138 }
140 /* Append "INBOX" to the folder array if result is empty and request comes from user dialog */
141 if(empty($result) && !empty($uid)){
142 $result[] = "INBOX";
143 }
145 return ($result);
146 }
149 function updateMailbox($folder)
150 {
151 /* Check if mailbox exists */
152 $cfg= $this->config[$this->gosaMailServer];
153 $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
154 if ($list === FALSE){
155 if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
156 msg_dialog::display(_("IMAP error"), sprintf(_("Cannot create IMAP mailbox: %s"), '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
157 return;
158 }
159 }
160 }
163 function setQuota($folder, $gosaMailQuota)
164 {
165 /* Workaround for the php imap extension */
166 if (($gosaMailQuota == "") || ($gosaMailQuota== "2147483647")){
167 $gosaMailQuota= "2147483647";
168 }elseif($gosaMailQuota > 0){
169 $gosaMailQuota = $gosaMailQuota *1024;
170 }
173 /* Write mail quota */
174 if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
175 msg_dialog::display(_("IMAP error"), sprintf(_("Cannot modify IMAP mailbox quota: %s"), '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
176 return (FALSE);
177 }
178 return (TRUE);
179 }
182 function setSharedFolderPermissions($folder, $permissions)
183 {
184 /* Get list of subfolders */
185 $folders= $this->getMailboxList($folder, "");
186 $folders[]= $folder;
188 foreach ($folders as $subfolder){
190 /* Set shared folder acl's */
191 if (function_exists('imap_getacl')){
193 /* Remove all acl's for this folder */
194 $users= @imap_getacl ($this->mbox, $subfolder);
195 if(is_array($users)){
196 foreach ($users as $userid => $perms){
197 imap_setacl ($this->mbox, $subfolder, $userid, "");
198 }
199 }
200 } else {
201 msg_dialog::display(_("Internal error"), _("Cannot remove IMAP ACL: imap_getacl not implemented!"), ERROR_DIALOG);
202 }
204 /* Set permissions for this folder */
205 foreach ($permissions as $user => $acl){
206 imap_setacl ($this->mbox, $subfolder, $user, $acl);
207 }
208 }
210 }
213 function getSharedFolderPermissions($folder)
214 {
215 $result= array();
217 /* imap_getacl available? */
218 if (!function_exists('imap_getacl')){
219 msg_dialog::display(_("Internal error"), _("Cannot retrieve IMAP ACL: imap_getacl not implemented!"), ERROR_DIALOG);
220 }
222 /* Get permissions in case of shared folders */
223 else {
224 $users= imap_getacl ($this->mbox, $folder);
226 foreach ($users as $userid => $perms){
227 $result[preg_replace('/^user\./', '', $userid)]= $perms;
228 }
230 }
232 return ($result);
233 }
236 function deleteMailbox($folder)
237 {
238 $cfg= $this->config[$this->gosaMailServer];
239 imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
240 if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
241 msg_dialog::display(_("IMAP error"), sprintf(_('Cannot remove IMAP mailbox: %s'), '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
242 return (FALSE);
243 }
244 return (TRUE);
245 }
248 function configureFilter($user, $gosaMailDeliveryMode,
249 $mail, $gosaMailAlternateAddress,
250 $gosaMailMaxSize,
251 $gosaSpamMailbox, $gosaSpamSortLevel,
252 $gosaVacationMessage)
253 {
254 $cfg= $this->config[$this->gosaMailServer];
256 /* Build spamlevel. Spamassassin tags mails with "*" for each integer
257 point of spam. So a spam level of 5.3 gets "*****" which can be
258 checked easily by spam filters */
259 $spamlevel= "";
260 for ($i= 0; $i<$gosaSpamSortLevel; $i++){
261 $spamlevel .= "*";
262 }
264 /* Log into the mail server */
265 $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
266 $cfg["password"], $cfg["admin"]);
268 if (!$sieve->sieve_login()){
269 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot log into SIEVE server: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
270 return;
271 }
273 /* Load current script from server and remove everything between the comments
274 "###GOSA" */
275 $script= "";
276 if($sieve->sieve_listscripts()){
277 if (in_array("gosa", $sieve->response)){
279 /* get old GOsa script */
280 if(!$sieve->sieve_getscript("gosa")){
281 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot retrieve SIEVE script: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
282 return;
283 }
285 foreach ($sieve->response as $line){
286 if (preg_match ("/^###GOSA/", $line)){
287 break;
288 }
289 $line= rtrim($line);
290 if (!preg_match ('/^\s*$/', $line)){
291 $script .= $line."\n";
292 }
293 }
295 }
296 }
298 /* Only create a new one, if it is not empty */
299 if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
300 is_integer(strpos($gosaMailDeliveryMode, "C")) ||
301 !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
302 is_integer(strpos($gosaMailDeliveryMode, "V")) ||
303 is_integer(strpos($gosaMailDeliveryMode, "S"))){
305 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
306 eval ("\$script.=\"$text\";");
307 }
309 /* Add anti-spam code */
310 if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
311 $spambox= $gosaSpamMailbox;
312 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
313 eval ("\$script.=\"$text\";");
314 }
316 /* Add "reject due to mailsize" code, message is currently not
317 adjustable through GOsa. */
318 if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
319 $maxsize= $gosaMailMaxSize;
320 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
321 eval ("\$script.=\"$text\";");
322 }
324 /* Add vacation information */
325 if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
327 /* Sieve wants all destination addresses for the
328 vacation message, so we've to assemble them from
329 mail and mailAlternateAddress */
330 $addrlist= "\"".$mail."\"";
331 foreach ($gosaMailAlternateAddress as $val){
332 $addrlist .= ", \"$val\"";
333 }
334 $vacmsg= $gosaVacationMessage;
335 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
336 eval ("\$script.=\"$text\";");
337 }
339 /* If no local delivery is wanted, tell the script to discard the mail */
340 if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
341 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
342 eval ("\$script.=\"$text\";");
343 }
345 /* Just be aware of null scripts... */
346 if (!isset ($script)){
347 $script= "";
348 }
350 /* Upload script and make it the default one */
351 if (!$sieve->sieve_sendscript("gosa", $script)){
352 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot store SIEVE script: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
353 return;
354 }
355 if(!$sieve->sieve_setactivescript("gosa")){
356 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot activate SIEVE script: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
357 return;
358 }
360 $sieve->sieve_logout();
361 }
363 }
365 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
366 ?>