agents.php
3.44 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* @package Regular Labs Library
* @version 18.2.10140
*
* @author Peter van Westen <info@regularlabs.com>
* @link http://www.regularlabs.com
* @copyright Copyright © 2018 Regular Labs All Rights Reserved
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
/* @DEPRECATED */
defined('_JEXEC') or die;
require_once dirname(__DIR__) . '/assignment.php';
require_once dirname(__DIR__) . '/text.php';
require_once dirname(__DIR__) . '/mobile_detect.php';
class RLAssignmentsAgents extends RLAssignment
{
var $agent = null;
var $device = null;
/**
* passBrowsers
*/
public function passBrowsers()
{
if (empty($this->selection))
{
return $this->pass(false);
}
foreach ($this->selection as $browser)
{
if ( ! $this->passBrowser($browser))
{
continue;
}
return $this->pass(true);
}
return $this->pass(false);
}
/**
* passOS
*/
public function passOS()
{
return self::passBrowsers();
}
/**
* passDevices
*/
public function passDevices()
{
$pass = (in_array('mobile', $this->selection) && $this->isMobile())
|| (in_array('tablet', $this->selection) && $this->isTablet())
|| (in_array('desktop', $this->selection) && $this->isDesktop());
return $this->pass($pass);
}
/**
* isPhone
*/
public function isPhone()
{
return $this->isMobile();
}
/**
* isMobile
*/
public function isMobile()
{
return $this->getDevice() == 'mobile';
}
/**
* isTablet
*/
public function isTablet()
{
return $this->getDevice() == 'tablet';
}
/**
* isDesktop
*/
public function isDesktop()
{
return $this->getDevice() == 'desktop';
}
/**
* setDevice
*/
private function getDevice()
{
if ( ! is_null($this->device))
{
return $this->device;
}
$detect = new RLMobile_Detect;
$this->is_mobile = $detect->isMobile();
switch (true)
{
case($detect->isTablet()):
$this->device = 'tablet';
break;
case ($detect->isMobile()):
$this->device = 'mobile';
break;
default:
$this->device = 'desktop';
}
return $this->device;
}
/**
* getAgent
*/
private function getAgent()
{
if ( ! is_null($this->agent))
{
return $this->agent;
}
$detect = new RLMobile_Detect;
$agent = $detect->getUserAgent();
switch (true)
{
case (stripos($agent, 'Trident') !== false):
// Add MSIE to IE11
$agent = preg_replace('#(Trident/[0-9\.]+; rv:([0-9\.]+))#is', '\1 MSIE \2', $agent);
break;
case (stripos($agent, 'Chrome') !== false):
// Remove Safari from Chrome
$agent = preg_replace('#(Chrome/.*)Safari/[0-9\.]*#is', '\1', $agent);
// Add MSIE to IE Edge and remove Chrome from IE Edge
$agent = preg_replace('#Chrome/.*(Edge/[0-9])#is', 'MSIE \1', $agent);
break;
case (stripos($agent, 'Opera') !== false):
$agent = preg_replace('#(Opera/.*)Version/#is', '\1Opera/', $agent);
break;
}
$this->agent = $agent;
return $this->agent;
}
/**
* passBrowser
*/
private function passBrowser($browser = '')
{
if ( ! $browser)
{
return false;
}
if ($browser == 'mobile')
{
return $this->isMobile();
}
if ( ! (strpos($browser, '#') === 0))
{
$browser = '#' . RLText::pregQuote($browser) . '#';
}
// also check for _ instead of .
$browser = preg_replace('#\\\.([^\]])#', '[\._]\1', $browser);
$browser = str_replace('\.]', '\._]', $browser);
return preg_match($browser . 'i', $this->getAgent());
}
}