Code

Updated account creation
[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   /* Account ID creation 
24       !!Depends on the attributes 'user_prefix'/'share_prefix' and the option 'cyrusUseSlashes'
26       Examples - based on defaults : 
27         %PREFIX%    => "user." or "user/" (Depending on cyrusUseSlashes=FALSE/TRUE) 
28         %CN%        => "technik"          (The groups cn)
29         %UID%       => "herbert"          (The users uid)
30         %MAIL%      => "herbert@domain.de"(The mail address)
31         %DOMAIN%    => "domain.de"        (The domain part of the specified mail)
32         %MAILPART%  => "herbert"          (The mail address without domain)
33         %UATTRIB%   => "herbert"/"herbert@domains.de"
34                                             (Configured in gosa.conf mailAttribute="mail"/"uid")
35    */
36   protected $user_id    = "%PREFIX%%UATTRIB%";  
37   protected $share_id   = "%PREFIX%%UATTRIB%";
39   /* Create accounts in cyrus style with '/' instead of '.' */
40   protected $cyrusUseSlashes= FALSE;
42   /* The atribute mapping for this class  Source --> Destination */
43   protected $attributes     = array();
44   protected $userObjectClasses = array();
45   protected $shareObjectClasses = array();
47   /* Enabled mail domain selection. If enabled getMailDomains must the domain parts */ 
48   protected $enableDomainSelection= FALSE;
49   protected $enableQuota          = TRUE;
50   protected $enableSieveManager   = FALSE;
51   protected $enableVacationRange  = TRUE;
52   protected $enableFolderTypes    = FALSE;
54   /* Default values */
55   protected $quotaValue   = 0;  
56   protected $quotaUsage   = 0;  
58   /* Method internal */
59   protected $type               = "user"; 
60   protected $account_id         = "";
61   protected $initial_account_id = "";
62   protected $connected          = FALSE;
63   protected $error              = "";
64   protected $parent             = NULL;   
65   protected $MailServer         = "";
67   protected $default_acls = array("__anyone__" => "p", "__member__" => "lrswp");
69   protected $acl_map = array(
70       "lrsw"     => "read",
71       "lrswp"    => "post",
72       "p"        => "external post",
73       "lrswip"   => "append",
74       "lrswipcd" => "write",
75       "lrswipcda"=> "admin",
76       " "         => "none");
78   protected $acl_mapping = array();
81   /*! \brief  Constructs the mail class 
82       @param  Object  Config  The GOsa configuration object
83       @param  Object  Plugin  The initator
84       @param  String          Open "user" or "group" account.
85    */
86   function __construct(&$config, &$parent, $type = "user")
87   {
88     $this->parent = $parent;
89     $this->config = $config;
91     /* Create a refernce to the mail selected server 
92      */
93     if(isset($this->parent->gosaMailServer)){
94       $this->MailServer = &$this->parent->gosaMailServer;
95     }else{
96       trigger_error("mailMethod with invalid parent object initialized.");
97     }
99     if(!in_array($this->type,array("user","group"))){
100       trigger_error("Unknown mail class type used '".$type."'.");
101     }else{
102       $this->type = $type;
103     }
105   }
107   
108   /*! \brief  Intialize attributes and config settings.
109    */
110   protected function init()
111   {
112     /* Get config value for cyrusUseSlashes
113      */
114     if ($this->config->get_cfg_value("cyrusUseSlashes") == "true"){
115       $this->cyrusUseSlashes = TRUE;
116       @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "","<b>MAIL:</b> cyrusUseSlashes: <b>Enabled</b>");
117     }else{
118       @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "","<b>MAIL:</b> cyrusUseSlashes: <b>Disabled</b>");
119     }
121     /* Check if the mail account identification attribute
122        is overridden in the configuration file
123      */
124     if($this->config->get_cfg_value("mailAttribute","mail") != ""){
125       $new_uattrib= strtolower($this->config->get_cfg_value("mailAttribute"));
126       if(in_array($new_uattrib,array("mail","uid"))){
127         $this->uattrib = $new_uattrib;
128       }else{
129         @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$new_uattrib."</b>",
130           "<b>MAIL:</b> Unsupported 'mailAttribute' in gosa configuration specified");
131         msg_dialog::display(_("Configuration error"), 
132             sprintf(_("The configured mail attribute '%s' is unsupported!"), $new_uattrib), ERROR_DIALOG);
133       }
134     }
136     /* Create ACL map */
137     foreach($this->acl_map as $acl => $name){
138       $this->acl_mapping[$acl] = _($name);
139     }
141     /* Check if we have an individual user/folder creation syntax 
142      */
143     $tmp = $this->config->get_cfg_value("mailUserCreation");
144     if(!empty($tmp)){
145       @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<i>".$tmp."</i>",
146           "<b>MAIL:</b> User creation set to");
147       $this->user_id  = $tmp;
148     }
149     $tmp = $this->config->get_cfg_value("mailFolderCreation");
150     if(!empty($tmp)){
151       @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<i>".$tmp."</i>",
152           "<b>MAIL:</b> Shared folder creation set to");
153       $this->share_id = $tmp;
154     }
156     $this->build_account_id();
157     $this->initial_account_id = $this->account_id;
158   }
160   
161   public function fixAttributesOnLoad()
162   {
163     foreach($this->attributes as $source => $dest){
164       if(isset($this->parent->attrs[$source])){
165         $this->parent->attrs[$dest] = $this->parent->attrs[$source];
166       }
167       if(isset($this->parent->$source)){
168         $this->parent->$dest = $this->parent->$source;
169       }
170       if(isset($this->parent->attrs[$source][0])){
171         $this->parent->saved_attributes[$source] = $this->parent->attrs[$source][0];
172       }
173     }
174   }
177   public function mailEqualsCN()
178   {
179     return($this->mailEqualsCN);
180   }
181   
183   public function fixAttributesOnRemove()
184   {
185     /* Remove objectClasses  
186      */ 
187     if($this->type == "user"){
188       $this->parent->attrs['objectClass'] = 
189         array_remove_entries_ics($this->userObjectClasses, $this->parent->attrs['objectClass']);
190     }else{
191       $this->parent->attrs['objectClass'] = 
192         array_remove_entries_ics($this->shareObjectClasses, $this->parent->attrs['objectClass']);
193     }
194     foreach($this->attributes as $source => $dest){
195       $this->attrs[$dest]   = array();
196       $this->attrs[$source] = array();
197     }
198   }
200   public function fixAttributesOnStore()
201   {
202     foreach($this->attributes as $source => $dest){
203       if(isset($this->parent->attrs[$dest])){
204         $this->parent->attrs[$source] = $this->parent->attrs[$dest ];
205       }
206       if(isset($this->parent->$dest)){
207         $this->parent->$source = $this->parent->$dest;
208       }
209     }
211     if($this->type == "user"){
212       $ocs = $this->userObjectClasses;
213     }else{
214       $ocs = $this->shareObjectClasses;
215     }
216     foreach($ocs as $oc){
217       if(!in_array($oc, $this->parent->attrs['objectClass'])){
218         $this->parent->attrs['objectClass'][] = $oc;
219       }
220     }
221   }
224   /*! \brief  Connect services like imap.
225               Not necessary for the base class.
226       @return Boolean True if this method is connected else false.
227    */
228   public function connect()
229   {
230     $this->reset_error();
231     @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"","<b>MAIL: Connect method</b>: ".get_class($this));
232     @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"","<b>MAIL: Current server</b>: ".$this->MailServer);
233  
234     $this->connected = TRUE;
235     return(TRUE);
236   }
239   /*! \brief  Returns the connection status of this method.
240       @return Boolean True if this method is connected else false.
241    */
242   public function is_connected()
243   {
244     return($this->connected);
245   }
248   /*! \brief  Disconnect this method. Close services like imap connection.
249               Not necessary for the base class.
250    */
251   public function disconnect()
252   {
253     $this->reset_error();
254     if($this->is_connected()){
255       @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"","<b>MAIL: Disconnect method</b>: ".get_class($this));
256       $this->connected =FALSE; 
257     }
258   }
260   
261   /*! \brief  Returns true the current object represents a valid account
262               (Some methods may check imap accounts here.)
263       @return Boolean TRUE if this is a valid account else FALSE
264   */
265   public function account_exists()
266   {
267     $this->reset_error();
268     return(TRUE);
269   }
270   
272   /*! \brief  Returns the last error occurred 
273       @return String  The last error message.
274    */
275   public function get_error(){
276     return($this->error);
277   }
280   public function isModifyableMail()
281   {
282     return($this->modifyableMail);
283   }
286   public function isModifyableServer()
287   {
288     return($this->modifyableServer);
289   }
292   /*! \brief  Returns TRUE if the action caused an error.
293       @return Boolean TRUE on error else FALSE
294    */
295   public function is_error(){
296     return($this->error != "");
297   }
300   /*! \brief  Resets the error message.
301    */
302   public function reset_error(){
303     $this->error = "";
304   }
307   public function get_account_id()
308   {
309     $this->build_account_id();
310     return($this->account_id);
311   }
313   /*! \brief  Create a new account id, like 'user/name@domain.com'.
314    */
315   protected function build_account_id()
316   {
317     /* Build account identicator */
318     if($this->type == "user"){
319       $prefix = $this->user_prefix;
320       $acc_string = $this->user_id;
321     }else{
322       $prefix = $this->share_prefix;
323       $acc_string = $this->share_id;
324     }
326     /* Create account prefix and respect "cyrusUseSlashes" 
327        Do not replace escaped dots for cyrusUseSlashes.
328      */
329     $uattrib = $this->uattrib;
330     if($this->cyrusUseSlashes){
331       $prefix = preg_replace('/([^\\\\])\./',"\\1/",$prefix);
332     }
333     $prefix = preg_replace("/\\\\([\.\/])/","\\1",$prefix);
334    
335     $domain = $mailpart = "";
336     $mail = $this->parent->mail;
337     if(preg_match("/\@/",$mail)){
338       list($mailpart,$domain) = split("\@",$mail);
339     }
341     /* Create account_id 
342      */ 
343     $from = array("/%cn%/i","/%uid%/i","/%prefix%/i","/%uattrib%/i","/%domain%/i","/%mailpart%/i","/%mail%/i");
344     $to   = array($this->parent->cn,$this->parent->uid,$prefix,$this->parent->$uattrib, $domain, $mailpart,$mail);
345     $acc_id = trim(strtolower(preg_replace($from,$to,$acc_string)));
347     /* Check for not replaced pattern.
348      */
349     if(preg_match("/%/",$acc_id)){
350       $notr = preg_replace("/^[^%]*/","",$acc_id);
351       if(!empty($notr)){
352         @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"<b>Warning</b>", 
353             sprintf("<b>MAIL: WARNING unknown pattern in account creation string '%s' near '%s'</b>", $acc_id, $notr));
355         /* Remove incomprehensible patterns */
356         $acc_id = preg_replace("/%[^%]+%/","",$acc_id);
357       }
358     }
361     if($this->account_id != $acc_id){
362       $this->account_id = $acc_id;
363       @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"", "<b>MAIL:</b> AccountID generated: <b>".$acc_id."</b>");
364     }
365   }
368   /*! \brief  Creates a valid folder id for a given folder name.
369                e.g. $folder_id = "INBOX/test"  &&  $this->account_id = "share/mailbox@domain.de"
370                will result in "share/mailbox/test@domain.de"
371               This function is mainly used to read and write folder permissions.
372       @return String A valid folder id
373    */
374   public function create_folder_id($folder, $type = "")
375   {
377     if(!empty($folder)){
378       $folder = trim(preg_replace("/^INBOX[\.\/]*/i","",$folder));
379     }
380     if(!empty($folder)){
381       $folder = "/".$folder;
382     }
384     /* Build account identicator */
385     if($this->type == "user"){
386       $prefix = $this->user_prefix;
387       $acc_string = $this->user_id;
388     }else{
389       $prefix = $this->share_prefix;
390       $acc_string = $this->share_id;
391     }
393     /* Create account prefix and respect "cyrusUseSlashes"
394        Do not replace escaped dots for cyrusUseSlashes.
395      */
396     $uattrib = $this->uattrib;
397     if($this->cyrusUseSlashes){
398       $prefix = preg_replace('/([^\\\\])\./',"\\1/",$prefix);
399     }
400     $prefix = preg_replace("/\\\\([\.\/])/","\\1",$prefix);
402     $domain = $mailpart = "";
403     $mail = $this->parent->mail;
404     if(preg_match("/\@/",$mail)){
405       list($mailpart,$domain) = split("\@",$mail);
406     }
408     /* Create account_id
409      */
410     $from = array("/%CN%/","/%UID%/","/%PREFIX%/","/%UATTRIB%/","/%DOMAIN%/","/%MAILPART%/","/%MAIL%/");
411     $to   = array($this->parent->cn,$this->parent->uid,$prefix,$this->parent->$uattrib, $domain, $mailpart,$mail);
413     $acc_id = trim(strtolower(preg_replace($from,$to,$acc_string)));
415     if(preg_match("/\@/",$acc_id)){
416       list($mail,$domain) = split("\@",$acc_id);
417       $str = trim($mail . $folder . "@" . $domain);
418     }else{
419       $str = trim($acc_id . $folder);
420     }
421     return($str) ;
422   }
425   /*! \brief  Returns the configured mail method for the given parent object, 
426                 initialized and read for use.
427       @return mailMethod  The configured mailMethod.
428    */
429   public function get_method()
430   {
431     $methods = mailMethod::get_methods();
432     if ($this->config->get_cfg_value("mailmethod") != ""){
433       $method= $this->config->get_cfg_value("mailmethod");
434       $cls = get_correct_class_name("mailMethod$method");
435       if(isset($methods[$cls])){
436         @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"", "<b>MAIL:</b> Selected mailMethod: <b>".$cls."</b>");
437         $tmp = new $cls($this->config,$this->parent,$this->type);
438         $tmp->init();
439         return($tmp);
440       }else{
441         @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "","<b>MAIL: Invalid mailMethod defined: <b>".$cls.
442             "</b> falling back to <b>".get_class($this)."</b>");
444         /* Print out configuration errors directly, we can't catch them everywhere. 
445          */
446         msg_dialog::display(_("Configuration error"), 
447             sprintf(_("Mail method '%s' is unknown!"), $method), ERROR_DIALOG);
448       }
449     }
451     /* If no valued mailMethod could be detected, return the base class.
452      */
453     $this->init();
454     return($this);
455   }
458   /*! \brief Saves sieve settings 
459    */
460   public function saveSieveSettings()
461   {
462     $this->reset_error();
463     return(TRUE);
464   }
466   
467   /*! \brief  Creates or Updates the mailAccount represented by this class. 
468    */
469   public function updateMailbox()
470   {
471     $this->reset_error();
472     return(TRUE);
473   }
476   /*! \brief  Update shared folder dependencies 
477    */
478   public function updateSharedFolder()
479   {
480     $this->reset_error();
481     return(TRUE);
482   }
485   /*! \brief  Removes the mailbox represented by this class,
486                and update shared folder ACLs .
487    */
488   public function deleteMailbox()
489   {
490     $this->reset_error();
492     @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id."</b>" ,
493         "<b>MAIL: Remove account</b> from server :".$this->MailServer);
495     return(TRUE);
496   }
498   
499   /*! \brief  Returns the used mail attribute (mail,uid)
500       @param  String  One out of 'mail','uid'
501    */
502   public function getUAttrib()
503   {
504     return($this->uattrib);
505   }
508   /*! \brief  Returns the used mail attribute (mail,uid)
509       @param  String  One out of 'mail','uid'
510    */
511   public function getUAttribValue()
512   {
513     $uattrib = $this->getUAttrib();
514     return($this->parent->$uattrib);
515   }
517   
518   /*! \brief  Returns whether the quota settings are enabled or not 
519       @return Boolean TRUE if enabled else FALSE
520    */
521   public function quotaEnabled()
522   {
523     return($this->enableQuota);
524   }
526   
527   /*! \brief  Returns the used quota 
528       @return Integer Quota used.
529    */
530   public function getQuotaUsage()
531   {
532     return(-1);
533   }
536   /*! \brief  Returns the quota restrictions.
537       @return Integer Quota restrictions.
538    */
539   public function getQuota($quotaValue)
540   {
541     return($quotaValue);
542   }
544   
545   /*! \brief  Sets the mail quota
546    */
547   public function setQuota($number)
548   {
549     if(!is_numeric($number)){
550       $number = (int) $number;
551       if(!$number){
552         $number = 0;
553       }
554     }
555     $this->quotaValue = $number; 
556     return(TRUE);
557   }
560   /*! \brief  Returns true whether the domain is selectable or not 
561    */
562   public function domainSelectionEnabled()
563   {
564     return($this->enableDomainSelection);
565   } 
568   /*! \brief Returns a list of configured mail domains 
569       @return Array A list of mail domains
570    */
571   public function getMailDomains()
572   {
573     return(array("Unconfigured"));
574   } 
576   
577   /*! \brief  Returns the used Spamlevels for this mailmethod 
578    */
579   public function getSpamLevels()
580   {
581     $spamlevel= array();
582     for ($i= 0; $i<21; $i++){
583       $spamlevel[]= $i;
584     }
585     return($spamlevel);
586   }
588   
589   /*! \brief  Returns the list of configured mailbox folders
590       @return Array The mailbox folders.
591    */
592   public function getMailboxList()
593   {
594     return(array("INBOX"));
595   }
598   /*! \brief  Returns whether the vacation range is selectable or not
599       @return Boolean TRUE, FALSE
600    */
601   public function vacationRangeEnabled()
602   {
603     return($this->enableVacationRange);
604   }
606   
607   /*! \brief  Returns true if the sieveManagement is allowed
608       @return Boolean TRUE, FALSE
609    */
610   public function allowSieveManagement()
611   {
612     return($this->enableSieveManager);
613   } 
616   /*! \brief  Checks dependencies to other GOsa plugins.
617    */
618   public function accountCreateable(&$reason = ""){
619     return(TRUE);
620   }
623   /*! \brief  Checks whether this account is removeable or not.
624               There may be some dependencies left, eg. kolab.
625    */
626   public function accountRemoveable(&$reason = ""){
627     return(TRUE);
628   }
630   
631   /*! \brief  Returns all mail servers configured in GOsa 
632                that are useable with this mailMethod.
633       @return Array  All useable mail servers.
634   */
635   public function getMailServers()
636   {
637     $mailserver = array();
638     $ui = get_userinfo();
639     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
640       if( $this->MailServer == $key ||
641           preg_match("/r/",$ui->get_permissions($val['server_dn'],"server/goImapServer",""))){
642         $mailserver[]= $key;
643       }
644     }
645     return($mailserver);
646   }
648   
649   /*! \brief  Returns the available mailMethods
650       @return Array   A list of all avaialable mailMethods_
651    */ 
652   static protected function get_methods()
653   {
654     global $class_mapping;
655     $available = array();
656     foreach($class_mapping as $class => $path){
657       if($class == "mailMethod") continue;
658       if(preg_match("/^mailMethod/",$class)){
659         $available[$class] = $class;
660       }
661     }
662     return($available);
663   }
665   
666   /* \brief   Some method require special folder types, "kolab" for example.
667       !! Those values are dummy values, the base class doesn't use folder types;
668      @return  Array Return folder types.
669    */
670   public function getAvailableFolderTypes()
671   {
672     $ret = array();
673     $ret['CAT'][''] = _("None"); 
674     $ret['SUB_CAT'][''][''] = _("None");
675     return($ret);
676   }
679   /* \brief  Returns the selected folder type. 
680       !! Those values are dummy values, the base class doesn't use folder types;
681      @return  Array  The folde type.
682    */
683   public function getFolderType($default)
684   {
685     return($default);
686   }
688  
689   /* \brief  Returns the selected folder type. 
690       !! Those values are dummy values, the base class doesn't use folder types;
691      @return  Array  The folde type.
692    */
693   public function setFolderType($type)
694   {
695     return(TRUE) ;   
696   }
699   /*! \brief  Returns configured acls 
700    */
701   public function  getFolderACLs($folder_acls)
702   {
703     /* Merge given ACL with acl mapping 
704        This ensures that no ACL will accidentally overwritten by gosa.
705      */
706     foreach($folder_acls as $user => $acl){
707       if(!isset($this->acl_mapping[$acl])){
708         $this->acl_mapping[$acl] = $acl;
709       }
710     }
712     return($folder_acls);
713   }
716   /*! \brief  Write ACLs back to imap or what ever 
717    */
718   public function  setFolderACLs($array)
719   {
720     return(TRUE);
721   }
724   /*! \brief  Returns a list of all possible acls.
725       @return Array   ACLs.
726   */
727   public function getAclTypes()
728   {
729     return( $this->acl_mapping);
730   }
732   public function folderTypesEnabled()
733   {
734     return($this->enableFolderTypes);
735   }
737   public function allow_remove(&$reason)
738   {
739     return(TRUE);
740   }
743   /*! \brief  Returns the configured mailMethod
744       @return String  the configured mail method or ""
745    */
746   static public function get_current_method($config)
747   {
748     global $class_mapping;
749     $method= $config->get_cfg_value("mailmethod");
750     $cls = get_correct_class_name("mailMethod$method");
751     foreach($class_mapping as $class => $path){
752       if($class == $cls){
753         return($class);
754       }
755     }
756     return("");
757   }
760   static function quota_to_image($use,$quota)
761   {
762     if($use == -1){
763       return("&nbsp;&nbsp;"._("Unknown"));
764     }elseif(empty($quota)){
765       return("&nbsp;&nbsp;"._("Unlimited"));
766     }else{
767       $usage =round(($use/$quota) * 100);
768       return("<img src='progress.php?x=100&amp;y=17&amp;p=$usage'>");
769     }
770   }
772  
773   /*! \brief  Returns the default sharedFolder ACLs for this method.
774       @return Array Returns an array containg acls for __member__ and __anyone__
775    */ 
776   public function getDefaultACLs()
777   {
778     $tmp = $this->default_acls;
779     if(!isset($tmp['__member__'])) $tmp['__member__'] = " ";
780     if(!isset($tmp['__anyone__'])) $tmp['__anyone__'] = " ";
781     return($tmp);
782   }
786 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
787 ?>