2 * "$Id: classes.c 11928 2014-06-13 00:08:32Z msweet $"
4 * Class status CGI for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
20 #include "cgi-private.h"
27 static void do_class_op(http_t
*http
, const char *printer
, ipp_op_t op
,
29 static void show_all_classes(http_t
*http
, const char *username
);
30 static void show_class(http_t
*http
, const char *printer
);
34 * 'main()' - Main entry for CGI.
37 int /* O - Exit status */
40 const char *pclass
; /* Class name */
41 const char *user
; /* Username */
42 http_t
*http
; /* Connection to the server */
43 ipp_t
*request
, /* IPP request */
44 *response
; /* IPP response */
45 ipp_attribute_t
*attr
; /* IPP attribute */
46 const char *op
; /* Operation to perform, if any */
47 static const char *def_attrs
[] = /* Attributes for default printer */
50 "printer-uri-supported"
55 * Get any form variables...
60 op
= cgiGetVariable("OP");
63 * Set the web interface section...
66 cgiSetVariable("SECTION", "classes");
67 cgiSetVariable("REFRESH_PAGE", "");
70 * See if we are displaying a printer or all classes...
73 if ((pclass
= getenv("PATH_INFO")) != NULL
)
81 cgiSetVariable("PRINTER_NAME", pclass
);
85 * See who is logged in...
88 user
= getenv("REMOTE_USER");
91 * Connect to the HTTP server...
94 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
97 * Get the default printer...
100 if (!op
|| !cgiIsPOST())
103 * Get the default destination...
106 request
= ippNewRequest(CUPS_GET_DEFAULT
);
108 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
109 "requested-attributes",
110 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
112 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
114 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
115 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
117 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
119 char url
[HTTP_MAX_URI
]; /* New URL */
122 cgiSetVariable("DEFAULT_URI",
123 cgiRewriteURL(attr
->values
[0].string
.text
,
124 url
, sizeof(url
), NULL
));
131 * See if we need to show a list of classes or the status of a
136 show_all_classes(http
, user
);
138 show_class(http
, pclass
);
144 const char *server_port
= getenv("SERVER_PORT");
145 /* Port number string */
146 int port
= atoi(server_port
? server_port
: "0");
148 char uri
[1024]; /* URL */
150 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
151 getenv("HTTPS") ? "https" : "http", NULL
,
152 getenv("SERVER_NAME"), port
, "/classes/%s", pclass
);
154 printf("Location: %s\n\n", uri
);
156 else if (!strcmp(op
, "start-class"))
157 do_class_op(http
, pclass
, IPP_RESUME_PRINTER
, cgiText(_("Resume Class")));
158 else if (!strcmp(op
, "stop-class"))
159 do_class_op(http
, pclass
, IPP_PAUSE_PRINTER
, cgiText(_("Pause Class")));
160 else if (!strcmp(op
, "accept-jobs"))
161 do_class_op(http
, pclass
, CUPS_ACCEPT_JOBS
, cgiText(_("Accept Jobs")));
162 else if (!strcmp(op
, "reject-jobs"))
163 do_class_op(http
, pclass
, CUPS_REJECT_JOBS
, cgiText(_("Reject Jobs")));
164 else if (!strcmp(op
, "cancel-jobs"))
165 do_class_op(http
, pclass
, IPP_OP_CANCEL_JOBS
, cgiText(_("Cancel Jobs")));
166 else if (!_cups_strcasecmp(op
, "print-test-page"))
167 cgiPrintTestPage(http
, pclass
);
168 else if (!_cups_strcasecmp(op
, "move-jobs"))
169 cgiMoveJobs(http
, pclass
, 0);
173 * Unknown/bad operation...
176 cgiStartHTML(pclass
);
177 cgiCopyTemplateLang("error-op.tmpl");
184 * Unknown/bad operation...
187 cgiStartHTML(cgiText(_("Classes")));
188 cgiCopyTemplateLang("error-op.tmpl");
193 * Close the HTTP server connection...
199 * Return with no errors...
207 * 'do_class_op()' - Do a class operation.
211 do_class_op(http_t
*http
, /* I - HTTP connection */
212 const char *printer
, /* I - Printer name */
213 ipp_op_t op
, /* I - Operation to perform */
214 const char *title
) /* I - Title of page */
216 ipp_t
*request
; /* IPP request */
217 char uri
[HTTP_MAX_URI
], /* Printer URI */
218 resource
[HTTP_MAX_URI
]; /* Path for request */
222 * Build a printer request, which requires the following
226 * attributes-natural-language
230 request
= ippNewRequest(op
);
232 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
233 "localhost", 0, "/classes/%s", printer
);
234 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
238 * Do the request and get back a response...
241 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
);
242 ippDelete(cupsDoRequest(http
, request
, resource
));
244 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
246 puts("Status: 401\n");
249 else if (cupsLastError() > IPP_OK_CONFLICT
)
252 cgiShowIPPError(_("Unable to do maintenance command"));
257 * Redirect successful updates back to the printer page...
260 char url
[1024], /* Printer/class URL */
261 refresh
[1024]; /* Refresh URL */
264 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
265 cgiFormEncode(uri
, url
, sizeof(uri
));
266 snprintf(refresh
, sizeof(refresh
), "5;URL=%s", uri
);
267 cgiSetVariable("refresh_page", refresh
);
271 cgiSetVariable("IS_CLASS", "YES");
273 if (op
== IPP_PAUSE_PRINTER
)
274 cgiCopyTemplateLang("printer-stop.tmpl");
275 else if (op
== IPP_RESUME_PRINTER
)
276 cgiCopyTemplateLang("printer-start.tmpl");
277 else if (op
== CUPS_ACCEPT_JOBS
)
278 cgiCopyTemplateLang("printer-accept.tmpl");
279 else if (op
== CUPS_REJECT_JOBS
)
280 cgiCopyTemplateLang("printer-reject.tmpl");
281 else if (op
== IPP_OP_CANCEL_JOBS
)
282 cgiCopyTemplateLang("printer-cancel-jobs.tmpl");
290 * 'show_all_classes()' - Show all classes...
294 show_all_classes(http_t
*http
, /* I - Connection to server */
295 const char *user
) /* I - Username */
297 int i
; /* Looping var */
298 ipp_t
*request
, /* IPP request */
299 *response
; /* IPP response */
300 cups_array_t
*classes
; /* Array of class objects */
301 ipp_attribute_t
*pclass
; /* Class object */
302 int ascending
, /* Order of classes (0 = descending) */
303 first
, /* First class to show */
304 count
; /* Number of classes */
305 const char *var
; /* Form variable */
306 void *search
; /* Search data */
307 char val
[1024]; /* Form variable */
311 * Show the standard header...
314 cgiStartHTML(cgiText(_("Classes")));
317 * Build a CUPS_GET_CLASSES request, which requires the following
321 * attributes-natural-language
322 * requesting-user-name
325 request
= ippNewRequest(CUPS_GET_CLASSES
);
328 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
329 "requesting-user-name", NULL
, user
);
331 cgiGetAttributes(request
, "classes.tmpl");
334 * Do the request and get back a response...
337 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
340 * Get a list of matching job objects.
343 if ((var
= cgiGetVariable("QUERY")) != NULL
&&
344 !cgiGetVariable("CLEAR"))
345 search
= cgiCompileSearch(var
);
349 classes
= cgiGetIPPObjects(response
, search
);
350 count
= cupsArrayCount(classes
);
353 cgiFreeSearch(search
);
356 * Figure out which classes to display...
359 if ((var
= cgiGetVariable("FIRST")) != NULL
)
365 first
= count
- CUPS_PAGE_MAX
;
367 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
372 sprintf(val
, "%d", count
);
373 cgiSetVariable("TOTAL", val
);
375 if ((var
= cgiGetVariable("ORDER")) != NULL
&& *var
)
376 ascending
= !_cups_strcasecmp(var
, "asc");
382 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, first
);
383 i
< CUPS_PAGE_MAX
&& pclass
;
384 i
++, pclass
= (ipp_attribute_t
*)cupsArrayNext(classes
))
385 cgiSetIPPObjectVars(pclass
, NULL
, i
);
389 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, count
- first
- 1);
390 i
< CUPS_PAGE_MAX
&& pclass
;
391 i
++, pclass
= (ipp_attribute_t
*)cupsArrayPrev(classes
))
392 cgiSetIPPObjectVars(pclass
, NULL
, i
);
396 * Save navigation URLs...
399 cgiSetVariable("THISURL", "/classes/");
403 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
404 cgiSetVariable("PREV", val
);
407 if ((first
+ CUPS_PAGE_MAX
) < count
)
409 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
410 cgiSetVariable("NEXT", val
);
414 * Then show everything...
417 cgiCopyTemplateLang("search.tmpl");
419 cgiCopyTemplateLang("classes-header.tmpl");
421 if (count
> CUPS_PAGE_MAX
)
422 cgiCopyTemplateLang("pager.tmpl");
424 cgiCopyTemplateLang("classes.tmpl");
426 if (count
> CUPS_PAGE_MAX
)
427 cgiCopyTemplateLang("pager.tmpl");
430 * Delete the response...
433 cupsArrayDelete(classes
);
442 cgiShowIPPError(_("Unable to get class list"));
450 * 'show_class()' - Show a single class.
454 show_class(http_t
*http
, /* I - Connection to server */
455 const char *pclass
) /* I - Name of class */
457 ipp_t
*request
, /* IPP request */
458 *response
; /* IPP response */
459 ipp_attribute_t
*attr
; /* IPP attribute */
460 char uri
[HTTP_MAX_URI
]; /* Printer URI */
461 char refresh
[1024]; /* Refresh URL */
465 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
469 * attributes-natural-language
473 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
475 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
476 "localhost", 0, "/classes/%s", pclass
);
477 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
480 cgiGetAttributes(request
, "class.tmpl");
483 * Do the request and get back a response...
486 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
489 * Got the result; set the CGI variables and check the status of a
490 * single-queue request...
493 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
495 if (pclass
&& (attr
= ippFindAttribute(response
, "printer-state",
496 IPP_TAG_ENUM
)) != NULL
&&
497 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
500 * Class is processing - automatically refresh the page until we
501 * are done printing...
504 cgiFormEncode(uri
, pclass
, sizeof(uri
));
505 snprintf(refresh
, sizeof(refresh
), "10;URL=/classes/%s", uri
);
506 cgiSetVariable("refresh_page", refresh
);
510 * Delete the response...
516 * Show the standard header...
519 cgiStartHTML(pclass
);
522 * Show the class status...
525 cgiCopyTemplateLang("class.tmpl");
528 * Show jobs for the specified class...
531 cgiCopyTemplateLang("class-jobs-header.tmpl");
532 cgiShowJobs(http
, pclass
);
537 * Show the IPP error...
540 cgiStartHTML(pclass
);
541 cgiShowIPPError(_("Unable to get class status"));
549 * End of "$Id: classes.c 11928 2014-06-13 00:08:32Z msweet $".