]> 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/*
f7deaa1a 2 * "$Id: printers.c 5571 2006-05-22 18:46:55Z 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
f301802f 101 user = getenv("REMOTE_USER");
ef416fc2 102
103 /*
fa73b229 104 * Connect to the HTTP server...
ef416fc2 105 */
106
fa73b229 107 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
ef416fc2 108
fa73b229 109 /*
110 * Get the default printer...
111 */
ef416fc2 112
fa73b229 113 if (!op)
ef416fc2 114 {
115 /*
116 * Get the default destination...
117 */
118
fa73b229 119 request = ippNewRequest(CUPS_GET_DEFAULT);
ef416fc2 120
121 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
122 "requested-attributes",
123 sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
124
125 if ((response = cupsDoRequest(http, request, "/")) != NULL)
126 {
127 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
128 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
129
130 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
131 {
132 char url[HTTP_MAX_URI]; /* New URL */
133
134
135 cgiSetVariable("DEFAULT_URI",
136 cgiRewriteURL(attr->values[0].string.text,
137 url, sizeof(url), NULL));
138 }
139
140 ippDelete(response);
141 }
142
143 /*
fa73b229 144 * See if we need to show a list of printers or the status of a
145 * single printer...
ef416fc2 146 */
147
fa73b229 148 if (!printer)
149 show_all_printers(http, user);
150 else
151 show_printer(http, printer);
152 }
b423cd4c 153 else if (!strcasecmp(op, "print-self-test-page") && printer)
154 print_command(http, printer, "PrintSelfTestPage");
155 else if (!strcasecmp(op, "clean-print-heads") && printer)
156 print_command(http, printer, "Clean all");
fa73b229 157 else if (!strcasecmp(op, "print-test-page") && printer)
158 cgiPrintTestPage(http, printer);
159 else if (!strcasecmp(op, "move-jobs") && printer)
160 cgiMoveJobs(http, printer, 0);
161 else
162 {
163 /*
164 * Unknown/bad operation...
165 */
ef416fc2 166
fa73b229 167 if (printer)
168 cgiStartHTML(printer);
169 else
170 cgiStartHTML(cgiText(_("Printers")));
ef416fc2 171
fa73b229 172 cgiCopyTemplateLang("error-op.tmpl");
173 cgiEndHTML();
174 }
ef416fc2 175
fa73b229 176 /*
177 * Close the HTTP server connection...
178 */
179
180 httpClose(http);
181
182 /*
183 * Return with no errors...
184 */
ef416fc2 185
fa73b229 186 return (0);
187}
ef416fc2 188
ef416fc2 189
b423cd4c 190/*
191 * 'print_command()' - Send a print command to the printer.
192 */
193
194void
195print_command(http_t *http, /* I - Connection to server */
196 const char *printer, /* I - Printer */
197 const char *command) /* I - Command to send */
198{
199 cups_file_t *fp; /* File pointer */
200 char filename[1024]; /* Temporary file */
201 ipp_t *request, /* IPP request */
202 *response; /* IPP response */
203 char uri[HTTP_MAX_URI], /* Printer URI */
204 resource[1024], /* POST resource path */
205 refresh[1024]; /* Refresh URL */
206 const char *user; /* Username */
207
208
209 /*
210 * See who is logged in...
211 */
212
213 if ((user = getenv("REMOTE_USER")) == NULL)
214 user = "guest";
215
216 /*
217 * Create the CUPS command file to print...
218 */
219
220 if ((fp = cupsTempFile2(filename, sizeof(filename))) == NULL)
221 {
222 cgiStartHTML(cgiText(_("Printer Maintenance")));
223 cgiSetVariable("MESSAGE", _("Unable to create temporary file:"));
224 cgiSetVariable("ERROR", strerror(errno));
225 cgiCopyTemplateLang("error.tmpl");
226 cgiEndHTML();
227 return;
228 }
229
230 cupsFilePuts(fp, "#CUPS-COMMAND\n");
231 cupsFilePrintf(fp, "%s\n", command);
232 cupsFileClose(fp);
233
234 /*
235 * Point to the printer...
236 */
237
238 snprintf(resource, sizeof(resource), "/printers/%s", printer);
239
240 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
241 "localhost", ippPort(), "/printers/%s", printer);
242
243 /*
244 * Build an IPP_PRINT_JOB request, which requires the following
245 * attributes:
246 *
247 * attributes-charset
248 * attributes-natural-language
249 * printer-uri
250 * requesting-user-name
251 * document-format
252 */
253
254 request = ippNewRequest(IPP_PRINT_JOB);
255
256 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
257 NULL, uri);
258
259 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
260 "requesting-user-name", NULL, user);
261
262 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
263 NULL, "Printer Maintenance");
264
265 ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
266 NULL, "application/postscript");
267
268 /*
269 * Do the request and get back a response...
270 */
271
272 if ((response = cupsDoFileRequest(http, request, resource,
273 filename)) != NULL)
274 {
275 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
276
277 ippDelete(response);
278 }
279
280 unlink(filename);
281
282 if (cupsLastError() <= IPP_OK_CONFLICT)
283 {
284 /*
285 * Automatically reload the printer status page...
286 */
287
288 cgiFormEncode(uri, resource, sizeof(uri));
f301802f 289 snprintf(refresh, sizeof(refresh), "2;URL=%s", uri);
b423cd4c 290 cgiSetVariable("refresh_page", refresh);
291 }
292
293 cgiStartHTML(cgiText(_("Printer Maintenance")));
294
295 if (cupsLastError() > IPP_OK_CONFLICT)
296 cgiShowIPPError(_("Unable to send maintenance job:"));
297 else
298 {
299 cgiSetVariable("PRINTER_NAME", printer);
300
301 cgiCopyTemplateLang("maintenance.tmpl");
302 }
303
304 cgiEndHTML();
305}
306
307
fa73b229 308/*
309 * 'show_all_printers()' - Show all printers...
310 */
ef416fc2 311
fa73b229 312void
313show_all_printers(http_t *http, /* I - Connection to server */
314 const char *user) /* I - Username */
315{
316 int i; /* Looping var */
317 ipp_t *request, /* IPP request */
318 *response; /* IPP response */
319 cups_array_t *printers; /* Array of printer objects */
b423cd4c 320 ipp_attribute_t *printer, /* Printer object */
321 *attr; /* Current attribute */
fa73b229 322 int ascending, /* Order of printers (0 = descending) */
323 first, /* First printer to show */
324 count; /* Number of printers */
325 const char *var; /* Form variable */
326 void *search; /* Search data */
327 char url[1024], /* URL for prev/next/this */
328 *urlptr, /* Position in URL */
329 *urlend; /* End of URL */
ef416fc2 330
ef416fc2 331
bd7854cb 332 fprintf(stderr, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
f301802f 333 http, user ? user : "(null)");
bd7854cb 334
fa73b229 335 /*
336 * Show the standard header...
337 */
ef416fc2 338
fa73b229 339 cgiStartHTML(cgiText(_("Printers")));
ef416fc2 340
fa73b229 341 /*
342 * Build a CUPS_GET_PRINTERS request, which requires the following
343 * attributes:
344 *
345 * attributes-charset
346 * attributes-natural-language
347 * printer-type
348 * printer-type-mask
349 * requesting-user-name
350 */
ef416fc2 351
fa73b229 352 request = ippNewRequest(CUPS_GET_PRINTERS);
ef416fc2 353
fa73b229 354 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
355 "printer-type", 0);
356 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
357 "printer-type-mask", CUPS_PRINTER_CLASS);
ef416fc2 358
f301802f 359 if (user)
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",
f301802f 546 http, printer ? printer : "(null)");
bd7854cb 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));
f301802f 597 snprintf(refresh, sizeof(refresh), "10;URL=/printers/%s", uri);
fa73b229 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/*
f7deaa1a 641 * End of "$Id: printers.c 5571 2006-05-22 18:46:55Z mike $".
ef416fc2 642 */