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