forked from romaricdrigon/EasyGallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlister.php
154 lines (128 loc) · 4.62 KB
/
lister.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
148
149
150
151
152
153
154
<?php
/*
* EasyGallery
* http://github.com/romaricdrigon/
*/
/*
* This very little piece of script will list
* files (pictures only) & folders in a directory,
* sorted by ascending alphabetic order
* @param :
* - dir name {string}, with no slashes
*/
// allowed files extensions
$extensions = array('jpg', 'JPG', 'jpeg', 'JPEG', 'png', 'PNG', 'gif', 'GIF');
// list images in a given directory
function lister($dir, $use_thumbs, $thumbs_suffix = '')
{
// test if dir exists before
if (is_dir($dir)) {
$files = scandir($dir);
} else {
echo 'Dossier introuvable';
return '';
}
if ($files == FALSE) {
echo 'Unable to open folder';
return '';
}
$content = array('folder' => [], 'picture' => []);
global $extensions; // access defined file extensions list
$files = array_diff($files, array('.','..')); // array_diff to remove . and ..
sort($files, SORT_STRING); // sort, important for next step ; make sure everything is seen as strings
foreach ($files as $file) {
$f = $dir.'/'.$file; // careful, you have to call is_dir or is_file on the full path!
if (is_dir($f) === TRUE && sizeof(scandir($f)) > 2) { // directory, not empty (2 items by default : . and ..)
$content['folder'][] = $file;
} else if (is_file($f) === TRUE && in_array(pathinfo($file, PATHINFO_EXTENSION), $extensions)) { // file
// we consider there's only pictures files, this is part of the "contract" - so we don't re-check mime type
if (($use_thumbs === TRUE) && (strpos($file, $thumbs_suffix) != FALSE)) {
// it's a thumbnail - using str_replace and an associative array allow to match big picture & thumb
$content['picture'][str_replace($thumbs_suffix, '', $file)]['thumb'] = $file;
} else {
$content['picture'][$file]['big'] = $file; // otherwise it's a big one
}
}
// we elude symlinks, to avoid loops
}
return $content;
}
// return thumbnail representing a directory
// param : dir : folder that must represented by a thumbnail
function get_thumbnail($dir)
{
$list = lister($dir, FALSE); // we don't care about thumbs, so save some time
if (isset($list['picture']['thumbnail.jpg']) === TRUE) {
return 'thumbnail.jpg';
} else if ((isset($list['picture']) === TRUE) && (sizeof($list['picture']) > 0)) { // get first picture file in the folder
$key = key($list['picture']); // get current value
return $list['picture'][$key]['big'];
} else if ((isset($list['folder']) === TRUE) && (sizeof($list['folder']) > 0)) { // search in sub folders
foreach ($list['folder'] as $a_folder) {
// recursivity FTW
$a_folder_thumb = get_thumbnail($dir.'/'.$a_folder);
if ($a_folder_thumb !== '') {
return $a_folder.'/'.$a_folder_thumb;
}
}
}
return ''; // nothing was found
}
// return the folder from GET param
function get_folder()
{
if (! isset($_GET['gallery'])) {
return ''; // means root
}
$folder = $_GET['gallery']; // $_GET is automatically url-decoded - and backslashes added
$folder = stripslashes($folder); // remove theses backslashes
if (stripos($folder, '..') !== FALSE) {
//die("Forbidden directory"); // don't fuck with me bro
return ''; // be nice, just return to root
}
// remove ending slash
return no_slash($folder);
}
// display a path link
// @param :
// - root (first directory, Home)
// - then path
function show_path($dir)
{
echo '<a href="?gallery=/" title="Index">Index</a>';
$link = '';
$full_path = '';
if ($dir != '') {
$path = explode('/', $dir);
foreach ($path as $step) {
$link .= $step.'/';
// last step, assemble and apply gallery_link
$full_path .= ' / <a href="?gallery='.link_safe(gallery_link($link)).'" title="'.link_safe($step).'">'.html_safe($step).'</a>';
}
}
echo $full_path;
}
// withdrew the ending slash from a name
// (if one found)
// not so useful, more kind of an alias
function no_slash($name)
{
return rtrim($name, '/');
}
// format a link to a sub gallery
// url encode and remove last right slash
function gallery_link($name)
{
return urlencode(no_slash($name));
}
// escape " but not '
function link_safe($string)
{
return htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
}
// html encode everything, including accents
function html_safe($string)
{
return htmlentities($string, ENT_QUOTES, 'UTF-8');
}
/* EOF */