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 print_red(_("Warning: Account has an invalid mailserver string!"));
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 $this->mbox = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
51 /* Mailbox reachable? */
52 if ($this->mbox === FALSE){
53 print_red (_("Warning: IMAP Server cannot be reached! If you save this account, some mail settings will not be stored on your server!"));
54 return (FALSE);
55 }
56 return (TRUE);
57 }
59 function disconnect()
60 {
61 imap_close ($this->mbox);
62 }
64 function getQuota($folder)
65 {
66 $result= array('quotaUsage' => '', 'gosaMailQuota' => '');
68 /* Load quota settings */
69 error_reporting (0);
70 $quota_value = @imap_get_quota($this->mbox, $folder);
71 if(is_array($quota_value)) {
72 if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])){
73 /* use for PHP >= 4.3 */
74 if($quota_value["STORAGE"]['limit'] == 2147483647){
75 $result['quotaUsage']= (int) ($quota_value["STORAGE"]['usage'] / 1024);
76 $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] );
77 }else{
78 $result['quotaUsage']= (int) ($quota_value["STORAGE"]['usage'] / 1024);
79 $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] / 1024);
80 }
81 } else {
82 /* backward icompatible */
83 if($quota_value['usage'] == 2147483647){
84 $result['quotaUsage']= (int) ($quota_value['usage'] / 1024);
85 $result['gosaMailQuota']= (int) ($quota_value['limit'] );
86 }else{
87 $result['quotaUsage']= (int) ($quota_value['usage'] / 1024);
88 $result['gosaMailQuota']= (int) ($quota_value['limit'] / 1024);
89 }
90 }
91 }elseif(!$quota_value){
92 /* If there is no quota defined for this account, the function imap_get_quota returns false. */
93 return(array("quotaUsage"=>"","gosaMailQuota"=>""));
94 }
96 error_reporting (E_ALL);
97 return ($result);
98 }
101 /* return all folders of the users mailbox*/
102 function getMailboxList($folder, $uid= "")
103 {
104 global $config;
105 $result = array();
107 /* Get domain an mail address if uid is an mail address */
108 $domain = "";
109 if(preg_match("/@/",$folder)){
110 $domain = "@".preg_replace("/^.*@/","",$folder);
111 $folder = preg_replace("/@.*$/","",$folder);
112 }
114 /* Get list of mailboxes for combo box */
115 $cfg= $this->config[$this->gosaMailServer];
117 /* Create search pattern
118 (user/kekse*@domain.de
119 user.kekse*@domain.de
120 user.kekse* )
121 depending on given folder name) */
122 $q = $folder."*".$domain;
123 $list = imap_listmailbox($this->mbox, $cfg["connect"], $q);
125 /* Create list of returned folder names */
126 if (is_array($list)){
127 foreach ($list as $val){
129 /* Cut domain name */
130 $val = preg_replace("/@.*$/","",$val);
131 $result[]=preg_replace ("/^.*".normalizePreg($folder)."/","INBOX", imap_utf7_decode ($val));
132 }
133 }
135 /* Append "INBOX" to the folder array if result is empty and request comes from user dialog */
136 if(empty($result) && !empty($uid)){
137 $result[] = "INBOX";
138 }
140 return ($result);
141 }
144 function updateMailbox($folder)
145 {
146 /* Check if mailbox exists */
147 $cfg= $this->config[$this->gosaMailServer];
148 $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
149 if ($list === FALSE){
150 if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
151 print_red(sprintf(_("Can't create IMAP mailbox. Server says '%s'."), imap_last_error()));
152 return;
153 }
154 }
155 }
158 function setQuota($folder, $gosaMailQuota)
159 {
160 /* Workaround for the php imap extension */
161 if (($gosaMailQuota == "") || ($gosaMailQuota== "2147483647")){
162 $gosaMailQuota= "2147483647";
163 }elseif($gosaMailQuota > 0){
164 $gosaMailQuota = $gosaMailQuota *1024;
165 }
168 /* Write mail quota */
169 if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
170 print_red(sprintf(_("Can't write IMAP quota. Server says '%s'."), imap_last_error()));
171 return (FALSE);
172 }
173 return (TRUE);
174 }
177 function setSharedFolderPermissions($folder, $permissions)
178 {
179 /* Get list of subfolders */
180 $folders= $this->getMailboxList($folder, "");
181 $folders[]= $folder;
183 foreach ($folders as $subfolder){
185 /* Set shared folder acl's */
186 if (function_exists('imap_getacl')){
188 /* Remove all acl's for this folder */
189 $users= @imap_getacl ($this->mbox, $subfolder);
190 if(is_array($users)){
191 foreach ($users as $userid => $perms){
192 imap_setacl ($this->mbox, $subfolder, $userid, "");
193 }
194 }
195 } else {
196 print_red (_("Warning: imap_getacl is not implemented, can't remove acl informations."));
197 }
199 /* Set permissions for this folder */
200 foreach ($permissions as $user => $acl){
201 imap_setacl ($this->mbox, $subfolder, $user, $acl);
202 }
203 }
205 }
208 function getSharedFolderPermissions($folder)
209 {
210 $result= array();
212 /* imap_getacl available? */
213 if (!function_exists('imap_getacl')){
214 print_red (_("Warning: imap_getacl is not available, can't get imap permissions!"));
215 }
217 /* Get permissions in case of shared folders */
218 else {
219 $users= imap_getacl ($this->mbox, $folder);
221 foreach ($users as $userid => $perms){
222 $result[preg_replace('/^user\./', '', $userid)]= $perms;
223 }
225 }
227 return ($result);
228 }
231 function deleteMailbox($folder)
232 {
233 $cfg= $this->config[$this->gosaMailServer];
234 imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
235 if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
236 print_red(sprintf(_("Can't remove IMAP mailbox. Server says '%s'."), imap_last_error()));
237 return (FALSE);
238 }
239 return (TRUE);
240 }
243 function configureFilter($user, $gosaMailDeliveryMode,
244 $mail, $gosaMailAlternateAddress,
245 $gosaMailMaxSize,
246 $gosaSpamMailbox, $gosaSpamSortLevel,
247 $gosaVacationMessage)
248 {
249 $cfg= $this->config[$this->gosaMailServer];
251 /* Build spamlevel. Spamassassin tags mails with "*" for each integer
252 point of spam. So a spam level of 5.3 gets "*****" which can be
253 checked easily by spam filters */
254 $spamlevel= "";
255 for ($i= 0; $i<$gosaSpamSortLevel; $i++){
256 $spamlevel .= "*";
257 }
259 /* Log into the mail server */
260 $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
261 $cfg["password"], $cfg["admin"]);
263 if (!$sieve->sieve_login()){
264 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
265 to_string($sieve->error_raw)));
266 return;
267 }
269 /* Load current script from server and remove everything between the comments
270 "###GOSA" */
271 $script= "";
272 if($sieve->sieve_listscripts()){
273 if (in_array("gosa", $sieve->response)){
275 /* get old GOsa script */
276 if(!$sieve->sieve_getscript("gosa")){
277 print_red(sprintf(_("Can't get sieve script. Server says '%s'."), to_string($sieve->error_raw)));
278 return;
279 }
281 foreach ($sieve->response as $line){
282 if (preg_match ("/^###GOSA/", $line)){
283 break;
284 }
285 $line= rtrim($line);
286 if (!preg_match ('/^\s*$/', $line)){
287 $script .= $line."\n";
288 }
289 }
291 }
292 }
294 /* Only create a new one, if it is not empty */
295 if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
296 is_integer(strpos($gosaMailDeliveryMode, "C")) ||
297 !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
298 is_integer(strpos($gosaMailDeliveryMode, "V")) ||
299 is_integer(strpos($gosaMailDeliveryMode, "S"))){
301 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
302 eval ("\$script.=\"$text\";");
303 }
305 /* Add anti-spam code */
306 if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
307 $spambox= $gosaSpamMailbox;
308 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
309 eval ("\$script.=\"$text\";");
310 }
312 /* Add "reject due to mailsize" code, message is currently not
313 adjustable through GOsa. */
314 if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
315 $maxsize= $gosaMailMaxSize;
316 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
317 eval ("\$script.=\"$text\";");
318 }
320 /* Add vacation information */
321 if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
323 /* Sieve wants all destination addresses for the
324 vacation message, so we've to assemble them from
325 mail and mailAlternateAddress */
326 $addrlist= "\"".$mail."\"";
327 foreach ($gosaMailAlternateAddress as $val){
328 $addrlist .= ", \"$val\"";
329 }
330 $vacmsg= $gosaVacationMessage;
331 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
332 eval ("\$script.=\"$text\";");
333 }
335 /* If no local delivery is wanted, tell the script to discard the mail */
336 if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
337 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
338 eval ("\$script.=\"$text\";");
339 }
341 /* Just be aware of null scripts... */
342 if (!isset ($script)){
343 $script= "";
344 }
346 /* Upload script and make it the default one */
347 if (!$sieve->sieve_sendscript("gosa", $script)){
348 print_red(sprintf(_("Can't send sieve script. Server says '%s'."), to_string($sieve->error_raw)));
349 return;
350 }
351 if(!$sieve->sieve_setactivescript("gosa")){
352 print_red(sprintf(_("Can't activate GOsa sieve script. Server says '%s'."), to_string($sieve->error_raw)));
353 return;
354 }
356 $sieve->sieve_logout();
357 }
359 }
361 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
362 ?>