#!/home/web/bin/python2.2 import cgi import cPickle import os import string import sybase import sys import tempfile import traceback def ReadForm(form): Dict = {} for key in form.keys(): Dict[key] = form[key].value return Dict def InitDb(): db = sybase.new("www", "0tw@4eg", "ESOECF") db.sql("use eis") db.sql("set textsize 2000000") return db def IsLegalString(strg): legalChars = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "-", ","] for a in strg: if a not in legalChars: return 0 return 1 def RetrieveSummary(db, Dict): legalDict = {"period": 1, "type": 1, "survey_id": 1, "eis_programs_run_id": 1, "instrument_id": 1, "obsnight_id": 1, "eis_programs_id": 1} varcharKeys = ["period", "type"] clause = "" for key in Dict.keys(): # Raise an exception and abort if an # illegal key is supplied test = legalDict[key] # Make sure that nobody sends control # chars value = str(Dict[key]) if not IsLegalString: raise "Illegal Chars" if len(clause) == 0: if key in varcharKeys: clause = key+"='"+value+"'" else: clause = key+"="+value else: if key in varcharKeys: clause = clause + " and "+key+"='"+value+"'" else: clause = clause + " and "+key+"="+value query = string.join(['select summary', 'from eis_guest.guest_summary', 'where', clause, 'and '+accessClause]) # As a last check make sure that this really is a # select query and nobody managed to overwrite the # query string if query[:6] != "select": raise "Illegal query" result = db.sql(query) if len(result) > 0: summary = result[0][0][0] else: summary = None return summary # Stolen EISUnpickle from EISUtilLibrary def EISUnpickle(pickledValue): unpickleCorrect = 0 ## first try as if we got this from Sybase try: pickledValueNew = string.replace(pickledValue, "\\012", "\n") pickledValueNew = string.replace(pickledValueNew, '@single_quote', "'") pickledValueNew = string.replace(pickledValueNew, '@double_quote', '"') value = cPickle.loads(pickledValueNew) unpickleCorrect = 1 except ValueError: unpickleCorrect = 0 if unpickleCorrect: return value ## if this didn't produce a correct result, try it as if we did NOT get this from Sybase try: pickledValueNew = string.replace(pickledValue, '@single_quote', "'") pickledValueNew = string.replace(pickledValueNew, '@double_quote', '"') value = cPickle.loads(pickledValueNew) unpickleCorrect = 1 except ValueError: unpickleCorrect = 0 if unpickleCorrect: return value ## the object could not be unpickled return "UNPICKLE FAILED" if __name__ == "__main__": accessClause = "world=1" commandPre = "env LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:../extprogs/ ../extprogs/" sys.stderr = sys.stdout xmlDict = {} xmlDict["det-night"] = "../xsl/details_night_summ_int.xslt" xmlDict["night-stat"] = "../xsl/night_stat_summ_int.xslt" xmlDict["det-run"] = "../xsl/details_run_summ_int.xslt" xmlDict["run-stat"] = "../xsl/run_stat_summ_int.xslt" xmlDict["night"] = "../xsl/night_summ_int.xslt" xmlDict["run"] = "../xsl/run_summ_int_new.xslt" xmlDict["period"] = "../xsl/period_summ_int.xslt" xmlDict["det-period"] = "../xsl/period_summ_int.xslt" xmlDict["per-stat"] = "../xsl/run_stat_summ_int.xslt" xmlDict["survey"] = "../xsl/period_summ_int.xslt" xmlDict["det-survey"] = "../xsl/period_summ_int.xslt" xmlDict["sur-stat"] = "../xsl/run_stat_summ_int.xslt" xmlDict["eis-stat"] = "../xsl/EIS_summ_int.xslt" print "Content-type: text/html" print try: form = cgi.FieldStorage() Dict = ReadForm(form) if not Dict.has_key("type"): print "

Requiered key \"type\" missing

" sys.exit(1) db = InitDb() summaryPickle = RetrieveSummary(db, Dict) del db if not summaryPickle: print "

No data for query!

" else: summary = EISUnpickle(summaryPickle) if summary == "UNPICKLE FAILED": print "

"+summary+"

" sys.exit(1) infile=tempfile.mktemp() outfile=tempfile.mktemp() open(infile,"w").write(summary) command = commandPre+"xsltproc "+xmlDict[Dict["type"]]+" "+infile+"> "+outfile os.system(command) os.remove(infile) f = open(outfile, "r") print f.read() f.close() os.remove(outfile) except: print "\n\n
"
	traceback.print_exc()
        print "
"