Skip to content

Commit 24ff899

Browse files
committed
Validate deletion parameters and add tests
1 parent 62c20a2 commit 24ff899

File tree

2 files changed

+109
-44
lines changed

2 files changed

+109
-44
lines changed

func_gen.php

Lines changed: 81 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -139,50 +139,87 @@ function save2temp($field, $val){
139139

140140
}
141141

142-
function delMessage($mid, $cid){
143-
global $chat_id;
144-
if($mid != ''){
145-
$message_id = $mid-1;
146-
}
147-
elseif($cid != ''){
148-
$message_id = $cid;
149-
}
150-
151-
$ch2 = curl_init('https://api.telegram.org/bot' . TOKEN . '/deleteMessage');
152-
curl_setopt($ch2, CURLOPT_POST, 1);
153-
curl_setopt($ch2, CURLOPT_POSTFIELDS, array('chat_id' => $chat_id, 'message_id' => $message_id));
154-
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
155-
curl_setopt($ch2, CURLOPT_HEADER, false);
156-
$res2 = curl_exec($ch2);
157-
curl_close($ch2);
158-
}
159-
160-
function delMessage2($mid, $cid){
161-
global $chat_id;
162-
if($mid != ''){
163-
$message_id = $mid-1;
164-
}
165-
elseif($cid != ''){
166-
$message_id = $cid;
167-
}
168-
169-
$ch2 = curl_init('https://api.telegram.org/bot' . TOKEN . '/deleteMessage');
170-
curl_setopt($ch2, CURLOPT_POST, 1);
171-
curl_setopt($ch2, CURLOPT_POSTFIELDS, array('chat_id' => $chat_id, 'message_id' => ($cid-1)));
172-
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
173-
curl_setopt($ch2, CURLOPT_HEADER, false);
174-
$res2 = curl_exec($ch2);
175-
curl_close($ch2);
176-
177-
$ch2 = curl_init('https://api.telegram.org/bot' . TOKEN . '/deleteMessage');
178-
curl_setopt($ch2, CURLOPT_POST, 1);
179-
curl_setopt($ch2, CURLOPT_POSTFIELDS, array('chat_id' => $chat_id, 'message_id' => $message_id));
180-
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
181-
curl_setopt($ch2, CURLOPT_HEADER, false);
182-
$res2 = curl_exec($ch2);
183-
curl_close($ch2);
184-
185-
}
142+
function delMessage($mid, $cid){
143+
global $chat_id;
144+
$message_id = null;
145+
if(!empty($mid) && is_numeric($mid)){
146+
$message_id = $mid-1;
147+
}
148+
elseif(!empty($cid) && is_numeric($cid)){
149+
$message_id = $cid;
150+
}
151+
if($message_id === null){
152+
error_log('delMessage: missing or invalid $mid and $cid');
153+
return false;
154+
}
155+
$ch2 = curl_init('https://api.telegram.org/bot' . TOKEN . '/deleteMessage');
156+
curl_setopt($ch2, CURLOPT_POST, 1);
157+
curl_setopt($ch2, CURLOPT_POSTFIELDS, array('chat_id' => $chat_id, 'message_id' => $message_id));
158+
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
159+
curl_setopt($ch2, CURLOPT_HEADER, false);
160+
$res2 = curl_exec($ch2);
161+
if($res2 === false){
162+
error_log('delMessage API error: '.curl_error($ch2));
163+
curl_close($ch2);
164+
return false;
165+
}
166+
$resJson = json_decode($res2, true);
167+
if(empty($resJson['ok'])){
168+
error_log('delMessage API responded with error: '.$res2);
169+
}
170+
curl_close($ch2);
171+
return $resJson['ok'] ?? false;
172+
}
173+
174+
function delMessage2($mid, $cid){
175+
global $chat_id;
176+
$message_id = null;
177+
if(!empty($mid) && is_numeric($mid)){
178+
$message_id = $mid-1;
179+
}
180+
elseif(!empty($cid) && is_numeric($cid)){
181+
$message_id = $cid;
182+
}
183+
if($message_id === null || empty($cid) || !is_numeric($cid)){
184+
error_log('delMessage2: missing or invalid $mid and $cid');
185+
return false;
186+
}
187+
$ch2 = curl_init('https://api.telegram.org/bot' . TOKEN . '/deleteMessage');
188+
curl_setopt($ch2, CURLOPT_POST, 1);
189+
curl_setopt($ch2, CURLOPT_POSTFIELDS, array('chat_id' => $chat_id, 'message_id' => ($cid-1)));
190+
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
191+
curl_setopt($ch2, CURLOPT_HEADER, false);
192+
$res2 = curl_exec($ch2);
193+
if($res2 === false){
194+
error_log('delMessage2 API error (first call): '.curl_error($ch2));
195+
curl_close($ch2);
196+
return false;
197+
}
198+
$resJson = json_decode($res2, true);
199+
if(empty($resJson['ok'])){
200+
error_log('delMessage2 API responded with error (first call): '.$res2);
201+
}
202+
curl_close($ch2);
203+
204+
$ch2 = curl_init('https://api.telegram.org/bot' . TOKEN . '/deleteMessage');
205+
curl_setopt($ch2, CURLOPT_POST, 1);
206+
curl_setopt($ch2, CURLOPT_POSTFIELDS, array('chat_id' => $chat_id, 'message_id' => $message_id));
207+
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
208+
curl_setopt($ch2, CURLOPT_HEADER, false);
209+
$res2 = curl_exec($ch2);
210+
if($res2 === false){
211+
error_log('delMessage2 API error (second call): '.curl_error($ch2));
212+
curl_close($ch2);
213+
return false;
214+
}
215+
$resJson2 = json_decode($res2, true);
216+
if(empty($resJson2['ok'])){
217+
error_log('delMessage2 API responded with error (second call): '.$res2);
218+
}
219+
curl_close($ch2);
220+
return $resJson2['ok'] ?? false;
221+
222+
}
186223

187224
function getTGRrate(){
188225
global $link;

tests/DelMessageTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../func_gen.php';
4+
5+
define('TOKEN', 'TEST_TOKEN');
6+
$chat_id = 1;
7+
8+
$ok = true;
9+
if(delMessage(null, null) !== false){
10+
echo "delMessage without params should return false\n";
11+
$ok = false;
12+
}
13+
if(delMessage2(5, null) !== false){
14+
echo "delMessage2 without cid should return false\n";
15+
$ok = false;
16+
}
17+
if(delMessage2(null, null) !== false){
18+
echo "delMessage2 without params should return false\n";
19+
$ok = false;
20+
}
21+
22+
if($ok){
23+
echo "All tests passed\n";
24+
exit(0);
25+
} else {
26+
exit(1);
27+
}
28+

0 commit comments

Comments
 (0)