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($config->get_cfg_value("mail_attrib") != ""){
34 $new_uattrib= strtolower($config->get_cfg_value("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 msg_dialog::display(_("Warning"), _("Mail server for this account is invalid!"), WARNING_DIALOG);
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 /* Setting connect timeout to 10 seconds,
62 else the GOsa UI may freeze for 60 seconds.
63 (PHP default is 'default_socket_timeout = 60') */
64 imap_timeout(1, 10 );
66 $this->mbox = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
68 /* Mailbox reachable? */
69 if ($this->mbox === FALSE){
70 msg_dialog::display(_("IMAP error"), _("Cannot store mail settings on IMAP server!"), ERROR_DIALOG);
71 return (FALSE);
72 }
73 return (TRUE);
74 }
76 function disconnect()
77 {
78 imap_close ($this->mbox);
79 }
81 function getQuota($folder)
82 {
83 $result= array('quotaUsage' => '', 'gosaMailQuota' => '');
85 /* Load quota settings */
86 error_reporting (0);
87 $quota_value = @imap_get_quota($this->mbox, $folder);
88 if(is_array($quota_value)) {
89 if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])){
90 /* use for PHP >= 4.3 */
91 if($quota_value["STORAGE"]['limit'] == 2147483647){
92 $result['quotaUsage']= (int) ($quota_value["STORAGE"]['usage'] / 1024);
93 $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] );
94 }else{
95 $result['quotaUsage']= (int) ($quota_value["STORAGE"]['usage'] / 1024);
96 $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] / 1024);
97 }
98 } else {
99 /* backward icompatible */
100 if($quota_value['usage'] == 2147483647){
101 $result['quotaUsage']= (int) ($quota_value['usage'] / 1024);
102 $result['gosaMailQuota']= (int) ($quota_value['limit'] );
103 }else{
104 $result['quotaUsage']= (int) ($quota_value['usage'] / 1024);
105 $result['gosaMailQuota']= (int) ($quota_value['limit'] / 1024);
106 }
107 }
108 }elseif(!$quota_value){
109 /* If there is no quota defined for this account, the function imap_get_quota returns false. */
110 return(array("quotaUsage"=>"","gosaMailQuota"=>""));
111 }
113 error_reporting (E_ALL | E_STRICT);
114 return ($result);
115 }
118 /* return all folders of the users mailbox*/
119 function getMailboxList($folder, $uid)
120 {
121 global $config;
122 $result = array();
124 /* Get domain an mail address if uid is an mail address */
125 $domain = "";
126 if(preg_match("/@/",$folder)){
127 $domain = "@".preg_replace("/^.*@/","",$folder);
128 $folder = preg_replace("/@.*$/","",$folder);
129 }
131 /* Get list of mailboxes for combo box */
132 $cfg= $this->config[$this->gosaMailServer];
134 /* Create search pattern
135 (user/kekse*@domain.de
136 user.kekse*@domain.de
137 user.kekse* )
138 depending on given folder name) */
139 $q = $folder."*".$domain;
140 $list = imap_listmailbox($this->mbox, $cfg["connect"], $q);
142 /* Create list of returned folder names */
143 if (is_array($list)){
144 foreach ($list as $val){
146 /* Cut domain name */
147 $val = preg_replace("/@.*$/","",$val);
148 $result[]=preg_replace ("/^.*".normalizePreg($folder)."/","INBOX", mb_convert_encoding($val, "UTF-8", "UTF7-IMAP"));
149 }
150 }
152 /* Append "INBOX" to the folder array if result is empty and request comes from user dialog */
153 if(empty($result) && !empty($uid)){
154 $result[] = "INBOX";
155 }
157 return ($result);
158 }
161 function updateMailbox($folder)
162 {
163 /* Check if mailbox exists */
164 $cfg= $this->config[$this->gosaMailServer];
165 $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
166 if ($list === FALSE){
167 if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
168 msg_dialog::display(_("IMAP error"), sprintf(_("Cannot create IMAP mailbox: %s"), '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
169 return;
170 }
171 }
172 }
175 function setQuota($folder, $gosaMailQuota)
176 {
177 /* Workaround for the php imap extension */
178 if (($gosaMailQuota == "") || ($gosaMailQuota== "2147483647")){
179 $gosaMailQuota= "2147483647";
180 }elseif($gosaMailQuota > 0){
181 $gosaMailQuota = $gosaMailQuota *1024;
182 }
185 /* Write mail quota */
186 if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
187 msg_dialog::display(_("IMAP error"), sprintf(_("Cannot modify IMAP mailbox quota: %s"), '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
188 return (FALSE);
189 }
190 return (TRUE);
191 }
194 function setSharedFolderPermissions($folder, $permissions)
195 {
196 /* Get list of subfolders */
197 $folders= $this->getMailboxList($folder, "");
198 $folders[]= $folder;
200 foreach ($folders as $subfolder){
202 /* Set shared folder acl's */
203 if (function_exists('imap_getacl')){
205 /* Remove all acl's for this folder */
206 $users= @imap_getacl ($this->mbox, $subfolder);
207 if(is_array($users)){
208 foreach ($users as $userid => $perms){
209 imap_setacl ($this->mbox, $subfolder, $userid, "");
210 }
211 }
212 } else {
213 msg_dialog::display(_("Internal error"), _("Cannot remove IMAP ACL: imap_getacl not implemented!"), ERROR_DIALOG);
214 }
216 /* Set permissions for this folder */
217 foreach ($permissions as $user => $acl){
218 imap_setacl ($this->mbox, $subfolder, $user, $acl);
219 }
220 }
222 }
225 function getSharedFolderPermissions($folder)
226 {
227 $result= array();
229 /* imap_getacl available? */
230 if (!function_exists('imap_getacl')){
231 msg_dialog::display(_("Internal error"), _("Cannot retrieve IMAP ACL: imap_getacl not implemented!"), ERROR_DIALOG);
232 }
234 /* Get permissions in case of shared folders */
235 else {
236 $users= imap_getacl ($this->mbox, $folder);
238 foreach ($users as $userid => $perms){
239 $result[preg_replace('/^user\./', '', $userid)]= $perms;
240 }
242 }
244 return ($result);
245 }
248 function deleteMailbox($folder)
249 {
250 $cfg= $this->config[$this->gosaMailServer];
251 imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
252 if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
253 msg_dialog::display(_("IMAP error"), sprintf(_('Cannot remove IMAP mailbox: %s'), '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
254 return (FALSE);
255 }
256 return (TRUE);
257 }
260 function configureFilter($user, $gosaMailDeliveryMode,
261 $mail, $gosaMailAlternateAddress,
262 $gosaMailMaxSize,
263 $gosaSpamMailbox, $gosaSpamSortLevel,
264 $gosaVacationMessage)
265 {
266 $cfg= $this->config[$this->gosaMailServer];
268 /* Build spamlevel. Spamassassin tags mails with "*" for each integer
269 point of spam. So a spam level of 5.3 gets "*****" which can be
270 checked easily by spam filters */
271 $spamlevel= "";
272 for ($i= 0; $i<$gosaSpamSortLevel; $i++){
273 $spamlevel .= "*";
274 }
276 /* Log into the mail server */
277 $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
278 $cfg["password"], $cfg["admin"],$cfg["sieve_option"]);
280 if (!$sieve->sieve_login()){
281 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot log into SIEVE server: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
282 return;
283 }
285 /* Load current script from server and remove everything between the comments
286 "###GOSA" */
287 $script= "";
288 if($sieve->sieve_listscripts()){
289 if (in_array("gosa", $sieve->response)){
291 /* get old GOsa script */
292 if(!$sieve->sieve_getscript("gosa")){
293 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot retrieve SIEVE script: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
294 return;
295 }
297 foreach ($sieve->response as $line){
298 if (preg_match ("/^###GOSA/", $line)){
299 break;
300 }
301 $line= rtrim($line);
302 if (!preg_match ('/^\s*$/', $line)){
303 $script .= $line."\n";
304 }
305 }
307 }
308 }
310 /* Only create a new one, if it is not empty */
311 if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
312 is_integer(strpos($gosaMailDeliveryMode, "C")) ||
313 !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
314 is_integer(strpos($gosaMailDeliveryMode, "V")) ||
315 is_integer(strpos($gosaMailDeliveryMode, "S"))){
317 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
318 eval ("\$script.=\"$text\";");
319 }
321 /* Add anti-spam code */
322 if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
323 $spambox= $gosaSpamMailbox;
324 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
325 eval ("\$script.=\"$text\";");
326 }
328 /* Add "reject due to mailsize" code, message is currently not
329 adjustable through GOsa. */
330 if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
331 $maxsize= $gosaMailMaxSize;
332 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
333 eval ("\$script.=\"$text\";");
334 }
336 /* Add vacation information */
337 if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
339 /* Sieve wants all destination addresses for the
340 vacation message, so we've to assemble them from
341 mail and mailAlternateAddress */
342 $addrlist= "\"".$mail."\"";
343 foreach ($gosaMailAlternateAddress as $val){
344 $addrlist .= ", \"$val\"";
345 }
346 $vacmsg= $gosaVacationMessage;
347 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
348 eval ("\$script.=\"$text\";");
349 }
351 /* If no local delivery is wanted, tell the script to discard the mail */
352 if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
353 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
354 eval ("\$script.=\"$text\";");
355 }
357 /* Just be aware of null scripts... */
358 if (!isset ($script)){
359 $script= "";
360 }
362 /* Upload script and make it the default one */
363 if (!$sieve->sieve_sendscript("gosa", $script)){
364 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot store SIEVE script: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
365 return;
366 }
367 if(!$sieve->sieve_setactivescript("gosa")){
368 msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot activate SIEVE script: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
369 return;
370 }
372 $sieve->sieve_logout();
373 }
375 }
377 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
378 ?>