Mercurial > hg > mgallery
annotate mgallery.php @ 5:1bf73b49fc91
Beautify the generated URLs by removing occurences of duplicate forward
slash //.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 15 May 2015 16:33:30 +0300 |
parents | 014e1d6991a3 |
children | dec931d9e860 |
rev | line source |
---|---|
0 | 1 <?php |
2 // | |
3 // Yet Another Image Gallery | |
4 // (C) Copyright 2015 Tecnic Software productions (TNSP) | |
5 // | |
6 require "msitegen.inc.php"; | |
7 require "mgallery.inc.php"; | |
8 | |
9 | |
10 // | |
11 // Various utility functions | |
12 // | |
13 function mgGetImageURL() | |
14 { | |
15 global $galImageURL, $galPath; | |
5
1bf73b49fc91
Beautify the generated URLs by removing occurences of duplicate forward
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
16 return str_replace("//", "/", $galImageURL.$galPath."/".implode("", func_get_args())); |
0 | 17 } |
18 | |
19 | |
20 function mgGetURL($path, $image, $entities = TRUE) | |
21 { | |
22 global $galBaseURL, $galCleanURLS; | |
23 $amp = $entities ? "&" : "&"; | |
24 | |
25 if ($galCleanURLS) | |
26 { | |
5
1bf73b49fc91
Beautify the generated URLs by removing occurences of duplicate forward
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
27 $url = $galBaseURL.$path."/".($image !== FALSE ? $image : ""); |
0 | 28 } |
29 else | |
30 { | |
5
1bf73b49fc91
Beautify the generated URLs by removing occurences of duplicate forward
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
31 $url = |
0 | 32 $galBaseURL.mgGetSetting("mgallery_php")."?path=". |
33 $path.($image !== FALSE ? $amp."image=".$image : ""); | |
34 } | |
5
1bf73b49fc91
Beautify the generated URLs by removing occurences of duplicate forward
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
35 |
1bf73b49fc91
Beautify the generated URLs by removing occurences of duplicate forward
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
36 return str_replace("//", "/", $url); |
0 | 37 } |
38 | |
39 | |
40 function mgGetNaviActive(&$galIndex, $index, $delta, &$res, &$url, $entities) | |
41 { | |
42 global $galPath; | |
43 $res = $index + $delta; | |
44 if ($res >= 0 && $res <= sizeof($galIndex) - 1) | |
45 { | |
46 $url = mgGetURL($galPath, $galIndex[$res], $entities); | |
47 return TRUE; | |
48 } | |
49 else | |
50 return FALSE; | |
51 } | |
52 | |
53 | |
54 function mgGetNaviControlImage(&$galIndex, $index, $class, $url) | |
55 { | |
56 global $galTNPath; | |
57 | |
58 $img = "<div class=\"imageCtrl ".$class."\">"; | |
59 | |
60 if ($url !== FALSE) | |
61 { | |
62 $img .= | |
63 "<a href=\"".$url."\"><img src=\"". | |
64 mgGetImageURL($galTNPath, $galIndex[$index]). | |
65 "\" alt=\"".$galIndex[$index]."\" /></a>"; | |
66 } | |
67 | |
68 return $img."</div>\n"; | |
69 } | |
70 | |
71 | |
72 function mgGetNaviControlImageBox(&$galIndex, $index, $class, $delta) | |
73 { | |
74 if (!mgGetNaviActive($galIndex, $index, $delta, $res, $url, TRUE)) | |
75 $url = FALSE; | |
76 | |
77 return mgGetNaviControlImage($galIndex, $res, $class, $url); | |
78 } | |
79 | |
80 | |
81 function mgGetControl($str, $class, &$galIndex, $index, $delta, $naviFlags) | |
82 { | |
83 $active = mgGetNaviActive($galIndex, $index, $delta, $res, $url, TRUE); | |
84 if ($active && ($naviFlags & GNAV_IMG)) | |
85 $img = mgGetNaviControlImage($galIndex, $res, $class, $url); | |
86 else | |
87 $img = ""; | |
88 | |
89 if ($naviFlags & GNAV_TEXT) | |
90 $str = "<span class=\"naviControl ".$class."\">[".($active ? "<a href=\"".$url."\">".$str."</a>" : $str)."]</span>"; | |
91 else | |
92 $str = ""; | |
93 | |
94 if ($delta < 0) | |
95 return $img.$str; | |
96 else | |
97 return $str.$img; | |
98 } | |
99 | |
100 | |
101 function mgGetNaviControls(&$galIndex, $index, $naviFlags) | |
102 { | |
103 global $galPath; | |
104 | |
105 return | |
106 "<div class=\"naviControls\">". | |
107 mgGetControl("<<", "prev", $galIndex, $index, -1, $naviFlags). | |
108 "[<a href=\"".mgGetURL($galPath, FALSE)."\">^^</a>]". | |
109 mgGetControl(">>", "next", $galIndex, $index, 1, $naviFlags). | |
110 "</div>\n"; | |
111 } | |
112 | |
113 | |
114 function mgPrintTable($class, &$galEntries, &$galIndex, $start, $limit) | |
115 { | |
116 global $galAlbumIcon, $galPath, $galTNPath; | |
117 | |
118 $galCount = count($galIndex); | |
119 if ($start >= $galCount) | |
120 return $start; | |
121 | |
122 $end = ($limit === FALSE) ? $galCount : $start + $limit; | |
123 if ($end > $galCount) $end = $galCount; | |
124 | |
125 $rowLimit = mgGetSetting("album_row_limit"); | |
126 $n = 0; | |
127 | |
128 echo "<table class=\"".$class."\">\n"; | |
129 for ($index = $start; $index < $end; $index++) | |
130 { | |
131 $filename = &$galIndex[$index]; | |
132 $data = &$galEntries[$filename]; | |
133 | |
134 if ($n == 0) echo " <tr>\n"; | |
135 | |
136 echo | |
137 " <td id=\"cd".$data["base"]."\">\n"; | |
138 | |
139 if ($data["type"] == 0) | |
140 { | |
141 echo | |
142 "<div class=\"imageBox\"><a href=\"".mgGetURL($galPath, $filename)."\">". | |
143 "<img src=\"".mgGetImageURL($galTNPath, $filename)."\" alt=\"". | |
144 chentities($filename)."\"></a>". | |
145 "</div>". | |
146 mgGetArr($data, "caption", "<div class=\"imageCaption\">%1</div>", "", "chentities"); | |
147 /* | |
148 if ($mode == "") | |
149 { | |
150 echo | |
151 " <select class=\"dropdown\" id=\"dd".$data["base"]."\" name=\"dd".$data["base"]. | |
152 "\" onchange=\"galPhotoDataChanged('".$data["base"]."');\">\n"; | |
153 | |
154 foreach ($picChoices as $name => $value) | |
155 { | |
156 echo " <option value=\"$value\"".($value == $data["id"] ? " selected=\"selected\"" : "").">".chentities($name)."</option>\n"; | |
157 } | |
158 echo | |
159 " </select>\n"; | |
160 } | |
161 */ | |
162 } | |
163 else | |
164 { | |
165 echo | |
166 " <a href=\"".mgGetURL(mgCleanPath(TRUE, $galPath, $data["base"]), FALSE)."\">". | |
167 "<img class=\"albumIcon\" src=\"".$galAlbumIcon."\" alt=\"".chentities($data["caption"])."\" />\n". | |
168 "<div class=\"albumTitle\">".chentities($data["caption"])."</div></a>\n"; | |
169 } | |
170 | |
171 echo | |
172 " </td>\n"; | |
173 | |
174 if (++$n >= $rowLimit) | |
175 { | |
176 echo " </tr>\n"; | |
177 $n = 0; | |
178 } | |
179 } | |
180 if ($n > 0) | |
181 { | |
182 while ($n++ < $rowLimit) | |
183 echo " <td></td>\n"; | |
184 echo " </tr>\n"; | |
185 } | |
186 echo "</table>\n"; | |
187 return $index; | |
188 } | |
189 | |
190 | |
191 function mgTimeStr($str) | |
192 { | |
193 $tmp = date_create_from_format("Y:m:d H:i:s", $str); | |
194 return date_format($tmp, "d M Y (H:i)"); | |
195 } | |
196 | |
197 | |
198 function mgPrintPageInfoFooter() | |
199 { | |
200 if (($str = mgGetSetting("page_info")) !== FALSE) | |
201 echo "<div class=\"pageInfoFooter\">".$str."</div>"; | |
202 } | |
203 | |
204 | |
205 function mgPrintBreadCrumbs($galData) | |
206 { | |
207 $res = array(); | |
208 if ($galData["caption"]) | |
4
014e1d6991a3
Oops, fix the last breadcrumb.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
209 $res[] = "<a class=\"naviBreadCrumbCurr\" href=\"".mgGetURL($galData["path"], FALSE)."\">".chentities($galData["caption"])."</a>"; |
0 | 210 |
211 $tmp = $galData; | |
212 while (isset($tmp["parent"])) | |
213 { | |
214 $pdata = $tmp["parent"]; | |
3
c85f630a4198
Improve bread crumb navigation by making the current album a link as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
215 $res[] = "<a class=\"naviBreadCrumbItem\" href=\"".mgGetURL($pdata["path"], FALSE)."\">".chentities($pdata["caption"])."</a>"; |
0 | 216 $tmp = $tmp["parent"]; |
217 } | |
218 | |
219 if (count($res) > 1) | |
220 { | |
221 echo | |
222 "<div class=\"naviBreadCrumbs\">". | |
223 implode("<span class=\"naviBreadCrumbSep\"></span>", array_reverse($res)). | |
224 "</div>\n"; | |
225 } | |
226 } | |
227 | |
228 | |
229 // | |
230 // Get gallery settings | |
231 // | |
232 mgReadSettings(); | |
233 | |
234 $pageCSS = mgGetSetting("css"); | |
235 $pageCSSSelect = mgGetSetting("css_select"); | |
236 $galBasePath = mgGetSetting("base_path"); | |
237 $galBaseURL = mgGetSetting("base_url"); | |
238 $galImageURL = mgGetSetting("image_url", mgGetSetting("base_url")); | |
239 | |
240 $galAlbumIcon = mgGetSetting("album_icon"); | |
241 $galCleanURLS = mgGetSetting("clean_urls"); | |
242 $galTNPath = mgGetSetting("tn_path"); | |
243 $galMedSuffix = mgGetSetting("med_suffix"); | |
244 $galTitlePrefix = mgGetSetting("title_prefix"); | |
245 $galTitleSep = mgGetSetting("title_sep"); | |
246 | |
247 $galMode = stGetRequestItem("mode", "view", TRUE); | |
248 $galPath = stGetRequestItem("path", ".", TRUE); | |
249 $galPageIndex = intval(stGetRequestItem("index", 0, TRUE)); | |
250 $galImage = stGetRequestItem("image", FALSE, TRUE); | |
251 | |
252 if (is_string($galImage)) | |
253 $galImage = basename($galImage); | |
254 | |
255 | |
256 // | |
257 // Attempt to read the data cache file | |
258 // | |
259 $filename = mgGetPath(mgCleanPath(TRUE, $galBasePath, $galPath), "cache_file"); | |
260 $filename2 = mgGetPath(mgCleanPath(FALSE, $galBasePath, $galPath), "cache_file"); | |
261 if ($filename == $filename2 && file_exists($filename) && ($fp = @fopen($filename, "rb")) !== FALSE) | |
262 { | |
263 if (flock($fp, LOCK_SH)) | |
264 { | |
265 require($filename); | |
266 flock($fp, LOCK_UN); | |
267 } | |
268 fclose($fp); | |
269 } | |
270 | |
271 | |
272 // If no data available, show an error page | |
273 if (!isset($galData) || !isset($galEntries) || | |
274 !isset($galAlbumsIndex) || !isset($galImagesIndex)) | |
275 { | |
276 cmPrintPageHeader(mgGetVal(array("title_prefix", "title_sep"), "%1%2")."ERROR!"); | |
277 | |
278 echo | |
279 "<h1>Gallery error</h1>\n". | |
280 "<p>Gallery path <b>".chentities($galPath)."</b> does not exist or is invalid.</p>\n"; | |
281 | |
282 //echo "<p>".$filename."</p><p>".$filename2."</p>"; | |
283 | |
284 mgPrintPageInfoFooter(); | |
285 cmPrintPageFooter(TRUE); | |
286 exit; | |
287 } | |
288 | |
289 | |
290 // | |
291 // Print page header, etc. | |
292 // | |
293 if (($index = array_search($galImage, $galImagesIndex)) !== FALSE) | |
294 { | |
295 // | |
296 // Single image mode | |
297 // | |
298 $naviFlags = mgGetSetting("image_navigation"); | |
299 $data = $galEntries[$galImage]; | |
300 | |
301 $pageTitle = $galTitlePrefix.$galTitleSep.$galData["caption"]." - ".$galImage; | |
302 cmPrintPageHeader($pageTitle); | |
303 echo "<h1>".chentities($pageTitle)."</h1>\n"; | |
304 | |
305 if ($naviFlags & GNAV_BREADCRUMBS) | |
306 mgPrintBreadCrumbs($galData); | |
307 | |
308 if ($naviFlags & GNAV_TOP) | |
309 echo mgGetNaviControls($galImagesIndex, $index, $naviFlags); | |
310 | |
311 echo | |
312 "<div class=\"imageCBox\">\n". | |
313 mgGetNaviControlImageBox($galImagesIndex, $index, "prev", -1). | |
314 "<div class=\"imageBox\">\n". | |
315 "<a target=\"_blank\" href=\"".$galImageURL.$galPath."/".$galImage."\">". | |
316 "<img src=\"".mgGetImageURL($galTNPath, $data["base"].$galMedSuffix.$data["ext"])."\" alt=\"". | |
317 chentities($data["base"].$galMedSuffix.$data["ext"])."\"></a>\n". | |
318 "</div>\n". | |
319 mgGetNaviControlImageBox($galImagesIndex, $index, "next", 1). | |
320 "</div>\n". | |
321 "<div class=\"imageCaption\">".mgGetArr($data, "caption", "%1", "")."</div>\n"; | |
322 | |
323 $list = array( | |
324 mgGetArr($data, array("width", "height"), "<span class=\"infoDimensions\"><b>%1</b> x <b>%2</b> px</span>", NULL), | |
325 mgGetArr($data, "model", "<span class=\"infoModel\"><b>%1</b></span>", NULL), | |
326 mgGetArr($data, "fnumber", "<span class=\"infoFNumber\"><b>f/%1</b></span>", NULL), | |
327 mgGetArr($data, "exposure", "<span class=\"infoExposure\"><b>%1</b> sec</span>", NULL, NULL), | |
328 mgGetArr($data, "iso", "<span class=\"infoISO\">ISO <b>%1</b></span>", NULL), | |
329 ); | |
330 | |
331 echo | |
332 "<div class=\"infoBox\">\n". | |
333 mgGetArr($data, "datetime", "<span class=\"infoDateTime\">%1</span>", "", "mgTimeStr"). | |
334 implode(", ", array_filter($list, function($a) { return $a !== NULL; })). | |
335 "</div>\n"; | |
336 | |
337 if ($naviFlags & GNAV_BOTTOM) | |
338 echo mgGetNaviControls($galImagesIndex, $index, $naviFlags); | |
339 | |
340 // Javascript navigation | |
341 if ($naviFlags & GNAV_JAVASCRIPT) | |
342 { | |
343 $prevActive = mgGetNaviActive($galImagesIndex, $index, -1, $res, $prevURL, FALSE); | |
344 $nextActive = mgGetNaviActive($galImagesIndex, $index, 1, $res, $nextURL, FALSE); | |
345 echo | |
346 "<script type=\"text/javascript\">\n". | |
347 "var mgalPrevURL = \"".($prevActive ? $prevURL : "")."\";\n". | |
348 "var mgalNextURL = \"".($nextActive ? $nextURL : "")."\";\n". | |
349 "\n"; | |
350 ?> | |
351 function mgalNavigateTo(url) | |
352 { | |
353 if (url != "") | |
354 window.location = url; | |
355 } | |
356 | |
357 | |
358 function mgalProcessKeyPress(ev) | |
359 { | |
360 ev = ev || window.event; | |
361 var key = ev.keyCode ? ev.keyCode : ev.which; | |
362 switch (key) | |
363 { | |
364 case 37: | |
365 case 65: | |
366 // left | |
367 mgalNavigateTo(mgalPrevURL); | |
368 break; | |
369 | |
370 case 39: | |
371 case 68: | |
372 // right | |
373 mgalNavigateTo(mgalNextURL); | |
374 break; | |
375 } | |
376 } | |
377 | |
378 document.onkeypress = mgalProcessKeyPress; | |
379 <? | |
380 echo | |
381 "</script>\n"; | |
382 } | |
383 } | |
384 else | |
385 { | |
386 // | |
387 // Gallery mode | |
388 // | |
389 // - needs sub-modes / handling of order shit | |
390 // - Javascript stuff for picture data updates | |
391 // | |
392 $pageTitle = $galTitlePrefix.mgGetArr($galData, "caption", " - %1", "", "chentities"); | |
393 cmPrintPageHeader($pageTitle); | |
394 echo "<h1>".$pageTitle."</h1>\n"; | |
395 | |
396 $naviFlags = mgGetSetting("album_navigation"); | |
397 if ($naviFlags & GNAV_BREADCRUMBS) | |
398 mgPrintBreadCrumbs($galData); | |
399 | |
400 if (isset($galData["header"]) && strlen($galData["header"]) > 0) | |
401 echo "<div class=\"albumHeaderText\">".$galData["header"]."</div>\n"; | |
402 | |
403 mgPrintTable("albumTable", $galEntries, $galAlbumsIndex, 0, FALSE); | |
404 mgPrintTable("imageTable", $galEntries, $galImagesIndex, 0, FALSE); | |
405 } | |
406 | |
407 mgPrintPageInfoFooter(); | |
408 cmPrintPageFooter(TRUE); | |
409 ?> |