Code

Updated mail methods
[gosa.git] / gosa-plugins / mail / personal / mail / class_mail-methods.inc
1 <?php
4 class mailMethod{
6   /* Allow modification of account_ids for existing mail accounts */
7   protected $modifyableMail   = TRUE;
9   /* Allow modification of the mail server attribute existing mail accounts */
10   protected $modifyableServer = TRUE;
12   /* Enforces same value for 'mail' as used for 'cn' */
13   protected $mailEqualsCN   = FALSE; 
15   /* the attribute used to create accounts */ 
16   protected $uattrib        = "mail";  // Naming attribute for accounts, e.g. imap.
18   /* The account prefixes, keep the '.' here! See FAQ cyrusUseSlashes */
19   protected $user_prefix    = "user.";  
20   protected $share_prefix   = "share.";
22   /* Create accounts in cyrus style with '/' instead of '.' */
23   protected $cyrusUseSlashes= FALSE;
25   /* The atribute mapping for this class  Source --> Destination */
26   protected $attributes     = array();
27   protected $userObjectClasses = array();
28   protected $shareObjectClasses = array();
30   /* Enabled mail domain selection. If enabled getMailDomains must the domain parts */ 
31   protected $enableDomainSelection= FALSE;
32   protected $enableQuota          = TRUE;
33   protected $enableSieveManager   = FALSE;
34   protected $enableVacationRange  = TRUE;
35   protected $enableFolderTypes    = FALSE;
37   /* Default values */
38   protected $quotaValue   = 0;  
39   protected $quotaUsage   = 0;  
41   /* Method internal */
42   protected $type               = "user"; 
43   protected $account_id         = "";
44   protected $initial_account_id = "";
45   protected $connected          = FALSE;
46   protected $error              = "";
47   protected $parent             = NULL;   
48   protected $MailServer         = "";
50   protected $acl_map = array(
51       "lrsw"     => "read",
52       "lrswp"    => "post",
53       "p"        => "external post",
54       "lrswip"   => "append",
55       "lrswipcd" => "write",
56       "lrswipcda"=> "admin",
57       " "         => "none");
59   protected $acl_mapping = array();
60  
62   /*! \brief  Constructs the mail class 
63       @param  Object  Config  The GOsa configuration object
64       @param  Object  Plugin  The initator
65       @param  String          Open "user" or "group" account.
66    */
67   function __construct(&$config, &$parent, $type = "user")
68   {
69     $this->parent = $parent;
70     $this->config = $config;
72     /* Create a refernce to the mail selected server 
73      */
74     if(isset($this->parent->gosaMailServer)){
75       $this->MailServer = &$this->parent->gosaMailServer;
76     }else{
77       trigger_error("mailMethod with invalid parent object initialized.");
78     }
80     if(!in_array($this->type,array("user","group"))){
81       trigger_error("Unknown mail class type used '".$type."'.");
82     }else{
83       $this->type = $type;
84     }
85   }
87   
88   /*! \brief  Intialize attributes and config settings.
89    */
90   protected function init()
91   {
92     /* Get config value for cyrusUseSlashes
93      */
94     if ($this->config->get_cfg_value("cyrusUseSlashes") == "true"){
95       $this->cyrusUseSlashes = TRUE;
96       @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "cyrusUseSlashes: <b>Enabled</b>","");
97     }else{
98       @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "cyrusUseSlashes: <b>Disabled</b>","");
99     }
101     /* Check if the mail account identification attribute
102        is overridden in the configuration file
103      */
104     if($this->config->get_cfg_value("mailAttribute","mail") != ""){
105       $new_uattrib= strtolower($this->config->get_cfg_value("mailAttribute"));
106       if(in_array($new_uattrib,array("mail","uid"))){
107         $this->uattrib = $new_uattrib;
108       }else{
109         @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$new_uattrib."</b>",
110             "Unsupported 'mailAttribute' in gosa configuration specified");
111         msg_dialog::display(_("Configuration error"), 
112             sprintf(_("The configured mail attribute '%s' is unsupported!"), $new_uattrib), ERROR_DIALOG);
113       }
114     }
116     /* Create ACL map */
117     foreach($this->acl_map as $acl => $name){
118       $this->acl_mapping[$acl] = _($name);
119     }
121     $this->build_account_id();
122     $this->initial_account_id = $this->account_id;
123   }
125   
126   public function fixAttributesOnLoad()
127   {
128     foreach($this->attributes as $source => $dest){
129       if(isset($this->parent->attrs[$source])){
130         $this->parent->attrs[$dest] = $this->parent->attrs[$source];
131       }
132       if(isset($this->parent->$source)){
133         $this->parent->$dest = $this->parent->$source;
134       }
135       if(isset($this->parent->attrs[$source][0])){
136         $this->parent->saved_attributes[$source] = $this->parent->attrs[$source][0];
137       }
138     }
139   }
142   public function mailEqualsCN()
143   {
144     return($this->mailEqualsCN);
145   }
146   
148   public function fixAttributesOnRemove()
149   {
150     /* Remove objectClasses  
151      */ 
152     if($this->type == "user"){
153       $this->parent->attrs['objectClass'] = 
154         array_remove_entries_ics($this->userObjectClasses, $this->parent->attrs['objectClass']);
155     }else{
156       $this->parent->attrs['objectClass'] = 
157         array_remove_entries_ics($this->shareObjectClasses, $this->parent->attrs['objectClass']);
158     }
159     foreach($this->attributes as $source => $dest){
160       $this->attrs[$dest]   = array();
161       $this->attrs[$source] = array();
162     }
163   }
165   public function fixAttributesOnStore()
166   {
167     foreach($this->attributes as $source => $dest){
168       if(isset($this->parent->attrs[$dest])){
169         $this->parent->attrs[$source] = $this->parent->attrs[$dest ];
170       }
171       if(isset($this->parent->$dest)){
172         $this->parent->$source = $this->parent->$dest;
173       }
174     }
176     if($this->type == "user"){
177       $ocs = $this->userObjectClasses;
178     }else{
179       $ocs = $this->shareObjectClasses;
180     }
181     foreach($ocs as $oc){
182       if(!in_array($oc, $this->parent->attrs['objectClass'])){
183         $this->parent->attrs['objectClass'][] = $oc;
184       }
185     }
186   }
189   /*! \brief  Connect services like imap.
190               Not necessary for the base class.
191       @return Boolean True if this method is connected else false.
192    */
193   public function connect()
194   {
195     $this->reset_error();
196     @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"<b>Connect method</b>: ".get_class($this),"");
197     @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"<b>Current server</b>: ".$this->MailServer,"");
198  
199     $this->connected = TRUE;
200     return(TRUE);
201   }
204   /*! \brief  Returns the connection status of this method.
205       @return Boolean True if this method is connected else false.
206    */
207   public function is_connected()
208   {
209     return($this->connected);
210   }
213   /*! \brief  Disconnect this method. Close services like imap connection.
214               Not necessary for the base class.
215    */
216   public function disconnect()
217   {
218     $this->reset_error();
219     if($this->is_connected()){
220       @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"<b>Disconnect method</b>: ".get_class($this),"");
221       $this->connected =FALSE; 
222     }
223   }
225   
226   /*! \brief  Returns true the current object represents a valid account
227               (Some methods may check imap accounts here.)
228       @return Boolean TRUE if this is a valid account else FALSE
229   */
230   public function account_exists()
231   {
232     $this->reset_error();
233     return(TRUE);
234   }
235   
237   /*! \brief  Returns the last error occurred 
238       @return String  The last error message.
239    */
240   public function get_error(){
241     return($this->error);
242   }
245   public function isModifyableMail()
246   {
247     return($this->modifyableMail);
248   }
251   public function isModifyableServer()
252   {
253     return($this->modifyableServer);
254   }
257   /*! \brief  Returns TRUE if the action caused an error.
258       @return Boolean TRUE on error else FALSE
259    */
260   public function is_error(){
261     return($this->error != "");
262   }
265   /*! \brief  Resets the error message.
266    */
267   public function reset_error(){
268     $this->error = "";
269   }
272   public function get_account_id()
273   {
274     $this->build_account_id();
275     return($this->account_id);
276   }
278   /*! \brief  Create a new account id, like 'user/name@domain.com'.
279    */
280   protected function build_account_id()
281   {
282     /* Build account identicator */
283     if($this->type == "user"){
284       $str = $this->user_prefix;
285     }else{
286       $str = $this->share_prefix;
287     }
289     /* Create account prefix and respect "cyrusUseSlashes" 
290        Do not replace escaped dots for cyrusUseSlashes.
291      */
292     $uattrib = $this->uattrib;
293     if($this->cyrusUseSlashes){
294       $str = preg_replace('/([^\\\\])\./',"\\1/",$str);
295     }
296     $str = preg_replace("/\\\\([\.\/])/","\\1",$str);
297     $str = trim(strtolower($str . $this->parent->$uattrib));
299     if($this->account_id != $str){
300       $this->account_id = $str;
301       @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "accountID generated: <b>".$str."</b>","");
302     }
303   }
306   /*! \brief  Creates a valid folder id for a given folder name.
307                e.g. $folder_id = "INBOX/test"  &&  $this->account_id = "share/mailbox@domain.de"
308                will result in "share/mailbox/test@domain.de"
309               This function is mainly used to read and write folder permissions.
310       @return String A valid folder id
311    */
312   public function create_folder_id($folder, $type = "")
313   {
315     if(!empty($folder)){
316       $folder = trim(preg_replace("/^INBOX[\.\/]*/i","",$folder));
317     }
318     if(!empty($folder)){
319       $folder = "/".$folder;
320     }
322     /* Build account identicator */
323     if($type == ""){
324       $type = $this->type;
325     }
326     if($type == "user"){
327       $str = $this->user_prefix;
328     }else{
329       $str = $this->share_prefix;
330     } 
332     /* Create account prefix and respect "cyrusUseSlashes"
333        Do not replace escaped dots for cyrusUseSlashes.
334      */
335     $uattrib = $this->uattrib;
336     if($this->cyrusUseSlashes){
337       $str = preg_replace('/([^\\\\])\./',"\\1/",$str);
338     }
339     $str = preg_replace("/\\\\([\.\/])/","\\1",$str);
340     $str = trim(strtolower($str . $this->parent->$uattrib));
342     if(preg_match("/\@/",$this->parent->$uattrib)){
343       list($mail,$domain) = split("\@",$this->parent->$uattrib);
344       $str = trim(strtolower($str . $mail . $folder . "@" . $domain));
345     }else{
346       $str = trim(strtolower($str . $this->parent->$uattrib));
347     }
348     return($str) ;
349   }
352   /*! \brief  Returns the configured mail method for the given parent object, 
353                 initialized and read for use.
354       @return mailMethod  The configured mailMethod.
355    */
356   public function get_method()
357   {
358     $methods = mailMethod::get_methods();
359     if ($this->config->get_cfg_value("mailmethod") != ""){
360       $method= $this->config->get_cfg_value("mailmethod");
361       $cls = get_correct_class_name("mailMethod$method");
362       if(isset($methods[$cls])){
363         @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "Selected mailMethod: <b>".$cls."</b>","");
364         $tmp = new $cls($this->config,$this->parent,$this->type);
365         $tmp->init();
366         return($tmp);
367       }else{
368         @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "Invalid mailMethod defined <b>".$cls.
369             "</b> falling back to <b>".get_class($this)."</b>","");
371         /* Print out configuration errors directly, we can't catch them everywhere. 
372          */
373         msg_dialog::display(_("Configuration error"), 
374             sprintf(_("Mail method '%s' is unknown!"), $method), ERROR_DIALOG);
375       }
376     }
378     /* If no valued mailMethod could be detected, return the base class.
379      */
380     $this->init();
381     return($this);
382   }
385   /*! \brief Saves sieve settings 
386    */
387   public function saveSieveSettings()
388   {
389     $this->reset_error();
390     return(TRUE);
391   }
393   
394   /*! \brief  Creates or Updates the mailAccount represented by this class. 
395    */
396   public function updateMailbox()
397   {
398     $this->reset_error();
399     return(TRUE);
400   }
403   /*! \brief  Update shared folder dependencies 
404    */
405   public function updateSharedFolder()
406   {
407     $this->reset_error();
408     return(TRUE);
409   }
412   /*! \brief  Removes the mailbox represented by this class,
413                and update shared folder ACLs .
414    */
415   public function deleteMailbox()
416   {
417     $this->reset_error();
419     @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id."</b>" ,
420         "<b>Remove account</b> from server :".$this->MailServer);
422     return(TRUE);
424     /* No imap actions here, just updated shared folder membership 
425      */
426     $ldap = $this->config->get_ldap_link();
427     $ldap->cd($this->config->current['BASE']);
428     $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaMailAccount)(memberUid=".$account."))",array('dn','cn'));
429     if(class_exists("grouptabs")){
430       while($attrs = $ldap->fetch()){
431         $tmp = new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $attrs['dn']);
432         if(isset($tmp->by_object['mailgroup'])){
433           $tmp->by_object['mailgroup']->members= $tmp->by_object['group']->memberUid;
434           if(!$this->is_account){
435             $tmp->by_object['mailgroup']->removeUserAcl($account);
436             $tmp->by_object['mailgroup']->removeUserAcl($this->mail);
437           }
438           $tmp->by_object['mailgroup']->save();
439         }
440       }
441     }
442     return(TRUE);
443   }
445   
446   /*! \brief  Returns the used mail attribute (mail,uid)
447       @param  String  One out of 'mail','uid'
448    */
449   public function getUAttrib()
450   {
451     return($this->uattrib);
452   }
455   /*! \brief  Returns the used mail attribute (mail,uid)
456       @param  String  One out of 'mail','uid'
457    */
458   public function getUAttribValue()
459   {
460     $uattrib = $this->getUAttrib();
461     return($this->parent->$uattrib);
462   }
464   
465   /*! \brief  Returns whether the quota settings are enabled or not 
466       @return Boolean TRUE if enabled else FALSE
467    */
468   public function quotaEnabled()
469   {
470     return($this->enableQuota);
471   }
473   
474   /*! \brief  Returns the used quota 
475       @return Integer Quota used.
476    */
477   public function getQuotaUsage()
478   {
479     return(-1);
480   }
483   /*! \brief  Returns the quota restrictions.
484       @return Integer Quota restrictions.
485    */
486   public function getQuota($quotaValue)
487   {
488     return($quotaValue);
489   }
491   
492   /*! \brief  Sets the mail quota
493    */
494   public function setQuota($number)
495   {
496     if(!is_numeric($number)){
497       $number = (int) $number;
498       if(!$number){
499         $number = 0;
500       }
501     }
502     $this->quotaValue = $number; 
503     return(TRUE);
504   }
507   /*! \brief  Returns true whether the domain is selectable or not 
508    */
509   public function domainSelectionEnabled()
510   {
511     return($this->enableDomainSelection);
512   } 
515   /*! \brief Returns a list of configured mail domains 
516       @return Array A list of mail domains
517    */
518   public function getMailDomains()
519   {
520     return(array("Unconfigured"));
521   } 
523   
524   /*! \brief  Returns the used Spamlevels for this mailmethod 
525    */
526   public function getSpamLevels()
527   {
528     $spamlevel= array();
529     for ($i= 0; $i<21; $i++){
530       $spamlevel[]= $i;
531     }
532     return($spamlevel);
533   }
535   
536   /*! \brief  Returns the list of configured mailbox folders
537       @return Array The mailbox folders.
538    */
539   public function getMailboxList()
540   {
541     return(array("INBOX"));
542   }
545   /*! \brief  Returns whether the vacation range is selectable or not
546       @return Boolean TRUE, FALSE
547    */
548   public function vacationRangeEnabled()
549   {
550     return($this->enableVacationRange);
551   }
553   
554   /*! \brief  Returns true if the sieveManagement is allowed
555       @return Boolean TRUE, FALSE
556    */
557   public function allowSieveManagement()
558   {
559     return($this->enableSieveManager);
560   } 
563   /*! \brief  Checks dependencies to other GOsa plugins.
564    */
565   public function accountCreateable(&$reason = ""){
566     return(TRUE);
567   }
570   /*! \brief  Checks whether this account is removeable or not.
571               There may be some dependencies left, eg. kolab.
572    */
573   public function accountRemoveable(&$reason = ""){
574     return(TRUE);
575   }
577   
578   /*! \brief  Returns all mail servers configured in GOsa 
579                that are useable with this mailMethod.
580       @return Array  All useable mail servers.
581   */
582   public function getMailServers()
583   {
584     $mailserver = array();
585     $ui = get_userinfo();
586     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
587       if( $this->MailServer == $key ||
588           preg_match("/r/",$ui->get_permissions($val['server_dn'],"server/goImapServer",""))){
589         $mailserver[]= $key;
590       }
591     }
592     return($mailserver);
593   }
595   
596   /*! \brief  Returns the available mailMethods
597       @return Array   A list of all avaialable mailMethods_
598    */ 
599   static protected function get_methods()
600   {
601     global $class_mapping;
602     $available = array();
603     foreach($class_mapping as $class => $path){
604       if($class == "mailMethod") continue;
605       if(preg_match("/^mailMethod/",$class)){
606         $available[$class] = $class;
607       }
608     }
609     return($available);
610   }
612   
613   /* \brief   Some method require special folder types, "kolab" for example.
614       !! Those values are dummy values, the base class doesn't use folder types;
615      @return  Array Return folder types.
616    */
617   public function getAvailableFolderTypes()
618   {
619     $ret = array();
620     $ret['CAT'][''] = _("None"); 
621     $ret['SUB_CAT'][''][''] = _("None");
622     return($ret);
623   }
626   /* \brief  Returns the selected folder type. 
627       !! Those values are dummy values, the base class doesn't use folder types;
628      @return  Array  The folde type.
629    */
630   public function getFolderType($default)
631   {
632     return($default);
633   }
635  
636   /* \brief  Returns the selected folder type. 
637       !! Those values are dummy values, the base class doesn't use folder types;
638      @return  Array  The folde type.
639    */
640   public function setFolderType($type)
641   {
642     return(TRUE) ;   
643   }
646   /*! \brief  Returns configured acls 
647    */
648   public function  getFolderACLs($folder_acls)
649   {
650     /* Merge given ACL with acl mapping 
651        This ensures that no ACL will accidentally overwritten by gosa.
652      */
653     foreach($folder_acls as $user => $acl){
654       if(!isset($this->acl_mapping[$acl])){
655         $this->acl_mapping[$acl] = $acl;
656       }
657     }
659     return($folder_acls);
660   }
663   /*! \brief  Write ACLs back to imap or what ever 
664    */
665   public function  setFolderACLs($array)
666   {
667     return(TRUE);
668   }
671   /*! \brief  Returns a list of all possible acls.
672       @return Array   ACLs.
673   */
674   public function getAclTypes()
675   {
676     return( $this->acl_mapping);
677   }
679   public function folderTypesEnabled()
680   {
681     return($this->enableFolderTypes);
682   }
684   public function allow_remove(&$reason)
685   {
686     return(TRUE);
687   }
690   /*! \brief  Returns the configured mailMethod
691       @return String  the configured mail method or ""
692    */
693   static public function get_current_method($config)
694   {
695     global $class_mapping;
696     $method= $config->get_cfg_value("mailmethod");
697     $cls = get_correct_class_name("mailMethod$method");
698     foreach($class_mapping as $class => $path){
699       if($class == $cls){
700         return($class);
701       }
702     }
703     return("");
704   }
709 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
710 ?>