ADD: started implementing downloader thru POST but it's janky and the resulting files are empty ._.

This commit is contained in:
TheShinyMelon 2025-03-13 00:02:44 +01:00
parent f284b620cb
commit 5f1b57cd87
3 changed files with 89 additions and 55 deletions

View File

@ -1,5 +1,6 @@
<?php
$pdo;
try {
$pdo = new PDO($dsn, $user, $passwd);
// if ($pdo) echo "Connected to the $dbname database successfully!";
@ -8,64 +9,60 @@
echo $e->getMessage();
}
function get_images($pdo, $search='%', $sort='img_date', $sdir='DESC') {
if ($sort != 'img_date' && $sdir != 'ASC') {
if ($sort != 'img_title' && $sdir != 'DESC') {
echo('Ey boss, database request is kinda f*cked up. Maybe you want to try that again, hey...<br>'.$sort.' '.$sdir);
die();
function get_images($pdo, $search='%', $sort='img_date', $sdir='DESC') {
if ($sort != 'img_date' && $sdir != 'ASC') {
if ($sort != 'img_title' && $sdir != 'DESC') {
echo('Ey boss, database request is kinda f*cked up. Maybe you want to try that again, hey...<br>'.$sort.' '.$sdir);
die();
}
}
}
$params = array('search' => $search);
$stmt = $pdo->prepare("SELECT * FROM images WHERE img_title LIKE CONCAT('%', :search ,'%') ORDER BY $sort $sdir;");
$stmt->execute($params);
// $stmt->debugDumpParams();
$data = $stmt->fetchAll();
$params = array('search' => $search);
$stmt = $pdo->prepare("SELECT * FROM images WHERE img_title LIKE CONCAT('%', :search ,'%') ORDER BY $sort $sdir;");
$stmt->execute($params);
// $stmt->debugDumpParams();
$data = $stmt->fetchAll();
$img_list = '<ul class="img_list">';
foreach ($data as $block) {
$img_list .= '<li>';
$img_list .= '<img src=\''.$block['img_path'].'\'><br>';
$img_list .= '<h4>'.$block['img_title']."<br></h4>";
$img_list .= '<p class="list_text">'.$block['img_desc']."</p>";
$img_list .= '</li>';
}
$img_list .= '</ul>';
echo $img_list;
}
function get_audio($pdo, $search='%', $sort='aud_date', $sdir='DESC') {
if ($sort != 'aud_date' && $sdir != 'ASC') {
if ($sort != 'aud_title' && $sdir != 'DESC') {
echo('Ey boss, database request is kinda f*cked up. Maybe you want to try that again, hey...<br>'.$sort.' '.$sdir);
die();
$img_list = '<ul class="img_list">';
foreach ($data as $block) {
$img_list .= '<li>';
$img_list .= '<img src=\''.$block['img_path'].'\'><br>';
$img_list .= '<h4>'.$block['img_title']."<br></h4>";
$img_list .= '<p class="list_text">'.$block['img_desc']."</p>";
$img_list .= '</li>';
}
$img_list .= '</ul>';
echo $img_list;
}
$params = array('search' => $search);
$stmt = $pdo->prepare("SELECT * FROM audio WHERE aud_title LIKE CONCAT('%', :search ,'%') ORDER BY $sort $sdir;");
$stmt->execute($params);
// $stmt->debugDumpParams();
$data = $stmt->fetchAll();
$aud_list = '<ul class="aud_list">';
foreach ($data as $block) {
$aud_list .= '<li>';
$aud_list .= '<div class="music_player">';
$aud_list .= '<img src="'.get_album_cover($block["aud_title"]).'">';
$aud_list .= '<audio controls>';
$aud_list .= '<source src=\''.$block['aud_path'].'\' type="audio/mpeg">';
$aud_list .= 'There would be an awesome music player here if you weren\'t such a weirdo...';
$aud_list .= '</audio>';
$aud_list .= '</div>';
$aud_list .= '<h4>'.$block['aud_title']."<br></h4>";
$aud_list .= '<p class="list_text">'.$block['aud_desc']."</p>";
$aud_list .= '<p><a href="'.$block['aud_path'].'">download</a></p>'; // TODO: mask dl link with helper class
$aud_list .= '</li>';
}
$aud_list .= '</ul>';
echo $aud_list;
}
function get_audio($pdo, $search='%', $sort='aud_date', $sdir='DESC') {
if ($sort != 'aud_date' && $sdir != 'ASC') {
if ($sort != 'aud_title' && $sdir != 'DESC') {
echo('Ey boss, database request is kinda f*cked up. Maybe you want to try that again, hey...<br>'.$sort.' '.$sdir);
die();
}
}
$params = array('search' => $search);
$stmt = $pdo->prepare("SELECT * FROM audio WHERE aud_title LIKE CONCAT('%', :search ,'%') ORDER BY $sort $sdir;");
$stmt->execute($params);
// $stmt->debugDumpParams();
$data = $stmt->fetchAll();
function get_album_cover($title) {
// TODO: put actual code here, maybe steal from this lib https://github.com/wapmorgan/Mp3Info/blob/master/src/Mp3Info.php
return "nopathyet";
}
$aud_list = '<ul class="aud_list">';
foreach ($data as $block) {
$aud_list .= '<li>';
$aud_list .= '<div class="music_player">';
$aud_list .= '<img src="tba">';
$aud_list .= '<audio controls>';
$aud_list .= '<source src=\''.$block['aud_path'].'\' type="audio/mpeg">';
$aud_list .= 'There would be an awesome music player here if you weren\'t such a weirdo...';
$aud_list .= '</audio>';
$aud_list .= '</div>';
$aud_list .= '<h4>'.$block['aud_title']."<br></h4>";
$aud_list .= '<p class="list_text">'.$block['aud_desc']."</p>";
$aud_list .= '<p><form method="POST" action="'.$_SERVER['PHP_SELF'].'"><input name="dl_path" type="hidden" value="'.$block['aud_path'].'"><input value="download" type="submit"></form></p>';
$aud_list .= '</li>';
}
$aud_list .= '</ul>';
echo $aud_list;
}

View File

@ -0,0 +1,32 @@
<?php
function downloader($path, $mm_type="application/octet-stream") {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " . filesize($path) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
ob_clean();
flush();
$chunk_size = 8192;
$handle = fopen($path, 'rb');
if ($handle) {
while (!feof($handle)) {
echo fread($handle, $chunk_size);
flush();
}
fclose($handle);
}
die(); // TODO: Fix file being empty ._.
}
//https://www.kavoir.com/2009/05/php-hide-the-real-file-url-and-provide-download-via-a-php-script.html

View File

@ -61,6 +61,11 @@
include('404.php');
break;
}
if($_SERVER["REQUEST_METHOD"] == "POST") {
require_once('../app/downloader.php');
downloader($_POST['dl_path']);
}
?>
</div>