added gallery functions

This commit is contained in:
azett 2022-06-16 12:32:12 +02:00
parent 226482d3b6
commit 1eb93121d5
2 changed files with 148 additions and 41 deletions

View File

@ -0,0 +1,110 @@
<?php
/*
* Functions for handling image galleries.
*/
/**
* The name of the captions file
*
* @var string
*/
const GALLERY_CAPTIONS_FILENAME = 'captions.conf';
/**
* The name of the captions file (legacy mode for galleries managed with PhotoSwipe plugin version < 1.1)
*
* @var string
*/
const GALLERY_CAPTIONS_LEGACYFILENAME = 'texte.conf';
/**
* Reads the images from the gallery directory and returns their names as iterative array
*
* @param string $galleryDir
* the gallery dir, e.g. 'images/NameOfTheGallery'
* @return array
*/
function gallery_read_images($galleryDir) {
$d = substr_replace($galleryDir, IMAGES_DIR, 0, 7);
$fs = new fs_filelister($d);
$l = $fs->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 <code>true</code> if captions were written successfully; <code>false</code> 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;
}

View File

@ -2,14 +2,12 @@
// includes.php
// this is just a list of all the standard includes
require_once INCLUDES_DIR . 'core.utils.php';
utils_checksmarty();
require (SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty;
$smarty = new Smarty();
$_FP_SMARTY = & $smarty;
// WordPress plugin system
require_once INCLUDES_DIR . 'core.wp-plugin-interface.php';
require_once INCLUDES_DIR . 'core.wp-functions.php';
@ -17,14 +15,12 @@
require_once INCLUDES_DIR . 'core.wp-formatting.php';
require_once INCLUDES_DIR . 'core.wp-default-filters.php';
require_once INCLUDES_DIR . 'core.filesystem.php';
require_once INCLUDES_DIR . 'core.fileio.php';
require_once INCLUDES_DIR . 'core.cache.php';
require_once INCLUDES_DIR . 'core.blogdb.php';
require_once INCLUDES_DIR . 'core.bplustree.class.php';
require_once INCLUDES_DIR . 'core.administration.php';
require_once INCLUDES_DIR . 'core.widgets.php';
require_once INCLUDES_DIR . 'core.comment.php';
@ -44,5 +40,6 @@
require_once INCLUDES_DIR . 'core.theme.php';
// require_once INCLUDES_DIR.'core.layout.php';
require_once INCLUDES_DIR . 'core.users.php';
require_once INCLUDES_DIR . 'core.gallery.php';
?>