]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/ipp-var.c
Merge changes from CUPS 1.4svn-r7874.
[thirdparty/cups.git] / cgi-bin / ipp-var.c
index 6a59d5c8b757cb35e67399168317899d211594da..4ee9ef867c9dc65b4c13d0c088bbfc9ed5b5c80d 100644 (file)
@@ -1,25 +1,16 @@
 /*
- * "$Id: ipp-var.c 5023 2006-01-29 14:39:44Z mike $"
+ * "$Id: ipp-var.c 7541 2008-05-05 21:22:58Z mike $"
  *
  *   CGI <-> IPP variable routines for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 1997-2006 by Easy Software Products.
+ *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 1997-2007 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products 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 missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   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:
  *
@@ -110,6 +101,7 @@ cgiGetAttributes(ipp_t      *request,       /* I - IPP request */
   */
 
   num_attrs = 0;
+  attrs[0]  = NULL;                    /* Eliminate compiler warning */
 
   while ((ch = getc(in)) != EOF)
     if (ch == '\\')
@@ -166,6 +158,8 @@ cgiGetAttributes(ipp_t      *request,       /* I - IPP request */
     for (i = 0; i < num_attrs; i ++)
       free(attrs[i]);
   }
+
+  fclose(in);
 }
 
 
@@ -480,10 +474,16 @@ cgiMoveJobs(http_t     *http,             /* I - Connection to server */
 
     if (cupsLastError() <= IPP_OK_CONFLICT)
     {
-      cgiRewriteURL(job_printer_uri, resource, sizeof(resource), NULL);
-      cgiFormEncode(uri, resource, sizeof(uri));
-      snprintf(refresh, sizeof(refresh), "2;%s", uri);
-      cgiSetVariable("refresh_page", refresh);
+      const char *path = strstr(job_printer_uri, "/printers/");
+      if (!path)
+        path = strstr(job_printer_uri, "/classes/");
+
+      if (path)
+      {
+        cgiFormEncode(uri, path, sizeof(uri));
+        snprintf(refresh, sizeof(refresh), "2;URL=%s", uri);
+       cgiSetVariable("refresh_page", refresh);
+      }
     }
 
     if (job_id)
@@ -531,8 +531,7 @@ cgiPrintTestPage(http_t     *http,  /* I - Connection to server */
   * See who is logged in...
   */
 
-  if ((user = getenv("REMOTE_USER")) == NULL)
-    user = "guest";
+  user = getenv("REMOTE_USER");
 
  /*
   * Locate the test page file...
@@ -570,8 +569,9 @@ cgiPrintTestPage(http_t     *http,  /* I - Connection to server */
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                NULL, uri);
 
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
-               "requesting-user-name", NULL, user);
+  if (user)
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+                "requesting-user-name", NULL, user);
 
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
                NULL, "Test Page");
@@ -598,9 +598,14 @@ cgiPrintTestPage(http_t     *http, /* I - Connection to server */
     */
 
     cgiFormEncode(uri, resource, sizeof(uri));
-    snprintf(refresh, sizeof(refresh), "2;%s", uri);
+    snprintf(refresh, sizeof(refresh), "2;URL=%s", uri);
     cgiSetVariable("refresh_page", refresh);
   }
+  else if (cupsLastError() == IPP_NOT_AUTHORIZED)
+  {
+    puts("Status: 401\n");
+    exit(0);
+  }
 
   cgiStartHTML(cgiText(_("Print Test Page")));
 
