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");
77 * See if we are displaying a printer or all classes...
80 if ((pclass
= getenv("PATH_INFO")) != NULL
)
88 cgiSetVariable("PRINTER_NAME", pclass
);
92 * See who is logged in...
95 user
= getenv("REMOTE_USER");
98 * Connect to the HTTP server...
101 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
104 * Get the default printer...
107 if (!op
|| !cgiIsPOST())
110 * Get the default destination...
113 request
= ippNewRequest(CUPS_GET_DEFAULT
);
115 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
116 "requested-attributes",
117 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
119 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
121 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
122 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
124 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
126 char url
[HTTP_MAX_URI
]; /* New URL */
129 cgiSetVariable("DEFAULT_URI",
130 cgiRewriteURL(attr
->values
[0].string
.text
,
131 url
, sizeof(url
), NULL
));
138 * See if we need to show a list of classes or the status of a
143 show_all_classes(http
, user
);
145 show_class(http
, pclass
);
149 if (!strcmp(op
, "start-class"))
150 do_class_op(http
, pclass
, IPP_RESUME_PRINTER
, cgiText(_("Resume Class")));
151 else if (!strcmp(op
, "stop-class"))
152 do_class_op(http
, pclass
, IPP_PAUSE_PRINTER
, cgiText(_("Pause Class")));
153 else if (!strcmp(op
, "accept-jobs"))
154 do_class_op(http
, pclass
, CUPS_ACCEPT_JOBS
, cgiText(_("Accept Jobs")));
155 else if (!strcmp(op
, "reject-jobs"))
156 do_class_op(http
, pclass
, CUPS_REJECT_JOBS
, cgiText(_("Reject Jobs")));
157 else if (!strcmp(op
, "purge-jobs"))
158 do_class_op(http
, pclass
, IPP_PURGE_JOBS
, cgiText(_("Purge Jobs")));
159 else if (!strcasecmp(op
, "print-test-page"))
160 cgiPrintTestPage(http
, pclass
);
161 else if (!strcasecmp(op
, "move-jobs"))
162 cgiMoveJobs(http
, pclass
, 0);
166 * Unknown/bad operation...
169 cgiStartHTML(pclass
);
170 cgiCopyTemplateLang("error-op.tmpl");
177 * Unknown/bad operation...
180 cgiStartHTML(cgiText(_("Classes")));
181 cgiCopyTemplateLang("error-op.tmpl");
186 * Close the HTTP server connection...
192 * Return with no errors...
200 * 'do_class_op()' - Do a class operation.
204 do_class_op(http_t
*http
, /* I - HTTP connection */
205 const char *printer
, /* I - Printer name */
206 ipp_op_t op
, /* I - Operation to perform */
207 const char *title
) /* I - Title of page */
209 ipp_t
*request
; /* IPP request */
210 char uri
[HTTP_MAX_URI
], /* Printer URI */
211 resource
[HTTP_MAX_URI
]; /* Path for request */
215 * Build a printer request, which requires the following
219 * attributes-natural-language
223 request
= ippNewRequest(op
);
225 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
226 "localhost", 0, "/classes/%s", printer
);
227 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
231 * Do the request and get back a response...
234 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
);
235 ippDelete(cupsDoRequest(http
, request
, resource
));
237 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
239 puts("Status: 401\n");
242 else if (cupsLastError() > IPP_OK_CONFLICT
)
245 cgiShowIPPError(_("Unable to do maintenance command:"));
250 * Redirect successful updates back to the printer page...
253 char url
[1024], /* Printer/class URL */
254 refresh
[1024]; /* Refresh URL */
257 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
258 cgiFormEncode(uri
, url
, sizeof(uri
));
259 snprintf(refresh
, sizeof(refresh
), "5;URL=%s", uri
);
260 cgiSetVariable("refresh_page", refresh
);
264 cgiSetVariable("IS_CLASS", "YES");
266 if (op
== IPP_PAUSE_PRINTER
)
267 cgiCopyTemplateLang("printer-stop.tmpl");
268 else if (op
== IPP_RESUME_PRINTER
)
269 cgiCopyTemplateLang("printer-start.tmpl");
270 else if (op
== CUPS_ACCEPT_JOBS
)
271 cgiCopyTemplateLang("printer-accept.tmpl");
272 else if (op
== CUPS_REJECT_JOBS
)
273 cgiCopyTemplateLang("printer-reject.tmpl");
274 else if (op
== IPP_PURGE_JOBS
)
275 cgiCopyTemplateLang("printer-purge.tmpl");
283 * 'show_all_classes()' - Show all classes...
287 show_all_classes(http_t
*http
, /* I - Connection to server */
288 const char *user
) /* I - Username */
290 int i
; /* Looping var */
291 ipp_t
*request
, /* IPP request */
292 *response
; /* IPP response */
293 cups_array_t
*classes
; /* Array of class objects */
294 ipp_attribute_t
*pclass
; /* Class object */
295 int ascending
, /* Order of classes (0 = descending) */
296 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 if ((var
= cgiGetVariable("ORDER")) != NULL
)
369 ascending
= !strcasecmp(var
, "asc");
375 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, first
);
376 i
< CUPS_PAGE_MAX
&& pclass
;
377 i
++, pclass
= (ipp_attribute_t
*)cupsArrayNext(classes
))
378 cgiSetIPPObjectVars(pclass
, NULL
, i
);
382 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, count
- first
- 1);
383 i
< CUPS_PAGE_MAX
&& pclass
;
384 i
++, pclass
= (ipp_attribute_t
*)cupsArrayPrev(classes
))
385 cgiSetIPPObjectVars(pclass
, NULL
, i
);
389 * Save navigation URLs...
392 cgiSetVariable("THISURL", "/classes/");
396 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
397 cgiSetVariable("PREV", val
);
400 if ((first
+ CUPS_PAGE_MAX
) < count
)
402 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
403 cgiSetVariable("NEXT", val
);
407 * Then show everything...
410 cgiCopyTemplateLang("search.tmpl");
412 cgiCopyTemplateLang("classes-header.tmpl");
414 if (count
> CUPS_PAGE_MAX
)
415 cgiCopyTemplateLang("pager.tmpl");
417 cgiCopyTemplateLang("classes.tmpl");
419 if (count
> CUPS_PAGE_MAX
)
420 cgiCopyTemplateLang("pager.tmpl");
423 * Delete the response...
426 cupsArrayDelete(classes
);
435 cgiShowIPPError(_("Unable to get class list:"));
443 * 'show_class()' - Show a single class.
447 show_class(http_t
*http
, /* I - Connection to server */
448 const char *pclass
) /* I - Name of class */
450 ipp_t
*request
, /* IPP request */
451 *response
; /* IPP response */
452 ipp_attribute_t
*attr
; /* IPP attribute */
453 char uri
[HTTP_MAX_URI
]; /* Printer URI */
454 char refresh
[1024]; /* Refresh URL */
458 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
462 * attributes-natural-language
466 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
468 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
469 "localhost", 0, "/classes/%s", pclass
);
470 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
473 cgiGetAttributes(request
, "class.tmpl");
476 * Do the request and get back a response...
479 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
482 * Got the result; set the CGI variables and check the status of a
483 * single-queue request...
486 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
488 if (pclass
&& (attr
= ippFindAttribute(response
, "printer-state",
489 IPP_TAG_ENUM
)) != NULL
&&
490 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
493 * Class is processing - automatically refresh the page until we
494 * are done printing...
497 cgiFormEncode(uri
, pclass
, sizeof(uri
));
498 snprintf(refresh
, sizeof(refresh
), "10;URL=/classes/%s", uri
);
499 cgiSetVariable("refresh_page", refresh
);
503 * Delete the response...
509 * Show the standard header...
512 cgiStartHTML(pclass
);
515 * Show the class status...
518 cgiCopyTemplateLang("class.tmpl");
521 * Show jobs for the specified class...
524 cgiCopyTemplateLang("class-jobs-header.tmpl");
525 cgiShowJobs(http
, pclass
);
530 * Show the IPP error...
533 cgiStartHTML(pclass
);
534 cgiShowIPPError(_("Unable to get class status:"));
542 * End of "$Id: classes.c 7940 2008-09-16 00:45:16Z mike $".