]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/printers.c
Load cups into easysw/current.
[thirdparty/cups.git] / cgi-bin / printers.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: printers.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 3 *
4 * Printer status CGI for the Common UNIX Printing System (CUPS).
5 *
bc44d920 6 * Copyright 2007 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 *
fa73b229 17 * main() - Main entry for CGI.
b423cd4c 18 * print_command() - Send a print command to the printer.
fa73b229 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
b423cd4c 35void print_command(http_t *http, const char *printer, const char *command);
fa73b229 36void show_all_printers(http_t *http, const char *username);
37void show_printer(http_t *http, const char *printer);
38
39
ef416fc2 40/*
41 * 'main()' - Main entry for CGI.
42 */
43
44int /* O - Exit status */
45main(int argc, /* I - Number of command-line arguments */
46 char *argv[]) /* I - Command-line arguments */
47{
fa73b229 48 const char *printer; /* Printer name */
49 const char *user; /* Username */
ef416fc2 50 http_t *http; /* Connection to the server */
51 ipp_t *request, /* IPP request */
52 *response; /* IPP response */
53 ipp_attribute_t *attr; /* IPP attribute */
ef416fc2 54 const char *op; /* Operation to perform, if any */
ef416fc2 55 static const char *def_attrs[] = /* Attributes for default printer */
56 {
57 "printer-name",
58 "printer-uri-supported"
59 };
60
61
62 /*
63 * Get any form variables...
64 */
65
66 cgiInitialize();
ef416fc2 67
fa73b229 68 op = cgiGetVariable("OP");
ef416fc2 69
70 /*
71 * Set the web interface section...
72 */
73
74 cgiSetVariable("SECTION", "printers");
75
76 /*
fa73b229 77 * See if we are displaying a printer or all printers...
ef416fc2 78 */
79
b423cd4c 80 if ((printer = getenv("PATH_INFO")) != NULL)
4744bd90 81 {
b423cd4c 82 printer ++;
ef416fc2 83
4744bd90 84 if (!*printer)
85 printer = NULL;
86 }
87
ef416fc2 88 /*
fa73b229 89 * See who is logged in...
ef416fc2 90 */
91
f301802f 92 user = getenv("REMOTE_USER");
ef416fc2 93
94 /*
fa73b229 95 * Connect to the HTTP server...
ef416fc2 96 */
97
fa73b229 98 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
ef416fc2 99
fa73b229 100 /*
101 * Get the default printer...
102 */
ef416fc2 103
fa73b229 104 if (!op)
ef416fc2 105 {
106 /*
107 * Get the default destination...
108 */
109
fa73b229 110 request = ippNewRequest(CUPS_GET_DEFAULT);
ef416fc2 111
112 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
113 "requested-attributes",
114 sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
115
116 if ((response = cupsDoRequest(http, request, "/")) != NULL)
117 {
118 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
119 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
120
121 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
122 {
123 char url[HTTP_MAX_URI]; /* New URL */
124
125
126 cgiSetVariable("DEFAULT_URI",
127 cgiRewriteURL(attr->values[0].string.text,
128 url, sizeof(url), NULL));
129 }
130
131 ippDelete(response);
132 }
133
134 /*
fa73b229 135 * See if we need to show a list of printers or the status of a
136 * single printer...
ef416fc2 137 */
138
fa73b229 139 if (!printer)
140 show_all_printers(http, user);
141 else
142 show_printer(http, printer);
143 }
b423cd4c 144 else if (!strcasecmp(op, "print-self-test-page") && printer)
145 print_command(http, printer, "PrintSelfTestPage");
146 else if (!strcasecmp(op, "clean-print-heads") && printer)
147 print_command(http, printer, "Clean all");
fa73b229 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
b423cd4c 181/*
182 * 'print_command()' - Send a print command to the printer.
183 */
184
185void
186print_command(http_t *http, /* I - Connection to server */
187 const char *printer, /* I - Printer */
188 const char *command) /* I - Command to send */
189{
190 cups_file_t *fp; /* File pointer */
191 char filename[1024]; /* Temporary file */
192 ipp_t *request, /* IPP request */
193 *response; /* IPP response */
194 char uri[HTTP_MAX_URI], /* Printer URI */
195 resource[1024], /* POST resource path */
196 refresh[1024]; /* Refresh URL */
197 const char *user; /* Username */
198
199
200 /*
201 * See who is logged in...
202 */
203
204 if ((user = getenv("REMOTE_USER")) == NULL)
205 user = "guest";
206
207 /*
208 * Create the CUPS command file to print...
209 */
210
211 if ((fp = cupsTempFile2(filename, sizeof(filename))) == NULL)
212 {
213 cgiStartHTML(cgiText(_("Printer Maintenance")));
214 cgiSetVariable("MESSAGE", _("Unable to create temporary file:"));
215 cgiSetVariable("ERROR", strerror(errno));
216 cgiCopyTemplateLang("error.tmpl");
217 cgiEndHTML();
218 return;
219 }
220
221 cupsFilePuts(fp, "#CUPS-COMMAND\n");
222 cupsFilePrintf(fp, "%s\n", command);
223 cupsFileClose(fp);
224
225 /*
226 * Point to the printer...
227 */
228
229 snprintf(resource, sizeof(resource), "/printers/%s", printer);
230
231 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
232 "localhost", ippPort(), "/printers/%s", printer);
233
234 /*
235 * Build an IPP_PRINT_JOB request, which requires the following
236 * attributes:
237 *
238 * attributes-charset
239 * attributes-natural-language
240 * printer-uri
241 * requesting-user-name
242 * document-format
243 */
244
245 request = ippNewRequest(IPP_PRINT_JOB);
246
247 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
248 NULL, uri);
249
250 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
251 "requesting-user-name", NULL, user);
252
253 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
254 NULL, "Printer Maintenance");
255
256 ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
257 NULL, "application/postscript");
258
259 /*
260 * Do the request and get back a response...
261 */
262
263 if ((response = cupsDoFileRequest(http, request, resource,
264 filename)) != NULL)
265 {
266 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
267
268 ippDelete(response);
269 }
270
271 unlink(filename);
272
273 if (cupsLastError() <= IPP_OK_CONFLICT)
274 {
275 /*
276 * Automatically reload the printer status page...
277 */
278
279 cgiFormEncode(uri, resource, sizeof(uri));
f301802f 280 snprintf(refresh, sizeof(refresh), "2;URL=%s", uri);
b423cd4c 281 cgiSetVariable("refresh_page", refresh);
282 }
283
284 cgiStartHTML(cgiText(_("Printer Maintenance")));
285
286 if (cupsLastError() > IPP_OK_CONFLICT)
287 cgiShowIPPError(_("Unable to send maintenance job:"));
288 else
289 {
290 cgiSetVariable("PRINTER_NAME", printer);
291
292 cgiCopyTemplateLang("maintenance.tmpl");
293 }
294
295 cgiEndHTML();
296}
297
298
fa73b229 299/*
300 * 'show_all_printers()' - Show all printers...
301 */
ef416fc2 302
fa73b229 303void
304show_all_printers(http_t *http, /* I - Connection to server */
305 const char *user) /* I - Username */
306{
307 int i; /* Looping var */
308 ipp_t *request, /* IPP request */
309 *response; /* IPP response */
310 cups_array_t *printers; /* Array of printer objects */
b423cd4c 311 ipp_attribute_t *printer, /* Printer object */
312 *attr; /* Current attribute */
fa73b229 313 int ascending, /* Order of printers (0 = descending) */
314 first, /* First printer to show */
315 count; /* Number of printers */
316 const char *var; /* Form variable */
317 void *search; /* Search data */
318 char url[1024], /* URL for prev/next/this */
319 *urlptr, /* Position in URL */
320 *urlend; /* End of URL */
ef416fc2 321
ef416fc2 322
bd7854cb 323 fprintf(stderr, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
f301802f 324 http, user ? user : "(null)");
bd7854cb 325
fa73b229 326 /*
327 * Show the standard header...
328 */
ef416fc2 329
fa73b229 330 cgiStartHTML(cgiText(_("Printers")));
ef416fc2 331
fa73b229 332 /*
333 * Build a CUPS_GET_PRINTERS request, which requires the following
334 * attributes:
335 *
336 * attributes-charset
337 * attributes-natural-language
338 * printer-type
339 * printer-type-mask
340 * requesting-user-name
341 */
ef416fc2 342
fa73b229 343 request = ippNewRequest(CUPS_GET_PRINTERS);
ef416fc2 344
fa73b229 345 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
346 "printer-type", 0);
347 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
348 "printer-type-mask", CUPS_PRINTER_CLASS);
ef416fc2 349
f301802f 350 if (user)
351 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
352 "requesting-user-name", NULL, user);
ef416fc2 353
fa73b229 354 cgiGetAttributes(request, "printers.tmpl");
ef416fc2 355
fa73b229 356 /*
357 * Do the request and get back a response...
358 */
359
360 if ((response = cupsDoRequest(http, request, "/")) != NULL)
361 {
ef416fc2 362 /*
fa73b229 363 * Get a list of matching job objects.
ef416fc2 364 */
365
fa73b229 366 if ((var = cgiGetVariable("QUERY")) != NULL)
367 search = cgiCompileSearch(var);
368 else
369 search = NULL;
ef416fc2 370
fa73b229 371 printers = cgiGetIPPObjects(response, search);
372 count = cupsArrayCount(printers);
ef416fc2 373
fa73b229 374 if (search)
375 cgiFreeSearch(search);
ef416fc2 376
377 /*
fa73b229 378 * Figure out which printers to display...
ef416fc2 379 */
380
fa73b229 381 if ((var = cgiGetVariable("FIRST")) != NULL)
382 first = atoi(var);
383 else
384 first = 0;
ef416fc2 385
fa73b229 386 if (first >= count)
387 first = count - CUPS_PAGE_MAX;
ef416fc2 388
fa73b229 389 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
ef416fc2 390
fa73b229 391 if (first < 0)
392 first = 0;
ef416fc2 393
fa73b229 394 sprintf(url, "%d", count);
395 cgiSetVariable("TOTAL", url);
ef416fc2 396
fa73b229 397 if ((var = cgiGetVariable("ORDER")) != NULL)
398 ascending = !strcasecmp(var, "asc");
399 else
400 ascending = 1;
ef416fc2 401
fa73b229 402 if (ascending)
403 {
404 for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, first);
405 i < CUPS_PAGE_MAX && printer;
406 i ++, printer = (ipp_attribute_t *)cupsArrayNext(printers))
b423cd4c 407 {
fa73b229 408 cgiSetIPPObjectVars(printer, NULL, i);
b423cd4c 409
410 cgiSetArray("cupscommand", i, "0");
411
412 for (attr = printer; attr; attr = attr->next)
413 if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
414 break;
415 else if (!strcmp(attr->name, "printer-type"))
416 {
417 if (attr->values[0].integer & CUPS_PRINTER_COMMANDS)
418 cgiSetArray("cupscommand", i, "1");
419 break;
420 }
421 }
fa73b229 422 }
423 else
424 {
425 for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, count - first - 1);
426 i < CUPS_PAGE_MAX && printer;
427 i ++, printer = (ipp_attribute_t *)cupsArrayPrev(printers))
b423cd4c 428 {
fa73b229 429 cgiSetIPPObjectVars(printer, NULL, i);
b423cd4c 430
431 cgiSetArray("cupscommand", i, "0");
432
433 for (attr = printer; attr; attr = attr->next)
434 if (attr->group_tag == IPP_TAG_ZERO || !attr->name)
435 break;
436 else if (!strcmp(attr->name, "printer-type"))
437 {
438 if (attr->values[0].integer & CUPS_PRINTER_COMMANDS)
439 cgiSetArray("cupscommand", i, "1");
440 break;
441 }
442 }
fa73b229 443 }
ef416fc2 444
fa73b229 445 /*
446 * Save navigation URLs...
447 */
ef416fc2 448
fa73b229 449 urlend = url + sizeof(url);
ef416fc2 450
fa73b229 451 if ((var = cgiGetVariable("QUERY")) != NULL)
452 {
453 strlcpy(url, "/printers/?QUERY=", sizeof(url));
454 urlptr = url + strlen(url);
ef416fc2 455
fa73b229 456 cgiFormEncode(urlptr, var, urlend - urlptr);
457 urlptr += strlen(urlptr);
ef416fc2 458
fa73b229 459 strlcpy(urlptr, "&", urlend - urlptr);
460 urlptr += strlen(urlptr);
461 }
462 else
463 {
464 strlcpy(url, "/printers/?", sizeof(url));
465 urlptr = url + strlen(url);
466 }
ef416fc2 467
fa73b229 468 snprintf(urlptr, urlend - urlptr, "FIRST=%d", first);
469 cgiSetVariable("THISURL", url);
470
471 if (first > 0)
472 {
473 snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
474 first - CUPS_PAGE_MAX, ascending ? "asc" : "dec");
475 cgiSetVariable("PREVURL", url);
ef416fc2 476 }
fa73b229 477
478 if ((first + CUPS_PAGE_MAX) < count)
479 {
480 snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
481 first + CUPS_PAGE_MAX, ascending ? "asc" : "dec");
482 cgiSetVariable("NEXTURL", url);
483 }
484
ef416fc2 485 /*
fa73b229 486 * Then show everything...
ef416fc2 487 */
488
fa73b229 489 cgiCopyTemplateLang("search.tmpl");
ef416fc2 490
fa73b229 491 cgiCopyTemplateLang("printers-header.tmpl");
ef416fc2 492
fa73b229 493 if (count > 0)
494 cgiCopyTemplateLang("pager.tmpl");
ef416fc2 495
fa73b229 496 cgiCopyTemplateLang("printers.tmpl");
ef416fc2 497
fa73b229 498 if (count > 0)
499 cgiCopyTemplateLang("pager.tmpl");
ef416fc2 500
501 /*
fa73b229 502 * Delete the response...
ef416fc2 503 */
504
bd7854cb 505 cupsArrayDelete(printers);
fa73b229 506 ippDelete(response);
507 }
508 else
509 {
510 /*
511 * Show the error...
512 */
ef416fc2 513
fa73b229 514 cgiShowIPPError(_("Unable to get printer list:"));
515 }
ef416fc2 516
fa73b229 517 cgiEndHTML();
518}
ef416fc2 519
ef416fc2 520
fa73b229 521/*
522 * 'show_printer()' - Show a single printer.
523 */
ef416fc2 524
fa73b229 525void
526show_printer(http_t *http, /* I - Connection to server */
527 const char *printer) /* I - Name of printer */
528{
529 ipp_t *request, /* IPP request */
530 *response; /* IPP response */
531 ipp_attribute_t *attr; /* IPP attribute */
532 char uri[HTTP_MAX_URI]; /* Printer URI */
533 char refresh[1024]; /* Refresh URL */
ef416fc2 534
ef416fc2 535
bd7854cb 536 fprintf(stderr, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
f301802f 537 http, printer ? printer : "(null)");
bd7854cb 538
fa73b229 539 /*
540 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
541 * attributes:
542 *
543 * attributes-charset
544 * attributes-natural-language
545 * printer-uri
546 */
547
548 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
549
a4d04587 550 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
551 "localhost", 0, "/printers/%s", printer);
fa73b229 552 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
553 uri);
554
555 cgiGetAttributes(request, "printers.tmpl");
556
557 /*
558 * Do the request and get back a response...
559 */
ef416fc2 560
fa73b229 561 if ((response = cupsDoRequest(http, request, "/")) != NULL)
562 {
ef416fc2 563 /*
fa73b229 564 * Got the result; set the CGI variables and check the status of a
565 * single-queue request...
ef416fc2 566 */
567
fa73b229 568 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
569
b423cd4c 570 if ((attr = ippFindAttribute(response, "printer-type",
571 IPP_TAG_ENUM)) != NULL)
572 {
573 cgiSetVariable("cupscommand",
574 (attr->values[0].integer & CUPS_PRINTER_COMMANDS) ?
575 "1" : "0");
576 }
577
fa73b229 578 if (printer && (attr = ippFindAttribute(response, "printer-state",
579 IPP_TAG_ENUM)) != NULL &&
580 attr->values[0].integer == IPP_PRINTER_PROCESSING)
ef416fc2 581 {
fa73b229 582 /*
583 * Printer is processing - automatically refresh the page until we
584 * are done printing...
585 */
ef416fc2 586
fa73b229 587 cgiFormEncode(uri, printer, sizeof(uri));
f301802f 588 snprintf(refresh, sizeof(refresh), "10;URL=/printers/%s", uri);
fa73b229 589 cgiSetVariable("refresh_page", refresh);
ef416fc2 590 }
ef416fc2 591
fa73b229 592 /*
593 * Delete the response...
594 */
595
596 ippDelete(response);
ef416fc2 597
598 /*
599 * Show the standard header...
600 */
601
fa73b229 602 cgiStartHTML(printer);
ef416fc2 603
604 /*
fa73b229 605 * Show the printer status...
ef416fc2 606 */
607
fa73b229 608 cgiCopyTemplateLang("printers.tmpl");
ef416fc2 609
fa73b229 610 /*
611 * Show jobs for the specified printer...
612 */
ef416fc2 613
fa73b229 614 cgiCopyTemplateLang("printer-jobs-header.tmpl");
615 cgiShowJobs(http, printer);
616 }
617 else
618 {
619 /*
620 * Show the IPP error...
621 */
ef416fc2 622
fa73b229 623 cgiStartHTML(printer);
624 cgiShowIPPError(_("Unable to get printer status:"));
625 }
ef416fc2 626
fa73b229 627 cgiEndHTML();
ef416fc2 628}
629
630
631/*
bc44d920 632 * End of "$Id: printers.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 633 */