[2019] | 1 | #! /usr/bin/python |
---|
[3275] | 2 | # -*- coding: UTF-8 -*- |
---|
[2019] | 3 | |
---|
| 4 | # PyKota Print Quota Reports generator |
---|
| 5 | # |
---|
[3259] | 6 | # PyKota - Print Quotas for CUPS |
---|
[2019] | 7 | # |
---|
[3275] | 8 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
[3259] | 9 | # This program is free software: you can redistribute it and/or modify |
---|
[2019] | 10 | # it under the terms of the GNU General Public License as published by |
---|
[3259] | 11 | # the Free Software Foundation, either version 3 of the License, or |
---|
[2019] | 12 | # (at your option) any later version. |
---|
[3259] | 13 | # |
---|
[2019] | 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 |
---|
[3259] | 20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[2019] | 21 | # |
---|
| 22 | # $Id$ |
---|
| 23 | # |
---|
[2032] | 24 | # |
---|
[2019] | 25 | |
---|
| 26 | import sys |
---|
| 27 | import os |
---|
| 28 | import cgi |
---|
| 29 | import urllib |
---|
[3333] | 30 | from xml.sax import saxutils |
---|
[2019] | 31 | |
---|
[3295] | 32 | import pykota.appinit |
---|
| 33 | from pykota import version, utils |
---|
[2019] | 34 | from pykota.dumper import DumPyKota |
---|
| 35 | |
---|
[3055] | 36 | header = """Content-type: text/html;charset=%s |
---|
[2019] | 37 | |
---|
| 38 | <html> |
---|
| 39 | <head> |
---|
| 40 | <title>%s</title> |
---|
| 41 | <link rel="stylesheet" type="text/css" href="/pykota.css" /> |
---|
[3032] | 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> |
---|
[2019] | 68 | </head> |
---|
| 69 | <body> |
---|
[2265] | 70 | <p> |
---|
[3032] | 71 | <form action="dumpykota.cgi" method="GET" name="mainform" onsubmit="return checkvalues()"> |
---|
[2265] | 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>""" |
---|
[2019] | 94 | |
---|
| 95 | footer = """ |
---|
[2265] | 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"> |
---|
[2909] | 108 | <a href="http://www.pykota.com/">%s</a> |
---|
[2267] | 109 | © %s %s |
---|
[2586] | 110 | <br /> |
---|
| 111 | <pre> |
---|
| 112 | %s |
---|
| 113 | </pre> |
---|
[2265] | 114 | </font> |
---|
| 115 | </p> |
---|
[2019] | 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 |
---|
[3295] | 124 | content = [ header % (self.charset, _("PyKota Data Dumper"), \ |
---|
[2265] | 125 | self.config.getLogoLink(), \ |
---|
| 126 | self.config.getLogoURL(), version.__version__, \ |
---|
| 127 | self.config.getLogoLink(), \ |
---|
| 128 | version.__version__, _("PyKota Data Dumper"), \ |
---|
[3295] | 129 | _("Dump"), _("Please click on the above button")) ] |
---|
| 130 | content.append(self.htmlListDataTypes(self.options.get("data", ""))) |
---|
| 131 | content.append(u"<br />") |
---|
| 132 | content.append(self.htmlListFormats(self.options.get("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.get("orderby", ""))) |
---|
| 137 | content.append(u"<br />") |
---|
| 138 | content.append(self.htmlSumCheckbox(self.options.get("sum", ""))) |
---|
[3333] | 139 | content.append(footer % (_("Dump"), |
---|
| 140 | version.__doc__, |
---|
| 141 | version.__years__, |
---|
| 142 | version.__author__, |
---|
| 143 | saxutils.escape(version.__gplblurb__))) |
---|
[3295] | 144 | for c in content : |
---|
| 145 | sys.stdout.write(c.encode(self.charset, "replace")) |
---|
| 146 | sys.stdout.flush() |
---|
[2019] | 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.""" |
---|
[3030] | 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 "")) |
---|
[2019] | 173 | |
---|
[3211] | 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 | |
---|
[2284] | 178 | def htmlSumCheckbox(self, checked="") : |
---|
| 179 | """Input the optional Sum option.""" |
---|
[2298] | 180 | return _("Summarize") + (' : <input type="checkbox" name="sum" %s /> <em>%s</em>' % ((checked and 'checked="checked"'), _("only for payments or history"))) |
---|
[2284] | 181 | |
---|
[2019] | 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 : |
---|
[2032] | 190 | try : |
---|
[2229] | 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() |
---|
[2284] | 197 | if self.form.has_key("sum") : |
---|
| 198 | self.options["sum"] = self.form["sum"].value |
---|
[3211] | 199 | if self.form.has_key("orderby") : |
---|
| 200 | self.options["orderby"] = self.form["orderby"].value |
---|
[2229] | 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 | |
---|
[2630] | 217 | fname = "error" |
---|
| 218 | ctype = "text/plain" |
---|
[2229] | 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 | print "Content-type: %s" % ctype |
---|
| 234 | print "Content-disposition: attachment; filename=%s" % fname |
---|
| 235 | print |
---|
[2032] | 236 | self.main(self.arguments, self.options, restricted=0) |
---|
[2229] | 237 | except : |
---|
| 238 | 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 />") |
---|
[2019] | 239 | else : |
---|
| 240 | self.guiDisplay() |
---|
| 241 | |
---|
| 242 | if __name__ == "__main__" : |
---|
[3295] | 243 | utils.reinitcgilocale() |
---|
| 244 | admin = PyKotaDumperGUI() |
---|
[2210] | 245 | admin.deferredInit() |
---|
[2019] | 246 | admin.form = cgi.FieldStorage() |
---|
| 247 | admin.options = { "output" : "-", |
---|
| 248 | "data" : "history", |
---|
| 249 | "format" : "cups", |
---|
[2309] | 250 | "sum" : None, |
---|
[3170] | 251 | "orderby" : None, |
---|
[2019] | 252 | } |
---|
| 253 | admin.arguments = [] |
---|
| 254 | admin.guiAction() |
---|
| 255 | try : |
---|
| 256 | admin.storage.close() |
---|
| 257 | except (TypeError, NameError, AttributeError) : |
---|
| 258 | pass |
---|
| 259 | |
---|
| 260 | sys.exit(0) |
---|