From 1eb93121d5ff7b470f1cd891797c49f1acf2acf5 Mon Sep 17 00:00:00 2001 From: azett Date: Thu, 16 Jun 2022 12:32:12 +0200 Subject: [PATCH] added gallery functions --- fp-includes/core/core.gallery.php | 110 ++++++++++++++++++++++++++++++ fp-includes/core/includes.php | 79 +++++++++++---------- 2 files changed, 148 insertions(+), 41 deletions(-) create mode 100644 fp-includes/core/core.gallery.php diff --git a/fp-includes/core/core.gallery.php b/fp-includes/core/core.gallery.php new file mode 100644 index 0000000..19f0bad --- /dev/null +++ b/fp-includes/core/core.gallery.php @@ -0,0 +1,110 @@ +getlist(); + foreach ($l as $i => $f) { + // remove caption files + if ($f === GALLERY_CAPTIONS_FILENAME || $f === GALLERY_CAPTIONS_LEGACYFILENAME) { + array_splice($l, $i, 1); + } + } + sort($l); + return $l; +} + +/** + * Reads the captions from the given gallery directory. + * + * @param string $galleryDir + * the gallery dir, e.g. 'images/NameOfTheGallery' + * @return array the gallery captions as associative array { filename => caption } + */ +function gallery_read_captions($galleryDir) { + $captions = array(); + + $captionsFileContent = null; + $galleryDirPathAbs = ABS_PATH . FP_CONTENT . $galleryDir . '/'; + // read captions.conf from gallery dir + if (file_exists($galleryDirPathAbs . GALLERY_CAPTIONS_FILENAME)) { + $captionsFileContent = file($galleryDirPathAbs . GALLERY_CAPTIONS_FILENAME); + } // + // legacy mode: if captions.conf is not available, check for texte.conf + elseif (file_exists($galleryDirPathAbs . GALLERY_CAPTIONS_LEGACYFILENAME)) { + $captionsFileContent = file($galleryDirPathAbs . GALLERY_CAPTIONS_LEGACYFILENAME); + } // + // no caption file available + else { + return array(); + } + + // read captions file line by line + foreach ($captionsFileContent as $currentline) { + // image file name is before of the '=' character, ... + $image = trim(substr($currentline, 0, strpos($currentline, '='))); + // ... the caption after. + $caption = trim(substr($currentline, (strpos($currentline, '=') + 1))); + // $captions [$image] = htmlentities($descript); + $captions [$image] = $caption; + } + return $captions; +} + +/** + * Stores the given captions for the given gallery + * + * @param string $galleryName + * the gallery dir, e.g. 'NameOfTheGallery' + * @param array $captions + * the gallery captions as associative array { filename => caption } + * @return boolean true if captions were written successfully; false otherwise + */ +function gallery_write_captions($galleryName, $captions) { + $gallerydirPath = IMAGES_DIR . $galleryName; + if (!file_exists($gallerydirPath)) { + return false; + } + $captionfilePath = IMAGES_DIR . $galleryName . DIRECTORY_SEPARATOR . GALLERY_CAPTIONS_FILENAME; + $captionfileHandle = fopen($captionfilePath, 'w'); + + $filecontent = ''; + foreach ($captions as $filename => $caption) { + $filecontent .= $filename . ' = ' . $caption . PHP_EOL; + } + + fwrite($captionfileHandle, $filecontent); + fclose($captionfileHandle); + + // Updating from legacy versions: If legacy captions fill still exists, delete if + $legacyCaptionfilePath = IMAGES_DIR . $galleryName . DIRECTORY_SEPARATOR . GALLERY_CAPTIONS_LEGACYFILENAME; + if (file_exists($legacyCaptionfilePath)) { + unlink($legacyCaptionfilePath); + } + return true; +} \ No newline at end of file diff --git a/fp-includes/core/includes.php b/fp-includes/core/includes.php index 37cb377..58cd496 100755 --- a/fp-includes/core/includes.php +++ b/fp-includes/core/includes.php @@ -1,48 +1,45 @@