4e752abf6534c6c8e098793df698bbd4d25e10e6
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 getMailboxList2($folder, $uid= "")
145 {
146 /* Initialize depending on group or user mode */
147 if ($uid != ""){
148 $result= array("INBOX");
149 } else {
150 $result= array();
151 }
153 /* Get list of mailboxes for combo box */
154 $cfg= $this->config[$this->gosaMailServer];
155 $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder.".*");
156 if (is_array($list)){
157 foreach ($list as $val){
158 $result[]=preg_replace ("/.*user\.".$uid."\./",
159 "INBOX.", imap_utf7_decode ($val));
160 }
161 }
163 return ($result);
164 }
166 function updateMailbox($folder)
167 {
168 /* Check if mailbox exists */
169 $cfg= $this->config[$this->gosaMailServer];
170 $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
171 if ($list === FALSE){
172 if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
173 print_red(sprintf(_("Can't create IMAP mailbox. Server says '%s'."), imap_last_error()));
174 return;
175 }
176 }
177 }
180 function setQuota($folder, $gosaMailQuota)
181 {
182 /* Workaround for the php imap extension */
183 if (($gosaMailQuota == "") || ($gosaMailQuota== "2147483647")){
184 $gosaMailQuota= "2147483647";
185 }elseif($gosaMailQuota > 0){
186 $gosaMailQuota = $gosaMailQuota *1024;
187 }
190 /* Write mail quota */
191 if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
192 print_red(sprintf(_("Can't write IMAP quota. Server says '%s'."), imap_last_error()));
193 return (FALSE);
194 }
195 return (TRUE);
196 }
199 function setSharedFolderPermissions($folder, $permissions)
200 {
201 /* Get list of subfolders */
202 $folders= $this->getMailboxList($folder, "");
203 $folders[]= $folder;
205 foreach ($folders as $subfolder){
207 /* Set shared folder acl's */
208 if (function_exists('imap_getacl')){
210 /* Remove all acl's for this folder */
211 $users= @imap_getacl ($this->mbox, $subfolder);
212 if(is_array($users)){
213 foreach ($users as $userid => $perms){
214 imap_setacl ($this->mbox, $subfolder, $userid, "");
215 }
216 }
217 } else {
218 print_red (_("Warning: imap_getacl is not implemented, can't remove acl informations."));
219 }
221 /* Set permissions for this folder */
222 foreach ($permissions as $user => $acl){
223 imap_setacl ($this->mbox, $subfolder, $user, $acl);
224 }
225 }
227 }
230 function getSharedFolderPermissions($folder)
231 {
232 $result= array();
234 /* imap_getacl available? */
235 if (!function_exists('imap_getacl')){
236 print_red (_("Warning: imap_getacl is not available, can't get imap permissions!"));
237 }
239 /* Get permissions in case of shared folders */
240 else {
241 $users= imap_getacl ($this->mbox, $folder);
243 foreach ($users as $userid => $perms){
244 $result[preg_replace('/^user\./', '', $userid)]= $perms;
245 }
247 }
249 return ($result);
250 }
253 function deleteMailbox($folder)
254 {
255 $cfg= $this->config[$this->gosaMailServer];
256 imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
257 if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
258 print_red(sprintf(_("Can't remove IMAP mailbox. Server says '%s'."), imap_last_error()));
259 return (FALSE);
260 }
261 return (TRUE);
262 }
265 function configureFilter($user, $gosaMailDeliveryMode,
266 $mail, $gosaMailAlternateAddress,
267 $gosaMailMaxSize,
268 $gosaSpamMailbox, $gosaSpamSortLevel,
269 $gosaVacationMessage)
270 {
271 $cfg= $this->config[$this->gosaMailServer];
273 /* Build spamlevel. Spamassassin tags mails with "*" for each integer
274 point of spam. So a spam level of 5.3 gets "*****" which can be
275 checked easily by spam filters */
276 $spamlevel= "";
277 for ($i= 0; $i<$gosaSpamSortLevel; $i++){
278 $spamlevel .= "*";
279 }
281 /* Log into the mail server */
282 $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
283 $cfg["password"], $cfg["admin"]);
285 if (!$sieve->sieve_login()){
286 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
287 to_string($sieve->error_raw)));
288 return;
289 }
291 /* Load current script from server and remove everything between the comments
292 "###GOSA" */
293 $script= "";
294 if($sieve->sieve_listscripts()){
295 if (in_array("gosa", $sieve->response)){
297 /* get old GOsa script */
298 if(!$sieve->sieve_getscript("gosa")){
299 print_red(sprintf(_("Can't get sieve script. Server says '%s'."), to_string($sieve->error_raw)));
300 return;
301 }
303 foreach ($sieve->response as $line){
304 if (preg_match ("/^###GOSA/", $line)){
305 break;
306 }
307 $line= rtrim($line);
308 if (!preg_match ('/^\s*$/', $line)){
309 $script .= $line."\n";
310 }
311 }
313 }
314 }
316 /* Only create a new one, if it is not empty */
317 if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
318 is_integer(strpos($gosaMailDeliveryMode, "C")) ||
319 !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
320 is_integer(strpos($gosaMailDeliveryMode, "V")) ||
321 is_integer(strpos($gosaMailDeliveryMode, "S"))){
323 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
324 eval ("\$script.=\"$text\";");
325 }
327 /* Add anti-spam code */
328 if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
329 $spambox= $gosaSpamMailbox;
330 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
331 eval ("\$script.=\"$text\";");
332 }
334 /* Add "reject due to mailsize" code, message is currently not
335 adjustable through GOsa. */
336 if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
337 $maxsize= $gosaMailMaxSize;
338 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
339 eval ("\$script.=\"$text\";");
340 }
342 /* Add vacation information */
343 if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
345 /* Sieve wants all destination addresses for the
346 vacation message, so we've to assemble them from
347 mail and mailAlternateAddress */
348 $addrlist= "\"".$mail."\"";
349 foreach ($gosaMailAlternateAddress as $val){
350 $addrlist .= ", \"$val\"";
351 }
352 $vacmsg= $gosaVacationMessage;
353 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
354 eval ("\$script.=\"$text\";");
355 }
357 /* If no local delivery is wanted, tell the script to discard the mail */
358 if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
359 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
360 eval ("\$script.=\"$text\";");
361 }
363 /* Just be aware of null scripts... */
364 if (!isset ($script)){
365 $script= "";
366 }
368 /* Upload script and make it the default one */
369 if (!$sieve->sieve_sendscript("gosa", $script)){
370 print_red(sprintf(_("Can't send sieve script. Server says '%s'."), to_string($sieve->error_raw)));
371 return;
372 }
373 if(!$sieve->sieve_setactivescript("gosa")){
374 print_red(sprintf(_("Can't activate GOsa sieve script. Server says '%s'."), to_string($sieve->error_raw)));
375 return;
376 }
378 $sieve->sieve_logout();
379 }
381 }
383 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
384 ?>