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