#----------------------------------------------------------------------------
# /www/srv/admin/cgi-helper
#
# Creation:     2005-08-12 hh
# Last Update:  $Id$
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
# do some useful things
#----------------------------------------------------------------------------
# set_debug=yes
# parse variables
cgi_helper="yes"
eval "`proccgi $*`"
: ${set_debug:=$FORM_debug}
# Write debug info to debug.log
case $set_debug in
    yes)
        exec 2>/srv/www/admin/debug.log
        set -x
    ;;
    *)
        exec 2>/dev/null
    ;;
esac
. /var/run/mini_httpd.conf
myname=`basename $0`
: ${cginame:=`basename $0 .cgi`}
: ${lang:="$HTTPD_GUI_LANG"}
: ${showmenu:=$FORM_showmenu}
# get language files
if [ -f /srv/www/lang/main.$lang ]
then
    . /srv/www/lang/main.$lang
else
    . /srv/www/lang/main.en
fi
if [ -f /srv/www/lang/$cginame.$lang ]
then
    . /srv/www/lang/$cginame.$lang
else
    [ -f /srv/www/lang/$cginame.en ] && . /srv/www/lang/$cginame.en
fi
#----------------------------------------------------------------------------
# Security function
#----------------------------------------------------------------------------
user_has_rights ()
{ # returns true if the user has access
    [ -z "$1" ] && return 0
    if grep ^$REMOTE_USER= /etc/httpd/rights | cut -d= -f2 | \
       grep -vq -e "^all$" \
                -e "\( \|^\)$1:all\( \|$\)" \
                -e "\( \|^\)$1[^ ]*[:,]$2\( \|,\|$\)"
    then
        return 1
    else
        return 0
    fi
}
check_rights ()
{
    : ${SEC_REALM:=$1}
    : ${SEC_ACTION:=$2}
    if [ -z "$SEC_REALM" -o -z "$SEC_ACTION" ]
    then
        show_html_header "$_MN_accdenied"
        show_error "$_MN_seclib" "$_MN_nopar"
        show_html_footer
        # Exit the CGI-Script
        exit 1
    fi
    case $REMOTE_USER in
        "") ;;
        *)
            if ! user_has_rights $SEC_REALM $SEC_ACTION
            then
                show_html_header "$_MN_accdenied"
                show_error "$_MN_accdenied" "$_MN_noright
 \
                                            $_MN_user: \"$REMOTE_USER\"
 \
                                            $_MN_realm: \"$SEC_REALM\"
 \
                                            $_MN_secaction: \"$SEC_ACTION\""
                show_html_footer
                # Exit the CGI-Script
                exit 1
            fi
          ;;
      esac
}
#----------------------------------------------------------------------------
# some useful functions
#----------------------------------------------------------------------------
reload ()
{  # Reload site
    echo "Location: $myname"
    echo
}
#
# Output HTTP Header
#
# Echoes additional HTTP headers. Should at least contain Content-Type.
#
# According to RFC 2616 each header line must be terminated with CR LF,
# which is \r\n. Shell adds \n but not \r so we must do this for each
# header line!
#
# $1    output type (case)
# $2    additional option string to be evaluated
#
http_header ()
{
    # need quoted string expansion, \r is not interpreted as is in heredoc
    local CR=$'\r'
    case "$1" in
        ""|html)
            cat <<-EOF
Content-Type: text/html; charset=utf-8$CR
Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate, no-store$CR
Pragma: no-cache$CR
Expires: $(date -Ru)$CR
EOF
            ;;
        htmlstatic)
            cat <<-EOF
Content-Type: text/html; charset=utf-8$CR
Cache-control: max-age=3600$CR
EOF
            ;;
        download)
            eval $2
            : ${ctype:=application/download}
            : ${filename:=file.txt}
            cat <<-EOF
Content-Type: ${ctype}$CR
Content-Disposition: attachment; filename=${filename}$CR
EOF
            ;;
    esac
    # headers are finished with an empty line ending with CR LF
    echo "$CR"
}
create_menu ()
{
    # read the menu file and create a menu
    menucache="/tmp/menu_${REMOTE_USER}`echo $menufile | sed 's#/#_#g'`"
    if ! [ -f "$menucache" ]
    then
        sec_close=
        cursec_print=
        {
            while read type prio link desc right
            do
            # Include the cgi-specific Language-File if available
        cginame=`echo "$link" | sed 's/.*\///;s/\.cgi$//'`
        if [ "$cginame" != '-' ]; then
            if [ -f /srv/www/lang/$cginame.$lang ]
            then
            . /srv/www/lang/$cginame.$lang
            else
            [ -f /srv/www/lang/$cginame.en ] && . /srv/www/lang/$cginame.en
            fi
        fi
        eval desc=\"$desc\"
        case "$type" in
            t)
            case "$desc" in
                Opt) cursec=$_MT_opt ;;
                *)   cursec=$desc ;;
            esac
            cursec_print=yes
            ;;
            e)
            if user_has_rights "$right" "view"
            then
                if [ -n "$cursec_print" ]
                then
                echo "$sec_close"'
"
            cat /srv/www/admin/debug.log | htmlspecialchars
            echo ""
        ;;
    esac
    # restore stdout
    exec 1>&3
    # do the output
    http_header
    # with the redirection we are able to do a little cleaning
    cat /srv/www/include/header.inc /tmp/content_$$.html /srv/www/include/footer.inc |
        substitute_markers meta css title topnav subtitle date version menu comment_start comment_end flihost fli_arch $marker |
        xhtml_cleaning
    rm -f /tmp/content_$$.html
}
substitute_markers ()
{
    sedstring=
    for _marker in $*
    do
        # we have to escape double quotes, ampersand, dollar
        substitute=`eval echo '$'${_marker} | sed 's/"/\\\\\"/g; s/&/\\\&/g; s/\\\$/\\\\\$/g'`
        if [ $_marker = "menu" ]
        then
            substitute2=${substitute}
        else
            substitute2=${substitute//%/\\%}
        fi
        sedstring="${sedstring}s%###${_marker//%/\\%}###%${substitute2}%g; "
    done
    eval sed \"$sedstring\"
}
translate_label ()
{
   local label=`echo "${1}" | sed -e 's/ /\ /g'`
   if [ -z "${label}" ]
      then
          label="missing translation"
      fi
   echo "${label}"
}
xhtml_cleaning ()
{ # do a very basic xhtm cleaning, remove unused markers
    # todo strtolower of tags
    #img,br,hr,meta,link,base,area,input,param,col
    # HTTPD_GUI_XHTML_CLEANING is set on boottime by rc430.httpd
    eval sed $HTTPD_GUI_XHTML_CLEANING -e 's/\(###\)\([^#]*\)\(###\)//g'
}
# Cleans up standard input such that it can be safely used in eval'd context.
# Only letters, digits, dots and underscores are kept.
# Input:
#   $0[$$]: $1" else echo "$0[$$]: $1" fi fi } cgi_log_errors() { if [ -s "$2" ]; then cgi_log "Errors while rendering '$1'" while read line; do cgi_log "$line" done < $2 fi }