-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathvoice.php
More file actions
73 lines (69 loc) · 2.29 KB
/
voice.php
File metadata and controls
73 lines (69 loc) · 2.29 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
<?php
/*
Copyright (c) 2022-2026 Arman Jussupgaliyev
*/
function exceptions_error_handler($severity, $message, $filename, $lineno) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
set_error_handler('exceptions_error_handler');
try {
include 'mp.php';
if (!defined('CONVERT_VOICE_MESSAGES') || !CONVERT_VOICE_MESSAGES) {
http_response_code(403);
echo 'Voice messages converting disabled';
die;
}
$user = MP::getUser();
if (!$user) {
http_response_code(401);
die;
}
$MP = MP::getMadelineAPI($user);
if (!isset($_GET['c']) || !isset($_GET['m'])) die;
$cid = $_GET['c'];
$mid = $_GET['m'];
if (!is_numeric($cid) || !is_numeric($mid)) die;
if (MP::isChannel($cid)) {
$msg = $MP->channels->getMessages(channel: $cid, id: [$mid]);
} else {
$msg = $MP->messages->getMessages(id: [$mid]);
}
if ($msg && isset($msg['messages'][0])) {
$msg = $msg['messages'][0];
}
$di = $MP->getDownloadInfo($msg['media']);
if (!file_exists(VOICE_TMP_DIR)) mkdir(VOICE_TMP_DIR);
$max = 10 * 1024 * 1024; // 10 mb
if (defined('DOWNLOAD_SIZE_LIMIT')) $max = DOWNLOAD_SIZE_LIMIT;
if (($di['size'] ?? 0) > $max) {
http_response_code(400);
echo 'File is too large!';
die;
}
// automatically delete converted voices
try {
$scan = scandir(VOICE_TMP_DIR);
foreach ($scan as $n) {
if (!str_contains($n, '.mp3') || $n == '.' || $n == '..') continue;
if (date('d.m.y', filemtime(VOICE_TMP_DIR.$n)) == date('d.m.y', time())) {
continue;
}
unlink(VOICE_TMP_DIR.$n);
}
} catch (Exception) {}
$inpath = VOICE_TMP_DIR . hash('crc32',$user).$cid.'_'.$mid;
$outpath = $inpath.'.mp3';
if (!file_exists($outpath)) {
$MP->downloadToFile($di, $inpath);
$res = shell_exec(FFMPEG_DIR.'ffmpeg -i "'.$inpath.'" -b:a 64k -ac 1 -y -acodec mp3 "'.$outpath.'"'.(WINDOWS?'':' 2>&1')) ?? '';
unlink($inpath);
if (str_contains($res, 'failed')) {
echo 'Conversion failed';
die;
}
}
header('Content-Type: audio/mpeg');
echo file_get_contents($outpath);
} catch (Exception $e) {
echo $e;
}