index.cgi.in 3.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#!{{ extra_eggs_interpreter }}

import cgi
import cgitb
import Cookie
import jinja2
import json
import os
import subprocess
import sys
import urllib

cgitb.enable(display=0, logdir="/tmp/cgi.log")

form = cgi.FieldStorage()
cookie = Cookie.SimpleCookie()

cgi_path = "{{ cgi_directory }}"


def forward_form():
  command = os.path.join(cgi_path, form['posting-script'].value)
  params_dict = {}
  for f in form:
    params_dict[f] = form[f].value
  del params_dict['posting-script']
  os.environ['QUERY_STRING'] = urllib.urlencode(params_dict)
  try:
    if os.access(command, os.X_OK):
      print '\n', subprocess.check_output([command])
  except subprocess.CalledProcessError:
    print "There is a problem with sub-process"
    pass


def return_document():
  command = os.path.join(cgi_path, form['script'].value)
  #XXX this functions should be called only for display,
  #so a priori it doesn't need form data
  os.environ['QUERY_STRING'] = ''
  try:
    if os.access(command, os.X_OK):
      print '\n', subprocess.check_output([command])
    elif os.access(command, os.R_OK):
      print open(command).read()
    else:
      raise OSError
  except (subprocess.CalledProcessError, OSError):
    print "<p>File cannot be found</p>"


def make_menu():
  # Transform deep-2 tree in json
54
  folder_list = {}
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  for folder in os.listdir(cgi_path):
    if os.path.isdir(os.path.join(cgi_path, folder)):
      folder_list[folder] = []
  for folder in folder_list:
    for file in os.listdir(os.path.join(cgi_path, folder)):
      if os.path.isfile(os.path.join(cgi_path, folder, file)):
        folder_list[folder].append(file)
  return folder_list


# Beginning of response
print "Content-Type: text/html"

# Check if user is logged
if "password" in form:
  password = form['password'].value
  if password == '{{ password }}' :
    cookie['password'] = password
73
    print cookie, "; Path=/; HttpOnly"
74 75 76 77 78 79 80 81 82 83 84
else:
  cookie_string = os.environ.get('HTTP_COOKIE')
  if cookie_string:
    cookie.load(cookie_string)
    try:
      password = cookie['password'].value
    except KeyError:
      password = None
  else:
    password = None

85 86
print '\n'

87
if not password or password != '{{ password }}':
88 89 90 91 92
  print "<html><head>"
  print """
    <link rel="stylesheet" href="pure-min.css">
    <link rel="stylesheet" href="/style.css">"""
  print "</head><body>"
93 94 95 96 97 98
  if password is None:
    print "<h1>This is the monitoring interface</h1>"
  else:
    print "<h1>Error</h1><p>Wrong password</p>"
  print """
  <p>Please enter the monitor_password in the next field to access the data</p>
99
  <form action="/index.cgi" method="post" class="pure-form-aligned">
100
    Password : <input type="password" name="password">
101
    <button type="submit" class="pure-button pure-button-primary">Access</button>
102 103 104 105 106 107 108 109 110 111 112 113 114
  </form>
  </body></html>"""

# redirection to the required script/page
else:
  print
  if "posting-script" in form:
    forward_form()
  elif "script" in form:
    return_document()
  else:
    html_base = jinja2.Template(open('{{ index_template }}').read())
    print
115
    print html_base.render(tree=make_menu(), default_page="{{ default_page }}")