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