Code

* Created "old" branch and moved stuff
[gosa.git] / branches / old / gosa-plugins / mail / personal / mail / class_mail-methods-cyrus.inc
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   var $folder_prefix  = "share.";
29   var $user_prefix    = "user.";
31   function mailMethodCyrus(&$config)
32   {
33     /* Check if the mail account identification attribute
34        is overridden in the configuration file
35      */
36     if($config->get_cfg_value("mail_attrib") != ""){
37       $new_uattrib= strtolower($config->get_cfg_value("mail_attrib"));
38       if(in_array($new_uattrib,array("mail","uid"))){
39         $this->uattrib = $new_uattrib;
40       }else{
41         trigger_error(sprintf("Unsupported MAIL_ATTRIB in gosa configuration specified, use 'mail' or 'uid', instead of '%s'.",            $new_uattrib));
42       }
43     }
44     
45     /* Create the account prefix  user. user/ 
46        Preset folder prefix. Will change it later to respect
47        altnamespace. */
48     if ($config->get_cfg_value("cyrusunixstyle") == "true"){
49       $this->user_prefix= "user/";
50     }
52     $this->config= $config->data['SERVERS']['IMAP'];
53   }
55   function connect($gosaMailServer)
56   {
57     $cfg=array();
59     /* Connect to IMAP server. I don't want to see these warning here... */
60     $this->gosaMailServer= $gosaMailServer;
61     if (!isset($this->config[$gosaMailServer])){
62       msg_dialog::display(_("Warning"), _("Mail server for this account is invalid!"), WARNING_DIALOG);
63     } else {
64       $cfg= $this->config[$gosaMailServer];
65     }
66     /* For some reason, hiding errors with @ does not wor here... */
67     if(!isset($cfg['connect']))   $cfg['connect']="";
68     if(!isset($cfg['admin']))     $cfg['admin']="";
69     if(!isset($cfg['password']))  $cfg['password']="";
71     /* Setting connect timeout to 10 seconds,
72         else the GOsa UI may freeze for 60 seconds.
73        (PHP default is 'default_socket_timeout = 60') */
74     imap_timeout(1, 10 );
76     $this->mbox = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
78     /* Mailbox reachable? */
79     if ($this->mbox === FALSE){
80       msg_dialog::display(_("IMAP error"), _("Cannot store mail settings on IMAP server!"), ERROR_DIALOG);
81       return (FALSE);
82     }
83     return (TRUE);
84   }
86   function disconnect()
87   {
88     imap_close ($this->mbox);
89   }
91   function getQuota($folder)
92   {
93     $result= array('quotaUsage' => '', 'gosaMailQuota' => '');
95     /* Load quota settings */
96     error_reporting (0);
97     $quota_value = @imap_get_quota($this->mbox, $folder);
98     if(is_array($quota_value)) {
99       if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])){
100         /* use for PHP >= 4.3 */
101         if($quota_value["STORAGE"]['limit'] == 2147483647){
102           $result['quotaUsage']=    (int) ($quota_value["STORAGE"]['usage'] / 1024);
103           $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] );
104         }else{
105           $result['quotaUsage']=    (int) ($quota_value["STORAGE"]['usage'] / 1024);
106           $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] / 1024);
107         }
108       } else {
109         /* backward icompatible */
110         if($quota_value['usage'] == 2147483647){
111           $result['quotaUsage']=    (int) ($quota_value['usage'] / 1024);
112           $result['gosaMailQuota']= (int) ($quota_value['limit'] );
113         }else{
114           $result['quotaUsage']=    (int) ($quota_value['usage'] / 1024);
115           $result['gosaMailQuota']= (int) ($quota_value['limit'] / 1024);
116         }
117       }
118     }elseif(!$quota_value){
119       /* If there is no quota defined for this account, the function imap_get_quota returns false. */
120       return(array("quotaUsage"=>"","gosaMailQuota"=>""));
121     }
122  
123     error_reporting (E_ALL | E_STRICT);
124     return ($result);
125   }
128   /* return all folders of the users mailbox*/
129   function getMailboxList($folder, $uid)
130   {
131     global $config;
132     $result = array();
134     /* Get domain an mail address if uid is an mail address */
135     $domain = "";
136     if(preg_match("/@/",$folder)){
137       $domain = "@".preg_replace("/^.*@/","",$folder);
138       $folder = preg_replace("/@.*$/","",$folder);
139     }
141     /* Get list of mailboxes for combo box */
142     $cfg= $this->config[$this->gosaMailServer];
144     /* Create search pattern
145          (user/kekse*@domain.de
146           user.kekse*@domain.de
147           user.kekse*  )
148        depending on given folder name) */
149     $q = $folder."*".$domain;
150     $list = imap_listmailbox($this->mbox, $cfg["connect"], $q);
152     /* Create list of returned folder names */
153     if (is_array($list)){
154       foreach ($list as $val){
156         /* Cut domain name */
157         $val = preg_replace("/@.*$/","",$val);
158         $result[]=preg_replace ("/^.*".normalizePreg($folder)."/","INBOX", mb_convert_encoding($val, "UTF-8", "UTF7-IMAP"));
159       }
160     }
162     /* Append "INBOX" to the folder array if result is empty and request comes from user dialog */
163     if(empty($result) && !empty($uid)){
164       $result[] = "INBOX";
165     }
167     return ($result);
168   }
171   function updateMailbox($folder)
172   {
173     /* Check if mailbox exists */
174     $cfg= $this->config[$this->gosaMailServer];
175     $list = imap_listmailbox($this->mbox, $cfg["connect"], $folder);
176     if ($list === FALSE){
177       if (!imap_createmailbox($this->mbox, $cfg["connect"]. $folder)){
178         msg_dialog::display(_("IMAP error"), sprintf(_("Cannot create IMAP mailbox: %s"), '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
179         return;
180       }
181     }
182   }
185   function setQuota($folder, $gosaMailQuota)
186   {
187     /* Workaround for the php imap extension */
188     if (($gosaMailQuota == "") || ($gosaMailQuota== "2147483647")){
189       $gosaMailQuota= "2147483647";
190     }elseif($gosaMailQuota > 0){
191       $gosaMailQuota = $gosaMailQuota *1024;
192     }
193     
194     
195     /* Write mail quota */
196     if (!imap_set_quota($this->mbox, $folder, $gosaMailQuota)){
197       msg_dialog::display(_("IMAP error"), sprintf(_("Cannot modify IMAP mailbox quota: %s"), '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
198       return (FALSE);
199     }
200     return (TRUE);
201   }
204   function setSharedFolderPermissions($folder, $permissions)
205   {
206     /* Get list of subfolders */
207     $folders= $this->getMailboxList($folder, "");
208     $folders[]= $folder;
209     
210     foreach ($folders as $subfolder){
212       /* Set shared folder acl's */
213       if (function_exists('imap_getacl')){
215         /* Remove all acl's for this folder */
216         $users= @imap_getacl ($this->mbox, $subfolder);
217         if(is_array($users)){
218           foreach ($users as $userid => $perms){
219             imap_setacl ($this->mbox, $subfolder, $userid, "");
220           }
221         }
222       } else {
223         msg_dialog::display(_("Internal error"), _("Cannot remove IMAP ACL: imap_getacl not implemented!"), ERROR_DIALOG);
224       }
226       /* Set permissions for this folder */
227       foreach ($permissions as $user => $acl){
228         imap_setacl ($this->mbox, $subfolder, $user, $acl);
229       }
230     }
232   }
235   function getSharedFolderPermissions($folder)
236   {
237     $result= array();
239     /* imap_getacl available? */
240     if (!function_exists('imap_getacl')){
241       msg_dialog::display(_("Internal error"), _("Cannot retrieve IMAP ACL: imap_getacl not implemented!"), ERROR_DIALOG);
242     }
244     /* Get permissions in case of shared folders */
245     else {
246       $users= imap_getacl ($this->mbox, $folder);
248       foreach ($users as $userid => $perms){
249         $result[preg_replace('/^user\./', '', $userid)]= $perms;
250       }
252     }
254     return ($result);
255   }
258   function deleteMailbox($folder)
259   {
260     $cfg= $this->config[$this->gosaMailServer];
261     imap_setacl ($this->mbox, $folder, $cfg["admin"], "lrswipcda");
262     if (!imap_deletemailbox($this->mbox, $cfg["connect"].$folder)){
263       msg_dialog::display(_("IMAP error"), sprintf(_('Cannot remove IMAP mailbox: %s'), '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
264       return (FALSE);
265     }
266     return (TRUE);
267   }
270   function configureFilter($user, $gosaMailDeliveryMode,
271       $mail, $gosaMailAlternateAddress,
272       $gosaMailMaxSize,
273       $gosaSpamMailbox, $gosaSpamSortLevel,
274       $gosaVacationMessage)
275   {
276     $cfg= $this->config[$this->gosaMailServer];
278     /* Build spamlevel. Spamassassin tags mails with "*" for each integer
279        point of spam. So a spam level of 5.3 gets "*****" which can be
280        checked easily by spam filters */
281     $spamlevel= "";
282     for ($i= 0; $i<$gosaSpamSortLevel; $i++){
283       $spamlevel .= "*";
284     }
286     /* Log into the mail server */
287     $sieve= new sieve($cfg["sieve_server"], $cfg["sieve_port"], $user,
288         $cfg["password"], $cfg["admin"],$cfg["sieve_option"]);
290     if (!$sieve->sieve_login()){
291       msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot log into SIEVE server: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
292       return;
293     }
295     /* Load current script from server and remove everything between the comments
296        "###GOSA" */
297     $script= "";
298     if($sieve->sieve_listscripts()){
299       if (in_array("gosa", $sieve->response)){
301         /* get old GOsa script */
302         if(!$sieve->sieve_getscript("gosa")){
303           msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot retrieve SIEVE script: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
304           return;
305         }
307         foreach ($sieve->response as $line){
308           if (preg_match ("/^###GOSA/", $line)){
309             break;
310           }
311           $line= rtrim($line);
312           if (!preg_match ('/^\s*$/', $line)){
313             $script .= $line."\n";
314           }
315         }
317       }
318     }
320     /* Only create a new one, if it is not empty */
321     if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
322         is_integer(strpos($gosaMailDeliveryMode, "C")) ||
323         !is_integer(strpos($gosaMailDeliveryMode, "L")) ||
324         is_integer(strpos($gosaMailDeliveryMode, "V")) ||
325         is_integer(strpos($gosaMailDeliveryMode, "S"))){
327       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-header.txt")));
328       eval ("\$script.=\"$text\";");
329     }
331     /* Add anti-spam code */
332     if (is_integer(strpos($gosaMailDeliveryMode, "S"))){
333       $spambox= $gosaSpamMailbox;
334       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-spam.txt")));
335       eval ("\$script.=\"$text\";");
336     }
338     /* Add "reject due to mailsize" code, message is currently not
339        adjustable through GOsa. */
340     if (is_integer(strpos($gosaMailDeliveryMode, "R"))){
341       $maxsize= $gosaMailMaxSize;
342       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-mailsize.txt")));
343       eval ("\$script.=\"$text\";");
344     }
346     /* Add vacation information */
347     if (is_integer(strpos($gosaMailDeliveryMode, "V"))){
349       /* Sieve wants all destination addresses for the
350          vacation message, so we've to assemble them from
351          mail and mailAlternateAddress */
352       $addrlist= "\"".$mail."\"";
353       foreach ($gosaMailAlternateAddress as $val){
354         $addrlist .= ", \"$val\"";
355       }
356       $vacmsg= $gosaVacationMessage;
357       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-vacation.txt")));
358       eval ("\$script.=\"$text\";");
359     }
361     /* If no local delivery is wanted, tell the script to discard the mail */
362     if (!is_integer(strpos($gosaMailDeliveryMode, "L"))){
363       $text= preg_replace('/"/', '\\"', implode ("", file(CONFIG_DIR."/sieve-discard.txt")));
364       eval ("\$script.=\"$text\";");
365     }
367     /* Just be aware of null scripts... */
368     if (!isset ($script)){
369       $script= "";
370     }
372     /* Upload script and make it the default one */
373     if (!$sieve->sieve_sendscript("gosa", $script)){
374       msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot store SIEVE script: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
375       return;
376     }
377     if(!$sieve->sieve_setactivescript("gosa")){
378       msg_dialog::display(_("SIEVE error"), sprintf(_("Cannot activate SIEVE script: %s"), '<br><br><i>'.to_string($sieve->error_raw).'</i>'), ERROR_DIALOG);
379       return;
380     }
382     $sieve->sieve_logout();
383   }
387 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
388 ?>