1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Plinker\Core; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Server endpoint class. |
7
|
|
|
*/ |
8
|
|
|
class Server |
9
|
|
|
{ |
10
|
|
|
private $post = []; |
11
|
|
|
private $config = []; |
12
|
|
|
private $publicKey = ''; |
13
|
|
|
private $privateKey = ''; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param string $post |
|
|
|
|
17
|
|
|
* @param string $publicKey |
18
|
|
|
* @param string $privateKey |
19
|
|
|
* @param array $config |
20
|
|
|
*/ |
21
|
|
|
public function __construct( |
22
|
|
|
$post = [], |
23
|
|
|
$publicKey = '', |
24
|
|
|
$privateKey = '', |
25
|
|
|
$config = [] |
26
|
|
|
) { |
27
|
|
|
// define vars |
28
|
|
|
$this->post = $post; |
|
|
|
|
29
|
|
|
$this->config = $config; |
30
|
|
|
$this->publicKey = hash('sha256', gmdate('h').$publicKey); |
31
|
|
|
$this->privateKey = hash('sha256', gmdate('h').$privateKey); |
32
|
|
|
|
33
|
|
|
// init signer |
34
|
|
|
$this->signer = new Signer( |
|
|
|
|
35
|
|
|
$this->publicKey, |
36
|
|
|
$this->privateKey, |
37
|
|
|
(!empty($this->post['encrypt']) ? true : false) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Check client IP is in allowed list if allowed list is set. |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
final private function checkAllowedIps() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
return !( |
49
|
|
|
!empty($this->config['allowed_ips']) && |
50
|
|
|
!in_array($_SERVER['REMOTE_ADDR'], $this->config['allowed_ips']) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check client IP is in allowed list if allowed list is set. |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
final private function verifyRequestToken() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
return !( |
62
|
|
|
empty($this->post['token']) || |
63
|
|
|
empty($_SERVER['HTTP_TOKEN']) || |
64
|
|
|
hash_hmac('sha256', $this->post['token'], $this->privateKey) != $_SERVER['HTTP_TOKEN'] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Server exection method. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function execute() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
// set response header |
76
|
|
|
header('Content-Type: text/plain; charset=utf-8'); |
77
|
|
|
|
78
|
|
|
// check allowed ips |
79
|
|
|
if (!$this->checkAllowedIps()) { |
80
|
|
|
return serialize($this->signer->encode([ |
81
|
|
|
'response' => [ |
82
|
|
|
'error' => 'IP not in allowed list: '.$_SERVER['REMOTE_ADDR'], |
83
|
|
|
] |
84
|
|
|
])); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// verify request token |
88
|
|
|
if (!$this->verifyRequestToken()) { |
89
|
|
|
return serialize($this->signer->encode([ |
90
|
|
|
'response' => [ |
91
|
|
|
'error' => 'invalid request token', |
92
|
|
|
] |
93
|
|
|
])); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// decode post payload |
97
|
|
|
$data = $this->signer->decode( |
98
|
|
|
$this->post |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
// check client post is an array |
102
|
|
|
if (!is_array($data)) { |
103
|
|
|
return serialize($data); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// check data params array or set |
107
|
|
|
if (!isset($data['params'])) { |
108
|
|
|
$data['params'] = []; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// check data config array, set into scope |
112
|
|
|
if (!empty($data['config'])) { |
113
|
|
|
$this->config = $data['config']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// check for empty |
117
|
|
|
if (empty($data['component']) || empty($data['action'])) { |
118
|
|
|
$error = empty($data['component']) ? 'component class' : 'action'; |
119
|
|
|
return serialize($this->signer->encode([ |
120
|
|
|
'response' => [ |
121
|
|
|
'error' => $error.' cannot be empty', |
122
|
|
|
] |
123
|
|
|
])); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$class = '\\Plinker\\'.$data['component']; |
127
|
|
|
|
128
|
|
|
if (class_exists($class)) { |
129
|
|
|
$componentClass = new $class($this->config + $data + $this->post); |
130
|
|
|
|
131
|
|
|
if (method_exists($componentClass, $data['action'])) { |
132
|
|
|
$return = call_user_func( |
133
|
|
|
[ |
134
|
|
|
$componentClass, |
135
|
|
|
$data['action'] |
136
|
|
|
], |
137
|
|
|
$data['params'] |
138
|
|
|
); |
139
|
|
|
} else { |
140
|
|
|
$return = 'action not implemented'; |
141
|
|
|
} |
142
|
|
|
} else { |
143
|
|
|
$return = 'not implemented'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return serialize($this->signer->encode([ |
147
|
|
|
'response' => $return, |
148
|
|
|
])); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
array
and suggests a stricter type likearray<String>
.Most often this is a case of a parameter that can be null in addition to its declared types.