<?php
$var_sort = $_GET['sort'] ?? 'abc';
$is_logged_in = ($login ?? false) === true;
function formatbytes(int $bytes, int $precision = 2): string {
$units = array('b', 'kb', 'mb', 'gb', 'tb');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = (int)min($pow, count($units) - 1);
$bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}
if (!function_exists('calculate_directory_size')) {
function calculate_directory_size(string $dir): int {
$total_size = 0;
$files = glob($dir . '/*');
if ($files !== false) {
foreach ($files as $path) {
if (is_file($path)) {
$total_size += filesize($path);
} elseif (is_dir($path) && function_exists('get_dir_size')) {
$total_size += get_dir_size($path);
}
}
}
return $total_size;
}
}
$tenon_dir_size = calculate_directory_size('tenons');
$tenon_size = formatbytes($tenon_dir_size);
?>
<div class="tenon_header">
<div class="tenon_header_td_left">
<span class="title">Tenons <span style='opacity: 0.5;'>| <?php echo $tenon_count . " - " . $tenon_size; ?></span></span>
</div>
<div class="tenon_header_td_right">
<?php if ($var_sort === 'date'): ?>
<a href="?l&sort=abc"><img src="images/icons/sort_abc.png" class="tenon_list_button" alt="Sort Alphabetically"></a>
<img src="images/icons/sort_date.png" class="tenon_list_button" alt="Sorted by Date">
<?php else: ?>
<img src="images/icons/sort_abc.png" class="tenon_list_button" alt="Sorted Alphabetically">
<a href="?l&sort=date"><img src="images/icons/sort_date.png" class="tenon_list_button" alt="Sort by Date"></a>
<?php endif; ?>
</div>
</div>
<div class="tenon_table">
<div class="tenon_td">
<?php
$is_mobile = function_exists('isMobile') ? isMobile() : false;
$char_count = $is_mobile ? 30 : 50;
$i = 1;
$file_list = glob("tenons/*.txt");
if ($file_list !== false) {
if ($var_sort === 'date') {
usort($file_list, function($newest, $oldest) {
return filemtime($newest) < filemtime($oldest);
});
}
foreach($file_list as $file_name) {
$raw_name = str_replace(["tenons/", ".txt"], "", $file_name);
$file_print = str_replace("_", " ", $raw_name);
$safe_url = htmlspecialchars(urlencode($raw_name), ENT_QUOTES, 'UTF-8');
$safe_print = htmlspecialchars(substr($file_print, 0, $char_count), ENT_QUOTES, 'UTF-8');
?>
<div class="list_doc">
<div class="list_doc_left">
<span class="text"><?php echo $i++; ?>. </span><a href="index.php?t=<?php echo $safe_url; ?>"><?php echo $safe_print; ?></a>
</div>
<?php if ($is_logged_in): ?>
<div class="span_right">
<a href="back/tenon/delete_tenon_list.php?d=<?php echo urlencode($safe_url); ?>" onclick="return confirm_delete()" class="grey">
<img src="images/icons/delete_line.png" class="tenon_list_button_end" alt="delete">
</a>
</div>
<?php endif; ?>
</div>
<?php
}
}
?>
</div>
</div>