diff options
Diffstat (limited to 'edit.cgi')
-rwxr-xr-x | edit.cgi | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/edit.cgi b/edit.cgi new file mode 100755 index 0000000..d24657c --- /dev/null +++ b/edit.cgi @@ -0,0 +1,174 @@ +#!/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 "<html><head><title>Edit metadata</title>" +echo "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" +echo "</head><body>" +echo "<pre>" +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' "</body></html>" + 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 "</pre>" + +# show image if enabled +test "${show_image}" = "1" && { + if test "${show_thumbnail}" = "1" ; + then + echo "<img src=\"${thumburl}\">" + else + # show full image + echo "<img src=\"${dataurl}\" width=\"100%\" max-width=\"800px\">" + fi + echo "<br>" +} + +# 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 "<form method=\"post\" enctype="multipart/form-data" action=\"${apply_link}\" id=\"edit\">" +echo " <label for=\"title\">Title:</label>" +echo " <input type=\"text\" id=\"title\" name=\"title\" value=\"${title}\" ${placeholdertag} ${disabled}><br>" +test "${image}" = "folder" && { + echo " <label for=\"thumbnail\">Thumbnail:</label>" + echo " <input type=\"text\" id=\"thumbnail\" name=\"thumbnail\" value=\"${thumbnail}\" ${disabled}><br>" + echo " <label for=\"author\">Author:</label>" + echo " <input type=\"text\" id=\"author\" name=\"author\" value=\"${author}\" ${disabled}><br>" +} +echo " <label for=\"description\">Description:</label>" +#echo " <input form=\"edit\" type=\"textarea\" id=\"description\" name=\"description\" value=\"${description}\" ${disabled} rows=\"5\"></input><br>" +echo " <textarea form=\"edit\" type=\"textarea\" id=\"description\" name=\"description\" ${disabled} rows=\"5\">${description}</textarea><br>" +echo " <input type=\"hidden\" id=\"metadatafile\" name=\"metadatafile\" value=\"${metadatafile}\">" +echo " <input type=\"hidden\" id=\"referer\" name=\"referer\" value=\"${HTTP_REFERER}\">" +echo " <input type=\"hidden\" id=\"id\" name=\"id\" value=\"${gallery_id}\">" +echo " <input type=\"reset\" value=\"Reset\">" +echo " <input type=\"submit\" value=\"Update\">" +echo "</form>" +printf '%s' "</body></html>" +exit ${response} |