-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.autoindex.php
147 lines (116 loc) · 3.81 KB
/
.autoindex.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/* for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
*/
$CFG = [];
require('.autoindex/config.php');
if( file_exists('.autoindex.cfg/config.php') )
include('.autoindex.cfg/config.php');
// the favicon path should start from .autoindex.cfg if it is set
// this setting exists because the favicon can have various formats
if( $CFG['favicon'] === '' )
$CFG['favicon'] = '/.autoindex/favicon.png';
else
$CFG['favicon'] = '/.autoindex.cfg/' . $CFG['favicon'];
$NOW = time();
$OLDEST = $NOW - $CFG['timestamp_oldest'];
$path = urldecode( $_SERVER['REQUEST_URI'] ); // Unicode support
?><!DOCTYPE html>
<meta charset="utf-8">
<title><?= $CFG['title'], $path; ?></title>
<link rel="icon" href="<?= $CFG['favicon']; ?>">
<link rel="stylesheet" href="/.autoindex/style.css">
<link rel="stylesheet" href="/.autoindex/nav/nav.css">
<link rel="stylesheet" href="/.autoindex/search/search.css">
<?php
if( file_exists('.autoindex.cfg/style.css') )
echo '<link rel="stylesheet" href="/.autoindex.cfg/style.css">';
?>
<nav><?php
echo '<a href="/">', $_SERVER['HTTP_HOST'], '</a>';
$breadcrumbs = explode( '/', $path );
// remove first and last items as these are empty
array_shift( $breadcrumbs );
array_pop ( $breadcrumbs );
$url = '/';
foreach( $breadcrumbs as $crumb ){
$url .= $crumb .'/';
echo '<a href="', $url ,'">', $crumb ,'</a>';
}
?></nav>
<table>
<tr>
<th class="name">Filename</th>
<th class="size">Filesize</th>
<th class="date">Last Modified</th>
</tr>
<?php
// sort folders before files
$dirs = [];
$files = [];
// read directory
$d = dir( '.'. $path );
while( ( $entry = $d->read() ) !== false ){
// ignore hidden files and folders
if( $entry[0] === '.' )
continue;
if( is_dir( $d->path . $entry ) ) $dirs [] = $entry;
else $files[] = $entry;
}
$d->close();
// sort
sort( $dirs, SORT_NATURAL | SORT_FLAG_CASE );
sort( $files, SORT_NATURAL | SORT_FLAG_CASE );
// show .. folder, unless when already at document root
if( $path !== '/' ) array_unshift( $dirs, '..' );
$items = array_merge( $dirs, $files );
foreach( $items as $name ){
$file = substr( $path, 1 ) . $name;
$href = './'. $name; // ./ so it works with leading spaces
$type = is_file( $file ) ? 'file' : 'dir';
$mime = $size = $date = '';
if( $type === 'file' ){
$mime = mime_content_type( $file );
$size = filesize( $file );
// get file extension
$ext = '';
if( $pos = strrpos( $name, '.' ) ) // has file extension in the first place
$ext = substr( $name, $pos + 1 );
if( $CFG['parse_url'] && $ext === 'url' ){
$href = file_get_contents( $file );
$mime = 'text/x-url'; // custom file type
}
}
if( $name !== '..' ){
$time = filemtime( $file );
$date = date('Y-m-d H:i:s', $time );
$opacity = 1;
if( $time < $OLDEST ){
$opacity = $CFG['timestamp_min_opacity'];
}else{
$percentage = ($time - $OLDEST) / ($NOW - $OLDEST);
$opacity = $percentage * (1 - $CFG['timestamp_min_opacity']) + $CFG['timestamp_min_opacity'];
}
$date = '<time style="opacity:'. $opacity .'" datetime="'. $date .'">'. $date .'</time>';
}
echo
'<tr class="entry">',
'<td class="name"><a class="item ', $type ,'" href="', $href ,'" data-mime="', $mime ,'">', $name ,'</a></td>',
'<td class="size">', $size ,'</td>',
'<td class="date">', $date ,'</td>',
'</tr>';
}
?>
</table>
<footer>
<a href="https://github.com/desto-git/autoindex.php">autoindex.php | MIT</a>
</footer>
<script src="/.autoindex/nav/nav.js"></script>
<script src="/.autoindex/search/search.js"></script>
<script src="/.autoindex/config.js"></script>
<?php
if( file_exists('.autoindex.cfg/config.js') )
echo '<script src="/.autoindex.cfg/config.js"></script>';
?>