16e6b3db915f37dd1c0cbc2dc24ae71ca558b9ea
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= "";
27 function mailMethodCyrus($config)
28 {
29 $this->config= $config->data['SERVERS']['IMAP'];
30 }
32 function connect($gosaMailServer)
33 {
34 $cfg=array();
36 /* Connect to IMAP server. I don't want to see these warning here... */
37 $this->gosaMailServer= $gosaMailServer;
38 if (!isset($this->config[$gosaMailServer])){
39 print_red(_("Warning: Account has an invalid mailserver string!"));
40 } else {
41 $cfg= $this->config[$gosaMailServer];
42 }
43 /* For some reason, hiding errors with @ does not wor here... */
44 if(!isset($cfg['connect'])) $cfg['connect']="";
45 if(!isset($cfg['admin'])) $cfg['admin']="";
46 if(!isset($cfg['password'])) $cfg['password']="";
48 $this->mbox = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
50 /* Mailbox reachable? */
51 if ($this->mbox === FALSE){
52 print_red (_("Warning: IMAP Server cannot be reached! If you save this account, some mail settings will not be stored on your server!"));
53 return (FALSE);
54 }
55 return (TRUE);
56 }
58 function disconnect()
59 {
60 imap_close ($this->mbox);
61 }
63 function getQuota($folder)
64 {
65 $result= array('quotaUsage' => '', 'gosaMailQuota' => '');
67 /* Load quota settings */
68 error_reporting (0);
69 $quota_value = @imap_get_quota($this->mbox, $folder);
70 if(is_array($quota_value)) {
71 if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])){
72 /* use for PHP >= 4.3 */
73 if($quota_value["STORAGE"]['limit'] == 2147483647){
74 $result['quotaUsage']= (int) ($quota_value["STORAGE"]['usage'] / 1024);
75 $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] );
76 }else{
77 $result['quotaUsage']= (int) ($quota_value["STORAGE"]['usage'] / 1024);
78 $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] / 1024);
79 }
80 } else {
81 /* backward icompatible */
82 if($quota_value['usage'] == 2147483647){
83 $result['quotaUsage']= (int) ($quota_value['usage'] / 1024);
84 $result['gosaMailQuota']= (int) ($quota_value['limit'] );
85 }else{
86 $result['quotaUsage']= (int) ($quota_value['usage'] / 1024);
87 $result['gosaMailQuota']= (int) ($quota_value['limit'] / 1024);
88 }
89 }
90 }elseif(!$quota_value){
92 /* If there is no quota defined for this account, the function imap_get_quota returns false. */
93 return(array());
94 }
96 error_reporting (E_ALL);
97 return ($result);
98 }
100 function getMailboxList($folder, $uid= "")
101 {
102 /* Initialize depending on group or user mode */
103 if ($uid != ""){
104 $result= array("INBOX");
105 } else {
106 $result= array();
107 }
109 /* Get list of mailboxes for combo box */
110 $cfg= $this->config[$this->gosaMailServer];
111 $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder.".*");
112 if (is_array($list)){
113 foreach ($list as $val){
114 $result[]=preg_replace ("/.*user\.".$uid."\./",
115 "INBOX.", imap_utf7_decode ($val));
116 }
117 }
119 return ($result);
120 }
122 function updateMailbox($folder)
123 {
124 /* Check if mailbox exists */
125 $cfg= $this->config[$this->gosaMailServer];
126 $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
127 if ($list === FALSE){
128 if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
129 print_red(sprintf(_("Can't create IMAP mailbox. Server says '%s'."), imap_last_error()));
130 return;
131 }
132 }
133 }
136 function setQuota($folder, $gosaMailQuota)
137 {
138 /* Workaround for the php imap extension */
139 if (($gosaMailQuota == "") || ($gosaMailQuota== "2147483647")){
140 $gosaMailQuota= "2147483647";
141 }elseif($gosaMailQuota > 0){
142 $gosaMailQuota = $gosaMailQuota *1024;
143 }
146 /* Write mail quota */
147 if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
148 print_red(sprintf(_("Can't write IMAP quota. Server says '%s'."), imap_last_error()));
149 return (FALSE);
150 }
151 return (TRUE);
152 }
155 function setSharedFolderPermissions($folder, $permissions)
156 {
157 /* Get list of subfolders */
158 $folders= $this->getMailboxList($folder, "");
159 $folders[]= $folder;
161 foreach ($folders as $subfolder){
163 /* Set shared folder acl's */
164 if (function_exists('imap_getacl')){
166 /* Remove all acl's for this folder */
167 $users= @imap_getacl ($this->mbox, $subfolder);
168 if(is_array($users)){
169 foreach ($users as $userid => $perms){
170 imap_setacl ($this->mbox, $subfolder, $userid, "");
171 }
172 }
173 } else {
174 print_red (_("Warning: imap_getacl is not implemented, can't remove acl informations."));
175 }
177 /* Set permissions for this folder */
178 foreach ($permissions as $user => $acl){
179 imap_setacl ($this->mbox, $subfolder, $user, $acl);
180 }
181 }
183 }
186 function getSharedFolderPermissions($folder)
187 {
188 $result= array();
190 /* imap_getacl available? */
191 if (!function_exists('imap_getacl')){
192 print_red (_("Warning: imap_getacl is not available, can't get imap permissions!"));
193 }
195 /* Get permissions in case of shared folders */
196 else {
197 $users= imap_getacl ($this->mbox, $folder);
199 foreach ($users as $userid => $perms){
200 $result[preg_replace('/^user\./', '', $userid)]= $perms;
201 }
203 }
205 return ($result);
206 }
209 function deleteMailbox($folder)
210 {
211 $cfg= $this->config[$this->gosaMailServer];
212 imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
213 if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
214 print_red(sprintf(_("Can't remove IMAP mailbox. Server says '%s'."), imap_last_error()));
215 return (FALSE);
216 }
217 return (TRUE);
218 }
221 function configureFilter($user, $gosaMailDeliveryMode,
222 $mail, $gosaMailAlternateAddress,
223 $gosaMailMaxSize,
224 $gosaSpamMailbox, $gosaSpamSortLevel,
225 $gosaVacationMessage)
226 {
227 $cfg= $this->config[$this->gosaMailServer];
229 /* Build spamlevel. Spamassassin tags mails with "*" for each integer
230 point of spam. So a spam level of 5.3 gets "*****" which can be
231 checked easily by spam filters */
232 $spamlevel= "";
233 for ($i= 0; $i<$gosaSpamSortLevel; $i++){
234 $spamlevel .= "*";
235 }
237 /* Log into the mail server */
238 $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
239 $cfg["password"], $cfg["admin"]);
241 if (!$sieve->sieve_login()){
242 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
243 to_string($sieve->error_raw)));
244 return;
245 }
247 /* Load current script from server and remove everything between the comments
248 "###GOSA" */
249 $script= "";
250 if($sieve->sieve_listscripts()){
251 if (in_array("gosa", $sieve->response)){
253 /* get old GOsa script */
254 if(!$sieve->sieve_getscript("gosa")){
255 print_red(sprintf(_("Can't get sieve script. Server says '%s'."), to_string($sieve->error_raw)));
256 return;
257 }
259 foreach ($sieve->response as $line){
260 if (preg_match ("/^###GOSA/", $line)){
261 break;
262 }
263 $line= rtrim($line);
264 if (!preg_match ('/^\s*$/', $line)){
265 $script .= $line."\n";
266 }
267 }
269 }
270 }
272 /* Only create a new one, if it is not empty */
273 if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
274 is_integer(strpos($gosaMailDeliveryMode, "C")) ||
275 !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
276 is_integer(strpos($gosaMailDeliveryMode, "V")) ||
277 is_integer(strpos($gosaMailDeliveryMode, "S"))){
279 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
280 eval ("\$script.=\"$text\";");
281 }
283 /* Add anti-spam code */
284 if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
285 $spambox= $gosaSpamMailbox;
286 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
287 eval ("\$script.=\"$text\";");
288 }
290 /* Add "reject due to mailsize" code, message is currently not
291 adjustable through GOsa. */
292 if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
293 $maxsize= $gosaMailMaxSize;
294 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
295 eval ("\$script.=\"$text\";");
296 }
298 /* Add vacation information */
299 if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
301 /* Sieve wants all destination addresses for the
302 vacation message, so we've to assemble them from
303 mail and mailAlternateAddress */
304 $addrlist= "\"".$mail."\"";
305 foreach ($gosaMailAlternateAddress as $val){
306 $addrlist .= ", \"$val\"";
307 }
308 $vacmsg= $gosaVacationMessage;
309 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
310 eval ("\$script.=\"$text\";");
311 }
313 /* If no local delivery is wanted, tell the script to discard the mail */
314 if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
315 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
316 eval ("\$script.=\"$text\";");
317 }
319 /* Just be aware of null scripts... */
320 if (!isset ($script)){
321 $script= "";
322 }
324 /* Upload script and make it the default one */
325 if (!$sieve->sieve_sendscript("gosa", $script)){
326 print_red(sprintf(_("Can't send sieve script. Server says '%s'."), to_string($sieve->error_raw)));
327 return;
328 }
329 if(!$sieve->sieve_setactivescript("gosa")){
330 print_red(sprintf(_("Can't activate GOsa sieve script. Server says '%s'."), to_string($sieve->error_raw)));
331 return;
332 }
334 $sieve->sieve_logout();
335 }
337 }
339 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
340 ?>