2 * Printer status CGI for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cgi-private.h"
22 static void do_printer_op(http_t
*http
, const char *printer
, ipp_op_t op
,
24 static void show_all_printers(http_t
*http
, const char *username
);
25 static void show_printer(http_t
*http
, const char *printer
);
29 * 'main()' - Main entry for CGI.
32 int /* O - Exit status */
35 const char *printer
; /* Printer name */
36 const char *user
; /* Username */
37 http_t
*http
; /* Connection to the server */
38 ipp_t
*request
, /* IPP request */
39 *response
; /* IPP response */
40 ipp_attribute_t
*attr
; /* IPP attribute */
41 const char *op
; /* Operation to perform, if any */
42 static const char *def_attrs
[] = /* Attributes for default printer */
45 "printer-uri-supported"
50 * Get any form variables...
55 op
= cgiGetVariable("OP");
58 * Set the web interface section...
61 cgiSetVariable("SECTION", "printers");
62 cgiSetVariable("REFRESH_PAGE", "");
65 * See if we are displaying a printer or all printers...
68 if ((printer
= getenv("PATH_INFO")) != NULL
)
76 cgiSetVariable("PRINTER_NAME", printer
);
80 * See who is logged in...
83 user
= getenv("REMOTE_USER");
86 * Connect to the HTTP server...
89 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
92 * Get the default printer...
95 if (!op
|| !cgiIsPOST())
98 * Get the default destination...
101 request
= ippNewRequest(CUPS_GET_DEFAULT
);
103 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
104 "requested-attributes",
105 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
107 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
109 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
110 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
112 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
114 char url
[HTTP_MAX_URI
]; /* New URL */
117 cgiSetVariable("DEFAULT_URI",
118 cgiRewriteURL(attr
->values
[0].string
.text
,
119 url
, sizeof(url
), NULL
));
126 * See if we need to show a list of printers or the status of a
131 show_all_printers(http
, user
);
133 show_printer(http
, printer
);
139 const char *server_port
= getenv("SERVER_PORT");
140 /* Port number string */
141 int port
= atoi(server_port
? server_port
: "0");
143 char uri
[1024]; /* URL */
145 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
146 getenv("HTTPS") ? "https" : "http", NULL
,
147 getenv("SERVER_NAME"), port
, "/printers/%s", printer
);
149 printf("Location: %s\n\n", uri
);
151 else 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
, "cancel-jobs"))
162 do_printer_op(http
, printer
, IPP_OP_CANCEL_JOBS
, cgiText(_("Cancel Jobs")));
163 else if (!_cups_strcasecmp(op
, "print-self-test-page"))
164 cgiPrintCommand(http
, printer
, "PrintSelfTestPage",
165 cgiText(_("Print Self-Test Page")));
166 else if (!_cups_strcasecmp(op
, "clean-print-heads"))
167 cgiPrintCommand(http
, printer
, "Clean all",
168 cgiText(_("Clean Print Heads")));
169 else if (!_cups_strcasecmp(op
, "print-test-page"))
170 cgiPrintTestPage(http
, printer
);
171 else if (!_cups_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_OP_CANCEL_JOBS
)
283 cgiCopyTemplateLang("printer-cancel-jobs.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 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 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, first
);
386 i
< CUPS_PAGE_MAX
&& printer
;
387 i
++, printer
= (ipp_attribute_t
*)cupsArrayNext(printers
))
388 cgiSetIPPObjectVars(printer
, NULL
, i
);
391 * Save navigation URLs...
394 cgiSetVariable("THISURL", "/printers/");
398 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
399 cgiSetVariable("PREV", val
);
402 if ((first
+ CUPS_PAGE_MAX
) < count
)
404 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
405 cgiSetVariable("NEXT", val
);
408 if (count
> CUPS_PAGE_MAX
)
410 snprintf(val
, sizeof(val
), "%d", CUPS_PAGE_MAX
* (count
/ CUPS_PAGE_MAX
));
411 cgiSetVariable("LAST", val
);
415 * Then show everything...
418 cgiCopyTemplateLang("search.tmpl");
420 cgiCopyTemplateLang("printers-header.tmpl");
422 if (count
> CUPS_PAGE_MAX
)
423 cgiCopyTemplateLang("pager.tmpl");
425 cgiCopyTemplateLang("printers.tmpl");
427 if (count
> CUPS_PAGE_MAX
)
428 cgiCopyTemplateLang("pager.tmpl");
431 * Delete the response...
434 cupsArrayDelete(printers
);
443 cgiShowIPPError(_("Unable to get printer list"));
451 * 'show_printer()' - Show a single printer.
455 show_printer(http_t
*http
, /* I - Connection to server */
456 const char *printer
) /* I - Name of printer */
458 ipp_t
*request
, /* IPP request */
459 *response
; /* IPP response */
460 ipp_attribute_t
*attr
; /* IPP attribute */
461 char uri
[HTTP_MAX_URI
]; /* Printer URI */
462 char refresh
[1024]; /* Refresh URL */
465 fprintf(stderr
, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
466 http
, printer
? printer
: "(null)");
469 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
473 * attributes-natural-language
477 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
479 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
480 "localhost", 0, "/printers/%s", printer
);
481 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
484 cgiGetAttributes(request
, "printer.tmpl");
487 * Do the request and get back a response...
490 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
493 * Got the result; set the CGI variables and check the status of a
494 * single-queue request...
497 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
499 if (printer
&& (attr
= ippFindAttribute(response
, "printer-state",
500 IPP_TAG_ENUM
)) != NULL
&&
501 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
504 * Printer is processing - automatically refresh the page until we
505 * are done printing...
508 cgiFormEncode(uri
, printer
, sizeof(uri
));
509 snprintf(refresh
, sizeof(refresh
), "10;URL=/printers/%s", uri
);
510 cgiSetVariable("refresh_page", refresh
);
514 * Delete the response...
520 * Show the standard header...
523 cgiStartHTML(printer
);
526 * Show the printer status...
529 cgiCopyTemplateLang("printer.tmpl");
532 * Show jobs for the specified printer...
535 cgiCopyTemplateLang("printer-jobs-header.tmpl");
536 cgiShowJobs(http
, printer
);
541 * Show the IPP error...
544 cgiStartHTML(printer
);
545 cgiShowIPPError(_("Unable to get printer status"));