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