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 */
20 require_once("class_mail-methods-cyrus.inc");
22 class mailMethodKolab extends mailMethodCyrus
23 {
24 var $mbox= "-";
25 var $config;
26 var $gosaMailServer= "";
27 var $uattrib= "mail";
29 var $attribute_map= array("gosaMailAlternateAddress" => "alias",
30 "gosaMailQuota" => "cyrus-userquota",
31 "gosaMailServer" => "kolabHomeServer");
33 function mailMethodKolab($config)
34 {
35 $this->config= $config->data['SERVERS']['IMAP'];
36 }
38 function updateMailbox($folder)
39 {
40 }
42 function setQuota($folder, $gosaMailQuota)
43 {
44 return (TRUE);
45 }
47 /* return all folders of the users mailbox*/
48 function getMailboxList($folder, $uid= "")
49 {
50 global $config;
51 $result = array();
53 /* Get domain an mail address if uid is an mail address */
54 $domain = "";
55 if(preg_match("/@/",$folder)){
56 $domain = preg_replace("/^.*@/","",$folder);
57 $folder = preg_replace("/@.*$/","",$folder);
58 }
60 /* Get list of mailboxes for combo box */
61 $cfg= $this->config[$this->gosaMailServer];
63 /* Create search pattern
64 (user/kekse*@domain.de
65 user.kekse*@domain.de
66 user.kekse* )
67 depending on given folder name) */
68 $q = $folder."*@".$domain;
69 $list = imap_listmailbox($this->mbox, $cfg["connect"], $q);
71 /* Create list of returned folder names */
72 if (is_array($list)){
73 foreach ($list as $val){
75 /* Cut domain name */
76 $val = preg_replace("/@.*$/","",$val);
77 $result[]=preg_replace ("/^.*".normalizePreg($folder)."/","INBOX", imap_utf7_decode ($val));
78 }
79 }
81 /* Append "INBOX" to the folder array if result is empty and request comes from user dialog */
82 if(empty($result) && !empty($uid)){
83 $result[] = "INBOX";
84 }
86 return ($result);
87 }
91 /* Get quota and divide it by 1024, because in gosa we display in MB
92 but we get Kb */
93 function getQuota($folder)
94 {
95 $result= array('quotaUsage' => '', 'gosaMailQuota' => '');
97 /* Only use lower case folder names, if folder name is like "@domain.com" */
98 if(preg_match("/@/",$folder)){
99 $folder = strtolower($folder);
100 }
102 error_reporting(0);
104 /* Load quota settings */
105 $quota_value = @imap_get_quota($this->mbox, $folder);
106 if(is_array($quota_value)) {
107 if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])){
108 /* use for PHP >= 4.3 */
109 $result['quotaUsage']= (int) ($quota_value["STORAGE"]['usage'] / 1024);
110 $result['gosaMailQuota']= (int) ($quota_value["STORAGE"]['limit'] / 1024);
111 } else {
112 /* backward icompatible */
113 $result['quotaUsage']= (int) ($quota_value['usage'] / 1024);
114 $result['gosaMailQuota']= (int) ($quota_value['limit'] / 1024);
115 }
116 }elseif(!$quota_value){
117 return(false);
118 }
120 error_reporting(E_ALL);
122 return ($result);
123 }
126 function fixAttributesOnLoad(&$mailObject)
127 {
128 /* Kolab shared folder names are like ' shared.uid@server.de '
129 So overwrite uid to match these folder names. Else we can't read quota settings etc.
130 #FIXME is there a better way to detect if it is 'shared.' or 'shared+' or 'kolab+shared.' or what ever ?*/
131 if(get_class($mailObject) == "mailgroup"){
132 $mailObject->uid = "shared.".$mailObject->uid."@".preg_replace("/^.*@/","",$mailObject->mail);
133 }
135 /* Convert attributes and objectClasses */
136 foreach ($this->attribute_map as $dest => $source){
137 /* Hickert 11.11.05 : Alternate email addresses were saved, but not displayed again.
138 if (isset($mailObject->attrs[$source])){
139 $mailObject->attrs[$dest]= $mailObject->attrs[$source];
140 unset ($mailObject->attrs[$source]);
141 */
143 if (isset($mailObject->attrs[$source])){
144 unset($mailObject->attrs[$source]['count']);
145 $mailObject->attrs[$dest]= $mailObject->attrs[$source];
146 $mailObject->$dest= $mailObject->attrs[$source];
148 unset ($mailObject->$dest['count']);
149 unset ($mailObject->attrs[$source]);
150 }
151 }
153 /* Adjust server name if needed */
154 foreach ($mailObject->config->data['SERVERS']['IMAP'] as $srv => $dummy){
155 if (preg_match("%".$mailObject->attrs['gosaMailServer'][0]."$%", $srv)){
156 $mailObject->attrs['gosaMailServer']= array(0 => $srv, "count" => 1);
157 break;
158 }
159 }
160 }
163 function fixAttributesOnStore(&$mailObject)
164 {
165 global $config;
167 /* If quota is empty, remove quota restrictions by setting quota to 0 */
168 if(isset($mailObject->gosaMailQuota) && (empty($mailObject->gosaMailQuota))){
169 $mailObject->attrs['gosaMailQuota'] = 0;
170 }
172 /* Convert attributes and objectClasses */
173 foreach ($this->attribute_map as $source => $dest){
174 if (isset($mailObject->attrs[$source])){
175 $mailObject->attrs[$dest]= $mailObject->attrs[$source];
176 unset ($mailObject->attrs[$source]);
177 }
178 }
179 $objectclasses= array();
180 foreach ($mailObject->attrs['objectClass'] as $oc){
181 if ($oc != 'kolabInetOrgPerson' && $oc != 'kolabSharedFolder'){
182 $objectclasses[]= $oc;
183 }
184 }
185 $mailObject->attrs['objectClass']= $objectclasses;
186 if (in_array("posixGroup", $mailObject->attrs['objectClass'])){
188 /* Add kolabSharedFoleder Class */
189 $mailObject->attrs['objectClass'][]= 'kolabSharedFolder';
191 /* Work on acl attribute */
192 $new_acl= array();
193 foreach ($mailObject->attrs['acl'] as $uacl){
195 /* Get user=(mail) & acls */
196 list($user, $acl) = split(" ", $uacl);
198 /* Add al users which have seperated acls
199 %members% are all users in this group,
200 which have the standard group acl
201 */
202 if ($user != "%members%"){
203 $new_acl[$user]= $uacl;
204 } else {
206 /* All groupmembers will be added */
207 $ldap = $config->get_ldap_link();
208 $ldap->cd($config->current['BASE']);
209 foreach ($mailObject->members as $member){
211 /* Get user mail address .... */
212 $ldap->search("(&(objectClass=person)(|(uid=".$member.")(mail=".$member.")))",array("mail"));
213 $res = $ldap->fetch();
215 /* Default mail address is set to uid -
216 So if there is no mail address defined the uid is added
217 */
218 $mail = $member;
220 /* Use mail address if it is available */
221 if(isset($res['mail'][0])){
222 $mail = $res['mail'][0];
223 }
225 /* only append this mail/permission string to acl,
226 if there arn't already some (special) configs for this user */
227 $found =false;
228 foreach($mailObject->imapacl as $mailA => $acl){
229 if(strtolower(trim($mailA))==strtolower(trim($mail))){
230 $found = true;
231 }
232 }
234 /* Skipp user, with no email adress too */
235 if($member == $mail){
236 $found = true;
237 }
239 /* Append new user acl */
240 if(!$found){
241 $new_acl[$member]= "$mail $acl";
242 }
244 /* Old line */
245 // $new_acl[$member]= "$member $acl";
246 }
247 }
248 }
250 /* Save shared folder target */
251 $mailObject->attrs['gosaSharedFolderTarget']= "kolab+shared.".$mailObject->uid;
253 /* Kolab shared folder names are like ' shared.uid@server.de '
254 So overwrite uid to match these folder names. Else we can't read quota settings etc.
255 #FIXME is there a better way to detect if it is 'shared.' or 'shared+' or 'kolab+shared.' or what ever ?*/
256 $mailObject->uid = "shared.".$mailObject->uid."@".preg_replace("/^.*@/","",$mailObject->mail);
258 /* Assign new acls */
259 $mailObject->attrs['acl']= array();
260 foreach ($new_acl as $key => $value){
261 $mailObject->attrs['acl'][]= $value;
262 }
263 } else {
264 $mailObject->attrs['objectClass'][]= 'kolabInetOrgPerson';
265 }
267 /* Remove imap:// tagging */
268 $mailObject->attrs['kolabHomeServer']= preg_replace('%imap://%', '', $mailObject->attrs['kolabHomeServer']);
269 $mailObject->attrs['gosaMailServer']= $mailObject->attrs['kolabHomeServer'];
270 $mailObject->attrs['kolabDeleteFlag']= array();
271 }
273 function fixAttributesOnRemove(&$mailObject)
274 {
275 /* Add attribute for object deletion and remove GOsa specific
276 values from entry. */
277 foreach($this->attribute_map as $kolabAttr){
278 $mailObject->attrs[$kolabAttr] = array();
279 }
281 /* Only add kolab delete Flag in case of an user.mailAccount */
282 if(!in_array("posixGroup", $mailObject->attrs['objectClass'])){
283 $mailObject->attrs['kolabDeleteFlag']= preg_replace('%imap://%', '', $mailObject->gosaMailServer);
284 }else{
285 /* Kolab shared folder names are like ' shared.uid@server.de '
286 So overwrite uid to match these folder names. Else we can't read quota settings etc.
287 #FIXME is there a better way to detect if it is 'shared.' or 'shared+' or 'kolab+shared.' or what ever ?*/
288 $mailObject->uid = "shared.".$mailObject->uid."@".preg_replace("/^.*@/","",$mailObject->mail);
290 }
291 }
293 }
295 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
296 ?>