]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/printers.c
Merge changes from CUPS 1.5svn-r8857.
[thirdparty/cups.git] / cgi-bin / printers.c
1 /*
2 * "$Id: printers.c 7940 2008-09-16 00:45:16Z mike $"
3 *
4 * Printer status CGI for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * Contents:
16 *
17 * main() - Main entry for CGI.
18 * do_printer_op() - Do a printer operation.
19 * show_all_printers() - Show all printers...
20 * show_printer() - Show a single printer.
21 */
22
23 /*
24 * Include necessary headers...
25 */
26
27 #include "cgi-private.h"
28 #include <errno.h>
29
30
31 /*
32 * Local functions...
33 */
34
35 static void do_printer_op(http_t *http, const char *printer, ipp_op_t op,
36 const char *title);
37 static void show_all_printers(http_t *http, const char *username);
38 static void show_printer(http_t *http, const char *printer);
39
40
41 /*
42 * 'main()' - Main entry for CGI.
43 */
44
45 int /* O - Exit status */
46 main(int argc, /* I - Number of command-line arguments */
47 char *argv[]) /* I - Command-line arguments */
48 {
49 const char *printer; /* Printer name */
50 const char *user; /* Username */
51 http_t *http; /* Connection to the server */
52 ipp_t *request, /* IPP request */
53 *response; /* IPP response */
54 ipp_attribute_t *attr; /* IPP attribute */
55 const char *op; /* Operation to perform, if any */
56 static const char *def_attrs[] = /* Attributes for default printer */
57 {
58 "printer-name",
59 "printer-uri-supported"
60 };
61
62
63 /*
64 * Get any form variables...
65 */
66
67 cgiInitialize();
68
69 op = cgiGetVariable("OP");
70
71 /*
72 * Set the web interface section...
73 */
74
75 cgiSetVariable("SECTION", "printers");
76
77 /*
78 * See if we are displaying a printer or all printers...
79 */
80
81 if ((printer = getenv("PATH_INFO")) != NULL)
82 {
83 printer ++;
84
85 if (!*printer)
86 printer = NULL;
87
88 if (printer)
89 cgiSetVariable("PRINTER_NAME", printer);
90 }
91
92 /*
93 * See who is logged in...
94 */
95
96 user = getenv("REMOTE_USER");
97
98 /*
99 * Connect to the HTTP server...
100 */
101
102 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
103
104 /*
105 * Get the default printer...
106 */
107
108 if (!op || !cgiIsPOST())
109 {
110 /*
111 * Get the default destination...
112 */
113
114 request = ippNewRequest(CUPS_GET_DEFAULT);
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 /*
139 * See if we need to show a list of printers or the status of a
140 * single printer...
141 */
142
143 if (!printer)
144 show_all_printers(http, user);
145 else
146 show_printer(http, printer);
147 }
148 else if (printer)
149 {
150 if (!strcmp(op, "start-printer"))
151 do_printer_op(http, printer, IPP_RESUME_PRINTER,
152 cgiText(_("Resume Printer")));
153 else if (!strcmp(op, "stop-printer"))
154 do_printer_op(http, printer, IPP_PAUSE_PRINTER,
155 cgiText(_("Pause Printer")));
156 else if (!strcmp(op, "accept-jobs"))
157 do_printer_op(http, printer, CUPS_ACCEPT_JOBS, cgiText(_("Accept Jobs")));
158 else if (!strcmp(op, "reject-jobs"))
159 do_printer_op(http, printer, CUPS_REJECT_JOBS, cgiText(_("Reject Jobs")));
160 else if (!strcmp(op, "purge-jobs"))
161 do_printer_op(http, printer, IPP_PURGE_JOBS, cgiText(_("Purge Jobs")));
162 else if (!strcasecmp(op, "print-self-test-page"))
163 cgiPrintCommand(http, printer, "PrintSelfTestPage",
164 cgiText(_("Print Self-Test Page")));
165 else if (!strcasecmp(op, "clean-print-heads"))
166 cgiPrintCommand(http, printer, "Clean all",
167 cgiText(_("Clean Print Heads")));
168 else if (!strcasecmp(op, "print-test-page"))
169 cgiPrintTestPage(http, printer);
170 else if (!strcasecmp(op, "move-jobs"))
171 cgiMoveJobs(http, printer, 0);
172 else
173 {
174 /*
175 * Unknown/bad operation...
176 */
177
178 cgiStartHTML(printer);
179 cgiCopyTemplateLang("error-op.tmpl");
180 cgiEndHTML();
181 }
182 }
183 else
184 {
185 /*
186 * Unknown/bad operation...
187 */
188
189 cgiStartHTML(cgiText(_("Printers")));
190 cgiCopyTemplateLang("error-op.tmpl");
191 cgiEndHTML();
192 }
193
194 /*
195 * Close the HTTP server connection...
196 */
197
198 httpClose(http);
199
200 /*
201 * Return with no errors...
202 */
203
204 return (0);
205 }
206
207
208 /*
209 * 'do_printer_op()' - Do a printer operation.
210 */
211
212 static void
213 do_printer_op(http_t *http, /* I - HTTP connection */
214 const char *printer, /* I - Printer name */
215 ipp_op_t op, /* I - Operation to perform */
216 const char *title) /* I - Title of page */
217 {
218 ipp_t *request; /* IPP request */
219 char uri[HTTP_MAX_URI], /* Printer URI */
220 resource[HTTP_MAX_URI]; /* Path for request */
221
222
223 /*
224 * Build a printer request, which requires the following
225 * attributes:
226 *
227 * attributes-charset
228 * attributes-natural-language
229 * printer-uri
230 */
231
232 request = ippNewRequest(op);
233
234 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
235 "localhost", 0, "/printers/%s", printer);
236 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
237 NULL, uri);
238
239 /*
240 * Do the request and get back a response...
241 */
242
243 snprintf(resource, sizeof(resource), "/printers/%s", printer);
244 ippDelete(cupsDoRequest(http, request, resource));
245
246 if (cupsLastError() == IPP_NOT_AUTHORIZED)
247 {
248 puts("Status: 401\n");
249 exit(0);
250 }
251 else if (cupsLastError() > IPP_OK_CONFLICT)
252 {
253 cgiStartHTML(title);
254 cgiShowIPPError(_("Unable to do maintenance command:"));
255 }
256 else
257 {
258 /*
259 * Redirect successful updates back to the printer page...
260 */
261
262 char url[1024], /* Printer/class URL */
263 refresh[1024]; /* Refresh URL */
264
265
266 cgiRewriteURL(uri, url, sizeof(url), NULL);
267 cgiFormEncode(uri, url, sizeof(uri));
268 snprintf(refresh, sizeof(refresh), "5;URL=%s", uri);
269 cgiSetVariable("refresh_page", refresh);
270
271 cgiStartHTML(title);
272
273 if (op == IPP_PAUSE_PRINTER)
274 cgiCopyTemplateLang("printer-stop.tmpl");
275 else if (op == IPP_RESUME_PRINTER)
276 cgiCopyTemplateLang("printer-start.tmpl");
277 else if (op == CUPS_ACCEPT_JOBS)
278 cgiCopyTemplateLang("printer-accept.tmpl");
279 else if (op == CUPS_REJECT_JOBS)
280 cgiCopyTemplateLang("printer-reject.tmpl");
281 else if (op == IPP_PURGE_JOBS)
282 cgiCopyTemplateLang("printer-purge.tmpl");
283 }
284
285 cgiEndHTML();
286 }
287
288
289 /*
290 * 'show_all_printers()' - Show all printers...
291 */
292
293 static void
294 show_all_printers(http_t *http, /* I - Connection to server */
295 const char *user) /* I - Username */
296 {
297 int i; /* Looping var */
298 ipp_t *request, /* IPP request */
299 *response; /* IPP response */
300 cups_array_t *printers; /* Array of printer objects */
301 ipp_attribute_t *printer; /* Printer object */
302 int ascending, /* Order of printers (0 = descending) */
303 first, /* First printer to show */
304 count; /* Number of printers */
305 const char *var; /* Form variable */
306 void *search; /* Search data */
307 char val[1024]; /* Form variable */
308
309
310 fprintf(stderr, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
311 http, user ? user : "(null)");
312
313 /*
314 * Show the standard header...
315 */
316
317 cgiStartHTML(cgiText(_("Printers")));
318
319 /*
320 * Build a CUPS_GET_PRINTERS request, which requires the following
321 * attributes:
322 *
323 * attributes-charset
324 * attributes-natural-language
325 * printer-type
326 * printer-type-mask
327 * requesting-user-name
328 */
329
330 request = ippNewRequest(CUPS_GET_PRINTERS);
331
332 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
333 "printer-type", 0);
334 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
335 "printer-type-mask", CUPS_PRINTER_CLASS);
336
337 if (user)
338 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
339 "requesting-user-name", NULL, user);
340
341 cgiGetAttributes(request, "printers.tmpl");
342
343 /*
344 * Do the request and get back a response...
345 */
346
347 if ((response = cupsDoRequest(http, request, "/")) != NULL)
348 {
349 /*
350 * Get a list of matching job objects.
351 */
352
353 if ((var = cgiGetVariable("QUERY")) != NULL &&
354 !cgiGetVariable("CLEAR"))
355 search = cgiCompileSearch(var);
356 else
357 search = NULL;
358
359 printers = cgiGetIPPObjects(response, search);
360 count = cupsArrayCount(printers);
361
362 if (search)
363 cgiFreeSearch(search);
364
365 /*
366 * Figure out which printers to display...
367 */
368
369 if ((var = cgiGetVariable("FIRST")) != NULL)
370 first = atoi(var);
371 else
372 first = 0;
373
374 if (first >= count)
375 first = count - CUPS_PAGE_MAX;
376
377 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
378
379 if (first < 0)
380 first = 0;
381
382 sprintf(val, "%d", count);
383 cgiSetVariable("TOTAL", val);
384
385 if ((var = cgiGetVariable("ORDER")) != NULL)
386 ascending = !strcasecmp(var, "asc");
387 else
388 ascending = 1;
389
390 if (ascending)
391 {
392 for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, first);
393 i < CUPS_PAGE_MAX && printer;
394 i ++, printer = (ipp_attribute_t *)cupsArrayNext(printers))
395 cgiSetIPPObjectVars(printer, NULL, i);
396 }
397 else
398 {
399 for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, count - first - 1);
400 i < CUPS_PAGE_MAX && printer;
401 i ++, printer = (ipp_attribute_t *)cupsArrayPrev(printers))
402 cgiSetIPPObjectVars(printer, NULL, i);
403 }
404
405 /*
406 * Save navigation URLs...
407 */
408
409 cgiSetVariable("THISURL", "/printers/");
410
411 if (first > 0)
412 {
413 sprintf(val, "%d", first - CUPS_PAGE_MAX);
414 cgiSetVariable("PREV", val);
415 }
416
417 if ((first + CUPS_PAGE_MAX) < count)
418 {
419 sprintf(val, "%d", first + CUPS_PAGE_MAX);
420 cgiSetVariable("NEXT", val);
421 }
422
423 /*
424 * Then show everything...
425 */
426
427 cgiCopyTemplateLang("search.tmpl");
428
429 cgiCopyTemplateLang("printers-header.tmpl");
430
431 if (count > CUPS_PAGE_MAX)
432 cgiCopyTemplateLang("pager.tmpl");
433
434 cgiCopyTemplateLang("printers.tmpl");
435
436 if (count > CUPS_PAGE_MAX)
437 cgiCopyTemplateLang("pager.tmpl");
438
439 /*
440 * Delete the response...
441 */
442
443 cupsArrayDelete(printers);
444 ippDelete(response);
445 }
446 else
447 {
448 /*
449 * Show the error...
450 */
451
452 cgiShowIPPError(_("Unable to get printer list:"));
453 }
454
455 cgiEndHTML();
456 }
457
458
459 /*
460 * 'show_printer()' - Show a single printer.
461 */
462
463 static void
464 show_printer(http_t *http, /* I - Connection to server */
465 const char *printer) /* I - Name of printer */
466 {
467 ipp_t *request, /* IPP request */
468 *response; /* IPP response */
469 ipp_attribute_t *attr; /* IPP attribute */
470 char uri[HTTP_MAX_URI]; /* Printer URI */
471 char refresh[1024]; /* Refresh URL */
472
473
474 fprintf(stderr, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
475 http, printer ? printer : "(null)");
476
477 /*
478 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
479 * attributes:
480 *
481 * attributes-charset
482 * attributes-natural-language
483 * printer-uri
484 */
485
486 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
487
488 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
489 "localhost", 0, "/printers/%s", printer);
490 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
491 uri);
492
493 cgiGetAttributes(request, "printer.tmpl");
494
495 /*
496 * Do the request and get back a response...
497 */
498
499 if ((response = cupsDoRequest(http, request, "/")) != NULL)
500 {
501 /*
502 * Got the result; set the CGI variables and check the status of a
503 * single-queue request...
504 */
505
506 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
507
508 if (printer && (attr = ippFindAttribute(response, "printer-state",
509 IPP_TAG_ENUM)) != NULL &&
510 attr->values[0].integer == IPP_PRINTER_PROCESSING)
511 {
512 /*
513 * Printer is processing - automatically refresh the page until we
514 * are done printing...
515 */
516
517 cgiFormEncode(uri, printer, sizeof(uri));
518 snprintf(refresh, sizeof(refresh), "10;URL=/printers/%s", uri);
519 cgiSetVariable("refresh_page", refresh);
520 }
521
522 /*
523 * Delete the response...
524 */
525
526 ippDelete(response);
527
528 /*
529 * Show the standard header...
530 */
531
532 cgiStartHTML(printer);
533
534 /*
535 * Show the printer status...
536 */
537
538 cgiCopyTemplateLang("printer.tmpl");
539
540 /*
541 * Show jobs for the specified printer...
542 */
543
544 cgiCopyTemplateLang("printer-jobs-header.tmpl");
545 cgiShowJobs(http, printer);
546 }
547 else
548 {
549 /*
550 * Show the IPP error...
551 */
552
553 cgiStartHTML(printer);
554 cgiShowIPPError(_("Unable to get printer status:"));
555 }
556
557 cgiEndHTML();
558 }
559
560
561 /*
562 * End of "$Id: printers.c 7940 2008-09-16 00:45:16Z mike $".
563 */