initial
This commit is contained in:
commit
b30538c3c3
|
@ -0,0 +1,47 @@
|
|||
# CGI-Log
|
||||
CGI-Log is a dirt simple CGI-Bin based pile of scripts for automatically displaying a list of posts on a website, useful for small personal sites. It is not smart, a CMS, pretty, or best practice. But writing it was quicker than downloading something off the shelf.
|
||||
|
||||
There's not much code here, anything visual is old school HTML. If you want to use CGI files in the modern age I will assume you are crazy enough to know about the ramifications. Unix utilities like cat, echo, ls, head, and tail are used to build the HTML pages returned to the client.
|
||||
|
||||
Add a page by creating a file for it in the /log/posts folder.
|
||||
|
||||
## /
|
||||
- **index.cgi**
|
||||
Homepage for your index. It calls the other files in /cgilog.
|
||||
|
||||
- **home-top.htm**
|
||||
Opening HTML, HEAD, TITLE, BODY, and UL tags. Calls CSS and JS files.
|
||||
|
||||
- **list.cgi**
|
||||
Each file in /cgilog/log/posts is linked in a LI tag.
|
||||
|
||||
- **home-bottom.htm**
|
||||
Closing UL, BODY, and HTML tags.
|
||||
|
||||
- **style.css**
|
||||
Example CSS.
|
||||
|
||||
- **script.js**
|
||||
Example JAVASCRIPT.
|
||||
|
||||
## /log
|
||||
- **index.cgi**
|
||||
This is the page that loads posts via url query, for example web.com/log/?i=leaf. The most complex code is in this file, for dealing with queries in URL's (which I did not write). It calls the other files in /log.
|
||||
|
||||
- **header-top.htm**
|
||||
Opening HTML, and HEAD tags.
|
||||
|
||||
- **header-bottom.htm**
|
||||
Closing HEAD tag, opening BODY tag. Calls .css and .js files common to /cgilog (directory above).
|
||||
|
||||
- **footer.htm**
|
||||
Closing BODY and HTML tags.
|
||||
|
||||
## /log/posts
|
||||
These are where your posts go. Files are listed on the index by modified date. If you wish to reorder files, you may use the 'touch' unix command. Two files are provided as post examples.
|
||||
|
||||
The first line is interpretted as the title of the page, displayed in the TITLE tag and on the index. The rest of the file will be interpretted as HTML. The actual filename for the post is used as the query url.
|
||||
|
||||
|
||||
## Note
|
||||
You may need to alter the first line of each .cgi file to match the location of your shell of choice on your system. It is defaulted to #!/bin/bash. Also make sure your .cgi files are executable, for example 'chmod +x index.cgi'.
|
|
@ -0,0 +1,3 @@
|
|||
</ul>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,17 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>cgilog website</title>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="/style.css">
|
||||
<script src="/script.js" defer></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>cgilog website</h1>
|
||||
<p>
|
||||
Welcome to my internet website online.
|
||||
</p>
|
||||
<h3>log</h3>
|
||||
<ul>
|
|
@ -0,0 +1,5 @@
|
|||
#!/bin/bash
|
||||
echo -e "Content-type: text/html\n"
|
||||
cat home-top.htm
|
||||
./list.cgi
|
||||
cat home-bottom.htm
|
|
@ -0,0 +1,6 @@
|
|||
#!/bin/bash
|
||||
posts=`ls -1At log/posts`
|
||||
|
||||
while read -r line; do
|
||||
echo " <li><a href='log/?i=$line'>`head -1 log/posts/$line`</a></li>"
|
||||
done <<< "$posts"
|
|
@ -0,0 +1,2 @@
|
|||
</body>
|
||||
</html>
|
|
@ -0,0 +1,7 @@
|
|||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="/style.css">
|
||||
<script src="/script.js" defer></script>
|
||||
</head>
|
||||
<body class="post">
|
|
@ -0,0 +1,3 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
|
@ -0,0 +1,110 @@
|
|||
#!/bin/bash
|
||||
# (internal) routine to store POST data
|
||||
function cgi_get_POST_vars()
|
||||
{
|
||||
# check content type
|
||||
# FIXME: not sure if we could handle uploads with this..
|
||||
[ "${CONTENT_TYPE}" != "application/x-www-form-urlencoded" ] && \
|
||||
echo "bash.cgi warning: you should probably use MIME type "\
|
||||
"application/x-www-form-urlencoded!" 1>&2
|
||||
# save POST variables (only first time this is called)
|
||||
[ -z "$QUERY_STRING_POST" \
|
||||
-a "$REQUEST_METHOD" = "POST" -a ! -z "$CONTENT_LENGTH" ] && \
|
||||
read -n $CONTENT_LENGTH QUERY_STRING_POST
|
||||
# prevent shell execution
|
||||
local t
|
||||
t=${QUERY_STRING_POST//%60//} # %60 = `
|
||||
t=${t//\`//}
|
||||
t=${t//\$(//}
|
||||
t=${t//%24%28//} # %24 = $, %28 = (
|
||||
QUERY_STRING_POST=${t}
|
||||
return
|
||||
}
|
||||
|
||||
# (internal) routine to decode urlencoded strings
|
||||
function cgi_decodevar()
|
||||
{
|
||||
[ $# -ne 1 ] && return
|
||||
local v t h
|
||||
# replace all + with whitespace and append %%
|
||||
t="${1//+/ }%%"
|
||||
while [ ${#t} -gt 0 -a "${t}" != "%" ]; do
|
||||
v="${v}${t%%\%*}" # digest up to the first %
|
||||
t="${t#*%}" # remove digested part
|
||||
# decode if there is anything to decode and if not at end of string
|
||||
if [ ${#t} -gt 0 -a "${t}" != "%" ]; then
|
||||
h=${t:0:2} # save first two chars
|
||||
t="${t:2}" # remove these
|
||||
v="${v}"`echo -e \\\\x${h}` # convert hex to special char
|
||||
fi
|
||||
done
|
||||
# return decoded string
|
||||
echo "${v}"
|
||||
return
|
||||
}
|
||||
|
||||
# routine to get variables from http requests
|
||||
# usage: cgi_getvars method varname1 [.. varnameN]
|
||||
# method is either GET or POST or BOTH
|
||||
# the magic varible name ALL gets everything
|
||||
function cgi_getvars()
|
||||
{
|
||||
[ $# -lt 2 ] && return
|
||||
local q p k v s
|
||||
# prevent shell execution
|
||||
t=${QUERY_STRING//%60//} # %60 = `
|
||||
t=${t//\`//}
|
||||
t=${t//\$(//}
|
||||
t=${t//%24%28//} # %24 = $, %28 = (
|
||||
QUERY_STRING=${t}
|
||||
# get query
|
||||
case $1 in
|
||||
GET)
|
||||
[ ! -z "${QUERY_STRING}" ] && q="${QUERY_STRING}&"
|
||||
;;
|
||||
POST)
|
||||
cgi_get_POST_vars
|
||||
[ ! -z "${QUERY_STRING_POST}" ] && q="${QUERY_STRING_POST}&"
|
||||
;;
|
||||
BOTH)
|
||||
[ ! -z "${QUERY_STRING}" ] && q="${QUERY_STRING}&"
|
||||
cgi_get_POST_vars
|
||||
[ ! -z "${QUERY_STRING_POST}" ] && q="${q}${QUERY_STRING_POST}&"
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
s=" $* "
|
||||
# parse the query data
|
||||
while [ ! -z "$q" ]; do
|
||||
p="${q%%&*}" # get first part of query string
|
||||
k="${p%%=*}" # get the key (variable name) from it
|
||||
v="${p#*=}" # get the value from it
|
||||
q="${q#$p&*}" # strip first part from query string
|
||||
# decode and evaluate var if requested
|
||||
[ "$1" = "ALL" -o "${s/ $k /}" != "$s" ] && \
|
||||
eval "$k=\"`cgi_decodevar \"$v\"`\""
|
||||
done
|
||||
return
|
||||
}
|
||||
|
||||
# register all GET and POST variables
|
||||
cgi_getvars BOTH ALL
|
||||
|
||||
## cgilog: build page (everything above is boilerplate cgi stuff borrowed from the web)
|
||||
# if user has loaded /log without any query
|
||||
if [ -z "$i" ]
|
||||
then
|
||||
echo "sup girl?"
|
||||
|
||||
# if user had loaded a page with query /log?i=postname
|
||||
else
|
||||
title=`head -1 posts/$i`
|
||||
display=`tail -n +2 posts/$i`
|
||||
|
||||
echo -e "Content-type: text/html\n"
|
||||
cat header-top.htm
|
||||
echo " <title>$title</title>"
|
||||
cat header-bottom.htm
|
||||
echo "$display"
|
||||
cat footer.htm
|
||||
fi
|
|
@ -0,0 +1,5 @@
|
|||
Greetings on Earth
|
||||
<h1>time</h1>
|
||||
<p>is passing quickly</p>
|
||||
<hr>
|
||||
<p><a href="/">home</a></p>
|
|
@ -0,0 +1,5 @@
|
|||
Look Behind You
|
||||
<h1>It's gone now</h1>
|
||||
<p>There was something there a second ago.</p>
|
||||
<hr>
|
||||
<p><a href="/">home</a></p>
|
Loading…
Reference in New Issue