2 * "$Id: printers.c 7940 2008-09-16 00:45:16Z mike $"
4 * Printer status CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
20 * Include necessary headers...
23 #include "cgi-private.h"
31 static void do_printer_op(http_t
*http
, const char *printer
, ipp_op_t op
,
33 static void show_all_printers(http_t
*http
, const char *username
);
34 static void show_printer(http_t
*http
, const char *printer
);
38 * 'main()' - Main entry for CGI.
41 int /* O - Exit status */
42 main(int argc
, /* I - Number of command-line arguments */
43 char *argv
[]) /* I - Command-line arguments */
45 const char *printer
; /* Printer name */
46 const char *user
; /* Username */
47 http_t
*http
; /* Connection to the server */
48 ipp_t
*request
, /* IPP request */
49 *response
; /* IPP response */
50 ipp_attribute_t
*attr
; /* IPP attribute */
51 const char *op
; /* Operation to perform, if any */
52 static const char *def_attrs
[] = /* Attributes for default printer */
55 "printer-uri-supported"
60 * Get any form variables...
65 op
= cgiGetVariable("OP");
68 * Set the web interface section...
71 cgiSetVariable("SECTION", "printers");
74 * See if we are displaying a printer or all printers...
77 if ((printer
= getenv("PATH_INFO")) != NULL
)
85 cgiSetVariable("PRINTER_NAME", printer
);
89 * See who is logged in...
92 user
= getenv("REMOTE_USER");
95 * Connect to the HTTP server...
98 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
101 * Get the default printer...
104 if (!op
|| !cgiIsPOST())
107 * Get the default destination...
110 request
= ippNewRequest(CUPS_GET_DEFAULT
);
112 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
113 "requested-attributes",
114 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
116 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
118 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
119 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
121 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
123 char url
[HTTP_MAX_URI
]; /* New URL */
126 cgiSetVariable("DEFAULT_URI",
127 cgiRewriteURL(attr
->values
[0].string
.text
,
128 url
, sizeof(url
), NULL
));
135 * See if we need to show a list of printers or the status of a
140 show_all_printers(http
, user
);
142 show_printer(http
, printer
);
146 if (!strcmp(op
, "start-printer"))
147 do_printer_op(http
, printer
, IPP_RESUME_PRINTER
,
148 cgiText(_("Resume Printer")));
149 else if (!strcmp(op
, "stop-printer"))
150 do_printer_op(http
, printer
, IPP_PAUSE_PRINTER
,
151 cgiText(_("Pause Printer")));
152 else if (!strcmp(op
, "accept-jobs"))
153 do_printer_op(http
, printer
, CUPS_ACCEPT_JOBS
, cgiText(_("Accept Jobs")));
154 else if (!strcmp(op
, "reject-jobs"))
155 do_printer_op(http
, printer
, CUPS_REJECT_JOBS
, cgiText(_("Reject Jobs")));
156 else if (!strcmp(op
, "purge-jobs"))
157 do_printer_op(http
, printer
, IPP_PURGE_JOBS
, cgiText(_("Purge Jobs")));
158 else if (!strcasecmp(op
, "print-self-test-page"))
159 cgiPrintCommand(http
, printer
, "PrintSelfTestPage",
160 cgiText(_("Print Self-Test Page")));
161 else if (!strcasecmp(op
, "clean-print-heads"))
162 cgiPrintCommand(http
, printer
, "Clean all",
163 cgiText(_("Clean Print Heads")));
164 else if (!strcasecmp(op
, "print-test-page"))
165 cgiPrintTestPage(http
, printer
);
166 else if (!strcasecmp(op
, "move-jobs"))
167 cgiMoveJobs(http
, printer
, 0);
171 * Unknown/bad operation...
174 cgiStartHTML(printer
);
175 cgiCopyTemplateLang("error-op.tmpl");
182 * Unknown/bad operation...
185 cgiStartHTML(cgiText(_("Printers")));
186 cgiCopyTemplateLang("error-op.tmpl");
191 * Close the HTTP server connection...
197 * Return with no errors...
205 * 'do_printer_op()' - Do a printer operation.
209 do_printer_op(http_t
*http
, /* I - HTTP connection */
210 const char *printer
, /* I - Printer name */
211 ipp_op_t op
, /* I - Operation to perform */
212 const char *title
) /* I - Title of page */
214 ipp_t
*request
; /* IPP request */
215 char uri
[HTTP_MAX_URI
], /* Printer URI */
216 resource
[HTTP_MAX_URI
]; /* Path for request */
220 * Build a printer request, which requires the following
224 * attributes-natural-language
228 request
= ippNewRequest(op
);
230 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
231 "localhost", 0, "/printers/%s", printer
);
232 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
236 * Do the request and get back a response...
239 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
);
240 ippDelete(cupsDoRequest(http
, request
, resource
));
242 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
244 puts("Status: 401\n");
247 else if (cupsLastError() > IPP_OK_CONFLICT
)
250 cgiShowIPPError(_("Unable to do maintenance command:"));
255 * Redirect successful updates back to the printer page...
258 char url
[1024], /* Printer/class URL */
259 refresh
[1024]; /* Refresh URL */
262 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
263 cgiFormEncode(uri
, url
, sizeof(uri
));
264 snprintf(refresh
, sizeof(refresh
), "5;URL=q%s", uri
);
265 cgiSetVariable("refresh_page", refresh
);
269 if (op
== IPP_PAUSE_PRINTER
)
270 cgiCopyTemplateLang("printer-stop.tmpl");
271 else if (op
== IPP_RESUME_PRINTER
)
272 cgiCopyTemplateLang("printer-start.tmpl");
273 else if (op
== CUPS_ACCEPT_JOBS
)
274 cgiCopyTemplateLang("printer-accept.tmpl");
275 else if (op
== CUPS_REJECT_JOBS
)
276 cgiCopyTemplateLang("printer-reject.tmpl");
277 else if (op
== IPP_PURGE_JOBS
)
278 cgiCopyTemplateLang("printer-purge.tmpl");
286 * 'show_all_printers()' - Show all printers...
290 show_all_printers(http_t
*http
, /* I - Connection to server */
291 const char *user
) /* I - Username */
293 int i
; /* Looping var */
294 ipp_t
*request
, /* IPP request */
295 *response
; /* IPP response */
296 cups_array_t
*printers
; /* Array of printer objects */
297 ipp_attribute_t
*printer
; /* Printer object */
298 int ascending
, /* Order of printers (0 = descending) */
299 first
, /* First printer to show */
300 count
; /* Number of printers */
301 const char *var
; /* Form variable */
302 void *search
; /* Search data */
303 char val
[1024]; /* Form variable */
306 fprintf(stderr
, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
307 http
, user
? user
: "(null)");
310 * Show the standard header...
313 cgiStartHTML(cgiText(_("Printers")));
316 * Build a CUPS_GET_PRINTERS request, which requires the following
320 * attributes-natural-language
323 * requesting-user-name
326 request
= ippNewRequest(CUPS_GET_PRINTERS
);
328 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
330 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
331 "printer-type-mask", CUPS_PRINTER_CLASS
);
334 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
335 "requesting-user-name", NULL
, user
);
337 cgiGetAttributes(request
, "printers.tmpl");
340 * Do the request and get back a response...
343 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
346 * Get a list of matching job objects.
349 if ((var
= cgiGetVariable("QUERY")) != NULL
&&
350 !cgiGetVariable("CLEAR"))
351 search
= cgiCompileSearch(var
);
355 printers
= cgiGetIPPObjects(response
, search
);
356 count
= cupsArrayCount(printers
);
359 cgiFreeSearch(search
);
362 * Figure out which printers to display...
365 if ((var
= cgiGetVariable("FIRST")) != NULL
)
371 first
= count
- CUPS_PAGE_MAX
;
373 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
378 sprintf(val
, "%d", count
);
379 cgiSetVariable("TOTAL", val
);
381 if ((var
= cgiGetVariable("ORDER")) != NULL
)
382 ascending
= !strcasecmp(var
, "asc");
388 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, first
);
389 i
< CUPS_PAGE_MAX
&& printer
;
390 i
++, printer
= (ipp_attribute_t
*)cupsArrayNext(printers
))
391 cgiSetIPPObjectVars(printer
, NULL
, i
);
395 for (i
= 0, printer
= (ipp_attribute_t
*)cupsArrayIndex(printers
, count
- first
- 1);
396 i
< CUPS_PAGE_MAX
&& printer
;
397 i
++, printer
= (ipp_attribute_t
*)cupsArrayPrev(printers
))
398 cgiSetIPPObjectVars(printer
, NULL
, i
);
402 * Save navigation URLs...
405 cgiSetVariable("THISURL", "/printers/");
409 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
410 cgiSetVariable("PREV", val
);
413 if ((first
+ CUPS_PAGE_MAX
) < count
)
415 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
416 cgiSetVariable("NEXT", val
);
420 * Then show everything...
423 cgiCopyTemplateLang("search.tmpl");
425 cgiCopyTemplateLang("printers-header.tmpl");
427 if (count
> CUPS_PAGE_MAX
)
428 cgiCopyTemplateLang("pager.tmpl");
430 cgiCopyTemplateLang("printers.tmpl");
432 if (count
> CUPS_PAGE_MAX
)
433 cgiCopyTemplateLang("pager.tmpl");
436 * Delete the response...
439 cupsArrayDelete(printers
);
448 cgiShowIPPError(_("Unable to get printer list:"));
456 * 'show_printer()' - Show a single printer.
460 show_printer(http_t
*http
, /* I - Connection to server */
461 const char *printer
) /* I - Name of printer */
463 ipp_t
*request
, /* IPP request */
464 *response
; /* IPP response */
465 ipp_attribute_t
*attr
; /* IPP attribute */
466 char uri
[HTTP_MAX_URI
]; /* Printer URI */
467 char refresh
[1024]; /* Refresh URL */
470 fprintf(stderr
, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
471 http
, printer
? printer
: "(null)");
474 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
478 * attributes-natural-language
482 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
484 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
485 "localhost", 0, "/printers/%s", printer
);
486 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
489 cgiGetAttributes(request
, "printer.tmpl");
492 * Do the request and get back a response...
495 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
498 * Got the result; set the CGI variables and check the status of a
499 * single-queue request...
502 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
504 if (printer
&& (attr
= ippFindAttribute(response
, "printer-state",
505 IPP_TAG_ENUM
)) != NULL
&&
506 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
509 * Printer is processing - automatically refresh the page until we
510 * are done printing...
513 cgiFormEncode(uri
, printer
, sizeof(uri
));
514 snprintf(refresh
, sizeof(refresh
), "10;URL=/printers/%s", uri
);
515 cgiSetVariable("refresh_page", refresh
);
519 * Delete the response...
525 * Show the standard header...
528 cgiStartHTML(printer
);
531 * Show the printer status...
534 cgiCopyTemplateLang("printer.tmpl");
537 * Show jobs for the specified printer...
540 cgiCopyTemplateLang("printer-jobs-header.tmpl");
541 cgiShowJobs(http
, printer
);
546 * Show the IPP error...
549 cgiStartHTML(printer
);
550 cgiShowIPPError(_("Unable to get printer status:"));
558 * End of "$Id: printers.c 7940 2008-09-16 00:45:16Z mike $".