Skip to content

Commit 5cb915f

Browse files
committed
Improve temporary data handling
1 parent 62c20a2 commit 5cb915f

File tree

5 files changed

+85
-64
lines changed

5 files changed

+85
-64
lines changed

func_cheque.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,17 @@ function chequeHandleSum($data){
8181
$availableBalance = $r[2];
8282

8383
if($sum <= 0) {
84-
$response = array(
85-
'chat_id' => $chat_id,
86-
'text' => "❌ОШИБКА! Ввведенное значение не похоже на сумму. Повтори попытку.",
87-
'parse_mode' => 'HTML');
88-
sendit($response, 'sendMessage');
89-
}
84+
$response = array( $tmp = load_tmp_json('san_'.$chat_id, ['sum','asset']);
85+
if(empty($tmp)){
86+
$tmp = load_tmp_json($chat_id, ['availableBalance']);
87+
if(empty($tmp)){
88+
clean_temp_sess();
89+
@unlink(TMP_DIR."/$chat_id.json");
90+
@unlink(TMP_DIR."/san_$chat_id.json");
91+
#unlink(TMP_DIR."/chno_$chat_id.json");
92+
#$tofile = "<?php \$chequeno = $chequeno;";
93+
#file_put_contents(TMP_DIR."/chno_$chat_id.json", $tofile);
94+
9095
elseif($sum > $availableBalance){
9196
$response = array(
9297
'chat_id' => $chat_id,
@@ -164,13 +169,18 @@ function chequeSetNumActivations($sum, $asset, $availableBalance, $ref){
164169
$arInfo["inline_keyboard"][0][0]["callback_data"] = "CNUM|1";
165170
$arInfo["inline_keyboard"][0][0]["text"] = "Пропустить";
166171
$arInfo["inline_keyboard"][0][1]["callback_data"] = "CNUM|$maxnum";
167-
$arInfo["inline_keyboard"][0][1]["text"] = "Макс.кол-во - $maxnum";
168-
$arInfo["inline_keyboard"][1][0]["callback_data"] = $num;
169-
$arInfo["inline_keyboard"][1][0]["text"] = "⏪ Изменить сумму";
170-
send($chat_id, "По желанию укажи количество активаций чека, чтобы создать мультичек (до $maxnum активаций)", $arInfo);
171-
}
172-
}
173-
function chequeHandleNum($data, $row5){
172+
$arInfo["inline_keyboard"][0][1]["text"] = "Макс.кол-во - $maxnum"; ensure_tmp_dir(TMP_DIR);
173+
$filename = TMP_DIR."/".$chat_id."_".$time.".jpg";
174+
file_put_contents($filename, $img);
175+
176+
$response = array(
177+
'chat_id' => $chat_id,
178+
'caption' => '',
179+
'photo' => new CURLFile($filename),
180+
'parse_mode' => 'HTML'
181+
);
182+
sendit($response, 'sendPhoto');
183+
unlink($filename);
174184
global $chat_id, $link;
175185

176186
$num = intval(trim($data['message']['text']));
@@ -429,7 +439,8 @@ function chequeWait4Pass($rowid){
429439
send($chat_id, 'Введи пароль к чеку (не более 64 символов):', $arInfo);
430440
}
431441
function chequeSavePass($data, $row){
432-
global $chat_id, $link;
442+
$refData = load_tmp_json('chqref'.$chat_id, ['referral']);
443+
if(isset($refData['referral'])) $referral = $refData['referral'];
433444

434445
$chequepass = trim($data['message']['text']);
435446
$p = explode("|", $row->action);
@@ -476,7 +487,7 @@ function chequeChangeRef($rowid){
476487
$arInfo["inline_keyboard"][0][2]["callback_data"] = "CRF|50|$rowid";
477488
$arInfo["inline_keyboard"][0][2]["text"] = "50%".$ql;
478489
$ql = ($row->percent == 75) ? "🔸" : "";
479-
$arInfo["inline_keyboard"][0][3]["callback_data"] = "CRF|75|$rowid";
490+
$arInfo["inline_keyboard"][0][3]["callback_data"] = "CRF|75|$rowid"; @unlink(TMP_DIR.'/chqref'.$chat_id.'.json');
480491
$arInfo["inline_keyboard"][0][3]["text"] = "75%".$ql;
481492
$ql = ($row->percent == 100) ? "🔸" : "";
482493
$arInfo["inline_keyboard"][0][4]["callback_data"] = "CRF|100|$rowid";

func_gen.php

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -484,31 +484,35 @@ function payOut($asset, $network, $sum, $fee){
484484
}
485485
}
486486

487-
function ensure_tmp_dir($dir = 'tmp', $maxAge = 86400){
488-
if(!is_dir($dir)){
489-
mkdir($dir,0700,true);
490-
}else{
491-
@chmod($dir,0700);
492-
}
493-
foreach(glob($dir.'/*.json') as $file){
494-
if(filemtime($file) < time() - $maxAge){
495-
@unlink($file);
496-
}
497-
}
498-
}
499-
500-
function save_tmp_json($name, array $data, $dir = 'tmp'){
501-
ensure_tmp_dir($dir);
502-
file_put_contents($dir.'/'.$name.'.json', json_encode($data));
503-
}
504-
505-
function load_tmp_json($name, $dir = 'tmp'){
506-
ensure_tmp_dir($dir);
507-
$path = $dir.'/'.$name.'.json';
508-
if(!file_exists($path)) return [];
509-
$data = json_decode(file_get_contents($path), true);
510-
return is_array($data) ? $data : [];
511-
}
487+
function ensure_tmp_dir($dir = TMP_DIR, $maxAge = 86400){
488+
if(!is_dir($dir)){
489+
mkdir($dir,0700,true);
490+
}else{
491+
@chmod($dir,0700);
492+
}
493+
foreach(glob($dir.'/*') as $file){
494+
if(filemtime($file) < time() - $maxAge){
495+
@unlink($file);
496+
}
497+
}
498+
}
499+
500+
function save_tmp_json($name, array $data, $dir = TMP_DIR){
501+
ensure_tmp_dir($dir);
502+
file_put_contents($dir.'/'.$name.'.json', json_encode($data));
503+
}
504+
505+
function load_tmp_json($name, array $requiredKeys = [], $dir = TMP_DIR){
506+
ensure_tmp_dir($dir);
507+
$path = $dir.'/'.$name.'.json';
508+
if(!file_exists($path)) return [];
509+
$data = json_decode(file_get_contents($path), true);
510+
if(!is_array($data)) return [];
511+
foreach($requiredKeys as $k){
512+
if(!array_key_exists($k, $data)) return [];
513+
}
514+
return $data;
515+
}
512516

513517
function uuid()
514518
{

func_wallet.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,20 @@ function addFundsGetQRcode($asset,$network){
167167
$address = addFundsGetAddress($asset,$network);
168168

169169
//get QR code
170-
$time = time();
171-
$url = "https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=".$address."&choe=UTF-8";
172-
$img = file_get_contents($url);
173-
$filename = "tmp/".$chat_id."_".$time.".jpg";
174-
file_put_contents($filename, $img);
175-
176-
$initurl = "https://tegro.exchange/TegroMoneybot/tmp/".$chat_id."_".$time.".jpg";
177-
178-
$response = array(
179-
'chat_id' => $chat_id,
180-
'caption' => '',
181-
'photo' => $initurl,
182-
'parse_mode' => 'HTML'
183-
);
184-
sendit($response, 'sendPhoto');
170+
$time = time();
171+
$url = "https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=".$address."&choe=UTF-8";
172+
$img = file_get_contents($url);
173+
ensure_tmp_dir(TMP_DIR);
174+
$filename = TMP_DIR."/".$chat_id."_".$time.".jpg";
175+
file_put_contents($filename, $img);
176+
177+
$response = array(
178+
'chat_id' => $chat_id,
179+
'caption' => '',
180+
'photo' => new CURLFile($filename),
181+
'parse_mode' => 'HTML'
182+
);
183+
sendit($response, 'sendPhoto');
185184

186185
$arInfo["inline_keyboard"][0][0]["callback_data"] = 26;
187186
$arInfo["inline_keyboard"][0][0]["text"] = "⏪ Назад в кошелек";
@@ -190,7 +189,7 @@ function addFundsGetQRcode($asset,$network){
190189
Убедись, что ты переводишь в сети '.$network.'!', $arInfo);
191190

192191
sleep(5);
193-
unlink($filename);
192+
unlink($filename);
194193
}
195194

196195
function addFundsCheck($asset,$network){
@@ -620,7 +619,7 @@ function transferFunds(){
620619

621620
clean_temp_sess();
622621
clean_temp_wallet();
623-
@unlink("tmp/chno_$chat_id.json");
622+
@unlink(TMP_DIR."/chno_$chat_id.json");
624623

625624
send2('sendMessage',
626625
[

global.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,11 @@
33
$userName = 'tegromoneybot_bot';
44
$password = 'TV0Up5ARw036c';
55
$databaseName = 'tegromoneybot_bot';
6+
7+
define('TMP_DIR', __DIR__ . '/../tmp');
8+
if (!is_dir(TMP_DIR)) {
9+
mkdir(TMP_DIR, 0700, true);
10+
} else {
11+
@chmod(TMP_DIR, 0700);
12+
}
613
?>

tgbot.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@
267267
delMessage("", $data['callback_query']['message']['message_id']);
268268
$p = explode("|", $data['callback_query']['data']);
269269
$asset = ($p[2] == 1) ? "TON" : "TGR";
270-
$tmp = load_tmp_json($chat_id);
271-
if(!isset($tmp['availableBalance'])){
270+
$tmp = load_tmp_json($chat_id, ['availableBalance']);
271+
if(empty($tmp)){
272272
$response = array(
273273
'chat_id' => $chat_id,
274274
'text' => "❌ОШИБКА! Данные не найдены. Повтори попытку.",
@@ -283,8 +283,8 @@
283283
delMessage("", $data['callback_query']['message']['message_id']);
284284
$p = explode("|", $data['callback_query']['data']);
285285
#$asset = ($p[2] == 1) ? "TON" : "TGR";
286-
$tmp = load_tmp_json('san_'.$chat_id);
287-
if(!isset($tmp['sum'],$tmp['asset'])){
286+
$tmp = load_tmp_json('san_'.$chat_id, ['sum','asset']);
287+
if(empty($tmp)){
288288
$response = array(
289289
'chat_id' => $chat_id,
290290
'text' => "❌ОШИБКА! Данные не найдены. Повтори попытку.",
@@ -300,9 +300,9 @@
300300
elseif( preg_match("/CREF\|/", $data['callback_query']['data'])){
301301
delMessage("", $data['callback_query']['message']['message_id']);
302302
$p = explode("|", $data['callback_query']['data']);
303-
$tmpSan = load_tmp_json('san_'.$chat_id);
304-
$tmpBal = load_tmp_json($chat_id);
305-
if(!isset($tmpSan['sum'],$tmpSan['asset']) || !isset($tmpBal['availableBalance'])){
303+
$tmpSan = load_tmp_json('san_'.$chat_id, ['sum','asset']);
304+
$tmpBal = load_tmp_json($chat_id, ['availableBalance']);
305+
if(empty($tmpSan) || empty($tmpBal)){
306306
$response = array(
307307
'chat_id' => $chat_id,
308308
'text' => "❌ОШИБКА! Данные не найдены. Повтори попытку.",

0 commit comments

Comments
 (0)