2 * Class 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"
25 static void do_class_op(http_t
*http
, const char *printer
, ipp_op_t op
,
27 static void show_all_classes(http_t
*http
, const char *username
);
28 static void show_class(http_t
*http
, const char *printer
);
32 * 'main()' - Main entry for CGI.
35 int /* O - Exit status */
38 const char *pclass
; /* Class name */
39 const char *user
; /* Username */
40 http_t
*http
; /* Connection to the server */
41 ipp_t
*request
, /* IPP request */
42 *response
; /* IPP response */
43 ipp_attribute_t
*attr
; /* IPP attribute */
44 const char *op
; /* Operation to perform, if any */
45 static const char *def_attrs
[] = /* Attributes for default printer */
48 "printer-uri-supported"
53 * Get any form variables...
58 op
= cgiGetVariable("OP");
61 * Set the web interface section...
64 cgiSetVariable("SECTION", "classes");
65 cgiSetVariable("REFRESH_PAGE", "");
68 * See if we are displaying a printer or all classes...
71 if ((pclass
= getenv("PATH_INFO")) != NULL
)
79 cgiSetVariable("PRINTER_NAME", pclass
);
83 * See who is logged in...
86 user
= getenv("REMOTE_USER");
89 * Connect to the HTTP server...
92 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
95 * Get the default printer...
98 if (!op
|| !cgiIsPOST())
101 * Get the default destination...
104 request
= ippNewRequest(CUPS_GET_DEFAULT
);
106 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
107 "requested-attributes",
108 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
110 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
112 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
113 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
115 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
117 char url
[HTTP_MAX_URI
]; /* New URL */
120 cgiSetVariable("DEFAULT_URI",
121 cgiRewriteURL(attr
->values
[0].string
.text
,
122 url
, sizeof(url
), NULL
));
129 * See if we need to show a list of classes or the status of a
134 show_all_classes(http
, user
);
136 show_class(http
, pclass
);
142 const char *server_port
= getenv("SERVER_PORT");
143 /* Port number string */
144 int port
= atoi(server_port
? server_port
: "0");
146 char uri
[1024]; /* URL */
148 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
149 getenv("HTTPS") ? "https" : "http", NULL
,
150 getenv("SERVER_NAME"), port
, "/classes/%s", pclass
);
152 printf("Location: %s\n\n", uri
);
154 else if (!strcmp(op
, "start-class"))
155 do_class_op(http
, pclass
, IPP_RESUME_PRINTER
, cgiText(_("Resume Class")));
156 else if (!strcmp(op
, "stop-class"))
157 do_class_op(http
, pclass
, IPP_PAUSE_PRINTER
, cgiText(_("Pause Class")));
158 else if (!strcmp(op
, "accept-jobs"))
159 do_class_op(http
, pclass
, CUPS_ACCEPT_JOBS
, cgiText(_("Accept Jobs")));
160 else if (!strcmp(op
, "reject-jobs"))
161 do_class_op(http
, pclass
, CUPS_REJECT_JOBS
, cgiText(_("Reject Jobs")));
162 else if (!strcmp(op
, "cancel-jobs"))
163 do_class_op(http
, pclass
, IPP_OP_CANCEL_JOBS
, cgiText(_("Cancel Jobs")));
164 else if (!_cups_strcasecmp(op
, "print-test-page"))
165 cgiPrintTestPage(http
, pclass
);
166 else if (!_cups_strcasecmp(op
, "move-jobs"))
167 cgiMoveJobs(http
, pclass
, 0);
171 * Unknown/bad operation...
174 cgiStartHTML(pclass
);
175 cgiCopyTemplateLang("error-op.tmpl");
182 * Unknown/bad operation...
185 cgiStartHTML(cgiText(_("Classes")));
186 cgiCopyTemplateLang("error-op.tmpl");
191 * Close the HTTP server connection...
197 * Return with no errors...
205 * 'do_class_op()' - Do a class operation.
209 do_class_op(http_t
*http
, /* I - HTTP connection */
210 const char *printer
, /* I - Printer name */
211 ipp_op_t op
, /* I - Operation to perform */
212 const char *title
) /* I - Title of page */
214 ipp_t
*request
; /* IPP request */
215 char uri
[HTTP_MAX_URI
], /* Printer URI */
216 resource
[HTTP_MAX_URI
]; /* Path for request */
220 * Build a printer request, which requires the following
224 * attributes-natural-language
228 request
= ippNewRequest(op
);
230 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
231 "localhost", 0, "/classes/%s", printer
);
232 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
236 * Do the request and get back a response...
239 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
);
240 ippDelete(cupsDoRequest(http
, request
, resource
));
242 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
244 puts("Status: 401\n");
247 else if (cupsLastError() > IPP_OK_CONFLICT
)
250 cgiShowIPPError(_("Unable to do maintenance command"));
255 * Redirect successful updates back to the printer page...
258 char url
[1024], /* Printer/class URL */
259 refresh
[1024]; /* Refresh URL */
262 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
263 cgiFormEncode(uri
, url
, sizeof(uri
));
264 snprintf(refresh
, sizeof(refresh
), "5;URL=%s", uri
);
265 cgiSetVariable("refresh_page", refresh
);
269 cgiSetVariable("IS_CLASS", "YES");
271 if (op
== IPP_PAUSE_PRINTER
)
272 cgiCopyTemplateLang("printer-stop.tmpl");
273 else if (op
== IPP_RESUME_PRINTER
)
274 cgiCopyTemplateLang("printer-start.tmpl");
275 else if (op
== CUPS_ACCEPT_JOBS
)
276 cgiCopyTemplateLang("printer-accept.tmpl");
277 else if (op
== CUPS_REJECT_JOBS
)
278 cgiCopyTemplateLang("printer-reject.tmpl");
279 else if (op
== IPP_OP_CANCEL_JOBS
)
280 cgiCopyTemplateLang("printer-cancel-jobs.tmpl");
288 * 'show_all_classes()' - Show all classes...
292 show_all_classes(http_t
*http
, /* I - Connection to server */
293 const char *user
) /* I - Username */
295 int i
; /* Looping var */
296 ipp_t
*request
, /* IPP request */
297 *response
; /* IPP response */
298 cups_array_t
*classes
; /* Array of class objects */
299 ipp_attribute_t
*pclass
; /* Class object */
300 int ascending
, /* Order of classes (0 = descending) */
301 first
, /* First class to show */
302 count
; /* Number of classes */
303 const char *var
; /* Form variable */
304 void *search
; /* Search data */
305 char val
[1024]; /* Form variable */
309 * Show the standard header...
312 cgiStartHTML(cgiText(_("Classes")));
315 * Build a CUPS_GET_CLASSES request, which requires the following
319 * attributes-natural-language
320 * requesting-user-name
323 request
= ippNewRequest(CUPS_GET_CLASSES
);
326 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
327 "requesting-user-name", NULL
, user
);
329 cgiGetAttributes(request
, "classes.tmpl");
332 * Do the request and get back a response...
335 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
338 * Get a list of matching job objects.
341 if ((var
= cgiGetVariable("QUERY")) != NULL
&&
342 !cgiGetVariable("CLEAR"))
343 search
= cgiCompileSearch(var
);
347 classes
= cgiGetIPPObjects(response
, search
);
348 count
= cupsArrayCount(classes
);
351 cgiFreeSearch(search
);
354 * Figure out which classes to display...
357 if ((var
= cgiGetVariable("FIRST")) != NULL
)
363 first
= count
- CUPS_PAGE_MAX
;
365 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
370 sprintf(val
, "%d", count
);
371 cgiSetVariable("TOTAL", val
);
373 if ((var
= cgiGetVariable("ORDER")) != NULL
&& *var
)
374 ascending
= !_cups_strcasecmp(var
, "asc");
380 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, first
);
381 i
< CUPS_PAGE_MAX
&& pclass
;
382 i
++, pclass
= (ipp_attribute_t
*)cupsArrayNext(classes
))
383 cgiSetIPPObjectVars(pclass
, NULL
, i
);
387 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, count
- first
- 1);
388 i
< CUPS_PAGE_MAX
&& pclass
;
389 i
++, pclass
= (ipp_attribute_t
*)cupsArrayPrev(classes
))
390 cgiSetIPPObjectVars(pclass
, NULL
, i
);
394 * Save navigation URLs...
397 cgiSetVariable("THISURL", "/classes/");
401 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
402 cgiSetVariable("PREV", val
);
405 if ((first
+ CUPS_PAGE_MAX
) < count
)
407 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
408 cgiSetVariable("NEXT", 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_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_PRINTER_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"));