]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/ipp-var.c
Mirror 1.1.x changes.
[thirdparty/cups.git] / cgi-bin / ipp-var.c
CommitLineData
13c1a6d9 1/*
dd63ebe2 2 * "$Id: ipp-var.c,v 1.23.2.10 2003/04/08 03:48:03 mike Exp $"
13c1a6d9 3 *
4 * IPP variable routines for the Common UNIX Printing System (CUPS).
5 *
1d9595ab 6 * Copyright 1997-2003 by Easy Software Products.
13c1a6d9 7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
31a80a7f 26 * ippGetTemplateDir() - Get the templates directory...
3d9e2586 27 * ippSetServerVersion() - Set the server name and CUPS version...
28 * ippSetCGIVars() - Set CGI variables from an IPP response.
13c1a6d9 29 */
30
31/*
32 * Include necessary headers...
33 */
34
35#include "ipp-var.h"
36
37
7e59c3e3 38/*
39 * 'ippGetTemplateDir()' - Get the templates directory...
40 */
41
42char * /* O - Template directory */
43ippGetTemplateDir(void)
44{
45 const char *datadir; /* CUPS_DATADIR env var */
46 static char templates[1024] = ""; /* Template directory */
47
48
49 if (!templates[0])
50 {
51 /*
52 * Build the template directory pathname...
53 */
54
55 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
56 datadir = CUPS_DATADIR;
57
58 snprintf(templates, sizeof(templates), "%s/templates", datadir);
59 }
60
61 return (templates);
62}
63
64
3d9e2586 65/*
66 * 'ippSetServerVersion()' - Set the server name and CUPS version...
67 */
68
69void
70ippSetServerVersion(void)
71{
72 cgiSetVariable("SERVER_NAME", getenv("SERVER_NAME"));
73 cgiSetVariable("REMOTE_USER", getenv("REMOTE_USER"));
74 cgiSetVariable("CUPS_VERSION", CUPS_SVERSION);
d0e4469d 75
76#ifdef LC_TIME
77 setlocale(LC_TIME, "");
78#endif /* LC_TIME */
3d9e2586 79}
80
81
13c1a6d9 82/*
83 * 'ippSetCGIVars()' - Set CGI variables from an IPP response.
84 */
85
86void
3d9e2586 87ippSetCGIVars(ipp_t *response, /* I - Response data to be copied... */
88 const char *filter_name, /* I - Filter name */
71e8876a 89 const char *filter_value, /* I - Filter value */
dd63ebe2 90 const char *prefix, /* I - Prefix for name or NULL */
91 int parent_el) /* I - Parent element number */
13c1a6d9 92{
93 int element; /* Element in CGI array */
3d9e2586 94 ipp_attribute_t *attr, /* Attribute in response... */
95 *filter; /* Filtering attribute */
13c1a6d9 96 int i; /* Looping var */
97 char name[1024], /* Name of attribute */
71e8876a 98 *nameptr, /* Pointer into name */
13c1a6d9 99 value[16384], /* Value(s) */
100 *valptr; /* Pointer into value */
100cd8c8 101 char method[HTTP_MAX_URI],
102 username[HTTP_MAX_URI],
103 hostname[HTTP_MAX_URI],
104 resource[HTTP_MAX_URI],
105 uri[HTTP_MAX_URI];
fa779d48 106 int port; /* URI data */
30d60919 107 int ishttps; /* Using encryption? */
aa66d75f 108 const char *server; /* Name of server */
5b28477f 109 char servername[1024];/* Locale server name */
358cc876 110 struct tm *date; /* Date information */
13c1a6d9 111
112
dd63ebe2 113 DEBUG_printf(("<P>ippSetCGIVars(response=%p, filter_name=\"%s\", filter_value=\"%s\", prefix=\"%s\")\n",
114 response, filter_name, filter_value, prefix));
115
5b28477f 116 /*
117 * Set common CGI template variables...
118 */
119
dd63ebe2 120 if (!prefix)
121 ippSetServerVersion();
13c1a6d9 122
5b28477f 123 /*
124 * Get the server name associated with the client interface as well as
125 * the locally configured hostname. We'll check *both* of these to
126 * see if the printer URL is local...
127 */
128
129 server = getenv("SERVER_NAME");
130 gethostname(servername, sizeof(servername));
131
132 /*
133 * Flag whether we are using SSL on this connection...
134 */
135
30d60919 136 ishttps = getenv("HTTPS") != NULL;
fa779d48 137
5b28477f 138 /*
139 * Loop through the attributes and set them for the template...
140 */
141
dd63ebe2 142 attr = response->attrs;
100cd8c8 143
dd63ebe2 144 if (!prefix)
145 while (attr && attr->group_tag == IPP_TAG_OPERATION)
146 attr = attr->next;
147
148 for (element = parent_el; attr != NULL; attr = attr->next, element ++)
13c1a6d9 149 {
150 /*
151 * Copy attributes to a separator...
152 */
153
3d9e2586 154 if (filter_name)
155 {
156 for (filter = attr;
157 filter != NULL && filter->group_tag != IPP_TAG_ZERO;
158 filter = filter->next)
159 if (filter->name && strcmp(filter->name, filter_name) == 0 &&
31a80a7f 160 (filter->value_tag == IPP_TAG_STRING ||
161 (filter->value_tag >= IPP_TAG_TEXTLANG &&
162 filter->value_tag <= IPP_TAG_MIMETYPE)) &&
163 filter->values[0].string.text != NULL &&
3d9e2586 164 strcasecmp(filter->values[0].string.text, filter_value) == 0)
165 break;
166
167 if (!filter)
168 return;
169
170 if (filter->group_tag == IPP_TAG_ZERO)
171 {
172 attr = filter;
173 element --;
174 continue;
175 }
176 }
177
13c1a6d9 178 for (; attr != NULL && attr->group_tag != IPP_TAG_ZERO; attr = attr->next)
179 {
180 /*
181 * Copy the attribute name, substituting "_" for "-"...
182 */
183
184 if (attr->name == NULL)
185 continue;
186
71e8876a 187 if (prefix)
188 {
189 snprintf(name, sizeof(name), "%s.", prefix);
190 nameptr = name + strlen(name);
191 }
192 else
193 nameptr = name;
194
195 for (i = 0; attr->name[i] && nameptr < (name + sizeof(name) - 1); i ++)
13c1a6d9 196 if (attr->name[i] == '-')
71e8876a 197 *nameptr++ = '_';
13c1a6d9 198 else
71e8876a 199 *nameptr++ = attr->name[i];
13c1a6d9 200
71e8876a 201 *nameptr = '\0';
13c1a6d9 202
df7507f5 203 /*
204 * Add "job_printer_name" variable if we have a "job_printer_uri"
205 * attribute...
206 */
207
208 if (strcmp(name, "job_printer_uri") == 0)
209 {
210 if ((valptr = strrchr(attr->values[0].string.text, '/')) == NULL)
211 valptr = "unknown";
212 else
213 valptr ++;
214
215 cgiSetArray("job_printer_name", element, valptr);
216 }
217
13c1a6d9 218 /*
219 * Copy values...
220 */
221
def978d5 222 value[0] = '\0'; /* Initially an empty string */
223 valptr = value; /* Start at the beginning */
13c1a6d9 224
225 for (i = 0; i < attr->num_values; i ++)
226 {
227 if (i)
def978d5 228 strlcat(valptr, ",", sizeof(value) - (valptr - value));
13c1a6d9 229
230 valptr += strlen(valptr);
231
232 switch (attr->value_tag)
233 {
234 case IPP_TAG_INTEGER :
235 case IPP_TAG_ENUM :
358cc876 236 if (strncmp(name, "time_at_", 8) == 0)
237 {
333f3652 238 date = localtime((time_t *)&(attr->values[i].integer));
0819478b 239 strftime(valptr, sizeof(value) - (valptr - value),
8a32fc08 240 CUPS_STRFTIME_FORMAT, date);
358cc876 241 }
242 else
0819478b 243 snprintf(valptr, sizeof(value) - (valptr - value),
244 "%d", attr->values[i].integer);
13c1a6d9 245 break;
246
247 case IPP_TAG_BOOLEAN :
0819478b 248 snprintf(valptr, sizeof(value) - (valptr - value),
249 "%d", attr->values[i].boolean);
f63a2256 250 break;
13c1a6d9 251
252 case IPP_TAG_NOVALUE :
def978d5 253 strlcat(valptr, "novalue", sizeof(value) - (valptr - value));
13c1a6d9 254 break;
255
256 case IPP_TAG_RANGE :
0819478b 257 snprintf(valptr, sizeof(value) - (valptr - value),
258 "%d-%d", attr->values[i].range.lower,
259 attr->values[i].range.upper);
13c1a6d9 260 break;
261
262 case IPP_TAG_RESOLUTION :
0819478b 263 snprintf(valptr, sizeof(value) - (valptr - value),
264 "%dx%d%s", attr->values[i].resolution.xres,
265 attr->values[i].resolution.yres,
266 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
267 "dpi" : "dpc");
13c1a6d9 268 break;
269
100cd8c8 270 case IPP_TAG_URI :
082b40d2 271 if (strchr(attr->values[i].string.text, ':') != NULL)
272 {
273 httpSeparate(attr->values[i].string.text, method, username,
274 hostname, &port, resource);
275
276 if (strcmp(method, "ipp") == 0 ||
277 strcmp(method, "http") == 0)
278 {
279 /*
5b28477f 280 * Map local access to a local URI...
082b40d2 281 */
282
5b28477f 283 if (strcasecmp(hostname, server) == 0 ||
284 strcasecmp(hostname, servername) == 0)
30d60919 285 {
7200e26c 286 /*
287 * Make URI relative to the current server...
288 */
082b40d2 289
def978d5 290 strlcpy(uri, resource, sizeof(uri));
7200e26c 291 }
292 else
293 {
294 /*
295 * Rewrite URI with HTTP address...
296 */
297
298 if (username[0])
299 snprintf(uri, sizeof(uri), "%s://%s@%s:%d%s",
300 ishttps ? "https" : "http",
301 username, hostname, port, resource);
302 else
303 snprintf(uri, sizeof(uri), "%s://%s:%d%s",
304 ishttps ? "https" : "http",
305 hostname, port, resource);
306 }
082b40d2 307
def978d5 308 strlcat(valptr, uri, sizeof(value) - (valptr - value));
082b40d2 309 break;
310 }
fa779d48 311 }
100cd8c8 312
13c1a6d9 313 case IPP_TAG_STRING :
314 case IPP_TAG_TEXT :
315 case IPP_TAG_NAME :
316 case IPP_TAG_KEYWORD :
317 case IPP_TAG_CHARSET :
318 case IPP_TAG_LANGUAGE :
dd63ebe2 319 case IPP_TAG_MIMETYPE :
def978d5 320 strlcat(valptr, attr->values[i].string.text,
321 sizeof(value) - (valptr - value));
13c1a6d9 322 break;
d21a7597 323
71e8876a 324 case IPP_TAG_BEGIN_COLLECTION :
dd63ebe2 325 snprintf(value, sizeof(value), "%s%d", name, i + 1);
71e8876a 326 ippSetCGIVars(attr->values[i].collection, filter_name,
dd63ebe2 327 filter_value, value, element);
71e8876a 328 break;
329
d21a7597 330 default :
331 break; /* anti-compiler-warning-code */
13c1a6d9 332 }
333 }
334
335 /*
336 * Add the element...
337 */
338
71e8876a 339 if (attr->value_tag != IPP_TAG_BEGIN_COLLECTION)
dd63ebe2 340 {
71e8876a 341 cgiSetArray(name, element, value);
0a968cfb 342
dd63ebe2 343 DEBUG_printf(("<P>%s[%d]=\"%s\"\n", name, element, value));
344 }
13c1a6d9 345 }
346
347 if (attr == NULL)
348 break;
349 }
dd63ebe2 350
351 DEBUG_puts("<P>Leaving ippSetCGIVars()...");
13c1a6d9 352}
353
354
355/*
dd63ebe2 356 * End of "$Id: ipp-var.c,v 1.23.2.10 2003/04/08 03:48:03 mike Exp $".
13c1a6d9 357 */