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