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