|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Http\Parameters; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
5
|
|
|
use Wandu\Http\Contracts\ServerParamsInterface; |
|
6
|
|
|
|
|
7
|
|
|
class ServerParams extends Parameter implements ServerParamsInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @reference https://www.sitepoint.com/web-foundations/mime-types-complete-list/ |
|
11
|
|
|
* @reference https://msdn.microsoft.com/ko-kr/library/microsoft.reportingservices.reportrendering.image.mimetype%28v=sql.120%29.aspx (for IE) |
|
12
|
|
|
* @reference http://sunwebexpert.com/books/detail/php/ie-non-standard-image-mime-type.html (for IE) |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected static $mimeTypes = [ |
|
16
|
|
|
'html' => ['text/html', 'application/xhtml+xml'], |
|
17
|
|
|
'txt' => ['text/plain'], |
|
18
|
|
|
'js' => ['application/x-javascript', 'application/javascript', 'application/ecmascript', 'text/javascript', 'text/ecmascript'], |
|
19
|
|
|
'css' => ['application/x-pointplus', 'text/css'], |
|
20
|
|
|
'json' => ['application/json', 'application/x-json'], |
|
21
|
|
|
'xml' => ['text/xml', 'application/xml', 'application/x-xml'], |
|
22
|
|
|
'atom' => ['application/atom+xml'], |
|
23
|
|
|
'rss' => ['application/rss+xml'], |
|
24
|
|
|
'form' => ['application/x-www-form-urlencoded'], |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/** @var \Psr\Http\Message\ServerRequestInterface */ |
|
28
|
|
|
protected $request; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
|
32
|
|
|
*/ |
|
33
|
5 |
|
public function __construct(ServerRequestInterface $request) |
|
34
|
|
|
{ |
|
35
|
5 |
|
$this->request = $request; |
|
36
|
5 |
|
parent::__construct($request->getServerParams()); |
|
37
|
5 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function isAjax() |
|
43
|
|
|
{ |
|
44
|
1 |
|
return $this->request->hasHeader('x-requested-with') && |
|
45
|
1 |
|
$this->request->getHeaderLine('x-requested-with') === 'XMLHttpRequest'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function getIpAddress($customHeaderName = null) |
|
52
|
|
|
{ |
|
53
|
1 |
|
return $this->request->getHeaderLine('x-forwarded-for') ?: $this->get('REMOTE_ADDR'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @reference https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html |
|
58
|
|
|
* @reference https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function accepts($contentTypes) |
|
62
|
|
|
{ |
|
63
|
1 |
|
if (is_string($contentTypes) && array_key_exists($contentTypes, static::$mimeTypes)) { |
|
64
|
1 |
|
$contentTypes = static::$mimeTypes[$contentTypes]; |
|
65
|
1 |
|
} elseif (is_string($contentTypes)) { |
|
66
|
1 |
|
$contentTypes = [$contentTypes]; |
|
67
|
1 |
|
} |
|
68
|
1 |
|
$accepts = $this->request->getHeader('accept'); |
|
69
|
1 |
|
if (count($accepts) === 0) return true; |
|
70
|
1 |
|
foreach ($accepts as $accept) { |
|
71
|
1 |
|
list($acceptType, $acceptSubType) = $this->splitType($accept); |
|
72
|
1 |
|
if ($acceptType === '*') return true; |
|
73
|
1 |
|
foreach ($contentTypes as $type) { |
|
74
|
1 |
|
if ($type === $accept) return true; |
|
75
|
1 |
|
list($checkType, $checkSubType) = $this->splitType($type); |
|
|
|
|
|
|
76
|
1 |
|
if ($acceptSubType === '*' && $checkType === $acceptType) return true; |
|
77
|
1 |
|
} |
|
78
|
1 |
|
} |
|
79
|
1 |
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getLanguages() |
|
86
|
|
|
{ |
|
87
|
1 |
|
return array_map(function ($language) { |
|
88
|
1 |
|
return strtok($language, ';'); |
|
89
|
1 |
|
}, $this->request->getHeader('accept-language')); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $type |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
1 |
|
private function splitType($type) |
|
97
|
|
|
{ |
|
98
|
1 |
|
$acceptTypes = explode('/', strtok($type, ';')); |
|
99
|
1 |
|
if (!isset($acceptTypes[1])) $acceptTypes[1] = '*'; |
|
100
|
1 |
|
return $acceptTypes; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.