foto: translate tag album requests to .tag
[minimedit.git] / foto / index.php
1 <?php
2 $rootdir = $Page->link;
3
4 $nav = explode('/', $rootdir);
5 if (count($nav) > 1 and $nav[1] == 'tag') {
6         $rootdir = preg_replace('{/}', '/.', $rootdir, 1);
7 }
8 $nav[0] = "Foto's"; # override of root 'foto'
9 $title = array_pop($nav);
10 $Page->title = ($nav ? implode(' ', $nav) . ': ' : '') . $title;
11
12 if ($_SERVER['HTTP_ACCEPT'] === 'application/json') {
13         header('Access-Control-Allow-Origin: *');
14         $imgs = [];
15         foreach (glob("$rootdir/*.jpg") as $path) {
16                 $target = preg_replace('{^(\.\./)*}', '', readlink($path));
17                 $imgs[] = ["/$target"];
18         }
19         print json_encode($imgs);
20         exit;
21 }
22 elseif ($Page->api) {
23         $img = "$rootdir/index.jpg";
24         if (file_exists($img)) {
25                 # cover image of current album
26                 $Page->image = "/$img";
27         }
28         if (!$Page->path) {
29                 return array_map(function ($dir) {
30                         return new ArchiveArticle($dir . '/index.html');
31                 }, glob("$rootdir/*", GLOB_ONLYDIR)); #TODO: recurse
32         }
33         return;
34 }
35
36 if ($User->admin('foto')) {
37         if ($Page->restricted) {
38                 $access = '<span class="icon icon-locked">&#x1F512;</span> Bewoners';
39                 if ($Page->restricted != $rootdir) {
40                         $access .= sprintf(' vanaf <a href="%s">%s</a>',
41                                 "/{$Page->restricted}", pathinfo($Page->restricted, PATHINFO_FILENAME)
42                         );
43                 }
44         }
45         else {
46                 $access = '<span class="icon icon-locked">&#x1F513;</span> Openbaar';
47         }
48         print "<aside>$access</aside>\n\n";
49 }
50
51 $link = '';
52 print "<h2>";
53 foreach ($nav as $i => $linktitle) {
54         $link .= '/' . ($i ? $linktitle : $Page->handler);
55         printf('<a href="%s">%s</a> →'."\n", $link, $linktitle);
56 }
57 print $title;
58 print "</h2>\n\n";
59
60 if (isset($Page->raw)) {
61         print $Page->raw;  # page intro
62 }
63
64 if ($imgs = glob("$rootdir/*", GLOB_ONLYDIR)) {
65         natsort($imgs);
66         print '<ul class="gallery cat">'."\n";
67         foreach ($imgs as $path) {
68                 $album = htmlspecialchars(pathinfo($path, PATHINFO_FILENAME));
69                 $cover = "$path/index.jpg";
70                 if (!file_exists($cover)) $cover = 'foto/index.jpg';
71                 if (is_link($cover)) {
72                         $cover = preg_replace('{^(?:\.\./)*(?=data/)}', 'thumb/100/', readlink($cover));
73                 }
74
75                 $html = sprintf('<img src="/%s" />', htmlspecialchars($cover));
76                 $html .= "<figcaption>$album</figcaption>";
77                 if (!$User->login and file_exists("$path/.private")) {
78                         $html = '<s title="bewoners">'.$html.'</s>';
79                 }
80                 $html = "<figure>$html</figure>";
81
82                 printf('<li id="%s">', $album);
83                 printf('<a href="/%s">%s</a>'."\n", htmlspecialchars($path), $html);
84         }
85         print "</ul>\n\n";
86 }
87
88 if ($imgs = glob("$rootdir/*.jpg")) {
89         print '<ul class="gallery album">'."\n";
90         foreach ($imgs as $path) {
91                 if ($path == "$rootdir/index.jpg") {
92                         # cover image of current album
93                         $Page->image = "/$path";
94                         continue;
95                 }
96                 if (!is_link($path)) continue;
97
98                 // assume all album entries are symlinks to archive originals
99                 $target = preg_replace('{^(\.\./)*}', '', readlink($path));
100                 $thumb = 'thumb/262/' . $target;
101
102                 @list ($order, $size, $title) = explode(':', pathinfo($path, PATHINFO_FILENAME), 3);
103                 $imgtag = 'img src="/'.$thumb.'"';
104                 if ($title) {
105                         $imgtag .= ' title="'.htmlspecialchars(urldecode($title)).'"';
106                 }
107                 if ($size) {
108                         $imgtag .= ' data-size="'.$size.'"';
109                 }
110
111                 print '<li>';
112                 printf('<a href="/%s"><%s /></a>'."\n", $target, $imgtag);
113         }
114         print '</ul>'."\n\n";
115
116         include 'foto/album.inc.php';
117 }
118
119 return;