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