Forgot to add the new search.cgi.

This commit is contained in:
Darren 'Tadgy' Austin 2022-08-30 02:04:59 +01:00
commit f4036c3280

View file

@ -1,9 +1,13 @@
#!/bin/bash #!/bin/bash
# Configuration options # Configuration options
BASE_DIR="/data/sites"
STORAGE_PREFIX="/storage/md0" STORAGE_PREFIX="/storage/md0"
HEADER="/data/sites/slackware.uk/html/search_header.html" IGNORE_REGEXES=('^/\.sandbox.*' '^/\.lftp.*' '^/dead\.letter' '.*\.rsync-tmp.*')
FOOTER="/data/sites/slackware.uk/html/search_footer.html" HEADER_DARK="$BASE_DIR/${SERVER_NAME:-slackware.uk}/html/includes/search-header-dark.html"
FOOTER_DARK="$BASE_DIR/${SERVER_NAME:-slackware.uk}/html/includes/search-footer-dark.html"
HEADER_LIGHT="$BASE_DIR/${SERVER_NAME:-slackware.uk}/html/includes/search-header-light.html"
FOOTER_LIGHT="$BASE_DIR/${SERVER_NAME:-slackware.uk}/html/includes/search-footer-light.html"
LOCATEDB="/tmp/mirrors.db" LOCATEDB="/tmp/mirrors.db"
LIMIT=1000 LIMIT=1000
MAX_CONCURRENT=5 MAX_CONCURRENT=5
@ -13,55 +17,82 @@ shopt -s extglob
# Output an error in processing. # Output an error in processing.
die() { die() {
echo "<center><h2><b>Error: $1</b></h2><br></center>" # $1 = The error message to output.
# $2 = Whether to include the retry link ("false" to disable inclusion, everything else is true).
cat <<EOF
<br><br>
<center>
<table class="header" align="center">
<tr>
<td align="center">
<b class="subheading">Error: $1</b>$([[ "${2,,}" != "false" ]] && printf "<br>\n Please click <a href=\"%s\" title=\"Retry Search\">here</a> to retry your search." "${REQUEST_SCHEME:-https}://${SERVER_NAME:-slackware.uk}/search?$QUERY_STRING")
</td>
</tr>
</table>
</center>
</body>
</html>
EOF
exit 1 exit 1
} }
# A content type is required for CGI scripts. # Initial headers.
echo -ne "Content-type: text/html\r\n\r\n" printf "%s: %s\\n\\n" "Content-type" "text/html"
printf "%s: %s\\n\\n" "Cache-Control" "no-cache, no-store, must-revalidate"
printf "%s: %s\\n\\n" "Pragma" "no-cache"
printf "%s: %s\\n\\n" "Expires" "1"
# Include the header in the output. # Extract cookies.
# Note: The header cannot include PHP scripting. while read -r -d " " RAW; do
cat "$HEADER" KEY="${RAW%%=*}"
VALUE="${RAW#*=}"
declare -A COOKIES+=( ["${KEY,,}"]="${VALUE,,}" )
done <<<"$HTTP_COOKIE " # The space at the end is required.
# Limit the number of concurrent searches to avoid DoS. # Include the themed header.
if (( $(lsof -t "$LOCATEDB" | wc -l) > MAX_CONCURRENT - 1 )); then if [[ "${COOKIES['theme']}" == "dark" ]]; then
die "Too many concurrent searches - please try your search again in a few moments." cat "$HEADER_DARK"
else else
exec 9<"$LOCATEDB" && flock -s -E 10 -w 2 9 || die "Too many concurrent searches - please try your search again in a few moments." cat "$HEADER_LIGHT"
fi fi
while read -r -d '&'; do # Limit the number of concurrent searches to avoid DoS.
if (( $(lsof -t "$LOCATEDB" | wc -l) > (MAX_CONCURRENT - 1) )); then
die "too many concurrent searches!"
else
exec 9<"$LOCATEDB" && flock -s -E 10 -w 2 9 || die "too many concurrent searches!"
while read -r -d '&' QUERY; do
# If the read returned an empty string, skip. # If the read returned an empty string, skip.
[[ -z "$REPLY" ]] && continue [[ -z "$QUERY" ]] && continue
# Extract the key and value to temporary variables. # Extract the key and value to temporary variables.
KEY="${REPLY%%=*}" KEY="${QUERY%%=*}"
VALUE="${REPLY##*=}" VALUE="${QUERY#*=}"
# Check the key is valid as a variable name. # Check the key is valid as a variable name.
[[ ! "$KEY" =~ ^[[:alpha:]]+[[:digit:]_]*[[:alnum:]_]*$ ]] && die "Invalid key - please try your search again." [[ ! "$KEY" =~ ^[[:digit:]_]*[[:alnum:]_]*$ ]] && die "invalid query - don't try to be clever :)" "false"
# Remove spaces from beginning and end of value. # Remove spaces from beginning and end of value.
: "${VALUE/#+(+)}" : "${VALUE/#+(+)}"
: "${_/%+(+)}" : "${_/%+(+)}"
# Squash multiple spaces in value and add *s. # Squash multiple spaces in value.
: "${_//+(+)/+}" : "${_//+(+)/+}"
# Convert values from %-encoded form. # Convert values from %-encoded form.
: "${_//%/\\x}" : "${_//%/\\x}"
# Define the variable from the key name. # Define the variable from the key name.
declare $KEY="$(echo -e "${_//+/ }")" declare "X_$KEY"="$(printf "%b" "${_//+/ }")"
done <<<"${QUERY_STRING,,}&" done <<<"${QUERY_STRING,,}&" # The & at the end is required.
# Take a copy of q before it's modified, for the heading. # Take a copy of q before it's modified, for the heading.
Q_COPY="${q:-(empty)}" Q_COPY="${X_q:-(empty)}"
# Adjust 'q' for the locate command by wrapping search elements in *s. # Adjust 'X_q' for the locate command by wrapping search elements in *s.
: "*${q// /* *}*" : "*${X_q// /* *}*"
q="${_//\/*/*}" X_q="${_//\/*/*}"
# HTML boilerplate. # HTML boilerplate.
cat <<EOF cat <<EOF
@ -69,58 +100,60 @@ cat <<EOF
<table class="header" align="center"> <table class="header" align="center">
<tr> <tr>
<td align="center"> <td align="center">
<font size="+2"><b>Showing search results for: $Q_COPY</b></font><br><br> <b class="heading">Search Results</b><br><br>
Searches may contain multiple terms, include (basic) globs, and descend down from the current directory. <b class="subheading">Search query: $Q_COPY</b></h2><br>
Searches may contain multiple terms, include (basic) globs,<br>
and descend down from the current directory.
</td> </td>
</tr> </tr>
</table> </table>
</center> </center>
<table width="95%"> <table width="90%" align="center" cellspacing="15">
<tr> <tr>
<td class="navbar"> <td class="search" align="right" valign="center">
</td>
<td class="search" align="right" valign="bottom">
<form action="/search"> <form action="/search">
<input type="hidden" name="p" value="$p"> <input type="hidden" name="p" value="$X_p">
<input class="searchinput" alt="Search:" title="Search" type="text" name="q" placeholder="Search" value="$Q_COPY"> <input class="searchinput" title="Search" alt="Search Box" type="text" name="q" value="$Q_COPY" autocomplete="on" required>
<!-- <button class="searchbutton" type="submit" href="#"><img src="https://slackware.uk/html/search.png" height="15"></button> -->
</form> </form>
</td> </td>
<td width="100%">
<!-- This cell is intentionally empty -->
</td>
<td class="search" align="left" valign="center">
<a href="/html/toggletheme.php" title="Toggle Theme" alt="Toggle Theme"><img src="/html/toggletheme.png" title="Toggle Theme" alt="Toggle Theme"></a>
</td>
</tr> </tr>
</table>
<table class="searchresults" align="center" width="90%">
<tr> <tr>
<td> <td class="searchresults" colspan="3">
<hr> <hr>
<code> <code>
EOF EOF
I=1 I=1
COUNT=0 COUNT=0
while read -r; do while read -r ITEM; do
# Only show 1000 items to prevent long load times. # Only show 1000 items to prevent long load times.
(( I == LIMIT + 1 )) && break (( I == LIMIT + 1 )) && break
# Remove paths we don't want the user to see. # Remove paths we don't want the user to see.
[[ "${REPLY/$STORAGE_PREFIX}" =~ ^/\.sandbox.* ]] && continue while read -r -d " " REGEX; do
[[ "${REPLY/$STORAGE_PREFIX}" =~ ^/\.lftp.* ]] && continue [[ "${ITEM/$STORAGE_PREFIX}" =~ $REGEX ]] && continue 2
[[ "${REPLY/$STORAGE_PREFIX}" =~ ^/dead\.letter ]] && continue done <<<"${IGNORE_REGEXES[@]}"
[[ "${REPLY/$STORAGE_PREFIX}" =~ .*\.rsync-tmp.* ]] && continue
# List the item. # List the item.
echo " &bull; <a href=\"${REPLY/$STORAGE_PREFIX}\" title=\"${REPLY/$STORAGE_PREFIX}\" alt=\"${REPLY/$STORAGE_PREFIX}\">${REPLY/$STORAGE_PREFIX}</a><br>" printf " &bull; <a href=\"%s\" title=\"%s\">%s</a><br>\\n" "${ITEM/$STORAGE_PREFIX}" "${ITEM/$STORAGE_PREFIX}" "${ITEM/$STORAGE_PREFIX}"
(( I++ )) (( I++ ))
(( COUNT++ )) (( COUNT++ ))
done < <(locate -A -d "$LOCATEDB" -i -l "$(( LIMIT * 2 ))" "$STORAGE_PREFIX/${p##/}" $q | sort) done < <(locate -A -d "$LOCATEDB" -i -l "$(( LIMIT * 2 ))" "$STORAGE_PREFIX/${X_p##/}" $X_q | sort)
# If there's no results, tell the user. # Tell the user about the results.
if (( COUNT == 0 )); then if (( COUNT == 0 )); then
echo " No results - try to widen your search." printf " %s" "No results - try to widen your search."
elif (( COUNT == LIMIT )); then elif (( COUNT == LIMIT )); then
echo " Maximum $LIMIT items shown - try to refine your search." printf " %s" "Maximum $LIMIT items shown - try to refine your search."
else else
echo " $COUNT items found." printf " %s" "$COUNT items found."
fi fi
# HTML boilerplate. # HTML boilerplate.
@ -133,4 +166,9 @@ cat <<EOF
EOF EOF
# Include footer. # Include footer.
cat "$FOOTER" if [[ "${COOKIES['theme']}" == "dark" ]]; then
cat "$FOOTER_DARK"
else
cat "$FOOTER_LIGHT"
fi
fi