1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya\web; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Request Component. |
7
|
|
|
* |
8
|
|
|
* Extending the {{yii\web\Request}} class by predefine values and add ability to verify whether the request is in admin context or not. |
9
|
|
|
* |
10
|
|
|
* @property boolean $isAdmin Whether the request is admin or not. |
11
|
|
|
* |
12
|
|
|
* @author Basil Suter <[email protected]> |
13
|
|
|
* @since 1.0.0 |
14
|
|
|
*/ |
15
|
|
|
class Request extends \yii\web\Request |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var boolean Force web request to enable unit tests with simulated web requests |
19
|
|
|
*/ |
20
|
|
|
public $forceWebRequest = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string The validation cookie for cookies, should be overwritten in your configuration. |
24
|
|
|
* |
25
|
|
|
* The cookie validation key is generated randomly or by any new release but should be overriten in your config. |
26
|
|
|
* |
27
|
|
|
* http://randomkeygen.com using a 504-bit WPA Key |
28
|
|
|
*/ |
29
|
|
|
public $cookieValidationKey = '(`1gq(|TI2Zxx7zZH<Zk052a9a$@l2EtD9wT`lkTO@7uy{cPaJt4y70mxh4q(3'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array A list of default available parsers. |
33
|
|
|
*/ |
34
|
|
|
public $parsers = [ |
35
|
|
|
'application/json' => 'yii\web\JsonParser', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
|
|
public function init() |
42
|
|
|
{ |
43
|
|
|
parent::init(); |
44
|
|
|
|
45
|
|
|
// if an admin request is detected, change the csrf param. |
46
|
|
|
if ($this->isAdmin) { |
47
|
|
|
$this->csrfParam = '_csrf-admin'; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private $_isAdmin; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Setter method to force isAdmin request. |
55
|
|
|
* |
56
|
|
|
* @param boolean $state Whether its an admin request or not |
57
|
|
|
*/ |
58
|
|
|
public function setIsAdmin($state) |
59
|
|
|
{ |
60
|
|
|
$this->_isAdmin = $state; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Getter method resolves the current url request and check if admin context. |
65
|
|
|
* |
66
|
|
|
* This is mostly used in order to bootstrap more modules and application logic in admin context. |
67
|
|
|
* |
68
|
|
|
* @return boolean If the current request is in admin context return value is true, otherwise false. |
69
|
|
|
*/ |
70
|
|
|
public function getIsAdmin() |
71
|
|
|
{ |
72
|
|
|
if ($this->_isAdmin === null) { |
73
|
|
|
if ($this->getIsConsoleRequest() && !$this->forceWebRequest) { |
74
|
|
|
$this->_isAdmin = false; |
75
|
|
|
} else { |
76
|
|
|
$resolver = (new Composition($this))->getResolvedPathInfo($this); |
77
|
|
|
$parts = explode('/', $resolver['route']); |
78
|
|
|
$first = reset($parts); |
79
|
|
|
|
80
|
|
|
if (preg_match('/admin/i', $first, $results)) { |
81
|
|
|
$this->_isAdmin = true; |
82
|
|
|
} else { |
83
|
|
|
$this->_isAdmin = false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->_isAdmin; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the user client language. |
93
|
|
|
* |
94
|
|
|
* @param string $defaultValue Return if not set. |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getClientLanguage($defaultValue) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
return isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : $defaultValue; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: