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