2008-10-22 01:18:55 -06:00
|
|
|
##############################################
|
|
|
|
# Useful CGI functions
|
2008-09-26 02:35:12 -06:00
|
|
|
|
2008-12-19 22:32:37 -07:00
|
|
|
NEW_LINE='
|
2008-10-13 17:59:10 -06:00
|
|
|
'
|
|
|
|
|
2008-09-26 02:35:12 -06:00
|
|
|
fn dprint { echo $* >[1=2] }
|
|
|
|
|
2008-09-26 06:23:19 -06:00
|
|
|
fn escape_html { sed 's/&/\&/g; s/</\</g; s/>/\>/g' $* }
|
|
|
|
|
2008-09-26 02:35:12 -06:00
|
|
|
fn perm_redirect {
|
|
|
|
echo 'Status: 301 Moved Permanantly
|
|
|
|
Location: '^$1^'
|
|
|
|
|
|
|
|
'
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2009-01-10 20:02:07 -07:00
|
|
|
fn static_file {
|
|
|
|
echo 'Content-Type: '`{select_mime $1}
|
|
|
|
echo
|
|
|
|
cat $1
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Status is () if at least one arg is found.
|
2008-09-26 03:09:18 -06:00
|
|
|
fn get_post_args {
|
2009-01-10 20:02:07 -07:00
|
|
|
_status='Args not found'
|
|
|
|
if(! ~ $REQUEST_METHOD POST)
|
|
|
|
_status='No http post!'
|
|
|
|
if not if(~ $#POST_ARGS 0) {
|
2008-10-17 19:26:54 -06:00
|
|
|
ifs='&
|
|
|
|
' for(pair in `{cat}) {
|
2009-01-10 20:02:07 -07:00
|
|
|
pair=`{echo -n $pair | sed 's/=/\&/'}
|
2008-10-17 19:26:54 -06:00
|
|
|
# Maybe we should urldecode on the first pass?
|
2008-12-19 22:32:37 -07:00
|
|
|
POST_ARGS=( $POST_ARGS $pair )
|
2009-01-10 20:02:07 -07:00
|
|
|
_get_post_args_set_var $pair $*
|
2008-10-17 19:26:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if not {
|
2009-01-10 20:02:07 -07:00
|
|
|
pair=$POST_ARGS
|
|
|
|
while(! ~ $#pair 0) {
|
|
|
|
_get_post_args_set_var $pair $*
|
|
|
|
pair=$pair(3-)
|
2008-10-17 19:26:54 -06:00
|
|
|
}
|
2008-09-26 02:35:12 -06:00
|
|
|
}
|
2009-01-10 20:02:07 -07:00
|
|
|
status=$_status
|
|
|
|
}
|
|
|
|
fn _get_post_args_set_var {
|
|
|
|
if(~ $1 $*(3-)) {
|
|
|
|
ifs=() { $1=`{echo -n $2 | urldecode | tr -d '
'} }
|
|
|
|
_status=()
|
|
|
|
}
|
2008-09-26 02:35:12 -06:00
|
|
|
}
|
|
|
|
|
2009-01-08 18:06:41 -07:00
|
|
|
# This seems slightly improve performance, but might depend on httpd buffering behavior.
|
2008-09-26 02:35:12 -06:00
|
|
|
fn awk_buffer {
|
|
|
|
awk '{
|
|
|
|
buf = buf $0"\n"
|
2009-01-08 18:06:41 -07:00
|
|
|
if(length(buf) > 1400) {
|
2008-09-26 02:35:12 -06:00
|
|
|
printf "%s", buf
|
|
|
|
buf = ""
|
|
|
|
}
|
|
|
|
}
|
2008-12-19 22:32:37 -07:00
|
|
|
END { printf "%s", buf }'
|
2008-09-26 02:35:12 -06:00
|
|
|
}
|
|
|
|
|
2008-09-26 06:23:19 -06:00
|
|
|
fn urldecode {
|
|
|
|
awk '
|
|
|
|
BEGIN {
|
|
|
|
hextab ["0"] = 0; hextab ["8"] = 8;
|
|
|
|
hextab ["1"] = 1; hextab ["9"] = 9;
|
|
|
|
hextab ["2"] = 2; hextab ["A"] = hextab ["a"] = 10
|
|
|
|
hextab ["3"] = 3; hextab ["B"] = hextab ["b"] = 11;
|
|
|
|
hextab ["4"] = 4; hextab ["C"] = hextab ["c"] = 12;
|
|
|
|
hextab ["5"] = 5; hextab ["D"] = hextab ["d"] = 13;
|
|
|
|
hextab ["6"] = 6; hextab ["E"] = hextab ["e"] = 14;
|
|
|
|
hextab ["7"] = 7; hextab ["F"] = hextab ["f"] = 15;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
decoded = ""
|
2008-12-19 22:32:37 -07:00
|
|
|
i = 1
|
2008-09-26 06:23:19 -06:00
|
|
|
len = length ($0)
|
|
|
|
while ( i <= len ) {
|
|
|
|
c = substr ($0, i, 1)
|
|
|
|
if ( c == "%" ) {
|
|
|
|
if ( i+2 <= len ) {
|
|
|
|
c1 = substr ($0, i+1, 1)
|
|
|
|
c2 = substr ($0, i+2, 1)
|
|
|
|
if ( hextab [c1] == "" || hextab [c2] == "" ) {
|
|
|
|
print "WARNING: invalid hex encoding: %" c1 c2 | "cat >&2"
|
|
|
|
} else {
|
|
|
|
code = 0 + hextab [c1] * 16 + hextab [c2] + 0
|
|
|
|
c = sprintf ("%c", code)
|
|
|
|
i = i + 2
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print "WARNING: invalid % encoding: " substr ($0, i, len - i)
|
|
|
|
}
|
|
|
|
} else if ( c == "+" ) {
|
|
|
|
c = " "
|
|
|
|
}
|
|
|
|
decoded = decoded c
|
|
|
|
++i
|
|
|
|
}
|
2008-10-17 19:26:54 -06:00
|
|
|
printf decoded
|
2008-09-26 06:23:19 -06:00
|
|
|
}
|
|
|
|
'
|
2008-09-26 02:35:12 -06:00
|
|
|
}
|
2008-10-13 17:59:10 -06:00
|
|
|
|
2008-11-25 20:50:03 -07:00
|
|
|
fn crop_text {
|
2008-12-19 22:32:37 -07:00
|
|
|
ellipsis='...'
|
2008-12-11 18:25:10 -07:00
|
|
|
if(~ $#* 2)
|
2008-12-19 22:32:37 -07:00
|
|
|
ellipsis=$2
|
2008-12-11 18:25:10 -07:00
|
|
|
|
2009-01-11 18:07:31 -07:00
|
|
|
awk -v max'='^$"1^' ' -v 'ellipsis='$ellipsis '
|
2008-11-25 20:50:03 -07:00
|
|
|
{
|
|
|
|
nc += 1 + length;
|
|
|
|
if(nc > max) {
|
2008-12-11 18:25:10 -07:00
|
|
|
print substr($0, 1, nc - max) ellipsis
|
2008-11-25 20:50:03 -07:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
print
|
|
|
|
}'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-17 19:26:54 -06:00
|
|
|
# Cookies
|
|
|
|
fn set_cookie {
|
|
|
|
# TODO: should check input values more carefully
|
2008-12-19 22:32:37 -07:00
|
|
|
name=$1
|
|
|
|
val=$2
|
|
|
|
extraHttpHeaders=( $extraHttpHeaders 'Set-cookie: '^$"name^'='^$"val^'; path=/;' )
|
2008-10-17 19:26:54 -06:00
|
|
|
}
|
|
|
|
fn get_cookie {
|
|
|
|
ifs=';' { co = `{ echo $HTTP_COOKIE } }
|
|
|
|
|
|
|
|
#for(c in $co)
|
|
|
|
# if(~ $c $1^'='*) # This matching doesn't work
|
|
|
|
# echo $c|sed 's/[^=]*=//'
|
|
|
|
|
|
|
|
# WARNING: we might be adding a trailing new line
|
2008-12-19 22:32:37 -07:00
|
|
|
{ for(c in $co) echo $c } | sed -n 's/[^=]*=//p'
|
2008-10-17 19:26:54 -06:00
|
|
|
}
|
|
|
|
|
2009-01-10 20:02:07 -07:00
|
|
|
fn select_mime {
|
|
|
|
m='text/plain'
|
|
|
|
if(~ $1 *.css)
|
|
|
|
m='text/css'
|
|
|
|
if not if(~ $1 *.ico)
|
|
|
|
m='image/x-icon'
|
|
|
|
if not if(~ $1 *.png)
|
|
|
|
m='image/png'
|
|
|
|
if not if(~ $1 *.jpg *.jpeg)
|
|
|
|
m='image/jpeg'
|
|
|
|
if not if(~ $1 *.gif)
|
|
|
|
m='image/gif'
|
|
|
|
if not if(~ $1 *.pdf)
|
|
|
|
m='application/pdf'
|
|
|
|
echo $m
|
|
|
|
}
|
2008-10-22 01:18:55 -06:00
|
|
|
|
2009-01-08 08:46:00 -07:00
|
|
|
##############################################
|
|
|
|
# Generic rc programming helpers
|
|
|
|
|
|
|
|
fn ll_add {
|
2009-01-08 08:48:09 -07:00
|
|
|
_l=$1^_^$#$1
|
|
|
|
$_l=$*(2-)
|
|
|
|
$1=( $$1 $_l )
|
2009-01-08 08:46:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-22 01:18:55 -06:00
|
|
|
|
|
|
|
##############################################
|
2009-01-11 18:07:31 -07:00
|
|
|
# Werc-specific functions
|
2008-10-22 01:18:55 -06:00
|
|
|
|
2008-12-20 16:08:49 -07:00
|
|
|
fn template { awk -f bin/template.awk $* | rc $rcargs }
|
2008-10-22 01:18:55 -06:00
|
|
|
|
2008-10-13 17:59:10 -06:00
|
|
|
# Auth code
|
|
|
|
|
2008-10-22 01:18:55 -06:00
|
|
|
# Cookie format: WERC_USER: name:timestamp:hash(name.timestamp.password)
|
2008-10-21 08:36:45 -06:00
|
|
|
# login_user can't be used from a template because it sets a cookie
|
2008-10-17 19:26:54 -06:00
|
|
|
fn login_user {
|
2009-01-11 18:07:31 -07:00
|
|
|
# Note: we set the cookie even if it is already there.
|
2008-10-22 01:18:55 -06:00
|
|
|
if(get_user $*)
|
|
|
|
set_cookie werc_user $"logged_user^':0:'^$"logged_password
|
|
|
|
}
|
|
|
|
|
2009-01-11 18:07:31 -07:00
|
|
|
# Check loggin status, if called with group arg we check membership too
|
2008-10-22 01:18:55 -06:00
|
|
|
fn check_user {
|
|
|
|
if(! get_user)
|
|
|
|
status='Not logged in'
|
2009-01-11 18:07:31 -07:00
|
|
|
if not if(~ $#1 1 && ! grep -s '^'^$logged_user^'$' etc/groups/$1)
|
2008-10-22 01:18:55 -06:00
|
|
|
status=User $logged_user not in group $1
|
2008-10-21 08:36:45 -06:00
|
|
|
if not
|
2009-01-11 18:07:31 -07:00
|
|
|
status=()
|
2008-10-17 19:26:54 -06:00
|
|
|
}
|
|
|
|
|
2009-01-11 18:07:31 -07:00
|
|
|
# If not logged in, try to get user login info from POST or from cookie
|
2008-10-22 01:18:55 -06:00
|
|
|
fn get_user {
|
2008-12-19 22:32:37 -07:00
|
|
|
if(~ $#logged_user 0) {
|
|
|
|
if(~ $#* 2) {
|
|
|
|
user_name=$1
|
2009-01-11 18:07:31 -07:00
|
|
|
user_password=$2
|
2008-10-22 01:18:55 -06:00
|
|
|
}
|
|
|
|
if not if(~ $REQUEST_METHOD POST)
|
|
|
|
get_post_args user_name user_password
|
|
|
|
|
|
|
|
if(~ $#user_name 0) {
|
2008-12-19 22:32:37 -07:00
|
|
|
ifs=':' { cu=`{get_cookie werc_user|tr -d $NEW_LINE} }
|
2008-10-22 01:18:55 -06:00
|
|
|
if(! ~ $#cu 0) {
|
2008-12-19 22:32:37 -07:00
|
|
|
user_name=$cu(1)
|
|
|
|
user_password=$cu(3)
|
2008-10-22 01:18:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
auth_user $user_name $user_password
|
|
|
|
}
|
|
|
|
if not
|
2009-01-11 18:07:31 -07:00
|
|
|
status=()
|
2008-10-22 01:18:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# Check if user_name and user_password represent a valid user account
|
|
|
|
# If valid, 'log in' by setting logged_user
|
2008-10-13 17:59:10 -06:00
|
|
|
fn auth_user {
|
2008-12-19 22:32:37 -07:00
|
|
|
user_name=$1
|
|
|
|
user_password=$2
|
2008-10-17 19:26:54 -06:00
|
|
|
|
2008-12-19 22:32:37 -07:00
|
|
|
pfile='etc/users/'^$"user_name^'/password'
|
|
|
|
if(~ $#user_name 0 || ~ $#user_password 0)
|
2008-10-22 01:18:55 -06:00
|
|
|
status='Auth: missing user name or pass: '^$"user_name^' / '^$"user_password
|
2008-10-21 08:36:45 -06:00
|
|
|
if not if(! test -f $pfile)
|
|
|
|
status='Auth: cant find '^$pfile
|
2008-12-19 22:32:37 -07:00
|
|
|
if not if(! ~ $user_password `{cat $pfile})
|
2008-10-22 01:18:55 -06:00
|
|
|
status='Auth: Pass '$user_password' doesnt match '^`{cat $pfile}
|
|
|
|
if not {
|
2008-12-19 22:32:37 -07:00
|
|
|
logged_user=$user_name
|
|
|
|
logged_password=$user_password
|
2008-10-17 19:26:54 -06:00
|
|
|
dprint Auth: success
|
2009-01-11 18:07:31 -07:00
|
|
|
status=()
|
2008-10-22 01:18:55 -06:00
|
|
|
}
|
2008-10-17 19:26:54 -06:00
|
|
|
}
|
|
|
|
|
2009-01-11 18:07:31 -07:00
|
|
|
# .md '(meta-)data' extract
|
2009-01-08 18:03:36 -07:00
|
|
|
fn get_md_file_attr {
|
|
|
|
sed -n '/^\* '$2': /p; /^\* '$2': /q; /^$/q' < $1
|
2008-12-24 04:18:44 -07:00
|
|
|
}
|
|
|
|
|
2008-10-13 22:59:19 -06:00
|
|
|
#app_blog_methods = ( _post index.rss )
|
|
|
|
#fn app_blog__post {
|
|
|
|
# echo
|
|
|
|
#}
|
|
|
|
#
|
|
|
|
#app_blog___default {
|
|
|
|
# if (~ $blog)
|
|
|
|
# call_app blogpost
|
|
|
|
#}
|
|
|
|
#
|
|
|
|
## --
|
|
|
|
#app_blogpost_methods = ( comment _edit )
|
|
|
|
#
|
|
|
|
#fn app_blogpost_comment {
|
|
|
|
# call_app comments
|
|
|
|
#}
|
|
|
|
#
|
|
|
|
## --
|
|
|
|
#app_comments_methods = ( _post _edit )
|
|
|
|
#
|
|
|
|
#fn app_comments___default {
|
|
|
|
#
|
|
|
|
#}
|