152daee4d5ec2d1d74fb3b119f82c1b704143116
1 <?php
4 class mailMethodCyrus extends mailMethod{
6 protected $ServerList = array();
7 protected $imap_handle = NULL;
8 protected $quota_loaded = FALSE;
10 /* Allow modification of account_ids for existing mail accounts */
11 protected $modifyableMail = FALSE;
13 /* Allow modification of the mail server attribute existing mail accounts */
14 protected $modifyableServer = FALSE;
16 /* Enforces same value for 'mail' as used for 'cn' */
17 protected $mailEqualsCN = FALSE;
19 protected $enableDomainSelection= FALSE;
20 protected $enableQuota = TRUE;
21 protected $enableSieveManager = TRUE;
22 protected $enableVacationRange = TRUE;
23 protected $enableFolderTypes = FALSE;
25 protected function init()
26 {
27 mailMethod::init();
28 $this->ServerList = $this->config->data['SERVERS']['IMAP'];
29 }
32 public function connect()
33 {
34 mailMethod::connect();
36 if (!isset($this->ServerList[$this->MailServer])){
37 $this->error = _("Mail server for this account is invalid!");
38 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
39 "<b>IMAP: The selected mail server '".$this->MailServer."' is invalid.</b>","");
40 return(FALSE);
41 } else {
42 $cfg= $this->ServerList[$this->MailServer];
43 }
45 /* For some reason, hiding errors with @ does not wor here... */
46 if(!isset($cfg['connect'])) $cfg['connect']="";
47 if(!isset($cfg['admin'])) $cfg['admin']="";
48 if(!isset($cfg['password'])) $cfg['password']="";
50 /* Setting connect timeout to 10 seconds,
51 else the GOsa UI may freeze for 60 seconds.
52 (PHP default is 'default_socket_timeout = 60') */
53 imap_timeout(1, 10 );
54 $this->imap_handle = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
56 /* Mailbox reachable? */
57 if ($this->imap_handle === FALSE){
58 $this->error = imap_last_error();
60 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"<b>Failed</b> :".imap_last_error(),
61 "<b>IMAP:</b> ".$cfg['admin']."@".$cfg['connect']);
63 return (FALSE);
64 $this->connected = FALSE;
65 }
66 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"<b>successful</b>",
67 "<b>IMAP:</b> ".$cfg['admin']."@".$cfg['connect']);
68 $this->connected = TRUE;
70 return (TRUE);
71 }
74 public function account_exists()
75 {
76 if(!$this->is_connected() || !$this->imap_handle){
77 trigger_error("Method not connected, catch error.");
78 return(array());
79 }
81 /* Get server config */
82 $cfg= $this->ServerList[$this->MailServer];
83 $list = @imap_listmailbox($this->imap_handle, $cfg["connect"], $this->account_id);
84 $res = is_array($list) && count($list);
85 if($res){
86 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"","<b>IMAP: Account exists in imap server.</b>");
87 }else{
88 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"","<b>IMAP: Account seems NOT to exists in imap server.</b>");
89 }
90 return($res);
91 }
93 public function disconnect()
94 {
95 mailMethod::disconnect();
96 if($this->is_connected()){
97 @imap_close ($this->imap_handle);
98 }
99 }
102 public function is_connected()
103 {
104 $ret = mailMethod::is_connected();
105 return($ret && $this->imap_handle);
106 }
108 protected function loadQuota()
109 {
110 if(!$this->quotaEnabled()) return(TRUE);
111 if(!$this->is_connected() || !$this->imap_handle){
112 trigger_error("Method not connected, catch error.");
113 return(FALSE);
114 }
116 $this->reset_error();
118 /* Load quota settings */
119 $result = array("quotaUsage"=>"","gosaMailQuota"=>"");
120 $quota_value = @imap_get_quota($this->imap_handle, $this->account_id);
122 /* Reset error queue, imap_qet_quota() will fail if the quota wasn't set yet.
123 */
124 imap_errors();
126 if(is_array($quota_value) && count($quota_value)) {
127 if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])){
129 /* use for PHP >= 4.3 */
130 if($quota_value["STORAGE"]['limit'] == 2147483647){
131 $result['quotaUsage']= (int) ($quota_value["STORAGE"]['usage'] / 1024);
132 $result['gosaMailQuota']= "";
133 }else{
134 $result['quotaUsage']= (int) ($quota_value["STORAGE"]['usage'] / 1024);
135 $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] / 1024);
136 }
137 } else {
139 /* backward icompatible */
140 if($quota_value['usage'] == 2147483647){
141 $result['quotaUsage']= (int) ($quota_value['usage'] / 1024);
142 $result['gosaMailQuota']= "";
143 }else{
144 $result['quotaUsage']= (int) ($quota_value['usage'] / 1024);
145 $result['gosaMailQuota']= (int) ($quota_value['limit'] / 1024);
146 }
147 }
148 }
149 $this->quotaValue = $result['gosaMailQuota'];
150 $this->quotaUsage = $result['quotaUsage'];
152 /* Write debug output */
153 if(is_array($quota_value)){
154 if($this->quotaValue == ""){
155 $quota = "(".$this->quotaUsage." / unlimited)";
156 }else{
157 $quota = "(".$this->quotaUsage." / ".$this->quotaValue.")";
158 }
159 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $quota ,
160 "<b>IMAP: Successfully received account quota</b>");
161 }else{
162 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, imap_last_error() ,
163 "<b>IMAP: Failed to receive account quota</b>");
164 }
165 }
167 public function getQuota($quota)
168 {
169 mailMethod::getQuota($quota);
170 if(!$this->quota_loaded){
171 $this->quota_loaded = TRUE;
172 $this->loadQuota();
173 }
174 return($this->quotaValue);
175 }
177 public function getQuotaUsage()
178 {
179 mailMethod::getQuotaUsage();
180 if(!$this->quota_loaded){
181 $this->quota_loaded = TRUE;
182 $this->loadQuota();
183 }
184 return($this->quotaUsage);
185 }
187 public function setQuota($number)
188 {
189 mailMethod::setQuota($number);
191 if(!$this->quotaEnabled()) return(TRUE);
192 if(!$this->is_connected() || !$this->imap_handle){
193 trigger_error("Method not connected, catch error.");
194 return(FALSE);
195 }
197 $this->build_account_id();
199 /* Workaround for the php imap extension */
200 if (($this->quotaValue == "") || ($this->quotaValue== "2147483647")){
201 $this->quotaValue= "2147483647";
202 }elseif($this->quotaValue > 0){
203 $this->quotaValue = $this->quotaValue *1024;
204 }
205 $debug_number = $this->quotaValue." KB";
206 if($this->quotaValue == "2147483647"){
207 $debug_number .= "<i>Unlimited</i>";
208 }
210 if (!imap_set_quota($this->imap_handle, $this->account_id, $this->quotaValue)){
211 msg_dialog::display(_("IMAP error"), sprintf(_("Cannot modify IMAP mailbox quota: %s"),
212 '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
213 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id.": (".$debug_number.")</b>" ,
214 "<b>IMAP: Set account quota</b> on server '".$this->MailServer."' <b>".imap_last_error()."</b>");
215 return (FALSE);
216 }
217 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id.": (".$debug_number.")</b>" ,
218 "<b>IMAP: Set account quota</b> on server :".$this->MailServer);
219 return (TRUE);
220 }
223 public function updateMailbox()
224 {
225 mailMethod::updateMailbox();
227 if(!$this->is_connected() || !$this->imap_handle){
228 trigger_error("Method not connected, catch error.");
229 return(FALSE);
230 }
232 $this->build_account_id ();
233 if($this->is_connected()){
234 $cfg= $this->ServerList[$this->MailServer];
235 $list = imap_listmailbox($this->imap_handle, $cfg["connect"], $this->account_id);
236 if ($list === FALSE){
237 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id."</b>" ,
238 "<b>IMAP: Add/Update account</b> on server :".$this->MailServer);
239 if (!imap_createmailbox($this->imap_handle, $cfg["connect"]. $this->account_id)){
240 $this->error = imap_last_error();
241 return(FALSE);
242 }
243 }
244 }
245 return(TRUE);
246 }
249 public function deleteMailbox()
250 {
251 mailMethod::deleteMailbox();
253 if(!$this->is_connected() || !$this->imap_handle){
254 trigger_error("Method not connected, catch error.");
255 return(FALSE);
256 }
258 $this->build_account_id ();
260 $cfg= $this->ServerList[$this->MailServer];
261 @imap_setacl ($this->imap_handle, $this->account_id, $cfg["admin"], "lrswipcda");
262 if (!imap_deletemailbox($this->imap_handle, $cfg["connect"].$this->account_id)){
263 $this->error = imap_last_error();
264 return (FALSE);
265 }
266 return (TRUE);
267 }
270 public function getMailboxList()
271 {
272 mailMethod::getMailboxList();
274 if(!$this->is_connected() || !$this->imap_handle){
275 trigger_error("Method not connected, catch error.");
276 return(array());
277 }
279 $result = array();
281 /* Get server config */
282 $cfg= $this->ServerList[$this->MailServer];
284 /* Create search string
285 And prepare replacements
286 */
287 if(preg_match("/\@/",$this->account_id)){
288 $search = preg_replace("/\@/","*@",$this->account_id);
289 $with_domain = TRUE;
290 }else{
291 $search = $this->account_id."*";
292 $with_domain = FALSE;
293 }
294 $folder = $this->account_id;
295 if(preg_match("/\@/",$folder)){
296 $folder = preg_replace("/\@.*$/","",$folder);
297 }
299 /* Contact imap server */
300 $list = @imap_listmailbox($this->imap_handle, $cfg["connect"], $search);
302 /* Create list of returned folder names */
303 if (is_array($list)){
304 foreach ($list as $val){
305 $str = trim(preg_replace("/^\{[^\}]*+\}/","",$val));
306 if($with_domain){
307 $str = trim(preg_replace("/\@.*$/","",$str));
308 }
309 $str = preg_replace ("/^.*".preg_quote($folder, '/')."/","INBOX",
310 mb_convert_encoding($str, "UTF-8", "UTF7-IMAP"));
311 $result[] = $str;
312 }
313 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,trim(implode($result,", "),", "),
314 "<b>IMAP: Received mailbox folders.</b>");
315 $this->error = imap_last_error();
316 }else{
317 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,imap_last_error(),
318 "<b>IMAP: Cannot receive mailbox folders.</b>");
319 $this->error = imap_last_error();
320 return(array());
321 }
323 /* Append "INBOX" to the folder array if result is empty and request comes from user dialog */
324 if(!count($result)){
325 $result[] = "INBOX";
326 }
328 return($result);
329 }
332 /*! \brief Returns configured acls
333 */
334 public function getFolderACLs($folder_acls)
335 {
336 $this->reset_error();
338 /* imap_getacl available? */
339 if (!function_exists('imap_getacl')){
340 $this->error = _("The module imap_getacl is not implemented!");
341 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"The imap_getacl module is missing!",
342 "<b>IMAP: Cannot set folder acls.</b>");
343 return($folder_acls);
344 }
346 /* Get ACLs and merge them with the already given acls (ldap)
347 */
348 $this->build_account_id();
349 $acls = imap_getacl ($this->imap_handle, $this->account_id);
350 foreach($acls as $user => $acl){
351 if($user == "anyone") $user = "__anyone__"; // Map to internal placeholder
352 $folder_acls[$user] = $acl;
353 }
355 /* Merge given ACL with acl mapping
356 This ensures that no ACL will accidentally overwritten by gosa.
357 */
358 foreach($folder_acls as $user => $acl){
359 if(!isset($this->acl_mapping[$acl])){
360 $this->acl_mapping[$acl] = $acl;
361 }
362 }
364 return($folder_acls);
365 }
369 /*! \brief Write ACLs back to imap or what ever
370 */
371 public function setFolderACLs($permissions)
372 {
373 $this->reset_error();
375 /* imap_getacl available? */
376 if (!function_exists('imap_getacl')){
377 $this->error = _("The module imap_getacl is not implemented!");
378 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"The imap_getacl module is missing!",
379 "<b>IMAP: Cannot set folder acls.</b>");
380 return(FALSE);
381 }
383 /* Get list of subfolders */
384 $folders= $this->getMailboxList();
385 foreach ($folders as $subfolder){
386 $folder_id = $this->create_folder_id($subfolder);
387 echo $folder_id."<br>";
389 /* Remove all acl's for this folder */
390 $users= @imap_getacl ($this->imap_handle, $folder_id);
392 if(is_array($users)){
393 foreach ($users as $userid => $perms){
394 $userid = strtolower($userid);
395 imap_setacl ($this->imap_handle, $folder_id, $userid, "");
396 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,$folder_id." -> ".$userid,
397 "<b>IMAP: Removing folder permissions.</b>");
398 }
399 }
400 }
402 /* Set permissions for this folder */
403 foreach($folders as $subfolder){
404 $folder_id = $this->create_folder_id($subfolder);
406 foreach ($permissions as $user => $acl){
407 imap_setacl ($this->imap_handle, $folder_id, $user, $acl);
408 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,$folder_id." -> ".$user.": ".$acl,
409 "<b>IMAP: Setting new folder permissions.</b>");
410 }
411 }
412 return(TRUE);
413 }
416 public function saveSieveSettings()
417 {
418 mailMethod::saveSieveSettings();
420 /* Map attribute from parent class
421 */
422 $mail = $this->parent->mail;
423 $gosaMailDeliveryMode = $this->parent->gosaMailDeliveryMode;
424 $gosaMailAlternateAddress = $this->parent->gosaMailAlternateAddress;
425 $gosaMailMaxSize = $this->parent->gosaMailMaxSize;
426 $gosaSpamMailbox = $this->parent->gosaSpamMailbox;
427 $gosaSpamSortLevel = $this->parent->gosaSpamSortLevel;
428 $gosaVacationMessage = $this->parent->gosaVacationMessage;
430 /* Try to login into sieve
431 */
432 $cfg = $this->ServerList[$this->MailServer];
433 $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $this->getUAttribValue(),
434 $cfg["password"], $cfg["admin"],$cfg["sieve_option"]);
435 if (!$sieve->sieve_login()){
436 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,$sieve->error_raw ,"<b>SIEVE: login failed.</b>");
437 $this->error = $sieve->error_raw;
438 return(FALSE);
439 }
441 /* Build spamlevel. Spamassassin tags mails with "*" for each integer
442 point of spam. So a spam level of 5.3 gets "*****" which can be
443 checked easily by spam filters */
444 $spamlevel= str_pad("",(int) $gosaSpamSortLevel,"*");
446 /* Get current sieve script named 'gosa'.
447 Check if it valid ("###GOSA" must be the first string).
448 If it is valid just replace it, if it is NOT valid
449 create a backup of the old
450 */
451 $script= "";
452 if($sieve->sieve_listscripts()){
453 if (in_array("gosa", $sieve->response)){
454 if(!$sieve->sieve_getscript("gosa")){
455 $this->error = sprintf(_("Cannot retrieve SIEVE script: %s"),to_string($sieve->error_raw));
456 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,$sieve->error_raw ,
457 "<b>SIEVE: Connot read 'gosa' sieve script.</b>");
458 $this->error = $sieve->error_raw;
459 return(FALSE);
460 }
462 $is_valid_script = FALSE;
463 foreach ($sieve->response as $line){
464 if(empty($line)) continue;
465 if (preg_match ("/^###GOSA/", $line) && strlen($script) == 0){
466 $is_valid_script = TRUE;
467 }
468 $line= rtrim($line);
469 $script .= $line;
470 }
472 if($is_valid_script || strlen($script) == 0 || empty($script)){
473 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"" ,
474 "<b>SIEVE</b>: Sieve script 'gosa' was a valid GOsa script and will be replaced.");
475 }else{
476 $new_name = "non_gosa_".date("Ymd_H-i-s");
477 $sieve->sieve_sendscript($new_name, $script);
478 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,$this->sieve->error_raw ,
479 "<b>SIEVE</b>: Non GOsa sieve script. <b>Creating backup of the current sieve script '".$new_name."'.</b>");
480 }
481 }
482 }
485 /*****
486 Build up new sieve script here.
487 *****/
489 /* Only create a new one, if it is not empty */
490 $script= "";
491 if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
492 is_integer(strpos($gosaMailDeliveryMode, "C")) ||
493 !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
494 is_integer(strpos($gosaMailDeliveryMode, "V")) ||
495 is_integer(strpos($gosaMailDeliveryMode, "S"))){
497 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
498 eval ("\$script.=\"$text\";");
499 }
501 /* Add anti-spam code */
502 if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
503 $spambox= $gosaSpamMailbox;
504 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
505 eval ("\$script.=\"$text\";");
506 }
508 /* Add "reject due to mailsize" code, message is currently not
509 adjustable through GOsa. */
510 if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
511 $maxsize= $gosaMailMaxSize;
512 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
513 eval ("\$script.=\"$text\";");
514 }
516 /* Add vacation information */
517 if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
519 /* Sieve wants all destination addresses for the
520 vacation message, so we've to assemble them from
521 mail and mailAlternateAddress */
522 $addrlist= "\"".$mail."\"";
523 foreach ($gosaMailAlternateAddress as $val){
524 $addrlist .= ", \"$val\"";
525 }
526 $vacmsg= $gosaVacationMessage;
527 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
528 eval ("\$script.=\"$text\";");
529 }
531 /* If no local delivery is wanted, tell the script to discard the mail */
532 if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
533 $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
534 eval ("\$script.=\"$text\";");
535 }
537 /****
538 Sieve script build complete
539 ****/
541 /* Upload script and make it the default one */
542 if (!$sieve->sieve_sendscript("gosa", $script)){
543 $this->error = sprintf(_("Cannot store SIEVE script: %s"), to_string($sieve->error_raw));
544 @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "Error was: ".to_string($sieve->error_raw) ,
545 "<b>SIEVE: Writing new Sieve script failed!</b>");
546 return(FALSE);
547 }
549 if(!$sieve->sieve_setactivescript("gosa")){
550 $this->error = sprintf(_("Cannot activate SIEVE script: %s"), to_string($sieve->error_raw));
551 return(FALSE);
552 }
554 $sieve->sieve_logout();
555 }
556 }
558 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
559 ?>