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