]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/printers.c
Load cups into easysw/current.
[thirdparty/cups.git] / cgi-bin / printers.c
CommitLineData
ef416fc2 1/*
bd7854cb 2 * "$Id: printers.c 5104 2006-02-15 03:21:04Z mike $"
ef416fc2 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 *
fa73b229 26 * main() - Main entry for CGI.
27 * show_all_printers() - Show all printers...
28 * show_printer() - Show a single printer.
ef416fc2 29 */
30
31/*
32 * Include necessary headers...
33 */
34
35#include "cgi-private.h"
36
37
fa73b229 38/*
39 * Local functions...
40 */
41
42void show_all_printers(http_t *http, const char *username);
43void show_printer(http_t *http, const char *printer);
44
45
ef416fc2 46/*
47 * 'main()' - Main entry for CGI.
48 */
49
50int /* O - Exit status */
51main(int argc, /* I - Number of command-line arguments */
52 char *argv[]) /* I - Command-line arguments */
53{
fa73b229 54 const char *printer; /* Printer name */
55 const char *user; /* Username */
ef416fc2 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 */
ef416fc2 60 const char *op; /* Operation to perform, if any */
ef416fc2 61 static const char *def_attrs[] = /* Attributes for default printer */
62 {
63 "printer-name",
64 "printer-uri-supported"
65 };
66
67
68 /*
69 * Get any form variables...
70 */
71
72 cgiInitialize();
ef416fc2 73
fa73b229 74 op = cgiGetVariable("OP");
ef416fc2 75
76 /*
77 * Set the web interface section...
78 */
79
80 cgiSetVariable("SECTION", "printers");
81
82 /*
fa73b229 83 * See if we are displaying a printer or all printers...
ef416fc2 84 */
85
fa73b229 86 if (!strcmp(argv[0], "/") || strstr(argv[0], "printers.cgi"))
87 printer = NULL;
88 else
89 printer = argv[0];
ef416fc2 90
91 /*
fa73b229 92 * See who is logged in...
ef416fc2 93 */
94
fa73b229 95 if ((user = getenv("REMOTE_USER")) == NULL)
96 user = "guest";
ef416fc2 97
98 /*
fa73b229 99 * Connect to the HTTP server...
ef416fc2 100 */
101
fa73b229 102 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
ef416fc2 103
fa73b229 104 /*
105 * Get the default printer...
106 */
ef416fc2 107
fa73b229 108 if (!op)
ef416fc2 109 {
110 /*
111 * Get the default destination...
112 */
113
fa73b229 114 request = ippNewRequest(CUPS_GET_DEFAULT);
ef416fc2 115
116 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
117 "requested-attributes",
118 sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
119
120 if ((response = cupsDoRequest(http, request, "/")) != NULL)
121 {
122 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
123 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
124
125 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
126 {
127 char url[HTTP_MAX_URI]; /* New URL */
128
129
130 cgiSetVariable("DEFAULT_URI",
131 cgiRewriteURL(attr->values[0].string.text,
132 url, sizeof(url), NULL));
133 }
134
135 ippDelete(response);
136 }
137
138 /*
fa73b229 139 * See if we need to show a list of printers or the status of a
140 * single printer...
ef416fc2 141 */
142
fa73b229 143 if (!printer)
144 show_all_printers(http, user);
145 else
146 show_printer(http, printer);
147 }
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);
152 else
153 {
154 /*
155 * Unknown/bad operation...
156 */
ef416fc2 157
fa73b229 158 if (printer)
159 cgiStartHTML(printer);
160 else
161 cgiStartHTML(cgiText(_("Printers")));
ef416fc2 162
fa73b229 163 cgiCopyTemplateLang("error-op.tmpl");
164 cgiEndHTML();
165 }
ef416fc2 166
fa73b229 167 /*
168 * Close the HTTP server connection...
169 */
170
171 httpClose(http);
172
173 /*
174 * Return with no errors...
175 */
ef416fc2 176
fa73b229 177 return (0);
178}
ef416fc2 179
ef416fc2 180
fa73b229 181/*
182 * 'show_all_printers()' - Show all printers...
183 */
ef416fc2 184
fa73b229 185void
186show_all_printers(http_t *http, /* I - Connection to server */
187 const char *user) /* I - Username */
188{
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 */
ef416fc2 202
ef416fc2 203
bd7854cb 204 fprintf(stderr, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
205 http, user);
206
fa73b229 207 /*
208 * Show the standard header...
209 */
ef416fc2 210
fa73b229 211 cgiStartHTML(cgiText(_("Printers")));
ef416fc2 212
fa73b229 213 /*
214 * Build a CUPS_GET_PRINTERS request, which requires the following
215 * attributes:
216 *
217 * attributes-charset
218 * attributes-natural-language
219 * printer-type
220 * printer-type-mask
221 * requesting-user-name
222 */
ef416fc2 223
fa73b229 224 request = ippNewRequest(CUPS_GET_PRINTERS);
ef416fc2 225
fa73b229 226 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
227 "printer-type", 0);
228 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
229 "printer-type-mask", CUPS_PRINTER_CLASS);
ef416fc2 230
fa73b229 231 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
232 "requesting-user-name", NULL, user);
ef416fc2 233
fa73b229 234 cgiGetAttributes(request, "printers.tmpl");
ef416fc2 235
fa73b229 236 /*
237 * Do the request and get back a response...
238 */
239
240 if ((response = cupsDoRequest(http, request, "/")) != NULL)
241 {
ef416fc2 242 /*
fa73b229 243 * Get a list of matching job objects.
ef416fc2 244 */
245
fa73b229 246 if ((var = cgiGetVariable("QUERY")) != NULL)
247 search = cgiCompileSearch(var);
248 else
249 search = NULL;
ef416fc2 250
fa73b229 251 printers = cgiGetIPPObjects(response, search);
252 count = cupsArrayCount(printers);
ef416fc2 253
fa73b229 254 if (search)
255 cgiFreeSearch(search);
ef416fc2 256
257 /*
fa73b229 258 * Figure out which printers to display...
ef416fc2 259 */
260
fa73b229 261 if ((var = cgiGetVariable("FIRST")) != NULL)
262 first = atoi(var);
263 else
264 first = 0;
ef416fc2 265
fa73b229 266 if (first >= count)
267 first = count - CUPS_PAGE_MAX;
ef416fc2 268
fa73b229 269 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
ef416fc2 270
fa73b229 271 if (first < 0)
272 first = 0;
ef416fc2 273
fa73b229 274 sprintf(url, "%d", count);
275 cgiSetVariable("TOTAL", url);
ef416fc2 276
fa73b229 277 if ((var = cgiGetVariable("ORDER")) != NULL)
278 ascending = !strcasecmp(var, "asc");
279 else
280 ascending = 1;
ef416fc2 281
fa73b229 282 if (ascending)
283 {
284 for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, first);
285 i < CUPS_PAGE_MAX && printer;
286 i ++, printer = (ipp_attribute_t *)cupsArrayNext(printers))
287 cgiSetIPPObjectVars(printer, NULL, i);
288 }
289 else
290 {
291 for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, count - first - 1);
292 i < CUPS_PAGE_MAX && printer;
293 i ++, printer = (ipp_attribute_t *)cupsArrayPrev(printers))
294 cgiSetIPPObjectVars(printer, NULL, i);
295 }
ef416fc2 296
fa73b229 297 /*
298 * Save navigation URLs...
299 */
ef416fc2 300
fa73b229 301 urlend = url + sizeof(url);
ef416fc2 302
fa73b229 303 if ((var = cgiGetVariable("QUERY")) != NULL)
304 {
305 strlcpy(url, "/printers/?QUERY=", sizeof(url));
306 urlptr = url + strlen(url);
ef416fc2 307
fa73b229 308 cgiFormEncode(urlptr, var, urlend - urlptr);
309 urlptr += strlen(urlptr);
ef416fc2 310
fa73b229 311 strlcpy(urlptr, "&", urlend - urlptr);
312 urlptr += strlen(urlptr);
313 }
314 else
315 {
316 strlcpy(url, "/printers/?", sizeof(url));
317 urlptr = url + strlen(url);
318 }
ef416fc2 319
fa73b229 320 snprintf(urlptr, urlend - urlptr, "FIRST=%d", first);
321 cgiSetVariable("THISURL", url);
322
323 if (first > 0)
324 {
325 snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
326 first - CUPS_PAGE_MAX, ascending ? "asc" : "dec");
327 cgiSetVariable("PREVURL", url);
ef416fc2 328 }
fa73b229 329
330 if ((first + CUPS_PAGE_MAX) < count)
331 {
332 snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
333 first + CUPS_PAGE_MAX, ascending ? "asc" : "dec");
334 cgiSetVariable("NEXTURL", url);
335 }
336
ef416fc2 337 /*
fa73b229 338 * Then show everything...
ef416fc2 339 */
340
fa73b229 341 cgiCopyTemplateLang("search.tmpl");
ef416fc2 342
fa73b229 343 cgiCopyTemplateLang("printers-header.tmpl");
ef416fc2 344
fa73b229 345 if (count > 0)
346 cgiCopyTemplateLang("pager.tmpl");
ef416fc2 347
fa73b229 348 cgiCopyTemplateLang("printers.tmpl");
ef416fc2 349
fa73b229 350 if (count > 0)
351 cgiCopyTemplateLang("pager.tmpl");
ef416fc2 352
353 /*
fa73b229 354 * Delete the response...
ef416fc2 355 */
356
bd7854cb 357 cupsArrayDelete(printers);
fa73b229 358 ippDelete(response);
359 }
360 else
361 {
362 /*
363 * Show the error...
364 */
ef416fc2 365
fa73b229 366 cgiShowIPPError(_("Unable to get printer list:"));
367 }
ef416fc2 368
fa73b229 369 cgiEndHTML();
370}
ef416fc2 371
ef416fc2 372
fa73b229 373/*
374 * 'show_printer()' - Show a single printer.
375 */
ef416fc2 376
fa73b229 377void
378show_printer(http_t *http, /* I - Connection to server */
379 const char *printer) /* I - Name of printer */
380{
381 ipp_t *request, /* IPP request */
382 *response; /* IPP response */
383 ipp_attribute_t *attr; /* IPP attribute */
384 char uri[HTTP_MAX_URI]; /* Printer URI */
385 char refresh[1024]; /* Refresh URL */
ef416fc2 386
ef416fc2 387
bd7854cb 388 fprintf(stderr, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
389 http, printer);
390
fa73b229 391 /*
392 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
393 * attributes:
394 *
395 * attributes-charset
396 * attributes-natural-language
397 * printer-uri
398 */
399
400 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
401
a4d04587 402 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
403 "localhost", 0, "/printers/%s", printer);
fa73b229 404 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
405 uri);
406
407 cgiGetAttributes(request, "printers.tmpl");
408
409 /*
410 * Do the request and get back a response...
411 */
ef416fc2 412
fa73b229 413 if ((response = cupsDoRequest(http, request, "/")) != NULL)
414 {
ef416fc2 415 /*
fa73b229 416 * Got the result; set the CGI variables and check the status of a
417 * single-queue request...
ef416fc2 418 */
419
fa73b229 420 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
421
422 if (printer && (attr = ippFindAttribute(response, "printer-state",
423 IPP_TAG_ENUM)) != NULL &&
424 attr->values[0].integer == IPP_PRINTER_PROCESSING)
ef416fc2 425 {
fa73b229 426 /*
427 * Printer is processing - automatically refresh the page until we
428 * are done printing...
429 */
ef416fc2 430
fa73b229 431 cgiFormEncode(uri, printer, sizeof(uri));
432 snprintf(refresh, sizeof(refresh), "10;/printers/%s", uri);
433 cgiSetVariable("refresh_page", refresh);
ef416fc2 434 }
ef416fc2 435
fa73b229 436 /*
437 * Delete the response...
438 */
439
440 ippDelete(response);
ef416fc2 441
442 /*
443 * Show the standard header...
444 */
445
fa73b229 446 cgiStartHTML(printer);
ef416fc2 447
448 /*
fa73b229 449 * Show the printer status...
ef416fc2 450 */
451
fa73b229 452 cgiCopyTemplateLang("printers.tmpl");
ef416fc2 453
fa73b229 454 /*
455 * Show jobs for the specified printer...
456 */
ef416fc2 457
fa73b229 458 cgiCopyTemplateLang("printer-jobs-header.tmpl");
459 cgiShowJobs(http, printer);
460 }
461 else
462 {
463 /*
464 * Show the IPP error...
465 */
ef416fc2 466
fa73b229 467 cgiStartHTML(printer);
468 cgiShowIPPError(_("Unable to get printer status:"));
469 }
ef416fc2 470
fa73b229 471 cgiEndHTML();
ef416fc2 472}
473
474
475/*
bd7854cb 476 * End of "$Id: printers.c 5104 2006-02-15 03:21:04Z mike $".
ef416fc2 477 */