-
Notifications
You must be signed in to change notification settings - Fork 1
/
Devices.php
129 lines (100 loc) · 3.43 KB
/
Devices.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* Choose options (profile, output, interface)
* using user-agent
* @author ximarx
*
*/
class X_VlcShares_Plugins_Devices extends X_VlcShares_Plugins_Abstract {
private $guis = array(
'X_VlcShares_Plugins_MobileRenderer',
'X_VlcShares_Plugins_WiimcPlxRenderer',
);
/**
* @var Application_Model_Device|false|null
*/
private $device = null;
function __construct() {
$this
->setPriority('getIndexManageLinks')
->setPriority('gen_beforeInit')
->setPriority('gen_beforePageBuild', 1)
;
}
/**
* Add the link for -manage-devices-
* @param Zend_Controller_Action $this
* @return X_Page_ItemList_ManageLink
*/
public function getIndexManageLinks(Zend_Controller_Action $controller) {
$link = new X_Page_Item_ManageLink($this->getId(), X_Env::_('p_devices_mlink'));
$link->setTitle(X_Env::_('p_devices_managetitle'))
->setIcon('/images/devices/logo.png')
->setLink(array(
'controller' => 'devices',
'action' => 'index',
), 'default', true);
return new X_Page_ItemList_ManageLink(array($link));
}
// choose the gui to be used
public function gen_beforePageBuild(/*X_Page_ItemList_PItem $items, */Zend_Controller_Action $controller) {
X_Debug::i("Tuning options");
try {
/* @var $cacheHelper X_VlcShares_Plugins_Helper_Cache */
$cacheHelper = $this->helpers()->helper('cache');
$lastdevices = false;
try {
$lastdevices = $cacheHelper->retrieveItem('devices::lastdevices');
} catch (Exception $e) { /* key missing */ }
if ( $lastdevices ) {
$lastdevices = @unserialize($lastdevices);
}
if ( !is_array($lastdevices) ) {
$lastdevices = array();
}
foreach ($lastdevices as $key => $time ) {
if ( $time < time() ) {
unset($lastdevices[$key]);
}
}
if ( !array_key_exists($_SERVER['HTTP_USER_AGENT'], $lastdevices) ) {
$lastdevices[$_SERVER['HTTP_USER_AGENT']] = (time() + ( 15 * 60 ));
}
// clear the cache entry every 60 min if untouched
$cacheHelper->storeItem('devices::lastdevices', serialize($lastdevices), 60);
} catch (Exception $e) {
X_Debug::i("User agent cannot be added to the cache");
}
$guiClass = $this->helpers()->devices()->getDefaultDeviceGuiClass();
X_Debug::i("Device configs: {{label: {$this->helpers()->devices()->getDeviceLabel()}, guiClass: {$guiClass}}");
foreach (X_VlcShares_Plugins::broker()->getPlugins() as $pluginKey => $pluginObj) {
if ( $pluginObj instanceof X_VlcShares_Plugins_RendererInterface ) {
if ( $guiClass == get_class($pluginObj) ) {
/* @var $pluginObj X_VlcShares_Plugins_RendererInterface */
$pluginObj->setDefaultRenderer(true);
}
}
}
// disable this trigger, prevent double initialization
$this->setPriority('gen_beforePageBuild', -1);
}
/**
* Load the device features inside the $this->device
*/
private function loadDevice() {
if ( $this->device === null ) {
$this->device = false;
$devices = Application_Model_DevicesMapper::i()->fetchAll();
/* @var Application_Model_Device $device */
foreach ($devices as $device) {
// if exact do an == comparison
if ( ($device->isExact() && $device->getPattern() == $_SERVER['HTTP_USER_AGENT'])
// otherwise a regex match
|| (!$device->isExact() && preg_match($device->getPattern(), $_SERVER['HTTP_USER_AGENT'] ) > 0 ) ) {
// valid $device found;
$this->device = $device;
} // false + 0 matches
}
}
}
}