2 * Class status CGI for CUPS.
4 * Copyright © 2020-2025 by OpenPrinting.
5 * Copyright © 2007-2016 by Apple Inc.
6 * Copyright © 1997-2006 by Easy Software Products.
8 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 #include "cgi-private.h"
19 static void do_class_op(http_t
*http
, const char *printer
, ipp_op_t op
,
21 static void show_all_classes(http_t
*http
, const char *username
);
22 static void show_class(http_t
*http
, const char *printer
);
26 * 'main()' - Main entry for CGI.
29 int /* O - Exit status */
32 const char *pclass
; /* Class name */
33 const char *user
; /* Username */
34 http_t
*http
; /* Connection to the server */
35 ipp_t
*request
, /* IPP request */
36 *response
; /* IPP response */
37 ipp_attribute_t
*attr
; /* IPP attribute */
38 const char *op
; /* Operation to perform, if any */
39 static const char *def_attrs
[] = /* Attributes for default printer */
42 "printer-uri-supported"
47 * Get any form variables...
52 op
= cgiGetVariable("OP");
55 * Set the web interface section...
58 cgiSetVariable("SECTION", "classes");
59 cgiSetVariable("REFRESH_PAGE", "");
62 * See if we are displaying a printer or all classes...
65 if ((pclass
= getenv("PATH_INFO")) != NULL
)
73 cgiSetVariable("PRINTER_NAME", pclass
);
77 * See who is logged in...
80 user
= getenv("REMOTE_USER");
83 * Connect to the HTTP server...
86 if ((http
= httpConnect2(cupsGetServer(), ippGetPort(), /*addrlist*/NULL
, AF_UNSPEC
, cupsGetEncryption(), /*blocking*/1, /*msec*/30000, /*cancel*/NULL
)) == NULL
)
88 fprintf(stderr
, "ERROR: Unable to connect to cupsd: %s\n", cupsGetErrorString());
89 fprintf(stderr
, "DEBUG: cupsGetServer()=\"%s\"\n", cupsGetServer() ? cupsGetServer() : "(null)");
90 fprintf(stderr
, "DEBUG: ippGetPort()=%d\n", ippGetPort());
91 fprintf(stderr
, "DEBUG: cupsGetEncryption()=%d\n", cupsGetEncryption());
96 const char *authorization
; /* HTTP_AUTHORIZATION value */
98 if ((authorization
= getenv("HTTP_AUTHORIZATION")) != NULL
&& !strncmp(authorization
, "Bearer ", 7))
99 httpSetAuthString(http
, "Bearer", authorization
+ 7);
102 fprintf(stderr
, "DEBUG: http=%p\n", (void *)http
);
105 * Get the default printer...
108 if (!op
|| !cgiIsPOST())
111 * Get the default destination...
114 request
= ippNewRequest(IPP_OP_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 classes or the status of a
144 show_all_classes(http
, user
);
146 show_class(http
, pclass
);
152 const char *server_port
= getenv("SERVER_PORT");
153 /* Port number string */
154 int port
= atoi(server_port
? server_port
: "0");
156 char uri
[1024]; /* URL */
158 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
159 getenv("HTTPS") ? "https" : "http", NULL
,
160 getenv("SERVER_NAME"), port
, "/classes/%s", pclass
);
162 printf("Location: %s\n\n", uri
);
164 else if (!strcmp(op
, "start-class"))
165 do_class_op(http
, pclass
, IPP_OP_RESUME_PRINTER
, cgiText(_("Resume Class")));
166 else if (!strcmp(op
, "stop-class"))
167 do_class_op(http
, pclass
, IPP_OP_PAUSE_PRINTER
, cgiText(_("Pause Class")));
168 else if (!strcmp(op
, "accept-jobs"))
169 do_class_op(http
, pclass
, IPP_OP_CUPS_ACCEPT_JOBS
, cgiText(_("Accept Jobs")));
170 else if (!strcmp(op
, "reject-jobs"))
171 do_class_op(http
, pclass
, IPP_OP_CUPS_REJECT_JOBS
, cgiText(_("Reject Jobs")));
172 else if (!strcmp(op
, "cancel-jobs"))
173 do_class_op(http
, pclass
, IPP_OP_CANCEL_JOBS
, cgiText(_("Cancel Jobs")));
174 else if (!_cups_strcasecmp(op
, "print-test-page"))
175 cgiPrintTestPage(http
, pclass
);
176 else if (!_cups_strcasecmp(op
, "move-jobs"))
177 cgiMoveJobs(http
, pclass
, 0);
181 * Unknown/bad operation...
184 cgiStartHTML(pclass
);
185 cgiCopyTemplateLang("error-op.tmpl");
192 * Unknown/bad operation...
195 cgiStartHTML(cgiText(_("Classes")));
196 cgiCopyTemplateLang("error-op.tmpl");
201 * Close the HTTP server connection...
207 * Return with no errors...
215 * 'do_class_op()' - Do a class operation.
219 do_class_op(http_t
*http
, /* I - HTTP connection */
220 const char *printer
, /* I - Printer name */
221 ipp_op_t op
, /* I - Operation to perform */
222 const char *title
) /* I - Title of page */
224 ipp_t
*request
; /* IPP request */
225 char uri
[HTTP_MAX_URI
], /* Printer URI */
226 resource
[HTTP_MAX_URI
]; /* Path for request */
230 * Build a printer request, which requires the following
234 * attributes-natural-language
238 request
= ippNewRequest(op
);
240 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
241 "localhost", 0, "/classes/%s", printer
);
242 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
246 * Do the request and get back a response...
249 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
);
250 ippDelete(cupsDoRequest(http
, request
, resource
));
252 if (cupsGetError() == IPP_STATUS_ERROR_NOT_AUTHORIZED
)
254 puts("Status: 401\n");
257 else if (cupsGetError() > IPP_STATUS_OK_CONFLICTING
)
260 cgiShowIPPError(_("Unable to do maintenance command"));
265 * Redirect successful updates back to the printer page...
268 char url
[1024], /* Printer/class URL */
269 refresh
[1024]; /* Refresh URL */
272 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
273 cgiFormEncode(uri
, url
, sizeof(uri
));
274 snprintf(refresh
, sizeof(refresh
), "5;URL=%s", uri
);
275 cgiSetVariable("refresh_page", refresh
);
279 cgiSetVariable("IS_CLASS", "YES");
281 if (op
== IPP_OP_PAUSE_PRINTER
)
282 cgiCopyTemplateLang("printer-stop.tmpl");
283 else if (op
== IPP_OP_RESUME_PRINTER
)
284 cgiCopyTemplateLang("printer-start.tmpl");
285 else if (op
== IPP_OP_CUPS_ACCEPT_JOBS
)
286 cgiCopyTemplateLang("printer-accept.tmpl");
287 else if (op
== IPP_OP_CUPS_REJECT_JOBS
)
288 cgiCopyTemplateLang("printer-reject.tmpl");
289 else if (op
== IPP_OP_CANCEL_JOBS
)
290 cgiCopyTemplateLang("printer-cancel-jobs.tmpl");
298 * 'show_all_classes()' - Show all classes...
302 show_all_classes(http_t
*http
, /* I - Connection to server */
303 const char *user
) /* I - Username */
305 int i
; /* Looping var */
306 ipp_t
*request
, /* IPP request */
307 *response
; /* IPP response */
308 cups_array_t
*classes
; /* Array of class objects */
309 ipp_attribute_t
*pclass
; /* Class object */
310 int first
, /* First class to show */
311 count
; /* Number of classes */
312 const char *var
; /* Form variable */
313 void *search
; /* Search data */
314 char val
[1024]; /* Form variable */
318 * Show the standard header...
321 cgiStartHTML(cgiText(_("Classes")));
324 * Build a CUPS_GET_CLASSES request, which requires the following
328 * attributes-natural-language
329 * requesting-user-name
332 request
= ippNewRequest(IPP_OP_CUPS_GET_CLASSES
);
335 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
336 "requesting-user-name", NULL
, user
);
338 cgiGetAttributes(request
, "classes.tmpl");
341 * Do the request and get back a response...
344 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
347 * Get a list of matching job objects.
350 if ((var
= cgiGetTextfield("QUERY")) != NULL
&&
351 !cgiGetVariable("CLEAR"))
352 search
= cgiCompileSearch(var
);
356 classes
= cgiGetIPPObjects(response
, search
);
357 count
= cupsArrayCount(classes
);
360 cgiFreeSearch(search
);
363 * Figure out which classes to display...
366 if ((var
= cgiGetVariable("FIRST")) != NULL
)
372 first
= count
- CUPS_PAGE_MAX
;
374 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
379 snprintf(val
, sizeof(val
), "%d", count
);
380 cgiSetVariable("TOTAL", val
);
382 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, first
);
383 i
< CUPS_PAGE_MAX
&& pclass
;
384 i
++, pclass
= (ipp_attribute_t
*)cupsArrayNext(classes
))
385 cgiSetIPPObjectVars(pclass
, NULL
, i
);
388 * Save navigation URLs...
391 cgiSetVariable("THISURL", "/classes/");
395 snprintf(val
, sizeof(val
), "%d", first
- CUPS_PAGE_MAX
);
396 cgiSetVariable("PREV", val
);
399 if ((first
+ CUPS_PAGE_MAX
) < count
)
401 snprintf(val
, sizeof(val
), "%d", first
+ CUPS_PAGE_MAX
);
402 cgiSetVariable("NEXT", val
);
405 if (count
> CUPS_PAGE_MAX
)
407 snprintf(val
, sizeof(val
), "%d", CUPS_PAGE_MAX
* (count
/ CUPS_PAGE_MAX
));
408 cgiSetVariable("LAST", val
);
412 * Then show everything...
415 cgiCopyTemplateLang("search.tmpl");
417 cgiCopyTemplateLang("classes-header.tmpl");
419 if (count
> CUPS_PAGE_MAX
)
420 cgiCopyTemplateLang("pager.tmpl");
422 cgiCopyTemplateLang("classes.tmpl");
424 if (count
> CUPS_PAGE_MAX
)
425 cgiCopyTemplateLang("pager.tmpl");
428 * Delete the response...
431 cupsArrayDelete(classes
);
440 cgiShowIPPError(_("Unable to get class list"));
448 * 'show_class()' - Show a single class.
452 show_class(http_t
*http
, /* I - Connection to server */
453 const char *pclass
) /* I - Name of class */
455 ipp_t
*request
, /* IPP request */
456 *response
; /* IPP response */
457 ipp_attribute_t
*attr
; /* IPP attribute */
458 char uri
[HTTP_MAX_URI
]; /* Printer URI */
459 char refresh
[1024]; /* Refresh URL */
463 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
467 * attributes-natural-language
471 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
473 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
474 "localhost", 0, "/classes/%s", pclass
);
475 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
478 cgiGetAttributes(request
, "class.tmpl");
481 * Do the request and get back a response...
484 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
487 * Got the result; set the CGI variables and check the status of a
488 * single-queue request...
491 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
493 if (pclass
&& (attr
= ippFindAttribute(response
, "printer-state",
494 IPP_TAG_ENUM
)) != NULL
&&
495 attr
->values
[0].integer
== IPP_PSTATE_PROCESSING
)
498 * Class is processing - automatically refresh the page until we
499 * are done printing...
502 cgiFormEncode(uri
, pclass
, sizeof(uri
));
503 snprintf(refresh
, sizeof(refresh
), "10;URL=/classes/%s", uri
);
504 cgiSetVariable("refresh_page", refresh
);
508 * Delete the response...
514 * Show the standard header...
517 cgiStartHTML(pclass
);
520 * Show the class status...
523 cgiCopyTemplateLang("class.tmpl");
526 * Show jobs for the specified class...
529 cgiCopyTemplateLang("class-jobs-header.tmpl");
530 cgiShowJobs(http
, pclass
);
535 * Show the IPP error...
538 cgiStartHTML(pclass
);
539 cgiShowIPPError(_("Unable to get class status"));