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