Merge branch '2016.11' into 2016.11-develop
[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     
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 Tasks_Convert_Task_VCalendar_Factory::CLIENT_GENERIC:
47                 return new Tasks_Convert_Task_VCalendar_Generic($_version);
48                              
49             case Tasks_Convert_Task_VCalendar_Factory::CLIENT_IPHONE:
50                 return new Tasks_Convert_Task_VCalendar_Iphone($_version);
51                                 
52             case Tasks_Convert_Task_VCalendar_Factory::CLIENT_KDE:
53                 return new Tasks_Convert_Task_VCalendar_KDE($_version);
54                 
55             case Tasks_Convert_Task_VCalendar_Factory::CLIENT_MACOSX:
56                 return new Tasks_Convert_Task_VCalendar_MacOSX($_version);
57                 
58             case Tasks_Convert_Task_VCalendar_Factory::CLIENT_THUNDERBIRD:
59                 return new Tasks_Convert_Task_VCalendar_Thunderbird($_version);
60                  
61             case Tasks_Convert_Task_VCalendar_Factory::CLIENT_EMCLIENT:
62                 return new Tasks_Convert_Task_VCalendar_EMClient($_version);
63                 
64                         case Calendar_Convert_Event_VCalendar_Factory::CLIENT_CALDAVSYNCHRONIZER:
65                 return new Tasks_Convert_Task_VCalendar_CalDAVSynchronizer($_version);
66                 
67         }
68                 return new Tasks_Convert_Task_VCalendar_Generic($_version);
69     }
70     
71     /**
72      * parse iseragent and return backend and version
73      * 
74      * @return array
75      */
76     static public function parseUserAgent($_userAgent)
77     {
78         if (isset(self::$_parsedUserAgentCache[$_userAgent])) {
79             return self::$_parsedUserAgentCache[$_userAgent];
80         }
81         
82         // MacOS X
83         if (preg_match(Tasks_Convert_Task_VCalendar_MacOSX::HEADER_MATCH, $_userAgent, $matches)) {
84             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_MACOSX;
85             $version = $matches['version'];
86         
87         // iPhone
88         } elseif (preg_match(Tasks_Convert_Task_VCalendar_Iphone::HEADER_MATCH, $_userAgent, $matches)) {
89             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_IPHONE;
90             $version = $matches['version'];
91         
92         // KDE
93         } elseif (preg_match(Tasks_Convert_Task_VCalendar_KDE::HEADER_MATCH, $_userAgent, $matches)) {
94             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_KDE;
95             $version = $matches['version'];
96         
97         // Thunderbird
98         } elseif (preg_match(Tasks_Convert_Task_VCalendar_Thunderbird::HEADER_MATCH, $_userAgent, $matches)) {
99             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_THUNDERBIRD;
100             $version = $matches['version'];
101         
102          // EMClient       
103         } elseif (preg_match(Tasks_Convert_Task_VCalendar_EMClient::HEADER_MATCH, $_userAgent, $matches)) {
104             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_EMCLIENT;
105             $version = $matches['version'];
106                         
107                 // CalDAVSynchronizer
108         } elseif (preg_match(Tasks_Convert_Task_VCalendar_CalDAVSynchronizer::HEADER_MATCH, $_userAgent, $matches)) {
109             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_CALDAVSYNCHRONIZER;
110             $version = $matches['version'];
111         
112         } else {
113             $backend = Tasks_Convert_Task_VCalendar_Factory::CLIENT_GENERIC;
114             $version = null;
115         }
116
117         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) 
118             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " $_userAgent ->  backend: $backend version: $version");
119         
120         self::$_parsedUserAgentCache[$_userAgent] = array($backend, $version);
121         
122         return array($backend, $version);
123     }
124 }