#!/bin/sh # File: /mnt/public/www/cgi-bin/gallery/edit.cgi # Author: bgstack15@gmail.com # Startdate: 2021-01-24 # SPDX-License-Identifier: GPL-3.0 # Title: Edit gallery item # Package: gallery # Purpose: edit media title and description from webpage # History: # Usage: called by the links generated by the custom theme for sigal # Reference: # https://tutorial.eyehunts.com/html/html-textarea-input-placeholder-value-resize-readonly/ # Improve: # Documentation: # needed parameters include id, image, and index. # Dependencies: # /etc/gallery-cgi.conf # sigal.conf.py from indicated gallery_id # apply.cgi # Reverse dependencies: # custom sigal theme that links to this when editing is enabled # apply.cgi DEBUG=0 response=0 export apply_link="apply.cgi" # web path to apply.cgi. Relative or absolute. export show_image=1 # if 0, then show no image at all export show_thumbnail=1 # if 1 then use thumbnail instead of full image test -e /etc/gallery-cgi.conf && . /etc/gallery-cgi.conf printf "%s\n\n" "Content-type: text/html" echo "Edit metadata" echo "" echo "" echo "
"
test -n "${DEBUG}" && test ${DEBUG} -ge 3 1>/dev/null 2>&1 && {
   env
}

# image is the image whose .md file needs 
echo "${QUERY_STRING}" | grep -qE "(^|\&|\?)image=[^\&]+" && {
   export image="$( echo "${QUERY_STRING}" | grep -oE "(^|\&|\?)image=[^\&]+" | awk -F'=' '{$1="";print;}' | sed -r -e 's:^ ::' )"
}
echo "${QUERY_STRING}" | grep -qE "(^|\&|\?)index=[^\&]+" && {
   export index="$( echo "${QUERY_STRING}" | grep -oE "(^|\&|\?)index=[^\&]+" | awk -F'=' '{$1="";print;}' | sed -r -e 's:^ ::' )"
}
# find gallery_id from query string
echo "${QUERY_STRING}" | grep -qE "(^|\&|\?)id=[^\&]+" && {
   export gallery_id="$( echo "${QUERY_STRING}" | grep -oE "(^|\&|\?)id=[^\&]+" | awk -F'=' '{$1="";print;}' | sed -r -e 's:^ ::' )"
}

# dynamically load the config file from the gallery_id defined in gallery-cgi.conf
eval export sigal_conf_py=\"\${"${gallery_id}"}\"/sigal.conf.py

# due to a limitation in the static site generator, I cannot get the full path of the image in the request, so we must rely on HTTP_REFERER and the provided index value (album.index_url).
test -z "${HTTP_REFERER}" && {
   echo "Invalid request. Please use the [edit] links in the main application."
   response=1
}

# learn edit_enabled from config file
edit_enabled="$( awk -F'=' '$1 ~ /edit_enabled/ {print $2}' "${sigal_conf_py}" | sed -r -e "s/[\"']//g;" -e 's/^ //;' | tr '[A-Z]' '[a-z]' )"

# between HTTP_REFERER and index we should be able to figure the actual path of the file whose metadata to edit
# count how many directories up
up_count="$( echo "${index}" | tr '/' '\n' | grep -cE '\.\.' )" ; up_count=$(( up_count + 1 ))
urlrootpath="$( echo "${HTTP_REFERER}" | sed -r -e 's@://@:==@;' | tr '\/' '\n' | head --lines=-${up_count} | tr '\n' '\/' | sed -r -e 's@:==@:\/\/@' )"
subpath="/$( echo "${HTTP_REFERER}" | sed -r -e 's@://@:==@;' | tr '\/' '\n' | tail --lines=${up_count} | tr '\n' '\/' | sed -r -e 's:\/+:\/:g;' -e 's:\/$::' )"
# remove any quote marks
filerootpath="$( awk -F'=' -v 'a="' -v "b='" "\$1 ~ /source/ && \$2 ~ /./ {gsub(\$2,a,\"\");gsub(\$2,b,\"\");print \$2;}" "${sigal_conf_py}" 2>&1 | sed -r -e "s/'//g;" -e 's/^ //;' )"

# react to if image=folder
if test "${image}" = "folder" ;
then
   dataurl="${urlrootpath%%/}${subpath}"
   # will not bother support thumbnails for folders
   datafile="${filerootpath%%/}${subpath}"
   metadatafile="${filerootpath%%/}${subpath%%/}/index.md"
else
   dataurl="${urlrootpath%%/}${subpath%%/}/${image}"
   thumburl="${urlrootpath%%/}${subpath%%/}/thumbnails/${image}"
   datafile="${filerootpath%%/}${subpath%%/}/${image}"
   metadatafile="${datafile%%.???}.md" # yes, hardcoded to the .3 ending
fi

test -n "${DEBUG}" && test ${DEBUG} -ge 2 1>/dev/null 2>&1 && {
   echo "image=${image}"
   echo "index=${index}"
   echo "up_count=${up_count}"
   echo "urlrootpath=${urlrootpath}"
   echo "subpath=${subpath}"
   echo "filerootpath=${filerootpath}"
   echo "dataurl=${dataurl}"
   echo "datafile=${datafile}"
   echo "metadatafile=${metadatafile}"
}

# it is OK if the metadatafile does not exist, we will just make it if writeable.
# but we will only bother to work with existing files
test ! -e "${datafile}" && {
   echo "That file does not exist. Please try again."
   response=1
   printf '%s' ""
   exit ${response}
}

# check if writable
if ! touch "${metadatafile}" ;
then
   echo "Need to determine what to do when not writeable."
   response=1
   export disabled="disabled" # or readonly, but disabled is clearer
fi
# check if edit_enabled
if test "${edit_enabled}" = "false" ;
then
   export disabled="disabled"
fi
# get current values of metadatafile, which is markdown format
title="$( awk -F':' '$1 ~ /Title/{$1="";print}' "${metadatafile}" 2>/dev/null | sed -r -e 's/^ *//;' )"
thumbnail="$( awk -F':' '$1 ~ /Thumbnail/{$1="";print}' "${metadatafile}" 2>/dev/null | sed -r -e 's/^ *//;' )"
author="$( awk -F':' '$1 ~ /Author/{$1="";print}' "${metadatafile}" 2>/dev/null | sed -r -e 's/^ *//;' )"
# description is everything except the above tags.
description="$( sed -r -e '/^(Title|Thumbnail|Author):/d' -e '/^\s*$/d' "${metadatafile}" )"
test -n "${DEBUG}" && test ${DEBUG} -ge 1 1>/dev/null 2>&1 && {
   echo "title: ${title}"
   test "${image}" = "folder" && echo "thumbnail: ${thumbnail}"
   echo "description: ${description}"
}
echo "
" # show image if enabled test "${show_image}" = "1" && { if test "${show_thumbnail}" = "1" ; then echo "" else # show full image echo "" fi echo "
" } # provide a placeholder if the title is empty for a directory test -z "${title}" && test "${image}" = "folder" && { placeholder="$( echo "${subpath}" | awk -F'/' '{print $NF}' )" placeholdertag="placeholder=\"${placeholder}\"" } # print the form if test "${image}" = "folder" ; then echo "Folder: ${dataurl}" else echo "Image: ${dataurl}" fi echo "
" echo " " echo "
" test "${image}" = "folder" && { echo " " echo "
" echo " " echo "
" } echo " " #echo "
" echo "
" echo " " echo " " echo " " echo " " echo " " echo "
" printf '%s' "" exit ${response}