2 * "$Id: printers.c 7940 2008-09-16 00:45:16Z 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 * do_printer_op() - Do a printer operation.
19 * show_all_printers() - Show all printers...
20 * show_printer() - Show a single printer.
24 * Include necessary headers...
27 #include "cgi-private.h"
35 static void do_printer_op(http_t
*http
, const char *printer
, ipp_op_t op
,
37 static void show_all_printers(http_t
*http
, const char *username
);
38 static void show_printer(http_t
*http
, const char *printer
);
42 * 'main()' - Main entry for CGI.
45 int /* O - Exit status */
46 main(int argc
, /* I - Number of command-line arguments */
47 char *argv
[]) /* I - Command-line arguments */
49 const char *printer
; /* Printer name */
50 const char *user
; /* Username */
51 http_t
*http
; /* Connection to the server */
52 ipp_t
*request
, /* IPP request */
53 *response
; /* IPP response */
54 ipp_attribute_t
*attr
; /* IPP attribute */
55 const char *op
; /* Operation to perform, if any */
56 static const char *def_attrs
[] = /* Attributes for default printer */
59 "printer-uri-supported"
64 * Get any form variables...
69 op
= cgiGetVariable("OP");
72 * Set the web interface section...
75 cgiSetVariable("SECTION", "printers");
76 cgiSetVariable("REFRESH_PAGE", "");
79 * See if we are displaying a printer or all printers...
82 if ((printer
= getenv("PATH_INFO")) != NULL
)
90 cgiSetVariable("PRINTER_NAME", printer
);
94 * See who is logged in...
97 user
= getenv("REMOTE_USER");
100 * Connect to the HTTP server...
103 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
106 * Get the default printer...
109 if (!op
|| !cgiIsPOST())
112 * Get the default destination...
115 request
= ippNewRequest(CUPS_GET_DEFAULT
);
117 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
118 "requested-attributes",
119 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
121 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
123 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
124 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
126 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
128 char url
[HTTP_MAX_URI
]; /* New URL */
131 cgiSetVariable("DEFAULT_URI",
132 cgiRewriteURL(attr
->values
[0].string
.text
,
133 url
, sizeof(url
), NULL
));
140 * See if we need to show a list of printers or the status of a
145 show_all_printers(http
, user
);
147 show_printer(http
, printer
);
151 if (!strcmp(op
, "start-printer"))
152 do_printer_op(http
, printer
, IPP_RESUME_PRINTER
,
153 cgiText(_("Resume Printer")));
154 else if (!strcmp(op
, "stop-printer"))
155 do_printer_op(http
, printer
, IPP_PAUSE_PRINTER
,
156 cgiText(_("Pause Printer")));
157 else if (!strcmp(op
, "accept-jobs"))
158 do_printer_op(http
, printer
, CUPS_ACCEPT_JOBS
, cgiText(_("Accept Jobs")));
159 else if (!strcmp(op
, "reject-jobs"))
160 do_printer_op(http
, printer
, CUPS_REJECT_JOBS
, cgiText(_("Reject Jobs")));
161 else if (!strcmp(op
, "purge-jobs"))
162 do_printer_op(http
, printer
, IPP_PURGE_JOBS
, cgiText(_("Purge Jobs")));
163 else if (!strcasecmp(op
, "print-self-test-page"))
164 cgiPrintCommand(http
, printer
, "PrintSelfTestPage",
165 cgiText(_("Print Self-Test Page")));
166 else if (!strcasecmp(op
, "clean-print-heads"))
167 cgiPrintCommand(http
, printer
, "Clean all",
168 cgiText(_("Clean Print Heads")));
169 else if (!strcasecmp(op
, "print-test-page"))
170 cgiPrintTestPage(http
, printer
);
171 else if (!strcasecmp(op
, "move-jobs"))
172 cgiMoveJobs(http
, printer
, 0);
176 * Unknown/bad operation...
179 cgiStartHTML(printer
);
180 cgiCopyTemplateLang("error-op.tmpl");
187 * Unknown/bad operation...
190 cgiStartHTML(cgiText(_("Printers")));
191 cgiCopyTemplateLang("error-op.tmpl");
196 * Close the HTTP server connection...
202 * Return with no errors...
210 * 'do_printer_op()' - Do a printer operation.
214 do_printer_op(http_t
*http
, /* I - HTTP connection */
215 const char *printer
, /* I - Printer name */
216 ipp_op_t op
, /* I - Operation to perform */
217 const char *title
) /* I - Title of page */
219 ipp_t
*request
; /* IPP request */
220 char uri
[HTTP_MAX_URI
], /* Printer URI */
221 resource
[HTTP_MAX_URI
]; /* Path for request */
225 * Build a printer request, which requires the following
229 * attributes-natural-language
233 request
= ippNewRequest(op
);
235 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
236 "localhost", 0, "/printers/%s", printer
);
237 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
241 * Do the request and get back a response...
244 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
);
245 ippDelete(cupsDoRequest(http
, request
, resource
));
247 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
249 puts("Status: 401\n");
252 else if (cupsLastError() > IPP_OK_CONFLICT
)
255 cgiShowIPPError(_("Unable to do maintenance command:"));
260 * Redirect successful updates back to the printer page...
263 char url
[1024], /* Printer/class URL */
264 refresh
[1024]; /* Refresh URL */
267 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
268 cgiFormEncode(uri
, url
, sizeof(uri
));
269 snprintf(refresh
, sizeof(refresh
), "5;URL=%s", uri
);
270 cgiSetVariable("refresh_page", refresh
);
274 if (op
== IPP_PAUSE_PRINTER
)
275 cgiCopyTemplateLang("printer-stop.tmpl");
276 else if (op
== IPP_RESUME_PRINTER
)
277 cgiCopyTemplateLang("printer-start.tmpl");
278 else if (op
== CUPS_ACCEPT_JOBS
)
279 cgiCopyTemplateLang("printer-accept.tmpl");
280 else if (op
== CUPS_REJECT_JOBS
)
281 cgiCopyTemplateLang("printer-reject.tmpl");
282 else if (op
== IPP_PURGE_JOBS
)
283 cgiCopyTemplateLang("printer-purge.tmpl");
291 * 'show_all_printers()' - Show all printers...
295 show_all_printers(http_t
*http
, /* I - Connection to server */
296 const char *user
) /* I - Username */
298 int i
; /* Looping var */
299 ipp_t
*request
, /* IPP request */
300 *response
; /* IPP response */
301 cups_array_t
*printers
; /* Array of printer objects */
302 ipp_attribute_t
*printer
; /* Printer object */
303 int ascending
, /* Order of printers (0 = descending) */
304 first
, /* First printer to show */
305 count
; /* Number of printers */
306 const char *var
; /* Form variable */
307 void *search
; /* Search data */
308 char val
[1024]; /* Form variable */
311 fprintf(stderr
, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
312 http
, user
? user
: "(null)");
315 * Show the standard header...
318 cgiStartHTML(cgiText(_("Printers")));
321 * Build a CUPS_GET_PRINTERS request, which requires the following
325 * attributes-natural-language
328 * requesting-user-name
331 request
= ippNewRequest(CUPS_GET_PRINTERS
);
333 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
335 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
336 "printer-type-mask", CUPS_PRINTER_CLASS
);
339 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
340 "requesting-user-name", NULL
, user
);
342 cgiGetAttributes(request
, "printers.tmpl");
345 * Do the request and get back a response...
348 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
351 * Get a list of matching job objects.
354 if ((var
= cgiGetVariable("QUERY")) != NULL
&&
355 !cgiGetVariable("CLEAR"))
356 search
= cgiCompileSearch(var
);
360 printers
= cgiGetIPPObjects(response
, search
);
361 count
= cupsArrayCount(printers
);
364 cgiFreeSearch(search
);
367 * Figure out which printers to display...
370 if ((var
= cgiGetVariable("FIRST")) != NULL
)
376 first
= count
- CUPS_PAGE_MAX
;
378 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
383 sprintf(val
, "%d", count
);
384 cgiSetVariable("TOTAL", val
);
386 if ((var
= cgiGetVariable("ORDER")) != NULL
)
387 ascending
= !strcasecmp(var
, "asc");
393 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, first
);
394 i
< CUPS_PAGE_MAX
&& printer
;
395 i
++, printer
= (ipp_attribute_t
*)cupsArrayNext(printers
))
396 cgiSetIPPObjectVars(printer
, NULL
, i
);
400 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, count
- first
- 1);
401 i
< CUPS_PAGE_MAX
&& printer
;
402 i
++, printer
= (ipp_attribute_t
*)cupsArrayPrev(printers
))
403 cgiSetIPPObjectVars(printer
, NULL
, i
);
407 * Save navigation URLs...
410 cgiSetVariable("THISURL", "/printers/");
414 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
415 cgiSetVariable("PREV", val
);
418 if ((first
+ CUPS_PAGE_MAX
) < count
)
420 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
421 cgiSetVariable("NEXT", val
);
425 * Then show everything...
428 cgiCopyTemplateLang("search.tmpl");
430 cgiCopyTemplateLang("printers-header.tmpl");
432 if (count
> CUPS_PAGE_MAX
)
433 cgiCopyTemplateLang("pager.tmpl");
435 cgiCopyTemplateLang("printers.tmpl");
437 if (count
> CUPS_PAGE_MAX
)
438 cgiCopyTemplateLang("pager.tmpl");
441 * Delete the response...
444 cupsArrayDelete(printers
);
453 cgiShowIPPError(_("Unable to get printer list:"));
461 * 'show_printer()' - Show a single printer.
465 show_printer(http_t
*http
, /* I - Connection to server */
466 const char *printer
) /* I - Name of printer */
468 ipp_t
*request
, /* IPP request */
469 *response
; /* IPP response */
470 ipp_attribute_t
*attr
; /* IPP attribute */
471 char uri
[HTTP_MAX_URI
]; /* Printer URI */
472 char refresh
[1024]; /* Refresh URL */
475 fprintf(stderr
, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
476 http
, printer
? printer
: "(null)");
479 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
483 * attributes-natural-language
487 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
489 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
490 "localhost", 0, "/printers/%s", printer
);
491 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
494 cgiGetAttributes(request
, "printer.tmpl");
497 * Do the request and get back a response...
500 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
503 * Got the result; set the CGI variables and check the status of a
504 * single-queue request...
507 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
509 if (printer
&& (attr
= ippFindAttribute(response
, "printer-state",
510 IPP_TAG_ENUM
)) != NULL
&&
511 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
514 * Printer is processing - automatically refresh the page until we
515 * are done printing...
518 cgiFormEncode(uri
, printer
, sizeof(uri
));
519 snprintf(refresh
, sizeof(refresh
), "10;URL=/printers/%s", uri
);
520 cgiSetVariable("refresh_page", refresh
);
524 * Delete the response...
530 * Show the standard header...
533 cgiStartHTML(printer
);
536 * Show the printer status...
539 cgiCopyTemplateLang("printer.tmpl");
542 * Show jobs for the specified printer...
545 cgiCopyTemplateLang("printer-jobs-header.tmpl");
546 cgiShowJobs(http
, printer
);
551 * Show the IPP error...
554 cgiStartHTML(printer
);
555 cgiShowIPPError(_("Unable to get printer status:"));
563 * End of "$Id: printers.c 7940 2008-09-16 00:45:16Z mike $".