-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
209 lines (195 loc) · 7.53 KB
/
functions.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/*
This file is part of viood.
viood is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
viood is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
require_once 'config.php';
/**
* Recurses into a directory and writes the structure
* in an associative array
* @global string $pictureDir from config.php
* @param string $directory The directory to recurse into
* @param array $array The array to put the results in
* @param int $depth How deep should it go? (Default 1)
* @return array
*
*/
function recurse_dir($directory, $array, $depth=1, $include_info=true, $full_path=false) {
//TODO: if the folder contains neither images, nor folders, don't display
// echo $directory,"<br><br>";
global $pictureDir;
$relativePath = str_replace("..", "", $directory);
// echo $relativePath;
$path = $pictureDir.$relativePath;
// echo $path;
$dirName = basename($path);
if (!is_dir($path)) include '404.php';
$gallery_info = getGalleryInfo($dirName);
//get images to be ignored from gallery.json
if (is_array($gallery_info["ignore"])) $ignore_images = $gallery_info["ignore"];
else $ignore_images = array();
if ($handle = opendir($path)) {
//for each entry found inside $directory
while (false !== ($entry = readdir($handle))) {
$relativePath = $directory."/".$entry;
// echo $relativePath."<br>";
$path = $pictureDir."/".$relativePath;
//echo "$pictureDir -- $dirName -- $path -- $entry \n </br>";
//ignore . and .. , write directories to the array and recurse into them
if ($entry != "." && $entry != ".." && !in_array($entry,$ignore_images)) {
if (is_dir($path)) {
$array[$entry] = array();
if ($include_info) $array[$entry]["__info__"] = getGalleryInfo($path);
if ($depth!=0) $array[$entry] = recurse_dir($relativePath, $array[$entry], $depth - 1, $include_info, $full_path);
} else if(is_image($path) && is_supported($path)){
$array[$entry]= $full_path?$relativePath:$entry;
}
}
}
closedir($handle);
} //echo "final"; _print_r($array);
return $array;
}
/**
* Prints the html required to show a gallery
* @global string $base_url from config.php
* @global string $pictureDir from config.php
* @param string $path the path of the gallery we're printing div's from
* @param array the array with the directory structure
*/
function print_divs($path, $array) {
global $base_url;
global $pictureDir;
$rel_path = str_replace($pictureDir, "", $path);
foreach ($array as $name => $item) {
if ($name == "__info__") continue;
if (is_array($item) && count($item)) {
$info=array_shift($item);
//check if hidden
if (!isset($info['hidden']) || $info['hidden']==false){
//check json for flagship
if (isset($info['flagship']) && file_exists($pictureDir . "/" .$path . "/" . $name . "/" . $info['flagship']))
$galleryFlagship = $info['flagship']; else $galleryFlagship = reset($item);
?><a class="gallery" href="<?=$name?>/"><!--
flagship=<?= $info['flagship']; ?> resetitem= <?= reset($item); ?>
--><div class="galleryOverlay"><h3><?php echo $info["name"]; ?></h3><?=$info["description"]?></div>
<img class="gallery" src="<?= getThumb($path . "/" . $name . "/" . $galleryFlagship ) ?>"/></a><?php
}
} else { //sorry for the mess we must not have any spaces between inline-block items
?><a href="<?=$base_url."slideshow".$rel_path . $item?>"/><img class="picture" src="<?= getThumb($path . $item) ?>"/></a><?php
}
}
}
/**
* Returns the mime type of a file using php fileinfo
* @param string $file path to file
* @return string
*/
function mimetype($file){
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $file);
finfo_close($finfo);
return $mimetype;
}
/**
* Checks mime type to see if file is image
* @param string $file the path to file to check if is image
* @return type
*/
function is_image($file){
return strstr(mimetype($file),"image")!==FALSE;
}
/**
* Checks mime type to see if file is supported
* @param string $file the path to file to check if supported
* @return type
*/
function is_supported($file){
return strstr(mimetype($file),"jpeg")!==FALSE ||
strstr(mimetype($file),"png")!==FALSE ||
strstr(mimetype($file),"gif")!==FALSE;
}
/**
* Same as builtin print_r but wrapped in <pre> tags
* If you have php_pdl you can uncomment two lines so that it overrides
* the default print_r
* @param type $array
*/
//override_function('print_r', '$array', 'return _print_r($array);');
function _print_r($array){
echo "<pre>";
print_r($array);
// __overridden__($array);
echo "</pre>";
}
/**
* Passes the image to liveThumb and returns the thumbnail
* @global string $base_url
* @param string $path The image path
* @return string The thumbnail url
*/
function getThumb($path){
global $base_url;
$lt = new Livethumb(array("thumbWidth"=>400, "thumbHeight"=>400, "fname"=>basename($path), "pathToImages"=> str_replace(basename($path), "", $path)));
return $base_url.$lt->getSrc();
}
/**
* Same as getThumb() but returns a bigger image
* @global string $base_url
* @param string $path The image path
* @return string The thumbnail url
*/
function getSlide($path){
global $base_url;
$lt = new Livethumb(array("thumbWidth"=>1980, "thumbHeight"=>1980, "fname"=>basename($path), "pathToImages"=> str_replace(basename($path), "", $path)));
return $base_url.$lt->getSrc();
}
/**
* Reads gallery.json and returns the data decoded into an associative array
* @param string $path The path to the gallery
* @return array The gallery info as an array
*/
function getGalleryInfo($path){
if (file_exists($path."/gallery.json"))
return json_decode(file_get_contents($path."/gallery.json"),true);
else return array();
}
/**
* Checks if there are standalone images in the requested path or only other galleries
* @param array $gallery_array The gallery
* @return bool Whether the gallery has images or not
*/
function hasImages($gallery_array){
foreach ($gallery_array as $item){
if (!is_array($item)){
return true;
}
}
return false;
}
function printSlideshowIcon($requested_path){
if (hasImages($requested_path)) { ?>
<a href="<?= $base_url."slideshow".$requested_path ?>/">
<img src="<?=$base_url?>slideshow.svg" alt="slideshow">
</a>
<?php }
}
/**
* Flattens the recursive array into just a list of images. Removes all info nodes
* @param array $array The gallery multidimensional array
* @return array A monodimentional array
*/
function flatten($array){
$return = array();
array_walk_recursive($array, function($a) use (&$return) { if ($a)$return[] = $a; });
return $return;
}