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