-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
242 lines (213 loc) · 9.19 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
// Config.
$cfile = __DIR__ . '/config.json';
if(is_file($cfile)) {
$str = file_get_contents($cfile);
$config = json_decode($str, true);
// Make sure JSON is valid.
if(!(is_string($str) && is_array(json_decode($str, true)) && (json_last_error() === JSON_ERROR_NONE))) {
error_log('Invalid JSON: ' . $cfile);
}
// Check file permissions.
if((fileperms($cfile) & 0777) !== 0600) {
error_log('Insecure file permissions: ' . $cfile . ' (0' . decoct(fileperms($cfile) & 0777) . ') - recommended file permissions: 0600');
}
// Define constants.
foreach($config as $key => $val) {
// Skip comments starting and ending with 2 underscores, e.g. __SQL-CONFIG__
if(substr($key, 0, 2) == '__' && substr($key, -2)) continue;
// Define constants.
defined($key) or define($key, $val);
}
// Config not found, exit!
} else {
error_log('Config file missing: ' . $cfile);
exit();
}
// Tell telegram 'OK'
http_response_code(200);
// Get content from POST data.
$content = file_get_contents('php://input');
// Decode the json string.
$update = json_decode($content, true);
// Get bot directories
$botdirs = str_replace(__DIR__ . '/','',glob(__DIR__ . '/*', GLOB_ONLYDIR));
// Add DEFAULT_BOT dir as first entry
array_unshift($botdirs, DEFAULT_BOT);
// Remove EXCLUDE_DIRS from bot dirs
if(defined('EXCLUDE_DIRS') && !empty(EXCLUDE_DIRS)) {
$excludedirs = explode(',', EXCLUDE_DIRS);
// Remove dir
foreach($excludedirs as $exdir) {
if(($key = array_search($exdir, $botdirs)) !== false) {
unset($botdirs[$key]);
}
}
}
// Remove second DEFAULT_BOT dir entry
$botdirs = array_unique($botdirs);
// Reset keys of bot dirs
$botdirs = array_values($botdirs);
// Count bot dirs
$botdirs_count = count($botdirs);
// Initialize filenames and foldertype
$filename = '';
$altfilename = '';
$foldertype = '';
// Callback query.
if (isset($update['callback_query'])) {
// Set foldertype
$foldertype = 'mods';
// Init empty data array.
$data = [];
// Callback data found.
if ($update['callback_query']['data']) {
// Split bot folder name away from actual data.
$botnameData = explode(':', $update['callback_query']['data'], 2);
$botname = $botnameData[0];
$thedata = $botnameData[1];
// Split callback data and assign to data array.
$splitData = explode(':', $thedata);
$filename = $splitData[1];
}
// Check if filename exists
if(is_file(__DIR__ . '/' . $botname . '/' . $foldertype . '/' . $filename . '.php')) {
include_once(__DIR__ . '/' . $botname . '/index.php');
exit();
}
// Location.
} else if (isset($update['message']['location'])) {
// Forward request to location bot and exit.
include_once(__DIR__ . '/' . LOCATION_BOT . '/index.php');
exit();
// Message.
} else if (isset($update['message']) && $update['message']['chat']['type'] == 'private') {
// Portal message?
if(isset($update['message']['entities']['1']['type']) && $update['message']['entities']['1']['type'] == 'text_link' && strpos($update['message']['entities']['1']['url'], 'https://intel.ingress.com/intel?ll=') === 0) {
// Ingressportalbot
$icon = iconv('UCS-4LE', 'UTF-8', pack('V', 0x1F4DC));
if(strpos($update['message']['text'], $icon . 'Portal:') === 0) {
// Make sure the bot exits and forward then
if(is_file(__DIR__ . '/' . INGRESSPORTALBOT . '/index.php')) {
include_once(__DIR__ . '/' . INGRESSPORTALBOT . '/index.php');
exit();
}
// PortalMapBot
} else if(substr_compare(strtok($update['message']['text'], PHP_EOL), '(Intel)', -strlen('(Intel)')) === 0) {
// Make sure the bot exits and forward then
if(is_file(__DIR__ . '/' . PORTALMAPBOT . '/index.php')) {
include_once(__DIR__ . '/' . PORTALMAPBOT . '/index.php');
exit();
}
}
}
// Set foldertype to commands
$foldertype = 'commands';
// Check message text for a leading slash.
if (substr($update['message']['text'], 0, 1) == '/') {
// Get command name
$filename = strtolower(str_replace('/', '', explode(' ', $update['message']['text'])[0]));
// Check if name of a botdir is inside command
foreach ($botdirs as $key => $dir) {
// Filename starting with name of botdir?
if (substr($filename, 0, strlen($dir)) === $dir) {
// Set alternative filename, substract botdir name from command
$altfilename = substr($filename, strlen($dir));
// Make sure alternative filename exists inside and forward then
if (is_file(__DIR__ . '/' . $dir . '/' . $foldertype . '/' . $altfilename . '.php')) {
include_once(__DIR__ . '/' . $dir . '/index.php');
exit();
// Check if a core command was requested and forward then
} else if (is_file(__DIR__ . '/' . $dir . '/core/' . $foldertype . '/' . $altfilename . '.php')) {
include_once(__DIR__ . '/' . $dir . '/index.php');
exit();
}
}
}
// If filename is equal to a botdir, forward to that bot
if (in_array($filename, $botdirs)) {
include_once(__DIR__ . '/' . $filename . '/index.php');
exit();
}
}
// Inline query.
} else if (isset($update['inline_query'])) {
$count = substr_count($update['inline_query']['query'], ':');
if (substr_count($update['inline_query']['query'], ':') == 1) {
// Split bot folder name away from actual data.
$botnameData = explode(':', $update['inline_query']['query'], 2);
$botname = $botnameData[0];
$thedata = '';
// Do we have any data yet?
if(strlen(explode(':', $update['inline_query']['query'])[1]) != 0) {
$thedata = $botnameData[1];
}
} else {
$botname = DEFAULT_BOT;
$thedata = '';
}
// Check if filename exists
if(is_file(__DIR__ . '/' . $botname . '/index.php')) {
include_once(__DIR__ . '/' . $botname . '/index.php');
exit();
}
// Channel post / Supergroup message.
} else if ((isset($update['channel_post']['text']) && $update['channel_post']['chat']['type'] == "channel") || (isset($update['message']['text']) && $update['message']['chat']['type'] == "supergroup")) {
// Get Bot_ID
$bot_id = '0';
if(isset($update['channel_post']['text'])) {
$id_pos = strrpos($update['channel_post']['text'], '-ID = ');
$bot_id = ($id_pos === false) ? ('0') : (substr($update['channel_post']['text'], ($id_pos - 1), 1));
$bot_id = strtoupper($bot_id);
} else if ($update['message']['chat']['type'] == "supergroup") {
$id_pos = strrpos($update['message']['text'], '-ID = ');
$bot_id = ($id_pos === false) ? ('0') : (substr($update['message']['text'], ($id_pos - 1), 1));
$bot_id = strtoupper($bot_id);
}
// Make sure bot_id was received.
if($bot_id != '0') {
// Search BOT_ID in config files.
//$search = 'BOT_ID';
// Go thru every bots' config.
foreach ($botdirs as $key => $dir) {
// Make sure config file exists.
if(is_file(__DIR__ . '/' . $dir . '/config/config.json')) {
// Read config json.
$str = file_get_contents(__DIR__ . '/' . $dir . '/config/config.json');
$config = json_decode($str, true);
// Get bot id.
// strtoupper will make sure we compare uppercase to uppercase
// substr will get only the first character as it's in the bots handled too.
$config_bot_id = $config['BOT_ID'];
$config_bot_id = substr(strtoupper($config_bot_id), 0, 1);
// Compare bot_id and config_bot_id.
if($bot_id === $config_bot_id) {
// Check if filename exists
if(is_file(__DIR__ . '/' . $dir . '/index.php')) {
include_once(__DIR__ . '/' . $dir . '/index.php');
exit();
}
}
//}
//}
}
}
}
}
// Check files if filenames and foldertype are set
// Compare count of subfolders, as we can only search for filename if we have 2 folders or less
// First the default bot folder and then the other bot folder will be checked this way
// If we have more than 2 folders, searching for filename won't work and therefore be skipped
if ($botdirs_count <= 2 && !empty($filename) && !empty($foldertype)) {
// Check if file exists inside any of the botdirs
foreach ($botdirs as $key => $dir) {
// Check if filename exists
if(is_file(__DIR__ . '/' . $dir . '/' . $foldertype . '/' . $filename . '.php')) {
include_once(__DIR__ . '/' . $dir . '/index.php');
exit();
}
}
}
// Fallback - Forward request to default bot and exit.
include_once(__DIR__ . '/' . DEFAULT_BOT . '/index.php');
exit();