login/pass: error messages below page title
[minimedit.git] / foto / index.php
index 27482a484134b72485ed0dc8e5d298112631d563..45c49c90458fa05bd050b2ba38f707f8d8f01f08 100644 (file)
@@ -1,12 +1,51 @@
 <?php
-$rootdir = $Page . $Args;
+$rootdir = $Page->link;
+
+$nav = explode('/', $rootdir);
+if (count($nav) > 1 and $nav[1] == 'tag') {
+       $rootdir = preg_replace('{/}', '/.', $rootdir, 1);
+}
+$nav[0] = "Foto's"; # override of root 'foto'
+$title = array_pop($nav);
+$Page->title = ($nav ? implode(' ', $nav) . ': ' : '') . $title;
+
+if ($_SERVER['HTTP_ACCEPT'] === 'application/json') {
+       header('Access-Control-Allow-Origin: *');
+       header('Cache-Control: max-age=10');
+       if (file_exists($rootdir)) {
+               header('Last-Modified: '.gmdate(DATE_RFC7231, filemtime($rootdir)));
+       }
+       else {
+               http_response_code(404);
+       }
+       $imgs = [];
+       foreach (glob("$rootdir/*.jpg") as $path) {
+               $target = preg_replace('{^(\.\./)*}', '', readlink($path));
+               $imgs[] = ["/$target"];
+       }
+       print json_encode($imgs);
+       exit;
+}
+elseif ($Page->api) {
+       $img = "$rootdir/index.jpg";
+       if (file_exists($img)) {
+               # cover image of current album
+               $Page->image = "/$img";
+       }
+       if (!$Page->path) {
+               return array_map(function ($dir) {
+                       return new ArchiveArticle($dir . '/index.html');
+               }, glob("$rootdir/*", GLOB_ONLYDIR)); #TODO: recurse
+       }
+       return;
+}
 
 if ($User->admin('foto')) {
-       if (!empty($PageAccess)) {
+       if ($Page->restricted) {
                $access = '<span class="icon icon-locked">&#x1F512;</span> Bewoners';
-               if ($PageAccess != $rootdir) {
+               if ($Page->restricted != $rootdir) {
                        $access .= sprintf(' vanaf <a href="%s">%s</a>',
-                               "/$PageAccess", pathinfo($PageAccess, PATHINFO_FILENAME)
+                               "/{$Page->restricted}", pathinfo($Page->restricted, PATHINFO_FILENAME)
                        );
                }
        }
@@ -16,76 +55,68 @@ if ($User->admin('foto')) {
        print "<aside>$access</aside>\n\n";
 }
 
-$nav = explode('/', $rootdir);
-$nav[0] = "Foto's"; # override of root 'foto'
-$title = array_pop($nav);
-$Article->title = ($nav ? implode(' ', $nav) . ': ' : '') . $title;
-
 $link = '';
 print "<h2>";
 foreach ($nav as $i => $linktitle) {
-       $link .= '/' . ($i ? $linktitle : $Page);
+       $link .= '/' . ($i ? $linktitle : $Page->handler);
        printf('<a href="%s">%s</a> →'."\n", $link, $linktitle);
 }
 print $title;
 print "</h2>\n\n";
 
-if (isset($Article->raw)) {
-       print $Article->raw;  # page intro
-}
-
-function showthumb($path)
-{
-       // assume all album entries are symlinks to archive originals
-       $target = preg_replace('{^(\.\./)*}', '', readlink($path));
-       $thumb = 'thumb/262/' . $target;
-
-       @list ($order, $size, $title) = explode(':', pathinfo($path, PATHINFO_FILENAME), 3);
-       $imgtag = 'img src="/'.$thumb.'"';
-       if ($title) {
-               $imgtag .= ' title="'.htmlspecialchars(urldecode($title)).'"';
-       }
-       if ($size) {
-               $imgtag .= ' data-size="'.$size.'"';
-       }
-
-       return sprintf('<a href="/%s"><%s /></a>'."\n", $target, $imgtag);
+if (isset($Page->raw)) {
+       print $Page->raw;  # page intro
 }
 
 if ($imgs = glob("$rootdir/*", GLOB_ONLYDIR)) {
        natsort($imgs);
        print '<ul class="gallery cat">'."\n";
        foreach ($imgs as $path) {
-               $parts = pathinfo($path);
-               $album = $parts['filename'];
+               $album = htmlspecialchars(pathinfo($path, PATHINFO_FILENAME));
                $cover = "$path/index.jpg";
                if (!file_exists($cover)) $cover = 'foto/index.jpg';
                if (is_link($cover)) {
                        $cover = preg_replace('{^(?:\.\./)*(?=data/)}', 'thumb/100/', readlink($cover));
                }
 
-               $html = '<img src="/'.$cover.'" />';
+               $html = sprintf('<img src="/%s" />', htmlspecialchars($cover));
                $html .= "<figcaption>$album</figcaption>";
                if (!$User->login and file_exists("$path/.private")) {
                        $html = '<s title="bewoners">'.$html.'</s>';
                }
                $html = "<figure>$html</figure>";
 
-               printf('<li id="%s"><a href="%s">%s</a>'."\n", $album, "/$path", $html);
+               printf('<li id="%s">', $album);
+               printf('<a href="/%s">%s</a>'."\n", htmlspecialchars($path), $html);
        }
        print "</ul>\n\n";
 }
 
 if ($imgs = glob("$rootdir/*.jpg")) {
        print '<ul class="gallery album">'."\n";
-       foreach ($imgs as $img) {
-               if ($img == "$rootdir/index.jpg") {
+       foreach ($imgs as $path) {
+               if ($path == "$rootdir/index.jpg") {
                        # cover image of current album
-                       $Article->image = "/$img";
+                       $Page->image = "/$path";
                        continue;
                }
-               if (!is_link($img)) continue;
-               print '<li>'.showthumb($img);
+               if (!is_link($path)) continue;
+
+               // assume all album entries are symlinks to archive originals
+               $target = preg_replace('{^(\.\./)*}', '', readlink($path));
+               $thumb = 'thumb/262/' . $target;
+
+               @list ($order, $size, $title) = explode(':', pathinfo($path, PATHINFO_FILENAME), 3);
+               $imgtag = 'img src="/'.$thumb.'"';
+               if ($title) {
+                       $imgtag .= ' title="'.htmlspecialchars(urldecode($title)).'"';
+               }
+               if ($size) {
+                       $imgtag .= ' data-size="'.$size.'"';
+               }
+
+               print '<li>';
+               printf('<a href="/%s"><%s /></a>'."\n", $target, $imgtag);
        }
        print '</ul>'."\n\n";