0011960: print with 300 dpi by default
[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      * Imap Cyrus backend const
26      * 
27      * @staticvar string
28      */
29     const IMAP_CYRUS            = 'Imap_Cyrus';
30     
31     /**
32      * Imap standard backend const
33      * 
34      * @staticvar string
35      */
36     const IMAP_STANDARD         = 'Imap_Standard';
37     
38     /**
39      * Imap DBmail backend const
40      * 
41      * @staticvar string
42      */
43     const IMAP_DBMAIL           = 'Imap_Dbmail';
44     
45     /**
46      * Imap Dovecot backend const
47      * 
48      * @staticvar string
49      */
50     const IMAP_DOVECOT          = 'Imap_Dovecot_imap';
51     
52     /**
53      * Imap Dovecot combined backend const
54      * 
55      * @staticvar string
56      */
57     const IMAP_DOVECOT_COMBINED = 'Imap_Dovecotcombined';
58     
59     /**
60      * Smtp Postfix backend const
61      * 
62      * @staticvar string
63      */
64     const SMTP_POSTFIX          = 'Smtp_Postfix';
65     
66     /**
67      * Smtp Postfix backend const
68      * 
69      * @staticvar string
70      */
71     const SMTP_POSTFIX_COMBINED = 'Smtp_Postfixcombined';
72     
73     /**
74      * Imap ldap backend const
75      * 
76      * @staticvar string
77      */
78     const IMAP_LDAP             = 'Imap_Ldap_imap';
79     
80     /**
81      * Smtp Ldap backend const
82      * 
83      * @staticvar string
84      */
85     const SMTP_LDAP             = 'Smtp_Ldapsmtp';
86     
87     /**
88      * Smtp Ldap mail attribute backend const
89      * 
90      * @staticvar string
91      */
92     const SMTP_LDAP_MAIL        = 'Smtp_Ldapsmtpmail';
93     
94     /**
95      * Smtp Ldap backend const
96      * 
97      * @staticvar string
98      */
99     const SMTP_LDAP_QMAIL       = 'Smtp_Ldapsmtpqmail';
100     
101     /**
102      * univention smtp ldap backend const
103      * 
104      * @staticvar string
105      */
106     const SMTP_LDAP_UNIVENTION  = 'Smtp_Ldap_univention';
107
108     /**
109      * univention imap ldap backend const
110      * 
111      * @staticvar string
112      */
113     const IMAP_LDAP_UNIVENTION  = 'Imap_Ldap_univention';
114
115     /**
116      * simpleMail smtp ldap backend const
117      *
118      * @staticvar string
119      */
120     const SMTP_LDAP_SIMPLEMAIL  = 'Smtp_Ldap_simplemail';
121
122     /**
123      * Smtp standard backend const
124      * 
125      * @staticvar string
126      */
127     const SMTP_STANDARD         = 'Smtp_Standard';
128     
129     /**
130      * supported backends
131      * 
132      * @var array
133      */
134     protected static $_supportedBackends = array(
135         self::IMAP_CYRUS            => 'Tinebase_EmailUser_Imap_Cyrus',
136         self::IMAP_DBMAIL           => 'Tinebase_EmailUser_Imap_Dbmail',
137         self::IMAP_DOVECOT          => 'Tinebase_EmailUser_Imap_Dovecot',
138         self::IMAP_DOVECOT_COMBINED => 'Tinebase_EmailUser_Imap_DovecotCombined',
139         self::IMAP_STANDARD         => 'Tinebase_EmailUser_Imap_Standard',
140         self::IMAP_LDAP             => 'Tinebase_EmailUser_Imap_LdapDbmailSchema',
141         self::IMAP_LDAP_UNIVENTION  => 'Tinebase_EmailUser_Imap_LdapUniventionMailSchema',
142         self::SMTP_LDAP             => 'Tinebase_EmailUser_Smtp_LdapDbmailSchema',
143         self::SMTP_LDAP_MAIL        => 'Tinebase_EmailUser_Smtp_LdapMailSchema',
144         self::SMTP_LDAP_QMAIL       => 'Tinebase_EmailUser_Smtp_LdapQmailSchema',
145         self::SMTP_LDAP_UNIVENTION  => 'Tinebase_EmailUser_Smtp_LdapUniventionMailSchema',
146         self::SMTP_LDAP_SIMPLEMAIL  => 'Tinebase_EmailUser_Smtp_LdapSimpleMailSchema',
147         self::SMTP_POSTFIX          => 'Tinebase_EmailUser_Smtp_Postfix',
148         self::SMTP_POSTFIX_COMBINED => 'Tinebase_EmailUser_Smtp_PostfixCombined',
149         self::SMTP_STANDARD         => 'Tinebase_EmailUser_Smtp_Standard',
150     );
151     
152     /**
153      * backend object instances
154      * 
155      * @var array
156      */
157     private static $_backends = array();
158     
159     /**
160      * configs as static class var to minimize db queries
161      *  
162      * @var array
163      */
164     private static $_configs = array();
165     
166     /**
167      * the constructor
168      *
169      * don't use the constructor. use the singleton 
170      */
171     private function __construct() 
172     {
173     }
174     
175     /**
176      * don't clone. Use the singleton.
177      *
178      */
179     private function __clone() 
180     {
181     }
182     
183     /**
184      * the singleton pattern
185      *
186      * @param string $configType
187      * @return Tinebase_User_Plugin_Abstract
188      */
189     public static function getInstance($configType = Tinebase_Config::IMAP)
190     {
191         $type = self::getConfiguredBackend($configType);
192         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ .' Email user backend: ' . $type);
193         
194         if (!isset(self::$_backends[$type])) {
195             self::$_backends[$type] = self::factory($type);
196         }
197         
198         return self::$_backends[$type];
199     }
200     
201     /**
202      * return an instance of the defined backend
203      *
204      * @param   string $type name of the backend
205      * @return  Tinebase_User_Plugin_Abstract
206      * @throws  Tinebase_Exception_InvalidArgument
207      */
208     public static function factory($type) 
209     {
210         if (!isset(self::$_supportedBackends[$type])) {
211             throw new Tinebase_Exception_InvalidArgument("Backend type $type not implemented.");
212         }
213         
214         $className = self::$_supportedBackends[$type];
215         
216         $backend = new $className();
217         
218         return $backend;
219     }
220     
221     /**
222      * returns the configured backend
223      * 
224      * @param string $configType
225      * @return string
226      * @throws Tinebase_Exception_NotFound
227      */
228     public static function getConfiguredBackend($configType = Tinebase_Config::IMAP)
229     {
230         $config = self::getConfig($configType);
231         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($config, TRUE));
232         
233         if (!isset($config['backend'])) {
234             throw new Tinebase_Exception_NotFound("No backend in config for type $configType found.");
235         }
236         
237         $backend = ucfirst(strtolower($configType)) . '_' . ucfirst(strtolower($config['backend']));
238         
239         if (!isset(self::$_supportedBackends[$backend])) {
240             throw new Tinebase_Exception_NotFound("Config for type $configType / $backend not found.");
241         }
242         
243         return $backend;
244     }
245     
246     /**
247      * merge two email users
248      * 
249      * @param Tinebase_Model_EmailUser $_emailUserImap
250      * @param Tinebase_Model_EmailUser $_emailUserSmtp
251      * @return Tinebase_Model_EmailUser|NULL
252      */
253     public static function merge($_emailUserImap, $_emailUserSmtp)
254     {
255         $result = NULL;
256         
257         if ($_emailUserImap !== NULL && $_emailUserSmtp !== NULL) {
258             // merge
259             $_emailUserImap->emailAliases = $_emailUserSmtp->emailAliases;
260             $_emailUserImap->emailForwards = $_emailUserSmtp->emailForwards;
261             $_emailUserImap->emailForwardOnly = $_emailUserSmtp->emailForwardOnly;
262             $_emailUserImap->emailAddress = $_emailUserSmtp->emailAddress;
263             $result = $_emailUserImap;
264             
265         } else if ($_emailUserImap !== NULL) {
266             $result =  $_emailUserImap;
267             
268         } else if ($_emailUserSmtp !== NULL) {
269             $result =  $_emailUserSmtp;
270         }
271         
272         return $result;
273     }
274     
275     /**
276      * check if email users are managed for backend/config type
277      * 
278      * @param string $_configType IMAP/SMTP
279      * @return boolean
280      */
281     public static function manages($_configType)
282     {
283         $config = self::getConfig($_configType);
284
285         $result = (!empty($config['backend']) && isset($config['active']) && $config['active'] == true);
286         
287         return $result;
288     }
289     
290     /**
291      * get config for type IMAP/SMTP
292      * 
293      * @param string $_configType
294      * @return array
295      */
296     public static function getConfig($_configType)
297     {
298         if (!isset(self::$_configs[$_configType])) {
299             self::$_configs[$_configType] = Tinebase_Config::getInstance()->get($_configType, new Tinebase_Config_Struct())->toArray();
300             
301             /*
302              * If LDAP-Url is given (instead of comma separated domains) add secondary domains from LDAP
303              * e.g. ldap://localhost/ou=domains,ou=mailConfig,dc=example,dc=com?dc?sub?objectclass=mailDomain
304              */
305             if (($_configType == Tinebase_Config::SMTP) && (array_key_exists('secondarydomains', self::$_configs[Tinebase_Config::SMTP])) &&
306                     preg_match("~^ldaps?://~i", self::$_configs[Tinebase_Config::SMTP]['secondarydomains']))
307             {
308                 $ldap_url = parse_url(self::$_configs[Tinebase_Config::SMTP]['secondarydomains']);
309                 $ldap_url['path'] = substr($ldap_url['path'], 1);
310                 $query = explode('?', $ldap_url['query']);
311                 (count($query) > 0) ? $ldap_url['attributes'] = explode(',', $query[0]) : $ldap_url['attributes'] = array();
312                 $ldap_url['scope'] = Zend_Ldap::SEARCH_SCOPE_BASE;
313                 if (count($query) > 1)
314                 {
315                     switch ($query[1]) {
316                         case 'subtree':
317                         case 'sub':
318                             $ldap_url['scope'] = Zend_Ldap::SEARCH_SCOPE_SUB;
319                             break;
320                         case 'one':
321                             $ldap_url['scope'] = Zend_Ldap::SEARCH_SCOPE_ONE;
322                             break;
323
324                     }
325                 }
326                 (count($query) > 2) ? $ldap_url['filter'] = $query[2] : $ldap_url['filter'] = 'objectClass=*';
327                 // By now your options are limited to configured server
328                 $ldap = new Tinebase_Ldap(Tinebase_User::getBackendConfiguration());
329                 $ldap->connect()->bind();
330                 $secondarydomains = $ldap->searchEntries(
331                         $ldap_url['filter'],
332                         $ldap_url['path'],
333                         $ldap_url['scope'],
334                         $ldap_url['attributes']
335                 );
336                 self::$_configs[Tinebase_Config::SMTP]['secondarydomains'] = '';
337                 foreach ($secondarydomains as $dn) 
338                 {
339                     foreach ($ldap_url['attributes'] as $attr) 
340                     {
341                         if (array_key_exists($attr, $dn)) foreach ($dn[$attr] as $domain) 
342                         {
343                             self::$_configs[Tinebase_Config::SMTP]['secondarydomains'] != '' ? $domain = ','.$domain : $domain;
344                             self::$_configs[Tinebase_Config::SMTP]['secondarydomains'] .= $domain;                            
345                         }
346
347                     }
348                 }
349                 if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ .' Secondarydomains: ' . print_r(self::$_configs[Tinebase_Config::SMTP]['secondarydomains'], true));
350             }
351         }
352         
353         return self::$_configs[$_configType];
354     }
355 }