#! /usr/bin/python # -*- coding: utf-8 -*- # PyKota Print Quota Reports generator # # PyKota - Print Quotas for CUPS # # (c) 2003-2013 Jerome Alet # 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 . # # $Id$ # # import sys import os import cgi import urllib from xml.sax import saxutils import pykota.appinit from pykota import version, utils from pykota.dumper import DumPyKota header = """Content-type: text/html;charset=%s %s

PyKota's Logo
PyKota v%s

%s

%s

""" footer = """


%s © %s %s

%s
        

""" class PyKotaDumperGUI(DumPyKota) : """PyKota Dumper GUI""" def guiDisplay(self) : """Displays the dumper interface.""" global header, footer content = [ header % (self.charset, _("PyKota Data Dumper"), \ self.config.getLogoLink(), \ self.config.getLogoURL(), version.__version__, \ self.config.getLogoLink(), \ version.__version__, _("PyKota Data Dumper"), \ _("Dump"), _("Please click on the above button")) ] content.append(self.htmlListDataTypes(self.options.data)) content.append(u"
") content.append(self.htmlListFormats(self.options.format)) content.append(u"
") content.append(self.htmlFilterInput(" ".join(self.arguments))) content.append(u"
") content.append(self.htmlOrderbyInput(self.options.orderby)) content.append(u"
") content.append(self.htmlSumCheckbox(self.options.sum)) content.append(footer % (_("Dump"), version.__doc__, version.__years__, version.__author__, saxutils.escape(version.__gplblurb__))) for c in content : sys.stdout.write(c.encode(self.charset, "replace")) sys.stdout.flush() def htmlListDataTypes(self, selected="") : """Displays the datatype selection list.""" message = '
%s :
' return message def htmlListFormats(self, selected="") : """Displays the formats selection list.""" message = '
%s :
' return message def htmlFilterInput(self, value="") : """Input the optional dump filter.""" return _("Filter") + (' : e.g. username=jerome printername=HP2100 start=today-30' % (value or "")) def htmlOrderbyInput(self, value="") : """Input the optional ordering.""" return _("Ordering") + (' : e.g. +username,-printername' % (value or "")) def htmlSumCheckbox(self, checked="") : """Input the optional Sum option.""" return _("Summarize") + (' : %s' % ((checked and 'checked="checked"'), _("only for payments or history"))) def guiAction(self) : """Main function""" try : wantreport = self.form.has_key("report") except TypeError : pass # WebDAV request probably, seen when trying to open a csv file in OOo else : if wantreport : try : if self.form.has_key("datatype") : self.options.data = self.form["datatype"].value if self.form.has_key("format") : self.options.format = self.form["format"].value if self.form.has_key("filter") : self.arguments = self.form["filter"].value.split() if self.form.has_key("sum") : self.options.sum = self.form["sum"].value if self.form.has_key("orderby") : self.options.orderby = self.form["orderby"].value # when no authentication is done, or when the remote username # is 'root' (even if not run as root of course), then unrestricted # dump is allowed. remuser = os.environ.get("REMOTE_USER", "root") # special hack to accomodate mod_auth_ldap Apache module try : remuser = remuser.split("=")[1].split(",")[0] except IndexError : pass if remuser != "root" : # non-'root' users when the script is password protected # can not dump any data as they like, we restrict them # to their own datas. if self.options.data not in ["printers", "pmembers", "groups", "gpquotas"] : self.arguments.append("username=%s" % remuser) fname = "error" ctype = "text/plain" if self.options.format in ("csv", "ssv") : #ctype = "application/vnd.sun.xml.calc" # OpenOffice.org ctype = "text/comma-separated-values" fname = "dump.csv" elif self.options.format == "tsv" : #ctype = "application/vnd.sun.xml.calc" # OpenOffice.org ctype = "text/tab-separated-values" fname = "dump.tsv" elif self.options.format == "xml" : ctype = "text/xml" fname = "dump.xml" elif self.options.format == "cups" : ctype = "text/plain" fname = "page_log" sys.stdout.write("Content-type: %s\n" % ctype) sys.stdout.write("Content-disposition: attachment; filename=%s\n\n" % fname) self.main(self.arguments, self.options, restricted=0) except : sys.stdout.write('Content-type: text/html\n\nCGI Error

%s

\n' % self.crashed("CGI Error").replace("\n", "
")) else : self.guiDisplay() class FakeCommandLineOptions : """A class to fake command line options.""" output = "-" data = "history" format = "cups" sum = None orderby = None if __name__ == "__main__" : utils.reinitcgilocale() admin = PyKotaDumperGUI() admin.deferredInit() admin.form = cgi.FieldStorage() admin.options = FakeCommandLineOptions() admin.arguments = [] admin.guiAction() try : admin.storage.close() except (TypeError, NameError, AttributeError) : pass sys.exit(0)