<?php

// function to get the file extension (type)
function ext( $file ) {
    if ( is_dir( $file ) ) {
        return 'dir';
    } else {
        return str_replace( '7z', 'sevenz', strtolower( pathinfo( $file )['extension'] ) );
    }
}

// function to get the title, from the url
function title() {
    $url = substr( $_SERVER['REQUEST_URI'], 1 );
    if ( empty( $url ) ) $url = 'derg.rest/';
    return $url;
}

// function to get human-readable filesize
function human_filesize( $file ) {
    $bytes = filesize( $file );
    $decimals = 1;
    $factor = floor( ( strlen($bytes) - 1 ) / 3 );
    if ( $factor > 0 ) $sz = 'KMGT';
    return sprintf( "%.{$decimals}f", $bytes / pow( 1024, $factor ) ) . @$sz[$factor - 1] . 'B';
}

// get the file list for the current directory
$files = array_reverse(scandir( '.' ));

// files to exclude from the files array.
$exclude = array( '.', '..', '.DS_Store', 'edit', 'template.html', 'post.html', 'gay.php', '.stfolder', '.well-known', 'assets', 'index.php', '.index.php.swp', '.git', '.gitmodules', '.gitignore', 'node_modules'. '.*', '.s', '.info', '.hooks', '.hidden' );

// search files array and remove anything in the exclude array
foreach ( $exclude as $ex ) {
    if ( ( $key = array_search( $ex, $files ) ) !== false ) {
        unset( $files[$key] );
    }
}

// title bar and tiny stylesheet with all the icons encoded in it.
?>
<html lang="en">
<head>
<title>Sleepy Dragon File Cabinet</title>
<link rel="stylesheet" href="/2024/style.css">
<link rel="icon" href="/2022/fav.ico">
<link rel="me" href="https://mas.to/@tomxcd">
<link rel="me" href="https://stereophonic.space/users/tomxcd">
<meta name="viewport" content="width=device-width">
</head>
<body>
    <center>
<img id="derg" onmouseover="imageChanger()" width="288px" height="238px" src="/2023/drawing_by_mouse.png">
    </center>
<p><a href="mailto:tom@darsonian.net">Tom</a> (16 y/o) is a <a href="/2024/is_bi.gif">bisexual</a> <a href="/2024/tom_real_life_pic.jpg">human boy</a> from Michigan who acts like a <a href="/2024/DERGY.png">dragon</a> online.</p>
<p>Explore these ever-changing halls, if you dare.</p>
<hr>
<?php
// if the array of files isn't empty
if ( !empty( $files ) ) {

    // open unordered list tag
    print "<ul>";

    // loop through the files array
    foreach ( $files as $file ) {

        // show the file
        print '<a class="ls" href="./' . $file . '"><li class="' . ext( $file ) . '">' . $file . ( !is_dir( $file ) ? ' <span>' . human_filesize( $file ) . '</span>' : '' ) . '</li></a>';

    }

    // close unordered list tag
    print "</ul>";

} else {

    // display empty directory message
    print "<p>This folder contains no files.</p>";

}

?></div>
<hr>
</body>
</html>
