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