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