2 * "$Id: printers.c 5023 2006-01-29 14:39:44Z 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 * show_all_printers() - Show all printers...
28 * show_printer() - Show a single printer.
32 * Include necessary headers...
35 #include "cgi-private.h"
42 void show_all_printers(http_t
*http
, const char *username
);
43 void show_printer(http_t
*http
, const char *printer
);
47 * 'main()' - Main entry for CGI.
50 int /* O - Exit status */
51 main(int argc
, /* I - Number of command-line arguments */
52 char *argv
[]) /* I - Command-line arguments */
54 const char *printer
; /* Printer name */
55 const char *user
; /* Username */
56 http_t
*http
; /* Connection to the server */
57 ipp_t
*request
, /* IPP request */
58 *response
; /* IPP response */
59 ipp_attribute_t
*attr
; /* IPP attribute */
60 const char *op
; /* Operation to perform, if any */
61 static const char *def_attrs
[] = /* Attributes for default printer */
64 "printer-uri-supported"
69 * Get any form variables...
74 op
= cgiGetVariable("OP");
77 * Set the web interface section...
80 cgiSetVariable("SECTION", "printers");
83 * See if we are displaying a printer or all printers...
86 if (!strcmp(argv
[0], "/") || strstr(argv
[0], "printers.cgi"))
92 * See who is logged in...
95 if ((user
= getenv("REMOTE_USER")) == NULL
)
99 * Connect to the HTTP server...
102 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
105 * Get the default printer...
111 * Get the default destination...
114 request
= ippNewRequest(CUPS_GET_DEFAULT
);
116 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
117 "requested-attributes",
118 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
120 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
122 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
123 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
125 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
127 char url
[HTTP_MAX_URI
]; /* New URL */
130 cgiSetVariable("DEFAULT_URI",
131 cgiRewriteURL(attr
->values
[0].string
.text
,
132 url
, sizeof(url
), NULL
));
139 * See if we need to show a list of printers or the status of a
144 show_all_printers(http
, user
);
146 show_printer(http
, printer
);
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);
155 * Unknown/bad operation...
159 cgiStartHTML(printer
);
161 cgiStartHTML(cgiText(_("Printers")));
163 cgiCopyTemplateLang("error-op.tmpl");
168 * Close the HTTP server connection...
174 * Return with no errors...
182 * 'show_all_printers()' - Show all printers...
186 show_all_printers(http_t
*http
, /* I - Connection to server */
187 const char *user
) /* I - Username */
189 int i
; /* Looping var */
190 ipp_t
*request
, /* IPP request */
191 *response
; /* IPP response */
192 cups_array_t
*printers
; /* Array of printer objects */
193 ipp_attribute_t
*printer
; /* Printer object */
194 int ascending
, /* Order of printers (0 = descending) */
195 first
, /* First printer to show */
196 count
; /* Number of printers */
197 const char *var
; /* Form variable */
198 void *search
; /* Search data */
199 char url
[1024], /* URL for prev/next/this */
200 *urlptr
, /* Position in URL */
201 *urlend
; /* End of URL */
205 * Show the standard header...
208 cgiStartHTML(cgiText(_("Printers")));
211 * Build a CUPS_GET_PRINTERS request, which requires the following
215 * attributes-natural-language
218 * requesting-user-name
221 request
= ippNewRequest(CUPS_GET_PRINTERS
);
223 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
225 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
226 "printer-type-mask", CUPS_PRINTER_CLASS
);
228 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
229 "requesting-user-name", NULL
, user
);
231 cgiGetAttributes(request
, "printers.tmpl");
234 * Do the request and get back a response...
237 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
240 * Get a list of matching job objects.
243 if ((var
= cgiGetVariable("QUERY")) != NULL
)
244 search
= cgiCompileSearch(var
);
248 printers
= cgiGetIPPObjects(response
, search
);
249 count
= cupsArrayCount(printers
);
252 cgiFreeSearch(search
);
255 * Figure out which printers to display...
258 if ((var
= cgiGetVariable("FIRST")) != NULL
)
264 first
= count
- CUPS_PAGE_MAX
;
266 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
271 sprintf(url
, "%d", count
);
272 cgiSetVariable("TOTAL", url
);
274 if ((var
= cgiGetVariable("ORDER")) != NULL
)
275 ascending
= !strcasecmp(var
, "asc");
281 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, first
);
282 i
< CUPS_PAGE_MAX
&& printer
;
283 i
++, printer
= (ipp_attribute_t
*)cupsArrayNext(printers
))
284 cgiSetIPPObjectVars(printer
, NULL
, i
);
288 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, count
- first
- 1);
289 i
< CUPS_PAGE_MAX
&& printer
;
290 i
++, printer
= (ipp_attribute_t
*)cupsArrayPrev(printers
))
291 cgiSetIPPObjectVars(printer
, NULL
, i
);
295 * Save navigation URLs...
298 urlend
= url
+ sizeof(url
);
300 if ((var
= cgiGetVariable("QUERY")) != NULL
)
302 strlcpy(url
, "/printers/?QUERY=", sizeof(url
));
303 urlptr
= url
+ strlen(url
);
305 cgiFormEncode(urlptr
, var
, urlend
- urlptr
);
306 urlptr
+= strlen(urlptr
);
308 strlcpy(urlptr
, "&", urlend
- urlptr
);
309 urlptr
+= strlen(urlptr
);
313 strlcpy(url
, "/printers/?", sizeof(url
));
314 urlptr
= url
+ strlen(url
);
317 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d", first
);
318 cgiSetVariable("THISURL", url
);
322 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
323 first
- CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
324 cgiSetVariable("PREVURL", url
);
327 if ((first
+ CUPS_PAGE_MAX
) < count
)
329 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
330 first
+ CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
331 cgiSetVariable("NEXTURL", url
);
335 * Then show everything...
338 cgiCopyTemplateLang("search.tmpl");
340 cgiCopyTemplateLang("printers-header.tmpl");
343 cgiCopyTemplateLang("pager.tmpl");
345 cgiCopyTemplateLang("printers.tmpl");
348 cgiCopyTemplateLang("pager.tmpl");
351 * Delete the response...
362 cgiShowIPPError(_("Unable to get printer list:"));
370 * 'show_printer()' - Show a single printer.
374 show_printer(http_t
*http
, /* I - Connection to server */
375 const char *printer
) /* I - Name of printer */
377 ipp_t
*request
, /* IPP request */
378 *response
; /* IPP response */
379 ipp_attribute_t
*attr
; /* IPP attribute */
380 char uri
[HTTP_MAX_URI
]; /* Printer URI */
381 char refresh
[1024]; /* Refresh URL */
385 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
389 * attributes-natural-language
393 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
395 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
396 "localhost", 0, "/printers/%s", printer
);
397 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
400 cgiGetAttributes(request
, "printers.tmpl");
403 * Do the request and get back a response...
406 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
409 * Got the result; set the CGI variables and check the status of a
410 * single-queue request...
413 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
415 if (printer
&& (attr
= ippFindAttribute(response
, "printer-state",
416 IPP_TAG_ENUM
)) != NULL
&&
417 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
420 * Printer is processing - automatically refresh the page until we
421 * are done printing...
424 cgiFormEncode(uri
, printer
, sizeof(uri
));
425 snprintf(refresh
, sizeof(refresh
), "10;/printers/%s", uri
);
426 cgiSetVariable("refresh_page", refresh
);
430 * Delete the response...
436 * Show the standard header...
439 cgiStartHTML(printer
);
442 * Show the printer status...
445 cgiCopyTemplateLang("printers.tmpl");
448 * Show jobs for the specified printer...
451 cgiCopyTemplateLang("printer-jobs-header.tmpl");
452 cgiShowJobs(http
, printer
);
457 * Show the IPP error...
460 cgiStartHTML(printer
);
461 cgiShowIPPError(_("Unable to get printer status:"));
469 * End of "$Id: printers.c 5023 2006-01-29 14:39:44Z mike $".