diff options
| author | Mandragorat Wandystanu <wktir@wandystan.eu> | 2017-11-18 01:43:36 +0100 |
|---|---|---|
| committer | Mandragorat Wandystanu <wktir@wandystan.eu> | 2017-11-18 01:43:36 +0100 |
| commit | 178b78ddb580ffe49ecac84de53cf7e32e2a50e0 (patch) | |
| tree | 457f916b28d62705f878fc3b7f1cae07d3b8a4b7 | |
| parent | be94107872eda2e210bdc9e8f8555de5eb657c77 (diff) | |
show.php: Display 404 page if file not found; also a slight cleanup of the code.
| -rw-r--r-- | static/php/show.php | 55 |
1 files changed, 25 insertions, 30 deletions
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 @@ <?php -session_start(); - /** * Finds file in a database and shows its content. * @@ -42,38 +40,35 @@ function httpDate($timestamp) return gmdate('D, d M Y H:i:s T', $timestamp); } -try { - // Get file from a database - $q = $db->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]; } |
