]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/classes.c
Load cups into easysw/current.
[thirdparty/cups.git] / cgi-bin / classes.c
CommitLineData
ef416fc2 1/*
f7deaa1a 2 * "$Id: classes.c 5571 2006-05-22 18:46:55Z 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
42void show_all_classes(http_t *http, const char *username);
43void show_class(http_t *http, const char *printer);
44
45
ef416fc2 46/*
47 * 'main()' - Main entry for CGI.
48 */
49
50int /* O - Exit status */
51main(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
f301802f 98 user = getenv("REMOTE_USER");
ef416fc2 99
100 /*
fa73b229 101 * Connect to the HTTP server...
ef416fc2 102 */
103
fa73b229 104 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
ef416fc2 105
106 /*
fa73b229 107 * Get the default printer...
ef416fc2 108 */
109
fa73b229 110 if (!op)
ef416fc2 111 {
ef416fc2 112 /*
113 * Get the default destination...
114 */
115
fa73b229 116 request = ippNewRequest(CUPS_GET_DEFAULT);
ef416fc2 117
118 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
119 "requested-attributes",
120 sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
121
122 if ((response = cupsDoRequest(http, request, "/")) != NULL)
123 {
124 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
125 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
126
127 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
128 {
129 char url[HTTP_MAX_URI]; /* New URL */
130
131
132 cgiSetVariable("DEFAULT_URI",
133 cgiRewriteURL(attr->values[0].string.text,
134 url, sizeof(url), NULL));
135 }
136
137 ippDelete(response);
138 }
139
140 /*
fa73b229 141 * See if we need to show a list of classes or the status of a
142 * single printer...
ef416fc2 143 */
144
fa73b229 145 if (!pclass)
146 show_all_classes(http, user);
147 else
148 show_class(http, pclass);
149 }
150 else if (!strcasecmp(op, "print-test-page") && pclass)
151 cgiPrintTestPage(http, pclass);
152 else if (!strcasecmp(op, "move-jobs") && pclass)
153 cgiMoveJobs(http, pclass, 0);
154 else
155 {
156 /*
157 * Unknown/bad operation...
158 */
ef416fc2 159
fa73b229 160 if (pclass)
161 cgiStartHTML(pclass);
162 else
163 cgiStartHTML(cgiText(_("Classes")));
ef416fc2 164
fa73b229 165 cgiCopyTemplateLang("error-op.tmpl");
166 cgiEndHTML();
167 }
ef416fc2 168
fa73b229 169 /*
170 * Close the HTTP server connection...
171 */
ef416fc2 172
fa73b229 173 httpClose(http);
ef416fc2 174
fa73b229 175 /*
176 * Return with no errors...
177 */
ef416fc2 178
fa73b229 179 return (0);
180}
ef416fc2 181
ef416fc2 182
fa73b229 183/*
184 * 'show_all_classes()' - Show all classes...
185 */
ef416fc2 186
fa73b229 187void
188show_all_classes(http_t *http, /* I - Connection to server */
f301802f 189 const char *user) /* I - Username */
fa73b229 190{
191 int i; /* Looping var */
192 ipp_t *request, /* IPP request */
193 *response; /* IPP response */
194 cups_array_t *classes; /* Array of class objects */
195 ipp_attribute_t *pclass; /* Class object */
196 int ascending, /* Order of classes (0 = descending) */
197 first, /* First class to show */
198 count; /* Number of classes */
199 const char *var; /* Form variable */
200 void *search; /* Search data */
201 char url[1024], /* URL for prev/next/this */
202 *urlptr, /* Position in URL */
203 *urlend; /* End of URL */
ef416fc2 204
ef416fc2 205
fa73b229 206 /*
207 * Show the standard header...
208 */
209
210 cgiStartHTML(cgiText(_("Classes")));
211
212 /*
213 * Build a CUPS_GET_CLASSES request, which requires the following
214 * attributes:
215 *
216 * attributes-charset
217 * attributes-natural-language
218 * requesting-user-name
219 */
220
221 request = ippNewRequest(CUPS_GET_CLASSES);
222
f301802f 223 if (user)
224 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
225 "requesting-user-name", NULL, user);
fa73b229 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 370void
371show_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));
f301802f 422 snprintf(refresh, sizeof(refresh), "10;URL=/classes/%s", uri);
fa73b229 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/*
f7deaa1a 466 * End of "$Id: classes.c 5571 2006-05-22 18:46:55Z mike $".
ef416fc2 467 */