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, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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;charset=%s |
---|
38 | |
---|
39 | <html> |
---|
40 | <head> |
---|
41 | <title>%s</title> |
---|
42 | <link rel="stylesheet" type="text/css" href="/pykota.css" /> |
---|
43 | <script type="text/javascript"> |
---|
44 | <!-- |
---|
45 | function checkvalues() |
---|
46 | { |
---|
47 | if ((document.mainform.format.value == "cups") && (document.mainform.datatype.value != "history")) |
---|
48 | { |
---|
49 | alert("Output format and data type are incompatible."); |
---|
50 | return false; |
---|
51 | } |
---|
52 | |
---|
53 | if (document.mainform.sum.checked && (document.mainform.datatype.value != "payments") && (document.mainform.datatype.value != "history")) |
---|
54 | { |
---|
55 | alert("Summarize is only possible for History and Payments."); |
---|
56 | return false; |
---|
57 | } |
---|
58 | |
---|
59 | if (document.mainform.sum.checked && (document.mainform.format.value == "cups")) |
---|
60 | { |
---|
61 | alert("Summarize is not possible with CUPS' page_log format."); |
---|
62 | return false; |
---|
63 | } |
---|
64 | |
---|
65 | return true; |
---|
66 | } |
---|
67 | //--> |
---|
68 | </script> |
---|
69 | </head> |
---|
70 | <body> |
---|
71 | <!-- %s %s --> |
---|
72 | <p> |
---|
73 | <form action="dumpykota.cgi" method="GET" name="mainform" onsubmit="return checkvalues()"> |
---|
74 | <table> |
---|
75 | <tr> |
---|
76 | <td> |
---|
77 | <p> |
---|
78 | <a href="%s"><img src="%s?version=%s" alt="PyKota's Logo" /></a> |
---|
79 | <br /> |
---|
80 | <a href="%s">PyKota v%s</a> |
---|
81 | </p> |
---|
82 | </td> |
---|
83 | <td colspan="2"> |
---|
84 | <h1>%s</h1> |
---|
85 | </td> |
---|
86 | </tr> |
---|
87 | <tr> |
---|
88 | <td colspan="3" align="center"> |
---|
89 | <input type="submit" name="report" value="%s" /> |
---|
90 | </td> |
---|
91 | </tr> |
---|
92 | </table> |
---|
93 | <p> |
---|
94 | %s |
---|
95 | </p>""" |
---|
96 | |
---|
97 | footer = """ |
---|
98 | <table> |
---|
99 | <tr> |
---|
100 | <td colspan="3" align="center"> |
---|
101 | <input type="submit" name="report" value="%s" /> |
---|
102 | </td> |
---|
103 | </tr> |
---|
104 | </table> |
---|
105 | </form> |
---|
106 | </p> |
---|
107 | <hr width="25%%" /> |
---|
108 | <p> |
---|
109 | <font size="-2"> |
---|
110 | <a href="http://www.pykota.com/">%s</a> |
---|
111 | © %s %s |
---|
112 | <br /> |
---|
113 | <pre> |
---|
114 | %s |
---|
115 | </pre> |
---|
116 | </font> |
---|
117 | </p> |
---|
118 | </body> |
---|
119 | </html>""" |
---|
120 | |
---|
121 | class PyKotaDumperGUI(DumPyKota) : |
---|
122 | """PyKota Dumper GUI""" |
---|
123 | def guiDisplay(self) : |
---|
124 | """Displays the dumper interface.""" |
---|
125 | global header, footer |
---|
126 | print header % (self.charset, _("PyKota Data Dumper"), \ |
---|
127 | self.language, self.charset, \ |
---|
128 | self.config.getLogoLink(), \ |
---|
129 | self.config.getLogoURL(), version.__version__, \ |
---|
130 | self.config.getLogoLink(), \ |
---|
131 | version.__version__, _("PyKota Data Dumper"), \ |
---|
132 | _("Dump"), _("Please click on the above button")) |
---|
133 | print self.htmlListDataTypes(self.options.get("data", "")) |
---|
134 | print "<br />" |
---|
135 | print self.htmlListFormats(self.options.get("format", "")) |
---|
136 | print "<br />" |
---|
137 | print self.htmlFilterInput(" ".join(self.arguments)) |
---|
138 | print "<br />" |
---|
139 | print self.htmlSumCheckbox(self.options.get("sum", "")) |
---|
140 | print footer % (_("Dump"), version.__doc__, version.__years__, version.__author__, version.__gplblurb__) |
---|
141 | |
---|
142 | def htmlListDataTypes(self, selected="") : |
---|
143 | """Displays the datatype selection list.""" |
---|
144 | message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="datatype">' % _("Data Type") |
---|
145 | for dt in self.validdatatypes.items() : |
---|
146 | if dt[0] == selected : |
---|
147 | message += '<option value="%s" selected="selected">%s (%s)</option>' % (dt[0], dt[0], dt[1]) |
---|
148 | else : |
---|
149 | message += '<option value="%s">%s (%s)</option>' % (dt[0], dt[0], dt[1]) |
---|
150 | message += '</select></td></tr></table>' |
---|
151 | return message |
---|
152 | |
---|
153 | def htmlListFormats(self, selected="") : |
---|
154 | """Displays the formats selection list.""" |
---|
155 | message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="format">' % _("Output Format") |
---|
156 | for fmt in self.validformats.items() : |
---|
157 | if fmt[0] == selected : |
---|
158 | message += '<option value="%s" selected="selected">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1]) |
---|
159 | else : |
---|
160 | message += '<option value="%s">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1]) |
---|
161 | message += '</select></td></tr></table>' |
---|
162 | return message |
---|
163 | |
---|
164 | def htmlFilterInput(self, value="") : |
---|
165 | """Input the optional dump filter.""" |
---|
166 | return _("Filter") + (' : <input type="text" name="filter" size="40" value="%s" /> <em>e.g. <strong>username=jerome printername=HP2100 start=today-30</strong></em>' % (value or "")) |
---|
167 | |
---|
168 | def htmlSumCheckbox(self, checked="") : |
---|
169 | """Input the optional Sum option.""" |
---|
170 | return _("Summarize") + (' : <input type="checkbox" name="sum" %s /> <em>%s</em>' % ((checked and 'checked="checked"'), _("only for payments or history"))) |
---|
171 | |
---|
172 | def guiAction(self) : |
---|
173 | """Main function""" |
---|
174 | try : |
---|
175 | wantreport = self.form.has_key("report") |
---|
176 | except TypeError : |
---|
177 | pass # WebDAV request probably, seen when trying to open a csv file in OOo |
---|
178 | else : |
---|
179 | if wantreport : |
---|
180 | try : |
---|
181 | if self.form.has_key("datatype") : |
---|
182 | self.options["data"] = self.form["datatype"].value |
---|
183 | if self.form.has_key("format") : |
---|
184 | self.options["format"] = self.form["format"].value |
---|
185 | if self.form.has_key("filter") : |
---|
186 | self.arguments = self.form["filter"].value.split() |
---|
187 | if self.form.has_key("sum") : |
---|
188 | self.options["sum"] = self.form["sum"].value |
---|
189 | # when no authentication is done, or when the remote username |
---|
190 | # is 'root' (even if not run as root of course), then unrestricted |
---|
191 | # dump is allowed. |
---|
192 | remuser = os.environ.get("REMOTE_USER", "root") |
---|
193 | # special hack to accomodate mod_auth_ldap Apache module |
---|
194 | try : |
---|
195 | remuser = remuser.split("=")[1].split(",")[0] |
---|
196 | except IndexError : |
---|
197 | pass |
---|
198 | if remuser != "root" : |
---|
199 | # non-'root' users when the script is password protected |
---|
200 | # can not dump any data as they like, we restrict them |
---|
201 | # to their own datas. |
---|
202 | if self.options["data"] not in ["printers", "pmembers", "groups", "gpquotas"] : |
---|
203 | self.arguments.append("username=%s" % remuser) |
---|
204 | |
---|
205 | fname = "error" |
---|
206 | ctype = "text/plain" |
---|
207 | if self.options["format"] in ("csv", "ssv") : |
---|
208 | #ctype = "application/vnd.sun.xml.calc" # OpenOffice.org |
---|
209 | ctype = "text/comma-separated-values" |
---|
210 | fname = "dump.csv" |
---|
211 | elif self.options["format"] == "tsv" : |
---|
212 | #ctype = "application/vnd.sun.xml.calc" # OpenOffice.org |
---|
213 | ctype = "text/tab-separated-values" |
---|
214 | fname = "dump.tsv" |
---|
215 | elif self.options["format"] == "xml" : |
---|
216 | ctype = "text/xml" |
---|
217 | fname = "dump.xml" |
---|
218 | elif self.options["format"] == "cups" : |
---|
219 | ctype = "text/plain" |
---|
220 | fname = "page_log" |
---|
221 | print "Content-type: %s" % ctype |
---|
222 | print "Content-disposition: attachment; filename=%s" % fname |
---|
223 | print |
---|
224 | self.main(self.arguments, self.options, restricted=0) |
---|
225 | except : |
---|
226 | 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 />") |
---|
227 | else : |
---|
228 | self.guiDisplay() |
---|
229 | |
---|
230 | if __name__ == "__main__" : |
---|
231 | admin = PyKotaDumperGUI(lang=getLanguagePreference(), charset=getCharsetPreference()) |
---|
232 | admin.deferredInit() |
---|
233 | admin.form = cgi.FieldStorage() |
---|
234 | admin.options = { "output" : "-", |
---|
235 | "data" : "history", |
---|
236 | "format" : "cups", |
---|
237 | "sum" : None, |
---|
238 | } |
---|
239 | admin.arguments = [] |
---|
240 | admin.guiAction() |
---|
241 | try : |
---|
242 | admin.storage.close() |
---|
243 | except (TypeError, NameError, AttributeError) : |
---|
244 | pass |
---|
245 | |
---|
246 | sys.exit(0) |
---|