]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
b19ccc9e | 2 | * "$Id: classes.c 7940 2008-09-16 00:45:16Z mike $" |
ef416fc2 | 3 | * |
4 | * Class status CGI for the Common UNIX Printing System (CUPS). | |
5 | * | |
52f6f666 | 6 | * Copyright 2007-2008 by Apple Inc. |
ef416fc2 | 7 | * Copyright 1997-2006 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
52f6f666 MS |
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. | |
ef416fc2 | 21 | */ |
22 | ||
23 | /* | |
24 | * Include necessary headers... | |
25 | */ | |
26 | ||
27 | #include "cgi-private.h" | |
28 | ||
29 | ||
fa73b229 | 30 | /* |
31 | * Local functions... | |
32 | */ | |
33 | ||
58dc1933 MS |
34 | static void do_class_op(http_t *http, const char *printer, ipp_op_t op, |
35 | const char *title); | |
36 | static void show_all_classes(http_t *http, const char *username); | |
37 | static void show_class(http_t *http, const char *printer); | |
fa73b229 | 38 | |
39 | ||
ef416fc2 | 40 | /* |
41 | * 'main()' - Main entry for CGI. | |
42 | */ | |
43 | ||
44 | int /* O - Exit status */ | |
45 | main(int argc, /* I - Number of command-line arguments */ | |
46 | char *argv[]) /* I - Command-line arguments */ | |
47 | { | |
fa73b229 | 48 | const char *pclass; /* Class name */ |
49 | const char *user; /* Username */ | |
ef416fc2 | 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 */ | |
ef416fc2 | 54 | const char *op; /* Operation to perform, if any */ |
fa73b229 | 55 | static const char *def_attrs[] = /* Attributes for default printer */ |
ef416fc2 | 56 | { |
57 | "printer-name", | |
58 | "printer-uri-supported" | |
59 | }; | |
60 | ||
61 | ||
62 | /* | |
63 | * Get any form variables... | |
64 | */ | |
65 | ||
66 | cgiInitialize(); | |
fa73b229 | 67 | |
ef416fc2 | 68 | op = cgiGetVariable("OP"); |
69 | ||
70 | /* | |
71 | * Set the web interface section... | |
72 | */ | |
73 | ||
74 | cgiSetVariable("SECTION", "classes"); | |
75 | ||
76 | /* | |
fa73b229 | 77 | * See if we are displaying a printer or all classes... |
ef416fc2 | 78 | */ |
79 | ||
b423cd4c | 80 | if ((pclass = getenv("PATH_INFO")) != NULL) |
4744bd90 | 81 | { |
b423cd4c | 82 | pclass ++; |
ef416fc2 | 83 | |
4744bd90 | 84 | if (!*pclass) |
85 | pclass = NULL; | |
58dc1933 MS |
86 | |
87 | if (pclass) | |
88 | cgiSetVariable("PRINTER_NAME", pclass); | |
4744bd90 | 89 | } |
90 | ||
ef416fc2 | 91 | /* |
fa73b229 | 92 | * See who is logged in... |
ef416fc2 | 93 | */ |
94 | ||
f301802f | 95 | user = getenv("REMOTE_USER"); |
ef416fc2 | 96 | |
97 | /* | |
fa73b229 | 98 | * Connect to the HTTP server... |
ef416fc2 | 99 | */ |
100 | ||
fa73b229 | 101 | http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); |
ef416fc2 | 102 | |
103 | /* | |
fa73b229 | 104 | * Get the default printer... |
ef416fc2 | 105 | */ |
106 | ||
2e4ff8af | 107 | if (!op || !cgiIsPOST()) |
ef416fc2 | 108 | { |
ef416fc2 | 109 | /* |
110 | * Get the default destination... | |
111 | */ | |
112 | ||
fa73b229 | 113 | request = ippNewRequest(CUPS_GET_DEFAULT); |
ef416fc2 | 114 | |
115 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
116 | "requested-attributes", | |
117 | sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs); | |
118 | ||
119 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
120 | { | |
121 | if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL) | |
122 | cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text); | |
123 | ||
124 | if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL) | |
125 | { | |
126 | char url[HTTP_MAX_URI]; /* New URL */ | |
127 | ||
128 | ||
129 | cgiSetVariable("DEFAULT_URI", | |
130 | cgiRewriteURL(attr->values[0].string.text, | |
131 | url, sizeof(url), NULL)); | |
132 | } | |
133 | ||
134 | ippDelete(response); | |
135 | } | |
136 | ||
137 | /* | |
fa73b229 | 138 | * See if we need to show a list of classes or the status of a |
139 | * single printer... | |
ef416fc2 | 140 | */ |
141 | ||
fa73b229 | 142 | if (!pclass) |
143 | show_all_classes(http, user); | |
144 | else | |
145 | show_class(http, pclass); | |
146 | } | |
58dc1933 MS |
147 | else if (pclass) |
148 | { | |
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); | |
163 | else | |
164 | { | |
165 | /* | |
166 | * Unknown/bad operation... | |
167 | */ | |
168 | ||
169 | cgiStartHTML(pclass); | |
170 | cgiCopyTemplateLang("error-op.tmpl"); | |
171 | cgiEndHTML(); | |
172 | } | |
173 | } | |
fa73b229 | 174 | else |
175 | { | |
176 | /* | |
177 | * Unknown/bad operation... | |
178 | */ | |
ef416fc2 | 179 | |
58dc1933 | 180 | cgiStartHTML(cgiText(_("Classes"))); |
fa73b229 | 181 | cgiCopyTemplateLang("error-op.tmpl"); |
182 | cgiEndHTML(); | |
183 | } | |
ef416fc2 | 184 | |
fa73b229 | 185 | /* |
186 | * Close the HTTP server connection... | |
187 | */ | |
ef416fc2 | 188 | |
fa73b229 | 189 | httpClose(http); |
ef416fc2 | 190 | |
fa73b229 | 191 | /* |
192 | * Return with no errors... | |
193 | */ | |
ef416fc2 | 194 | |
fa73b229 | 195 | return (0); |
196 | } | |
ef416fc2 | 197 | |
ef416fc2 | 198 | |
58dc1933 MS |
199 | /* |
200 | * 'do_class_op()' - Do a class operation. | |
201 | */ | |
202 | ||
203 | static void | |
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 */ | |
208 | { | |
209 | ipp_t *request; /* IPP request */ | |
210 | char uri[HTTP_MAX_URI], /* Printer URI */ | |
211 | resource[HTTP_MAX_URI]; /* Path for request */ | |
212 | ||
213 | ||
214 | /* | |
215 | * Build a printer request, which requires the following | |
216 | * attributes: | |
217 | * | |
218 | * attributes-charset | |
219 | * attributes-natural-language | |
220 | * printer-uri | |
221 | */ | |
222 | ||
223 | request = ippNewRequest(op); | |
224 | ||
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", | |
228 | NULL, uri); | |
229 | ||
230 | /* | |
231 | * Do the request and get back a response... | |
232 | */ | |
233 | ||
234 | snprintf(resource, sizeof(resource), "/classes/%s", printer); | |
235 | ippDelete(cupsDoRequest(http, request, resource)); | |
236 | ||
237 | if (cupsLastError() == IPP_NOT_AUTHORIZED) | |
238 | { | |
239 | puts("Status: 401\n"); | |
240 | exit(0); | |
241 | } | |
242 | else if (cupsLastError() > IPP_OK_CONFLICT) | |
243 | { | |
244 | cgiStartHTML(title); | |
245 | cgiShowIPPError(_("Unable to do maintenance command:")); | |
246 | } | |
247 | else | |
248 | { | |
249 | /* | |
250 | * Redirect successful updates back to the printer page... | |
251 | */ | |
252 | ||
253 | char url[1024], /* Printer/class URL */ | |
254 | refresh[1024]; /* Refresh URL */ | |
255 | ||
256 | ||
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); | |
261 | ||
262 | cgiStartHTML(title); | |
263 | ||
264 | cgiSetVariable("IS_CLASS", "YES"); | |
265 | ||
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"); | |
276 | } | |
277 | ||
278 | cgiEndHTML(); | |
279 | } | |
280 | ||
281 | ||
fa73b229 | 282 | /* |
283 | * 'show_all_classes()' - Show all classes... | |
284 | */ | |
ef416fc2 | 285 | |
58dc1933 | 286 | static void |
fa73b229 | 287 | show_all_classes(http_t *http, /* I - Connection to server */ |
f301802f | 288 | const char *user) /* I - Username */ |
fa73b229 | 289 | { |
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 */ | |
2e4ff8af | 300 | char val[1024]; /* Form variable */ |
ef416fc2 | 301 | |
ef416fc2 | 302 | |
fa73b229 | 303 | /* |
304 | * Show the standard header... | |
305 | */ | |
306 | ||
307 | cgiStartHTML(cgiText(_("Classes"))); | |
308 | ||
309 | /* | |
310 | * Build a CUPS_GET_CLASSES request, which requires the following | |
311 | * attributes: | |
312 | * | |
313 | * attributes-charset | |
314 | * attributes-natural-language | |
315 | * requesting-user-name | |
316 | */ | |
317 | ||
318 | request = ippNewRequest(CUPS_GET_CLASSES); | |
319 | ||
f301802f | 320 | if (user) |
321 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
322 | "requesting-user-name", NULL, user); | |
fa73b229 | 323 | |
324 | cgiGetAttributes(request, "classes.tmpl"); | |
325 | ||
326 | /* | |
327 | * Do the request and get back a response... | |
328 | */ | |
329 | ||
330 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
331 | { | |
ef416fc2 | 332 | /* |
fa73b229 | 333 | * Get a list of matching job objects. |
ef416fc2 | 334 | */ |
335 | ||
2e4ff8af MS |
336 | if ((var = cgiGetVariable("QUERY")) != NULL && |
337 | !cgiGetVariable("CLEAR")) | |
fa73b229 | 338 | search = cgiCompileSearch(var); |
339 | else | |
340 | search = NULL; | |
341 | ||
342 | classes = cgiGetIPPObjects(response, search); | |
343 | count = cupsArrayCount(classes); | |
344 | ||
345 | if (search) | |
346 | cgiFreeSearch(search); | |
ef416fc2 | 347 | |
348 | /* | |
fa73b229 | 349 | * Figure out which classes to display... |
ef416fc2 | 350 | */ |
351 | ||
fa73b229 | 352 | if ((var = cgiGetVariable("FIRST")) != NULL) |
353 | first = atoi(var); | |
354 | else | |
355 | first = 0; | |
ef416fc2 | 356 | |
fa73b229 | 357 | if (first >= count) |
358 | first = count - CUPS_PAGE_MAX; | |
ef416fc2 | 359 | |
fa73b229 | 360 | first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX; |
ef416fc2 | 361 | |
fa73b229 | 362 | if (first < 0) |
363 | first = 0; | |
ef416fc2 | 364 | |
2e4ff8af MS |
365 | sprintf(val, "%d", count); |
366 | cgiSetVariable("TOTAL", val); | |
ef416fc2 | 367 | |
fa73b229 | 368 | if ((var = cgiGetVariable("ORDER")) != NULL) |
369 | ascending = !strcasecmp(var, "asc"); | |
370 | else | |
371 | ascending = 1; | |
ef416fc2 | 372 | |
fa73b229 | 373 | if (ascending) |
374 | { | |
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); | |
379 | } | |
380 | else | |
381 | { | |
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); | |
386 | } | |
ef416fc2 | 387 | |
fa73b229 | 388 | /* |
389 | * Save navigation URLs... | |
390 | */ | |
ef416fc2 | 391 | |
2e4ff8af | 392 | cgiSetVariable("THISURL", "/classes/"); |
fa73b229 | 393 | |
394 | if (first > 0) | |
395 | { | |
2e4ff8af MS |
396 | sprintf(val, "%d", first - CUPS_PAGE_MAX); |
397 | cgiSetVariable("PREV", val); | |
ef416fc2 | 398 | } |
fa73b229 | 399 | |
400 | if ((first + CUPS_PAGE_MAX) < count) | |
401 | { | |
2e4ff8af MS |
402 | sprintf(val, "%d", first + CUPS_PAGE_MAX); |
403 | cgiSetVariable("NEXT", val); | |
fa73b229 | 404 | } |
405 | ||
ef416fc2 | 406 | /* |
fa73b229 | 407 | * Then show everything... |
ef416fc2 | 408 | */ |
409 | ||
fa73b229 | 410 | cgiCopyTemplateLang("search.tmpl"); |
ef416fc2 | 411 | |
fa73b229 | 412 | cgiCopyTemplateLang("classes-header.tmpl"); |
ef416fc2 | 413 | |
b19ccc9e | 414 | if (count > CUPS_PAGE_MAX) |
fa73b229 | 415 | cgiCopyTemplateLang("pager.tmpl"); |
ef416fc2 | 416 | |
fa73b229 | 417 | cgiCopyTemplateLang("classes.tmpl"); |
ef416fc2 | 418 | |
b19ccc9e | 419 | if (count > CUPS_PAGE_MAX) |
fa73b229 | 420 | cgiCopyTemplateLang("pager.tmpl"); |
ef416fc2 | 421 | |
422 | /* | |
fa73b229 | 423 | * Delete the response... |
ef416fc2 | 424 | */ |
425 | ||
bd7854cb | 426 | cupsArrayDelete(classes); |
fa73b229 | 427 | ippDelete(response); |
428 | } | |
429 | else | |
430 | { | |
431 | /* | |
432 | * Show the error... | |
433 | */ | |
ef416fc2 | 434 | |
fa73b229 | 435 | cgiShowIPPError(_("Unable to get class list:")); |
436 | } | |
ef416fc2 | 437 | |
fa73b229 | 438 | cgiEndHTML(); |
439 | } | |
ef416fc2 | 440 | |
ef416fc2 | 441 | |
fa73b229 | 442 | /* |
443 | * 'show_class()' - Show a single class. | |
444 | */ | |
ef416fc2 | 445 | |
58dc1933 | 446 | static void |
fa73b229 | 447 | show_class(http_t *http, /* I - Connection to server */ |
448 | const char *pclass) /* I - Name of class */ | |
449 | { | |
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 */ | |
455 | ||
456 | ||
457 | /* | |
458 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following | |
459 | * attributes: | |
460 | * | |
461 | * attributes-charset | |
462 | * attributes-natural-language | |
463 | * printer-uri | |
464 | */ | |
ef416fc2 | 465 | |
fa73b229 | 466 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); |
ef416fc2 | 467 | |
a4d04587 | 468 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
469 | "localhost", 0, "/classes/%s", pclass); | |
fa73b229 | 470 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, |
471 | uri); | |
ef416fc2 | 472 | |
b19ccc9e | 473 | cgiGetAttributes(request, "class.tmpl"); |
fa73b229 | 474 | |
475 | /* | |
476 | * Do the request and get back a response... | |
477 | */ | |
478 | ||
479 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
480 | { | |
ef416fc2 | 481 | /* |
fa73b229 | 482 | * Got the result; set the CGI variables and check the status of a |
483 | * single-queue request... | |
ef416fc2 | 484 | */ |
485 | ||
fa73b229 | 486 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); |
487 | ||
488 | if (pclass && (attr = ippFindAttribute(response, "printer-state", | |
489 | IPP_TAG_ENUM)) != NULL && | |
490 | attr->values[0].integer == IPP_PRINTER_PROCESSING) | |
ef416fc2 | 491 | { |
fa73b229 | 492 | /* |
493 | * Class is processing - automatically refresh the page until we | |
494 | * are done printing... | |
495 | */ | |
ef416fc2 | 496 | |
fa73b229 | 497 | cgiFormEncode(uri, pclass, sizeof(uri)); |
f301802f | 498 | snprintf(refresh, sizeof(refresh), "10;URL=/classes/%s", uri); |
fa73b229 | 499 | cgiSetVariable("refresh_page", refresh); |
ef416fc2 | 500 | } |
ef416fc2 | 501 | |
fa73b229 | 502 | /* |
503 | * Delete the response... | |
504 | */ | |
505 | ||
506 | ippDelete(response); | |
ef416fc2 | 507 | |
508 | /* | |
509 | * Show the standard header... | |
510 | */ | |
511 | ||
fa73b229 | 512 | cgiStartHTML(pclass); |
ef416fc2 | 513 | |
514 | /* | |
fa73b229 | 515 | * Show the class status... |
ef416fc2 | 516 | */ |
517 | ||
b19ccc9e | 518 | cgiCopyTemplateLang("class.tmpl"); |
ef416fc2 | 519 | |
fa73b229 | 520 | /* |
521 | * Show jobs for the specified class... | |
522 | */ | |
ef416fc2 | 523 | |
fa73b229 | 524 | cgiCopyTemplateLang("class-jobs-header.tmpl"); |
525 | cgiShowJobs(http, pclass); | |
526 | } | |
527 | else | |
528 | { | |
529 | /* | |
530 | * Show the IPP error... | |
531 | */ | |
ef416fc2 | 532 | |
fa73b229 | 533 | cgiStartHTML(pclass); |
534 | cgiShowIPPError(_("Unable to get class status:")); | |
535 | } | |
ef416fc2 | 536 | |
fa73b229 | 537 | cgiEndHTML(); |
ef416fc2 | 538 | } |
539 | ||
540 | ||
541 | /* | |
b19ccc9e | 542 | * End of "$Id: classes.c 7940 2008-09-16 00:45:16Z mike $". |
ef416fc2 | 543 | */ |