-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsetup.php
80 lines (66 loc) · 1.96 KB
/
setup.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
<?php
$web = file_get_contents("templates/render.html");
$config = array();
function checkvar($v){ return (isset($v) && !empty($v)); }
function getMe($token){
$url = "https://api.telegram.org/bot$token/getMe";
return file_get_contents($url);
}
if(checkvar($_POST)){
if(checkvar($_POST['test-token'])){
header("Content-Type: application/json");
die(getMe($_POST['test-token']));
}elseif(checkvar($_POST['token']) && checkvar($_POST['user']) && checkvar($_POST['passwd'])){
// Create or append htpasswd file with login
$hash = $_POST['user'] .":" .password_hash($_POST['passwd'], PASSWORD_BCRYPT, ['cost' => 5]);
$fp = fopen(".htpasswd", 'a');
fwrite($fp, $hash ."\n");
fclose($fp);
// Check token before continue.
$token = $_POST['token'];
$bot = getMe($token);
if(empty($bot)){
// TODO Error
http_response_code(404);
die("Bot token invalid.");
}
$token = explode(":", $token);
$bot = json_decode($bot, TRUE);
$config['bot'] = [
'id' => $token[0],
'key' => $token[1],
'name' => $bot['result']['first_name'],
'username' => $bot['result']['username'],
];
// If using Telegram-PHP-App
$config['using-app'] = (
file_exists("../config.php") &&
is_dir("../app") &&
is_dir("../core") &&
is_dir("../libs/Telegram-PHP")
);
// Save Config
// http://stackoverflow.com/a/2237315
$config = var_export($config, TRUE);
file_put_contents('config.php', "<?php return $config ;");
// Delete setup files
if(file_exists('config.php')){
$files = ['templates/setup.html', 'setup.php'];
foreach($files as $f){ unlink($f); }
header("Location: index.php");
die();
}
// TODO Check config error
http_response_code(400);
die("Could not write config.");
}
}
$setup = file_get_contents("templates/setup.html");
$web = str_replace(
["%%TITLE%%", "%%BOTNAME%%", "%%MENU%%", "%%MAIN%%"],
["Configuración", "Telegram-PHP-Admin", "", $setup],
$web
);
echo $web;
// TODO Add Analytics tag to render Graphs?
?>