Source Code for /public/source-viewer.php
<?php
require('inc/functions.php');
$file_path = get_requested_file();
if( isset( $_GET['download'] ) ) {
header('Content-Disposition: attachment; filename="' . basename( $file_path ) . '"');
header('Content-Type: text/plain');
echo file_get_contents( $file_path );
die();
}
if( in_array( strtolower( pathinfo( $file_path, PATHINFO_EXTENSION ) ), [ 'svg', 'jpg', 'jpeg', 'png', 'gif', 'webp' ] ) ) {
$public_root = realpath( __DIR__);
$file_path = str_replace( $public_root, '', $file_path );
header('Location: ' . $file_path);
die();
}
require('vendor/autoload.php');
$hl = new \Highlight\Highlighter();
$hl->setAutodetectLanguages(array('php', 'html', 'css', 'javascript', 'json', 'markdown', 'text'));
$code = file_get_contents( $file_path );
$available_stylesheets = \HighlightUtilities\getAvailableStyleSheets();
$stylesheet = 'default';
if( isset( $_GET['stylesheet'] ) && in_array( $_GET['stylesheet'], $available_stylesheets ) ) {
$stylesheet = $_GET['stylesheet'];
}
$short_file_path = str_replace( realpath( __DIR__ . '/../' ), '', $file_path );
$highlighted = $hl->highlightAuto( $code );
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Source Code for <?php echo $short_file_path; ?></title>
<style>
<?php
echo \HighlightUtilities\getStyleSheet($stylesheet);
?>
</style>
</head>
<body>
<h1>Source Code for <?php echo $short_file_path; ?></h1>
<pre><code class="hljs <?php echo $highlighted->language; ?>"><?php echo $highlighted->value; ?></code></pre>
<?php add_backlink(); ?>
</body>
</html>