#!/usr/bin/env python #-*- encoding: utf-8 -*- # # index.cgiscript # My index page. # # Copyright (c) 2008 Pierre "delroth" Bourdon # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import cgi, cgitb, os, math, time, sys, pwd, grp from stat import * from glob import glob cgitb.enable() class File(object): def __init__(self, name, stat, orderby='name'): self.name = name self.own = pwd.getpwuid(stat[ST_UID])[0] self.grp = grp.getgrgid(stat[ST_GID])[0] self.size = stat[ST_SIZE] self.atime = stat[ST_ATIME] self.mtime = stat[ST_MTIME] if orderby not in dir(self) or callable(getattr(self, orderby)): orderby = 'name' self.orderby = orderby def __cmp__(self, other): fi = ['name', 'size', 'atime', 'mtime', 'own', 'grp'] fi.remove(self.orderby) fi.insert(0, self.orderby) li1, li2 = [[getattr(s, f) for f in fi] for s in [self, other]] if li1 == li2: return 0 elif li1 < li2: return -1 else: return 1 def format_size(size): names = ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'] if size == 0: return '0B' else: i = int(math.log(size, 1024)) return cgi.escape( str(round(float(size) / (1024 ** i), 2)) + names[i], True) form = cgi.FieldStorage() show_source = form.getvalue('source', None) sort_field = form.getvalue('sort', 'name') sort_order = form.getvalue('order', '0') regexp = form.getvalue('r', '*') try: sort_order = int(sort_order) except ValueError: sort_order = 0 if show_source == '1': print "HTTP/1.1 200 OK" print "Content-Type: text/html" print print '
'
    print cgi.escape(open(__file__, 'r').read())
    print '
' sys.exit(0) p = os.environ['DOCUMENT_ROOT'][:-1] p += '/'.join(os.environ['REQUEST_URI'].split('?')[0].split('/')[:-1]) os.chdir(p) files = [(f, os.lstat(f)) for f in glob(regexp)] dirs = [f for (f,s) in files if S_ISDIR(s[ST_MODE])] files = [File(f, s, sort_field) for (f,s) in files if not S_ISDIR(s[ST_MODE])] dirs.sort() files.sort(reverse=bool(sort_order)) if len(files) == 1 and len(dirs) == 0: print 'HTTP/1.1 302 REDIRECT' print 'Location: %s' % files[0].name.replace('\n', '') print sys.exit(0) print '''HTTP/1.1 200 OK Content-Type: text/html Files hosted on delroth.is-a-geek.org

Files hosted on delroth.is-a-geek.org

''' header = [('n', 'Name', 'name'), ('s', 'Size', 'size'), ('m', 'Last Modified', 'mtime'), ('a', 'Last Accessed', 'atime'), ('u', "Owner", 'own'), ('g', "Group", 'grp')] for (c, n, p) in header: print '' print '' template = '''''' for d in dirs: s = '%s' % (cgi.escape(d, True), cgi.escape(d)) print template % (s, '', '-', '-', '-', '-') for f in files: s = '%s' % (cgi.escape(f.name, True), cgi.escape(f.name)) print template % (s, format_size(f.size), time.ctime(f.mtime), time.ctime(f.atime), f.own, f.grp) print '''
' % c print '' % p print n print '' % p print '
%s%s%s %s%s%s
index.cgiscript - source available here
Search for (use standard shell glob characters, like *) :
''' % cgi.escape(regexp, True)