[blis-cvs] blis/cgi-bin blis.cgi,1.60,1.61
Status: Beta
Brought to you by:
cstroie
From: Costin S. <cs...@us...> - 2007-02-23 17:02:56
|
Update of /cvsroot/blis/blis/cgi-bin In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv3308 Modified Files: blis.cgi Log Message: Simplified the user's parameter configuration. Index: blis.cgi =================================================================== RCS file: /cvsroot/blis/blis/cgi-bin/blis.cgi,v retrieving revision 1.60 retrieving revision 1.61 diff -u -d -r1.60 -r1.61 --- blis.cgi 23 Feb 2007 12:32:21 -0000 1.60 +++ blis.cgi 23 Feb 2007 17:02:51 -0000 1.61 @@ -1,7 +1,7 @@ #!/bin/gawk -f # ############################################################################ -# Copyright (C) 2006 by Costin Stroie # +# Copyright (C) 2006-2007 by Costin Stroie # # cs...@us... # # # # This program is free software; you can redistribute it and/or modify # @@ -25,29 +25,11 @@ BEGIN { # Parameters - config["server_name"] = ENVIRON["HTTP_HOST"] != "" ? ENVIRON["HTTP_HOST"] : "blis.sourceforge.net" - config["root"] = "/home/www/blis" - config["url"] = "/blis" - config["pages_dir"] = "pages" - config["themes_dir"] = "themes" - config["images_dir"] = "images" - - config["theme"] = "relaxation" - config["charset"] = "UTF-8" - - # Default globals - config["page"] = "Home" - config["mode"] = "view" - config["camelcase"] = "y" - scriptname = ENVIRON["SCRIPT_NAME"] != "" ? ENVIRON["SCRIPT_NAME"] : "/cgi-bin/blis.cgi" - - # Software related parameters - software["name"] = "Blis" - software["version"] = "0.1" - software["author"] = "Costin Stroie" - software["email"] = "cs...@us..." + site["server_name"] = "blis.sourceforge.net" + site["base_dir"] = "/var/www/blis" + site["base_url"] = "/blis" - # Basic initialization + # Initialization init() # Start processing by decoding the cgi query string @@ -67,6 +49,9 @@ # If using the PATH_INFO, the page is specified as path if (ENVIRON["PATH_INFO"]) page = page_to_file(decode_url(ENVIRON["PATH_INFO"])) + # Go to the default page if null + if (!page) + page = page_to_file(config["page"]) # Decode the cookies, if any decode_cookie(cookie) @@ -88,8 +73,8 @@ # Initialization # -# Perform some initializing routines -function init() +# Perform some initializing routines and configuration +function init( i) { # Create the dec2hex[] array split("0 1 2 3 4 5 6 7 8 9 a b c d e f", dec2hex, " ") @@ -97,6 +82,38 @@ # Create the hex2dec[] array for (i=0; i<16; i++) hex2dec[dec2hex[i+1]] = i + + # Parameters + config["server_name"] = config_env("HTTP_HOST", site["server_name"]) + config["site"] = config_env("BLIS_SITE", "blis") + config["base_dir"] = config_env("BLIS_BASE_DIR", site["base_dir"]) + config["base_url"] = config_env("BLIS_BASE_URL", site["base_url"]) + delete site + + # Pages, images and themes + config["pages"] = "pages" + config["themes"] = "themes" + config["images"] = "images" + config["pages_dir"] = config["base_dir"] "/" config["pages"] + config["themes_dir"] = config["base_dir"] "/" config["themes"] + config["images_dir"] = config["base_dir"] "/" config["images"] + config["pages_url"] = config["base_url"] "/" config["pages"] + config["themes_url"] = config["base_url"] "/" config["themes"] + config["images_url"] = config["base_url"] "/" config["images"] + + # Some safe defaults + config["theme"] = "relaxation" + config["charset"] = "UTF-8" + config["page"] = "Home" + config["mode"] = "view" + config["camelcase"] = "n" + scriptname = config_env("SCRIPT_NAME", "/cgi-bin/blis.cgi") + + # Software related parameters + software["name"] = "Blis" + software["version"] = "0.1" + software["author"] = "Costin Stroie" + software["email"] = "cs...@us..." } ############################################################################ @@ -104,6 +121,22 @@ # General conversion functions # +# Configure from environment or fallback to default +function config_env(var, def, result) +{ + if (var in ENVIRON) + { + if (ENVIRON[var]) + result = ENVIRON[var] + else + result = def + } + else + result = def + + return result +} + # Trim leading and trailing whitespace function trim(str) { @@ -116,7 +149,7 @@ function decode_query(query, q, f, i, n) { # Make sure the cgi query string really exists - if (ENVIRON["QUERY_STRING"]) + if ("QUERY_STRING" in ENVIRON) { # Split the string using the '&' character as delimiter n = split(ENVIRON["QUERY_STRING"], q, "&") @@ -214,7 +247,7 @@ function get_filename(page, filename, line, ok) { # Construct the path to the file - filename = config["root"] "/" config["pages_dir"] "/" page + filename = config["pages_dir"] "/" page # Just a basic protection against the '..' attacks gsub(/\.\.+/, "_", filename) # Test the file is readable @@ -257,7 +290,7 @@ # First check the query parameters if ("theme" in query) { - thfile = config["root"] "/" config["themes_dir"] "/" query["theme"] "/" query["theme"] ".html" + thfile = config["themes_dir"] "/" query["theme"] "/" query["theme"] ".html" if (getline line < thfile > 0) theme = query["theme"] close (thfile) @@ -266,7 +299,7 @@ # If the theme has not been specified in query, check the cookie if (!theme && ("blis_theme" in cookie)) { - thfile = config["root"] "/" config["themes_dir"] "/" cookie["blis_theme"] "/" cookie["blis_theme"] ".html" + thfile = config["themes_dir"] "/" cookie["blis_theme"] "/" cookie["blis_theme"] ".html" if (getline line < thfile > 0) theme = cookie["blis_theme"] close (thfile) @@ -275,7 +308,7 @@ # If neither the query or the cookie have provided the theme, use the default config if (!theme && ("theme" in config)) { - thfile = config["root"] "/" config["themes_dir"] "/" config["theme"] "/" config["theme"] ".html" + thfile = config["themes_dir"] "/" config["theme"] "/" config["theme"] ".html" if (getline line < thfile > 0) theme = config["theme"] close (thfile) @@ -480,8 +513,8 @@ function apply_template(theme, parsed_content, metadata, thfile, thdir, line, result) { # Find the relative theme directory and theme file - thdir = config["themes_dir"] "/" theme - thfile = config["root"] "/" thdir "/" theme ".html" + thdir = config["themes"] "/" theme + thfile = config["themes_dir"] "/" theme "/" theme ".html" # Read the template file and replace the placeholders, if any while(getline line < thfile > 0) @@ -492,7 +525,7 @@ # Provide the theme directory if (index(line, "%THEME_DIR%")) - gsub(/%THEME_DIR%/, config["url"] "/" thdir, line) + gsub(/%THEME_DIR%/, config["base_url"] "/" thdir, line) # Some placeholders in head if (index(line, "%PAGE_TITLE%")) @@ -570,7 +603,7 @@ # The favicon html_head = html_head - sprintf("<link rel=\"shortcut icon\" type=\"image/ico\" href=\"%s/blis.ico\">", config["url"]) + sprintf("<link rel=\"shortcut icon\" type=\"image/ico\" href=\"%s/blis.ico\">", config["base_url"]) return html_head } @@ -993,7 +1026,7 @@ title = mch[1] # The matched segment replaced by the image tag - result = result sprintf("<img src=\"%s\" alt=\"%s\"/>", config["url"] "/" config["images_dir"] "/" mch[1], title) + result = result sprintf("<img src=\"%s\" alt=\"%s\"/>", config["images_url"] "/" mch[1], title) # The last segment: the string after the match line = substr(line, mch[0, "start"] + mch[0, "length"]) |