@@ -661,7 +666,7 @@ cgiRewriteURL(const char *uri,              /* I - Current URI */
     if ((server = getenv("SERVER_NAME")) == NULL)
       server = "";
 
-    httpGetHostname(servername, sizeof(servername));
+    httpGetHostname(NULL, servername, sizeof(servername));
 
    /*
     * Then flag whether we are using SSL on this connection...
@@ -774,7 +779,7 @@ cgiSetIPPObjectVars(
 
   fprintf(stderr, "DEBUG2: cgiSetIPPObjectVars(obj=%p, prefix=\"%s\", "
                   "element=%d)\n",
-          obj, prefix, element);
+          obj, prefix ? prefix : "(null)", element);
 
  /*
   * Set common CGI template variables...
@@ -827,6 +832,144 @@ cgiSetIPPObjectVars(
       cgiSetArray("job_printer_name", element, valptr);
     }
 
+   /*
+    * Localize event names in "notify_events" variable...
+    */
+
+    if (!strcmp(name, "notify_events"))
+    {
+      size_t   remaining;              /* Remaining bytes in buffer */
+
+
+      value[0] = '\0';
+      valptr   = value;
+
+      for (i = 0; i < attr->num_values; i ++)
+      {
+        if (valptr >= (value + sizeof(value) - 3))
+         break;
+
+        if (i)
+       {
+         *valptr++ = ',';
+         *valptr++ = ' ';
+        }
+
+        remaining = sizeof(value) - (valptr - value);
+
+        if (!strcmp(attr->values[i].string.text, "printer-stopped"))
+         strlcpy(valptr, _("Printer Paused"), remaining);
+       else if (!strcmp(attr->values[i].string.text, "printer-added"))
+         strlcpy(valptr, _("Printer Added"), remaining);
+       else if (!strcmp(attr->values[i].string.text, "printer-modified"))
+         strlcpy(valptr, _("Printer Modified"), remaining);
+       else if (!strcmp(attr->values[i].string.text, "printer-deleted"))
+         strlcpy(valptr, _("Printer Deleted"), remaining);
+       else if (!strcmp(attr->values[i].string.text, "job-created"))
+         strlcpy(valptr, _("Job Created"), remaining);
+       else if (!strcmp(attr->values[i].string.text, "job-completed"))
+         strlcpy(valptr, _("Job Completed"), remaining);
+       else if (!strcmp(attr->values[i].string.text, "job-stopped"))
+         strlcpy(valptr, _("Job Stopped"), remaining);
+       else if (!strcmp(attr->values[i].string.text, "job-config-changed"))
+         strlcpy(valptr, _("Job Options Changed"), remaining);
+       else if (!strcmp(attr->values[i].string.text, "server-restarted"))
+         strlcpy(valptr, _("Server Restarted"), remaining);
+       else if (!strcmp(attr->values[i].string.text, "server-started"))
+         strlcpy(valptr, _("Server Started"), remaining);
+       else if (!strcmp(attr->values[i].string.text, "server-stopped"))
+         strlcpy(valptr, _("Server Stopped"), remaining);
+       else if (!strcmp(attr->values[i].string.text, "server-audit"))
+         strlcpy(valptr, _("Server Security Auditing"), remaining);
+       else
+          strlcpy(valptr, attr->values[i].string.text, remaining);
+
+        valptr += strlen(valptr);
+      }
+
+      cgiSetArray("notify_events", element, value);
+      continue;
+    }
+
+   /*
+    * Add "notify_printer_name" variable if we have a "notify_printer_uri"
+    * attribute...
+    */
+
+    if (!strcmp(name, "notify_printer_uri"))
+    {
+      if ((valptr = strrchr(attr->values[0].string.text, '/')) == NULL)
+       valptr = "unknown";
+      else
+       valptr ++;
+
+      cgiSetArray("notify_printer_name", element, valptr);
+    }
+
+   /*
+    * Add "notify_recipient_name" variable if we have a "notify_recipient_uri"
+    * attribute, and rewrite recipient URI...
+    */
+
+    if (!strcmp(name, "notify_recipient_uri"))
+    {
+      char     uri[1024],              /* New URI */
+               scheme[32],             /* Scheme portion of URI */
+               userpass[256],          /* Username/password portion of URI */
+               host[1024],             /* Hostname portion of URI */
+               resource[1024],         /* Resource portion of URI */
+               *options;               /* Options in URI */
+      int      port;                   /* Port number */
+
+
+      httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text,
+                      scheme, sizeof(scheme), userpass, sizeof(userpass),
+                     host, sizeof(host), &port, resource, sizeof(resource));
+
+      if (!strcmp(scheme, "rss"))
+      {
+       /*
+        * RSS notification...
+       */
+
+        if ((options = strchr(resource, '?')) != NULL)
+         *options = '\0';
+
+        if (host[0])
+       {
+        /*
+         * Link to remote feed...
+         */
+
+         httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), "http",
+                         userpass, host, port, resource);
+          strlcpy(name, uri, sizeof(name));
+       }
+       else
+       {
+        /*
+         * Link to local feed...
+         */
+
+         snprintf(uri, sizeof(uri), "/rss%s", resource);
+          strlcpy(name, resource + 1, sizeof(name));
+       }
+      }
+      else
+      {
+       /*
+        * Other...
+       */
+
+        strlcpy(uri, attr->values[0].string.text, sizeof(uri));
+       strlcpy(name, resource, sizeof(name));
+      }
+
+      cgiSetArray("notify_recipient_uri", element, uri);
+      cgiSetArray("notify_recipient_name", element, name);
+      continue;
+    }
+
    /*
     * Add "admin_uri" variable if we have a "printer_uri_supported"
     * attribute...
@@ -850,7 +993,7 @@ cgiSetIPPObjectVars(
     for (i = 0; i < attr->num_values; i ++)
     {
       if (i)
-       strlcat(valptr, ",", sizeof(value) - (valptr - value));
+       strlcat(valptr, ", ", sizeof(value) - (valptr - value));
 
       valptr += strlen(valptr);
 
@@ -896,7 +1039,8 @@ cgiSetIPPObjectVars(
            break;
 
        case IPP_TAG_URI :
-           if (strchr(attr->values[i].string.text, ':') != NULL)
+           if (strchr(attr->values[i].string.text, ':') &&
+               strcmp(name, "device_uri"))
            {
             /*
              * Rewrite URIs...
@@ -912,7 +1056,7 @@ cgiSetIPPObjectVars(
 
                 snprintf(valptr, sizeof(value) - (valptr - value),
                         "<A HREF=\"%s\">%s</A>", url,
-                        strrchr(url, '/') + 1);
+                        strrchr(attr->values[i].string.text, '/') + 1);
              }
              else
                cgiRewriteURL(attr->values[i].string.text, valptr,
@@ -976,7 +1120,9 @@ cgiSetIPPVars(ipp_t      *response,        /* I - Response data to be copied... */
 
   fprintf(stderr, "DEBUG2: cgiSetIPPVars(response=%p, filter_name=\"%s\", "
                   "filter_value=\"%s\", prefix=\"%s\", parent_el=%d)\n",
-          response, filter_name, filter_value, prefix, parent_el);
+          response, filter_name ? filter_name : "(null)",
+         filter_value ? filter_value : "(null)",
+         prefix ? prefix : "(null)", parent_el);
 
  /*
   * Set common CGI template variables...
@@ -1034,9 +1180,9 @@ cgiSetIPPVars(ipp_t      *response,       /* I - Response data to be copied... */
     attr = cgiSetIPPObjectVars(attr, prefix, element);
   }
 
