]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
fa73b229 | 2 | * "$Id: printers.c 4931 2006-01-14 20:37:40Z mike $" |
ef416fc2 | 3 | * |
4 | * Printer status CGI for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
fa73b229 | 26 | * main() - Main entry for CGI. |
27 | * show_all_printers() - Show all printers... | |
28 | * show_printer() - Show a single printer. | |
ef416fc2 | 29 | */ |
30 | ||
31 | /* | |
32 | * Include necessary headers... | |
33 | */ | |
34 | ||
35 | #include "cgi-private.h" | |
36 | ||
37 | ||
fa73b229 | 38 | /* |
39 | * Local functions... | |
40 | */ | |
41 | ||
42 | void show_all_printers(http_t *http, const char *username); | |
43 | void show_printer(http_t *http, const char *printer); | |
44 | ||
45 | ||
ef416fc2 | 46 | /* |
47 | * 'main()' - Main entry for CGI. | |
48 | */ | |
49 | ||
50 | int /* O - Exit status */ | |
51 | main(int argc, /* I - Number of command-line arguments */ | |
52 | char *argv[]) /* I - Command-line arguments */ | |
53 | { | |
fa73b229 | 54 | const char *printer; /* Printer name */ |
55 | const char *user; /* Username */ | |
ef416fc2 | 56 | http_t *http; /* Connection to the server */ |
57 | ipp_t *request, /* IPP request */ | |
58 | *response; /* IPP response */ | |
59 | ipp_attribute_t *attr; /* IPP attribute */ | |
ef416fc2 | 60 | const char *op; /* Operation to perform, if any */ |
ef416fc2 | 61 | static const char *def_attrs[] = /* Attributes for default printer */ |
62 | { | |
63 | "printer-name", | |
64 | "printer-uri-supported" | |
65 | }; | |
66 | ||
67 | ||
68 | /* | |
69 | * Get any form variables... | |
70 | */ | |
71 | ||
72 | cgiInitialize(); | |
ef416fc2 | 73 | |
fa73b229 | 74 | op = cgiGetVariable("OP"); |
ef416fc2 | 75 | |
76 | /* | |
77 | * Set the web interface section... | |
78 | */ | |
79 | ||
80 | cgiSetVariable("SECTION", "printers"); | |
81 | ||
82 | /* | |
fa73b229 | 83 | * See if we are displaying a printer or all printers... |
ef416fc2 | 84 | */ |
85 | ||
fa73b229 | 86 | if (!strcmp(argv[0], "/") || strstr(argv[0], "printers.cgi")) |
87 | printer = NULL; | |
88 | else | |
89 | printer = argv[0]; | |
ef416fc2 | 90 | |
91 | /* | |
fa73b229 | 92 | * See who is logged in... |
ef416fc2 | 93 | */ |
94 | ||
fa73b229 | 95 | if ((user = getenv("REMOTE_USER")) == NULL) |
96 | user = "guest"; | |
ef416fc2 | 97 | |
98 | /* | |
fa73b229 | 99 | * Connect to the HTTP server... |
ef416fc2 | 100 | */ |
101 | ||
fa73b229 | 102 | http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); |
ef416fc2 | 103 | |
fa73b229 | 104 | /* |
105 | * Get the default printer... | |
106 | */ | |
ef416fc2 | 107 | |
fa73b229 | 108 | if (!op) |
ef416fc2 | 109 | { |
110 | /* | |
111 | * Get the default destination... | |
112 | */ | |
113 | ||
fa73b229 | 114 | request = ippNewRequest(CUPS_GET_DEFAULT); |
ef416fc2 | 115 | |
116 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
117 | "requested-attributes", | |
118 | sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs); | |
119 | ||
120 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
121 | { | |
122 | if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL) | |
123 | cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text); | |
124 | ||
125 | if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL) | |
126 | { | |
127 | char url[HTTP_MAX_URI]; /* New URL */ | |
128 | ||
129 | ||
130 | cgiSetVariable("DEFAULT_URI", | |
131 | cgiRewriteURL(attr->values[0].string.text, | |
132 | url, sizeof(url), NULL)); | |
133 | } | |
134 | ||
135 | ippDelete(response); | |
136 | } | |
137 | ||
138 | /* | |
fa73b229 | 139 | * See if we need to show a list of printers or the status of a |
140 | * single printer... | |
ef416fc2 | 141 | */ |
142 | ||
fa73b229 | 143 | if (!printer) |
144 | show_all_printers(http, user); | |
145 | else | |
146 | show_printer(http, printer); | |
147 | } | |
148 | else if (!strcasecmp(op, "print-test-page") && printer) | |
149 | cgiPrintTestPage(http, printer); | |
150 | else if (!strcasecmp(op, "move-jobs") && printer) | |
151 | cgiMoveJobs(http, printer, 0); | |
152 | else | |
153 | { | |
154 | /* | |
155 | * Unknown/bad operation... | |
156 | */ | |
ef416fc2 | 157 | |
fa73b229 | 158 | if (printer) |
159 | cgiStartHTML(printer); | |
160 | else | |
161 | cgiStartHTML(cgiText(_("Printers"))); | |
ef416fc2 | 162 | |
fa73b229 | 163 | cgiCopyTemplateLang("error-op.tmpl"); |
164 | cgiEndHTML(); | |
165 | } | |
ef416fc2 | 166 | |
fa73b229 | 167 | /* |
168 | * Close the HTTP server connection... | |
169 | */ | |
170 | ||
171 | httpClose(http); | |
172 | ||
173 | /* | |
174 | * Return with no errors... | |
175 | */ | |
ef416fc2 | 176 | |
fa73b229 | 177 | return (0); |
178 | } | |
ef416fc2 | 179 | |
ef416fc2 | 180 | |
fa73b229 | 181 | /* |
182 | * 'show_all_printers()' - Show all printers... | |
183 | */ | |
ef416fc2 | 184 | |
fa73b229 | 185 | void |
186 | show_all_printers(http_t *http, /* I - Connection to server */ | |
187 | const char *user) /* I - Username */ | |
188 | { | |
189 | int i; /* Looping var */ | |
190 | ipp_t *request, /* IPP request */ | |
191 | *response; /* IPP response */ | |
192 | cups_array_t *printers; /* Array of printer objects */ | |
193 | ipp_attribute_t *printer; /* Printer object */ | |
194 | int ascending, /* Order of printers (0 = descending) */ | |
195 | first, /* First printer to show */ | |
196 | count; /* Number of printers */ | |
197 | const char *var; /* Form variable */ | |
198 | void *search; /* Search data */ | |
199 | char url[1024], /* URL for prev/next/this */ | |
200 | *urlptr, /* Position in URL */ | |
201 | *urlend; /* End of URL */ | |
ef416fc2 | 202 | |
ef416fc2 | 203 | |
fa73b229 | 204 | /* |
205 | * Show the standard header... | |
206 | */ | |
ef416fc2 | 207 | |
fa73b229 | 208 | cgiStartHTML(cgiText(_("Printers"))); |
ef416fc2 | 209 | |
fa73b229 | 210 | /* |
211 | * Build a CUPS_GET_PRINTERS request, which requires the following | |
212 | * attributes: | |
213 | * | |
214 | * attributes-charset | |
215 | * attributes-natural-language | |
216 | * printer-type | |
217 | * printer-type-mask | |
218 | * requesting-user-name | |
219 | */ | |
ef416fc2 | 220 | |
fa73b229 | 221 | request = ippNewRequest(CUPS_GET_PRINTERS); |
ef416fc2 | 222 | |
fa73b229 | 223 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, |
224 | "printer-type", 0); | |
225 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, | |
226 | "printer-type-mask", CUPS_PRINTER_CLASS); | |
ef416fc2 | 227 | |
fa73b229 | 228 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, |
229 | "requesting-user-name", NULL, user); | |
ef416fc2 | 230 | |
fa73b229 | 231 | cgiGetAttributes(request, "printers.tmpl"); |
ef416fc2 | 232 | |
fa73b229 | 233 | /* |
234 | * Do the request and get back a response... | |
235 | */ | |
236 | ||
237 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
238 | { | |
ef416fc2 | 239 | /* |
fa73b229 | 240 | * Get a list of matching job objects. |
ef416fc2 | 241 | */ |
242 | ||
fa73b229 | 243 | if ((var = cgiGetVariable("QUERY")) != NULL) |
244 | search = cgiCompileSearch(var); | |
245 | else | |
246 | search = NULL; | |
ef416fc2 | 247 | |
fa73b229 | 248 | printers = cgiGetIPPObjects(response, search); |
249 | count = cupsArrayCount(printers); | |
ef416fc2 | 250 | |
fa73b229 | 251 | if (search) |
252 | cgiFreeSearch(search); | |
ef416fc2 | 253 | |
254 | /* | |
fa73b229 | 255 | * Figure out which printers to display... |
ef416fc2 | 256 | */ |
257 | ||
fa73b229 | 258 | if ((var = cgiGetVariable("FIRST")) != NULL) |
259 | first = atoi(var); | |
260 | else | |
261 | first = 0; | |
ef416fc2 | 262 | |
fa73b229 | 263 | if (first >= count) |
264 | first = count - CUPS_PAGE_MAX; | |
ef416fc2 | 265 | |
fa73b229 | 266 | first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX; |
ef416fc2 | 267 | |
fa73b229 | 268 | if (first < 0) |
269 | first = 0; | |
ef416fc2 | 270 | |
fa73b229 | 271 | sprintf(url, "%d", count); |
272 | cgiSetVariable("TOTAL", url); | |
ef416fc2 | 273 | |
fa73b229 | 274 | if ((var = cgiGetVariable("ORDER")) != NULL) |
275 | ascending = !strcasecmp(var, "asc"); | |
276 | else | |
277 | ascending = 1; | |
ef416fc2 | 278 | |
fa73b229 | 279 | if (ascending) |
280 | { | |
281 | for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, first); | |
282 | i < CUPS_PAGE_MAX && printer; | |
283 | i ++, printer = (ipp_attribute_t *)cupsArrayNext(printers)) | |
284 | cgiSetIPPObjectVars(printer, NULL, i); | |
285 | } | |
286 | else | |
287 | { | |
288 | for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, count - first - 1); | |
289 | i < CUPS_PAGE_MAX && printer; | |
290 | i ++, printer = (ipp_attribute_t *)cupsArrayPrev(printers)) | |
291 | cgiSetIPPObjectVars(printer, NULL, i); | |
292 | } | |
ef416fc2 | 293 | |
fa73b229 | 294 | /* |
295 | * Save navigation URLs... | |
296 | */ | |
ef416fc2 | 297 | |
fa73b229 | 298 | urlend = url + sizeof(url); |
ef416fc2 | 299 | |
fa73b229 | 300 | if ((var = cgiGetVariable("QUERY")) != NULL) |
301 | { | |
302 | strlcpy(url, "/printers/?QUERY=", sizeof(url)); | |
303 | urlptr = url + strlen(url); | |
ef416fc2 | 304 | |
fa73b229 | 305 | cgiFormEncode(urlptr, var, urlend - urlptr); |
306 | urlptr += strlen(urlptr); | |
ef416fc2 | 307 | |
fa73b229 | 308 | strlcpy(urlptr, "&", urlend - urlptr); |
309 | urlptr += strlen(urlptr); | |
310 | } | |
311 | else | |
312 | { | |
313 | strlcpy(url, "/printers/?", sizeof(url)); | |
314 | urlptr = url + strlen(url); | |
315 | } | |
ef416fc2 | 316 | |
fa73b229 | 317 | snprintf(urlptr, urlend - urlptr, "FIRST=%d", first); |
318 | cgiSetVariable("THISURL", url); | |
319 | ||
320 | if (first > 0) | |
321 | { | |
322 | snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s", | |
323 | first - CUPS_PAGE_MAX, ascending ? "asc" : "dec"); | |
324 | cgiSetVariable("PREVURL", url); | |
ef416fc2 | 325 | } |
fa73b229 | 326 | |
327 | if ((first + CUPS_PAGE_MAX) < count) | |
328 | { | |
329 | snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s", | |
330 | first + CUPS_PAGE_MAX, ascending ? "asc" : "dec"); | |
331 | cgiSetVariable("NEXTURL", url); | |
332 | } | |
333 | ||
ef416fc2 | 334 | /* |
fa73b229 | 335 | * Then show everything... |
ef416fc2 | 336 | */ |
337 | ||
fa73b229 | 338 | cgiCopyTemplateLang("search.tmpl"); |
ef416fc2 | 339 | |
fa73b229 | 340 | cgiCopyTemplateLang("printers-header.tmpl"); |
ef416fc2 | 341 | |
fa73b229 | 342 | if (count > 0) |
343 | cgiCopyTemplateLang("pager.tmpl"); | |
ef416fc2 | 344 | |
fa73b229 | 345 | cgiCopyTemplateLang("printers.tmpl"); |
ef416fc2 | 346 | |
fa73b229 | 347 | if (count > 0) |
348 | cgiCopyTemplateLang("pager.tmpl"); | |
ef416fc2 | 349 | |
350 | /* | |
fa73b229 | 351 | * Delete the response... |
ef416fc2 | 352 | */ |
353 | ||
fa73b229 | 354 | ippDelete(response); |
355 | } | |
356 | else | |
357 | { | |
358 | /* | |
359 | * Show the error... | |
360 | */ | |
ef416fc2 | 361 | |
fa73b229 | 362 | cgiShowIPPError(_("Unable to get printer list:")); |
363 | } | |
ef416fc2 | 364 | |
fa73b229 | 365 | cgiEndHTML(); |
366 | } | |
ef416fc2 | 367 | |
ef416fc2 | 368 | |
fa73b229 | 369 | /* |
370 | * 'show_printer()' - Show a single printer. | |
371 | */ | |
ef416fc2 | 372 | |
fa73b229 | 373 | void |
374 | show_printer(http_t *http, /* I - Connection to server */ | |
375 | const char *printer) /* I - Name of printer */ | |
376 | { | |
377 | ipp_t *request, /* IPP request */ | |
378 | *response; /* IPP response */ | |
379 | ipp_attribute_t *attr; /* IPP attribute */ | |
380 | char uri[HTTP_MAX_URI]; /* Printer URI */ | |
381 | char refresh[1024]; /* Refresh URL */ | |
ef416fc2 | 382 | |
ef416fc2 | 383 | |
fa73b229 | 384 | /* |
385 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following | |
386 | * attributes: | |
387 | * | |
388 | * attributes-charset | |
389 | * attributes-natural-language | |
390 | * printer-uri | |
391 | */ | |
392 | ||
393 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); | |
394 | ||
395 | httpAssembleURIf(uri, sizeof(uri), "ipp", NULL, "localhost", 0, | |
396 | "/printers/%s", printer); | |
397 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
398 | uri); | |
399 | ||
400 | cgiGetAttributes(request, "printers.tmpl"); | |
401 | ||
402 | /* | |
403 | * Do the request and get back a response... | |
404 | */ | |
ef416fc2 | 405 | |
fa73b229 | 406 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
407 | { | |
ef416fc2 | 408 | /* |
fa73b229 | 409 | * Got the result; set the CGI variables and check the status of a |
410 | * single-queue request... | |
ef416fc2 | 411 | */ |
412 | ||
fa73b229 | 413 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); |
414 | ||
415 | if (printer && (attr = ippFindAttribute(response, "printer-state", | |
416 | IPP_TAG_ENUM)) != NULL && | |
417 | attr->values[0].integer == IPP_PRINTER_PROCESSING) | |
ef416fc2 | 418 | { |
fa73b229 | 419 | /* |
420 | * Printer is processing - automatically refresh the page until we | |
421 | * are done printing... | |
422 | */ | |
ef416fc2 | 423 | |
fa73b229 | 424 | cgiFormEncode(uri, printer, sizeof(uri)); |
425 | snprintf(refresh, sizeof(refresh), "10;/printers/%s", uri); | |
426 | cgiSetVariable("refresh_page", refresh); | |
ef416fc2 | 427 | } |
ef416fc2 | 428 | |
fa73b229 | 429 | /* |
430 | * Delete the response... | |
431 | */ | |
432 | ||
433 | ippDelete(response); | |
ef416fc2 | 434 | |
435 | /* | |
436 | * Show the standard header... | |
437 | */ | |
438 | ||
fa73b229 | 439 | cgiStartHTML(printer); |
ef416fc2 | 440 | |
441 | /* | |
fa73b229 | 442 | * Show the printer status... |
ef416fc2 | 443 | */ |
444 | ||
fa73b229 | 445 | cgiCopyTemplateLang("printers.tmpl"); |
ef416fc2 | 446 | |
fa73b229 | 447 | /* |
448 | * Show jobs for the specified printer... | |
449 | */ | |
ef416fc2 | 450 | |
fa73b229 | 451 | cgiCopyTemplateLang("printer-jobs-header.tmpl"); |
452 | cgiShowJobs(http, printer); | |
453 | } | |
454 | else | |
455 | { | |
456 | /* | |
457 | * Show the IPP error... | |
458 | */ | |
ef416fc2 | 459 | |
fa73b229 | 460 | cgiStartHTML(printer); |
461 | cgiShowIPPError(_("Unable to get printer status:")); | |
462 | } | |
ef416fc2 | 463 | |
fa73b229 | 464 | cgiEndHTML(); |
ef416fc2 | 465 | } |
466 | ||
467 | ||
468 | /* | |
fa73b229 | 469 | * End of "$Id: printers.c 4931 2006-01-14 20:37:40Z mike $". |
ef416fc2 | 470 | */ |