-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrandom_org.php
More file actions
263 lines (250 loc) · 8.18 KB
/
random_org.php
File metadata and controls
263 lines (250 loc) · 8.18 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
//Stop Direct Access to the File
//Works only in PHP 5.0 and Up
if (get_included_files()[0] == __FILE__) {
http_response_code(403);
die('Forbidden');
}
//Stop Including This File Twice
if (defined(strtoupper(basename(__FILE__, '.php')) . '_PHP')) {
return true;
}
define(strtoupper(basename(__FILE__, '.php')) . '_PHP', true);
include_once 'discord_handler.php';
//Constants
$apiKey = '6052a395-b96c-44d8-ad7b-d3567336e5e6'; // Your API key
$hashedApiKey = 'tOVS/9QN52HSau6WGh8A1iGXG/FJNZRPAJFzXWttF5nY50+oGaT1RKGwlJxW9Y4737rpUfACt8iuSwIoCqVetQ=='; // Your Hashed Api Key
$userandomorg = true; // Set to true to use Random.org for random numbers
function sendPOSTrequest(
$url,
$data,
$content_type = 'application/json',
$headers = null
) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($content_type) {
if ($headers) {
$headers[] = 'Content-Type: ' . $content_type;
} else {
$headers = ['Content-Type: ' . $content_type];
}
}
if ($headers) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function sendJSONRPC($url, $method, $params, $id = null)
{
if (!gettype($params) == 'array') {
return [false, 'Params must be an array'];
}
if (!gettype($method) == 'string') {
return [false, 'Method must be a string'];
}
if ($id == null) {
$id = rand(1, 9999);
}
if (!gettype($id) == 'integer') {
return [false, 'ID must be an integer'];
}
if (!gettype($url) == 'string') {
return [false, 'URL must be a string'];
}
if ($id < 1) {
return [false, 'ID must be greater than 0'];
}
$data = [
'jsonrpc' => '2.0',
'method' => $method,
'params' => $params,
'id' => $id,
];
$resp = sendPOSTrequest($url, json_encode($data));
if ($resp) {
$resp = json_decode($resp, true);
if (!$resp) {
return [false, 'Invalid Response'];
}
if (isset($resp['error'])) {
return [false, json_encode($resp['error'])];
} else {
return [true, $resp['result']];
}
} else {
return [false, 'No Response'];
}
}
function sendGETrequest($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function verifySignature($random, $signature)
{
if (gettype($signature) != 'string') {
return [false, 'Signature is not a string'];
}
if (gettype($random) != 'array') {
return [false, 'Random is not an array'];
}
$url = 'https://api.random.org/json-rpc/4/invoke';
$result = sendJSONRPC($url, 'verifySignature', [
'random' => $random,
'signature' => $signature,
]);
if (!$result[0]) {
return $result;
}
if (!array_key_exists('authenticity', $result[1])) {
return [false, 'Invalid Result'];
}
return [true, $result[1]['authenticity']];
}
function createTickets($n, $showResult = true)
{
global $apiKey;
if (gettype($n) != 'integer') {
return [false, 'Number of tickets is not an integer'];
}
if ($n < 1) {
return [false, 'Number of tickets is less than 1'];
}
if (gettype($showResult) != 'boolean') {
return [false, 'Show Result is not a boolean'];
}
$url = 'https://api.random.org/json-rpc/4/invoke';
$result = sendJSONRPC($url, 'createTickets', [
'apiKey' => $apiKey,
'n' => $n,
'showResult' => $showResult,
]);
return $result;
}
function createTicket($showResult = true)
{
return createTickets(1, $showResult);
}
function getRandomSigned(
$n,
$min,
$max,
$ticket = null,
$replacement = true,
$base = 10
) {
global $apiKey;
global $hashedApiKey;
$url = 'https://api.random.org/json-rpc/4/invoke';
$result = sendJSONRPC($url, 'generateSignedIntegers', [
'apiKey' => $apiKey,
'n' => $n,
'min' => $min,
'max' => $max,
'replacement' => $replacement,
'base' => $base,
'ticketId' => $ticket,
'userData' => 'BloxPVP Coinflip Generator. 0 is Red and 1 is Blue.',
]);
if (!$result[0]) {
return $result;
}
$result = $result[1];
if (!array_key_exists('signature', $result)) {
return [false, 'No Signature in Response'];
}
if (array_key_exists('random', $result)) {
$verification = verifySignature(
$result['random'],
$result['signature']
);
if (!$verification[0]) {
return $verification;
} elseif (!$verification[1]) {
return [false, 'Invalid Signature'];
}
if (array_key_exists('hashedApiKey', $result['random'])) {
if ($result['random']['hashedApiKey'] == $hashedApiKey) {
if (array_key_exists('data', $result['random'])) {
if (array_key_exists('ticketData', $result['random'])) {
$ticketData = $result['random']['ticketData']
? $result['random']['ticketData']['ticketId']
: null;
if (array_key_exists('requestsLeft', $result)) {
if (array_key_exists('bitsLeft', $result)) {
if (array_key_exists('cost', $result)) {
return [
true,
[
'data' => $result['random']['data'],
'ticketId' => $ticketData,
'requestsLeft' =>
$result['requestsLeft'],
'bitsLeft' => $result['bitsLeft'],
'cost' => $result['cost'],
],
];
} else {
return [false, 'No Cost in Response'];
}
} else {
return [false, 'No Bits Left in Response'];
}
} else {
return [false, 'No Requests Left in Response'];
}
} else {
return [false, 'No Ticket Data in Response'];
}
} else {
return [false, 'No data in Response'];
}
} else {
return [false, 'Invalid Hashed Api Key'];
}
} else {
return [false, 'No Hashed Api Key in Response'];
}
} else {
return [false, 'No Random Info in Response'];
}
}
function getRandomSignedBinary(
$n,
$min,
$max,
$ticket = null,
$replacement = true
) {
return getRandomSigned($n, $min, $max, $ticket, $replacement, 2);
}
function randomCoinFlip()
{
$ticketId = createTicket();
if (!$ticketId[0]) {
sendErrorEmbedWebhook('Error Creating Random.Org Ticket', $ticketId[1]);
return [rand(0, 9999) % 2, null];
}
$ticketId = $ticketId[1][0]['ticketId'];
$data = getRandomSignedBinary(1, 0, 1, $ticketId);
if (!$data[0]) {
sendErrorEmbedWebhook(
'Error Getting Random.Org Result From Ticket',
$ticketId[1] . ' - Ticket Id: ' . $ticketId
);
return [rand(0, 9999) % 2, null];
} else {
return [$data[1]['data'][0], $data[1]['ticketId']];
}
}
?>