4 * Class status CGI for CUPS.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Main entry for CGI.
18 * do_class_op() - Do a class operation.
19 * show_all_classes() - Show all classes...
20 * show_class() - Show a single class.
24 * Include necessary headers...
27 #include "cgi-private.h"
34 static void do_class_op(http_t
*http
, const char *printer
, ipp_op_t op
,
36 static void show_all_classes(http_t
*http
, const char *username
);
37 static void show_class(http_t
*http
, const char *printer
);
41 * 'main()' - Main entry for CGI.
44 int /* O - Exit status */
45 main(int argc
, /* I - Number of command-line arguments */
46 char *argv
[]) /* I - Command-line arguments */
48 const char *pclass
; /* Class name */
49 const char *user
; /* Username */
50 http_t
*http
; /* Connection to the server */
51 ipp_t
*request
, /* IPP request */
52 *response
; /* IPP response */
53 ipp_attribute_t
*attr
; /* IPP attribute */
54 const char *op
; /* Operation to perform, if any */
55 static const char *def_attrs
[] = /* Attributes for default printer */
58 "printer-uri-supported"
63 * Get any form variables...
68 op
= cgiGetVariable("OP");
71 * Set the web interface section...
74 cgiSetVariable("SECTION", "classes");
75 cgiSetVariable("REFRESH_PAGE", "");
78 * See if we are displaying a printer or all classes...
81 if ((pclass
= getenv("PATH_INFO")) != NULL
)
89 cgiSetVariable("PRINTER_NAME", pclass
);
93 * See who is logged in...
96 user
= getenv("REMOTE_USER");
99 * Connect to the HTTP server...
102 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
105 * Get the default printer...
108 if (!op
|| !cgiIsPOST())
111 * Get the default destination...
114 request
= ippNewRequest(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_RESUME_PRINTER
, cgiText(_("Resume Class")));
166 else if (!strcmp(op
, "stop-class"))
167 do_class_op(http
, pclass
, IPP_PAUSE_PRINTER
, cgiText(_("Pause Class")));
168 else if (!strcmp(op
, "accept-jobs"))
169 do_class_op(http
, pclass
, CUPS_ACCEPT_JOBS
, cgiText(_("Accept Jobs")));
170 else if (!strcmp(op
, "reject-jobs"))
171 do_class_op(http
, pclass
, CUPS_REJECT_JOBS
, cgiText(_("Reject Jobs")));
172 else if (!strcmp(op
, "purge-jobs"))
173 do_class_op(http
, pclass
, IPP_PURGE_JOBS
, cgiText(_("Purge 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 (cupsLastError() == IPP_NOT_AUTHORIZED
)
254 puts("Status: 401\n");
257 else if (cupsLastError() > IPP_OK_CONFLICT
)
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_PAUSE_PRINTER
)
282 cgiCopyTemplateLang("printer-stop.tmpl");
283 else if (op
== IPP_RESUME_PRINTER
)
284 cgiCopyTemplateLang("printer-start.tmpl");
285 else if (op
== CUPS_ACCEPT_JOBS
)
286 cgiCopyTemplateLang("printer-accept.tmpl");
287 else if (op
== CUPS_REJECT_JOBS
)
288 cgiCopyTemplateLang("printer-reject.tmpl");
289 else if (op
== IPP_PURGE_JOBS
)
290 cgiCopyTemplateLang("printer-purge.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 ascending
, /* Order of classes (0 = descending) */
311 first
, /* First class to show */
312 count
; /* Number of classes */
313 const char *var
; /* Form variable */
314 void *search
; /* Search data */
315 char val
[1024]; /* Form variable */
319 * Show the standard header...
322 cgiStartHTML(cgiText(_("Classes")));
325 * Build a CUPS_GET_CLASSES request, which requires the following
329 * attributes-natural-language
330 * requesting-user-name
333 request
= ippNewRequest(CUPS_GET_CLASSES
);
336 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
337 "requesting-user-name", NULL
, user
);
339 cgiGetAttributes(request
, "classes.tmpl");
342 * Do the request and get back a response...
345 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
348 * Get a list of matching job objects.
351 if ((var
= cgiGetVariable("QUERY")) != NULL
&&
352 !cgiGetVariable("CLEAR"))
353 search
= cgiCompileSearch(var
);
357 classes
= cgiGetIPPObjects(response
, search
);
358 count
= cupsArrayCount(classes
);
361 cgiFreeSearch(search
);
364 * Figure out which classes to display...
367 if ((var
= cgiGetVariable("FIRST")) != NULL
)
373 first
= count
- CUPS_PAGE_MAX
;
375 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
380 sprintf(val
, "%d", count
);
381 cgiSetVariable("TOTAL", val
);
383 if ((var
= cgiGetVariable("ORDER")) != NULL
&& *var
)
384 ascending
= !_cups_strcasecmp(var
, "asc");
390 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, first
);
391 i
< CUPS_PAGE_MAX
&& pclass
;
392 i
++, pclass
= (ipp_attribute_t
*)cupsArrayNext(classes
))
393 cgiSetIPPObjectVars(pclass
, NULL
, i
);
397 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, count
- first
- 1);
398 i
< CUPS_PAGE_MAX
&& pclass
;
399 i
++, pclass
= (ipp_attribute_t
*)cupsArrayPrev(classes
))
400 cgiSetIPPObjectVars(pclass
, NULL
, i
);
404 * Save navigation URLs...
407 cgiSetVariable("THISURL", "/classes/");
411 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
412 cgiSetVariable("PREV", val
);
415 if ((first
+ CUPS_PAGE_MAX
) < count
)
417 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
418 cgiSetVariable("NEXT", val
);
422 * Then show everything...
425 cgiCopyTemplateLang("search.tmpl");
427 cgiCopyTemplateLang("classes-header.tmpl");
429 if (count
> CUPS_PAGE_MAX
)
430 cgiCopyTemplateLang("pager.tmpl");
432 cgiCopyTemplateLang("classes.tmpl");
434 if (count
> CUPS_PAGE_MAX
)
435 cgiCopyTemplateLang("pager.tmpl");
438 * Delete the response...
441 cupsArrayDelete(classes
);
450 cgiShowIPPError(_("Unable to get class list"));
458 * 'show_class()' - Show a single class.
462 show_class(http_t
*http
, /* I - Connection to server */
463 const char *pclass
) /* I - Name of class */
465 ipp_t
*request
, /* IPP request */
466 *response
; /* IPP response */
467 ipp_attribute_t
*attr
; /* IPP attribute */
468 char uri
[HTTP_MAX_URI
]; /* Printer URI */
469 char refresh
[1024]; /* Refresh URL */
473 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
477 * attributes-natural-language
481 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
483 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
484 "localhost", 0, "/classes/%s", pclass
);
485 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
488 cgiGetAttributes(request
, "class.tmpl");
491 * Do the request and get back a response...
494 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
497 * Got the result; set the CGI variables and check the status of a
498 * single-queue request...
501 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
503 if (pclass
&& (attr
= ippFindAttribute(response
, "printer-state",
504 IPP_TAG_ENUM
)) != NULL
&&
505 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
508 * Class is processing - automatically refresh the page until we
509 * are done printing...
512 cgiFormEncode(uri
, pclass
, sizeof(uri
));
513 snprintf(refresh
, sizeof(refresh
), "10;URL=/classes/%s", uri
);
514 cgiSetVariable("refresh_page", refresh
);
518 * Delete the response...
524 * Show the standard header...
527 cgiStartHTML(pclass
);
530 * Show the class status...
533 cgiCopyTemplateLang("class.tmpl");
536 * Show jobs for the specified class...
539 cgiCopyTemplateLang("class-jobs-header.tmpl");
540 cgiShowJobs(http
, pclass
);
545 * Show the IPP error...
548 cgiStartHTML(pclass
);
549 cgiShowIPPError(_("Unable to get class status"));