2 * Printer status CGI for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #include "cgi-private.h"
26 static void do_printer_op(http_t
*http
, const char *printer
, ipp_op_t op
,
28 static void show_all_printers(http_t
*http
, const char *username
);
29 static void show_printer(http_t
*http
, const char *printer
);
33 * 'main()' - Main entry for CGI.
36 int /* O - Exit status */
39 const char *printer
; /* Printer name */
40 const char *user
; /* Username */
41 http_t
*http
; /* Connection to the server */
42 ipp_t
*request
, /* IPP request */
43 *response
; /* IPP response */
44 ipp_attribute_t
*attr
; /* IPP attribute */
45 const char *op
; /* Operation to perform, if any */
46 static const char *def_attrs
[] = /* Attributes for default printer */
49 "printer-uri-supported"
54 * Get any form variables...
59 op
= cgiGetVariable("OP");
62 * Set the web interface section...
65 cgiSetVariable("SECTION", "printers");
66 cgiSetVariable("REFRESH_PAGE", "");
69 * See if we are displaying a printer or all printers...
72 if ((printer
= getenv("PATH_INFO")) != NULL
)
80 cgiSetVariable("PRINTER_NAME", printer
);
84 * See who is logged in...
87 user
= getenv("REMOTE_USER");
90 * Connect to the HTTP server...
93 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
96 * Get the default printer...
99 if (!op
|| !cgiIsPOST())
102 * Get the default destination...
105 request
= ippNewRequest(CUPS_GET_DEFAULT
);
107 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
108 "requested-attributes",
109 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
111 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
113 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
114 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
116 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
118 char url
[HTTP_MAX_URI
]; /* New URL */
121 cgiSetVariable("DEFAULT_URI",
122 cgiRewriteURL(attr
->values
[0].string
.text
,
123 url
, sizeof(url
), NULL
));
130 * See if we need to show a list of printers or the status of a
135 show_all_printers(http
, user
);
137 show_printer(http
, printer
);
143 const char *server_port
= getenv("SERVER_PORT");
144 /* Port number string */
145 int port
= atoi(server_port
? server_port
: "0");
147 char uri
[1024]; /* URL */
149 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
150 getenv("HTTPS") ? "https" : "http", NULL
,
151 getenv("SERVER_NAME"), port
, "/printers/%s", printer
);
153 printf("Location: %s\n\n", uri
);
155 else if (!strcmp(op
, "start-printer"))
156 do_printer_op(http
, printer
, IPP_RESUME_PRINTER
,
157 cgiText(_("Resume Printer")));
158 else if (!strcmp(op
, "stop-printer"))
159 do_printer_op(http
, printer
, IPP_PAUSE_PRINTER
,
160 cgiText(_("Pause Printer")));
161 else if (!strcmp(op
, "accept-jobs"))
162 do_printer_op(http
, printer
, CUPS_ACCEPT_JOBS
, cgiText(_("Accept Jobs")));
163 else if (!strcmp(op
, "reject-jobs"))
164 do_printer_op(http
, printer
, CUPS_REJECT_JOBS
, cgiText(_("Reject Jobs")));
165 else if (!strcmp(op
, "cancel-jobs"))
166 do_printer_op(http
, printer
, IPP_OP_CANCEL_JOBS
, cgiText(_("Cancel Jobs")));
167 else if (!_cups_strcasecmp(op
, "print-self-test-page"))
168 cgiPrintCommand(http
, printer
, "PrintSelfTestPage",
169 cgiText(_("Print Self-Test Page")));
170 else if (!_cups_strcasecmp(op
, "clean-print-heads"))
171 cgiPrintCommand(http
, printer
, "Clean all",
172 cgiText(_("Clean Print Heads")));
173 else if (!_cups_strcasecmp(op
, "print-test-page"))
174 cgiPrintTestPage(http
, printer
);
175 else if (!_cups_strcasecmp(op
, "move-jobs"))
176 cgiMoveJobs(http
, printer
, 0);
180 * Unknown/bad operation...
183 cgiStartHTML(printer
);
184 cgiCopyTemplateLang("error-op.tmpl");
191 * Unknown/bad operation...
194 cgiStartHTML(cgiText(_("Printers")));
195 cgiCopyTemplateLang("error-op.tmpl");
200 * Close the HTTP server connection...
206 * Return with no errors...
214 * 'do_printer_op()' - Do a printer operation.
218 do_printer_op(http_t
*http
, /* I - HTTP connection */
219 const char *printer
, /* I - Printer name */
220 ipp_op_t op
, /* I - Operation to perform */
221 const char *title
) /* I - Title of page */
223 ipp_t
*request
; /* IPP request */
224 char uri
[HTTP_MAX_URI
], /* Printer URI */
225 resource
[HTTP_MAX_URI
]; /* Path for request */
229 * Build a printer request, which requires the following
233 * attributes-natural-language
237 request
= ippNewRequest(op
);
239 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
240 "localhost", 0, "/printers/%s", printer
);
241 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
245 * Do the request and get back a response...
248 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
);
249 ippDelete(cupsDoRequest(http
, request
, resource
));
251 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
253 puts("Status: 401\n");
256 else if (cupsLastError() > IPP_OK_CONFLICT
)
259 cgiShowIPPError(_("Unable to do maintenance command"));
264 * Redirect successful updates back to the printer page...
267 char url
[1024], /* Printer/class URL */
268 refresh
[1024]; /* Refresh URL */
271 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
272 cgiFormEncode(uri
, url
, sizeof(uri
));
273 snprintf(refresh
, sizeof(refresh
), "5;URL=%s", uri
);
274 cgiSetVariable("refresh_page", refresh
);
278 if (op
== IPP_PAUSE_PRINTER
)
279 cgiCopyTemplateLang("printer-stop.tmpl");
280 else if (op
== IPP_RESUME_PRINTER
)
281 cgiCopyTemplateLang("printer-start.tmpl");
282 else if (op
== CUPS_ACCEPT_JOBS
)
283 cgiCopyTemplateLang("printer-accept.tmpl");
284 else if (op
== CUPS_REJECT_JOBS
)
285 cgiCopyTemplateLang("printer-reject.tmpl");
286 else if (op
== IPP_OP_CANCEL_JOBS
)
287 cgiCopyTemplateLang("printer-cancel-jobs.tmpl");
295 * 'show_all_printers()' - Show all printers...
299 show_all_printers(http_t
*http
, /* I - Connection to server */
300 const char *user
) /* I - Username */
302 int i
; /* Looping var */
303 ipp_t
*request
, /* IPP request */
304 *response
; /* IPP response */
305 cups_array_t
*printers
; /* Array of printer objects */
306 ipp_attribute_t
*printer
; /* Printer object */
307 int ascending
, /* Order of printers (0 = descending) */
308 first
, /* First printer to show */
309 count
; /* Number of printers */
310 const char *var
; /* Form variable */
311 void *search
; /* Search data */
312 char val
[1024]; /* Form variable */
315 fprintf(stderr
, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
316 http
, user
? user
: "(null)");
319 * Show the standard header...
322 cgiStartHTML(cgiText(_("Printers")));
325 * Build a CUPS_GET_PRINTERS request, which requires the following
329 * attributes-natural-language
332 * requesting-user-name
335 request
= ippNewRequest(CUPS_GET_PRINTERS
);
337 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
339 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
340 "printer-type-mask", CUPS_PRINTER_CLASS
);
343 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
344 "requesting-user-name", NULL
, user
);
346 cgiGetAttributes(request
, "printers.tmpl");
349 * Do the request and get back a response...
352 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
355 * Get a list of matching job objects.
358 if ((var
= cgiGetVariable("QUERY")) != NULL
&&
359 !cgiGetVariable("CLEAR"))
360 search
= cgiCompileSearch(var
);
364 printers
= cgiGetIPPObjects(response
, search
);
365 count
= cupsArrayCount(printers
);
368 cgiFreeSearch(search
);
371 * Figure out which printers to display...
374 if ((var
= cgiGetVariable("FIRST")) != NULL
)
380 first
= count
- CUPS_PAGE_MAX
;
382 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
387 sprintf(val
, "%d", count
);
388 cgiSetVariable("TOTAL", val
);
390 if ((var
= cgiGetVariable("ORDER")) != NULL
&& *var
)
391 ascending
= !_cups_strcasecmp(var
, "asc");
397 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, first
);
398 i
< CUPS_PAGE_MAX
&& printer
;
399 i
++, printer
= (ipp_attribute_t
*)cupsArrayNext(printers
))
400 cgiSetIPPObjectVars(printer
, NULL
, i
);
404 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, count
- first
- 1);
405 i
< CUPS_PAGE_MAX
&& printer
;
406 i
++, printer
= (ipp_attribute_t
*)cupsArrayPrev(printers
))
407 cgiSetIPPObjectVars(printer
, NULL
, i
);
411 * Save navigation URLs...
414 cgiSetVariable("THISURL", "/printers/");
418 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
419 cgiSetVariable("PREV", val
);
422 if ((first
+ CUPS_PAGE_MAX
) < count
)
424 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
425 cgiSetVariable("NEXT", val
);
429 * Then show everything...
432 cgiCopyTemplateLang("search.tmpl");
434 cgiCopyTemplateLang("printers-header.tmpl");
436 if (count
> CUPS_PAGE_MAX
)
437 cgiCopyTemplateLang("pager.tmpl");
439 cgiCopyTemplateLang("printers.tmpl");
441 if (count
> CUPS_PAGE_MAX
)
442 cgiCopyTemplateLang("pager.tmpl");
445 * Delete the response...
448 cupsArrayDelete(printers
);
457 cgiShowIPPError(_("Unable to get printer list"));
465 * 'show_printer()' - Show a single printer.
469 show_printer(http_t
*http
, /* I - Connection to server */
470 const char *printer
) /* I - Name of printer */
472 ipp_t
*request
, /* IPP request */
473 *response
; /* IPP response */
474 ipp_attribute_t
*attr
; /* IPP attribute */
475 char uri
[HTTP_MAX_URI
]; /* Printer URI */
476 char refresh
[1024]; /* Refresh URL */
479 fprintf(stderr
, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
480 http
, printer
? printer
: "(null)");
483 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
487 * attributes-natural-language
491 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
493 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
494 "localhost", 0, "/printers/%s", printer
);
495 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
498 cgiGetAttributes(request
, "printer.tmpl");
501 * Do the request and get back a response...
504 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
507 * Got the result; set the CGI variables and check the status of a
508 * single-queue request...
511 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
513 if (printer
&& (attr
= ippFindAttribute(response
, "printer-state",
514 IPP_TAG_ENUM
)) != NULL
&&
515 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
518 * Printer is processing - automatically refresh the page until we
519 * are done printing...
522 cgiFormEncode(uri
, printer
, sizeof(uri
));
523 snprintf(refresh
, sizeof(refresh
), "10;URL=/printers/%s", uri
);
524 cgiSetVariable("refresh_page", refresh
);
528 * Delete the response...
534 * Show the standard header...
537 cgiStartHTML(printer
);
540 * Show the printer status...
543 cgiCopyTemplateLang("printer.tmpl");
546 * Show jobs for the specified printer...
549 cgiCopyTemplateLang("printer-jobs-header.tmpl");
550 cgiShowJobs(http
, printer
);
555 * Show the IPP error...
558 cgiStartHTML(printer
);
559 cgiShowIPPError(_("Unable to get printer status"));