]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/classes.c
Load cups into easysw/current.
[thirdparty/cups.git] / cgi-bin / classes.c
1 /*
2 * "$Id: classes.c 5202 2006-02-28 19:37:03Z mike $"
3 *
4 * Class 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_classes() - Show all classes...
28 * show_class() - Show a single class.
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_classes(http_t *http, const char *username);
43 void show_class(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 *pclass; /* Class 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", "classes");
81
82 /*
83 * See if we are displaying a printer or all classes...
84 */
85
86 if ((pclass = getenv("PATH_INFO")) != NULL)
87 {
88 pclass ++;
89
90 if (!*pclass)
91 pclass = NULL;
92 }
93
94 /*
95 * See who is logged in...
96 */
97
98 if ((user = getenv("REMOTE_USER")) == NULL)
99 user = "guest";
100
101 /*
102 * Connect to the HTTP server...
103 */
104
105 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
106
107 /*
108 * Get the default printer...
109 */
110
111 if (!op)
112 {
113 /*
114 * Get the default destination...
115 */
116
117 request = ippNewRequest(CUPS_GET_DEFAULT);
118
119 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
120 "requested-attributes",
121 sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
122
123 if ((response = cupsDoRequest(http, request, "/")) != NULL)
124 {
125 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
126 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
127
128 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
129 {
130 char url[HTTP_MAX_URI]; /* New URL */
131
132
133 cgiSetVariable("DEFAULT_URI",
134 cgiRewriteURL(attr->values[0].string.text,
135 url, sizeof(url), NULL));
136 }
137
138 ippDelete(response);
139 }
140
141 /*
142 * See if we need to show a list of classes or the status of a
143 * single printer...
144 */
145
146 if (!pclass)
147 show_all_classes(http, user);
148 else
149 show_class(http, pclass);
150 }
151 else if (!strcasecmp(op, "print-test-page") && pclass)
152 cgiPrintTestPage(http, pclass);
153 else if (!strcasecmp(op, "move-jobs") && pclass)
154 cgiMoveJobs(http, pclass, 0);
155 else
156 {
157 /*
158 * Unknown/bad operation...
159 */
160
161 if (pclass)
162 cgiStartHTML(pclass);
163 else
164 cgiStartHTML(cgiText(_("Classes")));
165
166 cgiCopyTemplateLang("error-op.tmpl");
167 cgiEndHTML();
168 }
169
170 /*
171 * Close the HTTP server connection...
172 */
173
174 httpClose(http);
175
176 /*
177 * Return with no errors...
178 */
179
180 return (0);
181 }
182
183
184 /*
185 * 'show_all_classes()' - Show all classes...
186 */
187
188 void
189 show_all_classes(http_t *http, /* I - Connection to server */
190 const char *user) /* I - Username */
191 {
192 int i; /* Looping var */
193 ipp_t *request, /* IPP request */
194 *response; /* IPP response */
195 cups_array_t *classes; /* Array of class objects */
196 ipp_attribute_t *pclass; /* Class object */
197 int ascending, /* Order of classes (0 = descending) */
198 first, /* First class to show */
199 count; /* Number of classes */
200 const char *var; /* Form variable */
201 void *search; /* Search data */
202 char url[1024], /* URL for prev/next/this */
203 *urlptr, /* Position in URL */
204 *urlend; /* End of URL */
205
206
207 /*
208 * Show the standard header...
209 */
210
211 cgiStartHTML(cgiText(_("Classes")));
212
213 /*
214 * Build a CUPS_GET_CLASSES request, which requires the following
215 * attributes:
216 *
217 * attributes-charset
218 * attributes-natural-language
219 * requesting-user-name
220 */
221
222 request = ippNewRequest(CUPS_GET_CLASSES);
223
224 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
225 "requesting-user-name", NULL, user);
226
227 cgiGetAttributes(request, "classes.tmpl");
228
229 /*
230 * Do the request and get back a response...
231 */
232
233 if ((response = cupsDoRequest(http, request, "/")) != NULL)
234 {
235 /*
236 * Get a list of matching job objects.
237 */
238
239 if ((var = cgiGetVariable("QUERY")) != NULL)
240 search = cgiCompileSearch(var);
241 else
242 search = NULL;
243
244 classes = cgiGetIPPObjects(response, search);
245 count = cupsArrayCount(classes);
246
247 if (search)
248 cgiFreeSearch(search);
249
250 /*
251 * Figure out which classes to display...
252 */
253
254 if ((var = cgiGetVariable("FIRST")) != NULL)
255 first = atoi(var);
256 else
257 first = 0;
258
259 if (first >= count)
260 first = count - CUPS_PAGE_MAX;
261
262 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
263
264 if (first < 0)
265 first = 0;
266
267 sprintf(url, "%d", count);
268 cgiSetVariable("TOTAL", url);
269
270 if ((var = cgiGetVariable("ORDER")) != NULL)
271 ascending = !strcasecmp(var, "asc");
272 else
273 ascending = 1;
274
275 if (ascending)
276 {
277 for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, first);
278 i < CUPS_PAGE_MAX && pclass;
279 i ++, pclass = (ipp_attribute_t *)cupsArrayNext(classes))
280 cgiSetIPPObjectVars(pclass, NULL, i);
281 }
282 else
283 {
284 for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, count - first - 1);
285 i < CUPS_PAGE_MAX && pclass;
286 i ++, pclass = (ipp_attribute_t *)cupsArrayPrev(classes))
287 cgiSetIPPObjectVars(pclass, NULL, i);
288 }
289
290 /*
291 * Save navigation URLs...
292 */
293
294 urlend = url + sizeof(url);
295
296 if ((var = cgiGetVariable("QUERY")) != NULL)
297 {
298 strlcpy(url, "/classes/?QUERY=", sizeof(url));
299 urlptr = url + strlen(url);
300
301 cgiFormEncode(urlptr, var, urlend - urlptr);
302 urlptr += strlen(urlptr);
303
304 strlcpy(urlptr, "&", urlend - urlptr);
305 urlptr += strlen(urlptr);
306 }
307 else
308 {
309 strlcpy(url, "/classes/?", sizeof(url));
310 urlptr = url + strlen(url);
311 }
312
313 snprintf(urlptr, urlend - urlptr, "FIRST=%d", first);
314 cgiSetVariable("THISURL", url);
315
316 if (first > 0)
317 {
318 snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
319 first - CUPS_PAGE_MAX, ascending ? "asc" : "dec");
320 cgiSetVariable("PREVURL", url);
321 }
322
323 if ((first + CUPS_PAGE_MAX) < count)
324 {
325 snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
326 first + CUPS_PAGE_MAX, ascending ? "asc" : "dec");
327 cgiSetVariable("NEXTURL", url);
328 }
329
330 /*
331 * Then show everything...
332 */
333
334 cgiCopyTemplateLang("search.tmpl");
335
336 cgiCopyTemplateLang("classes-header.tmpl");
337
338 if (count > 0)
339 cgiCopyTemplateLang("pager.tmpl");
340
341 cgiCopyTemplateLang("classes.tmpl");
342
343 if (count > 0)
344 cgiCopyTemplateLang("pager.tmpl");
345
346 /*
347 * Delete the response...
348 */
349
350 cupsArrayDelete(classes);
351 ippDelete(response);
352 }
353 else
354 {
355 /*
356 * Show the error...
357 */
358
359 cgiShowIPPError(_("Unable to get class list:"));
360 }
361
362 cgiEndHTML();
363 }
364
365
366 /*
367 * 'show_class()' - Show a single class.
368 */
369
370 void
371 show_class(http_t *http, /* I - Connection to server */
372 const char *pclass) /* I - Name of class */
373 {
374 ipp_t *request, /* IPP request */
375 *response; /* IPP response */
376 ipp_attribute_t *attr; /* IPP attribute */
377 char uri[HTTP_MAX_URI]; /* Printer URI */
378 char refresh[1024]; /* Refresh URL */
379
380
381 /*
382 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
383 * attributes:
384 *
385 * attributes-charset
386 * attributes-natural-language
387 * printer-uri
388 */
389
390 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
391
392 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
393 "localhost", 0, "/classes/%s", pclass);
394 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
395 uri);
396
397 cgiGetAttributes(request, "classes.tmpl");
398
399 /*
400 * Do the request and get back a response...
401 */
402
403 if ((response = cupsDoRequest(http, request, "/")) != NULL)
404 {
405 /*
406 * Got the result; set the CGI variables and check the status of a
407 * single-queue request...
408 */
409
410 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
411
412 if (pclass && (attr = ippFindAttribute(response, "printer-state",
413 IPP_TAG_ENUM)) != NULL &&
414 attr->values[0].integer == IPP_PRINTER_PROCESSING)
415 {
416 /*
417 * Class is processing - automatically refresh the page until we
418 * are done printing...
419 */
420
421 cgiFormEncode(uri, pclass, sizeof(uri));
422 snprintf(refresh, sizeof(refresh), "10;/classes/%s", uri);
423 cgiSetVariable("refresh_page", refresh);
424 }
425
426 /*
427 * Delete the response...
428 */
429
430 ippDelete(response);
431
432 /*
433 * Show the standard header...
434 */
435
436 cgiStartHTML(pclass);
437
438 /*
439 * Show the class status...
440 */
441
442 cgiCopyTemplateLang("classes.tmpl");
443
444 /*
445 * Show jobs for the specified class...
446 */
447
448 cgiCopyTemplateLang("class-jobs-header.tmpl");
449 cgiShowJobs(http, pclass);
450 }
451 else
452 {
453 /*
454 * Show the IPP error...
455 */
456
457 cgiStartHTML(pclass);
458 cgiShowIPPError(_("Unable to get class status:"));
459 }
460
461 cgiEndHTML();
462 }
463
464
465 /*
466 * End of "$Id: classes.c 5202 2006-02-28 19:37:03Z mike $".
467 */