Merge branch '2014.11-develop' into 2015.11
[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     const CLIENT_DAVDROID    = 'davdroid';
28     
29     /**
30      * cache parsed user-agent strings
31      * 
32      * @var array
33      */
34     static protected $_parsedUserAgentCache = array();
35     
36     /**
37      * factory function to return a selected vcalendar backend class
38      *
39      * @param   string $_backend
40      * @param   string $_version
41      * @return  Tinebase_Convert_Interface
42      */
43     static public function factory($_backend, $_version = null)
44     {
45         switch ($_backend) {
46             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_GENERIC:
47                 return new Calendar_Convert_Event_VCalendar_Generic($_version);
48                 break;
49                 
50             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_IPHONE:
51                 return new Calendar_Convert_Event_VCalendar_Iphone($_version);
52                 break;
53                 
54             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_KDE:
55                 return new Calendar_Convert_Event_VCalendar_KDE($_version);
56                 break;
57                 
58             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_MACOSX:
59                 return new Calendar_Convert_Event_VCalendar_MacOSX($_version);
60                 break;
61                 
62             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_THUNDERBIRD:
63                 return new Calendar_Convert_Event_VCalendar_Thunderbird($_version);
64                 break;
65  
66             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_EMCLIENT:
67                 return new Calendar_Convert_Event_VCalendar_EMClient($_version);
68                 break;
69
70             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_DAVDROID:
71                 return new Calendar_Convert_Event_VCalendar_DavDroid($_version);
72                 break;
73
74             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_TINE:
75                 return new Calendar_Convert_Event_VCalendar_Tine($_version);
76         }
77     }
78     
79     /**
80      * parse useragent and return backend and version
81      * 
82      * @return array
83      */
84     static public function parseUserAgent($_userAgent)
85     {
86         if (isset(self::$_parsedUserAgentCache[$_userAgent])) {
87             return self::$_parsedUserAgentCache[$_userAgent];
88         }
89         
90         // MacOS X
91         if (preg_match(Calendar_Convert_Event_VCalendar_MacOSX::HEADER_MATCH, $_userAgent, $matches)) {
92             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_MACOSX;
93             $version = $matches['version'];
94         
95         // iPhone
96         } elseif (preg_match(Calendar_Convert_Event_VCalendar_Iphone::HEADER_MATCH, $_userAgent, $matches)) {
97             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_IPHONE;
98             $version = $matches['version'];
99         
100         // KDE
101         } elseif (preg_match(Calendar_Convert_Event_VCalendar_KDE::HEADER_MATCH, $_userAgent, $matches)) {
102             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_KDE;
103             $version = $matches['version'];
104         
105         // Thunderbird
106         } elseif (preg_match(Calendar_Convert_Event_VCalendar_Thunderbird::HEADER_MATCH, $_userAgent, $matches)) {
107             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_THUNDERBIRD;
108             $version = $matches['version'];
109         
110         // EMClient
111         } elseif (preg_match(Calendar_Convert_Event_VCalendar_EMClient::HEADER_MATCH, $_userAgent, $matches)) {
112             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_EMCLIENT;
113             $version = $matches['version'];
114
115         // Tine 2.0
116         } elseif (preg_match(Calendar_Convert_Event_VCalendar_Tine::HEADER_MATCH, $_userAgent, $matches)) {
117             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_TINE;
118             $version = $matches['version'];
119
120         // DavDroid
121         } elseif (preg_match(Calendar_Convert_Event_VCalendar_DavDroid::HEADER_MATCH, $_userAgent, $matches)) {
122             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_TINE;
123             $version = $matches['version'];
124
125         } else {
126             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_GENERIC;
127             $version = null;
128         }
129
130         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) 
131             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " $_userAgent ->  backend: $backend version: $version");
132         
133         self::$_parsedUserAgentCache[$_userAgent] = array($backend, $version);
134         
135         return array($backend, $version);
136     }
137
138     /**
139      * parse useragent and return backend and version
140      *
141      * @return array
142      */
143     static public function supportsSyncToken($_userAgent)
144     {
145         list($backend, $version) = self::parseUserAgent($_userAgent);
146         switch($backend)
147         {
148             case self::CLIENT_MACOSX:
149                 if (version_compare($version, '10.9', '>='))
150                     return true;
151                 break;
152         }
153         return false;
154     }
155 }