]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/ipp-var.c
Merge changes from CUPS 1.5svn-r8849.
[thirdparty/cups.git] / cgi-bin / ipp-var.c
index c1703e101afed0b8e4d7bb7ffbf9e34a48a63b40..1e9f3364f16e18265bb2756d0683280f8cc83204 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: ipp-var.c 6889 2007-08-29 22:23:35Z mike $"
+ * "$Id: ipp-var.c 7940 2008-09-16 00:45:16Z mike $"
  *
  *   CGI <-> IPP variable routines for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 2007-2009 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
  *
  * Contents:
  *
- *   cgiGetAttributes()    - Get the list of attributes that are needed
- *                           by the template file.
+ *   cgiGetAttributes()    - Get the list of attributes that are needed by the
+ *                           template file.
  *   cgiGetIPPObjects()    - Get the objects in an IPP response.
  *   cgiMoveJobs()         - Move one or more jobs.
+ *   cgiPrintCommand()     - Print a CUPS command job.
  *   cgiPrintTestPage()    - Print a test page.
  *   cgiRewriteURL()       - Rewrite a printer URI into a web browser URL...
  *   cgiSetIPPObjectVars() - Set CGI variables from an IPP object.
@@ -113,7 +114,7 @@ cgiGetAttributes(ipp_t      *request,       /* I - IPP request */
       */
 
       for (nameptr = name; (ch = getc(in)) != EOF;)
-        if (strchr("}]<>=! \t\n", ch))
+        if (strchr("}]<>=!~ \t\n", ch))
           break;
         else if (nameptr > name && ch == '?')
          break;
@@ -287,8 +288,7 @@ cgiMoveJobs(http_t     *http,               /* I - Connection to server */
   * See who is logged in...
   */
 
-  if ((user = getenv("REMOTE_USER")) == NULL)
-    user = "guest";
+  user = getenv("REMOTE_USER");
 
  /*
   * See if the user has already selected a new destination...
@@ -358,7 +358,7 @@ cgiMoveJobs(http_t     *http,               /* I - Connection to server */
        */
 
         cgiStartHTML(cgiText(_("Move Job")));
-       cgiShowIPPError(_("Unable to find destination for job!"));
+       cgiShowIPPError(_("Unable to find destination for job"));
        cgiEndHTML();
        return;
       }
@@ -373,8 +373,14 @@ cgiMoveJobs(http_t     *http,              /* I - Connection to server */
     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
                  "requested-attributes", NULL, "printer-uri-supported");
 
-    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);
+
+    ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type",
+                  CUPS_PRINTER_LOCAL);
+    ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type-mask",
+                  CUPS_PRINTER_SCANNER);
 
     if ((response = cupsDoRequest(http, request, "/")) != NULL)
     {
@@ -414,7 +420,18 @@ cgiMoveJobs(http_t     *http,              /* I - Connection to server */
     else
       cgiStartHTML(cgiText(_("Move All Jobs")));
 
-    cgiCopyTemplateLang("job-move.tmpl");
+    if (cgiGetSize("JOB_PRINTER_NAME") > 0)
+      cgiCopyTemplateLang("job-move.tmpl");
+    else
+    {
+      if (job_id)
+       cgiSetVariable("MESSAGE", cgiText(_("Unable to move job")));
+      else
+       cgiSetVariable("MESSAGE", cgiText(_("Unable to move jobs")));
+
+      cgiSetVariable("ERROR", cgiText(_("No destinations added.")));
+      cgiCopyTemplateLang("error.tmpl");
+    }
   }
   else
   {
@@ -474,10 +491,19 @@ 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;URL=%s", uri);
-      cgiSetVariable("refresh_page", refresh);
+      const char *path = strstr(job_printer_uri, "/printers/");
+      if (!path)
+      {
+        path = strstr(job_printer_uri, "/classes/");
+        cgiSetVariable("IS_CLASS", "YES");
+      }
+
+      if (path)
+      {
+        cgiFormEncode(uri, path, sizeof(uri));
+        snprintf(refresh, sizeof(refresh), "2;URL=%s", uri);
+       cgiSetVariable("refresh_page", refresh);
+      }
     }
 
     if (job_id)
@@ -503,6 +529,168 @@ cgiMoveJobs(http_t     *http,             /* I - Connection to server */
 }
 
 
+/*
+ * 'cgiPrintCommand()' - Print a CUPS command job.
+ */
+
+void
+cgiPrintCommand(http_t     *http,      /* I - Connection to server */
+                const char *dest,      /* I - Destination printer */
+                const char *command,   /* I - Command to send */
+               const char *title)      /* I - Page/job title */
+{
+  int          job_id;                 /* Command file job */
+  char         uri[HTTP_MAX_URI],      /* Job URI */
+               resource[1024],         /* Printer resource path */
+               refresh[1024],          /* Refresh URL */
+               command_file[1024];     /* Command "file" */
+  http_status_t        status;                 /* Document status */
+  cups_option_t        hold_option;            /* job-hold-until option */
+  const char   *user;                  /* User name */
+  ipp_t                *request,               /* Get-Job-Attributes request */
+               *response;              /* Get-Job-Attributes response */
+  ipp_attribute_t *attr;               /* Current job attribute */
+  static const char const *job_attrs[] =/* Job attributes we want */
+               {
+                 "job-state",
+                 "job-printer-state-message"
+               };
+
+
+ /*
+  * Create the CUPS command file...
+  */
+
+  snprintf(command_file, sizeof(command_file), "#CUPS-COMMAND\n%s\n", command);
+
+ /*
+  * Show status...
+  */
+
+  if (cgiSupportsMultipart())
+  {
+    cgiStartMultipart();
+    cgiStartHTML(title);
+    cgiCopyTemplateLang("command.tmpl");
+    cgiEndHTML();
+    fflush(stdout);
+  }
+
+ /*
+  * Send the command file job...
+  */
+
+  hold_option.name  = "job-hold-until";
+  hold_option.value = "no-hold";
+
+  if ((user = getenv("REMOTE_USER")) != NULL)
+    cupsSetUser(user);
+  else
+    cupsSetUser("anonymous");
+
+  if ((job_id = cupsCreateJob(http, dest, title,
+                             1, &hold_option)) < 1)
+  {
+    cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver")));
+    cgiSetVariable("ERROR", cupsLastErrorString());
+    cgiStartHTML(title);
+    cgiCopyTemplateLang("error.tmpl");
+    cgiEndHTML();
+
+    if (cgiSupportsMultipart())
+      cgiEndMultipart();
+    return;
+  }
+
+  status = cupsStartDocument(http, dest, job_id, NULL, CUPS_FORMAT_COMMAND, 1);
+  if (status == HTTP_CONTINUE)
+    status = cupsWriteRequestData(http, command_file,
+                                 strlen(command_file));
+  if (status == HTTP_CONTINUE)
+    cupsFinishDocument(http, dest);
+
+  if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE)
+  {
+    cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver")));
+    cgiSetVariable("ERROR", cupsLastErrorString());
+    cgiStartHTML(title);
+    cgiCopyTemplateLang("error.tmpl");
+    cgiEndHTML();
+
+    if (cgiSupportsMultipart())
+      cgiEndMultipart();
+
+    cupsCancelJob(dest, job_id);
+    return;
+  }
+
+ /*
+  * Wait for the job to complete...
+  */
+
+  if (cgiSupportsMultipart())
+  {
+    for (;;)
+    {
+     /*
+      * Get the current job state...
+      */
+
+      snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
+      request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES);
+      ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
+                  NULL, uri);
+      if (user)
+       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+                    "requesting-user-name", NULL, user);
+      ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
+                   "requested-attributes", 2, NULL, job_attrs);
+
+      if ((response = cupsDoRequest(http, request, "/")) != NULL)
+       cgiSetIPPVars(response, NULL, NULL, NULL, 0);
+
+      attr = ippFindAttribute(response, "job-state", IPP_TAG_ENUM);
+      if (!attr || attr->values[0].integer >= IPP_JOB_STOPPED ||
+          attr->values[0].integer == IPP_JOB_HELD)
+      {
+       ippDelete(response);
+       break;
+      }
+
+     /*
+      * Job not complete, so update the status...
+      */
+
+      ippDelete(response);
+
+      cgiStartHTML(title);
+      cgiCopyTemplateLang("command.tmpl");
+      cgiEndHTML();
+      fflush(stdout);
+
+      sleep(5);
+    }
+  }
+
+ /*
+  * Send the final page that reloads the printer's page...
+  */
+
+  snprintf(resource, sizeof(resource), "/printers/%s", dest);
+
+  cgiFormEncode(uri, resource, sizeof(uri));
+  snprintf(refresh, sizeof(refresh), "5;URL=%s", uri);
+  cgiSetVariable("refresh_page", refresh);
+
+  cgiStartHTML(title);
+  cgiCopyTemplateLang("command.tmpl");
+  cgiEndHTML();
+
+  if (cgiSupportsMultipart())
+    cgiEndMultipart();
+}
+
+
 /*
  * 'cgiPrintTestPage()' - Print a test page.
  */
