]> 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 5023 2006-01-29 14:39:44Z 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 /*
205 * Show the standard header...
206 */
207
208 cgiStartHTML(cgiText(_("Printers")));
209
210 /*
211 * Build a CUPS_GET_PRINTERS request, which requires the following
212 * attributes:
213 *
214 * attributes-charset
215 * attributes-natural-language
216 * printer-type
217 * printer-type-mask
218 * requesting-user-name
219 */
220
221 request = ippNewRequest(CUPS_GET_PRINTERS);
222
223 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
224 "printer-type", 0);
225 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
226 "printer-type-mask", CUPS_PRINTER_CLASS);
227
228 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
229 "requesting-user-name", NULL, user);
230
231 cgiGetAttributes(request, "printers.tmpl");
232
233 /*
234 * Do the request and get back a response...
235 */
236
237 if ((response = cupsDoRequest(http, request, "/")) != NULL)
238 {
239 /*
240 * Get a list of matching job objects.
241 */
242
243 if ((var = cgiGetVariable("QUERY")) != NULL)
244 search = cgiCompileSearch(var);
245 else
246 search = NULL;
247
248 printers = cgiGetIPPObjects(response, search);
249 count = cupsArrayCount(printers);
250
251 if (search)
252 cgiFreeSearch(search);
253
254 /*
255 * Figure out which printers to display...
256 */
257
258 if ((var = cgiGetVariable("FIRST")) != NULL)
259 first = atoi(var);
260 else
261 first = 0;
262
263 if (first >= count)
264 first = count - CUPS_PAGE_MAX;
265
266 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
267
268 if (first < 0)
269 first = 0;
270
271 sprintf(url, "%d", count);
272 cgiSetVariable("TOTAL", url);
273
274 if ((var = cgiGetVariable("ORDER")) != NULL)
275 ascending = !strcasecmp(var, "asc");
276 else
277 ascending = 1;
278
279 if (ascending)
280 {
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);
285 }
286 else
287 {
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);
292 }
293
294 /*
295 * Save navigation URLs...
296 */
297
298 urlend = url + sizeof(url);
299
300 if ((var = cgiGetVariable("QUERY")) != NULL)
301 {
302 strlcpy(url, "/printers/?QUERY=", sizeof(url));
303 urlptr = url + strlen(url);
304
305 cgiFormEncode(urlptr, var, urlend - urlptr);
306 urlptr += strlen(urlptr);
307
308 strlcpy(urlptr, "&", urlend - urlptr);
309 urlptr += strlen(urlptr);
310 }
311 else
312 {
313 strlcpy(url, "/printers/?", sizeof(url));
314 urlptr = url + strlen(url);
315 }
316
317 snprintf(urlptr, urlend - urlptr, "FIRST=%d", first);
318 cgiSetVariable("THISURL", url);
319
320 if (first > 0)
321 {
322 snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
323 first - CUPS_PAGE_MAX, ascending ? "asc" : "dec");
324 cgiSetVariable("PREVURL", url);
325 }
326
327 if ((first + CUPS_PAGE_MAX) < count)
328 {
329 snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
330 first + CUPS_PAGE_MAX, ascending ? "asc" : "dec");
331 cgiSetVariable("NEXTURL", url);
332 }
333
334 /*
335 * Then show everything...
336 */
337
338 cgiCopyTemplateLang("search.tmpl");
339
340 cgiCopyTemplateLang("printers-header.tmpl");
341
342 if (count > 0)
343 cgiCopyTemplateLang("pager.tmpl");
344
345 cgiCopyTemplateLang("printers.tmpl");
346
347 if (count > 0)
348 cgiCopyTemplateLang("pager.tmpl");
349
350 /*
351 * Delete the response...
352 */
353
354 ippDelete(response);
355 }
356 else
357 {
358 /*
359 * Show the error...
360 */
361
362 cgiShowIPPError(_("Unable to get printer list:"));
363 }
364
365 cgiEndHTML();
366 }
367
368
369 /*
370 * 'show_printer()' - Show a single printer.
371 */
372
373 void
374 show_printer(http_t *http, /* I - Connection to server */
375 const char *printer) /* I - Name of printer */
376 {
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 */
382
383
384 /*
385 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
386 * attributes:
387 *
388 * attributes-charset
389 * attributes-natural-language
390 * printer-uri
391 */
392
393 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
394
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,
398 uri);
399
400 cgiGetAttributes(request, "printers.tmpl");
401
402 /*
403 * Do the request and get back a response...
404 */
405
406 if ((response = cupsDoRequest(http, request, "/")) != NULL)
407 {
408 /*
409 * Got the result; set the CGI variables and check the status of a
410 * single-queue request...
411 */
412
413 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
414
415 if (printer && (attr = ippFindAttribute(response, "printer-state",
416 IPP_TAG_ENUM)) != NULL &&
417 attr->values[0].integer == IPP_PRINTER_PROCESSING)
418 {
419 /*
420 * Printer is processing - automatically refresh the page until we
421 * are done printing...
422 */
423
424 cgiFormEncode(uri, printer, sizeof(uri));
425 snprintf(refresh, sizeof(refresh), "10;/printers/%s", uri);
426 cgiSetVariable("refresh_page", refresh);
427 }
428
429 /*
430 * Delete the response...
431 */
432
433 ippDelete(response);
434
435 /*
436 * Show the standard header...
437 */
438
439 cgiStartHTML(printer);
440
441 /*
442 * Show the printer status...
443 */
444
445 cgiCopyTemplateLang("printers.tmpl");
446
447 /*
448 * Show jobs for the specified printer...
449 */
450
451 cgiCopyTemplateLang("printer-jobs-header.tmpl");
452 cgiShowJobs(http, printer);
453 }
454 else
455 {
456 /*
457 * Show the IPP error...
458 */
459
460 cgiStartHTML(printer);
461 cgiShowIPPError(_("Unable to get printer status:"));
462 }
463
464 cgiEndHTML();
465 }
466
467
468 /*
469 * End of "$Id: printers.c 5023 2006-01-29 14:39:44Z mike $".
470 */