]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
b423cd4c | 2 | * "$Id: classes.c 5160 2006-02-24 01:14:18Z 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) |
87 | pclass ++; | |
ef416fc2 | 88 | |
89 | /* | |
fa73b229 | 90 | * See who is logged in... |
ef416fc2 | 91 | */ |
92 | ||
fa73b229 | 93 | if ((user = getenv("REMOTE_USER")) == NULL) |
94 | user = "guest"; | |
ef416fc2 | 95 | |
96 | /* | |
fa73b229 | 97 | * Connect to the HTTP server... |
ef416fc2 | 98 | */ |
99 | ||
fa73b229 | 100 | http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); |
ef416fc2 | 101 | |
102 | /* | |
fa73b229 | 103 | * Get the default printer... |
ef416fc2 | 104 | */ |
105 | ||
fa73b229 | 106 | if (!op) |
ef416fc2 | 107 | { |
ef416fc2 | 108 | /* |
109 | * Get the default destination... | |
110 | */ | |
111 | ||
fa73b229 | 112 | request = ippNewRequest(CUPS_GET_DEFAULT); |
ef416fc2 | 113 | |
114 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
115 | "requested-attributes", | |
116 | sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs); | |
117 | ||
118 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
119 | { | |
120 | if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL) | |
121 | cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text); | |
122 | ||
123 | if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL) | |
124 | { | |
125 | char url[HTTP_MAX_URI]; /* New URL */ | |
126 | ||
127 | ||
128 | cgiSetVariable("DEFAULT_URI", | |
129 | cgiRewriteURL(attr->values[0].string.text, | |
130 | url, sizeof(url), NULL)); | |
131 | } | |
132 | ||
133 | ippDelete(response); | |
134 | } | |
135 | ||
136 | /* | |
fa73b229 | 137 | * See if we need to show a list of classes or the status of a |
138 | * single printer... | |
ef416fc2 | 139 | */ |
140 | ||
fa73b229 | 141 | if (!pclass) |
142 | show_all_classes(http, user); | |
143 | else | |
144 | show_class(http, pclass); | |
145 | } | |
146 | else if (!strcasecmp(op, "print-test-page") && pclass) | |
147 | cgiPrintTestPage(http, pclass); | |
148 | else if (!strcasecmp(op, "move-jobs") && pclass) | |
149 | cgiMoveJobs(http, pclass, 0); | |
150 | else | |
151 | { | |
152 | /* | |
153 | * Unknown/bad operation... | |
154 | */ | |
ef416fc2 | 155 | |
fa73b229 | 156 | if (pclass) |
157 | cgiStartHTML(pclass); | |
158 | else | |
159 | cgiStartHTML(cgiText(_("Classes"))); | |
ef416fc2 | 160 | |
fa73b229 | 161 | cgiCopyTemplateLang("error-op.tmpl"); |
162 | cgiEndHTML(); | |
163 | } | |
ef416fc2 | 164 | |
fa73b229 | 165 | /* |
166 | * Close the HTTP server connection... | |
167 | */ | |
ef416fc2 | 168 | |
fa73b229 | 169 | httpClose(http); |
ef416fc2 | 170 | |
fa73b229 | 171 | /* |
172 | * Return with no errors... | |
173 | */ | |
ef416fc2 | 174 | |
fa73b229 | 175 | return (0); |
176 | } | |
ef416fc2 | 177 | |
ef416fc2 | 178 | |
fa73b229 | 179 | /* |
180 | * 'show_all_classes()' - Show all classes... | |
181 | */ | |
ef416fc2 | 182 | |
fa73b229 | 183 | void |
184 | show_all_classes(http_t *http, /* I - Connection to server */ | |
185 | const char *user) /* I - Username */ | |
186 | { | |
187 | int i; /* Looping var */ | |
188 | ipp_t *request, /* IPP request */ | |
189 | *response; /* IPP response */ | |
190 | cups_array_t *classes; /* Array of class objects */ | |
191 | ipp_attribute_t *pclass; /* Class object */ | |
192 | int ascending, /* Order of classes (0 = descending) */ | |
193 | first, /* First class to show */ | |
194 | count; /* Number of classes */ | |
195 | const char *var; /* Form variable */ | |
196 | void *search; /* Search data */ | |
197 | char url[1024], /* URL for prev/next/this */ | |
198 | *urlptr, /* Position in URL */ | |
199 | *urlend; /* End of URL */ | |
ef416fc2 | 200 | |
ef416fc2 | 201 | |
fa73b229 | 202 | /* |
203 | * Show the standard header... | |
204 | */ | |
205 | ||
206 | cgiStartHTML(cgiText(_("Classes"))); | |
207 | ||
208 | /* | |
209 | * Build a CUPS_GET_CLASSES request, which requires the following | |
210 | * attributes: | |
211 | * | |
212 | * attributes-charset | |
213 | * attributes-natural-language | |
214 | * requesting-user-name | |
215 | */ | |
216 | ||
217 | request = ippNewRequest(CUPS_GET_CLASSES); | |
218 | ||
219 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
220 | "requesting-user-name", NULL, user); | |
221 | ||
222 | cgiGetAttributes(request, "classes.tmpl"); | |
223 | ||
224 | /* | |
225 | * Do the request and get back a response... | |
226 | */ | |
227 | ||
228 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
229 | { | |
ef416fc2 | 230 | /* |
fa73b229 | 231 | * Get a list of matching job objects. |
ef416fc2 | 232 | */ |
233 | ||
fa73b229 | 234 | if ((var = cgiGetVariable("QUERY")) != NULL) |
235 | search = cgiCompileSearch(var); | |
236 | else | |
237 | search = NULL; | |
238 | ||
239 | classes = cgiGetIPPObjects(response, search); | |
240 | count = cupsArrayCount(classes); | |
241 | ||
242 | if (search) | |
243 | cgiFreeSearch(search); | |
ef416fc2 | 244 | |
245 | /* | |
fa73b229 | 246 | * Figure out which classes to display... |
ef416fc2 | 247 | */ |
248 | ||
fa73b229 | 249 | if ((var = cgiGetVariable("FIRST")) != NULL) |
250 | first = atoi(var); | |
251 | else | |
252 | first = 0; | |
ef416fc2 | 253 | |
fa73b229 | 254 | if (first >= count) |
255 | first = count - CUPS_PAGE_MAX; | |
ef416fc2 | 256 | |
fa73b229 | 257 | first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX; |
ef416fc2 | 258 | |
fa73b229 | 259 | if (first < 0) |
260 | first = 0; | |
ef416fc2 | 261 | |
fa73b229 | 262 | sprintf(url, "%d", count); |
263 | cgiSetVariable("TOTAL", url); | |
ef416fc2 | 264 | |
fa73b229 | 265 | if ((var = cgiGetVariable("ORDER")) != NULL) |
266 | ascending = !strcasecmp(var, "asc"); | |
267 | else | |
268 | ascending = 1; | |
ef416fc2 | 269 | |
fa73b229 | 270 | if (ascending) |
271 | { | |
272 | for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, first); | |
273 | i < CUPS_PAGE_MAX && pclass; | |
274 | i ++, pclass = (ipp_attribute_t *)cupsArrayNext(classes)) | |
275 | cgiSetIPPObjectVars(pclass, NULL, i); | |
276 | } | |
277 | else | |
278 | { | |
279 | for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, count - first - 1); | |
280 | i < CUPS_PAGE_MAX && pclass; | |
281 | i ++, pclass = (ipp_attribute_t *)cupsArrayPrev(classes)) | |
282 | cgiSetIPPObjectVars(pclass, NULL, i); | |
283 | } | |
ef416fc2 | 284 | |
fa73b229 | 285 | /* |
286 | * Save navigation URLs... | |
287 | */ | |
ef416fc2 | 288 | |
fa73b229 | 289 | urlend = url + sizeof(url); |
ef416fc2 | 290 | |
fa73b229 | 291 | if ((var = cgiGetVariable("QUERY")) != NULL) |
292 | { | |
293 | strlcpy(url, "/classes/?QUERY=", sizeof(url)); | |
294 | urlptr = url + strlen(url); | |
ef416fc2 | 295 | |
fa73b229 | 296 | cgiFormEncode(urlptr, var, urlend - urlptr); |
297 | urlptr += strlen(urlptr); | |
ef416fc2 | 298 | |
fa73b229 | 299 | strlcpy(urlptr, "&", urlend - urlptr); |
300 | urlptr += strlen(urlptr); | |
301 | } | |
302 | else | |
303 | { | |
304 | strlcpy(url, "/classes/?", sizeof(url)); | |
305 | urlptr = url + strlen(url); | |
306 | } | |
ef416fc2 | 307 | |
fa73b229 | 308 | snprintf(urlptr, urlend - urlptr, "FIRST=%d", first); |
309 | cgiSetVariable("THISURL", url); | |
310 | ||
311 | if (first > 0) | |
312 | { | |
313 | snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s", | |
314 | first - CUPS_PAGE_MAX, ascending ? "asc" : "dec"); | |
315 | cgiSetVariable("PREVURL", url); | |
ef416fc2 | 316 | } |
fa73b229 | 317 | |
318 | if ((first + CUPS_PAGE_MAX) < count) | |
319 | { | |
320 | snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s", | |
321 | first + CUPS_PAGE_MAX, ascending ? "asc" : "dec"); | |
322 | cgiSetVariable("NEXTURL", url); | |
323 | } | |
324 | ||
ef416fc2 | 325 | /* |
fa73b229 | 326 | * Then show everything... |
ef416fc2 | 327 | */ |
328 | ||
fa73b229 | 329 | cgiCopyTemplateLang("search.tmpl"); |
ef416fc2 | 330 | |
fa73b229 | 331 | cgiCopyTemplateLang("classes-header.tmpl"); |
ef416fc2 | 332 | |
fa73b229 | 333 | if (count > 0) |
334 | cgiCopyTemplateLang("pager.tmpl"); | |
ef416fc2 | 335 | |
fa73b229 | 336 | cgiCopyTemplateLang("classes.tmpl"); |
ef416fc2 | 337 | |
fa73b229 | 338 | if (count > 0) |
339 | cgiCopyTemplateLang("pager.tmpl"); | |
ef416fc2 | 340 | |
341 | /* | |
fa73b229 | 342 | * Delete the response... |
ef416fc2 | 343 | */ |
344 | ||
bd7854cb | 345 | cupsArrayDelete(classes); |
fa73b229 | 346 | ippDelete(response); |
347 | } | |
348 | else | |
349 | { | |
350 | /* | |
351 | * Show the error... | |
352 | */ | |
ef416fc2 | 353 | |
fa73b229 | 354 | cgiShowIPPError(_("Unable to get class list:")); |
355 | } | |
ef416fc2 | 356 | |
fa73b229 | 357 | cgiEndHTML(); |
358 | } | |
ef416fc2 | 359 | |
ef416fc2 | 360 | |
fa73b229 | 361 | /* |
362 | * 'show_class()' - Show a single class. | |
363 | */ | |
ef416fc2 | 364 | |
fa73b229 | 365 | void |
366 | show_class(http_t *http, /* I - Connection to server */ | |
367 | const char *pclass) /* I - Name of class */ | |
368 | { | |
369 | ipp_t *request, /* IPP request */ | |
370 | *response; /* IPP response */ | |
371 | ipp_attribute_t *attr; /* IPP attribute */ | |
372 | char uri[HTTP_MAX_URI]; /* Printer URI */ | |
373 | char refresh[1024]; /* Refresh URL */ | |
374 | ||
375 | ||
376 | /* | |
377 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following | |
378 | * attributes: | |
379 | * | |
380 | * attributes-charset | |
381 | * attributes-natural-language | |
382 | * printer-uri | |
383 | */ | |
ef416fc2 | 384 | |
fa73b229 | 385 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); |
ef416fc2 | 386 | |
a4d04587 | 387 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
388 | "localhost", 0, "/classes/%s", pclass); | |
fa73b229 | 389 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, |
390 | uri); | |
ef416fc2 | 391 | |
fa73b229 | 392 | cgiGetAttributes(request, "classes.tmpl"); |
393 | ||
394 | /* | |
395 | * Do the request and get back a response... | |
396 | */ | |
397 | ||
398 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
399 | { | |
ef416fc2 | 400 | /* |
fa73b229 | 401 | * Got the result; set the CGI variables and check the status of a |
402 | * single-queue request... | |
ef416fc2 | 403 | */ |
404 | ||
fa73b229 | 405 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); |
406 | ||
407 | if (pclass && (attr = ippFindAttribute(response, "printer-state", | |
408 | IPP_TAG_ENUM)) != NULL && | |
409 | attr->values[0].integer == IPP_PRINTER_PROCESSING) | |
ef416fc2 | 410 | { |
fa73b229 | 411 | /* |
412 | * Class is processing - automatically refresh the page until we | |
413 | * are done printing... | |
414 | */ | |
ef416fc2 | 415 | |
fa73b229 | 416 | cgiFormEncode(uri, pclass, sizeof(uri)); |
417 | snprintf(refresh, sizeof(refresh), "10;/classes/%s", uri); | |
418 | cgiSetVariable("refresh_page", refresh); | |
ef416fc2 | 419 | } |
ef416fc2 | 420 | |
fa73b229 | 421 | /* |
422 | * Delete the response... | |
423 | */ | |
424 | ||
425 | ippDelete(response); | |
ef416fc2 | 426 | |
427 | /* | |
428 | * Show the standard header... | |
429 | */ | |
430 | ||
fa73b229 | 431 | cgiStartHTML(pclass); |
ef416fc2 | 432 | |
433 | /* | |
fa73b229 | 434 | * Show the class status... |
ef416fc2 | 435 | */ |
436 | ||
fa73b229 | 437 | cgiCopyTemplateLang("classes.tmpl"); |
ef416fc2 | 438 | |
fa73b229 | 439 | /* |
440 | * Show jobs for the specified class... | |
441 | */ | |
ef416fc2 | 442 | |
fa73b229 | 443 | cgiCopyTemplateLang("class-jobs-header.tmpl"); |
444 | cgiShowJobs(http, pclass); | |
445 | } | |
446 | else | |
447 | { | |
448 | /* | |
449 | * Show the IPP error... | |
450 | */ | |
ef416fc2 | 451 | |
fa73b229 | 452 | cgiStartHTML(pclass); |
453 | cgiShowIPPError(_("Unable to get class status:")); | |
454 | } | |
ef416fc2 | 455 | |
fa73b229 | 456 | cgiEndHTML(); |
ef416fc2 | 457 | } |
458 | ||
459 | ||
460 | /* | |
b423cd4c | 461 | * End of "$Id: classes.c 5160 2006-02-24 01:14:18Z mike $". |
ef416fc2 | 462 | */ |