]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/printers.c
Update svn:keyword properties.
[thirdparty/cups.git] / cgi-bin / printers.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
0268488e 4 * Printer status CGI for CUPS.
ef416fc2 5 *
f3c17241 6 * Copyright 2007-2012 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");
ef55b745 76 cgiSetVariable("REFRESH_PAGE", "");
ef416fc2 77
78 /*
fa73b229 79 * See if we are displaying a printer or all printers...
ef416fc2 80 */
81
b423cd4c 82 if ((printer = getenv("PATH_INFO")) != NULL)
4744bd90 83 {
b423cd4c 84 printer ++;
ef416fc2 85
4744bd90 86 if (!*printer)
87 printer = NULL;
58dc1933
MS
88
89 if (printer)
90 cgiSetVariable("PRINTER_NAME", printer);
4744bd90 91 }
92
ef416fc2 93 /*
fa73b229 94 * See who is logged in...
ef416fc2 95 */
96
f301802f 97 user = getenv("REMOTE_USER");
ef416fc2 98
99 /*
fa73b229 100 * Connect to the HTTP server...
ef416fc2 101 */
102
fa73b229 103 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
ef416fc2 104
fa73b229 105 /*
106 * Get the default printer...
107 */
ef416fc2 108
2e4ff8af 109 if (!op || !cgiIsPOST())
ef416fc2 110 {
111 /*
112 * Get the default destination...
113 */
114
fa73b229 115 request = ippNewRequest(CUPS_GET_DEFAULT);
ef416fc2 116
117 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
118 "requested-attributes",
119 sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
120
121 if ((response = cupsDoRequest(http, request, "/")) != NULL)
122 {
123 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
124 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
125
126 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
127 {
128 char url[HTTP_MAX_URI]; /* New URL */
129
130
131 cgiSetVariable("DEFAULT_URI",
132 cgiRewriteURL(attr->values[0].string.text,
133 url, sizeof(url), NULL));
134 }
135
136 ippDelete(response);
137 }
138
139 /*
fa73b229 140 * See if we need to show a list of printers or the status of a
141 * single printer...
ef416fc2 142 */
143
fa73b229 144 if (!printer)
145 show_all_printers(http, user);
146 else
147 show_printer(http, printer);
148 }
58dc1933
MS
149 else if (printer)
150 {
0268488e
MS
151 if (!*op)
152 {
153 const char *server_port = getenv("SERVER_PORT");
154 /* Port number string */
155 int port = atoi(server_port ? server_port : "0");
156 /* Port number */
157 char uri[1024]; /* URL */
158
159 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri),
160 getenv("HTTPS") ? "https" : "http", NULL,
161 getenv("SERVER_NAME"), port, "/printers/%s", printer);
162
163 printf("Location: %s\n\n", uri);
164 }
165 else if (!strcmp(op, "start-printer"))
58dc1933
MS
166 do_printer_op(http, printer, IPP_RESUME_PRINTER,
167 cgiText(_("Resume Printer")));
168 else if (!strcmp(op, "stop-printer"))
169 do_printer_op(http, printer, IPP_PAUSE_PRINTER,
170 cgiText(_("Pause Printer")));
171 else if (!strcmp(op, "accept-jobs"))
172 do_printer_op(http, printer, CUPS_ACCEPT_JOBS, cgiText(_("Accept Jobs")));
173 else if (!strcmp(op, "reject-jobs"))
174 do_printer_op(http, printer, CUPS_REJECT_JOBS, cgiText(_("Reject Jobs")));
175 else if (!strcmp(op, "purge-jobs"))
176 do_printer_op(http, printer, IPP_PURGE_JOBS, cgiText(_("Purge Jobs")));
88f9aafc 177 else if (!_cups_strcasecmp(op, "print-self-test-page"))
58dc1933
MS
178 cgiPrintCommand(http, printer, "PrintSelfTestPage",
179 cgiText(_("Print Self-Test Page")));
88f9aafc 180 else if (!_cups_strcasecmp(op, "clean-print-heads"))
58dc1933
MS
181 cgiPrintCommand(http, printer, "Clean all",
182 cgiText(_("Clean Print Heads")));
88f9aafc 183 else if (!_cups_strcasecmp(op, "print-test-page"))
58dc1933 184 cgiPrintTestPage(http, printer);
88f9aafc 185 else if (!_cups_strcasecmp(op, "move-jobs"))
58dc1933
MS
186 cgiMoveJobs(http, printer, 0);
187 else
188 {
189 /*
190 * Unknown/bad operation...
191 */
192
193 cgiStartHTML(printer);
194 cgiCopyTemplateLang("error-op.tmpl");
195 cgiEndHTML();
196 }
197 }
fa73b229 198 else
199 {
200 /*
201 * Unknown/bad operation...
202 */
ef416fc2 203
58dc1933 204 cgiStartHTML(cgiText(_("Printers")));
fa73b229 205 cgiCopyTemplateLang("error-op.tmpl");
206 cgiEndHTML();
207 }
ef416fc2 208
fa73b229 209 /*
210 * Close the HTTP server connection...
211 */
212
213 httpClose(http);
214
215 /*
216 * Return with no errors...
217 */
ef416fc2 218
fa73b229 219 return (0);
220}
ef416fc2 221
ef416fc2 222
b423cd4c 223/*
58dc1933 224 * 'do_printer_op()' - Do a printer operation.
b423cd4c 225 */
226
58dc1933
MS
227static void
228do_printer_op(http_t *http, /* I - HTTP connection */
229 const char *printer, /* I - Printer name */
230 ipp_op_t op, /* I - Operation to perform */
231 const char *title) /* I - Title of page */
b423cd4c 232{
58dc1933 233 ipp_t *request; /* IPP request */
b423cd4c 234 char uri[HTTP_MAX_URI], /* Printer URI */
58dc1933 235 resource[HTTP_MAX_URI]; /* Path for request */
b423cd4c 236
b423cd4c 237
238 /*
58dc1933 239 * Build a printer request, which requires the following
b423cd4c 240 * attributes:
241 *
242 * attributes-charset
243 * attributes-natural-language
244 * printer-uri
b423cd4c 245 */
246
58dc1933 247 request = ippNewRequest(op);
b423cd4c 248
58dc1933
MS
249 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
250 "localhost", 0, "/printers/%s", printer);
b423cd4c 251 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
252 NULL, uri);
253
b423cd4c 254 /*
255 * Do the request and get back a response...
256 */
257
58dc1933
MS
258 snprintf(resource, sizeof(resource), "/printers/%s", printer);
259 ippDelete(cupsDoRequest(http, request, resource));
b423cd4c 260
58dc1933
MS
261 if (cupsLastError() == IPP_NOT_AUTHORIZED)
262 {
263 puts("Status: 401\n");
264 exit(0);
b423cd4c 265 }
58dc1933
MS
266 else if (cupsLastError() > IPP_OK_CONFLICT)
267 {
268 cgiStartHTML(title);
f3c17241 269 cgiShowIPPError(_("Unable to do maintenance command"));
58dc1933
MS
270 }
271 else
b423cd4c 272 {
273 /*
58dc1933 274 * Redirect successful updates back to the printer page...
b423cd4c 275 */
276
58dc1933
MS
277 char url[1024], /* Printer/class URL */
278 refresh[1024]; /* Refresh URL */
b423cd4c 279
b423cd4c 280
58dc1933
MS
281 cgiRewriteURL(uri, url, sizeof(url), NULL);
282 cgiFormEncode(uri, url, sizeof(uri));
52f6f666 283 snprintf(refresh, sizeof(refresh), "5;URL=%s", uri);
58dc1933 284 cgiSetVariable("refresh_page", refresh);
b423cd4c 285
58dc1933
MS
286 cgiStartHTML(title);
287
288 if (op == IPP_PAUSE_PRINTER)
289 cgiCopyTemplateLang("printer-stop.tmpl");
290 else if (op == IPP_RESUME_PRINTER)
291 cgiCopyTemplateLang("printer-start.tmpl");
292 else if (op == CUPS_ACCEPT_JOBS)
293 cgiCopyTemplateLang("printer-accept.tmpl");
294 else if (op == CUPS_REJECT_JOBS)
295 cgiCopyTemplateLang("printer-reject.tmpl");
296 else if (op == IPP_PURGE_JOBS)
297 cgiCopyTemplateLang("printer-purge.tmpl");
b423cd4c 298 }
299
300 cgiEndHTML();
301}
302
303
fa73b229 304/*
305 * 'show_all_printers()' - Show all printers...
306 */
ef416fc2 307
58dc1933 308static void
fa73b229 309show_all_printers(http_t *http, /* I - Connection to server */
310 const char *user) /* I - Username */
311{
312 int i; /* Looping var */
313 ipp_t *request, /* IPP request */
314 *response; /* IPP response */
315 cups_array_t *printers; /* Array of printer objects */
e6013cfa 316 ipp_attribute_t *printer; /* Printer object */
fa73b229 317 int ascending, /* Order of printers (0 = descending) */
318 first, /* First printer to show */
319 count; /* Number of printers */
320 const char *var; /* Form variable */
321 void *search; /* Search data */
2e4ff8af 322 char val[1024]; /* Form variable */
ef416fc2 323
ef416fc2 324
bd7854cb 325 fprintf(stderr, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
f301802f 326 http, user ? user : "(null)");
bd7854cb 327
fa73b229 328 /*
329 * Show the standard header...
330 */
ef416fc2 331
fa73b229 332 cgiStartHTML(cgiText(_("Printers")));
ef416fc2 333
fa73b229 334 /*
335 * Build a CUPS_GET_PRINTERS request, which requires the following
336 * attributes:
337 *
338 * attributes-charset
339 * attributes-natural-language
340 * printer-type
341 * printer-type-mask
342 * requesting-user-name
343 */
ef416fc2 344
fa73b229 345 request = ippNewRequest(CUPS_GET_PRINTERS);
ef416fc2 346
fa73b229 347 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
348 "printer-type", 0);
349 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
350 "printer-type-mask", CUPS_PRINTER_CLASS);
ef416fc2 351
f301802f 352 if (user)
353 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
354 "requesting-user-name", NULL, user);
ef416fc2 355
fa73b229 356 cgiGetAttributes(request, "printers.tmpl");
ef416fc2 357
fa73b229 358 /*
359 * Do the request and get back a response...
360 */
361
362 if ((response = cupsDoRequest(http, request, "/")) != NULL)
363 {
ef416fc2 364 /*
fa73b229 365 * Get a list of matching job objects.
ef416fc2 366 */
367
2e4ff8af
MS
368 if ((var = cgiGetVariable("QUERY")) != NULL &&
369 !cgiGetVariable("CLEAR"))
fa73b229 370 search = cgiCompileSearch(var);
371 else
372 search = NULL;
ef416fc2 373
fa73b229 374 printers = cgiGetIPPObjects(response, search);
375 count = cupsArrayCount(printers);
ef416fc2 376
fa73b229 377 if (search)
378 cgiFreeSearch(search);
ef416fc2 379
380 /*
fa73b229 381 * Figure out which printers to display...
ef416fc2 382 */
383
fa73b229 384 if ((var = cgiGetVariable("FIRST")) != NULL)
385 first = atoi(var);
386 else
387 first = 0;
ef416fc2 388
fa73b229 389 if (first >= count)
390 first = count - CUPS_PAGE_MAX;
ef416fc2 391
fa73b229 392 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
ef416fc2 393
fa73b229 394 if (first < 0)
395 first = 0;
ef416fc2 396
2e4ff8af
MS
397 sprintf(val, "%d", count);
398 cgiSetVariable("TOTAL", val);
ef416fc2 399
5a9febac 400 if ((var = cgiGetVariable("ORDER")) != NULL && *var)
88f9aafc 401 ascending = !_cups_strcasecmp(var, "asc");
fa73b229 402 else
403 ascending = 1;
ef416fc2 404
fa73b229 405 if (ascending)
406 {
407 for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, first);
408 i < CUPS_PAGE_MAX && printer;
409 i ++, printer = (ipp_attribute_t *)cupsArrayNext(printers))
410 cgiSetIPPObjectVars(printer, NULL, i);
411 }
412 else
413 {
414 for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, count - first - 1);
415 i < CUPS_PAGE_MAX && printer;
416 i ++, printer = (ipp_attribute_t *)cupsArrayPrev(printers))
417 cgiSetIPPObjectVars(printer, NULL, i);
418 }
ef416fc2 419
fa73b229 420 /*
421 * Save navigation URLs...
422 */
ef416fc2 423
2e4ff8af 424 cgiSetVariable("THISURL", "/printers/");
fa73b229 425
426 if (first > 0)
427 {
2e4ff8af
MS
428 sprintf(val, "%d", first - CUPS_PAGE_MAX);
429 cgiSetVariable("PREV", val);
ef416fc2 430 }
fa73b229 431
432 if ((first + CUPS_PAGE_MAX) < count)
433 {
2e4ff8af
MS
434 sprintf(val, "%d", first + CUPS_PAGE_MAX);
435 cgiSetVariable("NEXT", val);
fa73b229 436 }
437
ef416fc2 438 /*
fa73b229 439 * Then show everything...
ef416fc2 440 */
441
fa73b229 442 cgiCopyTemplateLang("search.tmpl");
ef416fc2 443
fa73b229 444 cgiCopyTemplateLang("printers-header.tmpl");
ef416fc2 445
b19ccc9e 446 if (count > CUPS_PAGE_MAX)
fa73b229 447 cgiCopyTemplateLang("pager.tmpl");
ef416fc2 448
fa73b229 449 cgiCopyTemplateLang("printers.tmpl");
ef416fc2 450
b19ccc9e 451 if (count > CUPS_PAGE_MAX)
fa73b229 452 cgiCopyTemplateLang("pager.tmpl");
ef416fc2 453
454 /*
fa73b229 455 * Delete the response...
ef416fc2 456 */
457
bd7854cb 458 cupsArrayDelete(printers);
fa73b229 459 ippDelete(response);
460 }
461 else
462 {
463 /*
464 * Show the error...
465 */
ef416fc2 466
f3c17241 467 cgiShowIPPError(_("Unable to get printer list"));
fa73b229 468 }
ef416fc2 469
fa73b229 470 cgiEndHTML();
471}
ef416fc2 472
ef416fc2 473
fa73b229 474/*
475 * 'show_printer()' - Show a single printer.
476 */
ef416fc2 477
58dc1933 478static void
fa73b229 479show_printer(http_t *http, /* I - Connection to server */
480 const char *printer) /* I - Name of printer */
481{
482 ipp_t *request, /* IPP request */
483 *response; /* IPP response */
484 ipp_attribute_t *attr; /* IPP attribute */
485 char uri[HTTP_MAX_URI]; /* Printer URI */
486 char refresh[1024]; /* Refresh URL */
ef416fc2 487
ef416fc2 488
bd7854cb 489 fprintf(stderr, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
f301802f 490 http, printer ? printer : "(null)");
bd7854cb 491
fa73b229 492 /*
493 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
494 * attributes:
495 *
496 * attributes-charset
497 * attributes-natural-language
498 * printer-uri
499 */
500
501 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
502
a4d04587 503 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
504 "localhost", 0, "/printers/%s", printer);
fa73b229 505 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
506 uri);
507
b19ccc9e 508 cgiGetAttributes(request, "printer.tmpl");
fa73b229 509
510 /*
511 * Do the request and get back a response...
512 */
ef416fc2 513
fa73b229 514 if ((response = cupsDoRequest(http, request, "/")) != NULL)
515 {
ef416fc2 516 /*
fa73b229 517 * Got the result; set the CGI variables and check the status of a
518 * single-queue request...
ef416fc2 519 */
520
fa73b229 521 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
522
523 if (printer && (attr = ippFindAttribute(response, "printer-state",
524 IPP_TAG_ENUM)) != NULL &&
525 attr->values[0].integer == IPP_PRINTER_PROCESSING)
ef416fc2 526 {
fa73b229 527 /*
528 * Printer is processing - automatically refresh the page until we
529 * are done printing...
530 */
ef416fc2 531
fa73b229 532 cgiFormEncode(uri, printer, sizeof(uri));
f301802f 533 snprintf(refresh, sizeof(refresh), "10;URL=/printers/%s", uri);
fa73b229 534 cgiSetVariable("refresh_page", refresh);
ef416fc2 535 }
ef416fc2 536
fa73b229 537 /*
538 * Delete the response...
539 */
540
541 ippDelete(response);
ef416fc2 542
543 /*
544 * Show the standard header...
545 */
546
fa73b229 547 cgiStartHTML(printer);
ef416fc2 548
549 /*
fa73b229 550 * Show the printer status...
ef416fc2 551 */
552
b19ccc9e 553 cgiCopyTemplateLang("printer.tmpl");
ef416fc2 554
fa73b229 555 /*
556 * Show jobs for the specified printer...
557 */
ef416fc2 558
fa73b229 559 cgiCopyTemplateLang("printer-jobs-header.tmpl");
560 cgiShowJobs(http, printer);
561 }
562 else
563 {
564 /*
565 * Show the IPP error...
566 */
ef416fc2 567
fa73b229 568 cgiStartHTML(printer);
f3c17241 569 cgiShowIPPError(_("Unable to get printer status"));
fa73b229 570 }
ef416fc2 571
fa73b229 572 cgiEndHTML();
ef416fc2 573}
574
575
576/*
f2d18633 577 * End of "$Id$".
ef416fc2 578 */