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