58fe60b5281521a2dd60a459df6152e46991a862
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 /* 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 print_red (_("Warning: IMAP Server cannot be reached! If you save this account, some mail settings will not be stored on your server!"));
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", imap_utf7_decode ($val));
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 print_red(sprintf(_("Can't create IMAP mailbox. Server says '%s'."), imap_last_error()));
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 print_red(sprintf(_("Can't write IMAP quota. Server says '%s'."), imap_last_error()));
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 print_red (_("Warning: imap_getacl is not implemented, can't remove acl informations."));
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 print_red (_("Warning: imap_getacl is not available, can't get imap permissions!"));
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 print_red(sprintf(_("Can't remove IMAP mailbox. Server says '%s'."), imap_last_error()));
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 print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),
270 to_string($sieve->error_raw)));
271 return;
272 }
274 /* Load current script from server and remove everything between the comments
275 "###GOSA" */
276 $script= "";
277 if($sieve->sieve_listscripts()){
278 if (in_array("gosa", $sieve->response)){
280 /* get old GOsa script */
281 if(!$sieve->sieve_getscript("gosa")){
282 print_red(sprintf(_("Can't get sieve script. Server says '%s'."), to_string($sieve->error_raw)));
283 return;
284 }
286 foreach ($sieve->response as $line){
287 if (preg_match ("/^###GOSA/", $line)){
288 break;
289 }
290 $line= rtrim($line);
291 if (!preg_match ('/^\s*$/', $line)){
292 $script .= $line."\n";
293 }
294 }
296 }
297 }
299 /* Only create a new one, if it is not empty */
300 if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
301 is_integer(strpos($gosaMailDeliveryMode, "C")) ||
302 !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
303 is_integer(strpos($gosaMailDeliveryMode, "V")) ||
304 is_integer(strpos($gosaMailDeliveryMode, "S"))){
306 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
307 eval ("\$script.=\"$text\";");
308 }
310 /* Add anti-spam code */
311 if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
312 $spambox= $gosaSpamMailbox;
313 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
314 eval ("\$script.=\"$text\";");
315 }
317 /* Add "reject due to mailsize" code, message is currently not
318 adjustable through GOsa. */
319 if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
320 $maxsize= $gosaMailMaxSize;
321 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
322 eval ("\$script.=\"$text\";");
323 }
325 /* Add vacation information */
326 if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
328 /* Sieve wants all destination addresses for the
329 vacation message, so we've to assemble them from
330 mail and mailAlternateAddress */
331 $addrlist= "\"".$mail."\"";
332 foreach ($gosaMailAlternateAddress as $val){
333 $addrlist .= ", \"$val\"";
334 }
335 $vacmsg= $gosaVacationMessage;
336 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
337 eval ("\$script.=\"$text\";");
338 }
340 /* If no local delivery is wanted, tell the script to discard the mail */
341 if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
342 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
343 eval ("\$script.=\"$text\";");
344 }
346 /* Just be aware of null scripts... */
347 if (!isset ($script)){
348 $script= "";
349 }
351 /* Upload script and make it the default one */
352 if (!$sieve->sieve_sendscript("gosa", $script)){
353 print_red(sprintf(_("Can't send sieve script. Server says '%s'."), to_string($sieve->error_raw)));
354 return;
355 }
356 if(!$sieve->sieve_setactivescript("gosa")){
357 print_red(sprintf(_("Can't activate GOsa sieve script. Server says '%s'."), to_string($sieve->error_raw)));
358 return;
359 }
361 $sieve->sieve_logout();
362 }
364 }
366 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
367 ?>