From 02479196c68b246854c5de613ed859247c9c107a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Tue, 28 Nov 2017 18:59:23 +0100 Subject: [PATCH] Handle absolute paths for images and data dirs Bizou now handles correctly data and images directories. One can now configure * `MEDIA_BASE_DIR`: base directory for `images` and `data` and * `{IMAGES,DATA}_URL`: the base url that should be used to point to a file in `IMAGES_DIR` (resp. `DATA_DIR`). Assuming the web server itself can correctly serve those files at the given URL and Bizou has the correct permissions for those locations, Bizou should be able to work as well as using relative paths. --- config.php | 15 +++++++++++++-- functions.php | 41 +++++++++++++++++++++++------------------ index.php | 15 +++++++++++---- template.php | 4 ++-- 4 files changed, 49 insertions(+), 26 deletions(-) diff --git a/config.php b/config.php index a4d29c9..fc57dd5 100644 --- a/config.php +++ b/config.php @@ -1,7 +1,18 @@ diff --git a/functions.php b/functions.php index 867bb49..8f568d6 100644 --- a/functions.php +++ b/functions.php @@ -28,7 +28,7 @@ function getPathInfo() if (! function_exists('getImageLink')) { function getImageLink($imageSimplePath) { - return $GLOBALS['rootUrl'].IMAGES_DIR.$imageSimplePath; + return $GLOBALS['rootUrl'].IMAGES_URL.$imageSimplePath; } } @@ -36,11 +36,13 @@ if (! function_exists('getPreview')) { function getPreview($imgFile, $maxSize = THUMB_SIZE) { # example: data/myalbum/100.mypic.jpg - $newImgFile = DATA_DIR."/".dirname($imgFile)."/".$maxSize.".".basename($imgFile); - + $origImgFile = IMAGES_DIR . $imgFile; + $basePath = dirname($imgFile)."/".$maxSize.".".basename($imgFile); + $newImgFile = DATA_DIR . "/" . $basePath; + # if the preview is a symlink, image is already good sized if (is_link($newImgFile)) return $imgFile; - + if (! is_file($newImgFile)) { # this tels the template to flush output after displaying previews @@ -51,10 +53,10 @@ function getPreview($imgFile, $maxSize = THUMB_SIZE) $ext = strtolower(substr($imgFile, -4)); if ($ext == ".jpg") { - $img = imagecreatefromjpeg($imgFile); - $exif = exif_read_data($imgFile); + $img = imagecreatefromjpeg($origImgFile); + $exif = exif_read_data($origImgFile); } else { - $img = imagecreatefrompng($imgFile); + $img = imagecreatefrompng($origImgFile); } if ($img === false) return ""; #read error (wrong permission...) @@ -96,25 +98,27 @@ function getPreview($imgFile, $maxSize = THUMB_SIZE) } else { imagepng($newImg, $newImgFile); } - + imagedestroy($img); imagedestroy($newImg); } - return $newImgFile; + return $basePath; } } function getAlbumPreview($dir) { - $previewFile = DATA_DIR."/$dir/albumpreview"; + $previewBase = "/$dir/albumpreview"; + $previewFile = DATA_DIR . $previewBase; + $dirPath = IMAGES_DIR . "/$dir/"; if (is_file("$previewFile.jpg")) { - return "$previewFile.jpg"; + return "$previewBase.jpg"; } else if (is_file("$previewFile.empty")) { return ""; } else if (is_file("$previewFile.png")) { - return "$previewFile.png"; + return "$previewBase.png"; } else { # config to allow group writable files umask(DATA_UMASK); @@ -123,17 +127,18 @@ function getAlbumPreview($dir) or die("Could not write in data dir. Please fix permissions."); // no preview: look for a preview in current dir, write it, return it - foreach (scandir($dir) as $file) if ($file[0] != '.') { + foreach (scandir($dirPath) as $file) if ($file[0] != '.') { $ext = strtolower(substr($file, -4)); if ($ext == ".jpg" or $ext == ".png") { - $thumb = getPreview("$dir/$file"); + $thumbUrl = getPreview("$dir/$file"); + $thumb = DATA_DIR . $thumbUrl; copy($thumb, $previewFile.$ext); - return $previewFile.$ext; - } else if (is_dir("$dir/$file")) { + return $previewBase.$ext; + } else if (is_dir("$dirPath/$file")) { $subPreview = getAlbumPreview("$dir/$file"); if ($subPreview) { - $myPreview = dirname($previewFile)."/".basename($subPreview); - copy($subPreview, $myPreview); + $myPreview = dirname($previewBase)."/".basename($subPreview); + copy(DATA_DIR . $subPreview, DATA_DIR . $myPreview); return $myPreview; } } diff --git a/index.php b/index.php index e77be4e..602fac0 100644 --- a/index.php +++ b/index.php @@ -28,7 +28,6 @@ if (substr($rootUrl, -1) !== '/') $rootUrl.='/'; // add a trailing / to rootUrl require 'functions.php'; - // if url == http://localhost/photos/index.php/toto/titi, path_info == /toto/titi // if url == http://localhost/photos/index.php, path_info is not set // if url == http://localhost/photos/, path_info is not set @@ -59,15 +58,23 @@ foreach (scandir($realDir) as $file) if ($file[0] != '.') { if (is_dir("$realDir/$file")) { - $folders[] = array( "name" => $file, "file" => "$realDir/$file", "link" => "$scriptUrl$simplePath/$file" ); + $folders[] = array( + "name" => $file, + "file" => "$simplePath/$file", + "link" => "$scriptUrl$simplePath/$file"); } else { $ext = strtolower(substr($file, -4)); if ($ext == ".jpg" or $ext == ".png") { - $imageFiles[] = array( "name" => $file, "file" => "$realDir/$file", "link" => getImageLink("$simplePath/$file") ); + $imageFiles[] = array( + "name" => $file, + "file" => "$simplePath/$file", + "link" => getImageLink("$simplePath/$file")); } else { - $otherFiles[] = array( "name" => $file, "link" => "$rootUrl$realDir/$file" ); + $otherFiles[] = array( + "name" => $file, + "link" => "$rootUrl$realDir/$file"); } } } diff --git a/template.php b/template.php index 28d5985..62536cf 100644 --- a/template.php +++ b/template.php @@ -64,7 +64,7 @@ a {
">
-
">
+
">
">
@@ -74,7 +74,7 @@ a {
- +