]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/classes.c
Merge changes from CUPS 1.4svn-r8148.
[thirdparty/cups.git] / cgi-bin / classes.c
1 /*
2 * "$Id: classes.c 7940 2008-09-16 00:45:16Z mike $"
3 *
4 * Class status CGI for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 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 * do_class_op() - Do a class operation.
19 * show_all_classes() - Show all classes...
20 * show_class() - Show a single class.
21 */
22
23 /*
24 * Include necessary headers...
25 */
26
27 #include "cgi-private.h"
28
29
30 /*
31 * Local functions...
32 */
33
34 static void do_class_op(http_t *http, const char *printer, ipp_op_t op,
35 const char *title);
36 static void show_all_classes(http_t *http, const char *username);
37 static void show_class(http_t *http, const char *printer);
38
39
40 /*
41 * 'main()' - Main entry for CGI.
42 */
43
44 int /* O - Exit status */
45 main(int argc, /* I - Number of command-line arguments */
46 char *argv[]) /* I - Command-line arguments */
47 {
48 const char *pclass; /* Class name */
49 const char *user; /* Username */
50 http_t *http; /* Connection to the server */
51 ipp_t *request, /* IPP request */
52 *response; /* IPP response */
53 ipp_attribute_t *attr; /* IPP attribute */
54 const char *op; /* Operation to perform, if any */
55 static const char *def_attrs[] = /* Attributes for default printer */
56 {
57 "printer-name",
58 "printer-uri-supported"
59 };
60
61
62 /*
63 * Get any form variables...
64 */
65
66 cgiInitialize();
67
68 op = cgiGetVariable("OP");
69
70 /*
71 * Set the web interface section...
72 */
73
74 cgiSetVariable("SECTION", "classes");
75
76 /*
77 * See if we are displaying a printer or all classes...
78 */
79
80 if ((pclass = getenv("PATH_INFO")) != NULL)
81 {
82 pclass ++;
83
84 if (!*pclass)
85 pclass = NULL;
86
87 if (pclass)
88 cgiSetVariable("PRINTER_NAME", pclass);
89 }
90
91 /*
92 * See who is logged in...
93 */
94
95 user = getenv("REMOTE_USER");
96
97 /*
98 * Connect to the HTTP server...
99 */
100
101 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
102
103 /*
104 * Get the default printer...
105 */
106
107 if (!op || !cgiIsPOST())
108 {
109 /*
110 * Get the default destination...
111 */
112
113 request = ippNewRequest(CUPS_GET_DEFAULT);
114
115 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
116 "requested-attributes",
117 sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
118
119 if ((response = cupsDoRequest(http, request, "/")) != NULL)
120 {
121 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
122 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
123
124 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
125 {
126 char url[HTTP_MAX_URI]; /* New URL */
127
128
129 cgiSetVariable("DEFAULT_URI",
130 cgiRewriteURL(attr->values[0].string.text,
131 url, sizeof(url), NULL));
132 }
133
134 ippDelete(response);
135 }
136
137 /*
138 * See if we need to show a list of classes or the status of a
139 * single printer...
140 */
141
142 if (!pclass)
143 show_all_classes(http, user);
144 else
145 show_class(http, pclass);
146 }
147 else if (pclass)
148 {
149 if (!strcmp(op, "start-class"))
150 do_class_op(http, pclass, IPP_RESUME_PRINTER, cgiText(_("Resume Class")));
151 else if (!strcmp(op, "stop-class"))
152 do_class_op(http, pclass, IPP_PAUSE_PRINTER, cgiText(_("Pause Class")));
153 else if (!strcmp(op, "accept-jobs"))
154 do_class_op(http, pclass, CUPS_ACCEPT_JOBS, cgiText(_("Accept Jobs")));
155 else if (!strcmp(op, "reject-jobs"))
156 do_class_op(http, pclass, CUPS_REJECT_JOBS, cgiText(_("Reject Jobs")));
157 else if (!strcmp(op, "purge-jobs"))
158 do_class_op(http, pclass, IPP_PURGE_JOBS, cgiText(_("Purge Jobs")));
159 else if (!strcasecmp(op, "print-test-page"))
160 cgiPrintTestPage(http, pclass);
161 else if (!strcasecmp(op, "move-jobs"))
162 cgiMoveJobs(http, pclass, 0);
163 else
164 {
165 /*
166 * Unknown/bad operation...
167 */
168
169 cgiStartHTML(pclass);
170 cgiCopyTemplateLang("error-op.tmpl");
171 cgiEndHTML();
172 }
173 }
174 else
175 {
176 /*
177 * Unknown/bad operation...
178 */
179
180 cgiStartHTML(cgiText(_("Classes")));
181 cgiCopyTemplateLang("error-op.tmpl");
182 cgiEndHTML();
183 }
184
185 /*
186 * Close the HTTP server connection...
187 */
188
189 httpClose(http);
190
191 /*
192 * Return with no errors...
193 */
194
195 return (0);
196 }
197
198
199 /*
200 * 'do_class_op()' - Do a class operation.
201 */
202
203 static void
204 do_class_op(http_t *http, /* I - HTTP connection */
205 const char *printer, /* I - Printer name */
206 ipp_op_t op, /* I - Operation to perform */
207 const char *title) /* I - Title of page */
208 {
209 ipp_t *request; /* IPP request */
210 char uri[HTTP_MAX_URI], /* Printer URI */
211 resource[HTTP_MAX_URI]; /* Path for request */
212
213
214 /*
215 * Build a printer request, which requires the following
216 * attributes:
217 *
218 * attributes-charset
219 * attributes-natural-language
220 * printer-uri
221 */
222
223 request = ippNewRequest(op);
224
225 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
226 "localhost", 0, "/classes/%s", printer);
227 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
228 NULL, uri);
229
230 /*
231 * Do the request and get back a response...
232 */
233
234 snprintf(resource, sizeof(resource), "/classes/%s", printer);
235 ippDelete(cupsDoRequest(http, request, resource));
236
237 if (cupsLastError() == IPP_NOT_AUTHORIZED)
238 {
239 puts("Status: 401\n");
240 exit(0);
241 }
242 else if (cupsLastError() > IPP_OK_CONFLICT)
243 {
244 cgiStartHTML(title);
245 cgiShowIPPError(_("Unable to do maintenance command:"));
246 }
247 else
248 {
249 /*
250 * Redirect successful updates back to the printer page...
251 */
252
253 char url[1024], /* Printer/class URL */
254 refresh[1024]; /* Refresh URL */
255
256
257 cgiRewriteURL(uri, url, sizeof(url), NULL);
258 cgiFormEncode(uri, url, sizeof(uri));
259 snprintf(refresh, sizeof(refresh), "5;URL=%s", uri);
260 cgiSetVariable("refresh_page", refresh);
261
262 cgiStartHTML(title);
263
264 cgiSetVariable("IS_CLASS", "YES");
265
266 if (op == IPP_PAUSE_PRINTER)
267 cgiCopyTemplateLang("printer-stop.tmpl");
268 else if (op == IPP_RESUME_PRINTER)
269 cgiCopyTemplateLang("printer-start.tmpl");
270 else if (op == CUPS_ACCEPT_JOBS)
271 cgiCopyTemplateLang("printer-accept.tmpl");
272 else if (op == CUPS_REJECT_JOBS)
273 cgiCopyTemplateLang("printer-reject.tmpl");
274 else if (op == IPP_PURGE_JOBS)
275 cgiCopyTemplateLang("printer-purge.tmpl");
276 }
277
278 cgiEndHTML();
279 }
280
281
282 /*
283 * 'show_all_classes()' - Show all classes...
284 */
285
286 static void
287 show_all_classes(http_t *http, /* I - Connection to server */
288 const char *user) /* I - Username */
289 {
290 int i; /* Looping var */
291 ipp_t *request, /* IPP request */
292 *response; /* IPP response */
293 cups_array_t *classes; /* Array of class objects */
294 ipp_attribute_t *pclass; /* Class object */
295 int ascending, /* Order of classes (0 = descending) */
296 first, /* First class to show */
297 count; /* Number of classes */
298 const char *var; /* Form variable */
299 void *search; /* Search data */
300 char val[1024]; /* Form variable */
301
302
303 /*
304 * Show the standard header...
305 */
306
307 cgiStartHTML(cgiText(_("Classes")));
308
309 /*
310 * Build a CUPS_GET_CLASSES request, which requires the following
311 * attributes:
312 *
313 * attributes-charset
314 * attributes-natural-language
315 * requesting-user-name
316 */
317
318 request = ippNewRequest(CUPS_GET_CLASSES);
319
320 if (user)
321 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
322 "requesting-user-name", NULL, user);
323
324 cgiGetAttributes(request, "classes.tmpl");
325
326 /*
327 * Do the request and get back a response...
328 */
329
330 if ((response = cupsDoRequest(http, request, "/")) != NULL)
331 {
332 /*
333 * Get a list of matching job objects.
334 */
335
336 if ((var = cgiGetVariable("QUERY")) != NULL &&
337 !cgiGetVariable("CLEAR"))
338 search = cgiCompileSearch(var);
339 else
340 search = NULL;
341
342 classes = cgiGetIPPObjects(response, search);
343 count = cupsArrayCount(classes);
344
345 if (search)
346 cgiFreeSearch(search);
347
348 /*
349 * Figure out which classes to display...
350 */
351
352 if ((var = cgiGetVariable("FIRST")) != NULL)
353 first = atoi(var);
354 else
355 first = 0;
356
357 if (first >= count)
358 first = count - CUPS_PAGE_MAX;
359
360 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
361
362 if (first < 0)
363 first = 0;
364
365 sprintf(val, "%d", count);
366 cgiSetVariable("TOTAL", val);
367
368 if ((var = cgiGetVariable("ORDER")) != NULL)
369 ascending = !strcasecmp(var, "asc");
370 else
371 ascending = 1;
372
373 if (ascending)
374 {
375 for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, first);
376 i < CUPS_PAGE_MAX && pclass;
377 i ++, pclass = (ipp_attribute_t *)cupsArrayNext(classes))
378 cgiSetIPPObjectVars(pclass, NULL, i);
379 }
380 else
381 {
382 for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, count - first - 1);
383 i < CUPS_PAGE_MAX && pclass;
384 i ++, pclass = (ipp_attribute_t *)cupsArrayPrev(classes))
385 cgiSetIPPObjectVars(pclass, NULL, i);
386 }
387
388 /*
389 * Save navigation URLs...
390 */
391
392 cgiSetVariable("THISURL", "/classes/");
393
394 if (first > 0)
395 {
396 sprintf(val, "%d", first - CUPS_PAGE_MAX);
397 cgiSetVariable("PREV", val);
398 }
399
400 if ((first + CUPS_PAGE_MAX) < count)
401 {
402 sprintf(val, "%d", first + CUPS_PAGE_MAX);
403 cgiSetVariable("NEXT", val);
404 }
405
406 /*
407 * Then show everything...
408 */
409
410 cgiCopyTemplateLang("search.tmpl");
411
412 cgiCopyTemplateLang("classes-header.tmpl");
413
414 if (count > CUPS_PAGE_MAX)
415 cgiCopyTemplateLang("pager.tmpl");
416
417 cgiCopyTemplateLang("classes.tmpl");
418
419 if (count > CUPS_PAGE_MAX)
420 cgiCopyTemplateLang("pager.tmpl");
421
422 /*
423 * Delete the response...
424 */
425
426 cupsArrayDelete(classes);
427 ippDelete(response);
428 }
429 else
430 {
431 /*
432 * Show the error...
433 */
434
435 cgiShowIPPError(_("Unable to get class list:"));
436 }
437
438 cgiEndHTML();
439 }
440
441
442 /*
443 * 'show_class()' - Show a single class.
444 */
445
446 static void
447 show_class(http_t *http, /* I - Connection to server */
448 const char *pclass) /* I - Name of class */
449 {
450 ipp_t *request, /* IPP request */
451 *response; /* IPP response */
452 ipp_attribute_t *attr; /* IPP attribute */
453 char uri[HTTP_MAX_URI]; /* Printer URI */
454 char refresh[1024]; /* Refresh URL */
455
456
457 /*
458 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
459 * attributes:
460 *
461 * attributes-charset
462 * attributes-natural-language
463 * printer-uri
464 */
465
466 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
467
468 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
469 "localhost", 0, "/classes/%s", pclass);
470 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
471 uri);
472
473 cgiGetAttributes(request, "class.tmpl");
474
475 /*
476 * Do the request and get back a response...
477 */
478
479 if ((response = cupsDoRequest(http, request, "/")) != NULL)
480 {
481 /*
482 * Got the result; set the CGI variables and check the status of a
483 * single-queue request...
484 */
485
486 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
487
488 if (pclass && (attr = ippFindAttribute(response, "printer-state",
489 IPP_TAG_ENUM)) != NULL &&
490 attr->values[0].integer == IPP_PRINTER_PROCESSING)
491 {
492 /*
493 * Class is processing - automatically refresh the page until we
494 * are done printing...
495 */
496
497 cgiFormEncode(uri, pclass, sizeof(uri));
498 snprintf(refresh, sizeof(refresh), "10;URL=/classes/%s", uri);
499 cgiSetVariable("refresh_page", refresh);
500 }
501
502 /*
503 * Delete the response...
504 */
505
506 ippDelete(response);
507
508 /*
509 * Show the standard header...
510 */
511
512 cgiStartHTML(pclass);
513
514 /*
515 * Show the class status...
516 */
517
518 cgiCopyTemplateLang("class.tmpl");
519
520 /*
521 * Show jobs for the specified class...
522 */
523
524 cgiCopyTemplateLang("class-jobs-header.tmpl");
525 cgiShowJobs(http, pclass);
526 }
527 else
528 {
529 /*
530 * Show the IPP error...
531 */
532
533 cgiStartHTML(pclass);
534 cgiShowIPPError(_("Unable to get class status:"));
535 }
536
537 cgiEndHTML();
538 }
539
540
541 /*
542 * End of "$Id: classes.c 7940 2008-09-16 00:45:16Z mike $".
543 */