2 * "$Id: classes.c 7940 2008-09-16 00:45:16Z mike $"
4 * Class status CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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
);
150 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
, "purge-jobs"))
159 do_class_op(http
, pclass
, IPP_PURGE_JOBS
, cgiText(_("Purge Jobs")));
160 else if (!strcasecmp(op
, "print-test-page"))
161 cgiPrintTestPage(http
, pclass
);
162 else if (!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_PURGE_JOBS
)
276 cgiCopyTemplateLang("printer-purge.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 ascending
, /* Order of classes (0 = descending) */
297 first
, /* First class to show */
298 count
; /* Number of classes */
299 const char *var
; /* Form variable */
300 void *search
; /* Search data */
301 char val
[1024]; /* Form variable */
305 * Show the standard header...
308 cgiStartHTML(cgiText(_("Classes")));
311 * Build a CUPS_GET_CLASSES request, which requires the following
315 * attributes-natural-language
316 * requesting-user-name
319 request
= ippNewRequest(CUPS_GET_CLASSES
);
322 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
323 "requesting-user-name", NULL
, user
);
325 cgiGetAttributes(request
, "classes.tmpl");
328 * Do the request and get back a response...
331 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
334 * Get a list of matching job objects.
337 if ((var
= cgiGetVariable("QUERY")) != NULL
&&
338 !cgiGetVariable("CLEAR"))
339 search
= cgiCompileSearch(var
);
343 classes
= cgiGetIPPObjects(response
, search
);
344 count
= cupsArrayCount(classes
);
347 cgiFreeSearch(search
);
350 * Figure out which classes to display...
353 if ((var
= cgiGetVariable("FIRST")) != NULL
)
359 first
= count
- CUPS_PAGE_MAX
;
361 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
366 sprintf(val
, "%d", count
);
367 cgiSetVariable("TOTAL", val
);
369 if ((var
= cgiGetVariable("ORDER")) != NULL
)
370 ascending
= !strcasecmp(var
, "asc");
376 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, first
);
377 i
< CUPS_PAGE_MAX
&& pclass
;
378 i
++, pclass
= (ipp_attribute_t
*)cupsArrayNext(classes
))
379 cgiSetIPPObjectVars(pclass
, NULL
, i
);
383 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, count
- first
- 1);
384 i
< CUPS_PAGE_MAX
&& pclass
;
385 i
++, pclass
= (ipp_attribute_t
*)cupsArrayPrev(classes
))
386 cgiSetIPPObjectVars(pclass
, NULL
, i
);
390 * Save navigation URLs...
393 cgiSetVariable("THISURL", "/classes/");
397 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
398 cgiSetVariable("PREV", val
);
401 if ((first
+ CUPS_PAGE_MAX
) < count
)
403 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
404 cgiSetVariable("NEXT", val
);
408 * Then show everything...
411 cgiCopyTemplateLang("search.tmpl");
413 cgiCopyTemplateLang("classes-header.tmpl");
415 if (count
> CUPS_PAGE_MAX
)
416 cgiCopyTemplateLang("pager.tmpl");
418 cgiCopyTemplateLang("classes.tmpl");
420 if (count
> CUPS_PAGE_MAX
)
421 cgiCopyTemplateLang("pager.tmpl");
424 * Delete the response...
427 cupsArrayDelete(classes
);
436 cgiShowIPPError(_("Unable to get class list:"));
444 * 'show_class()' - Show a single class.
448 show_class(http_t
*http
, /* I - Connection to server */
449 const char *pclass
) /* I - Name of class */
451 ipp_t
*request
, /* IPP request */
452 *response
; /* IPP response */
453 ipp_attribute_t
*attr
; /* IPP attribute */
454 char uri
[HTTP_MAX_URI
]; /* Printer URI */
455 char refresh
[1024]; /* Refresh URL */
459 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
463 * attributes-natural-language
467 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
469 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
470 "localhost", 0, "/classes/%s", pclass
);
471 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
474 cgiGetAttributes(request
, "class.tmpl");
477 * Do the request and get back a response...
480 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
483 * Got the result; set the CGI variables and check the status of a
484 * single-queue request...
487 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
489 if (pclass
&& (attr
= ippFindAttribute(response
, "printer-state",
490 IPP_TAG_ENUM
)) != NULL
&&
491 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
494 * Class is processing - automatically refresh the page until we
495 * are done printing...
498 cgiFormEncode(uri
, pclass
, sizeof(uri
));
499 snprintf(refresh
, sizeof(refresh
), "10;URL=/classes/%s", uri
);
500 cgiSetVariable("refresh_page", refresh
);
504 * Delete the response...
510 * Show the standard header...
513 cgiStartHTML(pclass
);
516 * Show the class status...
519 cgiCopyTemplateLang("class.tmpl");
522 * Show jobs for the specified class...
525 cgiCopyTemplateLang("class-jobs-header.tmpl");
526 cgiShowJobs(http
, pclass
);
531 * Show the IPP error...
534 cgiStartHTML(pclass
);
535 cgiShowIPPError(_("Unable to get class status:"));
543 * End of "$Id: classes.c 7940 2008-09-16 00:45:16Z mike $".