-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdb.php
More file actions
167 lines (137 loc) · 5.23 KB
/
Copy pathdb.php
File metadata and controls
167 lines (137 loc) · 5.23 KB
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
<?php
/*
Wireguard4VPS v0.1a - Alpha - https://github.com/mvpsnet/wireguard4vps
The MIT License (MIT)
Copyright (c) 2022 MVPS LTD - www.mvps.net
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
*/
class db extends PDO
{
public function __construct($db_file)
{
$options = array(
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
);
try {
parent::__construct("sqlite:$db_file", null, null, $options);
} catch (PDOException $e) {
die("Could not connect to the database.");
}
}
public function run($sql, $bind = [])
{
try {
$query = $this->prepare($sql);
if ($query->execute($bind) !== false) {
if (preg_match("/^(select|describe|pragma)/i", $sql)) {
return $query->fetchAll(PDO::FETCH_ASSOC);
} elseif (preg_match("/^(delete|insert|update)/i", $sql)) {
return $query->rowCount();
} else {
return true;
}
}
} catch (PDOException $e) {
echo "\n\n$sql = ".$e->getMessage()."\n\n";
return false;
}
}
}
// credits https://github.com/dimamedia/PHP-Simple-TOTP-and-PubKey
class tfa
{
// RFC4648 Base32 alphabet
private $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
function getOtp($key)
{
/* Base32 decoder */
// Remove spaces from the given public key and converting to an array
$key = str_split(str_replace(" ", "", $key));
$n = 0;
$j = 0;
$binary_key = "";
// Decode public key's each character to base32 and save into binary chunks
foreach ($key as $char) {
$n = $n << 5;
$n = $n + stripos($this->alphabet, $char);
$j += 5;
if ($j >= 8) {
$j -= 8;
$binary_key .= chr(($n & (0xFF << $j)) >> $j);
}
}
/* End of Base32 decoder */
// current unix time 30sec period as binary
$binary_timestamp = pack('N*', 0) . pack('N*', floor(microtime(true) / 30));
// generate keyed hash
$hash = hash_hmac('sha1', $binary_timestamp, $binary_key, true);
// generate otp from hash
$offset = ord($hash[19]) & 0xf;
$otp = (
((ord($hash[$offset + 0]) & 0x7f) << 24) |
((ord($hash[$offset + 1]) & 0xff) << 16) |
((ord($hash[$offset + 2]) & 0xff) << 8) |
(ord($hash[$offset + 3]) & 0xff)
) % pow(10, 6);
return $otp;
}
function getPubKey()
{
$alphabet = str_split($this->alphabet);
$key = '';
// generate 16 chars public key from Base32 alphabet
for ($i = 0; $i < 16; $i++) $key .= $alphabet[random_int(0, 31)];
// split into 4x4 chunks for easy reading
return implode(" ", str_split($key, 4));
}
}
if (!extension_loaded('pdo_sqlite')) {
die("pdo_sqlite extension not enabled.");
}
// until debian moves to php 8
if (!function_exists('str_contains')) {
function str_contains(string $haystack, string $needle)
{
return empty($needle) || strpos($haystack, $needle) !== false;
}
}
function csrf_token($ret = false)
{
$uniq = time();
$token = hash("sha512", $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_HOST'] . $_SERVER['HTTP_USER_AGENT'] . $_SESSION['nonce'] . $uniq);
if ($ret) {
return array("token" => $token, "time" => $uniq);
}
echo "<input type='hidden' name='csrf4_tken' value='$token'>";
echo "<input type='hidden' name='csrf4_time' value='$uniq'>";
}
function csrf_check()
{
$uniq = $_REQUEST['csrf4_time'];
$token = hash("sha512", $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_HOST'] . $_SERVER['HTTP_USER_AGENT'] . $_SESSION['nonce'] . $uniq);
if ($token != $_POST['csrf4_tken']) {
return false;
}
if (time() - $uniq > 1800) {
return false;
}
return true;
}
$db_path = "/etc/wireguard/wireguard4vps.db";
$db = new DB($db_path);