]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/classes.c
Merge changes from CUPS 1.5svn-r9407.
[thirdparty/cups.git] / cgi-bin / classes.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: classes.c 7940 2008-09-16 00:45:16Z mike $"
ef416fc2 3 *
4 * Class status CGI for the Common UNIX Printing System (CUPS).
5 *
52f6f666 6 * Copyright 2007-2008 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 *
52f6f666
MS
17 * main() - Main entry for CGI.
18 * do_class_op() - Do a class operation.
19 * show_all_classes() - Show all classes...
20 * show_class() - Show a single class.
ef416fc2 21 */
22
23/*
24 * Include necessary headers...
25 */
26
27#include "cgi-private.h"
28
29
fa73b229 30/*
31 * Local functions...
32 */
33
58dc1933
MS
34static void do_class_op(http_t *http, const char *printer, ipp_op_t op,
35 const char *title);
36static void show_all_classes(http_t *http, const char *username);
37static void show_class(http_t *http, const char *printer);
fa73b229 38
39
ef416fc2 40/*
41 * 'main()' - Main entry for CGI.
42 */
43
44int /* O - Exit status */
45main(int argc, /* I - Number of command-line arguments */
46 char *argv[]) /* I - Command-line arguments */
47{
fa73b229 48 const char *pclass; /* Class name */
49 const char *user; /* Username */
ef416fc2 50 http_t *http; /* Connection to the server */
51 ipp_t *request, /* IPP request */
52 *response; /* IPP response */
53 ipp_attribute_t *attr; /* IPP attribute */
ef416fc2 54 const char *op; /* Operation to perform, if any */
fa73b229 55 static const char *def_attrs[] = /* Attributes for default printer */
ef416fc2 56 {
57 "printer-name",
58 "printer-uri-supported"
59 };
60
61
62 /*
63 * Get any form variables...
64 */
65
66 cgiInitialize();
fa73b229 67
ef416fc2 68 op = cgiGetVariable("OP");
69
70 /*
71 * Set the web interface section...
72 */
73
74 cgiSetVariable("SECTION", "classes");
ef55b745 75 cgiSetVariable("REFRESH_PAGE", "");
ef416fc2 76
77 /*
fa73b229 78 * See if we are displaying a printer or all classes...
ef416fc2 79 */
80
b423cd4c 81 if ((pclass = getenv("PATH_INFO")) != NULL)
4744bd90 82 {
b423cd4c 83 pclass ++;
ef416fc2 84
4744bd90 85 if (!*pclass)
86 pclass = NULL;
58dc1933
MS
87
88 if (pclass)
89 cgiSetVariable("PRINTER_NAME", pclass);
4744bd90 90 }
91
ef416fc2 92 /*
fa73b229 93 * See who is logged in...
ef416fc2 94 */
95
f301802f 96 user = getenv("REMOTE_USER");
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
2e4ff8af 108 if (!op || !cgiIsPOST())
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 }
58dc1933
MS
148 else if (pclass)
149 {
150 if (!strcmp(op, "start-class"))
151 do_class_op(http, pclass, IPP_RESUME_PRINTER, cgiText(_("Resume Class")));
152 else if (!strcmp(op, "stop-class"))
153 do_class_op(http, pclass, IPP_PAUSE_PRINTER, cgiText(_("Pause Class")));
154 else if (!strcmp(op, "accept-jobs"))
155 do_class_op(http, pclass, CUPS_ACCEPT_JOBS, cgiText(_("Accept Jobs")));
156 else if (!strcmp(op, "reject-jobs"))
157 do_class_op(http, pclass, CUPS_REJECT_JOBS, cgiText(_("Reject Jobs")));
158 else if (!strcmp(op, "purge-jobs"))
159 do_class_op(http, pclass, IPP_PURGE_JOBS, cgiText(_("Purge Jobs")));
160 else if (!strcasecmp(op, "print-test-page"))
161 cgiPrintTestPage(http, pclass);
162 else if (!strcasecmp(op, "move-jobs"))
163 cgiMoveJobs(http, pclass, 0);
164 else
165 {
166 /*
167 * Unknown/bad operation...
168 */
169
170 cgiStartHTML(pclass);
171 cgiCopyTemplateLang("error-op.tmpl");
172 cgiEndHTML();
173 }
174 }
fa73b229 175 else
176 {
177 /*
178 * Unknown/bad operation...
179 */
ef416fc2 180
58dc1933 181 cgiStartHTML(cgiText(_("Classes")));
fa73b229 182 cgiCopyTemplateLang("error-op.tmpl");
183 cgiEndHTML();
184 }
ef416fc2 185
fa73b229 186 /*
187 * Close the HTTP server connection...
188 */
ef416fc2 189
fa73b229 190 httpClose(http);
ef416fc2 191
fa73b229 192 /*
193 * Return with no errors...
194 */
ef416fc2 195
fa73b229 196 return (0);
197}
ef416fc2 198
ef416fc2 199
58dc1933
MS
200/*
201 * 'do_class_op()' - Do a class operation.
202 */
203
204static void
205do_class_op(http_t *http, /* I - HTTP connection */
206 const char *printer, /* I - Printer name */
207 ipp_op_t op, /* I - Operation to perform */
208 const char *title) /* I - Title of page */
209{
210 ipp_t *request; /* IPP request */
211 char uri[HTTP_MAX_URI], /* Printer URI */
212 resource[HTTP_MAX_URI]; /* Path for request */
213
214
215 /*
216 * Build a printer request, which requires the following
217 * attributes:
218 *
219 * attributes-charset
220 * attributes-natural-language
221 * printer-uri
222 */
223
224 request = ippNewRequest(op);
225
226 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
227 "localhost", 0, "/classes/%s", printer);
228 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
229 NULL, uri);
230
231 /*
232 * Do the request and get back a response...
233 */
234
235 snprintf(resource, sizeof(resource), "/classes/%s", printer);
236 ippDelete(cupsDoRequest(http, request, resource));
237
238 if (cupsLastError() == IPP_NOT_AUTHORIZED)
239 {
240 puts("Status: 401\n");
241 exit(0);
242 }
243 else if (cupsLastError() > IPP_OK_CONFLICT)
244 {
245 cgiStartHTML(title);
246 cgiShowIPPError(_("Unable to do maintenance command:"));
247 }
248 else
249 {
250 /*
251 * Redirect successful updates back to the printer page...
252 */
253
254 char url[1024], /* Printer/class URL */
255 refresh[1024]; /* Refresh URL */
256
257
258 cgiRewriteURL(uri, url, sizeof(url), NULL);
259 cgiFormEncode(uri, url, sizeof(uri));
260 snprintf(refresh, sizeof(refresh), "5;URL=%s", uri);
261 cgiSetVariable("refresh_page", refresh);
262
263 cgiStartHTML(title);
264
265 cgiSetVariable("IS_CLASS", "YES");
266
267 if (op == IPP_PAUSE_PRINTER)
268 cgiCopyTemplateLang("printer-stop.tmpl");
269 else if (op == IPP_RESUME_PRINTER)
270 cgiCopyTemplateLang("printer-start.tmpl");
271 else if (op == CUPS_ACCEPT_JOBS)
272 cgiCopyTemplateLang("printer-accept.tmpl");
273 else if (op == CUPS_REJECT_JOBS)
274 cgiCopyTemplateLang("printer-reject.tmpl");
275 else if (op == IPP_PURGE_JOBS)
276 cgiCopyTemplateLang("printer-purge.tmpl");
277 }
278
279 cgiEndHTML();
280}
281
282
fa73b229 283/*
284 * 'show_all_classes()' - Show all classes...
285 */
ef416fc2 286
58dc1933 287static void
fa73b229 288show_all_classes(http_t *http, /* I - Connection to server */
f301802f 289 const char *user) /* I - Username */
fa73b229 290{
291 int i; /* Looping var */
292 ipp_t *request, /* IPP request */
293 *response; /* IPP response */
294 cups_array_t *classes; /* Array of class objects */
295 ipp_attribute_t *pclass; /* Class object */
296 int ascending, /* Order of classes (0 = descending) */
297 first, /* First class to show */
298 count; /* Number of classes */
299 const char *var; /* Form variable */
300 void *search; /* Search data */
2e4ff8af 301 char val[1024]; /* Form variable */
ef416fc2 302
ef416fc2 303
fa73b229 304 /*
305 * Show the standard header...
306 */
307
308 cgiStartHTML(cgiText(_("Classes")));
309
310 /*
311 * Build a CUPS_GET_CLASSES request, which requires the following
312 * attributes:
313 *
314 * attributes-charset
315 * attributes-natural-language
316 * requesting-user-name
317 */
318
319 request = ippNewRequest(CUPS_GET_CLASSES);
320
f301802f 321 if (user)
322 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
323 "requesting-user-name", NULL, user);
fa73b229 324
325 cgiGetAttributes(request, "classes.tmpl");
326
327 /*
328 * Do the request and get back a response...
329 */
330
331 if ((response = cupsDoRequest(http, request, "/")) != NULL)
332 {
ef416fc2 333 /*
fa73b229 334 * Get a list of matching job objects.
ef416fc2 335 */
336
2e4ff8af
MS
337 if ((var = cgiGetVariable("QUERY")) != NULL &&
338 !cgiGetVariable("CLEAR"))
fa73b229 339 search = cgiCompileSearch(var);
340 else
341 search = NULL;
342
343 classes = cgiGetIPPObjects(response, search);
344 count = cupsArrayCount(classes);
345
346 if (search)
347 cgiFreeSearch(search);
ef416fc2 348
349 /*
fa73b229 350 * Figure out which classes to display...
ef416fc2 351 */
352
fa73b229 353 if ((var = cgiGetVariable("FIRST")) != NULL)
354 first = atoi(var);
355 else
356 first = 0;
ef416fc2 357
fa73b229 358 if (first >= count)
359 first = count - CUPS_PAGE_MAX;
ef416fc2 360
fa73b229 361 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
ef416fc2 362
fa73b229 363 if (first < 0)
364 first = 0;
ef416fc2 365
2e4ff8af
MS
366 sprintf(val, "%d", count);
367 cgiSetVariable("TOTAL", val);
ef416fc2 368
fa73b229 369 if ((var = cgiGetVariable("ORDER")) != NULL)
370 ascending = !strcasecmp(var, "asc");
371 else
372 ascending = 1;
ef416fc2 373
fa73b229 374 if (ascending)
375 {
376 for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, first);
377 i < CUPS_PAGE_MAX && pclass;
378 i ++, pclass = (ipp_attribute_t *)cupsArrayNext(classes))
379 cgiSetIPPObjectVars(pclass, NULL, i);
380 }
381 else
382 {
383 for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, count - first - 1);
384 i < CUPS_PAGE_MAX && pclass;
385 i ++, pclass = (ipp_attribute_t *)cupsArrayPrev(classes))
386 cgiSetIPPObjectVars(pclass, NULL, i);
387 }
ef416fc2 388
fa73b229 389 /*
390 * Save navigation URLs...
391 */
ef416fc2 392
2e4ff8af 393 cgiSetVariable("THISURL", "/classes/");
fa73b229 394
395 if (first > 0)
396 {
2e4ff8af
MS
397 sprintf(val, "%d", first - CUPS_PAGE_MAX);
398 cgiSetVariable("PREV", val);
ef416fc2 399 }
fa73b229 400
401 if ((first + CUPS_PAGE_MAX) < count)
402 {
2e4ff8af
MS
403 sprintf(val, "%d", first + CUPS_PAGE_MAX);
404 cgiSetVariable("NEXT", val);
fa73b229 405 }
406
ef416fc2 407 /*
fa73b229 408 * Then show everything...
ef416fc2 409 */
410
fa73b229 411 cgiCopyTemplateLang("search.tmpl");
ef416fc2 412
fa73b229 413 cgiCopyTemplateLang("classes-header.tmpl");
ef416fc2 414
b19ccc9e 415 if (count > CUPS_PAGE_MAX)
fa73b229 416 cgiCopyTemplateLang("pager.tmpl");
ef416fc2 417
fa73b229 418 cgiCopyTemplateLang("classes.tmpl");
ef416fc2 419
b19ccc9e 420 if (count > CUPS_PAGE_MAX)
fa73b229 421 cgiCopyTemplateLang("pager.tmpl");
ef416fc2 422
423 /*
fa73b229 424 * Delete the response...
ef416fc2 425 */
426
bd7854cb 427 cupsArrayDelete(classes);
fa73b229 428 ippDelete(response);
429 }
430 else
431 {
432 /*
433 * Show the error...
434 */
ef416fc2 435
fa73b229 436 cgiShowIPPError(_("Unable to get class list:"));
437 }
ef416fc2 438
fa73b229 439 cgiEndHTML();
440}
ef416fc2 441
ef416fc2 442
fa73b229 443/*
444 * 'show_class()' - Show a single class.
445 */
ef416fc2 446
58dc1933 447static void
fa73b229 448show_class(http_t *http, /* I - Connection to server */
449 const char *pclass) /* I - Name of class */
450{
451 ipp_t *request, /* IPP request */
452 *response; /* IPP response */
453 ipp_attribute_t *attr; /* IPP attribute */
454 char uri[HTTP_MAX_URI]; /* Printer URI */
455 char refresh[1024]; /* Refresh URL */
456
457
458 /*
459 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
460 * attributes:
461 *
462 * attributes-charset
463 * attributes-natural-language
464 * printer-uri
465 */
ef416fc2 466
fa73b229 467 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
ef416fc2 468
a4d04587 469 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
470 "localhost", 0, "/classes/%s", pclass);
fa73b229 471 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
472 uri);
ef416fc2 473
b19ccc9e 474 cgiGetAttributes(request, "class.tmpl");
fa73b229 475
476 /*
477 * Do the request and get back a response...
478 */
479
480 if ((response = cupsDoRequest(http, request, "/")) != NULL)
481 {
ef416fc2 482 /*
fa73b229 483 * Got the result; set the CGI variables and check the status of a
484 * single-queue request...
ef416fc2 485 */
486
fa73b229 487 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
488
489 if (pclass && (attr = ippFindAttribute(response, "printer-state",
490 IPP_TAG_ENUM)) != NULL &&
491 attr->values[0].integer == IPP_PRINTER_PROCESSING)
ef416fc2 492 {
fa73b229 493 /*
494 * Class is processing - automatically refresh the page until we
495 * are done printing...
496 */
ef416fc2 497
fa73b229 498 cgiFormEncode(uri, pclass, sizeof(uri));
f301802f 499 snprintf(refresh, sizeof(refresh), "10;URL=/classes/%s", uri);
fa73b229 500 cgiSetVariable("refresh_page", refresh);
ef416fc2 501 }
ef416fc2 502
fa73b229 503 /*
504 * Delete the response...
505 */
506
507 ippDelete(response);
ef416fc2 508
509 /*
510 * Show the standard header...
511 */
512
fa73b229 513 cgiStartHTML(pclass);
ef416fc2 514
515 /*
fa73b229 516 * Show the class status...
ef416fc2 517 */
518
b19ccc9e 519 cgiCopyTemplateLang("class.tmpl");
ef416fc2 520
fa73b229 521 /*
522 * Show jobs for the specified class...
523 */
ef416fc2 524
fa73b229 525 cgiCopyTemplateLang("class-jobs-header.tmpl");
526 cgiShowJobs(http, pclass);
527 }
528 else
529 {
530 /*
531 * Show the IPP error...
532 */
ef416fc2 533
fa73b229 534 cgiStartHTML(pclass);
535 cgiShowIPPError(_("Unable to get class status:"));
536 }
ef416fc2 537
fa73b229 538 cgiEndHTML();
ef416fc2 539}
540
541
542/*
b19ccc9e 543 * End of "$Id: classes.c 7940 2008-09-16 00:45:16Z mike $".
ef416fc2 544 */