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");
78 * See if we are displaying a printer or all printers...
81 if ((printer
= getenv("PATH_INFO")) != NULL
)
89 cgiSetVariable("PRINTER_NAME", printer
);
93 * See who is logged in...
96 user
= getenv("REMOTE_USER");
99 * Connect to the HTTP server...
102 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
105 * Get the default printer...
108 if (!op
|| !cgiIsPOST())
111 * Get the default destination...
114 request
= ippNewRequest(CUPS_GET_DEFAULT
);
116 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
117 "requested-attributes",
118 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
120 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
122 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
123 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
125 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
127 char url
[HTTP_MAX_URI
]; /* New URL */
130 cgiSetVariable("DEFAULT_URI",
131 cgiRewriteURL(attr
->values
[0].string
.text
,
132 url
, sizeof(url
), NULL
));
139 * See if we need to show a list of printers or the status of a
144 show_all_printers(http
, user
);
146 show_printer(http
, printer
);
150 if (!strcmp(op
, "start-printer"))
151 do_printer_op(http
, printer
, IPP_RESUME_PRINTER
,
152 cgiText(_("Resume Printer")));
153 else if (!strcmp(op
, "stop-printer"))
154 do_printer_op(http
, printer
, IPP_PAUSE_PRINTER
,
155 cgiText(_("Pause Printer")));
156 else if (!strcmp(op
, "accept-jobs"))
157 do_printer_op(http
, printer
, CUPS_ACCEPT_JOBS
, cgiText(_("Accept Jobs")));
158 else if (!strcmp(op
, "reject-jobs"))
159 do_printer_op(http
, printer
, CUPS_REJECT_JOBS
, cgiText(_("Reject Jobs")));
160 else if (!strcmp(op
, "purge-jobs"))
161 do_printer_op(http
, printer
, IPP_PURGE_JOBS
, cgiText(_("Purge Jobs")));
162 else if (!strcasecmp(op
, "print-self-test-page"))
163 cgiPrintCommand(http
, printer
, "PrintSelfTestPage",
164 cgiText(_("Print Self-Test Page")));
165 else if (!strcasecmp(op
, "clean-print-heads"))
166 cgiPrintCommand(http
, printer
, "Clean all",
167 cgiText(_("Clean Print Heads")));
168 else if (!strcasecmp(op
, "print-test-page"))
169 cgiPrintTestPage(http
, printer
);
170 else if (!strcasecmp(op
, "move-jobs"))
171 cgiMoveJobs(http
, printer
, 0);
175 * Unknown/bad operation...
178 cgiStartHTML(printer
);
179 cgiCopyTemplateLang("error-op.tmpl");
186 * Unknown/bad operation...
189 cgiStartHTML(cgiText(_("Printers")));
190 cgiCopyTemplateLang("error-op.tmpl");
195 * Close the HTTP server connection...
201 * Return with no errors...
209 * 'do_printer_op()' - Do a printer operation.
213 do_printer_op(http_t
*http
, /* I - HTTP connection */
214 const char *printer
, /* I - Printer name */
215 ipp_op_t op
, /* I - Operation to perform */
216 const char *title
) /* I - Title of page */
218 ipp_t
*request
; /* IPP request */
219 char uri
[HTTP_MAX_URI
], /* Printer URI */
220 resource
[HTTP_MAX_URI
]; /* Path for request */
224 * Build a printer request, which requires the following
228 * attributes-natural-language
232 request
= ippNewRequest(op
);
234 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
235 "localhost", 0, "/printers/%s", printer
);
236 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
240 * Do the request and get back a response...
243 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
);
244 ippDelete(cupsDoRequest(http
, request
, resource
));
246 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
248 puts("Status: 401\n");
251 else if (cupsLastError() > IPP_OK_CONFLICT
)
254 cgiShowIPPError(_("Unable to do maintenance command:"));
259 * Redirect successful updates back to the printer page...
262 char url
[1024], /* Printer/class URL */
263 refresh
[1024]; /* Refresh URL */
266 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
267 cgiFormEncode(uri
, url
, sizeof(uri
));
268 snprintf(refresh
, sizeof(refresh
), "5;URL=%s", uri
);
269 cgiSetVariable("refresh_page", refresh
);
273 if (op
== IPP_PAUSE_PRINTER
)
274 cgiCopyTemplateLang("printer-stop.tmpl");
275 else if (op
== IPP_RESUME_PRINTER
)
276 cgiCopyTemplateLang("printer-start.tmpl");
277 else if (op
== CUPS_ACCEPT_JOBS
)
278 cgiCopyTemplateLang("printer-accept.tmpl");
279 else if (op
== CUPS_REJECT_JOBS
)
280 cgiCopyTemplateLang("printer-reject.tmpl");
281 else if (op
== IPP_PURGE_JOBS
)
282 cgiCopyTemplateLang("printer-purge.tmpl");
290 * 'show_all_printers()' - Show all printers...
294 show_all_printers(http_t
*http
, /* I - Connection to server */
295 const char *user
) /* I - Username */
297 int i
; /* Looping var */
298 ipp_t
*request
, /* IPP request */
299 *response
; /* IPP response */
300 cups_array_t
*printers
; /* Array of printer objects */
301 ipp_attribute_t
*printer
; /* Printer object */
302 int ascending
, /* Order of printers (0 = descending) */
303 first
, /* First printer to show */
304 count
; /* Number of printers */
305 const char *var
; /* Form variable */
306 void *search
; /* Search data */
307 char val
[1024]; /* Form variable */
310 fprintf(stderr
, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
311 http
, user
? user
: "(null)");
314 * Show the standard header...
317 cgiStartHTML(cgiText(_("Printers")));
320 * Build a CUPS_GET_PRINTERS request, which requires the following
324 * attributes-natural-language
327 * requesting-user-name
330 request
= ippNewRequest(CUPS_GET_PRINTERS
);
332 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
334 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
335 "printer-type-mask", CUPS_PRINTER_CLASS
);
338 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
339 "requesting-user-name", NULL
, user
);
341 cgiGetAttributes(request
, "printers.tmpl");
344 * Do the request and get back a response...
347 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
350 * Get a list of matching job objects.
353 if ((var
= cgiGetVariable("QUERY")) != NULL
&&
354 !cgiGetVariable("CLEAR"))
355 search
= cgiCompileSearch(var
);
359 printers
= cgiGetIPPObjects(response
, search
);
360 count
= cupsArrayCount(printers
);
363 cgiFreeSearch(search
);
366 * Figure out which printers to display...
369 if ((var
= cgiGetVariable("FIRST")) != NULL
)
375 first
= count
- CUPS_PAGE_MAX
;
377 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
382 sprintf(val
, "%d", count
);
383 cgiSetVariable("TOTAL", val
);
385 if ((var
= cgiGetVariable("ORDER")) != NULL
)
386 ascending
= !strcasecmp(var
, "asc");
392 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, first
);
393 i
< CUPS_PAGE_MAX
&& printer
;
394 i
++, printer
= (ipp_attribute_t
*)cupsArrayNext(printers
))
395 cgiSetIPPObjectVars(printer
, NULL
, i
);
399 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, count
- first
- 1);
400 i
< CUPS_PAGE_MAX
&& printer
;
401 i
++, printer
= (ipp_attribute_t
*)cupsArrayPrev(printers
))
402 cgiSetIPPObjectVars(printer
, NULL
, i
);
406 * Save navigation URLs...
409 cgiSetVariable("THISURL", "/printers/");
413 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
414 cgiSetVariable("PREV", val
);
417 if ((first
+ CUPS_PAGE_MAX
) < count
)
419 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
420 cgiSetVariable("NEXT", val
);
424 * Then show everything...
427 cgiCopyTemplateLang("search.tmpl");
429 cgiCopyTemplateLang("printers-header.tmpl");
431 if (count
> CUPS_PAGE_MAX
)
432 cgiCopyTemplateLang("pager.tmpl");
434 cgiCopyTemplateLang("printers.tmpl");
436 if (count
> CUPS_PAGE_MAX
)
437 cgiCopyTemplateLang("pager.tmpl");
440 * Delete the response...
443 cupsArrayDelete(printers
);
452 cgiShowIPPError(_("Unable to get printer list:"));
460 * 'show_printer()' - Show a single printer.
464 show_printer(http_t
*http
, /* I - Connection to server */
465 const char *printer
) /* I - Name of printer */
467 ipp_t
*request
, /* IPP request */
468 *response
; /* IPP response */
469 ipp_attribute_t
*attr
; /* IPP attribute */
470 char uri
[HTTP_MAX_URI
]; /* Printer URI */
471 char refresh
[1024]; /* Refresh URL */
474 fprintf(stderr
, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
475 http
, printer
? printer
: "(null)");
478 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
482 * attributes-natural-language
486 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
488 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
489 "localhost", 0, "/printers/%s", printer
);
490 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
493 cgiGetAttributes(request
, "printer.tmpl");
496 * Do the request and get back a response...
499 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
502 * Got the result; set the CGI variables and check the status of a
503 * single-queue request...
506 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
508 if (printer
&& (attr
= ippFindAttribute(response
, "printer-state",
509 IPP_TAG_ENUM
)) != NULL
&&
510 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
513 * Printer is processing - automatically refresh the page until we
514 * are done printing...
517 cgiFormEncode(uri
, printer
, sizeof(uri
));
518 snprintf(refresh
, sizeof(refresh
), "10;URL=/printers/%s", uri
);
519 cgiSetVariable("refresh_page", refresh
);
523 * Delete the response...
529 * Show the standard header...
532 cgiStartHTML(printer
);
535 * Show the printer status...
538 cgiCopyTemplateLang("printer.tmpl");
541 * Show jobs for the specified printer...
544 cgiCopyTemplateLang("printer-jobs-header.tmpl");
545 cgiShowJobs(http
, printer
);
550 * Show the IPP error...
553 cgiStartHTML(printer
);
554 cgiShowIPPError(_("Unable to get printer status:"));
562 * End of "$Id: printers.c 7940 2008-09-16 00:45:16Z mike $".