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 NPM\TelegramBotManager; |
12
|
|
|
|
13
|
|
|
class Params |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array List of valid script parameters. |
17
|
|
|
*/ |
18
|
|
|
private static $valid_script_params = [ |
19
|
|
|
's', |
20
|
|
|
'a', |
21
|
|
|
'l', |
22
|
|
|
'i', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array List of vital parameters that must be passed. |
27
|
|
|
*/ |
28
|
|
|
private static $valid_vital_bot_params = [ |
29
|
|
|
'api_key', |
30
|
|
|
'botname', |
31
|
|
|
'secret', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array List of valid extra parameters that can be passed. |
36
|
|
|
*/ |
37
|
|
|
private static $valid_extra_bot_params = [ |
38
|
|
|
'webhook', |
39
|
|
|
'selfcrt', |
40
|
|
|
'logging', |
41
|
|
|
'admins', |
42
|
|
|
'mysql', |
43
|
|
|
'download_path', |
44
|
|
|
'upload_path', |
45
|
|
|
'commands_paths', |
46
|
|
|
'command_configs', |
47
|
|
|
'botan_token', |
48
|
|
|
'custom_input', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array List of all params passed to the script. |
53
|
|
|
*/ |
54
|
|
|
private $script_params = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array List of all params passed at construction. |
58
|
|
|
*/ |
59
|
|
|
private $bot_params = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Params constructor. |
63
|
|
|
* |
64
|
|
|
* api_key (string) Telegram Bot API key |
65
|
|
|
* botname (string) Telegram Bot name |
66
|
|
|
* secret (string) Secret string to validate calls |
67
|
|
|
* webhook (string) URI of the webhook |
68
|
|
|
* selfcrt (string) Path to the self-signed certificate |
69
|
|
|
* logging (array) Array of logger files to set. |
70
|
|
|
* admins (array) List of admins to enable. |
71
|
|
|
* mysql (array) MySQL credentials to use. |
72
|
|
|
* download_path (string) Custom download path to set. |
73
|
|
|
* upload_path (string) Custom upload path to set. |
74
|
|
|
* commands_paths (array) Custom commands paths to set. |
75
|
|
|
* command_configs (array) List of custom command configs. |
76
|
|
|
* botan_token (string) Botan token to enable botan.io support. |
77
|
|
|
* custom_input (string) Custom raw JSON string to use as input. |
78
|
|
|
* |
79
|
|
|
* @param array $params All params to set the bot up with. |
80
|
|
|
* |
81
|
|
|
* @throws \InvalidArgumentException |
82
|
|
|
*/ |
83
|
|
|
public function __construct(array $params) |
84
|
|
|
{ |
85
|
|
|
$this->validateAndSetBotParams($params); |
86
|
|
|
$this->validateAndSetScriptParams(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Validate and set up the vital and extra params. |
91
|
|
|
* |
92
|
|
|
* @param $params |
93
|
|
|
* |
94
|
|
|
* @return \NPM\TelegramBotManager\Params |
95
|
|
|
* @throws \InvalidArgumentException |
96
|
|
|
*/ |
97
|
|
|
private function validateAndSetBotParams($params): self |
98
|
|
|
{ |
99
|
|
|
// Set all vital params. |
100
|
|
|
foreach (self::$valid_vital_bot_params as $vital_key) { |
101
|
|
|
if (empty($params[$vital_key])) { |
102
|
|
|
throw new \InvalidArgumentException('Some vital info is missing: ' . $vital_key); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->bot_params[$vital_key] = $params[$vital_key]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Set all extra params. |
109
|
|
|
foreach (self::$valid_extra_bot_params as $extra_key) { |
110
|
|
|
if (empty($params[$extra_key])) { |
111
|
|
|
continue; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->bot_params[$extra_key] = $params[$extra_key]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Handle all script params, via web server handler or CLI. |
122
|
|
|
* |
123
|
|
|
* https://url/entry.php?s=<secret>&a=<action>&l=<loop> |
124
|
|
|
* $ php entry.php s=<secret> a=<action> l=<loop> |
125
|
|
|
* |
126
|
|
|
* @return \NPM\TelegramBotManager\Params |
127
|
|
|
*/ |
128
|
|
|
private function validateAndSetScriptParams(): self |
129
|
|
|
{ |
130
|
|
|
$this->script_params = $_GET; |
131
|
|
|
|
132
|
|
|
// If we're running from CLI, properly set script parameters. |
133
|
|
|
if ('cli' === PHP_SAPI) { |
134
|
|
|
// We don't need the first arg (the file name). |
135
|
|
|
$args = array_slice($_SERVER['argv'], 1); |
136
|
|
|
|
137
|
|
|
/** @var array $args */ |
138
|
|
|
foreach ($args as $arg) { |
139
|
|
|
@list($key, $val) = explode('=', $arg); |
140
|
|
|
isset($key, $val) && $this->script_params[$key] = $val; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Keep only valid ones. |
145
|
|
|
$this->script_params = array_intersect_key($this->script_params, |
146
|
|
|
array_fill_keys(self::$valid_script_params, null)); |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get a specific bot param. |
153
|
|
|
* |
154
|
|
|
* @param string $param |
155
|
|
|
* |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
public function getBotParam(string $param) |
159
|
|
|
{ |
160
|
|
|
return $this->bot_params[$param] ?? null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get an array of all bot params. |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
public function getBotParams(): array |
169
|
|
|
{ |
170
|
|
|
return $this->bot_params; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get a specific script param. |
175
|
|
|
* |
176
|
|
|
* @param string $param |
177
|
|
|
* |
178
|
|
|
* @return mixed |
179
|
|
|
*/ |
180
|
|
|
public function getScriptParam(string $param) |
181
|
|
|
{ |
182
|
|
|
return $this->script_params[$param] ?? null; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get an array of all script params. |
187
|
|
|
* |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
public function getScriptParams(): array |
191
|
|
|
{ |
192
|
|
|
return $this->script_params; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|