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