<?php |
require_once dirname(__FILE__).'/.config.php'; |
|
## Расшифровка ГЕТ запроса |
$get_id = explode('**', base64_decode($_GET['id'])); |
## Проверка, не подходит выкидываем 404 |
if(intval($get_id[0]) < 1 AND empty($get_id[1])) |
{ |
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found'); |
exit(); |
} |
|
## Запрашиваем данные файла в базе |
$icecast = $db->query('SELECT * FROM icecast2 WHERE id = "'.intval($get_id[0]).'" LIMIT 1')->fetch(PDO::FETCH_ASSOC); |
## Если нет ID файла выкидываем 404 |
if(empty($icecast['id'])) |
{ |
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found'); |
$db = NULL; |
exit(); |
} else if(empty($icecast['file_id'])) |
## Проверка на существование file_id, нет - 404 |
{ |
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found'); |
$db = NULL; |
exit(); |
} else |
## Всё прошли, отдаём файл через телеграмм |
{ |
$data = curl_get('https://api.telegram.org/bot'.BOT_TOKEN.'/getFile?file_id='.$icecast['file_id']); |
$file = json_decode($data, true); |
## Если телеграм выдал ошибку скачивания, а вдруг включена отдача локально - отдадим |
if(empty($file['ok']) OR !empty(FILE_DOWN)) |
{ |
## Если есть локально файл - отдаём |
if(file_exists(FILE_PATCH.$icecast['track'].'.mp3')) |
{ |
## Засчитаем скачивание |
$update = $db->prepare('UPDATE icecast2 SET download = ? WHERE id = "'.$icecast['id'].'"'); |
$update->execute(array($icecast['download']+1)); |
## Выдаём заголовки |
header ('Content-Description: File Transfer'); |
header ('Accept-Ranges: bytes'); |
header ('Content-Type: application/octet-stream'); |
header ('Content-Disposition: attachment; filename='.$icecast['track'].' ['.$_SERVER['HTTP_HOST'].'].mp3'); |
header ('Expires: 0'); |
header ('Cache-Control: must-revalidate'); |
header ('Pragma: public'); |
header ('Content-Length: '.filesize(FILE_PATCH.$icecast['track'].'.mp3')); |
readfile(FILE_PATCH.$icecast['track'].'.mp3'); |
} else |
## Если файла нет даже локально - 404 |
{ |
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found'); |
## Обнулим файл |
$delfile = $db->prepare('UPDATE icecast2 SET file_id = ? WHERE id = "'.$icecast['id'].'" LIMIT 1'); |
$delfile->execute(array(NULL)); |
## Сообщим об ошибке |
exec('curl -s -X POST https://api.telegram.org/bot'.BOT_TOKEN.'/sendMessage -d chat_id='.CHAT_ID_ERROR.' -d text="Поиск '.str_replace('&', '', $icecast['track']).'"'); |
} |
$db = NULL; |
exit(); |
} else |
## Телеграм выдал ссылку на файл - отдаём |
{ |
## Засчитаем скачивание |
$update = $db->prepare('UPDATE icecast2 SET download = ? WHERE id = "'.$icecast['id'].'"'); |
$update->execute(array($icecast['download']+1)); |
## Выдаём заголовки |
header ('Content-Description: File Transfer'); |
header ('Accept-Ranges: bytes'); |
header ('Content-Type: application/octet-stream'); |
header ('Content-Disposition: attachment; filename='.$icecast['track'].' ['.$_SERVER['HTTP_HOST'].'].mp3'); |
header ('Expires: 0'); |
header ('Cache-Control: must-revalidate'); |
header ('Pragma: public'); |
header ('Content-Length: '.$file['result']['file_size']); |
readfile('https://api.telegram.org/file/bot'.BOT_TOKEN.'/'.$file['result']['file_path']); |
$db = NULL; |
exit(); |
} |
} |