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