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