]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/template.c
2 * CGI template function.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
10 #include "cgi-private.h"
19 static void cgi_copy(FILE *out
, FILE *in
, int element
, char term
,
21 static void cgi_puts(const char *s
, FILE *out
);
22 static void cgi_puturi(const char *s
, FILE *out
);
26 * 'cgiCopyTemplateFile()' - Copy a template file and replace all the
27 * '{variable}' strings with the variable value.
31 cgiCopyTemplateFile(FILE *out
, /* I - Output file */
32 const char *tmpl
) /* I - Template file to read */
34 FILE *in
; /* Input file */
36 fprintf(stderr
, "DEBUG2: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out
,
37 tmpl
? tmpl
: "(null)");
40 * Range check input...
47 * Open the template file...
50 if ((in
= fopen(tmpl
, "r")) == NULL
)
52 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
53 tmpl
, strerror(errno
));
58 * Parse the file to the end...
61 cgi_copy(out
, in
, 0, 0, 0);
64 * Close the template file and return...
72 * 'cgiCopyTemplateLang()' - Copy a template file using a language...
76 cgiCopyTemplateLang(const char *tmpl
) /* I - Base filename */
78 char filename
[1024], /* Filename */
79 locale
[16], /* Locale name */
80 *locptr
; /* Pointer into locale name */
81 const char *directory
, /* Directory for templates */
83 FILE *in
; /* Input file */
86 fprintf(stderr
, "DEBUG2: cgiCopyTemplateLang(tmpl=\"%s\")\n",
87 tmpl
? tmpl
: "(null)");
90 * Convert the language to a locale name...
93 if ((lang
= getenv("LANG")) != NULL
)
96 strlcpy(locale
+ 1, lang
, sizeof(locale
) - 1);
98 if ((locptr
= strchr(locale
, '.')) != NULL
)
99 *locptr
= '\0'; /* Strip charset */
106 fprintf(stderr
, "DEBUG2: lang=\"%s\", locale=\"%s\"...\n",
107 lang
? lang
: "(null)", locale
);
110 * See if we have a template file for this language...
113 directory
= cgiGetTemplateDir();
115 snprintf(filename
, sizeof(filename
), "%s%s/%s", directory
, locale
, tmpl
);
116 if ((in
= fopen(filename
, "r")) == NULL
)
120 snprintf(filename
, sizeof(filename
), "%s%s/%s", directory
, locale
, tmpl
);
121 if ((in
= fopen(filename
, "r")) == NULL
)
123 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
124 in
= fopen(filename
, "r");
128 fprintf(stderr
, "DEBUG2: Template file is \"%s\"...\n", filename
);
131 * Open the template file...
136 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
137 filename
, strerror(errno
));
142 * Parse the file to the end...
145 cgi_copy(stdout
, in
, 0, 0, 0);
148 * Close the template file and return...
156 * 'cgiGetTemplateDir()' - Get the templates directory...
159 char * /* O - Template directory */
160 cgiGetTemplateDir(void)
162 const char *datadir
; /* CUPS_DATADIR env var */
163 static char templates
[1024] = ""; /* Template directory */
169 * Build the template directory pathname...
172 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
173 datadir
= CUPS_DATADIR
;
175 snprintf(templates
, sizeof(templates
), "%s/templates", datadir
);
183 * 'cgiSetServerVersion()' - Set the server name and CUPS version...
187 cgiSetServerVersion(void)
189 cgiSetVariable("SERVER_NAME", getenv("SERVER_NAME"));
190 cgiSetVariable("REMOTE_USER", getenv("REMOTE_USER"));
191 cgiSetVariable("CUPS_VERSION", CUPS_SVERSION
);
194 setlocale(LC_TIME
, "");
200 * 'cgi_copy()' - Copy the template file, substituting as needed...
204 cgi_copy(FILE *out
, /* I - Output file */
205 FILE *in
, /* I - Input file */
206 int element
, /* I - Element number (0 to N) */
207 char term
, /* I - Terminating character */
208 int indent
) /* I - Debug info indentation */
210 int ch
; /* Character from file */
211 char op
; /* Operation */
212 char name
[255], /* Name of variable */
213 *nameptr
, /* Pointer into name */
214 innername
[255], /* Inner comparison name */
215 *innerptr
, /* Pointer into inner name */
216 *s
; /* String pointer */
217 const char *value
; /* Value of variable */
218 const char *innerval
; /* Inner value */
219 const char *outptr
; /* Output string pointer */
220 char outval
[1024], /* Formatted output string */
221 compare
[1024]; /* Comparison string */
222 int result
; /* Result of comparison */
223 int uriencode
; /* Encode as URI */
224 regex_t re
; /* Regular expression to match */
227 fprintf(stderr
, "DEBUG2: %*sStarting at file position %ld...\n", indent
, "",
231 * Parse the file to the end...
234 while ((ch
= getc(in
)) != EOF
)
240 * Get a variable name...
245 for (s
= name
; (ch
= getc(in
)) != EOF
;)
246 if (strchr("}]<>=!~ \t\n", ch
))
248 else if (s
== name
&& ch
== '%')
250 else if (s
> name
&& ch
== '?')
252 else if (s
< (name
+ sizeof(name
) - 1))
257 if (s
== name
&& isspace(ch
& 255))
259 fprintf(stderr
, "DEBUG2: %*sLone { at %ld...\n", indent
, "", ftell(in
));
271 fprintf(stderr
, "DEBUG2: %*s\"{%s}\" at %ld...\n", indent
, "", name
,
275 * See if it has a value...
281 * Insert value only if it exists...
284 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
288 if ((value
= cgiGetArray(name
+ 1, atoi(nameptr
) - 1)) != NULL
)
296 else if ((value
= cgiGetArray(name
+ 1, element
)) != NULL
)
304 else if (name
[0] == '#')
311 snprintf(outval
, sizeof(outval
), "%d", cgiGetSize(name
+ 1));
313 snprintf(outval
, sizeof(outval
), "%d", element
+ 1);
317 else if (name
[0] == '[')
320 * Loop for # of elements...
323 int i
; /* Looping var */
324 long pos
; /* File position */
325 int count
; /* Number of elements */
328 if (isdigit(name
[1] & 255))
329 count
= atoi(name
+ 1);
331 count
= cgiGetSize(name
+ 1);
335 fprintf(stderr
, "DEBUG2: %*sLooping on \"%s\" at %ld, count=%d...\n",
336 indent
, "", name
+ 1, pos
, count
);
340 for (i
= 0; i
< count
; i
++)
343 fseek(in
, pos
, SEEK_SET
);
345 cgi_copy(out
, in
, i
, '}', indent
+ 2);
349 cgi_copy(NULL
, in
, 0, '}', indent
+ 2);
351 fprintf(stderr
, "DEBUG2: %*sFinished looping on \"%s\"...\n", indent
,
356 else if (name
[0] == '$')
359 * Insert cookie value or nothing if not defined.
362 if ((value
= cgiGetCookie(name
+ 1)) != NULL
)
373 * Insert variable or variable name (if element is NULL)...
376 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
379 if ((value
= cgiGetArray(name
, atoi(nameptr
) - 1)) == NULL
)
381 snprintf(outval
, sizeof(outval
), "{%s}", name
);
387 else if ((value
= cgiGetArray(name
, element
)) == NULL
)
389 snprintf(outval
, sizeof(outval
), "{%s}", name
);
397 * See if the terminating character requires another test...
403 * End of substitution...
409 cgi_puturi(outptr
, out
);
410 else if (!_cups_strcasecmp(name
, "?cupsdconf_default"))
411 fputs(outptr
, stdout
);
413 cgi_puts(outptr
, out
);
420 * OK, process one of the following checks:
422 * {name?exist:not-exist} Exists?
423 * {name=value?true:false} Equal
424 * {name<value?true:false} Less than
425 * {name>value?true:false} Greater than
426 * {name!value?true:false} Not equal
427 * {name~refex?true:false} Regex match
435 * Test for existance...
439 result
= cgiGetArray(name
+ 1, element
) != NULL
;
440 else if (name
[0] == '#')
441 result
= cgiGetVariable(name
+ 1) != NULL
;
443 result
= cgiGetArray(name
, element
) != NULL
;
445 result
= result
&& outptr
[0];
451 * Compare to a string...
454 for (s
= compare
; (ch
= getc(in
)) != EOF
;)
457 else if (s
>= (compare
+ sizeof(compare
) - 1))
461 snprintf(s
, sizeof(compare
) - (size_t)(s
- compare
), "%d", element
+ 1);
467 * Grab the value of a variable...
470 innerptr
= innername
;
471 while ((ch
= getc(in
)) != EOF
&& ch
!= '}')
472 if (innerptr
< (innername
+ sizeof(innername
) - 1))
473 *innerptr
++ = (char)ch
;
476 if (innername
[0] == '#')
477 snprintf(s
, sizeof(compare
) - (size_t)(s
- compare
), "%d", cgiGetSize(innername
+ 1));
478 else if ((innerptr
= strrchr(innername
, '-')) != NULL
&&
479 isdigit(innerptr
[1] & 255))
482 if ((innerval
= cgiGetArray(innername
, atoi(innerptr
) - 1)) == NULL
)
485 strlcpy(s
, innerval
, sizeof(compare
) - (size_t)(s
- compare
));
487 else if (innername
[0] == '?')
489 if ((innerval
= cgiGetArray(innername
+ 1, element
)) == NULL
)
492 strlcpy(s
, innerval
, sizeof(compare
) - (size_t)(s
- compare
));
494 else if ((innerval
= cgiGetArray(innername
, element
)) == NULL
)
495 snprintf(s
, sizeof(compare
) - (size_t)(s
- compare
), "{%s}", innername
);
497 strlcpy(s
, innerval
, sizeof(compare
) - (size_t)(s
- compare
));
502 *s
++ = (char)getc(in
);
511 "DEBUG2: %*sBad terminator '%c' at file position %ld...\n",
512 indent
, "", ch
, ftell(in
));
517 * Do the comparison...
523 result
= _cups_strcasecmp(outptr
, compare
) < 0;
526 result
= _cups_strcasecmp(outptr
, compare
) > 0;
529 result
= _cups_strcasecmp(outptr
, compare
) == 0;
532 result
= _cups_strcasecmp(outptr
, compare
) != 0;
535 fprintf(stderr
, "DEBUG: Regular expression \"%s\"\n", compare
);
537 if (regcomp(&re
, compare
, REG_EXTENDED
| REG_ICASE
))
540 "ERROR: Unable to compile regular expression \"%s\"!\n",
546 regmatch_t matches
[10];
550 if (!regexec(&re
, outptr
, 10, matches
, 0))
553 for (i
= 0; i
< 10; i
++)
555 fprintf(stderr
, "DEBUG: matches[%d].rm_so=%d\n", i
,
556 (int)matches
[i
].rm_so
);
557 if (matches
[i
].rm_so
< 0)
574 "DEBUG2: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n",
575 indent
, "", name
, op
, compare
, ftell(in
), result
);
580 * Comparison true; output first part and ignore second...
583 fprintf(stderr
, "DEBUG2: %*sOutput first part...\n", indent
, "");
584 cgi_copy(out
, in
, element
, ':', indent
+ 2);
586 fprintf(stderr
, "DEBUG2: %*sSkip second part...\n", indent
, "");
587 cgi_copy(NULL
, in
, element
, '}', indent
+ 2);
592 * Comparison false; ignore first part and output second...
595 fprintf(stderr
, "DEBUG2: %*sSkip first part...\n", indent
, "");
596 cgi_copy(NULL
, in
, element
, ':', indent
+ 2);
598 fprintf(stderr
, "DEBUG2: %*sOutput second part...\n", indent
, "");
599 cgi_copy(out
, in
, element
, '}', indent
+ 2);
602 fprintf(stderr
, "DEBUG2: %*sFinished \"{%s%c%s\", out=%p...\n", indent
, "",
603 name
, op
, compare
, out
);
605 else if (ch
== '\\') /* Quoted char */
616 fprintf(stderr
, "DEBUG2: %*sReturning at file position %ld on EOF...\n",
617 indent
, "", ftell(in
));
620 "DEBUG2: %*sReturning at file position %ld on character '%c'...\n",
621 indent
, "", ftell(in
), ch
);
623 if (ch
== EOF
&& term
)
624 fprintf(stderr
, "ERROR: %*sSaw EOF, expected '%c'!\n", indent
, "", term
);
627 * Flush any pending output...
636 * 'cgi_puts()' - Put a string to the output file, quoting as needed...
640 cgi_puts(const char *s
, /* I - String to output */
641 FILE *out
) /* I - Output file */
650 fputs(""", out
);
664 * 'cgi_puturi()' - Put a URI string to the output file, quoting as needed...
668 cgi_puturi(const char *s
, /* I - String to output */
669 FILE *out
) /* I - Output file */
673 if (strchr("%@&+ <>#=", *s
) || *s
< ' ' || *s
& 128)
674 fprintf(out
, "%%%02X", *s
& 255);