]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
4744bd90 | 2 | * "$Id: classes.c 5202 2006-02-28 19:37:03Z mike $" |
ef416fc2 | 3 | * |
4 | * Class status CGI for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
fa73b229 | 26 | * main() - Main entry for CGI. |
27 | * show_all_classes() - Show all classes... | |
28 | * show_class() - Show a single class. | |
ef416fc2 | 29 | */ |
30 | ||
31 | /* | |
32 | * Include necessary headers... | |
33 | */ | |
34 | ||
35 | #include "cgi-private.h" | |
36 | ||
37 | ||
fa73b229 | 38 | /* |
39 | * Local functions... | |
40 | */ | |
41 | ||
42 | void show_all_classes(http_t *http, const char *username); | |
43 | void show_class(http_t *http, const char *printer); | |
44 | ||
45 | ||
ef416fc2 | 46 | /* |
47 | * 'main()' - Main entry for CGI. | |
48 | */ | |
49 | ||
50 | int /* O - Exit status */ | |
51 | main(int argc, /* I - Number of command-line arguments */ | |
52 | char *argv[]) /* I - Command-line arguments */ | |
53 | { | |
fa73b229 | 54 | const char *pclass; /* Class name */ |
55 | const char *user; /* Username */ | |
ef416fc2 | 56 | http_t *http; /* Connection to the server */ |
57 | ipp_t *request, /* IPP request */ | |
58 | *response; /* IPP response */ | |
59 | ipp_attribute_t *attr; /* IPP attribute */ | |
ef416fc2 | 60 | const char *op; /* Operation to perform, if any */ |
fa73b229 | 61 | static const char *def_attrs[] = /* Attributes for default printer */ |
ef416fc2 | 62 | { |
63 | "printer-name", | |
64 | "printer-uri-supported" | |
65 | }; | |
66 | ||
67 | ||
68 | /* | |
69 | * Get any form variables... | |
70 | */ | |
71 | ||
72 | cgiInitialize(); | |
fa73b229 | 73 | |
ef416fc2 | 74 | op = cgiGetVariable("OP"); |
75 | ||
76 | /* | |
77 | * Set the web interface section... | |
78 | */ | |
79 | ||
80 | cgiSetVariable("SECTION", "classes"); | |
81 | ||
82 | /* | |
fa73b229 | 83 | * See if we are displaying a printer or all classes... |
ef416fc2 | 84 | */ |
85 | ||
b423cd4c | 86 | if ((pclass = getenv("PATH_INFO")) != NULL) |
4744bd90 | 87 | { |
b423cd4c | 88 | pclass ++; |
ef416fc2 | 89 | |
4744bd90 | 90 | if (!*pclass) |
91 | pclass = NULL; | |
92 | } | |
93 | ||
ef416fc2 | 94 | /* |
fa73b229 | 95 | * See who is logged in... |
ef416fc2 | 96 | */ |
97 | ||
fa73b229 | 98 | if ((user = getenv("REMOTE_USER")) == NULL) |
99 | user = "guest"; | |
ef416fc2 | 100 | |
101 | /* | |
fa73b229 | 102 | * Connect to the HTTP server... |
ef416fc2 | 103 | */ |
104 | ||
fa73b229 | 105 | http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); |
ef416fc2 | 106 | |
107 | /* | |
fa73b229 | 108 | * Get the default printer... |
ef416fc2 | 109 | */ |
110 | ||
fa73b229 | 111 | if (!op) |
ef416fc2 | 112 | { |
ef416fc2 | 113 | /* |
114 | * Get the default destination... | |
115 | */ | |
116 | ||
fa73b229 | 117 | request = ippNewRequest(CUPS_GET_DEFAULT); |
ef416fc2 | 118 | |
119 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
120 | "requested-attributes", | |
121 | sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs); | |
122 | ||
123 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
124 | { | |
125 | if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL) | |
126 | cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text); | |
127 | ||
128 | if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL) | |
129 | { | |
130 | char url[HTTP_MAX_URI]; /* New URL */ | |
131 | ||
132 | ||
133 | cgiSetVariable("DEFAULT_URI", | |
134 | cgiRewriteURL(attr->values[0].string.text, | |
135 | url, sizeof(url), NULL)); | |
136 | } | |
137 | ||
138 | ippDelete(response); | |
139 | } | |
140 | ||
141 | /* | |
fa73b229 | 142 | * See if we need to show a list of classes or the status of a |
143 | * single printer... | |
ef416fc2 | 144 | */ |
145 | ||
fa73b229 | 146 | if (!pclass) |
147 | show_all_classes(http, user); | |
148 | else | |
149 | show_class(http, pclass); | |
150 | } | |
151 | else if (!strcasecmp(op, "print-test-page") && pclass) | |
152 | cgiPrintTestPage(http, pclass); | |
153 | else if (!strcasecmp(op, "move-jobs") && pclass) | |
154 | cgiMoveJobs(http, pclass, 0); | |
155 | else | |
156 | { | |
157 | /* | |
158 | * Unknown/bad operation... | |
159 | */ | |
ef416fc2 | 160 | |
fa73b229 | 161 | if (pclass) |
162 | cgiStartHTML(pclass); | |
163 | else | |
164 | cgiStartHTML(cgiText(_("Classes"))); | |
ef416fc2 | 165 | |
fa73b229 | 166 | cgiCopyTemplateLang("error-op.tmpl"); |
167 | cgiEndHTML(); | |
168 | } | |
ef416fc2 | 169 | |
fa73b229 | 170 | /* |
171 | * Close the HTTP server connection... | |
172 | */ | |
ef416fc2 | 173 | |
fa73b229 | 174 | httpClose(http); |
ef416fc2 | 175 | |
fa73b229 | 176 | /* |
177 | * Return with no errors... | |
178 | */ | |
ef416fc2 | 179 | |
fa73b229 | 180 | return (0); |
181 | } | |
ef416fc2 | 182 | |
ef416fc2 | 183 | |
fa73b229 | 184 | /* |
185 | * 'show_all_classes()' - Show all classes... | |
186 | */ | |
ef416fc2 | 187 | |
fa73b229 | 188 | void |
189 | show_all_classes(http_t *http, /* I - Connection to server */ | |
190 | const char *user) /* I - Username */ | |
191 | { | |
192 | int i; /* Looping var */ | |
193 | ipp_t *request, /* IPP request */ | |
194 | *response; /* IPP response */ | |
195 | cups_array_t *classes; /* Array of class objects */ | |
196 | ipp_attribute_t *pclass; /* Class object */ | |
197 | int ascending, /* Order of classes (0 = descending) */ | |
198 | first, /* First class to show */ | |
199 | count; /* Number of classes */ | |
200 | const char *var; /* Form variable */ | |
201 | void *search; /* Search data */ | |
202 | char url[1024], /* URL for prev/next/this */ | |
203 | *urlptr, /* Position in URL */ | |
204 | *urlend; /* End of URL */ | |
ef416fc2 | 205 | |
ef416fc2 | 206 | |
fa73b229 | 207 | /* |
208 | * Show the standard header... | |
209 | */ | |
210 | ||
211 | cgiStartHTML(cgiText(_("Classes"))); | |
212 | ||
213 | /* | |
214 | * Build a CUPS_GET_CLASSES request, which requires the following | |
215 | * attributes: | |
216 | * | |
217 | * attributes-charset | |
218 | * attributes-natural-language | |
219 | * requesting-user-name | |
220 | */ | |
221 | ||
222 | request = ippNewRequest(CUPS_GET_CLASSES); | |
223 | ||
224 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
225 | "requesting-user-name", NULL, user); | |
226 | ||
227 | cgiGetAttributes(request, "classes.tmpl"); | |
228 | ||
229 | /* | |
230 | * Do the request and get back a response... | |
231 | */ | |
232 | ||
233 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
234 | { | |
ef416fc2 | 235 | /* |
fa73b229 | 236 | * Get a list of matching job objects. |
ef416fc2 | 237 | */ |
238 | ||
fa73b229 | 239 | if ((var = cgiGetVariable("QUERY")) != NULL) |
240 | search = cgiCompileSearch(var); | |
241 | else | |
242 | search = NULL; | |
243 | ||
244 | classes = cgiGetIPPObjects(response, search); | |
245 | count = cupsArrayCount(classes); | |
246 | ||
247 | if (search) | |
248 | cgiFreeSearch(search); | |
ef416fc2 | 249 | |
250 | /* | |
fa73b229 | 251 | * Figure out which classes to display... |
ef416fc2 | 252 | */ |
253 | ||
fa73b229 | 254 | if ((var = cgiGetVariable("FIRST")) != NULL) |
255 | first = atoi(var); | |
256 | else | |
257 | first = 0; | |
ef416fc2 | 258 | |
fa73b229 | 259 | if (first >= count) |
260 | first = count - CUPS_PAGE_MAX; | |
ef416fc2 | 261 | |
fa73b229 | 262 | first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX; |
ef416fc2 | 263 | |
fa73b229 | 264 | if (first < 0) |
265 | first = 0; | |
ef416fc2 | 266 | |
fa73b229 | 267 | sprintf(url, "%d", count); |
268 | cgiSetVariable("TOTAL", url); | |
ef416fc2 | 269 | |
fa73b229 | 270 | if ((var = cgiGetVariable("ORDER")) != NULL) |
271 | ascending = !strcasecmp(var, "asc"); | |
272 | else | |
273 | ascending = 1; | |
ef416fc2 | 274 | |
fa73b229 | 275 | if (ascending) |
276 | { | |
277 | for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, first); | |
278 | i < CUPS_PAGE_MAX && pclass; | |
279 | i ++, pclass = (ipp_attribute_t *)cupsArrayNext(classes)) | |
280 | cgiSetIPPObjectVars(pclass, NULL, i); | |
281 | } | |
282 | else | |
283 | { | |
284 | for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, count - first - 1); | |
285 | i < CUPS_PAGE_MAX && pclass; | |
286 | i ++, pclass = (ipp_attribute_t *)cupsArrayPrev(classes)) | |
287 | cgiSetIPPObjectVars(pclass, NULL, i); | |
288 | } | |
ef416fc2 | 289 | |
fa73b229 | 290 | /* |
291 | * Save navigation URLs... | |
292 | */ | |
ef416fc2 | 293 | |
fa73b229 | 294 | urlend = url + sizeof(url); |
ef416fc2 | 295 | |
fa73b229 | 296 | if ((var = cgiGetVariable("QUERY")) != NULL) |
297 | { | |
298 | strlcpy(url, "/classes/?QUERY=", sizeof(url)); | |
299 | urlptr = url + strlen(url); | |
ef416fc2 | 300 | |
fa73b229 | 301 | cgiFormEncode(urlptr, var, urlend - urlptr); |
302 | urlptr += strlen(urlptr); | |
ef416fc2 | 303 | |
fa73b229 | 304 | strlcpy(urlptr, "&", urlend - urlptr); |
305 | urlptr += strlen(urlptr); | |
306 | } | |
307 | else | |
308 | { | |
309 | strlcpy(url, "/classes/?", sizeof(url)); | |
310 | urlptr = url + strlen(url); | |
311 | } | |
ef416fc2 | 312 | |
fa73b229 | 313 | snprintf(urlptr, urlend - urlptr, "FIRST=%d", first); |
314 | cgiSetVariable("THISURL", url); | |
315 | ||
316 | if (first > 0) | |
317 | { | |
318 | snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s", | |
319 | first - CUPS_PAGE_MAX, ascending ? "asc" : "dec"); | |
320 | cgiSetVariable("PREVURL", url); | |
ef416fc2 | 321 | } |
fa73b229 | 322 | |
323 | if ((first + CUPS_PAGE_MAX) < count) | |
324 | { | |
325 | snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s", | |
326 | first + CUPS_PAGE_MAX, ascending ? "asc" : "dec"); | |
327 | cgiSetVariable("NEXTURL", url); | |
328 | } | |
329 | ||
ef416fc2 | 330 | /* |
fa73b229 | 331 | * Then show everything... |
ef416fc2 | 332 | */ |
333 | ||
fa73b229 | 334 | cgiCopyTemplateLang("search.tmpl"); |
ef416fc2 | 335 | |
fa73b229 | 336 | cgiCopyTemplateLang("classes-header.tmpl"); |
ef416fc2 | 337 | |
fa73b229 | 338 | if (count > 0) |
339 | cgiCopyTemplateLang("pager.tmpl"); | |
ef416fc2 | 340 | |
fa73b229 | 341 | cgiCopyTemplateLang("classes.tmpl"); |
ef416fc2 | 342 | |
fa73b229 | 343 | if (count > 0) |
344 | cgiCopyTemplateLang("pager.tmpl"); | |
ef416fc2 | 345 | |
346 | /* | |
fa73b229 | 347 | * Delete the response... |
ef416fc2 | 348 | */ |
349 | ||
bd7854cb | 350 | cupsArrayDelete(classes); |
fa73b229 | 351 | ippDelete(response); |
352 | } | |
353 | else | |
354 | { | |
355 | /* | |
356 | * Show the error... | |
357 | */ | |
ef416fc2 | 358 | |
fa73b229 | 359 | cgiShowIPPError(_("Unable to get class list:")); |
360 | } | |
ef416fc2 | 361 | |
fa73b229 | 362 | cgiEndHTML(); |
363 | } | |
ef416fc2 | 364 | |
ef416fc2 | 365 | |
fa73b229 | 366 | /* |
367 | * 'show_class()' - Show a single class. | |
368 | */ | |
ef416fc2 | 369 | |
fa73b229 | 370 | void |
371 | show_class(http_t *http, /* I - Connection to server */ | |
372 | const char *pclass) /* I - Name of class */ | |
373 | { | |
374 | ipp_t *request, /* IPP request */ | |
375 | *response; /* IPP response */ | |
376 | ipp_attribute_t *attr; /* IPP attribute */ | |
377 | char uri[HTTP_MAX_URI]; /* Printer URI */ | |
378 | char refresh[1024]; /* Refresh URL */ | |
379 | ||
380 | ||
381 | /* | |
382 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following | |
383 | * attributes: | |
384 | * | |
385 | * attributes-charset | |
386 | * attributes-natural-language | |
387 | * printer-uri | |
388 | */ | |
ef416fc2 | 389 | |
fa73b229 | 390 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); |
ef416fc2 | 391 | |
a4d04587 | 392 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
393 | "localhost", 0, "/classes/%s", pclass); | |
fa73b229 | 394 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, |
395 | uri); | |
ef416fc2 | 396 | |
fa73b229 | 397 | cgiGetAttributes(request, "classes.tmpl"); |
398 | ||
399 | /* | |
400 | * Do the request and get back a response... | |
401 | */ | |
402 | ||
403 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
404 | { | |
ef416fc2 | 405 | /* |
fa73b229 | 406 | * Got the result; set the CGI variables and check the status of a |
407 | * single-queue request... | |
ef416fc2 | 408 | */ |
409 | ||
fa73b229 | 410 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); |
411 | ||
412 | if (pclass && (attr = ippFindAttribute(response, "printer-state", | |
413 | IPP_TAG_ENUM)) != NULL && | |
414 | attr->values[0].integer == IPP_PRINTER_PROCESSING) | |
ef416fc2 | 415 | { |
fa73b229 | 416 | /* |
417 | * Class is processing - automatically refresh the page until we | |
418 | * are done printing... | |
419 | */ | |
ef416fc2 | 420 | |
fa73b229 | 421 | cgiFormEncode(uri, pclass, sizeof(uri)); |
422 | snprintf(refresh, sizeof(refresh), "10;/classes/%s", uri); | |
423 | cgiSetVariable("refresh_page", refresh); | |
ef416fc2 | 424 | } |
ef416fc2 | 425 | |
fa73b229 | 426 | /* |
427 | * Delete the response... | |
428 | */ | |
429 | ||
430 | ippDelete(response); | |
ef416fc2 | 431 | |
432 | /* | |
433 | * Show the standard header... | |
434 | */ | |
435 | ||
fa73b229 | 436 | cgiStartHTML(pclass); |
ef416fc2 | 437 | |
438 | /* | |
fa73b229 | 439 | * Show the class status... |
ef416fc2 | 440 | */ |
441 | ||
fa73b229 | 442 | cgiCopyTemplateLang("classes.tmpl"); |
ef416fc2 | 443 | |
fa73b229 | 444 | /* |
445 | * Show jobs for the specified class... | |
446 | */ | |
ef416fc2 | 447 | |
fa73b229 | 448 | cgiCopyTemplateLang("class-jobs-header.tmpl"); |
449 | cgiShowJobs(http, pclass); | |
450 | } | |
451 | else | |
452 | { | |
453 | /* | |
454 | * Show the IPP error... | |
455 | */ | |
ef416fc2 | 456 | |
fa73b229 | 457 | cgiStartHTML(pclass); |
458 | cgiShowIPPError(_("Unable to get class status:")); | |
459 | } | |
ef416fc2 | 460 | |
fa73b229 | 461 | cgiEndHTML(); |
ef416fc2 | 462 | } |
463 | ||
464 | ||
465 | /* | |
4744bd90 | 466 | * End of "$Id: classes.c 5202 2006-02-28 19:37:03Z mike $". |
ef416fc2 | 467 | */ |