root / pykota / trunk / cgi-bin / dumpykota.cgi @ 2032

Revision 2032, 8.1 kB (checked in by jalet, 19 years ago)

Now dumpykota.cgi behaves like printquota.cgi wrt the REMOTE_USER environment
variables if the script is username+password protected.
Small fix in printquota.cgi wrt ldap auth with Apache : the workaround was
not used everywhere.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/python
2# -*- coding: ISO-8859-15 -*-
3
4# PyKota Print Quota Reports generator
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
8# (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com>
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22#
23# $Id$
24#
25# $Log$
26# Revision 1.5  2005/01/19 08:49:41  jalet
27# Now dumpykota.cgi behaves like printquota.cgi wrt the REMOTE_USER environment
28# variables if the script is username+password protected.
29# Small fix in printquota.cgi wrt ldap auth with Apache : the workaround was
30# not used everywhere.
31#
32# Revision 1.4  2005/01/17 08:44:24  jalet
33# Modified copyright years
34#
35# Revision 1.3  2005/01/10 14:57:29  jalet
36# Fixed the default datatype which is now set to 'history'
37#
38# Revision 1.2  2005/01/08 19:47:00  jalet
39# Fixed a bad copy&paste
40#
41# Revision 1.1  2005/01/08 19:13:11  jalet
42# dumpykota.cgi was added to allow the use of dumpykota through the web.
43# This makes real time interfacing with the third party software phpPrintAnalyzer
44# a breeze !
45#
46#
47
48import sys
49import os
50import cgi
51import urllib
52
53from pykota import version
54from pykota.tool import PyKotaToolError
55from pykota.dumper import DumPyKota
56from pykota.cgifuncs import getLanguagePreference, getCharsetPreference
57
58header = """Content-type: text/html
59
60<?xml version="1.0" encoding="%s"?>
61<html>
62  <head>
63    <title>%s</title>
64    <link rel="stylesheet" type="text/css" href="/pykota.css" />
65  </head>
66  <body>
67    <form action="dumpykota.cgi" method="GET">
68      <table>
69        <tr>
70          <td>
71            <p>
72              <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/pykota.png" alt="PyKota's Logo" /></a>
73              <br />
74              <a href="http://www.librelogiciel.com/software/">PyKota v%s</a>
75            </p>
76          </td>
77          <td colspan="2">
78            <h1>%s</h1>
79          </td>
80        </tr>
81        <tr>
82          <td colspan="3" align="center">
83            <input type="submit" name="report" value="%s" />
84          </td>
85        </tr>
86      </table>
87      <p>
88        %s
89      </p>"""
90   
91footer = """
92      <table>
93        <tr>
94          <td colspan="3" align="center">
95            <input type="submit" name="report" value="%s" />
96          </td>
97        </tr>
98      </table> 
99    </form>
100  </body>
101</html>""" 
102
103class PyKotaDumperGUI(DumPyKota) :
104    """PyKota Dumper GUI"""
105    def guiDisplay(self) :
106        """Displays the dumper interface."""
107        global header, footer
108        print header % (self.getCharset(), _("PyKota Data Dumper"), version.__version__, _("PyKota Data Dumper"), _("Dump"), _("Please click on the above button"))
109        print self.htmlListDataTypes(self.options.get("data", ""))
110        print "<br />"
111        print self.htmlListFormats(self.options.get("format", ""))
112        print "<br />"
113        print self.htmlFilterInput(" ".join(self.arguments))
114        print footer % _("Dump")
115       
116    def htmlListDataTypes(self, selected="") :   
117        """Displays the datatype selection list."""
118        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="datatype">' % _("Data Type")
119        for dt in self.validdatatypes.items() :
120            if dt[0] == selected :
121                message += '<option value="%s" selected="selected">%s (%s)</option>' % (dt[0], dt[0], dt[1])
122            else :
123                message += '<option value="%s">%s (%s)</option>' % (dt[0], dt[0], dt[1])
124        message += '</select></td></tr></table>'
125        return message
126       
127    def htmlListFormats(self, selected="") :   
128        """Displays the formats selection list."""
129        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="format">' % _("Output Format")
130        for fmt in self.validformats.items() :
131            if fmt[0] == selected :
132                message += '<option value="%s" selected="selected">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1])
133            else :
134                message += '<option value="%s">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1])
135        message += '</select></td></tr></table>'
136        return message
137       
138    def htmlFilterInput(self, value="") :   
139        """Input the optional dump filter."""
140        return _("Filter") + (' : <input type="text" name="filter" size="40" value="%s" /> <em>e.g. <strong>username=jerome printername=HP2100</strong></em>' % (value or ""))
141       
142    def guiAction(self) :
143        """Main function"""
144        try :
145            wantreport = self.form.has_key("report")
146        except TypeError :   
147            pass # WebDAV request probably, seen when trying to open a csv file in OOo
148        else :   
149            if wantreport :
150                if self.form.has_key("datatype") :
151                    self.options["data"] = self.form["datatype"].value
152                if self.form.has_key("format") :
153                    self.options["format"] = self.form["format"].value
154                if self.form.has_key("filter") :   
155                    self.arguments = self.form["filter"].value.split()
156                   
157                # when no authentication is done, or when the remote username   
158                # is 'root' (even if not run as root of course), then unrestricted
159                # dump is allowed.
160                remuser = os.environ.get("REMOTE_USER", "root")   
161                # special hack to accomodate mod_auth_ldap Apache module
162                try :
163                    remuser = remuser.split("=")[1].split(",")[0]
164                except IndexError :   
165                    pass
166                if remuser != "root" :
167                    # non-'root' users when the script is password protected
168                    # can not dump any data as they like, we restrict them
169                    # to their own datas.
170                    if self.options["data"] not in ["printers", "pmembers", "groups", "gpquotas"] :
171                        self.arguments.append("username=%s" % remuser)
172                   
173                if self.options["format"] in ("csv", "ssv") :
174                    #ctype = "application/vnd.sun.xml.calc"     # OpenOffice.org
175                    ctype = "text/comma-separated-values"
176                    fname = "dump.csv"
177                elif self.options["format"] == "tsv" :
178                    #ctype = "application/vnd.sun.xml.calc"     # OpenOffice.org
179                    ctype = "text/tab-separated-values"
180                    fname = "dump.tsv"
181                elif self.options["format"] == "xml" :
182                    ctype = "text/xml"
183                    fname = "dump.xml"
184                elif self.options["format"] == "cups" :
185                    ctype = "text/plain"
186                    fname = "page_log"
187                print "Content-type: %s" % ctype   
188                print "Content-disposition: attachment; filename=%s" % fname
189                print
190                try :
191                    self.main(self.arguments, self.options, restricted=0)
192                except PyKotaToolError, msg :   
193                    print msg
194            else :       
195                self.guiDisplay()
196           
197if __name__ == "__main__" :
198    os.environ["LC_ALL"] = getLanguagePreference()
199    admin = PyKotaDumperGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference())
200    admin.form = cgi.FieldStorage()
201    admin.options = { "output" : "-",
202                "data" : "history",
203                "format" : "cups",
204              }
205    admin.arguments = []
206    admin.guiAction()
207    try :
208        admin.storage.close()
209    except (TypeError, NameError, AttributeError) :   
210        pass
211       
212    sys.exit(0)
Note: See TracBrowser for help on using the browser.