From 178b78ddb580ffe49ecac84de53cf7e32e2a50e0 Mon Sep 17 00:00:00 2001 From: Mandragorat Wandystanu Date: Sat, 18 Nov 2017 01:43:36 +0100 Subject: show.php: Display 404 page if file not found; also a slight cleanup of the code. --- static/php/show.php | 55 ++++++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 30 deletions(-) (limited to 'static/php/show.php') diff --git a/static/php/show.php b/static/php/show.php index 4705914..6dcf06a 100644 --- a/static/php/show.php +++ b/static/php/show.php @@ -1,6 +1,4 @@ prepare('SELECT filename, mime, size, date FROM files - WHERE filename = (:filename)'); - $q->bindValue(':filename', $_GET['filename'], PDO::PARAM_STR); +$q = $db->prepare('SELECT filename, mime, size, date FROM files + WHERE filename = (:filename)'); +$q->bindValue(':filename', $_GET['filename'], PDO::PARAM_STR); - if ($q->execute()) { - if ($q->rowCount() > 0) { - $file = $q->fetch(PDO::FETCH_OBJ); +if ($q->execute()) { + if ($q->rowCount() > 0) { + $file = $q->fetch(PDO::FETCH_OBJ); - // File metadata - header('Content-Length: ' . $file->size); - header('Content-Type: ' . $file->mime); + // File metadata + header('Content-Length: ' . $file->size); + header('Content-Type: ' . $file->mime); - // Cache control - header('Cache-Control: public, immutable, max-age=' . ONE_YEAR); - header('Expires: ' . httpDate(time() + ONE_YEAR)); - header('Last-Modified: ' . httpDate(strtotime($file->date))); - header('Pragma: public'); + // Cache control + header('Cache-Control: public, immutable, max-age=' . ONE_YEAR); + header('Expires: ' . httpDate(time() + ONE_YEAR)); + header('Last-Modified: ' . httpDate(strtotime($file->date))); + header('Pragma: public'); - readfile(POMF_FILES_ROOT . $file->filename); - exit; - } else { - throw new Exception('File not found', 404); - } + readfile(POMF_FILES_ROOT . $file->filename); + exit; } else { - $err = $q->errorInfo(); - throw new Exception('SQL error: ' . $err[2], 500); - } -} catch (Exception $e) { - header('Content-Type: text/plain', true, $e->getCode()); + // If file not found, show 404 page + header('Cache-Control: no-cache, no-store, must-revalidate'); + header('Pragma: no-cache'); + http_response_code(404); - echo $e->getCode() . ': ' . $e->getMessage(); - exit; + include '404.html'; + } +} else { + header('Content-Type: text/plain', true, 500); + echo $q->errorInfo()[2]; } -- cgit v1.2.3