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