]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/ipp-var.c
Merge changes from CUPS 1.4svn-r7282.
[thirdparty/cups.git] / cgi-bin / ipp-var.c
1 /*
2 * "$Id: ipp-var.c 6889 2007-08-29 22:23:35Z mike $"
3 *
4 * CGI <-> IPP variable routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 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 * cgiGetAttributes() - Get the list of attributes that are needed
18 * by the template file.
19 * cgiGetIPPObjects() - Get the objects in an IPP response.
20 * cgiMoveJobs() - Move one or more jobs.
21 * cgiPrintTestPage() - Print a test page.
22 * cgiRewriteURL() - Rewrite a printer URI into a web browser URL...
23 * cgiSetIPPObjectVars() - Set CGI variables from an IPP object.
24 * cgiSetIPPVars() - Set CGI variables from an IPP response.
25 * cgiShowIPPError() - Show the last IPP error message.
26 * cgiShowJobs() - Show print jobs.
27 * cgiText() - Return localized text.
28 */
29
30 /*
31 * Include necessary headers...
32 */
33
34 #include "cgi-private.h"
35
36
37 /*
38 * 'cgiGetAttributes()' - Get the list of attributes that are needed
39 * by the template file.
40 */
41
42 void
43 cgiGetAttributes(ipp_t *request, /* I - IPP request */
44 const char *tmpl) /* I - Base filename */
45 {
46 int num_attrs; /* Number of attributes */
47 char *attrs[1000]; /* Attributes */
48 int i; /* Looping var */
49 char filename[1024], /* Filename */
50 locale[16]; /* Locale name */
51 const char *directory, /* Directory */
52 *lang; /* Language */
53 FILE *in; /* Input file */
54 int ch; /* Character from file */
55 char name[255], /* Name of variable */
56 *nameptr; /* Pointer into name */
57
58
59 /*
60 * Convert the language to a locale name...
61 */
62
63 if ((lang = getenv("LANG")) != NULL)
64 {
65 for (i = 0; lang[i] && i < 15; i ++)
66 if (isalnum(lang[i] & 255))
67 locale[i] = tolower(lang[i]);
68 else
69 locale[i] = '_';
70
71 locale[i] = '\0';
72 }
73 else
74 locale[0] = '\0';
75
76 /*
77 * See if we have a template file for this language...
78 */
79
80 directory = cgiGetTemplateDir();
81
82 snprintf(filename, sizeof(filename), "%s/%s/%s", directory, locale, tmpl);
83 if (access(filename, 0))
84 {
85 locale[2] = '\0';
86
87 snprintf(filename, sizeof(filename), "%s/%s/%s", directory, locale, tmpl);
88 if (access(filename, 0))
89 snprintf(filename, sizeof(filename), "%s/%s", directory, tmpl);
90 }
91
92 /*
93 * Open the template file...
94 */
95
96 if ((in = fopen(filename, "r")) == NULL)
97 return;
98
99 /*
100 * Loop through the file adding attribute names as needed...
101 */
102
103 num_attrs = 0;
104 attrs[0] = NULL; /* Eliminate compiler warning */
105
106 while ((ch = getc(in)) != EOF)
107 if (ch == '\\')
108 getc(in);
109 else if (ch == '{' && num_attrs < (sizeof(attrs) / sizeof(attrs[0])))
110 {
111 /*
112 * Grab the name...
113 */
114
115 for (nameptr = name; (ch = getc(in)) != EOF;)
116 if (strchr("}]<>=! \t\n", ch))
117 break;
118 else if (nameptr > name && ch == '?')
119 break;
120 else if (nameptr < (name + sizeof(name) - 1))
121 {
122 if (ch == '_')
123 *nameptr++ = '-';
124 else
125 *nameptr++ = ch;
126 }
127
128 *nameptr = '\0';
129
130 if (!strncmp(name, "printer_state_history", 21))
131 strcpy(name, "printer_state_history");
132
133 /*
134 * Possibly add it to the list of attributes...
135 */
136
137 for (i = 0; i < num_attrs; i ++)
138 if (!strcmp(attrs[i], name))
139 break;
140
141 if (i >= num_attrs)
142 {
143 attrs[num_attrs] = strdup(name);
144 num_attrs ++;
145 }
146 }
147
148 /*
149 * If we have attributes, add a requested-attributes attribute to the
150 * request...
151 */
152
153 if (num_attrs > 0)
154 {
155 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
156 "requested-attributes", num_attrs, NULL, (const char **)attrs);
157
158 for (i = 0; i < num_attrs; i ++)
159 free(attrs[i]);
160 }
161
162 fclose(in);
163 }
164
165
166 /*
167 * 'cgiGetIPPObjects()' - Get the objects in an IPP response.
168 */
169
170 cups_array_t * /* O - Array of objects */
171 cgiGetIPPObjects(ipp_t *response, /* I - IPP response */
172 void *search) /* I - Search filter */
173 {
174 int i; /* Looping var */
175 cups_array_t *objs; /* Array of objects */
176 ipp_attribute_t *attr, /* Current attribute */
177 *first; /* First attribute for object */
178 ipp_tag_t group; /* Current group tag */
179 int add; /* Add this object to the array? */
180
181
182 if (!response)
183 return (0);
184
185 for (add = 0, first = NULL, objs = cupsArrayNew(NULL, NULL),
186 group = IPP_TAG_ZERO, attr = response->attrs;
187 attr;
188 attr = attr->next)
189 {
190 if (attr->group_tag != group)
191 {
192 group = attr->group_tag;
193
194 if (group != IPP_TAG_ZERO && group != IPP_TAG_OPERATION)
195 {
196 first = attr;
197 add = 0;
198 }
199 else if (add && first)
200 {
201 cupsArrayAdd(objs, first);
202
203 add = 0;
204 first = NULL;
205 }
206 }
207
208 if (attr->name && attr->group_tag != IPP_TAG_OPERATION && !add)
209 {
210 if (!search)
211 {
212 /*
213 * Add all objects if there is no search...
214 */
215
216 add = 1;
217 }
218 else
219 {
220 /*
221 * Check the search string against the string and integer values.
222 */
223
224 switch (attr->value_tag)
225 {
226 case IPP_TAG_TEXTLANG :
227 case IPP_TAG_NAMELANG :
228 case IPP_TAG_TEXT :
229 case IPP_TAG_NAME :
230 case IPP_TAG_KEYWORD :
231 case IPP_TAG_URI :
232 case IPP_TAG_MIMETYPE :
233 for (i = 0; !add && i < attr->num_values; i ++)
234 if (cgiDoSearch(search, attr->values[i].string.text))
235 add = 1;
236 break;
237
238 case IPP_TAG_INTEGER :
239 for (i = 0; !add && i < attr->num_values; i ++)
240 {
241 char buf[255]; /* Number buffer */
242
243
244 sprintf(buf, "%d", attr->values[i].integer);
245
246 if (cgiDoSearch(search, buf))
247 add = 1;
248 }
249 break;
250
251 default :
252 break;
253 }
254 }
255 }
256 }
257
258 if (add && first)
259 cupsArrayAdd(objs, first);
260
261 return (objs);
262 }
263
264
265 /*
266 * 'cgiMoveJobs()' - Move one or more jobs.
267 *
268 * At least one of dest or job_id must be non-zero/NULL.
269 */
270
271 void
272 cgiMoveJobs(http_t *http, /* I - Connection to server */
273 const char *dest, /* I - Destination or NULL */
274 int job_id) /* I - Job ID or 0 for all */
275 {
276 int i; /* Looping var */
277 const char *user; /* Username */
278 ipp_t *request, /* IPP request */
279 *response; /* IPP response */
280 ipp_attribute_t *attr; /* Current attribute */
281 const char *name; /* Destination name */
282 const char *job_printer_uri; /* JOB_PRINTER_URI form variable */
283 char current_dest[1024]; /* Current destination */
284
285
286 /*
287 * See who is logged in...
288 */
289
290 if ((user = getenv("REMOTE_USER")) == NULL)
291 user = "guest";
292
293 /*
294 * See if the user has already selected a new destination...
295 */
296
297 if ((job_printer_uri = cgiGetVariable("JOB_PRINTER_URI")) == NULL)
298 {
299 /*
300 * Make sure necessary form variables are set...
301 */
302
303 if (job_id)
304 {
305 char temp[255]; /* Temporary string */
306
307
308 sprintf(temp, "%d", job_id);
309 cgiSetVariable("JOB_ID", temp);
310 }
311
312 if (dest)
313 cgiSetVariable("PRINTER_NAME", dest);
314
315 /*
316 * No new destination specified, show the user what the available
317 * printers/classes are...
318 */
319
320 if (!dest)
321 {
322 /*
323 * Get the current destination for job N...
324 */
325
326 char job_uri[1024]; /* Job URI */
327
328
329 request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES);
330
331 snprintf(job_uri, sizeof(job_uri), "ipp://localhost/jobs/%d", job_id);
332 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
333 NULL, job_uri);
334 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
335 "requested-attributes", NULL, "job-printer-uri");
336
337 if ((response = cupsDoRequest(http, request, "/")) != NULL)
338 {
339 if ((attr = ippFindAttribute(response, "job-printer-uri",
340 IPP_TAG_URI)) != NULL)
341 {
342 /*
343 * Pull the name from the URI...
344 */
345
346 strlcpy(current_dest, strrchr(attr->values[0].string.text, '/') + 1,
347 sizeof(current_dest));
348 dest = current_dest;
349 }
350
351 ippDelete(response);
352 }
353
354 if (!dest)
355 {
356 /*
357 * Couldn't get the current destination...
358 */
359
360 cgiStartHTML(cgiText(_("Move Job")));
361 cgiShowIPPError(_("Unable to find destination for job!"));
362 cgiEndHTML();
363 return;
364 }
365 }
366
367 /*
368 * Get the list of available destinations...
369 */
370
371 request = ippNewRequest(CUPS_GET_PRINTERS);
372
373 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
374 "requested-attributes", NULL, "printer-uri-supported");
375
376 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
377 "requesting-user-name", NULL, user);
378
379 if ((response = cupsDoRequest(http, request, "/")) != NULL)
380 {
381 for (i = 0, attr = ippFindAttribute(response, "printer-uri-supported",
382 IPP_TAG_URI);
383 attr;
384 attr = ippFindNextAttribute(response, "printer-uri-supported",
385 IPP_TAG_URI))
386 {
387 /*
388 * Pull the name from the URI...
389 */
390
391 name = strrchr(attr->values[0].string.text, '/') + 1;
392
393 /*
394 * If the name is not the same as the current destination, add it!
395 */
396
397 if (strcasecmp(name, dest))
398 {
399 cgiSetArray("JOB_PRINTER_URI", i, attr->values[0].string.text);
400 cgiSetArray("JOB_PRINTER_NAME", i, name);
401 i ++;
402 }
403 }
404
405 ippDelete(response);
406 }
407
408 /*
409 * Show the form...
410 */
411
412 if (job_id)
413 cgiStartHTML(cgiText(_("Move Job")));
414 else
415 cgiStartHTML(cgiText(_("Move All Jobs")));
416
417 cgiCopyTemplateLang("job-move.tmpl");
418 }
419 else
420 {
421 /*
422 * Try moving the job or jobs...
423 */
424
425 char uri[1024], /* Job/printer URI */
426 resource[1024], /* Post resource */
427 refresh[1024]; /* Refresh URL */
428 const char *job_printer_name; /* New printer name */
429
430
431 request = ippNewRequest(CUPS_MOVE_JOB);
432
433 if (job_id)
434 {
435 /*
436 * Move 1 job...
437 */
438
439 snprintf(resource, sizeof(resource), "/jobs/%d", job_id);
440
441 snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
442 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
443 NULL, uri);
444 }
445 else
446 {
447 /*
448 * Move all active jobs on a destination...
449 */
450
451 snprintf(resource, sizeof(resource), "/%s/%s",
452 cgiGetVariable("SECTION"), dest);
453
454 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
455 "localhost", ippPort(), "/%s/%s",
456 cgiGetVariable("SECTION"), dest);
457 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
458 NULL, uri);
459 }
460
461 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-printer-uri",
462 NULL, job_printer_uri);
463
464 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
465 "requesting-user-name", NULL, user);
466
467 ippDelete(cupsDoRequest(http, request, resource));
468
469 /*
470 * Show the results...
471 */
472
473 job_printer_name = strrchr(job_printer_uri, '/') + 1;
474
475 if (cupsLastError() <= IPP_OK_CONFLICT)
476 {
477 cgiRewriteURL(job_printer_uri, resource, sizeof(resource), NULL);
478 cgiFormEncode(uri, resource, sizeof(uri));
479 snprintf(refresh, sizeof(refresh), "2;URL=%s", uri);
480 cgiSetVariable("refresh_page", refresh);
481 }
482
483 if (job_id)
484 cgiStartHTML(cgiText(_("Move Job")));
485 else
486 cgiStartHTML(cgiText(_("Move All Jobs")));
487
488 if (cupsLastError() > IPP_OK_CONFLICT)
489 {
490 if (job_id)
491 cgiShowIPPError(_("Unable to move job"));
492 else
493 cgiShowIPPError(_("Unable to move jobs"));
494 }
495 else
496 {
497 cgiSetVariable("JOB_PRINTER_NAME", job_printer_name);
498 cgiCopyTemplateLang("job-moved.tmpl");
499 }
500 }
501
502 cgiEndHTML();
503 }
504
505
506 /*
507 * 'cgiPrintTestPage()' - Print a test page.
508 */
509
510 void
511 cgiPrintTestPage(http_t *http, /* I - Connection to server */
512 const char *dest) /* I - Destination printer/class */
513 {
514 ipp_t *request, /* IPP request */
515 *response; /* IPP response */
516 char uri[HTTP_MAX_URI], /* Printer URI */
517 resource[1024], /* POST resource path */
518 refresh[1024], /* Refresh URL */
519 filename[1024]; /* Test page filename */
520 const char *datadir; /* CUPS_DATADIR env var */
521 const char *user; /* Username */
522
523
524 /*
525 * See who is logged in...
526 */
527
528 user = getenv("REMOTE_USER");
529
530 /*
531 * Locate the test page file...
532 */
533
534 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
535 datadir = CUPS_DATADIR;
536
537 snprintf(filename, sizeof(filename), "%s/data/testprint.ps", datadir);
538
539 /*
540 * Point to the printer/class...
541 */
542
543 snprintf(resource, sizeof(resource), "/%s/%s", cgiGetVariable("SECTION"),
544 dest);
545
546 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
547 "localhost", ippPort(), "/%s/%s", cgiGetVariable("SECTION"),
548 dest);
549
550 /*
551 * Build an IPP_PRINT_JOB request, which requires the following
552 * attributes:
553 *
554 * attributes-charset
555 * attributes-natural-language
556 * printer-uri
557 * requesting-user-name
558 * document-format
559 */
560
561 request = ippNewRequest(IPP_PRINT_JOB);
562
563 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
564 NULL, uri);
565
566 if (user)
567 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
568 "requesting-user-name", NULL, user);
569
570 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
571 NULL, "Test Page");
572
573 ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
574 NULL, "application/postscript");
575
576 /*
577 * Do the request and get back a response...
578 */
579
580 if ((response = cupsDoFileRequest(http, request, resource,
581 filename)) != NULL)
582 {
583 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
584
585 ippDelete(response);
586 }
587
588 if (cupsLastError() <= IPP_OK_CONFLICT)
589 {
590 /*
591 * Automatically reload the printer status page...
592 */
593
594 cgiFormEncode(uri, resource, sizeof(uri));
595 snprintf(refresh, sizeof(refresh), "2;URL=%s", uri);
596 cgiSetVariable("refresh_page", refresh);
597 }
598 else if (cupsLastError() == IPP_NOT_AUTHORIZED)
599 {
600 puts("Status: 401\n");
601 exit(0);
602 }
603
604 cgiStartHTML(cgiText(_("Print Test Page")));
605
606 if (cupsLastError() > IPP_OK_CONFLICT)
607 cgiShowIPPError(_("Unable to print test page:"));
608 else
609 {
610 cgiSetVariable("PRINTER_NAME", dest);
611
612 cgiCopyTemplateLang("test-page.tmpl");
613 }
614
615 cgiEndHTML();
616 }
617
618
619 /*
620 * 'cgiRewriteURL()' - Rewrite a printer URI into a web browser URL...
621 */
622
623 char * /* O - New URL */
624 cgiRewriteURL(const char *uri, /* I - Current URI */
625 char *url, /* O - New URL */
626 int urlsize, /* I - Size of URL buffer */
627 const char *newresource) /* I - Replacement resource */
628 {
629 char method[HTTP_MAX_URI],
630 userpass[HTTP_MAX_URI],
631 hostname[HTTP_MAX_URI],
632 rawresource[HTTP_MAX_URI],
633 resource[HTTP_MAX_URI],
634 /* URI components... */
635 *rawptr, /* Pointer into rawresource */
636 *resptr; /* Pointer into resource */
637 int port; /* Port number */
638 static int ishttps = -1; /* Using encryption? */
639 static const char *server; /* Name of server */
640 static char servername[1024];
641 /* Local server name */
642 static const char hexchars[] = "0123456789ABCDEF";
643 /* Hexadecimal conversion characters */
644
645
646 /*
647 * Check if we have been called before...
648 */
649
650 if (ishttps < 0)
651 {
652 /*
653 * No, initialize static vars for the conversion...
654 *
655 * First get the server name associated with the client interface as
656 * well as the locally configured hostname. We'll check *both* of
657 * these to see if the printer URL is local...
658 */
659
660 if ((server = getenv("SERVER_NAME")) == NULL)
661 server = "";
662
663 httpGetHostname(NULL, servername, sizeof(servername));
664
665 /*
666 * Then flag whether we are using SSL on this connection...
667 */
668
669 ishttps = getenv("HTTPS") != NULL;
670 }
671
672 /*
673 * Convert the URI to a URL...
674 */
675
676 httpSeparateURI(HTTP_URI_CODING_ALL, uri, method, sizeof(method), userpass,
677 sizeof(userpass), hostname, sizeof(hostname), &port,
678 rawresource, sizeof(rawresource));
679
680 if (!strcmp(method, "ipp") ||
681 !strcmp(method, "http") ||
682 !strcmp(method, "https"))
683 {
684 if (newresource)
685 {
686 /*
687 * Force the specified resource name instead of the one in the URL...
688 */
689
690 strlcpy(resource, newresource, sizeof(resource));
691 }
692 else
693 {
694 /*
695 * Rewrite the resource string so it doesn't contain any
696 * illegal chars...
697 */
698
699 for (rawptr = rawresource, resptr = resource; *rawptr; rawptr ++)
700 if ((*rawptr & 128) || *rawptr == '%' || *rawptr == ' ' ||
701 *rawptr == '#' || *rawptr == '?' ||
702 *rawptr == '.') /* For MSIE */
703 {
704 if (resptr < (resource + sizeof(resource) - 3))
705 {
706 *resptr++ = '%';
707 *resptr++ = hexchars[(*rawptr >> 4) & 15];
708 *resptr++ = hexchars[*rawptr & 15];
709 }
710 }
711 else if (resptr < (resource + sizeof(resource) - 1))
712 *resptr++ = *rawptr;
713
714 *resptr = '\0';
715 }
716
717 /*
718 * Map local access to a local URI...
719 */
720
721 if (!strcasecmp(hostname, "localhost") ||
722 !strncasecmp(hostname, "localhost.", 10) ||
723 !strcasecmp(hostname, server) ||
724 !strcasecmp(hostname, servername))
725 {
726 /*
727 * Make URI relative to the current server...
728 */
729
730 strlcpy(url, resource, urlsize);
731 }
732 else
733 {
734 /*
735 * Rewrite URI with HTTP/HTTPS scheme...
736 */
737
738 if (userpass[0])
739 snprintf(url, urlsize, "%s://%s@%s:%d%s",
740 ishttps ? "https" : "http",
741 userpass, hostname, port, resource);
742 else
743 snprintf(url, urlsize, "%s://%s:%d%s",
744 ishttps ? "https" : "http",
745 hostname, port, resource);
746 }
747 }
748 else
749 strlcpy(url, uri, urlsize);
750
751 return (url);
752 }
753
754
755 /*
756 * 'cgiSetIPPObjectVars()' - Set CGI variables from an IPP object.
757 */
758
759 ipp_attribute_t * /* O - Next object */
760 cgiSetIPPObjectVars(
761 ipp_attribute_t *obj, /* I - Response data to be copied... */
762 const char *prefix, /* I - Prefix for name or NULL */
763 int element) /* I - Parent element number */
764 {
765 ipp_attribute_t *attr; /* Attribute in response... */
766 int i; /* Looping var */
767 char name[1024], /* Name of attribute */
768 *nameptr, /* Pointer into name */
769 value[16384], /* Value(s) */
770 *valptr; /* Pointer into value */
771 struct tm *date; /* Date information */
772
773
774 fprintf(stderr, "DEBUG2: cgiSetIPPObjectVars(obj=%p, prefix=\"%s\", "
775 "element=%d)\n",
776 obj, prefix ? prefix : "(null)", element);
777
778 /*
779 * Set common CGI template variables...
780 */
781
782 if (!prefix)
783 cgiSetServerVersion();
784
785 /*
786 * Loop through the attributes and set them for the template...
787 */
788
789 for (attr = obj; attr && attr->group_tag != IPP_TAG_ZERO; attr = attr->next)
790 {
791 /*
792 * Copy the attribute name, substituting "_" for "-"...
793 */
794
795 if (!attr->name)
796 continue;
797
798 if (prefix)
799 {
800 snprintf(name, sizeof(name), "%s.", prefix);
801 nameptr = name + strlen(name);
802 }
803 else
804 nameptr = name;
805
806 for (i = 0; attr->name[i] && nameptr < (name + sizeof(name) - 1); i ++)
807 if (attr->name[i] == '-')
808 *nameptr++ = '_';
809 else
810 *nameptr++ = attr->name[i];
811
812 *nameptr = '\0';
813
814 /*
815 * Add "job_printer_name" variable if we have a "job_printer_uri"
816 * attribute...
817 */
818
819 if (!strcmp(name, "job_printer_uri"))
820 {
821 if ((valptr = strrchr(attr->values[0].string.text, '/')) == NULL)
822 valptr = "unknown";
823 else
824 valptr ++;
825
826 cgiSetArray("job_printer_name", element, valptr);
827 }
828
829 /*
830 * Localize event names in "notify_events" variable...
831 */
832
833 if (!strcmp(name, "notify_events"))
834 {
835 size_t remaining; /* Remaining bytes in buffer */
836
837
838 value[0] = '\0';
839 valptr = value;
840
841 for (i = 0; i < attr->num_values; i ++)
842 {
843 if (valptr >= (value + sizeof(value) - 3))
844 break;
845
846 if (i)
847 {
848 *valptr++ = ',';
849 *valptr++ = ' ';
850 }
851
852 remaining = sizeof(value) - (valptr - value);
853
854 if (!strcmp(attr->values[i].string.text, "printer-stopped"))
855 strlcpy(valptr, _("Printer Paused"), remaining);
856 else if (!strcmp(attr->values[i].string.text, "printer-added"))
857 strlcpy(valptr, _("Printer Added"), remaining);
858 else if (!strcmp(attr->values[i].string.text, "printer-modified"))
859 strlcpy(valptr, _("Printer Modified"), remaining);
860 else if (!strcmp(attr->values[i].string.text, "printer-deleted"))
861 strlcpy(valptr, _("Printer Deleted"), remaining);
862 else if (!strcmp(attr->values[i].string.text, "job-created"))
863 strlcpy(valptr, _("Job Created"), remaining);
864 else if (!strcmp(attr->values[i].string.text, "job-completed"))
865 strlcpy(valptr, _("Job Completed"), remaining);
866 else if (!strcmp(attr->values[i].string.text, "job-stopped"))
867 strlcpy(valptr, _("Job Stopped"), remaining);
868 else if (!strcmp(attr->values[i].string.text, "job-config-changed"))
869 strlcpy(valptr, _("Job Options Changed"), remaining);
870 else if (!strcmp(attr->values[i].string.text, "server-restarted"))
871 strlcpy(valptr, _("Server Restarted"), remaining);
872 else if (!strcmp(attr->values[i].string.text, "server-started"))
873 strlcpy(valptr, _("Server Started"), remaining);
874 else if (!strcmp(attr->values[i].string.text, "server-stopped"))
875 strlcpy(valptr, _("Server Stopped"), remaining);
876 else if (!strcmp(attr->values[i].string.text, "server-audit"))
877 strlcpy(valptr, _("Server Security Auditing"), remaining);
878 else
879 strlcpy(valptr, attr->values[i].string.text, remaining);
880
881 valptr += strlen(valptr);
882 }
883
884 cgiSetArray("notify_events", element, value);
885 continue;
886 }
887
888 /*
889 * Add "notify_printer_name" variable if we have a "notify_printer_uri"
890 * attribute...
891 */
892
893 if (!strcmp(name, "notify_printer_uri"))
894 {
895 if ((valptr = strrchr(attr->values[0].string.text, '/')) == NULL)
896 valptr = "unknown";
897 else
898 valptr ++;
899
900 cgiSetArray("notify_printer_name", element, valptr);
901 }
902
903 /*
904 * Add "notify_recipient_name" variable if we have a "notify_recipient_uri"
905 * attribute, and rewrite recipient URI...
906 */
907
908 if (!strcmp(name, "notify_recipient_uri"))
909 {
910 char uri[1024], /* New URI */
911 scheme[32], /* Scheme portion of URI */
912 userpass[256], /* Username/password portion of URI */
913 host[1024], /* Hostname portion of URI */
914 resource[1024], /* Resource portion of URI */
915 *options; /* Options in URI */
916 int port; /* Port number */
917
918
919 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text,
920 scheme, sizeof(scheme), userpass, sizeof(userpass),
921 host, sizeof(host), &port, resource, sizeof(resource));
922
923 if (!strcmp(scheme, "rss"))
924 {
925 /*
926 * RSS notification...
927 */
928
929 if ((options = strchr(resource, '?')) != NULL)
930 *options = '\0';
931
932 if (host[0])
933 {
934 /*
935 * Link to remote feed...
936 */
937
938 httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), "http",
939 userpass, host, port, resource);
940 strlcpy(name, uri, sizeof(name));
941 }
942 else
943 {
944 /*
945 * Link to local feed...
946 */
947
948 snprintf(uri, sizeof(uri), "/rss%s", resource);
949 strlcpy(name, resource + 1, sizeof(name));
950 }
951 }
952 else
953 {
954 /*
955 * Other...
956 */
957
958 strlcpy(uri, attr->values[0].string.text, sizeof(uri));
959 strlcpy(name, resource, sizeof(name));
960 }
961
962 cgiSetArray("notify_recipient_uri", element, uri);
963 cgiSetArray("notify_recipient_name", element, name);
964 continue;
965 }
966
967 /*
968 * Add "admin_uri" variable if we have a "printer_uri_supported"
969 * attribute...
970 */
971
972 if (!strcmp(name, "printer_uri_supported"))
973 {
974 cgiRewriteURL(attr->values[0].string.text, value, sizeof(value),
975 "/admin/");
976
977 cgiSetArray("admin_uri", element, value);
978 }
979
980 /*
981 * Copy values...
982 */
983
984 value[0] = '\0'; /* Initially an empty string */
985 valptr = value; /* Start at the beginning */
986
987 for (i = 0; i < attr->num_values; i ++)
988 {
989 if (i)
990 strlcat(valptr, ", ", sizeof(value) - (valptr - value));
991
992 valptr += strlen(valptr);
993
994 switch (attr->value_tag)
995 {
996 case IPP_TAG_INTEGER :
997 case IPP_TAG_ENUM :
998 if (strncmp(name, "time_at_", 8) == 0)
999 {
1000 time_t t; /* Temporary time value */
1001
1002 t = (time_t)attr->values[i].integer;
1003 date = localtime(&t);
1004
1005 strftime(valptr, sizeof(value) - (valptr - value), "%c", date);
1006 }
1007 else
1008 snprintf(valptr, sizeof(value) - (valptr - value),
1009 "%d", attr->values[i].integer);
1010 break;
1011
1012 case IPP_TAG_BOOLEAN :
1013 snprintf(valptr, sizeof(value) - (valptr - value),
1014 "%d", attr->values[i].boolean);
1015 break;
1016
1017 case IPP_TAG_NOVALUE :
1018 strlcat(valptr, "novalue", sizeof(value) - (valptr - value));
1019 break;
1020
1021 case IPP_TAG_RANGE :
1022 snprintf(valptr, sizeof(value) - (valptr - value),
1023 "%d-%d", attr->values[i].range.lower,
1024 attr->values[i].range.upper);
1025 break;
1026
1027 case IPP_TAG_RESOLUTION :
1028 snprintf(valptr, sizeof(value) - (valptr - value),
1029 "%dx%d%s", attr->values[i].resolution.xres,
1030 attr->values[i].resolution.yres,
1031 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
1032 "dpi" : "dpc");
1033 break;
1034
1035 case IPP_TAG_URI :
1036 if (strchr(attr->values[i].string.text, ':') &&
1037 strcmp(name, "device_uri"))
1038 {
1039 /*
1040 * Rewrite URIs...
1041 */
1042
1043 if (!strcmp(name, "member_uris"))
1044 {
1045 char url[1024]; /* URL for class member... */
1046
1047
1048 cgiRewriteURL(attr->values[i].string.text, url,
1049 sizeof(url), NULL);
1050
1051 snprintf(valptr, sizeof(value) - (valptr - value),
1052 "<A HREF=\"%s\">%s</A>", url,
1053 strrchr(attr->values[i].string.text, '/') + 1);
1054 }
1055 else
1056 cgiRewriteURL(attr->values[i].string.text, valptr,
1057 sizeof(value) - (valptr - value), NULL);
1058 break;
1059 }
1060
1061 case IPP_TAG_STRING :
1062 case IPP_TAG_TEXT :
1063 case IPP_TAG_NAME :
1064 case IPP_TAG_KEYWORD :
1065 case IPP_TAG_CHARSET :
1066 case IPP_TAG_LANGUAGE :
1067 case IPP_TAG_MIMETYPE :
1068 strlcat(valptr, attr->values[i].string.text,
1069 sizeof(value) - (valptr - value));
1070 break;
1071
1072 case IPP_TAG_BEGIN_COLLECTION :
1073 snprintf(value, sizeof(value), "%s%d", name, i + 1);
1074 cgiSetIPPVars(attr->values[i].collection, NULL, NULL, value,
1075 element);
1076 break;
1077
1078 default :
1079 break; /* anti-compiler-warning-code */
1080 }
1081 }
1082
1083 /*
1084 * Add the element...
1085 */
1086
1087 if (attr->value_tag != IPP_TAG_BEGIN_COLLECTION)
1088 {
1089 cgiSetArray(name, element, value);
1090
1091 fprintf(stderr, "DEBUG2: %s[%d]=\"%s\"\n", name, element, value);
1092 }
1093 }
1094
1095 return (attr ? attr->next : NULL);
1096 }
1097
1098
1099 /*
1100 * 'cgiSetIPPVars()' - Set CGI variables from an IPP response.
1101 */
1102
1103 int /* O - Maximum number of elements */
1104 cgiSetIPPVars(ipp_t *response, /* I - Response data to be copied... */
1105 const char *filter_name, /* I - Filter name */
1106 const char *filter_value, /* I - Filter value */
1107 const char *prefix, /* I - Prefix for name or NULL */
1108 int parent_el) /* I - Parent element number */
1109 {
1110 int element; /* Element in CGI array */
1111 ipp_attribute_t *attr, /* Attribute in response... */
1112 *filter; /* Filtering attribute */
1113
1114
1115 fprintf(stderr, "DEBUG2: cgiSetIPPVars(response=%p, filter_name=\"%s\", "
1116 "filter_value=\"%s\", prefix=\"%s\", parent_el=%d)\n",
1117 response, filter_name ? filter_name : "(null)",
1118 filter_value ? filter_value : "(null)",
1119 prefix ? prefix : "(null)", parent_el);
1120
1121 /*
1122 * Set common CGI template variables...
1123 */
1124
1125 if (!prefix)
1126 cgiSetServerVersion();
1127
1128 /*
1129 * Loop through the attributes and set them for the template...
1130 */
1131
1132 attr = response->attrs;
1133
1134 if (!prefix)
1135 while (attr && attr->group_tag == IPP_TAG_OPERATION)
1136 attr = attr->next;
1137
1138 for (element = parent_el; attr; element ++)
1139 {
1140 /*
1141 * Copy attributes to a separator...
1142 */
1143
1144 while (attr && attr->group_tag == IPP_TAG_ZERO)
1145 attr= attr->next;
1146
1147 if (!attr)
1148 break;
1149
1150 if (filter_name)
1151 {
1152 for (filter = attr;
1153 filter != NULL && filter->group_tag != IPP_TAG_ZERO;
1154 filter = filter->next)
1155 if (filter->name && !strcmp(filter->name, filter_name) &&
1156 (filter->value_tag == IPP_TAG_STRING ||
1157 (filter->value_tag >= IPP_TAG_TEXTLANG &&
1158 filter->value_tag <= IPP_TAG_MIMETYPE)) &&
1159 filter->values[0].string.text != NULL &&
1160 !strcasecmp(filter->values[0].string.text, filter_value))
1161 break;
1162
1163 if (!filter)
1164 return (element + 1);
1165
1166 if (filter->group_tag == IPP_TAG_ZERO)
1167 {
1168 attr = filter;
1169 element --;
1170 continue;
1171 }
1172 }
1173
1174 attr = cgiSetIPPObjectVars(attr, prefix, element);
1175 }
1176
1177 fprintf(stderr, "DEBUG2: Returing %d from cgiSetIPPVars()...\n", element);
1178
1179 return (element);
1180 }
1181
1182
1183 /*
1184 * 'cgiShowIPPError()' - Show the last IPP error message.
1185 *
1186 * The caller must still call cgiStartHTML() and cgiEndHTML().
1187 */
1188
1189 void
1190 cgiShowIPPError(const char *message) /* I - Contextual message */
1191 {
1192 cgiSetVariable("MESSAGE", cgiText(message));
1193 cgiSetVariable("ERROR", cupsLastErrorString());
1194 cgiCopyTemplateLang("error.tmpl");
1195 }
1196
1197
1198 /*
1199 * 'cgiShowJobs()' - Show print jobs.
1200 */
1201
1202 void
1203 cgiShowJobs(http_t *http, /* I - Connection to server */
1204 const char *dest) /* I - Destination name or NULL */
1205 {
1206 int i; /* Looping var */
1207 const char *which_jobs; /* Which jobs to show */
1208 ipp_t *request, /* IPP request */
1209 *response; /* IPP response */
1210 cups_array_t *jobs; /* Array of job objects */
1211 ipp_attribute_t *job; /* Job object */
1212 int ascending, /* Order of jobs (0 = descending) */
1213 first, /* First job to show */
1214 count; /* Number of jobs */
1215 const char *var; /* Form variable */
1216 void *search; /* Search data */
1217 char url[1024], /* Printer URI */
1218 val[1024]; /* Form variable */
1219
1220
1221 /*
1222 * Build an IPP_GET_JOBS request, which requires the following
1223 * attributes:
1224 *
1225 * attributes-charset
1226 * attributes-natural-language
1227 * printer-uri
1228 */
1229
1230 request = ippNewRequest(IPP_GET_JOBS);
1231
1232 if (dest)
1233 {
1234 httpAssembleURIf(HTTP_URI_CODING_ALL, url, sizeof(url), "ipp", NULL,
1235 "localhost", ippPort(), "/printers/%s", dest);
1236 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1237 NULL, url);
1238 }
1239 else
1240 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
1241 "ipp://localhost/jobs");
1242
1243 if ((which_jobs = cgiGetVariable("which_jobs")) != NULL)
1244 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs",
1245 NULL, which_jobs);
1246
1247 cgiGetAttributes(request, "jobs.tmpl");
1248
1249 /*
1250 * Do the request and get back a response...
1251 */
1252
1253 if ((response = cupsDoRequest(http, request, "/")) != NULL)
1254 {
1255 /*
1256 * Get a list of matching job objects.
1257 */
1258
1259 if ((var = cgiGetVariable("QUERY")) != NULL &&
1260 !cgiGetVariable("CLEAR"))
1261 search = cgiCompileSearch(var);
1262 else
1263 search = NULL;
1264
1265 jobs = cgiGetIPPObjects(response, search);
1266 count = cupsArrayCount(jobs);
1267
1268 if (search)
1269 cgiFreeSearch(search);
1270
1271 /*
1272 * Figure out which jobs to display...
1273 */
1274
1275 if ((var = cgiGetVariable("FIRST")) != NULL)
1276 first = atoi(var);
1277 else
1278 first = 0;
1279
1280 if (first >= count)
1281 first = count - CUPS_PAGE_MAX;
1282
1283 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
1284
1285 if (first < 0)
1286 first = 0;
1287
1288 sprintf(val, "%d", count);
1289 cgiSetVariable("TOTAL", val);
1290
1291 if ((var = cgiGetVariable("ORDER")) != NULL)
1292 ascending = !strcasecmp(var, "asc");
1293 else
1294 {
1295 ascending = !which_jobs || !strcasecmp(which_jobs, "not-completed");
1296 cgiSetVariable("ORDER", ascending ? "asc" : "dec");
1297 }
1298
1299 if (ascending)
1300 {
1301 for (i = 0, job = (ipp_attribute_t *)cupsArrayIndex(jobs, first);
1302 i < CUPS_PAGE_MAX && job;
1303 i ++, job = (ipp_attribute_t *)cupsArrayNext(jobs))
1304 cgiSetIPPObjectVars(job, NULL, i);
1305 }
1306 else
1307 {
1308 for (i = 0, job = (ipp_attribute_t *)cupsArrayIndex(jobs, count - first - 1);
1309 i < CUPS_PAGE_MAX && job;
1310 i ++, job = (ipp_attribute_t *)cupsArrayPrev(jobs))
1311 cgiSetIPPObjectVars(job, NULL, i);
1312 }
1313
1314 /*
1315 * Save navigation URLs...
1316 */
1317
1318 if (dest)
1319 snprintf(val, sizeof(val), "/%s/%s", cgiGetVariable("SECTION"), dest);
1320 else
1321 strlcpy(val, "/jobs/", sizeof(val));
1322
1323 cgiSetVariable("THISURL", val);
1324
1325 if (first > 0)
1326 {
1327 sprintf(val, "%d", first - CUPS_PAGE_MAX);
1328 cgiSetVariable("PREV", val);
1329 }
1330
1331 if ((first + CUPS_PAGE_MAX) < count)
1332 {
1333 sprintf(val, "%d", first + CUPS_PAGE_MAX);
1334 cgiSetVariable("NEXT", val);
1335 }
1336
1337 /*
1338 * Then show everything...
1339 */
1340
1341 if (dest)
1342 cgiSetVariable("SEARCH_DEST", dest);
1343
1344 cgiCopyTemplateLang("search.tmpl");
1345
1346 cgiCopyTemplateLang("jobs-header.tmpl");
1347
1348 if (count > 0)
1349 cgiCopyTemplateLang("pager.tmpl");
1350
1351 cgiCopyTemplateLang("jobs.tmpl");
1352
1353 if (count > 0)
1354 cgiCopyTemplateLang("pager.tmpl");
1355
1356 cupsArrayDelete(jobs);
1357 ippDelete(response);
1358 }
1359 }
1360
1361
1362 /*
1363 * 'cgiText()' - Return localized text.
1364 */
1365
1366 const char * /* O - Localized message */
1367 cgiText(const char *message) /* I - Message */
1368 {
1369 static cups_lang_t *language = NULL;
1370 /* Language */
1371
1372
1373 if (!language)
1374 language = cupsLangDefault();
1375
1376 return (_cupsLangString(language, message));
1377 }
1378
1379
1380 /*
1381 * End of "$Id: ipp-var.c 6889 2007-08-29 22:23:35Z mike $".
1382 */