|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* DeviceView.php |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright More in license.md |
|
6
|
|
|
* @license http://www.ipublikuj.eu |
|
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
|
8
|
|
|
* @package iPublikuj:MobileDetect! |
|
9
|
|
|
* @subpackage Helpers |
|
10
|
|
|
* @since 1.0.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @date 23.04.14 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
|
16
|
|
|
|
|
17
|
|
|
namespace IPub\MobileDetect\Helpers; |
|
18
|
|
|
|
|
19
|
|
|
use Nette; |
|
20
|
|
|
use Nette\Application; |
|
21
|
|
|
use Nette\Http; |
|
22
|
|
|
use Nette\Utils; |
|
23
|
|
|
|
|
24
|
|
|
final class DeviceView extends Nette\Object |
|
25
|
|
|
{ |
|
26
|
|
|
const VIEW_MOBILE = 'mobile'; |
|
27
|
|
|
const VIEW_PHONE = 'phone'; |
|
28
|
|
|
const VIEW_TABLET = 'tablet'; |
|
29
|
|
|
const VIEW_FULL = 'full'; |
|
30
|
|
|
const VIEW_NOT_MOBILE = 'not_mobile'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Http\IRequest |
|
34
|
|
|
*/ |
|
35
|
|
|
private $httpRequest; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var Http\IResponse |
|
39
|
|
|
*/ |
|
40
|
|
|
private $httpResponse; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $viewType = self::VIEW_FULL; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var CookieSettings |
|
49
|
|
|
*/ |
|
50
|
|
|
private $cookieSettings; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
private $switchParameterName = 'device_view'; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $setSwitchParameterName |
|
59
|
|
|
* @param CookieSettings $cookieSettings |
|
60
|
|
|
* @param Http\IRequest $httpRequest |
|
61
|
|
|
* @param Http\IResponse $httpResponse |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct(string $setSwitchParameterName, CookieSettings $cookieSettings, Http\IRequest $httpRequest, Http\IResponse $httpResponse) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->cookieSettings = $cookieSettings; |
|
66
|
|
|
$this->httpRequest = $httpRequest; |
|
67
|
|
|
$this->httpResponse = $httpResponse; |
|
68
|
|
|
|
|
69
|
|
|
$this->switchParameterName = $setSwitchParameterName; |
|
70
|
|
|
|
|
71
|
|
|
if ($this->httpRequest->getQuery($this->switchParameterName, FALSE)) { |
|
72
|
|
|
$this->viewType = $this->httpRequest->getQuery($this->switchParameterName); |
|
73
|
|
|
|
|
74
|
|
|
} elseif ($this->httpRequest->getCookie($this->cookieSettings->getName())) { |
|
75
|
|
|
$this->viewType = $this->httpRequest->getCookie($this->cookieSettings->getName()); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Gets the view type for a device |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getViewType() : string |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->viewType; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Is the device in full view |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
public function isFullView() : bool |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->viewType === self::VIEW_FULL; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Is the device a tablet view type |
|
101
|
|
|
* |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public function isTabletView() : bool |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->viewType === self::VIEW_TABLET; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Is the device a phone view type |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
|
|
public function isPhoneView() : bool |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->viewType === self::VIEW_PHONE; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Is the device a mobile view type |
|
121
|
|
|
* |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
|
|
public function isMobileView() : bool |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->viewType === self::VIEW_MOBILE || $this->isPhoneView() || $this->isTabletView(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Is not the device a mobile view type (PC, Mac, etc.) |
|
131
|
|
|
* |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public function isNotMobileView() : bool |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->viewType === self::VIEW_NOT_MOBILE; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Sets the tablet view type |
|
141
|
|
|
* |
|
142
|
|
|
* @return void |
|
143
|
|
|
*/ |
|
144
|
|
|
public function setTabletView() |
|
145
|
|
|
{ |
|
146
|
|
|
$this->viewType = self::VIEW_TABLET; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Sets the phone view type |
|
151
|
|
|
* |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
|
|
public function setPhoneView() |
|
155
|
|
|
{ |
|
156
|
|
|
$this->viewType = self::VIEW_PHONE; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Sets the mobile view type |
|
161
|
|
|
* |
|
162
|
|
|
* @return void |
|
163
|
|
|
*/ |
|
164
|
|
|
public function setMobileView() |
|
165
|
|
|
{ |
|
166
|
|
|
$this->viewType = self::VIEW_MOBILE; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Sets the not mobile view type |
|
171
|
|
|
* |
|
172
|
|
|
* @return void |
|
173
|
|
|
*/ |
|
174
|
|
|
public function setNotMobileView() |
|
175
|
|
|
{ |
|
176
|
|
|
$this->viewType = self::VIEW_NOT_MOBILE; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getSwitchParameterName() : string |
|
183
|
|
|
{ |
|
184
|
|
|
$this->switchParameterName; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Gets the switch param value from the query string (GET header) |
|
189
|
|
|
* |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
|
|
public function getSwitchParameterValue() : string |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->httpRequest->getQuery($this->switchParameterName, self::VIEW_FULL); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Has the Request the switch param in the query string (GET header) |
|
199
|
|
|
* |
|
200
|
|
|
* @return bool |
|
201
|
|
|
*/ |
|
202
|
|
|
public function hasSwitchParameter() : bool |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->httpRequest->getQuery($this->switchParameterName, FALSE) ? TRUE : FALSE; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Gets the RedirectResponse by switch param value |
|
209
|
|
|
* |
|
210
|
|
|
* @param string $redirectUrl |
|
211
|
|
|
* |
|
212
|
|
|
* @return Application\Responses\RedirectResponse |
|
213
|
|
|
*/ |
|
214
|
|
|
public function getRedirectResponseBySwitchParam(string $redirectUrl) : Application\Responses\RedirectResponse |
|
215
|
|
|
{ |
|
216
|
|
|
$statusCode = 302; |
|
217
|
|
|
|
|
218
|
|
|
switch ($this->getSwitchParameterValue()) { |
|
219
|
|
|
case self::VIEW_MOBILE: |
|
220
|
|
|
$this->createCookie(self::VIEW_MOBILE); |
|
221
|
|
|
break; |
|
222
|
|
|
|
|
223
|
|
|
case self::VIEW_PHONE: |
|
224
|
|
|
$this->createCookie(self::VIEW_PHONE); |
|
225
|
|
|
break; |
|
226
|
|
|
|
|
227
|
|
|
case self::VIEW_TABLET: |
|
228
|
|
|
$this->createCookie(self::VIEW_TABLET); |
|
229
|
|
|
break; |
|
230
|
|
|
|
|
231
|
|
|
default: |
|
232
|
|
|
$this->createCookie(self::VIEW_FULL); |
|
233
|
|
|
break; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return new Application\Responses\RedirectResponse($redirectUrl, $statusCode); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Modifies the Response for non-mobile devices |
|
241
|
|
|
* |
|
242
|
|
|
* @return Http\IResponse |
|
243
|
|
|
*/ |
|
244
|
|
|
public function modifyNotMobileResponse() : Http\IResponse |
|
245
|
|
|
{ |
|
246
|
|
|
// Create cookie |
|
247
|
|
|
$this->createCookie(self::VIEW_NOT_MOBILE); |
|
248
|
|
|
|
|
249
|
|
|
return $this->httpResponse; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Modifies the Response for tablet devices |
|
254
|
|
|
* |
|
255
|
|
|
* @return Http\IResponse |
|
256
|
|
|
*/ |
|
257
|
|
|
public function modifyTabletResponse() : Http\IResponse |
|
258
|
|
|
{ |
|
259
|
|
|
// Create cookie |
|
260
|
|
|
$this->createCookie(self::VIEW_TABLET); |
|
261
|
|
|
|
|
262
|
|
|
return $this->httpResponse; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Modifies the Response for phone devices |
|
267
|
|
|
* |
|
268
|
|
|
* @return Http\IResponse |
|
269
|
|
|
*/ |
|
270
|
|
|
public function modifyPhoneResponse() : Http\IResponse |
|
271
|
|
|
{ |
|
272
|
|
|
// Create cookie |
|
273
|
|
|
$this->createCookie(self::VIEW_PHONE); |
|
274
|
|
|
|
|
275
|
|
|
return $this->httpResponse; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Modifies the Response for mobile devices |
|
280
|
|
|
* |
|
281
|
|
|
* @return Http\IResponse |
|
282
|
|
|
*/ |
|
283
|
|
|
public function modifyMobileResponse() : Http\IResponse |
|
284
|
|
|
{ |
|
285
|
|
|
// Create cookie |
|
286
|
|
|
$this->createCookie(self::VIEW_MOBILE); |
|
287
|
|
|
|
|
288
|
|
|
return $this->httpResponse; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Gets the RedirectResponse for phone devices |
|
293
|
|
|
* |
|
294
|
|
|
* @param string $host Uri host |
|
295
|
|
|
* @param int $statusCode Status code |
|
296
|
|
|
* |
|
297
|
|
|
* @return Application\Responses\RedirectResponse |
|
298
|
|
|
*/ |
|
299
|
|
|
public function getPhoneRedirectResponse(string $host, int $statusCode) : Application\Responses\RedirectResponse |
|
300
|
|
|
{ |
|
301
|
|
|
// Create cookie |
|
302
|
|
|
$this->createCookie(self::VIEW_PHONE); |
|
303
|
|
|
|
|
304
|
|
|
return new Application\Responses\RedirectResponse($host, $statusCode); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* Gets the RedirectResponse for tablet devices |
|
309
|
|
|
* |
|
310
|
|
|
* @param string $host Uri host |
|
311
|
|
|
* @param int $statusCode Status code |
|
312
|
|
|
* |
|
313
|
|
|
* @return Application\Responses\RedirectResponse |
|
314
|
|
|
*/ |
|
315
|
|
|
public function getTabletRedirectResponse(string $host, int $statusCode) : Application\Responses\RedirectResponse |
|
316
|
|
|
{ |
|
317
|
|
|
// Create cookie |
|
318
|
|
|
$this->createCookie(self::VIEW_TABLET); |
|
319
|
|
|
|
|
320
|
|
|
return new Application\Responses\RedirectResponse($host, $statusCode); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Gets the RedirectResponse for mobile devices |
|
325
|
|
|
* |
|
326
|
|
|
* @param string $host Uri host |
|
327
|
|
|
* @param int $statusCode Status code |
|
328
|
|
|
* |
|
329
|
|
|
* @return Application\Responses\RedirectResponse |
|
330
|
|
|
*/ |
|
331
|
|
|
public function getMobileRedirectResponse(string $host, int $statusCode) : Application\Responses\RedirectResponse |
|
332
|
|
|
{ |
|
333
|
|
|
// Create cookie |
|
334
|
|
|
$this->createCookie(self::VIEW_MOBILE); |
|
335
|
|
|
|
|
336
|
|
|
return new Application\Responses\RedirectResponse($host, $statusCode); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* Gets the cookie |
|
341
|
|
|
* |
|
342
|
|
|
* @param string $cookieValue |
|
343
|
|
|
*/ |
|
344
|
|
|
private function createCookie(string $cookieValue) |
|
345
|
|
|
{ |
|
346
|
|
|
// Store cookie in response |
|
347
|
|
|
$this->httpResponse->setCookie( |
|
348
|
|
|
$this->cookieSettings->getName(), |
|
349
|
|
|
$cookieValue, |
|
350
|
|
|
$this->cookieSettings->getExpiresTime(), |
|
351
|
|
|
$this->cookieSettings->getPath(), |
|
352
|
|
|
$this->cookieSettings->getDomain(), |
|
353
|
|
|
$this->cookieSettings->isSecure(), |
|
354
|
|
|
$this->cookieSettings->isHttpOnly() |
|
355
|
|
|
); |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
|