Merge branch '2016.11-develop' into 2017.11
[tine20] / tine20 / Tasks / Convert / Task / 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 /**
14  * tasks vtodo convert factory class
15  *
16  * @package     Tasks
17  * @subpackage  Convert
18  */
19 class Tasks_Convert_Task_VCalendar_Factory
20 {
21     const CLIENT_GENERIC     = 'generic';
22     const CLIENT_IPHONE      = 'iphone';
23     const CLIENT_KDE         = 'kde';
24     const CLIENT_MACOSX      = 'macosx';
25     const CLIENT_THUNDERBIRD = 'thunderbird';
26     const CLIENT_EMCLIENT    = 'emclient';
27     const CLIENT_CALDAVSYNCHRONIZER = 'caldavsynchronizer';
28     const CLIENT_DAVDROID    = 'davdroid';
29     
30     /**
31      * cache parsed user-agent strings
32      * 
33      * @var array
34      */
35     static protected $_parsedUserAgentCache = array();
36     
37     /**
38      * factory function to return a selected vcalendar backend class
39      *
40      * @param   string $_backend
41      * @param   string $_version
42      * @return  Tinebase_Convert_Interface
43      */
44     static public function factory($_backend, $_version = null)
45     {
46         switch ($_backend) {
47             case Tasks_Convert_Task_VCalendar_Factory::CLIENT_GENERIC:
48                 return new Tasks_Convert_Task_VCalendar_Generic($_version);
49                              
50             case Tasks_Convert_Task_VCalendar_Factory::CLIENT_IPHONE:
51                 return new Tasks_Convert_Task_VCalendar_Iphone($_version);
52                                 
53             case Tasks_Convert_Task_VCalendar_Factory::CLIENT_KDE:
54                 return new Tasks_Convert_Task_VCalendar_KDE($_version);
55                 
56             case Tasks_Convert_Task_VCalendar_Factory::CLIENT_MACOSX:
57                 return new Tasks_Convert_Task_VCalendar_MacOSX($_version);
58                 
59             case Tasks_Convert_Task_VCalendar_Factory::CLIENT_THUNDERBIRD:
60                 return new Tasks_Convert_Task_VCalendar_Thunderbird($_version);
61                  
62             case Tasks_Convert_Task_VCalendar_Factory::CLIENT_EMCLIENT:
63                 return new Tasks_Convert_Task_VCalendar_EMClient($_version);
64
65             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_CALDAVSYNCHRONIZER:
66                 return new Tasks_Convert_Task_VCalendar_CalDAVSynchronizer($_version);
67
68             case Tasks_Convert_Task_VCalendar_Factory::CLIENT_DAVDROID:
69                 return new Tasks_Convert_Task_VCalendar_DavDroid($_version);
70         }
71                 return new Tasks_Convert_Task_VCalendar_Generic($_version);
72     }
73     
74     /**
75      * parse iseragent 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(Tasks_Convert_Task_VCalendar_MacOSX::HEADER_MATCH, $_userAgent, $matches)) {
87             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_MACOSX;
88             $version = $matches['version'];
89         
90         // iPhone
91         } elseif (preg_match(Tasks_Convert_Task_VCalendar_Iphone::HEADER_MATCH, $_userAgent, $matches)) {
92             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_IPHONE;
93             $version = $matches['version'];
94         
95         // KDE
96         } elseif (preg_match(Tasks_Convert_Task_VCalendar_KDE::HEADER_MATCH, $_userAgent, $matches)) {
97             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_KDE;
98             $version = $matches['version'];
99         
100         // Thunderbird
101         } elseif (preg_match(Tasks_Convert_Task_VCalendar_Thunderbird::HEADER_MATCH, $_userAgent, $matches)) {
102             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_THUNDERBIRD;
103             $version = $matches['version'];
104         
105         // EMClient       
106         } elseif (preg_match(Tasks_Convert_Task_VCalendar_EMClient::HEADER_MATCH, $_userAgent, $matches)) {
107             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_EMCLIENT;
108             $version = $matches['version'];
109
110         // CalDAVSynchronizer
111         } elseif (preg_match(Tasks_Convert_Task_VCalendar_CalDAVSynchronizer::HEADER_MATCH, $_userAgent, $matches)) {
112             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_CALDAVSYNCHRONIZER;
113             $version = $matches['version'];
114         
115         // DavDroid
116         } elseif (preg_match(Tasks_Convert_Task_VCalendar_DavDroid::HEADER_MATCH, $_userAgent, $matches)) {
117             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_DAVDROID;
118             $version = $matches['version'];
119         
120         } else {
121             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_GENERIC;
122             $version = null;
123         }
124
125         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) 
126             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " $_userAgent ->  backend: $backend version: $version");
127         
128         self::$_parsedUserAgentCache[$_userAgent] = array($backend, $version);
129         
130         return array($backend, $version);
131     }
132 }