44b2c2f4b2c6141898003ea2bb1b0ce79be4c455
[tine20] / tine20 / Calendar / Convert / Event / VCalendar / Factory.php
1 <?php
2 /**
3  * Tine 2.0
4  *
5  * @package     Calendar
6  * @subpackage  Convert
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Lars Kneschke <l.kneschke@metaways.de>
9  * @copyright   Copyright (c) 2011-2013 Metaways Infosystems GmbH (http://www.metaways.de)
10  */
11
12 /**
13  * calendar VCALENDAR converter factory class
14  *
15  * @package     Calendar
16  * @subpackage  Convert
17  */
18 class Calendar_Convert_Event_VCalendar_Factory
19 {
20     const CLIENT_GENERIC     = 'generic';
21     const CLIENT_IPHONE      = 'iphone';
22     const CLIENT_KDE         = 'kde';
23     const CLIENT_MACOSX      = 'macosx';
24     const CLIENT_THUNDERBIRD = 'thunderbird';
25     const CLIENT_EMCLIENT    = 'emclient';
26     const CLIENT_TINE        = 'tine';
27     
28     /**
29      * cache parsed user-agent strings
30      * 
31      * @var array
32      */
33     static protected $_parsedUserAgentCache = array();
34     
35     /**
36      * factory function to return a selected vcalendar backend class
37      *
38      * @param   string $_backend
39      * @param   string $_version
40      * @return  Tinebase_Convert_Interface
41      */
42     static public function factory($_backend, $_version = null)
43     {
44         switch ($_backend) {
45             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_GENERIC:
46                 return new Calendar_Convert_Event_VCalendar_Generic($_version);
47                 break;
48                 
49             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_IPHONE:
50                 return new Calendar_Convert_Event_VCalendar_Iphone($_version);
51                 break;
52                 
53             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_KDE:
54                 return new Calendar_Convert_Event_VCalendar_KDE($_version);
55                 break;
56                 
57             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_MACOSX:
58                 return new Calendar_Convert_Event_VCalendar_MacOSX($_version);
59                 break;
60                 
61             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_THUNDERBIRD:
62                 return new Calendar_Convert_Event_VCalendar_Thunderbird($_version);
63                 break;
64  
65             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_EMCLIENT:
66                 return new Calendar_Convert_Event_VCalendar_EMClient($_version);
67                 break;
68
69             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_TINE:
70                 return new Calendar_Convert_Event_VCalendar_Tine($_version);
71         }
72     }
73     
74     /**
75      * parse useragent and return backend and version
76      * 
77      * @return array
78      */
79     static public function parseUserAgent($_userAgent)
80     {
81         if (isset(self::$_parsedUserAgentCache[$_userAgent])) {
82             return self::$_parsedUserAgentCache[$_userAgent];
83         }
84         
85         // MacOS X
86         if (preg_match(Calendar_Convert_Event_VCalendar_MacOSX::HEADER_MATCH, $_userAgent, $matches)) {
87             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_MACOSX;
88             $version = $matches['version'];
89         
90         // iPhone
91         } elseif (preg_match(Calendar_Convert_Event_VCalendar_Iphone::HEADER_MATCH, $_userAgent, $matches)) {
92             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_IPHONE;
93             $version = $matches['version'];
94         
95         // KDE
96         } elseif (preg_match(Calendar_Convert_Event_VCalendar_KDE::HEADER_MATCH, $_userAgent, $matches)) {
97             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_KDE;
98             $version = $matches['version'];
99         
100         // Thunderbird
101         } elseif (preg_match(Calendar_Convert_Event_VCalendar_Thunderbird::HEADER_MATCH, $_userAgent, $matches)) {
102             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_THUNDERBIRD;
103             $version = $matches['version'];
104         
105         // EMClient
106         } elseif (preg_match(Calendar_Convert_Event_VCalendar_EMClient::HEADER_MATCH, $_userAgent, $matches)) {
107             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_EMCLIENT;
108             $version = $matches['version'];
109
110         // Tine 2.0
111         } elseif (preg_match(Calendar_Convert_Event_VCalendar_Tine::HEADER_MATCH, $_userAgent, $matches)) {
112             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_TINE;
113             $version = $matches['version'];
114
115
116         } else {
117             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_GENERIC;
118             $version = null;
119         }
120
121         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) 
122             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " $_userAgent ->  backend: $backend version: $version");
123         
124         self::$_parsedUserAgentCache[$_userAgent] = array($backend, $version);
125         
126         return array($backend, $version);
127     }
128 }