cache parsed user-agent
[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     
27     /**
28      * cache parsed user-agent strings
29      * 
30      * @var array
31      */
32     static protected $_parsedUserAgentCache = array();
33     
34     /**
35      * factory function to return a selected vcalendar backend class
36      *
37      * @param   string $_backend
38      * @param   string $_version
39      * @return  Tinebase_Convert_Interface
40      */
41     static public function factory($_backend, $_version = null)
42     {
43         switch ($_backend) {
44             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_GENERIC:
45                 return new Calendar_Convert_Event_VCalendar_Generic($_version);
46                 
47                 break;
48                 
49             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_IPHONE:
50                 return new Calendar_Convert_Event_VCalendar_Iphone($_version);
51                 
52                 break;
53                 
54             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_KDE:
55                 return new Calendar_Convert_Event_VCalendar_KDE($_version);
56                 
57                 break;
58                 
59             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_MACOSX:
60                 return new Calendar_Convert_Event_VCalendar_MacOSX($_version);
61                 
62                 break;
63                 
64             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_THUNDERBIRD:
65                 return new Calendar_Convert_Event_VCalendar_Thunderbird($_version);
66                  
67                 break;
68  
69             case Calendar_Convert_Event_VCalendar_Factory::CLIENT_EMCLIENT:
70                 return new Calendar_Convert_Event_VCalendar_EMClient($_version);
71                  
72                 break;
73  
74         }
75     }
76     
77     /**
78      * parse useragent and return backend and version
79      * 
80      * @return array
81      */
82     static public function parseUserAgent($_userAgent)
83     {
84         if (isset(self::$_parsedUserAgentCache[$_userAgent])) {
85             return self::$_parsedUserAgentCache[$_userAgent];
86         }
87         
88         // MacOS X
89         if (preg_match(Calendar_Convert_Event_VCalendar_MacOSX::HEADER_MATCH, $_userAgent, $matches)) {
90             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_MACOSX;
91             $version = $matches['version'];
92         
93         // iPhone
94         } elseif (preg_match(Calendar_Convert_Event_VCalendar_Iphone::HEADER_MATCH, $_userAgent, $matches)) {
95             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_IPHONE;
96             $version = $matches['version'];
97         
98         // KDE
99         } elseif (preg_match(Calendar_Convert_Event_VCalendar_KDE::HEADER_MATCH, $_userAgent, $matches)) {
100             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_KDE;
101             $version = $matches['version'];
102         
103         // Thunderbird
104         } elseif (preg_match(Calendar_Convert_Event_VCalendar_Thunderbird::HEADER_MATCH, $_userAgent, $matches)) {
105             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_THUNDERBIRD;
106             $version = $matches['version'];
107         
108         // EMClient
109         } elseif (preg_match(Calendar_Convert_Event_VCalendar_EMClient::HEADER_MATCH, $_userAgent, $matches)) {
110             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_EMCLIENT;
111             $version = $matches['version'];
112         
113         } else {
114             $backend = Calendar_Convert_Event_VCalendar_Factory::CLIENT_GENERIC;
115             $version = null;
116         }
117
118         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) 
119             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " $_userAgent ->  backend: $backend version: $version");
120         
121         self::$_parsedUserAgentCache[$_userAgent] = array($backend, $version);
122         
123         return array($backend, $version);
124     }
125 }