aboutsummaryrefslogtreecommitdiff
path: root/edit.cgi
blob: d24657c3c31974f6c901562a2816986781d0741b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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}
bgstack15