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 | # |
---|
26 | |
---|
27 | import sys |
---|
28 | import os |
---|
29 | import cgi |
---|
30 | import urllib |
---|
31 | |
---|
32 | from pykota import version |
---|
33 | from pykota.tool import PyKotaToolError |
---|
34 | from pykota.dumper import DumPyKota |
---|
35 | from pykota.cgifuncs import getLanguagePreference, getCharsetPreference |
---|
36 | |
---|
37 | header = """Content-type: text/html |
---|
38 | |
---|
39 | <?xml version="1.0" encoding="%s"?> |
---|
40 | <html> |
---|
41 | <head> |
---|
42 | <title>%s</title> |
---|
43 | <link rel="stylesheet" type="text/css" href="/pykota.css" /> |
---|
44 | </head> |
---|
45 | <body> |
---|
46 | <form action="dumpykota.cgi" method="GET"> |
---|
47 | <table> |
---|
48 | <tr> |
---|
49 | <td> |
---|
50 | <p> |
---|
51 | <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/pykota.png" alt="PyKota's Logo" /></a> |
---|
52 | <br /> |
---|
53 | <a href="http://www.librelogiciel.com/software/">PyKota v%s</a> |
---|
54 | </p> |
---|
55 | </td> |
---|
56 | <td colspan="2"> |
---|
57 | <h1>%s</h1> |
---|
58 | </td> |
---|
59 | </tr> |
---|
60 | <tr> |
---|
61 | <td colspan="3" align="center"> |
---|
62 | <input type="submit" name="report" value="%s" /> |
---|
63 | </td> |
---|
64 | </tr> |
---|
65 | </table> |
---|
66 | <p> |
---|
67 | %s |
---|
68 | </p>""" |
---|
69 | |
---|
70 | footer = """ |
---|
71 | <table> |
---|
72 | <tr> |
---|
73 | <td colspan="3" align="center"> |
---|
74 | <input type="submit" name="report" value="%s" /> |
---|
75 | </td> |
---|
76 | </tr> |
---|
77 | </table> |
---|
78 | </form> |
---|
79 | </body> |
---|
80 | </html>""" |
---|
81 | |
---|
82 | class PyKotaDumperGUI(DumPyKota) : |
---|
83 | """PyKota Dumper GUI""" |
---|
84 | def guiDisplay(self) : |
---|
85 | """Displays the dumper interface.""" |
---|
86 | global header, footer |
---|
87 | print header % (self.getCharset(), _("PyKota Data Dumper"), version.__version__, _("PyKota Data Dumper"), _("Dump"), _("Please click on the above button")) |
---|
88 | print self.htmlListDataTypes(self.options.get("data", "")) |
---|
89 | print "<br />" |
---|
90 | print self.htmlListFormats(self.options.get("format", "")) |
---|
91 | print "<br />" |
---|
92 | print self.htmlFilterInput(" ".join(self.arguments)) |
---|
93 | print footer % _("Dump") |
---|
94 | |
---|
95 | def htmlListDataTypes(self, selected="") : |
---|
96 | """Displays the datatype selection list.""" |
---|
97 | message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="datatype">' % _("Data Type") |
---|
98 | for dt in self.validdatatypes.items() : |
---|
99 | if dt[0] == selected : |
---|
100 | message += '<option value="%s" selected="selected">%s (%s)</option>' % (dt[0], dt[0], dt[1]) |
---|
101 | else : |
---|
102 | message += '<option value="%s">%s (%s)</option>' % (dt[0], dt[0], dt[1]) |
---|
103 | message += '</select></td></tr></table>' |
---|
104 | return message |
---|
105 | |
---|
106 | def htmlListFormats(self, selected="") : |
---|
107 | """Displays the formats selection list.""" |
---|
108 | message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="format">' % _("Output Format") |
---|
109 | for fmt in self.validformats.items() : |
---|
110 | if fmt[0] == selected : |
---|
111 | message += '<option value="%s" selected="selected">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1]) |
---|
112 | else : |
---|
113 | message += '<option value="%s">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1]) |
---|
114 | message += '</select></td></tr></table>' |
---|
115 | return message |
---|
116 | |
---|
117 | def htmlFilterInput(self, value="") : |
---|
118 | """Input the optional dump filter.""" |
---|
119 | return _("Filter") + (' : <input type="text" name="filter" size="40" value="%s" /> <em>e.g. <strong>username=jerome printername=HP2100</strong></em>' % (value or "")) |
---|
120 | |
---|
121 | def guiAction(self) : |
---|
122 | """Main function""" |
---|
123 | try : |
---|
124 | wantreport = self.form.has_key("report") |
---|
125 | except TypeError : |
---|
126 | pass # WebDAV request probably, seen when trying to open a csv file in OOo |
---|
127 | else : |
---|
128 | if wantreport : |
---|
129 | if self.form.has_key("datatype") : |
---|
130 | self.options["data"] = self.form["datatype"].value |
---|
131 | if self.form.has_key("format") : |
---|
132 | self.options["format"] = self.form["format"].value |
---|
133 | if self.form.has_key("filter") : |
---|
134 | self.arguments = self.form["filter"].value.split() |
---|
135 | |
---|
136 | # when no authentication is done, or when the remote username |
---|
137 | # is 'root' (even if not run as root of course), then unrestricted |
---|
138 | # dump is allowed. |
---|
139 | remuser = os.environ.get("REMOTE_USER", "root") |
---|
140 | # special hack to accomodate mod_auth_ldap Apache module |
---|
141 | try : |
---|
142 | remuser = remuser.split("=")[1].split(",")[0] |
---|
143 | except IndexError : |
---|
144 | pass |
---|
145 | if remuser != "root" : |
---|
146 | # non-'root' users when the script is password protected |
---|
147 | # can not dump any data as they like, we restrict them |
---|
148 | # to their own datas. |
---|
149 | if self.options["data"] not in ["printers", "pmembers", "groups", "gpquotas"] : |
---|
150 | self.arguments.append("username=%s" % remuser) |
---|
151 | |
---|
152 | if self.options["format"] in ("csv", "ssv") : |
---|
153 | #ctype = "application/vnd.sun.xml.calc" # OpenOffice.org |
---|
154 | ctype = "text/comma-separated-values" |
---|
155 | fname = "dump.csv" |
---|
156 | elif self.options["format"] == "tsv" : |
---|
157 | #ctype = "application/vnd.sun.xml.calc" # OpenOffice.org |
---|
158 | ctype = "text/tab-separated-values" |
---|
159 | fname = "dump.tsv" |
---|
160 | elif self.options["format"] == "xml" : |
---|
161 | ctype = "text/xml" |
---|
162 | fname = "dump.xml" |
---|
163 | elif self.options["format"] == "cups" : |
---|
164 | ctype = "text/plain" |
---|
165 | fname = "page_log" |
---|
166 | print "Content-type: %s" % ctype |
---|
167 | print "Content-disposition: attachment; filename=%s" % fname |
---|
168 | print |
---|
169 | try : |
---|
170 | self.main(self.arguments, self.options, restricted=0) |
---|
171 | except PyKotaToolError, msg : |
---|
172 | print msg |
---|
173 | else : |
---|
174 | self.guiDisplay() |
---|
175 | |
---|
176 | if __name__ == "__main__" : |
---|
177 | os.environ["LC_ALL"] = getLanguagePreference() |
---|
178 | admin = PyKotaDumperGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference()) |
---|
179 | admin.form = cgi.FieldStorage() |
---|
180 | admin.options = { "output" : "-", |
---|
181 | "data" : "history", |
---|
182 | "format" : "cups", |
---|
183 | } |
---|
184 | admin.arguments = [] |
---|
185 | admin.guiAction() |
---|
186 | try : |
---|
187 | admin.storage.close() |
---|
188 | except (TypeError, NameError, AttributeError) : |
---|
189 | pass |
---|
190 | |
---|
191 | sys.exit(0) |
---|