1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the TelegramBotManager package. |
4
|
|
|
* |
5
|
|
|
* (c) Armando Lüscher <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace TelegramBot\TelegramBotManager; |
12
|
|
|
|
13
|
|
|
use TelegramBot\TelegramBotManager\Exception\InvalidParamsException; |
14
|
|
|
|
15
|
|
|
class Params |
16
|
|
|
{ |
17
|
|
|
private static array $valid_script_params = [ |
18
|
|
|
's', // secret |
19
|
|
|
'a', // action |
20
|
|
|
'l', // loop |
21
|
|
|
'i', // interval |
22
|
|
|
'g', // group (for cron) |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
private static array $valid_vital_bot_params = [ |
26
|
|
|
'api_key', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
private static array $valid_extra_bot_params = [ |
30
|
|
|
'bot_username', |
31
|
|
|
'secret', |
32
|
|
|
'validate_request', |
33
|
|
|
'valid_ips', |
34
|
|
|
'webhook', |
35
|
|
|
'logging', |
36
|
|
|
'limiter', |
37
|
|
|
'admins', |
38
|
|
|
'mysql', |
39
|
|
|
'paths', |
40
|
|
|
'commands', |
41
|
|
|
'cron', |
42
|
|
|
'custom_input', |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
private array $script_params = []; |
46
|
|
|
|
47
|
|
|
private array $bot_params = [ |
48
|
|
|
'validate_request' => true, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @throws InvalidParamsException |
53
|
|
|
*/ |
54
|
|
|
public function __construct(array $params) |
55
|
|
|
{ |
56
|
|
|
$this->validateAndSetBotParams($params); |
57
|
|
|
$this->validateAndSetScriptParams(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @throws InvalidParamsException |
62
|
|
|
*/ |
63
|
|
|
private function validateAndSetBotParams(array $params): self |
64
|
|
|
{ |
65
|
|
|
$this->validateAndSetBotParamsVital($params); |
66
|
|
|
$this->validateAndSetBotParamsSpecial($params); |
67
|
|
|
$this->validateAndSetBotParamsExtra($params); |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @throws InvalidParamsException |
74
|
|
|
*/ |
75
|
|
|
private function validateAndSetBotParamsVital(array $params): void |
76
|
|
|
{ |
77
|
|
|
foreach (self::$valid_vital_bot_params as $vital_key) { |
78
|
|
|
if (!array_key_exists($vital_key, $params)) { |
79
|
|
|
throw new InvalidParamsException('Some vital info is missing: ' . $vital_key); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->bot_params[$vital_key] = $params[$vital_key]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @throws InvalidParamsException |
88
|
|
|
*/ |
89
|
|
|
private function validateAndSetBotParamsSpecial(array $params): void |
90
|
|
|
{ |
91
|
|
|
// Special case, where secret MUST be defined if we have a webhook. |
92
|
|
|
if (($params['webhook']['url'] ?? null) && !($params['secret'] ?? null)) { |
93
|
|
|
// This does not apply when using CLI, but make sure it gets tested for! |
94
|
|
|
if ('cli' !== PHP_SAPI || BotManager::inTest()) { |
95
|
|
|
throw new InvalidParamsException('Some vital info is missing: secret'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function validateAndSetBotParamsExtra(array $params): void |
101
|
|
|
{ |
102
|
|
|
foreach (self::$valid_extra_bot_params as $extra_key) { |
103
|
|
|
if (!array_key_exists($extra_key, $params)) { |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->bot_params[$extra_key] = $params[$extra_key]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Handle all script params, via web server handler or CLI. |
113
|
|
|
* |
114
|
|
|
* https://url/entry.php?s=<secret>&a=<action>&l=<loop> |
115
|
|
|
* $ php entry.php s=<secret> a=<action> l=<loop> |
116
|
|
|
*/ |
117
|
|
|
private function validateAndSetScriptParams(): self |
118
|
|
|
{ |
119
|
|
|
$this->setScriptParams(); |
120
|
|
|
$this->validateScriptParams(); |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function setScriptParams(): void |
126
|
|
|
{ |
127
|
|
|
$this->script_params = $_GET; |
128
|
|
|
|
129
|
|
|
// If we're not running from CLI, script parameters are already set from $_GET. |
130
|
|
|
if ('cli' !== PHP_SAPI) { |
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// We don't need the first arg (the file name). |
135
|
|
|
$args = array_slice($_SERVER['argv'], 1); |
136
|
|
|
|
137
|
|
|
foreach ($args as $arg) { |
138
|
|
|
@list($key, $val) = explode('=', $arg); |
139
|
|
|
isset($key, $val) && $this->script_params[$key] = $val; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function validateScriptParams(): void |
144
|
|
|
{ |
145
|
|
|
$this->script_params = array_intersect_key( |
146
|
|
|
$this->script_params, |
147
|
|
|
array_fill_keys(self::$valid_script_params, null) |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getBotParam(string $param, mixed $default = null): mixed |
152
|
|
|
{ |
153
|
|
|
$param_path = explode('.', $param); |
154
|
|
|
|
155
|
|
|
$value = $this->bot_params[array_shift($param_path)] ?? null; |
156
|
|
|
foreach ($param_path as $sub_param_key) { |
157
|
|
|
$value = $value[$sub_param_key] ?? null; |
158
|
|
|
if (null === $value) { |
159
|
|
|
break; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $value ?? $default; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function getBotParams(): array |
167
|
|
|
{ |
168
|
|
|
return $this->bot_params; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getScriptParam(string $param, mixed $default = null): mixed |
172
|
|
|
{ |
173
|
|
|
return $this->script_params[$param] ?? $default; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function getScriptParams(): array |
177
|
|
|
{ |
178
|
|
|
return $this->script_params; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|