]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/printers.c
Load cups into easysw/current.
[thirdparty/cups.git] / cgi-bin / printers.c
1 /*
2 * "$Id: printers.c 5104 2006-02-15 03:21:04Z mike $"
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 *
26 * main() - Main entry for CGI.
27 * show_all_printers() - Show all printers...
28 * show_printer() - Show a single printer.
29 */
30
31 /*
32 * Include necessary headers...
33 */
34
35 #include "cgi-private.h"
36
37
38 /*
39 * Local functions...
40 */
41
42 void show_all_printers(http_t *http, const char *username);
43 void show_printer(http_t *http, const char *printer);
44
45
46 /*
47 * 'main()' - Main entry for CGI.
48 */
49
50 int /* O - Exit status */
51 main(int argc, /* I - Number of command-line arguments */
52 char *argv[]) /* I - Command-line arguments */
53 {
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 */
62 {
63 "printer-name",
64 "printer-uri-supported"
65 };
66
67
68 /*
69 * Get any form variables...
70 */
71
72 cgiInitialize();
73
74 op = cgiGetVariable("OP");
75
76 /*
77 * Set the web interface section...
78 */
79
80 cgiSetVariable("SECTION", "printers");
81
82 /*
83 * See if we are displaying a printer or all printers...
84 */
85
86 if (!strcmp(argv[0], "/") || strstr(argv[0], "printers.cgi"))
87 printer = NULL;
88 else
89 printer = argv[0];
90
91 /*
92 * See who is logged in...
93 */
94
95 if ((user = getenv("REMOTE_USER")) == NULL)
96 user = "guest";
97
98 /*
99 * Connect to the HTTP server...
100 */
101
102 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
103
104 /*
105 * Get the default printer...
106 */
107
108 if (!op)
109 {
110 /*
111 * Get the default destination...
112 */
113
114 request = ippNewRequest(CUPS_GET_DEFAULT);
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 /*
139 * See if we need to show a list of printers or the status of a
140 * single printer...
141 */
142
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 */
157
158 if (printer)
159 cgiStartHTML(printer);
160 else
161 cgiStartHTML(cgiText(_("Printers")));
162
163 cgiCopyTemplateLang("error-op.tmpl");
164 cgiEndHTML();
165 }
166
167 /*
168 * Close the HTTP server connection...
169 */
170
171 httpClose(http);
172
173 /*
174 * Return with no errors...
175 */
176
177 return (0);
178 }
179
180
181 /*
182 * 'show_all_printers()' - Show all printers...
183 */
184
185 void
186 show_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 */
202
203
204 fprintf(stderr, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
205 http, user);
206
207 /*
208 * Show the standard header...
209 */
210
211 cgiStartHTML(cgiText(_("Printers")));
212
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 */
223
224 request = ippNewRequest(CUPS_GET_PRINTERS);
225
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);
230
231 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
232 "requesting-user-name", NULL, user);
233
234 cgiGetAttributes(request, "printers.tmpl");
235
236 /*
237 * Do the request and get back a response...
238 */
239
240 if ((response = cupsDoRequest(http, request, "/")) != NULL)
241 {
242 /*
243 * Get a list of matching job objects.
244 */
245
246 if ((var = cgiGetVariable("QUERY")) != NULL)
247 search = cgiCompileSearch(var);
248 else
249 search = NULL;
250
251 printers = cgiGetIPPObjects(response, search);
252 count = cupsArrayCount(printers);
253
254 if (search)
255 cgiFreeSearch(search);
256
257 /*
258 * Figure out which printers to display...
259 */
260
261 if ((var = cgiGetVariable("FIRST")) != NULL)
262 first = atoi(var);
263 else
264 first = 0;
265
266 if (first >= count)
267 first = count - CUPS_PAGE_MAX;
268
269 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
270
271 if (first < 0)
272 first = 0;
273
274 sprintf(url, "%d", count);
275 cgiSetVariable("TOTAL", url);
276
277 if ((var = cgiGetVariable("ORDER")) != NULL)
278 ascending = !strcasecmp(var, "asc");
279 else
280 ascending = 1;
281
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 }
296
297 /*
298 * Save navigation URLs...
299 */
300
301 urlend = url + sizeof(url);
302
303 if ((var = cgiGetVariable("QUERY")) != NULL)
304 {
305 strlcpy(url, "/printers/?QUERY=", sizeof(url));
306 urlptr = url + strlen(url);
307
308 cgiFormEncode(urlptr, var, urlend - urlptr);
309 urlptr += strlen(urlptr);
310
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 }
319
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);
328 }
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
337 /*
338 * Then show everything...
339 */
340
341 cgiCopyTemplateLang("search.tmpl");
342
343 cgiCopyTemplateLang("printers-header.tmpl");
344
345 if (count > 0)
346 cgiCopyTemplateLang("pager.tmpl");
347
348 cgiCopyTemplateLang("printers.tmpl");
349
350 if (count > 0)
351 cgiCopyTemplateLang("pager.tmpl");
352
353 /*
354 * Delete the response...
355 */
356
357 cupsArrayDelete(printers);
358 ippDelete(response);
359 }
360 else
361 {
362 /*
363 * Show the error...
364 */
365
366 cgiShowIPPError(_("Unable to get printer list:"));
367 }
368
369 cgiEndHTML();
370 }
371
372
373 /*
374 * 'show_printer()' - Show a single printer.
375 */
376
377 void
378 show_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 */
386
387
388 fprintf(stderr, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
389 http, printer);
390
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
402 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
403 "localhost", 0, "/printers/%s", printer);
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 */
412
413 if ((response = cupsDoRequest(http, request, "/")) != NULL)
414 {
415 /*
416 * Got the result; set the CGI variables and check the status of a
417 * single-queue request...
418 */
419
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)
425 {
426 /*
427 * Printer is processing - automatically refresh the page until we
428 * are done printing...
429 */
430
431 cgiFormEncode(uri, printer, sizeof(uri));
432 snprintf(refresh, sizeof(refresh), "10;/printers/%s", uri);
433 cgiSetVariable("refresh_page", refresh);
434 }
435
436 /*
437 * Delete the response...
438 */
439
440 ippDelete(response);
441
442 /*
443 * Show the standard header...
444 */
445
446 cgiStartHTML(printer);
447
448 /*
449 * Show the printer status...
450 */
451
452 cgiCopyTemplateLang("printers.tmpl");
453
454 /*
455 * Show jobs for the specified printer...
456 */
457
458 cgiCopyTemplateLang("printer-jobs-header.tmpl");
459 cgiShowJobs(http, printer);
460 }
461 else
462 {
463 /*
464 * Show the IPP error...
465 */
466
467 cgiStartHTML(printer);
468 cgiShowIPPError(_("Unable to get printer status:"));
469 }
470
471 cgiEndHTML();
472 }
473
474
475 /*
476 * End of "$Id: printers.c 5104 2006-02-15 03:21:04Z mike $".
477 */