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