2 * "$Id: printers.c 5160 2006-02-24 01:14:18Z mike $"
4 * Printer status CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
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.
33 * Include necessary headers...
36 #include "cgi-private.h"
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
);
50 * 'main()' - Main entry for CGI.
53 int /* O - Exit status */
54 main(int argc
, /* I - Number of command-line arguments */
55 char *argv
[]) /* I - Command-line arguments */
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 */
67 "printer-uri-supported"
72 * Get any form variables...
77 op
= cgiGetVariable("OP");
80 * Set the web interface section...
83 cgiSetVariable("SECTION", "printers");
86 * See if we are displaying a printer or all printers...
89 if ((printer
= getenv("PATH_INFO")) != NULL
)
93 * See who is logged in...
96 if ((user
= getenv("REMOTE_USER")) == NULL
)
100 * Connect to the HTTP server...
103 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
106 * Get the default printer...
112 * Get the default destination...
115 request
= ippNewRequest(CUPS_GET_DEFAULT
);
117 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
118 "requested-attributes",
119 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
121 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
123 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
124 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
126 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
128 char url
[HTTP_MAX_URI
]; /* New URL */
131 cgiSetVariable("DEFAULT_URI",
132 cgiRewriteURL(attr
->values
[0].string
.text
,
133 url
, sizeof(url
), NULL
));
140 * See if we need to show a list of printers or the status of a
145 show_all_printers(http
, user
);
147 show_printer(http
, printer
);
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);
160 * Unknown/bad operation...
164 cgiStartHTML(printer
);
166 cgiStartHTML(cgiText(_("Printers")));
168 cgiCopyTemplateLang("error-op.tmpl");
173 * Close the HTTP server connection...
179 * Return with no errors...
187 * 'print_command()' - Send a print command to the printer.
191 print_command(http_t
*http
, /* I - Connection to server */
192 const char *printer
, /* I - Printer */
193 const char *command
) /* I - Command to send */
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 */
206 * See who is logged in...
209 if ((user
= getenv("REMOTE_USER")) == NULL
)
213 * Create the CUPS command file to print...
216 if ((fp
= cupsTempFile2(filename
, sizeof(filename
))) == NULL
)
218 cgiStartHTML(cgiText(_("Printer Maintenance")));
219 cgiSetVariable("MESSAGE", _("Unable to create temporary file:"));
220 cgiSetVariable("ERROR", strerror(errno
));
221 cgiCopyTemplateLang("error.tmpl");
226 cupsFilePuts(fp
, "#CUPS-COMMAND\n");
227 cupsFilePrintf(fp
, "%s\n", command
);
231 * Point to the printer...
234 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
);
236 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
237 "localhost", ippPort(), "/printers/%s", printer
);
240 * Build an IPP_PRINT_JOB request, which requires the following
244 * attributes-natural-language
246 * requesting-user-name
250 request
= ippNewRequest(IPP_PRINT_JOB
);
252 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
255 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
256 "requesting-user-name", NULL
, user
);
258 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
259 NULL
, "Printer Maintenance");
261 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
262 NULL
, "application/postscript");
265 * Do the request and get back a response...
268 if ((response
= cupsDoFileRequest(http
, request
, resource
,
271 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
278 if (cupsLastError() <= IPP_OK_CONFLICT
)
281 * Automatically reload the printer status page...
284 cgiFormEncode(uri
, resource
, sizeof(uri
));
285 snprintf(refresh
, sizeof(refresh
), "2;%s", uri
);
286 cgiSetVariable("refresh_page", refresh
);
289 cgiStartHTML(cgiText(_("Printer Maintenance")));
291 if (cupsLastError() > IPP_OK_CONFLICT
)
292 cgiShowIPPError(_("Unable to send maintenance job:"));
295 cgiSetVariable("PRINTER_NAME", printer
);
297 cgiCopyTemplateLang("maintenance.tmpl");
305 * 'show_all_printers()' - Show all printers...
309 show_all_printers(http_t
*http
, /* I - Connection to server */
310 const char *user
) /* I - Username */
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 */
328 fprintf(stderr
, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
332 * Show the standard header...
335 cgiStartHTML(cgiText(_("Printers")));
338 * Build a CUPS_GET_PRINTERS request, which requires the following
342 * attributes-natural-language
345 * requesting-user-name
348 request
= ippNewRequest(CUPS_GET_PRINTERS
);
350 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
352 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
353 "printer-type-mask", CUPS_PRINTER_CLASS
);
355 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
356 "requesting-user-name", NULL
, user
);
358 cgiGetAttributes(request
, "printers.tmpl");
361 * Do the request and get back a response...
364 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
367 * Get a list of matching job objects.
370 if ((var
= cgiGetVariable("QUERY")) != NULL
)
371 search
= cgiCompileSearch(var
);
375 printers
= cgiGetIPPObjects(response
, search
);
376 count
= cupsArrayCount(printers
);
379 cgiFreeSearch(search
);
382 * Figure out which printers to display...
385 if ((var
= cgiGetVariable("FIRST")) != NULL
)
391 first
= count
- CUPS_PAGE_MAX
;
393 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
398 sprintf(url
, "%d", count
);
399 cgiSetVariable("TOTAL", url
);
401 if ((var
= cgiGetVariable("ORDER")) != NULL
)
402 ascending
= !strcasecmp(var
, "asc");
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
))
412 cgiSetIPPObjectVars(printer
, NULL
, i
);
414 cgiSetArray("cupscommand", i
, "0");
416 for (attr
= printer
; attr
; attr
= attr
->next
)
417 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
419 else if (!strcmp(attr
->name
, "printer-type"))
421 if (attr
->values
[0].integer
& CUPS_PRINTER_COMMANDS
)
422 cgiSetArray("cupscommand", i
, "1");
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
))
433 cgiSetIPPObjectVars(printer
, NULL
, i
);
435 cgiSetArray("cupscommand", i
, "0");
437 for (attr
= printer
; attr
; attr
= attr
->next
)
438 if (attr
->group_tag
== IPP_TAG_ZERO
|| !attr
->name
)
440 else if (!strcmp(attr
->name
, "printer-type"))
442 if (attr
->values
[0].integer
& CUPS_PRINTER_COMMANDS
)
443 cgiSetArray("cupscommand", i
, "1");
450 * Save navigation URLs...
453 urlend
= url
+ sizeof(url
);
455 if ((var
= cgiGetVariable("QUERY")) != NULL
)
457 strlcpy(url
, "/printers/?QUERY=", sizeof(url
));
458 urlptr
= url
+ strlen(url
);
460 cgiFormEncode(urlptr
, var
, urlend
- urlptr
);
461 urlptr
+= strlen(urlptr
);
463 strlcpy(urlptr
, "&", urlend
- urlptr
);
464 urlptr
+= strlen(urlptr
);
468 strlcpy(url
, "/printers/?", sizeof(url
));
469 urlptr
= url
+ strlen(url
);
472 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d", first
);
473 cgiSetVariable("THISURL", url
);
477 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
478 first
- CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
479 cgiSetVariable("PREVURL", url
);
482 if ((first
+ CUPS_PAGE_MAX
) < count
)
484 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
485 first
+ CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
486 cgiSetVariable("NEXTURL", url
);
490 * Then show everything...
493 cgiCopyTemplateLang("search.tmpl");
495 cgiCopyTemplateLang("printers-header.tmpl");
498 cgiCopyTemplateLang("pager.tmpl");
500 cgiCopyTemplateLang("printers.tmpl");
503 cgiCopyTemplateLang("pager.tmpl");
506 * Delete the response...
509 cupsArrayDelete(printers
);
518 cgiShowIPPError(_("Unable to get printer list:"));
526 * 'show_printer()' - Show a single printer.
530 show_printer(http_t
*http
, /* I - Connection to server */
531 const char *printer
) /* I - Name of printer */
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 */
540 fprintf(stderr
, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
544 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
548 * attributes-natural-language
552 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
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
,
559 cgiGetAttributes(request
, "printers.tmpl");
562 * Do the request and get back a response...
565 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
568 * Got the result; set the CGI variables and check the status of a
569 * single-queue request...
572 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
574 if ((attr
= ippFindAttribute(response
, "printer-type",
575 IPP_TAG_ENUM
)) != NULL
)
577 cgiSetVariable("cupscommand",
578 (attr
->values
[0].integer
& CUPS_PRINTER_COMMANDS
) ?
582 if (printer
&& (attr
= ippFindAttribute(response
, "printer-state",
583 IPP_TAG_ENUM
)) != NULL
&&
584 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
587 * Printer is processing - automatically refresh the page until we
588 * are done printing...
591 cgiFormEncode(uri
, printer
, sizeof(uri
));
592 snprintf(refresh
, sizeof(refresh
), "10;/printers/%s", uri
);
593 cgiSetVariable("refresh_page", refresh
);
597 * Delete the response...
603 * Show the standard header...
606 cgiStartHTML(printer
);
609 * Show the printer status...
612 cgiCopyTemplateLang("printers.tmpl");
615 * Show jobs for the specified printer...
618 cgiCopyTemplateLang("printer-jobs-header.tmpl");
619 cgiShowJobs(http
, printer
);
624 * Show the IPP error...
627 cgiStartHTML(printer
);
628 cgiShowIPPError(_("Unable to get printer status:"));
636 * End of "$Id: printers.c 5160 2006-02-24 01:14:18Z mike $".