]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/template.c
Update ipp documentation to reflect the behavior of configuring WiFi on IPP USB printers.
[thirdparty/cups.git] / cgi-bin / template.c
index f20a7a950342ddd735cef0bd1e756dee7ba8032e..3300a8852e01f8706001f0c12758c9a6f0b6e958 100644 (file)
@@ -1,27 +1,10 @@
 /*
- * "$Id: template.c 7219 2008-01-14 22:00:02Z mike $"
+ * CGI template function.
  *
- *   CGI template function.
+ * Copyright 2007-2015 by Apple Inc.
+ * Copyright 1997-2006 by Easy Software Products.
  *
- *   Copyright 2007-2008 by Apple Inc.
- *   Copyright 1997-2006 by Easy Software Products.
- *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * Contents:
- *
- *   cgiCopyTemplateFile() - Copy a template file and replace all the
- *                           '{variable}' strings with the variable value.
- *   cgiCopyTemplateLang() - Copy a template file using a language...
- *   cgiGetTemplateDir()   - Get the templates directory...
- *   cgiSetServerVersion() - Set the server name and CUPS version...
- *   cgi_copy()            - Copy the template file, substituting as needed...
- *   cgi_puts()            - Put a string to the output file, quoting as
- *                           needed...
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 #include "cgi-private.h"
@@ -50,7 +33,6 @@ cgiCopyTemplateFile(FILE       *out,  /* I - Output file */
 {
   FILE *in;                            /* Input file */
 
-
   fprintf(stderr, "DEBUG2: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out,
           tmpl ? tmpl : "(null)");
 
@@ -68,7 +50,7 @@ cgiCopyTemplateFile(FILE       *out,  /* I - Output file */
   if ((in = fopen(tmpl, "r")) == NULL)
   {
     fprintf(stderr, "ERROR: Unable to open template file \"%s\" - %s\n",
-            tmpl ? tmpl : "(null)", strerror(errno));
+            tmpl, strerror(errno));
     return;
   }
 
@@ -108,8 +90,6 @@ cgiCopyTemplateLang(const char *tmpl)        /* I - Base filename */
   * Convert the language to a locale name...
   */
 
-  locale[0] = '\0';
-
   if ((lang = getenv("LANG")) != NULL)
   {
     locale[0] = '/';
@@ -118,8 +98,12 @@ cgiCopyTemplateLang(const char *tmpl)       /* I - Base filename */
     if ((locptr = strchr(locale, '.')) != NULL)
       *locptr = '\0';                  /* Strip charset */
   }
+  else
+  {
+    locale[0] = '\0';
+  }
 
-  fprintf(stderr, "DEBUG: lang=\"%s\", locale=\"%s\"...\n",
+  fprintf(stderr, "DEBUG2: lang=\"%s\", locale=\"%s\"...\n",
           lang ? lang : "(null)", locale);
 
  /*
@@ -266,7 +250,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
         else if (s > name && ch == '?')
          break;
        else if (s < (name + sizeof(name) - 1))
-          *s++ = ch;
+          *s++ = (char)ch;
 
       *s = '\0';
 
@@ -324,9 +308,9 @@ cgi_copy(FILE *out,                 /* I - Output file */
        */
 
         if (name[1])
-          sprintf(outval, "%d", cgiGetSize(name + 1));
+          snprintf(outval, sizeof(outval), "%d", cgiGetSize(name + 1));
        else
-         sprintf(outval, "%d", element + 1);
+         snprintf(outval, sizeof(outval), "%d", element + 1);
 
         outptr = outval;
       }
@@ -369,6 +353,20 @@ cgi_copy(FILE *out,                        /* I - Output file */
 
         continue;
       }
+      else if (name[0] == '$')
+      {
+       /*
+        * Insert cookie value or nothing if not defined.
+       */
+
+        if ((value = cgiGetCookie(name + 1)) != NULL)
+         outptr = value;
+       else
+       {
+         outval[0] = '\0';
+         outptr    = outval;
+       }
+      }
       else
       {
        /*
@@ -409,7 +407,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
        {
          if (uriencode)
            cgi_puturi(outptr, out);
-         else if (!strcasecmp(name, "?cupsdconf_default"))
+         else if (!_cups_strcasecmp(name, "?cupsdconf_default"))
            fputs(outptr, stdout);
          else
            cgi_puts(outptr, out);
@@ -429,7 +427,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
       *   {name~refex?true:false}    Regex match
       */
 
-      op = ch;
+      op = (char)ch;
 
       if (ch == '?')
       {
@@ -437,7 +435,14 @@ cgi_copy(FILE *out,                        /* I - Output file */
         * Test for existance...
        */
 
-        result     = cgiGetArray(name, element) != NULL && outptr[0];
+        if (name[0] == '?')
+         result = cgiGetArray(name + 1, element) != NULL;
+       else if (name[0] == '#')
+         result = cgiGetVariable(name + 1) != NULL;
+        else
+          result = cgiGetArray(name, element) != NULL;
+
+       result     = result && outptr[0];
        compare[0] = '\0';
       }
       else
@@ -453,7 +458,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
            continue;
          else if (ch == '#')
          {
-           sprintf(s, "%d", element + 1);
+           snprintf(s, sizeof(compare) - (size_t)(s - compare), "%d", element + 1);
            s += strlen(s);
          }
          else if (ch == '{')
@@ -465,11 +470,11 @@ cgi_copy(FILE *out,                       /* I - Output file */
            innerptr = innername;
            while ((ch = getc(in)) != EOF && ch != '}')
              if (innerptr < (innername + sizeof(innername) - 1))
-               *innerptr++ = ch;
+               *innerptr++ = (char)ch;
            *innerptr = '\0';
 
             if (innername[0] == '#')
-             sprintf(s, "%d", cgiGetSize(innername + 1));
+             snprintf(s, sizeof(compare) - (size_t)(s - compare), "%d", cgiGetSize(innername + 1));
            else if ((innerptr = strrchr(innername, '-')) != NULL &&
                     isdigit(innerptr[1] & 255))
             {
@@ -477,26 +482,26 @@ cgi_copy(FILE *out,                       /* I - Output file */
              if ((innerval = cgiGetArray(innername, atoi(innerptr) - 1)) == NULL)
                *s = '\0';
              else
-               strlcpy(s, innerval, sizeof(compare) - (s - compare));
+               strlcpy(s, innerval, sizeof(compare) - (size_t)(s - compare));
            }
            else if (innername[0] == '?')
            {
              if ((innerval = cgiGetArray(innername + 1, element)) == NULL)
                *s = '\0';
              else
-               strlcpy(s, innerval, sizeof(compare) - (s - compare));
+               strlcpy(s, innerval, sizeof(compare) - (size_t)(s - compare));
             }
            else if ((innerval = cgiGetArray(innername, element)) == NULL)
-             snprintf(s, sizeof(compare) - (s - compare), "{%s}", innername);
+             snprintf(s, sizeof(compare) - (size_t)(s - compare), "{%s}", innername);
            else
-             strlcpy(s, innerval, sizeof(compare) - (s - compare));
+             strlcpy(s, innerval, sizeof(compare) - (size_t)(s - compare));
 
             s += strlen(s);
          }
           else if (ch == '\\')
-           *s++ = getc(in);
+           *s++ = (char)getc(in);
          else
-            *s++ = ch;
+            *s++ = (char)ch;
 
         *s = '\0';
 
@@ -515,16 +520,16 @@ cgi_copy(FILE *out,                       /* I - Output file */
         switch (op)
        {
          case '<' :
-             result = strcasecmp(outptr, compare) < 0;
+             result = _cups_strcasecmp(outptr, compare) < 0;
              break;
          case '>' :
-             result = strcasecmp(outptr, compare) > 0;
+             result = _cups_strcasecmp(outptr, compare) > 0;
              break;
          case '=' :
-             result = strcasecmp(outptr, compare) == 0;
+             result = _cups_strcasecmp(outptr, compare) == 0;
              break;
          case '!' :
-             result = strcasecmp(outptr, compare) != 0;
+             result = _cups_strcasecmp(outptr, compare) != 0;
              break;
          case '~' :
              fprintf(stderr, "DEBUG: Regular expression \"%s\"\n", compare);
@@ -532,7 +537,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
              if (regcomp(&re, compare, REG_EXTENDED | REG_ICASE))
              {
                fprintf(stderr,
-                       "ERROR: Unable to compile regular expresion \"%s\"!\n",
+                       "ERROR: Unable to compile regular expression \"%s\"!\n",
                        compare);
                result = 0;
              }
@@ -638,43 +643,13 @@ cgi_puts(const char *s,                   /* I - String to output */
   while (*s)
   {
     if (*s == '<')
-    {
-     /*
-      * Pass <A HREF="url"> and </A>, otherwise quote it...
-      */
-
-      if (!strncasecmp(s, "<A HREF=\"", 9))
-      {
-        fputs("<A HREF=\"", out);
-       s += 9;
-
-       while (*s && *s != '\"')
-       {
-          if (*s == '&')
-            fputs("&amp;", out);
-         else
-           putc(*s, out);
-
-         s ++;
-       }
-
-        if (*s)
-         s ++;
-
-       fputs("\">", out);
-      }
-      else if (!strncasecmp(s, "</A>", 4))
-      {
-        fputs("</A>", out);
-       s += 3;
-      }
-      else
-        fputs("&lt;", out);
-    }
+      fputs("&lt;", out);
     else if (*s == '>')
       fputs("&gt;", out);
     else if (*s == '\"')
       fputs("&quot;", out);
+    else if (*s == '\'')
+      fputs("&#39;", out);
     else if (*s == '&')
       fputs("&amp;", out);
     else
@@ -695,7 +670,7 @@ cgi_puturi(const char *s,           /* I - String to output */
 {
   while (*s)
   {
-    if (strchr("%&+ <>#=", *s) || *s & 128)
+    if (strchr("%@&+ <>#=", *s) || *s < ' ' || *s & 128)
       fprintf(out, "%%%02X", *s & 255);
     else
       putc(*s, out);
@@ -703,8 +678,3 @@ cgi_puturi(const char *s,           /* I - String to output */
     s ++;
   }
 }
-
-
-/*
- * End of "$Id: template.c 7219 2008-01-14 22:00:02Z mike $".
- */