ff6bb450f706f3894dd4dfa52e7529bb16e8e652
[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-2012 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      * postfix backend const
40      * 
41      * @staticvar string
42      */
43     const POSTFIX    = 'Postfix';
44
45     /**
46      * imap ldap backend const
47      * 
48      * @staticvar string
49      */
50     const LDAP_IMAP      = 'Ldap_imap';
51
52     /**
53      * smtp ldap backend const
54      * 
55      * @staticvar string
56      */
57     const LDAP_SMTP      = 'Ldapsmtp';
58     
59     /**
60      * smtp ldap mail attribute backend const
61      * 
62      * @staticvar string
63      */
64     const LDAP_SMTP_MAIL      = 'Ldapsmtpmail';
65     
66     /**
67      * smtp ldap backend const
68      * 
69      * @staticvar string
70      */
71     const LDAP_SMTP_QMAIL      = 'Ldapsmtpqmail';
72
73     /**
74      * cyrus backend const
75      * 
76      * @staticvar string
77      */
78     const CYRUS    = 'Cyrus';
79     
80     /**
81      * backend object instances
82      * 
83      * @var array
84      */
85     private static $_backends = array();
86     
87     /**
88      * configs as static class var to minimize db queries
89      *  
90      * @var array
91      */
92     private static $_configs = array();
93     
94     /**
95      * the constructor
96      *
97      * don't use the constructor. use the singleton 
98      */
99     private function __construct() 
100     {
101     }
102     
103     /**
104      * don't clone. Use the singleton.
105      *
106      */
107     private function __clone() 
108     {
109     }
110     
111     /**
112      * the singleton pattern
113      *
114      * @param string $_configType
115      * @return Tinebase_User_Plugin_Abstract
116      */
117     public static function getInstance($_configType = Tinebase_Config::IMAP) 
118     {
119         $backendType = self::getConfiguredBackend($_configType);
120         #if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ .' Email user backend: ' . $backendType);
121         
122         return self::factory($backendType);
123     }
124     
125     /**
126      * return an instance of the current backend
127      *
128      * @param   string $_type name of the backend
129      * @return  Tinebase_User_Plugin_Abstract
130      * @throws  Tinebase_Exception_InvalidArgument
131      */
132     public static function factory($_type = NULL) 
133     {
134         switch($_type) {
135             case self::LDAP_IMAP:
136                 if (!isset(self::$_backends[$_type])) {
137                     self::$_backends[$_type] = new Tinebase_EmailUser_Imap_LdapDbmailSchema();
138                 }
139                 break;
140                 
141             case self::DBMAIL:
142                 if (!isset(self::$_backends[$_type])) {
143                     self::$_backends[$_type] = new Tinebase_EmailUser_Imap_Dbmail();
144                 }
145                 break;
146             
147             case self::CYRUS:
148                 if (!isset(self::$_backends[$_type])) {
149                     self::$_backends[$_type] = new Tinebase_EmailUser_Imap_Cyrus();
150                 }
151                 break;
152                 
153             case self::POSTFIX:
154                 if (!isset(self::$_backends[$_type])) {
155                     self::$_backends[$_type] = new Tinebase_EmailUser_Smtp_Postfix();
156                 }
157                 break;
158                 
159             case self::LDAP_SMTP:
160                 if (!isset(self::$_backends[$_type])) {
161                     self::$_backends[$_type] = new Tinebase_EmailUser_Smtp_LdapDbmailSchema();
162                 }
163                 break;
164                 
165             case self::LDAP_SMTP_MAIL:
166                 if (!isset(self::$_backends[$_type])) {
167                     self::$_backends[$_type] = new Tinebase_EmailUser_Smtp_LdapMailSchema();
168                 }
169                 break;
170                 
171             case self::LDAP_SMTP_QMAIL:
172                 if (!isset(self::$_backends[$_type])) {
173                     self::$_backends[$_type] = new Tinebase_EmailUser_Smtp_LdapQmailSchema();
174                 }
175                 break;
176             
177             case self::DOVECOT_IMAP:
178                 if (!isset(self::$_backends[$_type])) {
179                     self::$_backends[$_type] = new Tinebase_EmailUser_Imap_Dovecot();
180                 }
181                 break;
182                 
183             default:
184                 throw new Tinebase_Exception_InvalidArgument("Backend type $_type not implemented.");
185         }
186         
187         $result = self::$_backends[$_type];
188         
189         return $result;
190     }
191     
192     /**
193      * returns the configured backend
194      * 
195      * @param string $_configType
196      * @return string
197      * @throws Tinebase_Exception_NotFound
198      */
199     public static function getConfiguredBackend($_configType = Tinebase_Config::IMAP)
200     {
201         $result = '';
202         
203         $config = self::getConfig($_configType);
204         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($config, TRUE));
205         
206         if (isset($config['backend'])) {
207             $backend = ucfirst(strtolower($config['backend']));
208             switch ($_configType) {
209                 case Tinebase_Config::IMAP:
210                     if ($backend == self::DBMAIL) {
211                         $result = self::DBMAIL;
212                     } else if ($backend == self::LDAP_IMAP) {
213                         $result = self::LDAP_IMAP;
214                     } else if ($backend == self::CYRUS) {
215                         $result = self::CYRUS;
216                     } else if ($backend == self::DOVECOT_IMAP) {
217                         $result = self::DOVECOT_IMAP;
218                     } 
219                     break;
220                 case Tinebase_Config::SMTP:
221                     if ($backend == self::POSTFIX) {
222                         $result = self::POSTFIX;
223                     } else if ($backend == self::LDAP_SMTP) {
224                         $result = self::LDAP_SMTP;
225                     } else if ($backend == self::LDAP_SMTP_MAIL) {
226                         $result = self::LDAP_SMTP_MAIL;
227                     } else if ($backend == self::LDAP_SMTP_QMAIL) {
228                         $result = self::LDAP_SMTP_QMAIL;
229                     }
230                     break;
231             }
232         }
233
234         if (empty($result)) {
235             throw new Tinebase_Exception_NotFound("Config for type $_configType / $backend not found.");
236         }
237         
238         return $result;
239     }
240     
241     /**
242      * merge two email users
243      * 
244      * @param Tinebase_Model_EmailUser $_emailUserImap
245      * @param Tinebase_Model_EmailUser $_emailUserSmtp
246      * @return Tinebase_Model_EmailUser|NULL
247      */
248     public static function merge($_emailUserImap, $_emailUserSmtp)
249     {
250         $result = NULL;
251         
252         if ($_emailUserImap !== NULL && $_emailUserSmtp !== NULL) {
253             // merge
254             $_emailUserImap->emailAliases = $_emailUserSmtp->emailAliases;
255             $_emailUserImap->emailForwards = $_emailUserSmtp->emailForwards;
256             $_emailUserImap->emailForwardOnly = $_emailUserSmtp->emailForwardOnly;
257             $_emailUserImap->emailAddress = $_emailUserSmtp->emailAddress;
258             $result = $_emailUserImap;
259             
260         } else if ($_emailUserImap !== NULL) {
261             $result =  $_emailUserImap;
262             
263         } else if ($_emailUserSmtp !== NULL) {
264             $result =  $_emailUserSmtp;
265         }
266         
267         return $result;
268     }
269     
270     /**
271      * check if email users are managed for backend/config type
272      * 
273      * @param string $_configType IMAP/SMTP
274      * @return boolean
275      */
276     public static function manages($_configType)
277     {
278         $config = self::getConfig($_configType);
279         
280         $result = (isset($config['backend']) && ! empty($config['backend']) && $config['backend'] != 'standard' && isset($config['active']) && $config['active'] == true);
281         
282         return $result;
283     }
284     
285     /**
286      * get config for type IMAP/SMTP
287      * 
288      * @param string $_configType
289      * @return array
290      */
291     public static function getConfig($_configType)
292     {
293         if (!isset(self::$_configs[$_configType])) {
294             self::$_configs[$_configType] = Tinebase_Config::getInstance()->get($_configType, new Tinebase_Config_Struct())->toArray();
295         }
296         
297         return self::$_configs[$_configType];
298     }
299 }