-  fprintf(stderr, "DEBUG2: Returing %d from cgiSetIPPVars()...\n", element + 1);
+  fprintf(stderr, "DEBUG2: Returing %d from cgiSetIPPVars()...\n", element);
 
-  return (element + 1);
+  return (element);
 }
 
 
@@ -1074,9 +1220,8 @@ cgiShowJobs(http_t     *http,             /* I - Connection to server */
                        count;          /* Number of jobs */
   const char           *var;           /* Form variable */
   void                 *search;        /* Search data */
-  char                 url[1024],      /* URL for prev/next/this */
-                       *urlptr,        /* Position in URL */
-                       *urlend;        /* End of URL */
+  char                 url[1024],      /* Printer URI */
+                       val[1024];      /* Form variable */
 
 
  /*
@@ -1117,7 +1262,8 @@ cgiShowJobs(http_t     *http,             /* I - Connection to server */
     * Get a list of matching job objects.
     */
 
-    if ((var = cgiGetVariable("QUERY")) != NULL)
+    if ((var = cgiGetVariable("QUERY")) != NULL &&
+        !cgiGetVariable("CLEAR"))
       search = cgiCompileSearch(var);
     else
       search = NULL;
@@ -1145,13 +1291,16 @@ cgiShowJobs(http_t     *http,           /* I - Connection to server */
     if (first < 0)
       first = 0;
 
-    sprintf(url, "%d", count);
-    cgiSetVariable("TOTAL", url);
+    sprintf(val, "%d", count);
+    cgiSetVariable("TOTAL", val);
 
     if ((var = cgiGetVariable("ORDER")) != NULL)
       ascending = !strcasecmp(var, "asc");
     else
-      ascending = 1;
+    {
+      ascending = !which_jobs || !strcasecmp(which_jobs, "not-completed");
+      cgiSetVariable("ORDER", ascending ? "asc" : "dec");
+    }
 
     if (ascending)
     {
@@ -1172,61 +1321,23 @@ cgiShowJobs(http_t     *http,           /* I - Connection to server */
     * Save navigation URLs...
     */
 
-    urlend = url + sizeof(url);
-
-    if ((var = cgiGetVariable("QUERY")) != NULL)
-    {
-      if (dest)
-        snprintf(url, sizeof(url), "/%s/%s?QUERY=", cgiGetVariable("SECTION"),
-                dest);
-      else
-        strlcpy(url, "/jobs/?QUERY=", sizeof(url));
-
-      urlptr = url + strlen(url);
-
-      cgiFormEncode(urlptr, var, urlend - urlptr);
-      urlptr += strlen(urlptr);
-
-      strlcpy(urlptr, "&", urlend - urlptr);
-      urlptr += strlen(urlptr);
-    }
+    if (dest)
+      snprintf(val, sizeof(val), "/%s/%s",  cgiGetVariable("SECTION"), dest);
     else
-    {
-      if (dest)
-        snprintf(url, sizeof(url), "/%s/%s?", cgiGetVariable("SECTION"), dest);
-      else
-        strlcpy(url, "/jobs/?", sizeof(url));
-
-      urlptr = url + strlen(url);
-    }
-
-    if (which_jobs)
-    {
-      strlcpy(urlptr, "WHICH_JOBS=", urlend - urlptr);
-      urlptr += strlen(urlptr);
-
-      cgiFormEncode(urlptr, which_jobs, urlend - urlptr);
-      urlptr += strlen(urlptr);
-
-      strlcpy(urlptr, "&", urlend - urlptr);
-      urlptr += strlen(urlptr);
-    }
+      strlcpy(val, "/jobs/", sizeof(val));
 
-    snprintf(urlptr, urlend - urlptr, "FIRST=%d", first);
-    cgiSetVariable("THISURL", url);
+    cgiSetVariable("THISURL", val);
 
     if (first > 0)
     {
-      snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
-              first - CUPS_PAGE_MAX, ascending ? "asc" : "dec");
-      cgiSetVariable("PREVURL", url);
+      sprintf(val, "%d", first - CUPS_PAGE_MAX);
+      cgiSetVariable("PREV", val);
     }
 
     if ((first + CUPS_PAGE_MAX) < count)
     {
-      snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
-              first + CUPS_PAGE_MAX, ascending ? "asc" : "dec");
-      cgiSetVariable("NEXTURL", url);
+      sprintf(val, "%d", first + CUPS_PAGE_MAX);
+      cgiSetVariable("NEXT", val);
     }
 
    /*
@@ -1248,6 +1359,7 @@ cgiShowJobs(http_t     *http,             /* I - Connection to server */
     if (count > 0)
       cgiCopyTemplateLang("pager.tmpl");
 
+    cupsArrayDelete(jobs);
     ippDelete(response);
   }
 }
@@ -1272,5 +1384,5 @@ cgiText(const char *message)              /* I - Message */
 
 
 /*
- * End of "$Id: ipp-var.c 5023 2006-01-29 14:39:44Z mike $".
+ * End of "$Id: ipp-var.c 7541 2008-05-05 21:22:58Z mike $".
  */