2 * Class 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"
21 static void do_class_op(http_t
*http
, const char *printer
, ipp_op_t op
,
23 static void show_all_classes(http_t
*http
, const char *username
);
24 static void show_class(http_t
*http
, const char *printer
);
28 * 'main()' - Main entry for CGI.
31 int /* O - Exit status */
34 const char *pclass
; /* Class name */
35 const char *user
; /* Username */
36 http_t
*http
; /* Connection to the server */
37 ipp_t
*request
, /* IPP request */
38 *response
; /* IPP response */
39 ipp_attribute_t
*attr
; /* IPP attribute */
40 const char *op
; /* Operation to perform, if any */
41 static const char *def_attrs
[] = /* Attributes for default printer */
44 "printer-uri-supported"
49 * Get any form variables...
54 op
= cgiGetVariable("OP");
57 * Set the web interface section...
60 cgiSetVariable("SECTION", "classes");
61 cgiSetVariable("REFRESH_PAGE", "");
64 * See if we are displaying a printer or all classes...
67 if ((pclass
= getenv("PATH_INFO")) != NULL
)
75 cgiSetVariable("PRINTER_NAME", pclass
);
79 * See who is logged in...
82 user
= getenv("REMOTE_USER");
85 * Connect to the HTTP server...
88 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
91 * Get the default printer...
94 if (!op
|| !cgiIsPOST())
97 * Get the default destination...
100 request
= ippNewRequest(CUPS_GET_DEFAULT
);
102 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
103 "requested-attributes",
104 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
106 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
108 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
109 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
111 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
113 char url
[HTTP_MAX_URI
]; /* New URL */
116 cgiSetVariable("DEFAULT_URI",
117 cgiRewriteURL(attr
->values
[0].string
.text
,
118 url
, sizeof(url
), NULL
));
125 * See if we need to show a list of classes or the status of a
130 show_all_classes(http
, user
);
132 show_class(http
, pclass
);
138 const char *server_port
= getenv("SERVER_PORT");
139 /* Port number string */
140 int port
= atoi(server_port
? server_port
: "0");
142 char uri
[1024]; /* URL */
144 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
145 getenv("HTTPS") ? "https" : "http", NULL
,
146 getenv("SERVER_NAME"), port
, "/classes/%s", pclass
);
148 printf("Location: %s\n\n", uri
);
150 else if (!strcmp(op
, "start-class"))
151 do_class_op(http
, pclass
, IPP_RESUME_PRINTER
, cgiText(_("Resume Class")));
152 else if (!strcmp(op
, "stop-class"))
153 do_class_op(http
, pclass
, IPP_PAUSE_PRINTER
, cgiText(_("Pause Class")));
154 else if (!strcmp(op
, "accept-jobs"))
155 do_class_op(http
, pclass
, CUPS_ACCEPT_JOBS
, cgiText(_("Accept Jobs")));
156 else if (!strcmp(op
, "reject-jobs"))
157 do_class_op(http
, pclass
, CUPS_REJECT_JOBS
, cgiText(_("Reject Jobs")));
158 else if (!strcmp(op
, "cancel-jobs"))
159 do_class_op(http
, pclass
, IPP_OP_CANCEL_JOBS
, cgiText(_("Cancel Jobs")));
160 else if (!_cups_strcasecmp(op
, "print-test-page"))
161 cgiPrintTestPage(http
, pclass
);
162 else if (!_cups_strcasecmp(op
, "move-jobs"))
163 cgiMoveJobs(http
, pclass
, 0);
167 * Unknown/bad operation...
170 cgiStartHTML(pclass
);
171 cgiCopyTemplateLang("error-op.tmpl");
178 * Unknown/bad operation...
181 cgiStartHTML(cgiText(_("Classes")));
182 cgiCopyTemplateLang("error-op.tmpl");
187 * Close the HTTP server connection...
193 * Return with no errors...
201 * 'do_class_op()' - Do a class operation.
205 do_class_op(http_t
*http
, /* I - HTTP connection */
206 const char *printer
, /* I - Printer name */
207 ipp_op_t op
, /* I - Operation to perform */
208 const char *title
) /* I - Title of page */
210 ipp_t
*request
; /* IPP request */
211 char uri
[HTTP_MAX_URI
], /* Printer URI */
212 resource
[HTTP_MAX_URI
]; /* Path for request */
216 * Build a printer request, which requires the following
220 * attributes-natural-language
224 request
= ippNewRequest(op
);
226 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
227 "localhost", 0, "/classes/%s", printer
);
228 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
232 * Do the request and get back a response...
235 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
);
236 ippDelete(cupsDoRequest(http
, request
, resource
));
238 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
240 puts("Status: 401\n");
243 else if (cupsLastError() > IPP_OK_CONFLICT
)
246 cgiShowIPPError(_("Unable to do maintenance command"));
251 * Redirect successful updates back to the printer page...
254 char url
[1024], /* Printer/class URL */
255 refresh
[1024]; /* Refresh URL */
258 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
259 cgiFormEncode(uri
, url
, sizeof(uri
));
260 snprintf(refresh
, sizeof(refresh
), "5;URL=%s", uri
);
261 cgiSetVariable("refresh_page", refresh
);
265 cgiSetVariable("IS_CLASS", "YES");
267 if (op
== IPP_PAUSE_PRINTER
)
268 cgiCopyTemplateLang("printer-stop.tmpl");
269 else if (op
== IPP_RESUME_PRINTER
)
270 cgiCopyTemplateLang("printer-start.tmpl");
271 else if (op
== CUPS_ACCEPT_JOBS
)
272 cgiCopyTemplateLang("printer-accept.tmpl");
273 else if (op
== CUPS_REJECT_JOBS
)
274 cgiCopyTemplateLang("printer-reject.tmpl");
275 else if (op
== IPP_OP_CANCEL_JOBS
)
276 cgiCopyTemplateLang("printer-cancel-jobs.tmpl");
284 * 'show_all_classes()' - Show all classes...
288 show_all_classes(http_t
*http
, /* I - Connection to server */
289 const char *user
) /* I - Username */
291 int i
; /* Looping var */
292 ipp_t
*request
, /* IPP request */
293 *response
; /* IPP response */
294 cups_array_t
*classes
; /* Array of class objects */
295 ipp_attribute_t
*pclass
; /* Class object */
296 int first
, /* First class to show */
297 count
; /* Number of classes */
298 const char *var
; /* Form variable */
299 void *search
; /* Search data */
300 char val
[1024]; /* Form variable */
304 * Show the standard header...
307 cgiStartHTML(cgiText(_("Classes")));
310 * Build a CUPS_GET_CLASSES request, which requires the following
314 * attributes-natural-language
315 * requesting-user-name
318 request
= ippNewRequest(CUPS_GET_CLASSES
);
321 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
322 "requesting-user-name", NULL
, user
);
324 cgiGetAttributes(request
, "classes.tmpl");
327 * Do the request and get back a response...
330 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
333 * Get a list of matching job objects.
336 if ((var
= cgiGetVariable("QUERY")) != NULL
&&
337 !cgiGetVariable("CLEAR"))
338 search
= cgiCompileSearch(var
);
342 classes
= cgiGetIPPObjects(response
, search
);
343 count
= cupsArrayCount(classes
);
346 cgiFreeSearch(search
);
349 * Figure out which classes to display...
352 if ((var
= cgiGetVariable("FIRST")) != NULL
)
358 first
= count
- CUPS_PAGE_MAX
;
360 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
365 sprintf(val
, "%d", count
);
366 cgiSetVariable("TOTAL", val
);
368 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, first
);
369 i
< CUPS_PAGE_MAX
&& pclass
;
370 i
++, pclass
= (ipp_attribute_t
*)cupsArrayNext(classes
))
371 cgiSetIPPObjectVars(pclass
, NULL
, i
);
374 * Save navigation URLs...
377 cgiSetVariable("THISURL", "/classes/");
381 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
382 cgiSetVariable("PREV", val
);
385 if ((first
+ CUPS_PAGE_MAX
) < count
)
387 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
388 cgiSetVariable("NEXT", val
);
391 if (count
> CUPS_PAGE_MAX
)
393 snprintf(val
, sizeof(val
), "%d", CUPS_PAGE_MAX
* (count
/ CUPS_PAGE_MAX
));
394 cgiSetVariable("LAST", val
);
398 * Then show everything...
401 cgiCopyTemplateLang("search.tmpl");
403 cgiCopyTemplateLang("classes-header.tmpl");
405 if (count
> CUPS_PAGE_MAX
)
406 cgiCopyTemplateLang("pager.tmpl");
408 cgiCopyTemplateLang("classes.tmpl");
410 if (count
> CUPS_PAGE_MAX
)
411 cgiCopyTemplateLang("pager.tmpl");
414 * Delete the response...
417 cupsArrayDelete(classes
);
426 cgiShowIPPError(_("Unable to get class list"));
434 * 'show_class()' - Show a single class.
438 show_class(http_t
*http
, /* I - Connection to server */
439 const char *pclass
) /* I - Name of class */
441 ipp_t
*request
, /* IPP request */
442 *response
; /* IPP response */
443 ipp_attribute_t
*attr
; /* IPP attribute */
444 char uri
[HTTP_MAX_URI
]; /* Printer URI */
445 char refresh
[1024]; /* Refresh URL */
449 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
453 * attributes-natural-language
457 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
459 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
460 "localhost", 0, "/classes/%s", pclass
);
461 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
464 cgiGetAttributes(request
, "class.tmpl");
467 * Do the request and get back a response...
470 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
473 * Got the result; set the CGI variables and check the status of a
474 * single-queue request...
477 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
479 if (pclass
&& (attr
= ippFindAttribute(response
, "printer-state",
480 IPP_TAG_ENUM
)) != NULL
&&
481 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
484 * Class is processing - automatically refresh the page until we
485 * are done printing...
488 cgiFormEncode(uri
, pclass
, sizeof(uri
));
489 snprintf(refresh
, sizeof(refresh
), "10;URL=/classes/%s", uri
);
490 cgiSetVariable("refresh_page", refresh
);
494 * Delete the response...
500 * Show the standard header...
503 cgiStartHTML(pclass
);
506 * Show the class status...
509 cgiCopyTemplateLang("class.tmpl");
512 * Show jobs for the specified class...
515 cgiCopyTemplateLang("class-jobs-header.tmpl");
516 cgiShowJobs(http
, pclass
);
521 * Show the IPP error...
524 cgiStartHTML(pclass
);
525 cgiShowIPPError(_("Unable to get class status"));