@@ -534,7 +722,7 @@ cgiPrintTestPage(http_t     *http,  /* I - Connection to server */
   if ((datadir = getenv("CUPS_DATADIR")) == NULL)
     datadir = CUPS_DATADIR;
 
-  snprintf(filename, sizeof(filename), "%s/data/testprint.ps", datadir);
+  snprintf(filename, sizeof(filename), "%s/data/testprint", datadir);
 
  /*
   * Point to the printer/class...
@@ -555,7 +743,6 @@ cgiPrintTestPage(http_t     *http,  /* I - Connection to server */
   *    attributes-natural-language
   *    printer-uri
   *    requesting-user-name
-  *    document-format
   */
 
   request = ippNewRequest(IPP_PRINT_JOB);
@@ -570,9 +757,6 @@ cgiPrintTestPage(http_t     *http,  /* I - Connection to server */
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
                NULL, "Test Page");
 
-  ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
-               NULL, "application/postscript");
-
  /*
   * Do the request and get back a response...
   */
@@ -626,7 +810,7 @@ cgiRewriteURL(const char *uri,              /* I - Current URI */
              int        urlsize,       /* I - Size of URL buffer */
              const char *newresource)  /* I - Replacement resource */
 {
-  char                 method[HTTP_MAX_URI],
+  char                 scheme[HTTP_MAX_URI],
                        userpass[HTTP_MAX_URI],
                        hostname[HTTP_MAX_URI],
                        rawresource[HTTP_MAX_URI],
@@ -673,13 +857,13 @@ cgiRewriteURL(const char *uri,            /* I - Current URI */
   * Convert the URI to a URL...
   */
 
-  httpSeparateURI(HTTP_URI_CODING_ALL, uri, method, sizeof(method), userpass,
+  httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass,
                   sizeof(userpass), hostname, sizeof(hostname), &port,
                  rawresource, sizeof(rawresource));
 
-  if (!strcmp(method, "ipp") ||
-      !strcmp(method, "http") ||
-      !strcmp(method, "https"))
+  if (!strcmp(scheme, "ipp") ||
+      !strcmp(scheme, "http") ||
+      !strcmp(scheme, "https"))
   {
     if (newresource)
     {
@@ -718,7 +902,9 @@ cgiRewriteURL(const char *uri,              /* I - Current URI */
     * Map local access to a local URI...
     */
 
-    if (!strcasecmp(hostname, "localhost") ||
+    if (!strcasecmp(hostname, "127.0.0.1") ||
+       !strcasecmp(hostname, "[::1]") ||
+       !strcasecmp(hostname, "localhost") ||
        !strncasecmp(hostname, "localhost.", 10) ||
        !strcasecmp(hostname, server) ||
        !strcasecmp(hostname, servername))
@@ -1237,8 +1423,8 @@ cgiShowJobs(http_t     *http,             /* I - Connection to server */
                  NULL, url);
   }
   else
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
-                "ipp://localhost/jobs");
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
+                "ipp://localhost/");
 
   if ((which_jobs = cgiGetVariable("which_jobs")) != NULL)
     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs",
@@ -1345,12 +1531,12 @@ cgiShowJobs(http_t     *http,           /* I - Connection to server */
 
     cgiCopyTemplateLang("jobs-header.tmpl");
 
-    if (count > 0)
+    if (count > CUPS_PAGE_MAX)
       cgiCopyTemplateLang("pager.tmpl");
 
     cgiCopyTemplateLang("jobs.tmpl");
 
-    if (count > 0)
+    if (count > CUPS_PAGE_MAX)
       cgiCopyTemplateLang("pager.tmpl");
 
     cupsArrayDelete(jobs);
@@ -1378,5 +1564,5 @@ cgiText(const char *message)              /* I - Message */
 
 
 /*
- * End of "$Id: ipp-var.c 6889 2007-08-29 22:23:35Z mike $".
+ * End of "$Id: ipp-var.c 7940 2008-09-16 00:45:16Z mike $".
  */