]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/printers.c
Merge changes from CUPS 1.4svn-r8115.
[thirdparty/cups.git] / cgi-bin / printers.c
1 /*
2 * "$Id: printers.c 7940 2008-09-16 00:45:16Z mike $"
3 *
4 * Printer status CGI for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
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/".
14 *
15 * Contents:
16 *
17 */
18
19 /*
20 * Include necessary headers...
21 */
22
23 #include "cgi-private.h"
24 #include <errno.h>
25
26
27 /*
28 * Local functions...
29 */
30
31 static void do_printer_op(http_t *http, const char *printer, ipp_op_t op,
32 const char *title);
33 static void show_all_printers(http_t *http, const char *username);
34 static void show_printer(http_t *http, const char *printer);
35
36
37 /*
38 * 'main()' - Main entry for CGI.
39 */
40
41 int /* O - Exit status */
42 main(int argc, /* I - Number of command-line arguments */
43 char *argv[]) /* I - Command-line arguments */
44 {
45 const char *printer; /* Printer name */
46 const char *user; /* Username */
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 */
51 const char *op; /* Operation to perform, if any */
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();
64
65 op = cgiGetVariable("OP");
66
67 /*
68 * Set the web interface section...
69 */
70
71 cgiSetVariable("SECTION", "printers");
72
73 /*
74 * See if we are displaying a printer or all printers...
75 */
76
77 if ((printer = getenv("PATH_INFO")) != NULL)
78 {
79 printer ++;
80
81 if (!*printer)
82 printer = NULL;
83
84 if (printer)
85 cgiSetVariable("PRINTER_NAME", printer);
86 }
87
88 /*
89 * See who is logged in...
90 */
91
92 user = getenv("REMOTE_USER");
93
94 /*
95 * Connect to the HTTP server...
96 */
97
98 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
99
100 /*
101 * Get the default printer...
102 */
103
104 if (!op || !cgiIsPOST())
105 {
106 /*
107 * Get the default destination...
108 */
109
110 request = ippNewRequest(CUPS_GET_DEFAULT);
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 /*
135 * See if we need to show a list of printers or the status of a
136 * single printer...
137 */
138
139 if (!printer)
140 show_all_printers(http, user);
141 else
142 show_printer(http, printer);
143 }
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 }
179 else
180 {
181 /*
182 * Unknown/bad operation...
183 */
184
185 cgiStartHTML(cgiText(_("Printers")));
186 cgiCopyTemplateLang("error-op.tmpl");
187 cgiEndHTML();
188 }
189
190 /*
191 * Close the HTTP server connection...
192 */
193
194 httpClose(http);
195
196 /*
197 * Return with no errors...
198 */
199
200 return (0);
201 }
202
203
204 /*
205 * 'do_printer_op()' - Do a printer operation.
206 */
207
208 static void
209 do_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 */
213 {
214 ipp_t *request; /* IPP request */
215 char uri[HTTP_MAX_URI], /* Printer URI */
216 resource[HTTP_MAX_URI]; /* Path for request */
217
218
219 /*
220 * Build a printer request, which requires the following
221 * attributes:
222 *
223 * attributes-charset
224 * attributes-natural-language
225 * printer-uri
226 */
227
228 request = ippNewRequest(op);
229
230 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
231 "localhost", 0, "/printers/%s", printer);
232 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
233 NULL, uri);
234
235 /*
236 * Do the request and get back a response...
237 */
238
239 snprintf(resource, sizeof(resource), "/printers/%s", printer);
240 ippDelete(cupsDoRequest(http, request, resource));
241
242 if (cupsLastError() == IPP_NOT_AUTHORIZED)
243 {
244 puts("Status: 401\n");
245 exit(0);
246 }
247 else if (cupsLastError() > IPP_OK_CONFLICT)
248 {
249 cgiStartHTML(title);
250 cgiShowIPPError(_("Unable to do maintenance command:"));
251 }
252 else
253 {
254 /*
255 * Redirect successful updates back to the printer page...
256 */
257
258 char url[1024], /* Printer/class URL */
259 refresh[1024]; /* Refresh URL */
260
261
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);
266
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");
279 }
280
281 cgiEndHTML();
282 }
283
284
285 /*
286 * 'show_all_printers()' - Show all printers...
287 */
288
289 static void
290 show_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 */
297 ipp_attribute_t *printer; /* Printer object */
298 int ascending, /* Order of printers (0 = descending) */
299 first, /* First printer to show */
300 count; /* Number of printers */
301 const char *var; /* Form variable */
302 void *search; /* Search data */
303 char val[1024]; /* Form variable */
304
305
306 fprintf(stderr, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
307 http, user ? user : "(null)");
308
309 /*
310 * Show the standard header...
311 */
312
313 cgiStartHTML(cgiText(_("Printers")));
314
315 /*
316 * Build a CUPS_GET_PRINTERS request, which requires the following
317 * attributes:
318 *
319 * attributes-charset
320 * attributes-natural-language
321 * printer-type
322 * printer-type-mask
323 * requesting-user-name
324 */
325
326 request = ippNewRequest(CUPS_GET_PRINTERS);
327
328 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
329 "printer-type", 0);
330 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
331 "printer-type-mask", CUPS_PRINTER_CLASS);
332
333 if (user)
334 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
335 "requesting-user-name", NULL, user);
336
337 cgiGetAttributes(request, "printers.tmpl");
338
339 /*
340 * Do the request and get back a response...
341 */
342
343 if ((response = cupsDoRequest(http, request, "/")) != NULL)
344 {
345 /*
346 * Get a list of matching job objects.
347 */
348
349 if ((var = cgiGetVariable("QUERY")) != NULL &&
350 !cgiGetVariable("CLEAR"))
351 search = cgiCompileSearch(var);
352 else
353 search = NULL;
354
355 printers = cgiGetIPPObjects(response, search);
356 count = cupsArrayCount(printers);
357
358 if (search)
359 cgiFreeSearch(search);
360
361 /*
362 * Figure out which printers to display...
363 */
364
365 if ((var = cgiGetVariable("FIRST")) != NULL)
366 first = atoi(var);
367 else
368 first = 0;
369
370 if (first >= count)
371 first = count - CUPS_PAGE_MAX;
372
373 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
374
375 if (first < 0)
376 first = 0;
377
378 sprintf(val, "%d", count);
379 cgiSetVariable("TOTAL", val);
380
381 if ((var = cgiGetVariable("ORDER")) != NULL)
382 ascending = !strcasecmp(var, "asc");
383 else
384 ascending = 1;
385
386 if (ascending)
387 {
388 for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, first);
389 i < CUPS_PAGE_MAX && printer;
390 i ++, printer = (ipp_attribute_t *)cupsArrayNext(printers))
391 cgiSetIPPObjectVars(printer, NULL, i);
392 }
393 else
394 {
395 for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, count - first - 1);
396 i < CUPS_PAGE_MAX && printer;
397 i ++, printer = (ipp_attribute_t *)cupsArrayPrev(printers))
398 cgiSetIPPObjectVars(printer, NULL, i);
399 }
400
401 /*
402 * Save navigation URLs...
403 */
404
405 cgiSetVariable("THISURL", "/printers/");
406
407 if (first > 0)
408 {
409 sprintf(val, "%d", first - CUPS_PAGE_MAX);
410 cgiSetVariable("PREV", val);
411 }
412
413 if ((first + CUPS_PAGE_MAX) < count)
414 {
415 sprintf(val, "%d", first + CUPS_PAGE_MAX);
416 cgiSetVariable("NEXT", val);
417 }
418
419 /*
420 * Then show everything...
421 */
422
423 cgiCopyTemplateLang("search.tmpl");
424
425 cgiCopyTemplateLang("printers-header.tmpl");
426
427 if (count > CUPS_PAGE_MAX)
428 cgiCopyTemplateLang("pager.tmpl");
429
430 cgiCopyTemplateLang("printers.tmpl");
431
432 if (count > CUPS_PAGE_MAX)
433 cgiCopyTemplateLang("pager.tmpl");
434
435 /*
436 * Delete the response...
437 */
438
439 cupsArrayDelete(printers);
440 ippDelete(response);
441 }
442 else
443 {
444 /*
445 * Show the error...
446 */
447
448 cgiShowIPPError(_("Unable to get printer list:"));
449 }
450
451 cgiEndHTML();
452 }
453
454
455 /*
456 * 'show_printer()' - Show a single printer.
457 */
458
459 static void
460 show_printer(http_t *http, /* I - Connection to server */
461 const char *printer) /* I - Name of printer */
462 {
463 ipp_t *request, /* IPP request */
464 *response; /* IPP response */
465 ipp_attribute_t *attr; /* IPP attribute */
466 char uri[HTTP_MAX_URI]; /* Printer URI */
467 char refresh[1024]; /* Refresh URL */
468
469
470 fprintf(stderr, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
471 http, printer ? printer : "(null)");
472
473 /*
474 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
475 * attributes:
476 *
477 * attributes-charset
478 * attributes-natural-language
479 * printer-uri
480 */
481
482 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
483
484 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
485 "localhost", 0, "/printers/%s", printer);
486 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
487 uri);
488
489 cgiGetAttributes(request, "printer.tmpl");
490
491 /*
492 * Do the request and get back a response...
493 */
494
495 if ((response = cupsDoRequest(http, request, "/")) != NULL)
496 {
497 /*
498 * Got the result; set the CGI variables and check the status of a
499 * single-queue request...
500 */
501
502 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
503
504 if (printer && (attr = ippFindAttribute(response, "printer-state",
505 IPP_TAG_ENUM)) != NULL &&
506 attr->values[0].integer == IPP_PRINTER_PROCESSING)
507 {
508 /*
509 * Printer is processing - automatically refresh the page until we
510 * are done printing...
511 */
512
513 cgiFormEncode(uri, printer, sizeof(uri));
514 snprintf(refresh, sizeof(refresh), "10;URL=/printers/%s", uri);
515 cgiSetVariable("refresh_page", refresh);
516 }
517
518 /*
519 * Delete the response...
520 */
521
522 ippDelete(response);
523
524 /*
525 * Show the standard header...
526 */
527
528 cgiStartHTML(printer);
529
530 /*
531 * Show the printer status...
532 */
533
534 cgiCopyTemplateLang("printer.tmpl");
535
536 /*
537 * Show jobs for the specified printer...
538 */
539
540 cgiCopyTemplateLang("printer-jobs-header.tmpl");
541 cgiShowJobs(http, printer);
542 }
543 else
544 {
545 /*
546 * Show the IPP error...
547 */
548
549 cgiStartHTML(printer);
550 cgiShowIPPError(_("Unable to get printer status:"));
551 }
552
553 cgiEndHTML();
554 }
555
556
557 /*
558 * End of "$Id: printers.c 7940 2008-09-16 00:45:16Z mike $".
559 */