2 * "$Id: printers.c 177 2006-06-21 00:20:03Z jlovell $"
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
)
98 * See who is logged in...
101 user
= getenv("REMOTE_USER");
104 * Connect to the HTTP server...
107 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
110 * Get the default printer...
116 * Get the default destination...
119 request
= ippNewRequest(CUPS_GET_DEFAULT
);
121 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
122 "requested-attributes",
123 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
125 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
127 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
128 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
130 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
132 char url
[HTTP_MAX_URI
]; /* New URL */
135 cgiSetVariable("DEFAULT_URI",
136 cgiRewriteURL(attr
->values
[0].string
.text
,
137 url
, sizeof(url
), NULL
));
144 * See if we need to show a list of printers or the status of a
149 show_all_printers(http
, user
);
151 show_printer(http
, printer
);
153 else if (!strcasecmp(op
, "print-self-test-page") && printer
)
154 print_command(http
, printer
, "PrintSelfTestPage");
155 else if (!strcasecmp(op
, "clean-print-heads") && printer
)
156 print_command(http
, printer
, "Clean all");
157 else if (!strcasecmp(op
, "print-test-page") && printer
)
158 cgiPrintTestPage(http
, printer
);
159 else if (!strcasecmp(op
, "move-jobs") && printer
)
160 cgiMoveJobs(http
, printer
, 0);
164 * Unknown/bad operation...
168 cgiStartHTML(printer
);
170 cgiStartHTML(cgiText(_("Printers")));
172 cgiCopyTemplateLang("error-op.tmpl");
177 * Close the HTTP server connection...
183 * Return with no errors...
191 * 'print_command()' - Send a print command to the printer.
195 print_command(http_t
*http
, /* I - Connection to server */
196 const char *printer
, /* I - Printer */
197 const char *command
) /* I - Command to send */
199 cups_file_t
*fp
; /* File pointer */
200 char filename
[1024]; /* Temporary file */
201 ipp_t
*request
, /* IPP request */
202 *response
; /* IPP response */
203 char uri
[HTTP_MAX_URI
], /* Printer URI */
204 resource
[1024], /* POST resource path */
205 refresh
[1024]; /* Refresh URL */
206 const char *user
; /* Username */
210 * See who is logged in...
213 if ((user
= getenv("REMOTE_USER")) == NULL
)
217 * Create the CUPS command file to print...
220 if ((fp
= cupsTempFile2(filename
, sizeof(filename
))) == NULL
)
222 cgiStartHTML(cgiText(_("Printer Maintenance")));
223 cgiSetVariable("MESSAGE", _("Unable to create temporary file:"));
224 cgiSetVariable("ERROR", strerror(errno
));
225 cgiCopyTemplateLang("error.tmpl");
230 cupsFilePuts(fp
, "#CUPS-COMMAND\n");
231 cupsFilePrintf(fp
, "%s\n", command
);
235 * Point to the printer...
238 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
);
240 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
241 "localhost", ippPort(), "/printers/%s", printer
);
244 * Build an IPP_PRINT_JOB request, which requires the following
248 * attributes-natural-language
250 * requesting-user-name
254 request
= ippNewRequest(IPP_PRINT_JOB
);
256 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
259 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
260 "requesting-user-name", NULL
, user
);
262 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
263 NULL
, "Printer Maintenance");
265 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
266 NULL
, "application/postscript");
269 * Do the request and get back a response...
272 if ((response
= cupsDoFileRequest(http
, request
, resource
,
275 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
282 if (cupsLastError() <= IPP_OK_CONFLICT
)
285 * Automatically reload the printer status page...
288 cgiFormEncode(uri
, resource
, sizeof(uri
));
289 snprintf(refresh
, sizeof(refresh
), "2;URL=%s", uri
);
290 cgiSetVariable("refresh_page", refresh
);
293 cgiStartHTML(cgiText(_("Printer Maintenance")));
295 if (cupsLastError() > IPP_OK_CONFLICT
)
296 cgiShowIPPError(_("Unable to send maintenance job:"));
299 cgiSetVariable("PRINTER_NAME", printer
);
301 cgiCopyTemplateLang("maintenance.tmpl");
309 * 'show_all_printers()' - Show all printers...
313 show_all_printers(http_t
*http
, /* I - Connection to server */
314 const char *user
) /* I - Username */
316 int i
; /* Looping var */
317 ipp_t
*request
, /* IPP request */
318 *response
; /* IPP response */
319 cups_array_t
*printers
; /* Array of printer objects */
320 ipp_attribute_t
*printer
, /* Printer object */
321 *attr
; /* Current attribute */
322 int ascending
, /* Order of printers (0 = descending) */
323 first
, /* First printer to show */
324 count
; /* Number of printers */
325 const char *var
; /* Form variable */
326 void *search
; /* Search data */
327 char url
[1024], /* URL for prev/next/this */
328 *urlptr
, /* Position in URL */
329 *urlend
; /* End of URL */
332 fprintf(stderr
, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
333 http
, user
? user
: "(null)");
336 * Show the standard header...
339 cgiStartHTML(cgiText(_("Printers")));
342 * Build a CUPS_GET_PRINTERS request, which requires the following
346 * attributes-natural-language
349 * requesting-user-name
352 request
= ippNewRequest(CUPS_GET_PRINTERS
);
354 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
356 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
357 "printer-type-mask", CUPS_PRINTER_CLASS
);
360 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
361 "requesting-user-name", NULL
, user
);
363 cgiGetAttributes(request
, "printers.tmpl");
366 * Do the request and get back a response...
369 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
372 * Get a list of matching job objects.
375 if ((var
= cgiGetVariable("QUERY")) != NULL
)
376 search
= cgiCompileSearch(var
);
380 printers
= cgiGetIPPObjects(response
, search
);
381 count
= cupsArrayCount(printers
);
384 cgiFreeSearch(search
);
387 * Figure out which printers to display...
390 if ((var
= cgiGetVariable("FIRST")) != NULL
)
396 first
= count
- CUPS_PAGE_MAX
;
398 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
403 sprintf(url
, "%d", count
);
404 cgiSetVariable("TOTAL", url
);
406 if ((var
= cgiGetVariable("ORDER")) != NULL
)
407 ascending
= !strcasecmp(var
, "asc");
413 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, first
);
414 i
< CUPS_PAGE_MAX
&& printer
;
415 i
++, printer
= (ipp_attribute_t
*)cupsArrayNext(printers
))
417 cgiSetIPPObjectVars(printer
, NULL
, i
);
419 cgiSetArray("cupscommand", i
, "0");
421 for (attr
= printer
; attr
; attr
= attr
->next
)
422 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
424 else if (!strcmp(attr
->name
, "printer-type"))
426 if (attr
->values
[0].integer
& CUPS_PRINTER_COMMANDS
)
427 cgiSetArray("cupscommand", i
, "1");
434 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, count
- first
- 1);
435 i
< CUPS_PAGE_MAX
&& printer
;
436 i
++, printer
= (ipp_attribute_t
*)cupsArrayPrev(printers
))
438 cgiSetIPPObjectVars(printer
, NULL
, i
);
440 cgiSetArray("cupscommand", i
, "0");
442 for (attr
= printer
; attr
; attr
= attr
->next
)
443 if (attr
->group_tag
== IPP_TAG_ZERO
|| !attr
->name
)
445 else if (!strcmp(attr
->name
, "printer-type"))
447 if (attr
->values
[0].integer
& CUPS_PRINTER_COMMANDS
)
448 cgiSetArray("cupscommand", i
, "1");
455 * Save navigation URLs...
458 urlend
= url
+ sizeof(url
);
460 if ((var
= cgiGetVariable("QUERY")) != NULL
)
462 strlcpy(url
, "/printers/?QUERY=", sizeof(url
));
463 urlptr
= url
+ strlen(url
);
465 cgiFormEncode(urlptr
, var
, urlend
- urlptr
);
466 urlptr
+= strlen(urlptr
);
468 strlcpy(urlptr
, "&", urlend
- urlptr
);
469 urlptr
+= strlen(urlptr
);
473 strlcpy(url
, "/printers/?", sizeof(url
));
474 urlptr
= url
+ strlen(url
);
477 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d", first
);
478 cgiSetVariable("THISURL", url
);
482 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
483 first
- CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
484 cgiSetVariable("PREVURL", url
);
487 if ((first
+ CUPS_PAGE_MAX
) < count
)
489 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
490 first
+ CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
491 cgiSetVariable("NEXTURL", url
);
495 * Then show everything...
498 cgiCopyTemplateLang("search.tmpl");
500 cgiCopyTemplateLang("printers-header.tmpl");
503 cgiCopyTemplateLang("pager.tmpl");
505 cgiCopyTemplateLang("printers.tmpl");
508 cgiCopyTemplateLang("pager.tmpl");
511 * Delete the response...
514 cupsArrayDelete(printers
);
523 cgiShowIPPError(_("Unable to get printer list:"));
531 * 'show_printer()' - Show a single printer.
535 show_printer(http_t
*http
, /* I - Connection to server */
536 const char *printer
) /* I - Name of printer */
538 ipp_t
*request
, /* IPP request */
539 *response
; /* IPP response */
540 ipp_attribute_t
*attr
; /* IPP attribute */
541 char uri
[HTTP_MAX_URI
]; /* Printer URI */
542 char refresh
[1024]; /* Refresh URL */
545 fprintf(stderr
, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
546 http
, printer
? printer
: "(null)");
549 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
553 * attributes-natural-language
557 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
559 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
560 "localhost", 0, "/printers/%s", printer
);
561 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
564 cgiGetAttributes(request
, "printers.tmpl");
567 * Do the request and get back a response...
570 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
573 * Got the result; set the CGI variables and check the status of a
574 * single-queue request...
577 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
579 if ((attr
= ippFindAttribute(response
, "printer-type",
580 IPP_TAG_ENUM
)) != NULL
)
582 cgiSetVariable("cupscommand",
583 (attr
->values
[0].integer
& CUPS_PRINTER_COMMANDS
) ?
587 if (printer
&& (attr
= ippFindAttribute(response
, "printer-state",
588 IPP_TAG_ENUM
)) != NULL
&&
589 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
592 * Printer is processing - automatically refresh the page until we
593 * are done printing...
596 cgiFormEncode(uri
, printer
, sizeof(uri
));
597 snprintf(refresh
, sizeof(refresh
), "10;URL=/printers/%s", uri
);
598 cgiSetVariable("refresh_page", refresh
);
602 * Delete the response...
608 * Show the standard header...
611 cgiStartHTML(printer
);
614 * Show the printer status...
617 cgiCopyTemplateLang("printers.tmpl");
620 * Show jobs for the specified printer...
623 cgiCopyTemplateLang("printer-jobs-header.tmpl");
624 cgiShowJobs(http
, printer
);
629 * Show the IPP error...
632 cgiStartHTML(printer
);
633 cgiShowIPPError(_("Unable to get printer status:"));
641 * End of "$Id: printers.c 177 2006-06-21 00:20:03Z jlovell $".