allow to define supported SQL adapter in email plugin
[tine20] / tine20 / Tinebase / EmailUser.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     Tinebase
6  * @subpackage  User
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @copyright   Copyright (c) 2009-2015 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Philipp Schüle <p.schuele@metaways.de>
10  * 
11  * @todo        think about splitting email user model in two (imap + smtp)
12  */
13
14 /**
15  * class Tinebase_EmailUser
16  * 
17  * Email Account Managing
18  * 
19  * @package Tinebase
20  * @subpackage User
21  */
22 class Tinebase_EmailUser
23 {
24     /**
25      * dbmail backend const
26      * 
27      * @staticvar string
28      */
29     const DBMAIL    = 'Dbmail';
30     
31     /**
32      * Dovecot imap backend const
33      * 
34      * @staticvar string
35      */
36     const DOVECOT_IMAP    = 'Dovecot_imap';
37     
38     /**
39      * Dovecot imap combined backend const
40      * 
41      * @staticvar string
42      */
43     const DOVECOT_IMAP_COMBINED    = 'Dovecot_imap_combined';
44     
45     /**
46      * postfix backend const
47      * 
48      * @staticvar string
49      */
50     const POSTFIX    = 'Postfix';
51     
52     /**
53      * postfix backend const
54      * 
55      * @staticvar string
56      */
57     const POSTFIX_COMBINED    = 'Postfix_combined';
58     
59     /**
60      * imap ldap backend const
61      * 
62      * @staticvar string
63      */
64     const LDAP_IMAP      = 'Ldap_imap';
65     
66     /**
67      * smtp ldap backend const
68      * 
69      * @staticvar string
70      */
71     const LDAP_SMTP      = 'Ldapsmtp';
72     
73     /**
74      * smtp ldap mail attribute backend const
75      * 
76      * @staticvar string
77      */
78     const LDAP_SMTP_MAIL      = 'Ldapsmtpmail';
79     
80     /**
81      * smtp ldap backend const
82      * 
83      * @staticvar string
84      */
85     const LDAP_SMTP_QMAIL      = 'Ldapsmtpqmail';
86     
87     /**
88      * cyrus backend const
89      * 
90      * @staticvar string
91      */
92     const CYRUS    = 'Cyrus';
93     
94     /**
95      * backend object instances
96      * 
97      * @var array
98      */
99     private static $_backends = array();
100     
101     /**
102      * configs as static class var to minimize db queries
103      *  
104      * @var array
105      */
106     private static $_configs = array();
107     
108     /**
109      * the constructor
110      *
111      * don't use the constructor. use the singleton 
112      */
113     private function __construct() 
114     {
115     }
116     
117     /**
118      * don't clone. Use the singleton.
119      *
120      */
121     private function __clone() 
122     {
123     }
124     
125     /**
126      * the singleton pattern
127      *
128      * @param string $_configType
129      * @return Tinebase_User_Plugin_Abstract
130      */
131     public static function getInstance($_configType = Tinebase_Config::IMAP) 
132     {
133         $backendType = self::getConfiguredBackend($_configType);
134         #if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ .' Email user backend: ' . $backendType);
135         
136         return self::factory($backendType);
137     }
138     
139     /**
140      * return an instance of the current backend
141      *
142      * @param   string $_type name of the backend
143      * @return  Tinebase_User_Plugin_Abstract
144      * @throws  Tinebase_Exception_InvalidArgument
145      */
146     public static function factory($_type = NULL) 
147     {
148         switch($_type) {
149             case self::LDAP_IMAP:
150                 if (!isset(self::$_backends[$_type])) {
151                     self::$_backends[$_type] = new Tinebase_EmailUser_Imap_LdapDbmailSchema();
152                 }
153                 break;
154                 
155             case self::DBMAIL:
156                 if (!isset(self::$_backends[$_type])) {
157                     self::$_backends[$_type] = new Tinebase_EmailUser_Imap_Dbmail();
158                 }
159                 break;
160             
161             case self::CYRUS:
162                 if (!isset(self::$_backends[$_type])) {
163                     self::$_backends[$_type] = new Tinebase_EmailUser_Imap_Cyrus();
164                 }
165                 break;
166                 
167             case self::POSTFIX:
168                 if (!isset(self::$_backends[$_type])) {
169                     self::$_backends[$_type] = new Tinebase_EmailUser_Smtp_Postfix();
170                 }
171                 break;
172                 
173             case self::POSTFIX_COMBINED:
174                 if (!isset(self::$_backends[$_type])) {
175                     self::$_backends[$_type] = new Tinebase_EmailUser_Smtp_PostfixCombined();
176                 }
177                 break;
178                 
179             case self::LDAP_SMTP:
180                 if (!isset(self::$_backends[$_type])) {
181                     self::$_backends[$_type] = new Tinebase_EmailUser_Smtp_LdapDbmailSchema();
182                 }
183                 break;
184                 
185             case self::LDAP_SMTP_MAIL:
186                 if (!isset(self::$_backends[$_type])) {
187                     self::$_backends[$_type] = new Tinebase_EmailUser_Smtp_LdapMailSchema();
188                 }
189                 break;
190                 
191             case self::LDAP_SMTP_QMAIL:
192                 if (!isset(self::$_backends[$_type])) {
193                     self::$_backends[$_type] = new Tinebase_EmailUser_Smtp_LdapQmailSchema();
194                 }
195                 break;
196                 
197             case self::DOVECOT_IMAP:
198                 if (!isset(self::$_backends[$_type])) {
199                     self::$_backends[$_type] = new Tinebase_EmailUser_Imap_Dovecot();
200                 }
201                 break;
202                 
203             case self::DOVECOT_IMAP_COMBINED:
204                 if (!isset(self::$_backends[$_type])) {
205                     self::$_backends[$_type] = new Tinebase_EmailUser_Imap_DovecotCombined();
206                 }
207                 break;
208                 
209             default:
210                 throw new Tinebase_Exception_InvalidArgument("Backend type $_type not implemented.");
211         }
212         
213         $result = self::$_backends[$_type];
214         
215         return $result;
216     }
217     
218     /**
219      * returns the configured backend
220      * 
221      * @param string $_configType
222      * @return string
223      * @throws Tinebase_Exception_NotFound
224      */
225     public static function getConfiguredBackend($_configType = Tinebase_Config::IMAP)
226     {
227         $result = '';
228         
229         $config = self::getConfig($_configType);
230         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($config, TRUE));
231         
232         if (isset($config['backend'])) {
233             $backend = ucfirst(strtolower($config['backend']));
234             switch ($_configType) {
235                 case Tinebase_Config::IMAP:
236                     if ($backend == self::DBMAIL) {
237                         $result = self::DBMAIL;
238                     } else if ($backend == self::LDAP_IMAP) {
239                         $result = self::LDAP_IMAP;
240                     } else if ($backend == self::CYRUS) {
241                         $result = self::CYRUS;
242                     } else if ($backend == self::DOVECOT_IMAP) {
243                         $result = self::DOVECOT_IMAP;
244                     } 
245                     break;
246                 case Tinebase_Config::SMTP:
247                     if ($backend == self::POSTFIX) {
248                         $result = self::POSTFIX;
249                     } else if ($backend == self::LDAP_SMTP) {
250                         $result = self::LDAP_SMTP;
251                     } else if ($backend == self::LDAP_SMTP_MAIL) {
252                         $result = self::LDAP_SMTP_MAIL;
253                     } else if ($backend == self::LDAP_SMTP_QMAIL) {
254                         $result = self::LDAP_SMTP_QMAIL;
255                     }
256                     break;
257             }
258         }
259         
260         if (empty($result)) {
261             throw new Tinebase_Exception_NotFound("Config for type $_configType / $backend not found.");
262         }
263         
264         return $result;
265     }
266     
267     /**
268      * merge two email users
269      * 
270      * @param Tinebase_Model_EmailUser $_emailUserImap
271      * @param Tinebase_Model_EmailUser $_emailUserSmtp
272      * @return Tinebase_Model_EmailUser|NULL
273      */
274     public static function merge($_emailUserImap, $_emailUserSmtp)
275     {
276         $result = NULL;
277         
278         if ($_emailUserImap !== NULL && $_emailUserSmtp !== NULL) {
279             // merge
280             $_emailUserImap->emailAliases = $_emailUserSmtp->emailAliases;
281             $_emailUserImap->emailForwards = $_emailUserSmtp->emailForwards;
282             $_emailUserImap->emailForwardOnly = $_emailUserSmtp->emailForwardOnly;
283             $_emailUserImap->emailAddress = $_emailUserSmtp->emailAddress;
284             $result = $_emailUserImap;
285             
286         } else if ($_emailUserImap !== NULL) {
287             $result =  $_emailUserImap;
288             
289         } else if ($_emailUserSmtp !== NULL) {
290             $result =  $_emailUserSmtp;
291         }
292         
293         return $result;
294     }
295     
296     /**
297      * check if email users are managed for backend/config type
298      * 
299      * @param string $_configType IMAP/SMTP
300      * @return boolean
301      */
302     public static function manages($_configType)
303     {
304         $config = self::getConfig($_configType);
305         
306         $result = (isset($config['backend']) && ! empty($config['backend']) && $config['backend'] != 'standard' && isset($config['active']) && $config['active'] == true);
307         
308         return $result;
309     }
310     
311     /**
312      * get config for type IMAP/SMTP
313      * 
314      * @param string $_configType
315      * @return array
316      */
317     public static function getConfig($_configType)
318     {
319         if (!isset(self::$_configs[$_configType])) {
320             self::$_configs[$_configType] = Tinebase_Config::getInstance()->get($_configType, new Tinebase_Config_Struct())->toArray();
321         }
322         
323         return self::$_configs[$_configType];
324     }
325 }