]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/ipp-var.c
2 * "$Id: ipp-var.c 5572 2006-05-22 18:47:09Z mike $"
4 * CGI <-> IPP variable routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cgiGetAttributes() - Get the list of attributes that are needed
27 * by the template file.
28 * cgiGetIPPObjects() - Get the objects in an IPP response.
29 * cgiMoveJobs() - Move one or more jobs.
30 * cgiPrintTestPage() - Print a test page.
31 * cgiRewriteURL() - Rewrite a printer URI into a web browser URL...
32 * cgiSetIPPObjectVars() - Set CGI variables from an IPP object.
33 * cgiSetIPPVars() - Set CGI variables from an IPP response.
34 * cgiShowIPPError() - Show the last IPP error message.
35 * cgiShowJobs() - Show print jobs.
36 * cgiText() - Return localized text.
40 * Include necessary headers...
43 #include "cgi-private.h"
47 * 'cgiGetAttributes()' - Get the list of attributes that are needed
48 * by the template file.
52 cgiGetAttributes(ipp_t
*request
, /* I - IPP request */
53 const char *tmpl
) /* I - Base filename */
55 int num_attrs
; /* Number of attributes */
56 char *attrs
[1000]; /* Attributes */
57 int i
; /* Looping var */
58 char filename
[1024], /* Filename */
59 locale
[16]; /* Locale name */
60 const char *directory
, /* Directory */
62 FILE *in
; /* Input file */
63 int ch
; /* Character from file */
64 char name
[255], /* Name of variable */
65 *nameptr
; /* Pointer into name */
69 * Convert the language to a locale name...
72 if ((lang
= getenv("LANG")) != NULL
)
74 for (i
= 0; lang
[i
] && i
< 15; i
++)
75 if (isalnum(lang
[i
] & 255))
76 locale
[i
] = tolower(lang
[i
]);
86 * See if we have a template file for this language...
89 directory
= cgiGetTemplateDir();
91 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
92 if (access(filename
, 0))
96 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
97 if (access(filename
, 0))
98 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
102 * Open the template file...
105 if ((in
= fopen(filename
, "r")) == NULL
)
109 * Loop through the file adding attribute names as needed...
114 while ((ch
= getc(in
)) != EOF
)
117 else if (ch
== '{' && num_attrs
< (sizeof(attrs
) / sizeof(attrs
[0])))
123 for (nameptr
= name
; (ch
= getc(in
)) != EOF
;)
124 if (strchr("}]<>=! \t\n", ch
))
126 else if (nameptr
> name
&& ch
== '?')
128 else if (nameptr
< (name
+ sizeof(name
) - 1))
138 if (!strncmp(name
, "printer_state_history", 21))
139 strcpy(name
, "printer_state_history");
142 * Possibly add it to the list of attributes...
145 for (i
= 0; i
< num_attrs
; i
++)
146 if (!strcmp(attrs
[i
], name
))
151 attrs
[num_attrs
] = strdup(name
);
157 * If we have attributes, add a requested-attributes attribute to the
163 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
164 "requested-attributes", num_attrs
, NULL
, (const char **)attrs
);
166 for (i
= 0; i
< num_attrs
; i
++)
173 * 'cgiGetIPPObjects()' - Get the objects in an IPP response.
176 cups_array_t
* /* O - Array of objects */
177 cgiGetIPPObjects(ipp_t
*response
, /* I - IPP response */
178 void *search
) /* I - Search filter */
180 int i
; /* Looping var */
181 cups_array_t
*objs
; /* Array of objects */
182 ipp_attribute_t
*attr
, /* Current attribute */
183 *first
; /* First attribute for object */
184 ipp_tag_t group
; /* Current group tag */
185 int add
; /* Add this object to the array? */
191 for (add
= 0, first
= NULL
, objs
= cupsArrayNew(NULL
, NULL
),
192 group
= IPP_TAG_ZERO
, attr
= response
->attrs
;
196 if (attr
->group_tag
!= group
)
198 group
= attr
->group_tag
;
200 if (group
!= IPP_TAG_ZERO
&& group
!= IPP_TAG_OPERATION
)
205 else if (add
&& first
)
207 cupsArrayAdd(objs
, first
);
214 if (attr
->name
&& attr
->group_tag
!= IPP_TAG_OPERATION
&& !add
)
219 * Add all objects if there is no search...
227 * Check the search string against the string and integer values.
230 switch (attr
->value_tag
)
232 case IPP_TAG_TEXTLANG
:
233 case IPP_TAG_NAMELANG
:
236 case IPP_TAG_KEYWORD
:
238 case IPP_TAG_MIMETYPE
:
239 for (i
= 0; !add
&& i
< attr
->num_values
; i
++)
240 if (cgiDoSearch(search
, attr
->values
[i
].string
.text
))
244 case IPP_TAG_INTEGER
:
245 for (i
= 0; !add
&& i
< attr
->num_values
; i
++)
247 char buf
[255]; /* Number buffer */
250 sprintf(buf
, "%d", attr
->values
[i
].integer
);
252 if (cgiDoSearch(search
, buf
))
265 cupsArrayAdd(objs
, first
);
272 * 'cgiMoveJobs()' - Move one or more jobs.
274 * At least one of dest or job_id must be non-zero/NULL.
278 cgiMoveJobs(http_t
*http
, /* I - Connection to server */
279 const char *dest
, /* I - Destination or NULL */
280 int job_id
) /* I - Job ID or 0 for all */
282 int i
; /* Looping var */
283 const char *user
; /* Username */
284 ipp_t
*request
, /* IPP request */
285 *response
; /* IPP response */
286 ipp_attribute_t
*attr
; /* Current attribute */
287 const char *name
; /* Destination name */
288 const char *job_printer_uri
; /* JOB_PRINTER_URI form variable */
289 char current_dest
[1024]; /* Current destination */
293 * See who is logged in...
296 if ((user
= getenv("REMOTE_USER")) == NULL
)
300 * See if the user has already selected a new destination...
303 if ((job_printer_uri
= cgiGetVariable("JOB_PRINTER_URI")) == NULL
)
306 * Make sure necessary form variables are set...
311 char temp
[255]; /* Temporary string */
314 sprintf(temp
, "%d", job_id
);
315 cgiSetVariable("JOB_ID", temp
);
319 cgiSetVariable("PRINTER_NAME", dest
);
322 * No new destination specified, show the user what the available
323 * printers/classes are...
329 * Get the current destination for job N...
332 char job_uri
[1024]; /* Job URI */
335 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
337 snprintf(job_uri
, sizeof(job_uri
), "ipp://localhost/jobs/%d", job_id
);
338 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
340 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
341 "requested-attributes", NULL
, "job-printer-uri");
343 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
345 if ((attr
= ippFindAttribute(response
, "job-printer-uri",
346 IPP_TAG_URI
)) != NULL
)
349 * Pull the name from the URI...
352 strlcpy(current_dest
, strrchr(attr
->values
[0].string
.text
, '/') + 1,
353 sizeof(current_dest
));
363 * Couldn't get the current destination...
366 cgiStartHTML(cgiText(_("Move Job")));
367 cgiShowIPPError(_("Unable to find destination for job!"));
374 * Get the list of available destinations...
377 request
= ippNewRequest(CUPS_GET_PRINTERS
);
379 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
380 "requested-attributes", NULL
, "printer-uri-supported");
382 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
383 "requesting-user-name", NULL
, user
);
385 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
387 for (i
= 0, attr
= ippFindAttribute(response
, "printer-uri-supported",
390 attr
= ippFindNextAttribute(response
, "printer-uri-supported",
394 * Pull the name from the URI...
397 name
= strrchr(attr
->values
[0].string
.text
, '/') + 1;
400 * If the name is not the same as the current destination, add it!
403 if (strcasecmp(name
, dest
))
405 cgiSetArray("JOB_PRINTER_URI", i
, attr
->values
[0].string
.text
);
406 cgiSetArray("JOB_PRINTER_NAME", i
, name
);
419 cgiStartHTML(cgiText(_("Move Job")));
421 cgiStartHTML(cgiText(_("Move All Jobs")));
423 cgiCopyTemplateLang("job-move.tmpl");
428 * Try moving the job or jobs...
431 char uri
[1024], /* Job/printer URI */
432 resource
[1024], /* Post resource */
433 refresh
[1024]; /* Refresh URL */
434 const char *job_printer_name
; /* New printer name */
437 request
= ippNewRequest(CUPS_MOVE_JOB
);
445 snprintf(resource
, sizeof(resource
), "/jobs/%d", job_id
);
447 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", job_id
);
448 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
454 * Move all active jobs on a destination...
457 snprintf(resource
, sizeof(resource
), "/%s/%s",
458 cgiGetVariable("SECTION"), dest
);
460 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
461 "localhost", ippPort(), "/%s/%s",
462 cgiGetVariable("SECTION"), dest
);
463 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
467 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-printer-uri",
468 NULL
, job_printer_uri
);
470 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
471 "requesting-user-name", NULL
, user
);
473 ippDelete(cupsDoRequest(http
, request
, resource
));
476 * Show the results...
479 job_printer_name
= strrchr(job_printer_uri
, '/') + 1;
481 if (cupsLastError() <= IPP_OK_CONFLICT
)
483 cgiRewriteURL(job_printer_uri
, resource
, sizeof(resource
), NULL
);
484 cgiFormEncode(uri
, resource
, sizeof(uri
));
485 snprintf(refresh
, sizeof(refresh
), "2;URL=%s", uri
);
486 cgiSetVariable("refresh_page", refresh
);
490 cgiStartHTML(cgiText(_("Move Job")));
492 cgiStartHTML(cgiText(_("Move All Jobs")));
494 if (cupsLastError() > IPP_OK_CONFLICT
)
497 cgiShowIPPError(_("Unable to move job"));
499 cgiShowIPPError(_("Unable to move jobs"));
503 cgiSetVariable("JOB_PRINTER_NAME", job_printer_name
);
504 cgiCopyTemplateLang("job-moved.tmpl");
513 * 'cgiPrintTestPage()' - Print a test page.
517 cgiPrintTestPage(http_t
*http
, /* I - Connection to server */
518 const char *dest
) /* I - Destination printer/class */
520 ipp_t
*request
, /* IPP request */
521 *response
; /* IPP response */
522 char uri
[HTTP_MAX_URI
], /* Printer URI */
523 resource
[1024], /* POST resource path */
524 refresh
[1024], /* Refresh URL */
525 filename
[1024]; /* Test page filename */
526 const char *datadir
; /* CUPS_DATADIR env var */
527 const char *user
; /* Username */
531 * See who is logged in...
534 if ((user
= getenv("REMOTE_USER")) == NULL
)
538 * Locate the test page file...
541 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
542 datadir
= CUPS_DATADIR
;
544 snprintf(filename
, sizeof(filename
), "%s/data/testprint.ps", datadir
);
547 * Point to the printer/class...
550 snprintf(resource
, sizeof(resource
), "/%s/%s", cgiGetVariable("SECTION"),
553 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
554 "localhost", ippPort(), "/%s/%s", cgiGetVariable("SECTION"),
558 * Build an IPP_PRINT_JOB request, which requires the following
562 * attributes-natural-language
564 * requesting-user-name
568 request
= ippNewRequest(IPP_PRINT_JOB
);
570 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
573 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
574 "requesting-user-name", NULL
, user
);
576 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
579 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
580 NULL
, "application/postscript");
583 * Do the request and get back a response...
586 if ((response
= cupsDoFileRequest(http
, request
, resource
,
589 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
594 if (cupsLastError() <= IPP_OK_CONFLICT
)
597 * Automatically reload the printer status page...
600 cgiFormEncode(uri
, resource
, sizeof(uri
));
601 snprintf(refresh
, sizeof(refresh
), "2;URL=%s", uri
);
602 cgiSetVariable("refresh_page", refresh
);
605 cgiStartHTML(cgiText(_("Print Test Page")));
607 if (cupsLastError() > IPP_OK_CONFLICT
)
608 cgiShowIPPError(_("Unable to print test page:"));
611 cgiSetVariable("PRINTER_NAME", dest
);
613 cgiCopyTemplateLang("test-page.tmpl");
621 * 'cgiRewriteURL()' - Rewrite a printer URI into a web browser URL...
624 char * /* O - New URL */
625 cgiRewriteURL(const char *uri
, /* I - Current URI */
626 char *url
, /* O - New URL */
627 int urlsize
, /* I - Size of URL buffer */
628 const char *newresource
) /* I - Replacement resource */
630 char method
[HTTP_MAX_URI
],
631 userpass
[HTTP_MAX_URI
],
632 hostname
[HTTP_MAX_URI
],
633 rawresource
[HTTP_MAX_URI
],
634 resource
[HTTP_MAX_URI
],
635 /* URI components... */
636 *rawptr
, /* Pointer into rawresource */
637 *resptr
; /* Pointer into resource */
638 int port
; /* Port number */
639 static int ishttps
= -1; /* Using encryption? */
640 static const char *server
; /* Name of server */
641 static char servername
[1024];
642 /* Local server name */
643 static const char hexchars
[] = "0123456789ABCDEF";
644 /* Hexadecimal conversion characters */
648 * Check if we have been called before...
654 * No, initialize static vars for the conversion...
656 * First get the server name associated with the client interface as
657 * well as the locally configured hostname. We'll check *both* of
658 * these to see if the printer URL is local...
661 if ((server
= getenv("SERVER_NAME")) == NULL
)
664 httpGetHostname(NULL
, servername
, sizeof(servername
));
667 * Then flag whether we are using SSL on this connection...
670 ishttps
= getenv("HTTPS") != NULL
;
674 * Convert the URI to a URL...
677 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), userpass
,
678 sizeof(userpass
), hostname
, sizeof(hostname
), &port
,
679 rawresource
, sizeof(rawresource
));
681 if (!strcmp(method
, "ipp") ||
682 !strcmp(method
, "http") ||
683 !strcmp(method
, "https"))
688 * Force the specified resource name instead of the one in the URL...
691 strlcpy(resource
, newresource
, sizeof(resource
));
696 * Rewrite the resource string so it doesn't contain any
700 for (rawptr
= rawresource
, resptr
= resource
; *rawptr
; rawptr
++)
701 if ((*rawptr
& 128) || *rawptr
== '%' || *rawptr
== ' ' ||
702 *rawptr
== '#' || *rawptr
== '?' ||
703 *rawptr
== '.') /* For MSIE */
705 if (resptr
< (resource
+ sizeof(resource
) - 3))
708 *resptr
++ = hexchars
[(*rawptr
>> 4) & 15];
709 *resptr
++ = hexchars
[*rawptr
& 15];
712 else if (resptr
< (resource
+ sizeof(resource
) - 1))
719 * Map local access to a local URI...
722 if (!strcasecmp(hostname
, "localhost") ||
723 !strncasecmp(hostname
, "localhost.", 10) ||
724 !strcasecmp(hostname
, server
) ||
725 !strcasecmp(hostname
, servername
))
728 * Make URI relative to the current server...
731 strlcpy(url
, resource
, urlsize
);
736 * Rewrite URI with HTTP/HTTPS scheme...
740 snprintf(url
, urlsize
, "%s://%s@%s:%d%s",
741 ishttps
? "https" : "http",
742 userpass
, hostname
, port
, resource
);
744 snprintf(url
, urlsize
, "%s://%s:%d%s",
745 ishttps
? "https" : "http",
746 hostname
, port
, resource
);
750 strlcpy(url
, uri
, urlsize
);
757 * 'cgiSetIPPObjectVars()' - Set CGI variables from an IPP object.
760 ipp_attribute_t
* /* O - Next object */
762 ipp_attribute_t
*obj
, /* I - Response data to be copied... */
763 const char *prefix
, /* I - Prefix for name or NULL */
764 int element
) /* I - Parent element number */
766 ipp_attribute_t
*attr
; /* Attribute in response... */
767 int i
; /* Looping var */
768 char name
[1024], /* Name of attribute */
769 *nameptr
, /* Pointer into name */
770 value
[16384], /* Value(s) */
771 *valptr
; /* Pointer into value */
772 struct tm
*date
; /* Date information */
775 fprintf(stderr
, "DEBUG2: cgiSetIPPObjectVars(obj=%p, prefix=\"%s\", "
777 obj
, prefix
? prefix
: "(null)", element
);
780 * Set common CGI template variables...
784 cgiSetServerVersion();
787 * Loop through the attributes and set them for the template...
790 for (attr
= obj
; attr
&& attr
->group_tag
!= IPP_TAG_ZERO
; attr
= attr
->next
)
793 * Copy the attribute name, substituting "_" for "-"...
801 snprintf(name
, sizeof(name
), "%s.", prefix
);
802 nameptr
= name
+ strlen(name
);
807 for (i
= 0; attr
->name
[i
] && nameptr
< (name
+ sizeof(name
) - 1); i
++)
808 if (attr
->name
[i
] == '-')
811 *nameptr
++ = attr
->name
[i
];
816 * Add "job_printer_name" variable if we have a "job_printer_uri"
820 if (!strcmp(name
, "job_printer_uri"))
822 if ((valptr
= strrchr(attr
->values
[0].string
.text
, '/')) == NULL
)
827 cgiSetArray("job_printer_name", element
, valptr
);
831 * Add "admin_uri" variable if we have a "printer_uri_supported"
835 if (!strcmp(name
, "printer_uri_supported"))
837 cgiRewriteURL(attr
->values
[0].string
.text
, value
, sizeof(value
),
840 cgiSetArray("admin_uri", element
, value
);
847 value
[0] = '\0'; /* Initially an empty string */
848 valptr
= value
; /* Start at the beginning */
850 for (i
= 0; i
< attr
->num_values
; i
++)
853 strlcat(valptr
, ",", sizeof(value
) - (valptr
- value
));
855 valptr
+= strlen(valptr
);
857 switch (attr
->value_tag
)
859 case IPP_TAG_INTEGER
:
861 if (strncmp(name
, "time_at_", 8) == 0)
863 time_t t
; /* Temporary time value */
865 t
= (time_t)attr
->values
[i
].integer
;
866 date
= localtime(&t
);
868 strftime(valptr
, sizeof(value
) - (valptr
- value
), "%c", date
);
871 snprintf(valptr
, sizeof(value
) - (valptr
- value
),
872 "%d", attr
->values
[i
].integer
);
875 case IPP_TAG_BOOLEAN
:
876 snprintf(valptr
, sizeof(value
) - (valptr
- value
),
877 "%d", attr
->values
[i
].boolean
);
880 case IPP_TAG_NOVALUE
:
881 strlcat(valptr
, "novalue", sizeof(value
) - (valptr
- value
));
885 snprintf(valptr
, sizeof(value
) - (valptr
- value
),
886 "%d-%d", attr
->values
[i
].range
.lower
,
887 attr
->values
[i
].range
.upper
);
890 case IPP_TAG_RESOLUTION
:
891 snprintf(valptr
, sizeof(value
) - (valptr
- value
),
892 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
893 attr
->values
[i
].resolution
.yres
,
894 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
899 if (strchr(attr
->values
[i
].string
.text
, ':') &&
900 strcmp(name
, "device_uri"))
906 if (!strcmp(name
, "member_uris"))
908 char url
[1024]; /* URL for class member... */
911 cgiRewriteURL(attr
->values
[i
].string
.text
, url
,
914 snprintf(valptr
, sizeof(value
) - (valptr
- value
),
915 "<A HREF=\"%s\">%s</A>", url
,
916 strrchr(url
, '/') + 1);
919 cgiRewriteURL(attr
->values
[i
].string
.text
, valptr
,
920 sizeof(value
) - (valptr
- value
), NULL
);
924 case IPP_TAG_STRING
:
927 case IPP_TAG_KEYWORD
:
928 case IPP_TAG_CHARSET
:
929 case IPP_TAG_LANGUAGE
:
930 case IPP_TAG_MIMETYPE
:
931 strlcat(valptr
, attr
->values
[i
].string
.text
,
932 sizeof(value
) - (valptr
- value
));
935 case IPP_TAG_BEGIN_COLLECTION
:
936 snprintf(value
, sizeof(value
), "%s%d", name
, i
+ 1);
937 cgiSetIPPVars(attr
->values
[i
].collection
, NULL
, NULL
, value
,
942 break; /* anti-compiler-warning-code */
950 if (attr
->value_tag
!= IPP_TAG_BEGIN_COLLECTION
)
952 cgiSetArray(name
, element
, value
);
954 fprintf(stderr
, "DEBUG2: %s[%d]=\"%s\"\n", name
, element
, value
);
958 return (attr
? attr
->next
: NULL
);
963 * 'cgiSetIPPVars()' - Set CGI variables from an IPP response.
966 int /* O - Maximum number of elements */
967 cgiSetIPPVars(ipp_t
*response
, /* I - Response data to be copied... */
968 const char *filter_name
, /* I - Filter name */
969 const char *filter_value
, /* I - Filter value */
970 const char *prefix
, /* I - Prefix for name or NULL */
971 int parent_el
) /* I - Parent element number */
973 int element
; /* Element in CGI array */
974 ipp_attribute_t
*attr
, /* Attribute in response... */
975 *filter
; /* Filtering attribute */
978 fprintf(stderr
, "DEBUG2: cgiSetIPPVars(response=%p, filter_name=\"%s\", "
979 "filter_value=\"%s\", prefix=\"%s\", parent_el=%d)\n",
980 response
, filter_name
? filter_name
: "(null)",
981 filter_value
? filter_value
: "(null)",
982 prefix
? prefix
: "(null)", parent_el
);
985 * Set common CGI template variables...
989 cgiSetServerVersion();
992 * Loop through the attributes and set them for the template...
995 attr
= response
->attrs
;
998 while (attr
&& attr
->group_tag
== IPP_TAG_OPERATION
)
1001 for (element
= parent_el
; attr
; element
++)
1004 * Copy attributes to a separator...
1007 while (attr
&& attr
->group_tag
== IPP_TAG_ZERO
)
1016 filter
!= NULL
&& filter
->group_tag
!= IPP_TAG_ZERO
;
1017 filter
= filter
->next
)
1018 if (filter
->name
&& !strcmp(filter
->name
, filter_name
) &&
1019 (filter
->value_tag
== IPP_TAG_STRING
||
1020 (filter
->value_tag
>= IPP_TAG_TEXTLANG
&&
1021 filter
->value_tag
<= IPP_TAG_MIMETYPE
)) &&
1022 filter
->values
[0].string
.text
!= NULL
&&
1023 !strcasecmp(filter
->values
[0].string
.text
, filter_value
))
1027 return (element
+ 1);
1029 if (filter
->group_tag
== IPP_TAG_ZERO
)
1037 attr
= cgiSetIPPObjectVars(attr
, prefix
, element
);
1040 fprintf(stderr
, "DEBUG2: Returing %d from cgiSetIPPVars()...\n", element
);
1047 * 'cgiShowIPPError()' - Show the last IPP error message.
1049 * The caller must still call cgiStartHTML() and cgiEndHTML().
1053 cgiShowIPPError(const char *message
) /* I - Contextual message */
1055 cgiSetVariable("MESSAGE", cgiText(message
));
1056 cgiSetVariable("ERROR", cupsLastErrorString());
1057 cgiCopyTemplateLang("error.tmpl");
1062 * 'cgiShowJobs()' - Show print jobs.
1066 cgiShowJobs(http_t
*http
, /* I - Connection to server */
1067 const char *dest
) /* I - Destination name or NULL */
1069 int i
; /* Looping var */
1070 const char *which_jobs
; /* Which jobs to show */
1071 ipp_t
*request
, /* IPP request */
1072 *response
; /* IPP response */
1073 cups_array_t
*jobs
; /* Array of job objects */
1074 ipp_attribute_t
*job
; /* Job object */
1075 int ascending
, /* Order of jobs (0 = descending) */
1076 first
, /* First job to show */
1077 count
; /* Number of jobs */
1078 const char *var
; /* Form variable */
1079 void *search
; /* Search data */
1080 char url
[1024], /* URL for prev/next/this */
1081 *urlptr
, /* Position in URL */
1082 *urlend
; /* End of URL */
1086 * Build an IPP_GET_JOBS request, which requires the following
1089 * attributes-charset
1090 * attributes-natural-language
1094 request
= ippNewRequest(IPP_GET_JOBS
);
1098 httpAssembleURIf(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "ipp", NULL
,
1099 "localhost", ippPort(), "/printers/%s", dest
);
1100 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1104 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
,
1105 "ipp://localhost/jobs");
1107 if ((which_jobs
= cgiGetVariable("which_jobs")) != NULL
)
1108 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "which-jobs",
1111 cgiGetAttributes(request
, "jobs.tmpl");
1114 * Do the request and get back a response...
1117 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1120 * Get a list of matching job objects.
1123 if ((var
= cgiGetVariable("QUERY")) != NULL
)
1124 search
= cgiCompileSearch(var
);
1128 jobs
= cgiGetIPPObjects(response
, search
);
1129 count
= cupsArrayCount(jobs
);
1132 cgiFreeSearch(search
);
1135 * Figure out which jobs to display...
1138 if ((var
= cgiGetVariable("FIRST")) != NULL
)
1144 first
= count
- CUPS_PAGE_MAX
;
1146 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
1151 sprintf(url
, "%d", count
);
1152 cgiSetVariable("TOTAL", url
);
1154 if ((var
= cgiGetVariable("ORDER")) != NULL
)
1155 ascending
= !strcasecmp(var
, "asc");
1158 ascending
= !which_jobs
|| !strcasecmp(which_jobs
, "not-completed");
1159 cgiSetVariable("ORDER", ascending
? "asc" : "dec");
1164 for (i
= 0, job
= (ipp_attribute_t
*)cupsArrayIndex(jobs
, first
);
1165 i
< CUPS_PAGE_MAX
&& job
;
1166 i
++, job
= (ipp_attribute_t
*)cupsArrayNext(jobs
))
1167 cgiSetIPPObjectVars(job
, NULL
, i
);
1171 for (i
= 0, job
= (ipp_attribute_t
*)cupsArrayIndex(jobs
, count
- first
- 1);
1172 i
< CUPS_PAGE_MAX
&& job
;
1173 i
++, job
= (ipp_attribute_t
*)cupsArrayPrev(jobs
))
1174 cgiSetIPPObjectVars(job
, NULL
, i
);
1178 * Save navigation URLs...
1181 urlend
= url
+ sizeof(url
);
1183 if ((var
= cgiGetVariable("QUERY")) != NULL
)
1186 snprintf(url
, sizeof(url
), "/%s/%s?QUERY=", cgiGetVariable("SECTION"),
1189 strlcpy(url
, "/jobs/?QUERY=", sizeof(url
));
1191 urlptr
= url
+ strlen(url
);
1193 cgiFormEncode(urlptr
, var
, urlend
- urlptr
);
1194 urlptr
+= strlen(urlptr
);
1196 strlcpy(urlptr
, "&", urlend
- urlptr
);
1197 urlptr
+= strlen(urlptr
);
1202 snprintf(url
, sizeof(url
), "/%s/%s?", cgiGetVariable("SECTION"), dest
);
1204 strlcpy(url
, "/jobs/?", sizeof(url
));
1206 urlptr
= url
+ strlen(url
);
1211 strlcpy(urlptr
, "WHICH_JOBS=", urlend
- urlptr
);
1212 urlptr
+= strlen(urlptr
);
1214 cgiFormEncode(urlptr
, which_jobs
, urlend
- urlptr
);
1215 urlptr
+= strlen(urlptr
);
1217 strlcpy(urlptr
, "&", urlend
- urlptr
);
1218 urlptr
+= strlen(urlptr
);
1221 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d", first
);
1222 cgiSetVariable("THISURL", url
);
1226 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
1227 first
- CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
1228 cgiSetVariable("PREVURL", url
);
1231 if ((first
+ CUPS_PAGE_MAX
) < count
)
1233 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
1234 first
+ CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
1235 cgiSetVariable("NEXTURL", url
);
1239 * Then show everything...
1243 cgiSetVariable("SEARCH_DEST", dest
);
1245 cgiCopyTemplateLang("search.tmpl");
1247 cgiCopyTemplateLang("jobs-header.tmpl");
1250 cgiCopyTemplateLang("pager.tmpl");
1252 cgiCopyTemplateLang("jobs.tmpl");
1255 cgiCopyTemplateLang("pager.tmpl");
1257 cupsArrayDelete(jobs
);
1258 ippDelete(response
);
1264 * 'cgiText()' - Return localized text.
1267 const char * /* O - Localized message */
1268 cgiText(const char *message
) /* I - Message */
1270 static cups_lang_t
*language
= NULL
;
1275 language
= cupsLangDefault();
1277 return (_cupsLangString(language
, message
));
1282 * End of "$Id: ipp-var.c 5572 2006-05-22 18:47:09Z mike $".