2 * "$Id: printers.c 6889 2007-08-29 22:23:35Z mike $"
4 * Printer status CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
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/".
17 * main() - Main entry for CGI.
18 * print_command() - Send a print command to the printer.
19 * show_all_printers() - Show all printers...
20 * show_printer() - Show a single printer.
24 * Include necessary headers...
27 #include "cgi-private.h"
35 void print_command(http_t
*http
, const char *printer
, const char *command
);
36 void show_all_printers(http_t
*http
, const char *username
);
37 void show_printer(http_t
*http
, const char *printer
);
41 * 'main()' - Main entry for CGI.
44 int /* O - Exit status */
45 main(int argc
, /* I - Number of command-line arguments */
46 char *argv
[]) /* I - Command-line arguments */
48 const char *printer
; /* Printer name */
49 const char *user
; /* Username */
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 */
54 const char *op
; /* Operation to perform, if any */
55 static const char *def_attrs
[] = /* Attributes for default printer */
58 "printer-uri-supported"
63 * Get any form variables...
68 op
= cgiGetVariable("OP");
71 * Set the web interface section...
74 cgiSetVariable("SECTION", "printers");
77 * See if we are displaying a printer or all printers...
80 if ((printer
= getenv("PATH_INFO")) != NULL
)
89 * See who is logged in...
92 user
= getenv("REMOTE_USER");
95 * Connect to the HTTP server...
98 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
101 * Get the default printer...
104 if (!op
|| !cgiIsPOST())
107 * Get the default destination...
110 request
= ippNewRequest(CUPS_GET_DEFAULT
);
112 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
113 "requested-attributes",
114 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
116 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
118 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
119 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
121 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
123 char url
[HTTP_MAX_URI
]; /* New URL */
126 cgiSetVariable("DEFAULT_URI",
127 cgiRewriteURL(attr
->values
[0].string
.text
,
128 url
, sizeof(url
), NULL
));
135 * See if we need to show a list of printers or the status of a
140 show_all_printers(http
, user
);
142 show_printer(http
, printer
);
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");
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);
155 * Unknown/bad operation...
159 cgiStartHTML(printer
);
161 cgiStartHTML(cgiText(_("Printers")));
163 cgiCopyTemplateLang("error-op.tmpl");
168 * Close the HTTP server connection...
174 * Return with no errors...
182 * 'print_command()' - Send a print command to the printer.
186 print_command(http_t
*http
, /* I - Connection to server */
187 const char *printer
, /* I - Printer */
188 const char *command
) /* I - Command to send */
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 */
201 * See who is logged in...
204 if ((user
= getenv("REMOTE_USER")) == NULL
)
208 * Create the CUPS command file to print...
211 if ((fp
= cupsTempFile2(filename
, sizeof(filename
))) == NULL
)
213 cgiStartHTML(cgiText(_("Printer Maintenance")));
214 cgiSetVariable("MESSAGE", _("Unable to create temporary file:"));
215 cgiSetVariable("ERROR", strerror(errno
));
216 cgiCopyTemplateLang("error.tmpl");
221 cupsFilePuts(fp
, "#CUPS-COMMAND\n");
222 cupsFilePrintf(fp
, "%s\n", command
);
226 * Point to the printer...
229 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
);
231 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
232 "localhost", ippPort(), "/printers/%s", printer
);
235 * Build an IPP_PRINT_JOB request, which requires the following
239 * attributes-natural-language
241 * requesting-user-name
245 request
= ippNewRequest(IPP_PRINT_JOB
);
247 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
250 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
251 "requesting-user-name", NULL
, user
);
253 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
254 NULL
, "Printer Maintenance");
256 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
257 NULL
, "application/postscript");
260 * Do the request and get back a response...
263 if ((response
= cupsDoFileRequest(http
, request
, resource
,
266 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
273 if (cupsLastError() <= IPP_OK_CONFLICT
)
276 * Automatically reload the printer status page...
279 cgiFormEncode(uri
, resource
, sizeof(uri
));
280 snprintf(refresh
, sizeof(refresh
), "2;URL=%s", uri
);
281 cgiSetVariable("refresh_page", refresh
);
283 else if (cupsLastError() == IPP_NOT_AUTHORIZED
)
285 puts("Status: 401\n");
289 cgiStartHTML(cgiText(_("Printer Maintenance")));
291 if (cupsLastError() > IPP_OK_CONFLICT
)
292 cgiShowIPPError(_("Unable to send maintenance job:"));
295 cgiSetVariable("PRINTER_NAME", printer
);
297 cgiCopyTemplateLang("maintenance.tmpl");
305 * 'show_all_printers()' - Show all printers...
309 show_all_printers(http_t
*http
, /* I - Connection to server */
310 const char *user
) /* I - Username */
312 int i
; /* Looping var */
313 ipp_t
*request
, /* IPP request */
314 *response
; /* IPP response */
315 cups_array_t
*printers
; /* Array of printer objects */
316 ipp_attribute_t
*printer
, /* Printer object */
317 *attr
; /* Current attribute */
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 val
[1024]; /* Form variable */
326 fprintf(stderr
, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
327 http
, user
? user
: "(null)");
330 * Show the standard header...
333 cgiStartHTML(cgiText(_("Printers")));
336 * Build a CUPS_GET_PRINTERS request, which requires the following
340 * attributes-natural-language
343 * requesting-user-name
346 request
= ippNewRequest(CUPS_GET_PRINTERS
);
348 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
350 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
351 "printer-type-mask", CUPS_PRINTER_CLASS
);
354 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
355 "requesting-user-name", NULL
, user
);
357 cgiGetAttributes(request
, "printers.tmpl");
360 * Do the request and get back a response...
363 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
366 * Get a list of matching job objects.
369 if ((var
= cgiGetVariable("QUERY")) != NULL
&&
370 !cgiGetVariable("CLEAR"))
371 search
= cgiCompileSearch(var
);
375 printers
= cgiGetIPPObjects(response
, search
);
376 count
= cupsArrayCount(printers
);
379 cgiFreeSearch(search
);
382 * Figure out which printers to display...
385 if ((var
= cgiGetVariable("FIRST")) != NULL
)
391 first
= count
- CUPS_PAGE_MAX
;
393 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
398 sprintf(val
, "%d", count
);
399 cgiSetVariable("TOTAL", val
);
401 if ((var
= cgiGetVariable("ORDER")) != NULL
)
402 ascending
= !strcasecmp(var
, "asc");
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
))
412 cgiSetIPPObjectVars(printer
, NULL
, i
);
414 cgiSetArray("cupscommand", i
, "0");
416 for (attr
= printer
; attr
; attr
= attr
->next
)
417 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
419 else if (!strcmp(attr
->name
, "printer-type"))
421 if (attr
->values
[0].integer
& CUPS_PRINTER_COMMANDS
)
422 cgiSetArray("cupscommand", i
, "1");
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
))
433 cgiSetIPPObjectVars(printer
, NULL
, i
);
435 cgiSetArray("cupscommand", i
, "0");
437 for (attr
= printer
; attr
; attr
= attr
->next
)
438 if (attr
->group_tag
== IPP_TAG_ZERO
|| !attr
->name
)
440 else if (!strcmp(attr
->name
, "printer-type"))
442 if (attr
->values
[0].integer
& CUPS_PRINTER_COMMANDS
)
443 cgiSetArray("cupscommand", i
, "1");
450 * Save navigation URLs...
453 cgiSetVariable("THISURL", "/printers/");
457 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
458 cgiSetVariable("PREV", val
);
461 if ((first
+ CUPS_PAGE_MAX
) < count
)
463 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
464 cgiSetVariable("NEXT", val
);
468 * Then show everything...
471 cgiCopyTemplateLang("search.tmpl");
473 cgiCopyTemplateLang("printers-header.tmpl");
476 cgiCopyTemplateLang("pager.tmpl");
478 cgiCopyTemplateLang("printers.tmpl");
481 cgiCopyTemplateLang("pager.tmpl");
484 * Delete the response...
487 cupsArrayDelete(printers
);
496 cgiShowIPPError(_("Unable to get printer list:"));
504 * 'show_printer()' - Show a single printer.
508 show_printer(http_t
*http
, /* I - Connection to server */
509 const char *printer
) /* I - Name of printer */
511 ipp_t
*request
, /* IPP request */
512 *response
; /* IPP response */
513 ipp_attribute_t
*attr
; /* IPP attribute */
514 char uri
[HTTP_MAX_URI
]; /* Printer URI */
515 char refresh
[1024]; /* Refresh URL */
518 fprintf(stderr
, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
519 http
, printer
? printer
: "(null)");
522 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
526 * attributes-natural-language
530 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
532 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
533 "localhost", 0, "/printers/%s", printer
);
534 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
537 cgiGetAttributes(request
, "printers.tmpl");
540 * Do the request and get back a response...
543 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
546 * Got the result; set the CGI variables and check the status of a
547 * single-queue request...
550 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
552 if ((attr
= ippFindAttribute(response
, "printer-type",
553 IPP_TAG_ENUM
)) != NULL
)
555 cgiSetVariable("cupscommand",
556 (attr
->values
[0].integer
& CUPS_PRINTER_COMMANDS
) ?
560 if (printer
&& (attr
= ippFindAttribute(response
, "printer-state",
561 IPP_TAG_ENUM
)) != NULL
&&
562 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
565 * Printer is processing - automatically refresh the page until we
566 * are done printing...
569 cgiFormEncode(uri
, printer
, sizeof(uri
));
570 snprintf(refresh
, sizeof(refresh
), "10;URL=/printers/%s", uri
);
571 cgiSetVariable("refresh_page", refresh
);
575 * Delete the response...
581 * Show the standard header...
584 cgiStartHTML(printer
);
587 * Show the printer status...
590 cgiSetVariable("_SINGLE_DEST", "1");
591 cgiCopyTemplateLang("printers.tmpl");
594 * Show jobs for the specified printer...
597 cgiCopyTemplateLang("printer-jobs-header.tmpl");
598 cgiShowJobs(http
, printer
);
603 * Show the IPP error...
606 cgiStartHTML(printer
);
607 cgiShowIPPError(_("Unable to get printer status:"));
615 * End of "$Id: printers.c 6889 2007-08-29 22:23:35Z mike $".