-
Notifications
You must be signed in to change notification settings - Fork 40
/
bitcoin.php
executable file
·389 lines (277 loc) · 17.2 KB
/
bitcoin.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
session_start();
require '/var/www/classes/Session.class.php';
$session = new Session();
$result = Array();
$result['status'] = 'ERROR';
$result['redirect'] = '';
$result['msg'] = 'STOP SPYING ON ME!';
if($session->issetLogin()){
$result['status'] = 'OK';
require 'template/gameHeader.php'; //TODO: is it really needed? (perhaps for set_time, but..)
if(isset($_POST['func'])){
$func = $_POST['func'];
switch($func){
case 'btcTransfer':
if(!$session->issetWalletSession()){
break;
}
require '/var/www/classes/Finances.class.php';
$finances = new Finances();
$walletInfo = $finances->getWalletInfoByAddress($_SESSION['WALLET_ADDR']);
$btcValue = $finances->bitcoin_getValue();
if(isset($_POST['amount'])){
$amountToTransfer = round($_POST['amount'], 7);
if(!is_numeric($amountToTransfer)){ //esse realmente é is_numeric!
$session->addMsg('Invalid amount.', 'error');
break;
}
if(!isset($_POST['destination'])){
$session->addMsg('Missing destination address.', 'error');
break;
}
$destination = $_POST['destination'];
if(strlen($destination) != 34){
$session->addMsg('Invalid destination address.', 'error');
break;
}
if(!$finances->issetWallet($destination)){
$session->addMsg('This address is invalid.', 'error');
break;
}
if($destination == $_SESSION['WALLET_ADDR']){
$session->addMsg('LOL, you cant transfer to yourself.', 'error');
break;
}
if($amountToTransfer > $walletInfo->amount){
$session->addMsg('Yo transfering more bitcoins than you have.', 'error');
break;
}
if($amountToTransfer < 0){
exit();
}
$finances->bitcoin_transfer($walletInfo->address, $amountToTransfer, $destination);
$session->addMsg(sprintf(_('%s BTC transfered from %s to %s.'), $amountToTransfer, $walletInfo->address, $destination), 'notice');
require '/var/www/classes/PC.class.php';
$log = new LogVPC();
$log->addLog($finances->bitcoin_getID(), $amountToTransfer.' BTC transfered from '.$walletInfo->address.' to '.$destination, 'NPC');
} else {
// 2019: Function to format bitcoin number to 7-digit precision
//função pra formatar o número de bitcoins pra precisão 7
$rightZeroFill = '';
$explode = explode('.', $walletInfo->amount);
if(!is_int($walletInfo->amount)){
$afterComma = $explode[1];
} else {
$afterComma = 0;
}
if(strlen($afterComma) == 0){
$rightZeroFill = '0000000';
} elseif(strlen($afterComma) < 6){
for($i = 0; $i < 6 - (strlen($afterComma) - 1); $i++){
$rightZeroFill .= '0';
}
}
$title = 'Transfer bitcoins';
$text = '<div class=\"control-group\"><div class=\"controls\">From <span class=\"item\">'.$_SESSION['WALLET_ADDR'].'</span></div><br/><div class=\"controls\"><input id=\"btc-amount\" class=\"name\" type=\"text\" name=\"btc-amount\" placeholder=\"BTC Amount\" style=\"width: 80%;\" value=\"'.$walletInfo->amount.$rightZeroFill.'\"/></div></div><div class=\"control-group\"><div class=\"controls\"><input id=\"btc-to\" class=\"name\" type=\"text\" name=\"btc-to\" placeholder=\"Destination address\" style=\"width: 80%;\"/></div></div>';
$result['msg'] = '[{"title":"'.$title.'","text":"'.$text.'","value":"'.$btcValue.'"}]';
}
break;
case 'btcBuy':
if(!$session->issetWalletSession()){
break;
}
require '/var/www/classes/Finances.class.php';
$finances = new Finances();
$walletInfo = $finances->getWalletInfoByAddress($_SESSION['WALLET_ADDR']);
$btcValue = $finances->bitcoin_getValue();
if(isset($_POST['amount'])){
$amountToBuy = round($_POST['amount'], 7);
if(!is_numeric($amountToBuy)){ //esse realmente é is_numeric!
$session->addMsg('Invalid amount', 'error');
break;
}
if($amountToBuy < 0){
$session->addMsg('Invalid amount', 'error');
break;
}
if(!isset($_POST['acc'])){
$session->addMsg('Missing bank account.', 'error');
break;
}
$acc = $_POST['acc'];
if($acc == '' || !ctype_digit($acc)){
$session->addMsg('Invalid bank account.', 'error');
break;
}
$accInfo = $finances->bankAccountInfo($acc);
if($accInfo['0']['exists'] == '0'){
$session->addMsg('This bank account does not exists.', 'error');
break;
}
$accInfo = $finances->getBankAccountInfo($_SESSION['id'], '', $acc);
if($accInfo['VALID_ACC'] == 0 || $accInfo['USER'] != $_SESSION['id']){
$session->addMsg('This bank account is not valid.', 'error');
break;
}
if($finances->totalMoney() < $amountToBuy * $btcValue){
$session->addMsg('Dude! You dont have money for this :/', 'error');
break;
}
$finances->bitcoin_buy($walletInfo->address, $amountToBuy, $btcValue, $acc);
$session->addMsg(sprintf(_('%s BTC bought for $%s.'), $amountToBuy, number_format($amountToBuy * $btcValue)), 'notice');
require '/var/www/classes/PC.class.php';
$log = new LogVPC();
$log->addLog($finances->bitcoin_getID(), $walletInfo->address.' bought '.$amountToBuy.' BTC for $'.number_format(ceil($amountToBuy * $btcValue)), 'NPC');
} else {
$textAcc = '<br/><div id=\"loading\" class=\"pull-left\" style=\"margin-left: 9%;\"><img src=\"images/ajax-money.gif\">Loading...</div><input type=\"hidden\" id=\"accSelect\" value=\"\"><span id=\"desc-money\" class=\"pull-left\" style=\"margin-left: 9%;\"></span>';
$title = 'Buy bitcoins';
$text = '<div class=\"control-group\"><div class=\"controls\"><input id=\"btc-amount\" class=\"name\" type=\"text\" name=\"btc-amount\" placeholder=\"BTC Amount\" style=\"width: 80%;\" value=\"1.0\"/></div><div class=\"controls\"><span class=\"pull-left\" style=\"margin-left: 9%;\"><span class=\"item\">Rate: </span>1 BTC = $'.$btcValue.'</span></div><br/><div class=\"controls\"><span class=\"pull-left\" style=\"margin-left: 9%;\"><span class=\"item\">Value: </span><span class=\"green\">$<span id=\"btc-total\"></span></span></span></div></div>';
$text .= $textAcc;
$result['msg'] = '[{"title":"'.$title.'","text":"'.$text.'","value":"'.$btcValue.'"}]';
}
break;
case 'btcSell':
if(!$session->issetWalletSession()){
break;
}
require '/var/www/classes/Finances.class.php';
$finances = new Finances();
$walletInfo = $finances->getWalletInfoByAddress($_SESSION['WALLET_ADDR']);
$btcValue = $finances->bitcoin_getValue();
$walletInfo->amount = $walletInfo->amount;
if(isset($_POST['amount'])){
$amountToSell = round($_POST['amount'], 7);
if(!is_numeric($amountToSell)){ //esse realmente é is_numeric!
$session->addMsg('Invalid amount.', 'error');
break;
}
if($amountToSell < 1){
exit();
}
if($amountToSell > $walletInfo->amount || $_POST['amount'] > $walletInfo->amount){
$session->addMsg('You cant sell more than you have :/', 'error');
break;
}
if(!isset($_POST['acc'])){
$session->addMsg('Missing bank account.', 'error');
break;
}
$acc = $_POST['acc'];
if($acc == '' || !ctype_digit($acc)){
$session->addMsg('Invalid bank account.', 'error');
break;
}
$accInfo = $finances->bankAccountInfo($acc);
if($accInfo['0']['exists'] == '0'){
$session->addMsg('This bank account does not exists.', 'error');
break;
}
$accInfo = $finances->getBankAccountInfo($_SESSION['id'], '', $acc);
if($accInfo['VALID_ACC'] == 0 || $accInfo['USER'] != $_SESSION['id']){
$session->addMsg('This bank account is not valid.', 'error');
break;
}
$finances->bitcoin_sell($walletInfo->address, $amountToSell, $btcValue, $acc);
$session->addMsg(sprintf(_('$%s transfered to account #%s'), number_format(ceil($amountToSell * $btcValue)), $acc), 'notice');
require '/var/www/classes/PC.class.php';
$log = new LogVPC();
$log->addLog($finances->bitcoin_getID(), $walletInfo->address.' sold '.$amountToSell.' BTC for $'.number_format(ceil($amountToSell * $btcValue)), 'NPC');
} else {
if($walletInfo->amount >= 1){
$textSell = '<br/><div id=\"loading\" class=\"pull-left\" style=\"margin-left: 9%;\"><img src=\"images/ajax-money.gif\">Loading...</div><input type=\"hidden\" id=\"accSelect\" value=\"\"><span id=\"desc-money\" class=\"pull-left\" style=\"margin-left: 9%;\"></span>';
} else {
$textSell = '<br/><span class=\"pull-left red\" style=\"margin-left: 9%;\">You need at least 1 BTC in order to sell.</span>';
}
//função pra formatar o número de bitcoins pra precisão 7
$rightZeroFill = '';
$explode = explode('.', $walletInfo->amount);
if(!is_int($walletInfo->amount)){
$afterComma = $explode[1];
} else {
$afterComma = 0;
}
if(strlen($afterComma) == 0){
$rightZeroFill = '0000000';
} elseif(strlen($afterComma) < 6){
for($i = 0; $i < 6 - (strlen($afterComma) - 1); $i++){
$rightZeroFill .= '0';
}
}
$title = 'Sell bitcoins';
$text = '<div class=\"control-group\"><div class=\"controls\"><input id=\"btc-amount\" class=\"name\" type=\"text\" name=\"btc-amount\" placeholder=\"BTC Amount\" style=\"width: 80%;\" value=\"'.$walletInfo->amount.$rightZeroFill.'\"/></div><div class=\"controls\"><span class=\"pull-left\" style=\"margin-left: 9%;\"><span class=\"item\">Rate: </span>1 BTC = $'.$btcValue.'</span></div><br/><div class=\"controls\"><span class=\"pull-left\" style=\"margin-left: 9%;\"><span class=\"item\">Value: </span><span class=\"green\">$<span id=\"btc-total\"></span></span></span></div></div>';
$text .= $textSell;
$result['msg'] = '[{"title":"'.$title.'","text":"'.$text.'","value":"'.$btcValue.'","amount":"'.$walletInfo->amount.'"}]';
}
break;
case 'btcLogout':
if($session->issetWalletSession()){
$addr = $_SESSION['WALLET_ADDR'];
$session->deleteWalletSession();
$session->addMsg(sprintf(_('Logged out from address <strong>%s</strong>.'), $addr), 'notice');
}
break;
case 'btcLogin':
$result['status'] = 'ERROR';
if(!isset($_POST['addr']) || !isset($_POST['key'])){
$session->addMsg('Missing information.', 'error');
break;
}
$addr = $_POST['addr'];
$key = $_POST['key'];
if(strlen($addr) != 34){
$session->addMsg('Invalid address.', 'error');
break;
}
if(strlen($key) != 64){
$session->addMsg('Invalid key.', 'error');
break;
}
require '/var/www/classes/Finances.class.php';
$finances = new Finances();
if(!$finances->issetWallet($addr)){
$session->addMsg('This address does not exists.', 'error');
break;
}
//untaint($key);
if($key != $finances->getWalletKey($addr)){
$session->addMsg('This key is invalid.', 'error');
break;
}
require '/var/www/classes/NPC.class.php';
require '/var/www/classes/PC.class.php';
$npc = new NPC();
$btcInfo = $npc->getNPCByKey('BITCOIN');
$btcIP = $btcInfo->npcip;
$player = new Player();
$playerInfo = $player->getPlayerInfo($_SESSION['id']);
$log = new LogVPC();
$session->addMsg(sprintf(_('You logged in to the address <strong>%s</strong>.'), $addr), 'notice');
$session->createWalletSession($addr);
$paramInfoHacked = Array(1, long2ip($playerInfo->gameip), $addr);
$paramInfoHacker = Array(2, long2ip($btcIP), $addr, $key);
$log->addLog($btcInfo->id, $log->logText('BTCLOGIN', $paramInfoHacked), 1);
$log->addLog($_SESSION['id'], $log->logText('BTCLOGIN', $paramInfoHacker), '0');
break;
case 'btcRegister':
$result['status'] = 'OK';
require '/var/www/classes/Finances.class.php';
$finances = new Finances();
$btcID = $finances->bitcoin_getID();
if($finances->userHaveWallet($_SESSION['id'], $btcID)){
$session->addMsg('You already have an account :S', 'error');
break;
}
$finances->bitcoin_createAcc($btcID);
$session->addMsg('Your wallet was created! You can find it\'s information on the Finances page.', 'success');
break;
default:
$result['status'] = 'ERROR';
break;
}
} else {
$result['status'] = 'ERROR';
}
}
header('Content-type: application/json');
die(json_encode($result));