4 * Printer status CGI for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
20 #include "cgi-private.h"
28 static void do_printer_op(http_t
*http
, const char *printer
, ipp_op_t op
,
30 static void show_all_printers(http_t
*http
, const char *username
);
31 static void show_printer(http_t
*http
, const char *printer
);
35 * 'main()' - Main entry for CGI.
38 int /* O - Exit status */
41 const char *printer
; /* Printer name */
42 const char *user
; /* Username */
43 http_t
*http
; /* Connection to the server */
44 ipp_t
*request
, /* IPP request */
45 *response
; /* IPP response */
46 ipp_attribute_t
*attr
; /* IPP attribute */
47 const char *op
; /* Operation to perform, if any */
48 static const char *def_attrs
[] = /* Attributes for default printer */
51 "printer-uri-supported"
56 * Get any form variables...
61 op
= cgiGetVariable("OP");
64 * Set the web interface section...
67 cgiSetVariable("SECTION", "printers");
68 cgiSetVariable("REFRESH_PAGE", "");
71 * See if we are displaying a printer or all printers...
74 if ((printer
= getenv("PATH_INFO")) != NULL
)
82 cgiSetVariable("PRINTER_NAME", printer
);
86 * See who is logged in...
89 user
= getenv("REMOTE_USER");
92 * Connect to the HTTP server...
95 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
98 * Get the default printer...
101 if (!op
|| !cgiIsPOST())
104 * Get the default destination...
107 request
= ippNewRequest(CUPS_GET_DEFAULT
);
109 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
110 "requested-attributes",
111 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
113 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
115 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
116 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
118 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
120 char url
[HTTP_MAX_URI
]; /* New URL */
123 cgiSetVariable("DEFAULT_URI",
124 cgiRewriteURL(attr
->values
[0].string
.text
,
125 url
, sizeof(url
), NULL
));
132 * See if we need to show a list of printers or the status of a
137 show_all_printers(http
, user
);
139 show_printer(http
, printer
);
145 const char *server_port
= getenv("SERVER_PORT");
146 /* Port number string */
147 int port
= atoi(server_port
? server_port
: "0");
149 char uri
[1024]; /* URL */
151 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
152 getenv("HTTPS") ? "https" : "http", NULL
,
153 getenv("SERVER_NAME"), port
, "/printers/%s", printer
);
155 printf("Location: %s\n\n", uri
);
157 else if (!strcmp(op
, "start-printer"))
158 do_printer_op(http
, printer
, IPP_RESUME_PRINTER
,
159 cgiText(_("Resume Printer")));
160 else if (!strcmp(op
, "stop-printer"))
161 do_printer_op(http
, printer
, IPP_PAUSE_PRINTER
,
162 cgiText(_("Pause Printer")));
163 else if (!strcmp(op
, "accept-jobs"))
164 do_printer_op(http
, printer
, CUPS_ACCEPT_JOBS
, cgiText(_("Accept Jobs")));
165 else if (!strcmp(op
, "reject-jobs"))
166 do_printer_op(http
, printer
, CUPS_REJECT_JOBS
, cgiText(_("Reject Jobs")));
167 else if (!strcmp(op
, "purge-jobs"))
168 do_printer_op(http
, printer
, IPP_PURGE_JOBS
, cgiText(_("Purge Jobs")));
169 else if (!_cups_strcasecmp(op
, "print-self-test-page"))
170 cgiPrintCommand(http
, printer
, "PrintSelfTestPage",
171 cgiText(_("Print Self-Test Page")));
172 else if (!_cups_strcasecmp(op
, "clean-print-heads"))
173 cgiPrintCommand(http
, printer
, "Clean all",
174 cgiText(_("Clean Print Heads")));
175 else if (!_cups_strcasecmp(op
, "print-test-page"))
176 cgiPrintTestPage(http
, printer
);
177 else if (!_cups_strcasecmp(op
, "move-jobs"))
178 cgiMoveJobs(http
, printer
, 0);
182 * Unknown/bad operation...
185 cgiStartHTML(printer
);
186 cgiCopyTemplateLang("error-op.tmpl");
193 * Unknown/bad operation...
196 cgiStartHTML(cgiText(_("Printers")));
197 cgiCopyTemplateLang("error-op.tmpl");
202 * Close the HTTP server connection...
208 * Return with no errors...
216 * 'do_printer_op()' - Do a printer operation.
220 do_printer_op(http_t
*http
, /* I - HTTP connection */
221 const char *printer
, /* I - Printer name */
222 ipp_op_t op
, /* I - Operation to perform */
223 const char *title
) /* I - Title of page */
225 ipp_t
*request
; /* IPP request */
226 char uri
[HTTP_MAX_URI
], /* Printer URI */
227 resource
[HTTP_MAX_URI
]; /* Path for request */
231 * Build a printer request, which requires the following
235 * attributes-natural-language
239 request
= ippNewRequest(op
);
241 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
242 "localhost", 0, "/printers/%s", printer
);
243 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
247 * Do the request and get back a response...
250 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
);
251 ippDelete(cupsDoRequest(http
, request
, resource
));
253 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
255 puts("Status: 401\n");
258 else if (cupsLastError() > IPP_OK_CONFLICT
)
261 cgiShowIPPError(_("Unable to do maintenance command"));
266 * Redirect successful updates back to the printer page...
269 char url
[1024], /* Printer/class URL */
270 refresh
[1024]; /* Refresh URL */
273 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
274 cgiFormEncode(uri
, url
, sizeof(uri
));
275 snprintf(refresh
, sizeof(refresh
), "5;URL=%s", uri
);
276 cgiSetVariable("refresh_page", refresh
);
280 if (op
== IPP_PAUSE_PRINTER
)
281 cgiCopyTemplateLang("printer-stop.tmpl");
282 else if (op
== IPP_RESUME_PRINTER
)
283 cgiCopyTemplateLang("printer-start.tmpl");
284 else if (op
== CUPS_ACCEPT_JOBS
)
285 cgiCopyTemplateLang("printer-accept.tmpl");
286 else if (op
== CUPS_REJECT_JOBS
)
287 cgiCopyTemplateLang("printer-reject.tmpl");
288 else if (op
== IPP_PURGE_JOBS
)
289 cgiCopyTemplateLang("printer-purge.tmpl");
297 * 'show_all_printers()' - Show all printers...
301 show_all_printers(http_t
*http
, /* I - Connection to server */
302 const char *user
) /* I - Username */
304 int i
; /* Looping var */
305 ipp_t
*request
, /* IPP request */
306 *response
; /* IPP response */
307 cups_array_t
*printers
; /* Array of printer objects */
308 ipp_attribute_t
*printer
; /* Printer object */
309 int ascending
, /* Order of printers (0 = descending) */
310 first
, /* First printer to show */
311 count
; /* Number of printers */
312 const char *var
; /* Form variable */
313 void *search
; /* Search data */
314 char val
[1024]; /* Form variable */
317 fprintf(stderr
, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
318 http
, user
? user
: "(null)");
321 * Show the standard header...
324 cgiStartHTML(cgiText(_("Printers")));
327 * Build a CUPS_GET_PRINTERS request, which requires the following
331 * attributes-natural-language
334 * requesting-user-name
337 request
= ippNewRequest(CUPS_GET_PRINTERS
);
339 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
341 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
342 "printer-type-mask", CUPS_PRINTER_CLASS
);
345 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
346 "requesting-user-name", NULL
, user
);
348 cgiGetAttributes(request
, "printers.tmpl");
351 * Do the request and get back a response...
354 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
357 * Get a list of matching job objects.
360 if ((var
= cgiGetVariable("QUERY")) != NULL
&&
361 !cgiGetVariable("CLEAR"))
362 search
= cgiCompileSearch(var
);
366 printers
= cgiGetIPPObjects(response
, search
);
367 count
= cupsArrayCount(printers
);
370 cgiFreeSearch(search
);
373 * Figure out which printers to display...
376 if ((var
= cgiGetVariable("FIRST")) != NULL
)
382 first
= count
- CUPS_PAGE_MAX
;
384 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
389 sprintf(val
, "%d", count
);
390 cgiSetVariable("TOTAL", val
);
392 if ((var
= cgiGetVariable("ORDER")) != NULL
&& *var
)
393 ascending
= !_cups_strcasecmp(var
, "asc");
399 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, first
);
400 i
< CUPS_PAGE_MAX
&& printer
;
401 i
++, printer
= (ipp_attribute_t
*)cupsArrayNext(printers
))
402 cgiSetIPPObjectVars(printer
, NULL
, i
);
406 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, count
- first
- 1);
407 i
< CUPS_PAGE_MAX
&& printer
;
408 i
++, printer
= (ipp_attribute_t
*)cupsArrayPrev(printers
))
409 cgiSetIPPObjectVars(printer
, NULL
, i
);
413 * Save navigation URLs...
416 cgiSetVariable("THISURL", "/printers/");
420 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
421 cgiSetVariable("PREV", val
);
424 if ((first
+ CUPS_PAGE_MAX
) < count
)
426 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
427 cgiSetVariable("NEXT", val
);
431 * Then show everything...
434 cgiCopyTemplateLang("search.tmpl");
436 cgiCopyTemplateLang("printers-header.tmpl");
438 if (count
> CUPS_PAGE_MAX
)
439 cgiCopyTemplateLang("pager.tmpl");
441 cgiCopyTemplateLang("printers.tmpl");
443 if (count
> CUPS_PAGE_MAX
)
444 cgiCopyTemplateLang("pager.tmpl");
447 * Delete the response...
450 cupsArrayDelete(printers
);
459 cgiShowIPPError(_("Unable to get printer list"));
467 * 'show_printer()' - Show a single printer.
471 show_printer(http_t
*http
, /* I - Connection to server */
472 const char *printer
) /* I - Name of printer */
474 ipp_t
*request
, /* IPP request */
475 *response
; /* IPP response */
476 ipp_attribute_t
*attr
; /* IPP attribute */
477 char uri
[HTTP_MAX_URI
]; /* Printer URI */
478 char refresh
[1024]; /* Refresh URL */
481 fprintf(stderr
, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
482 http
, printer
? printer
: "(null)");
485 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
489 * attributes-natural-language
493 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
495 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
496 "localhost", 0, "/printers/%s", printer
);
497 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
500 cgiGetAttributes(request
, "printer.tmpl");
503 * Do the request and get back a response...
506 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
509 * Got the result; set the CGI variables and check the status of a
510 * single-queue request...
513 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
515 if (printer
&& (attr
= ippFindAttribute(response
, "printer-state",
516 IPP_TAG_ENUM
)) != NULL
&&
517 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
520 * Printer is processing - automatically refresh the page until we
521 * are done printing...
524 cgiFormEncode(uri
, printer
, sizeof(uri
));
525 snprintf(refresh
, sizeof(refresh
), "10;URL=/printers/%s", uri
);
526 cgiSetVariable("refresh_page", refresh
);
530 * Delete the response...
536 * Show the standard header...
539 cgiStartHTML(printer
);
542 * Show the printer status...
545 cgiCopyTemplateLang("printer.tmpl");
548 * Show jobs for the specified printer...
551 cgiCopyTemplateLang("printer-jobs-header.tmpl");
552 cgiShowJobs(http
, printer
);
557 * Show the IPP error...
560 cgiStartHTML(printer
);
561 cgiShowIPPError(_("Unable to get printer status"));