]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/jobs.c
Changelog.
[thirdparty/cups.git] / cgi-bin / jobs.c
index cc4ab19e227da9d1b9dcb2843d5eb8cfb99b8faf..e57b0218d0b5a9008ca329baa4c84683b0e3c97e 100644 (file)
@@ -1,25 +1,16 @@
 /*
  * "$Id$"
  *
- *   Job status CGI for the Common UNIX Printing System (CUPS).
+ *   Job status CGI for CUPS.
  *
+ *   Copyright 2007-2012 by Apple Inc.
  *   Copyright 1997-2006 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:
  *
@@ -38,7 +29,7 @@
  * Local functions...
  */
 
-static void    do_job_op(http_t *http, cups_lang_t *language, ipp_op_t op);
+static void    do_job_op(http_t *http, int job_id, ipp_op_t op);
 
 
 /*
@@ -49,10 +40,11 @@ int                                 /* O - Exit status */
 main(int  argc,                                /* I - Number of command-line arguments */
      char *argv[])                     /* I - Command-line arguments */
 {
-  cups_lang_t  *language;              /* Language information */
   http_t       *http;                  /* Connection to the server */
   const char   *op;                    /* Operation name */
-  
+  const char   *job_id_var;            /* Job ID form variable */
+  int          job_id;                 /* Job ID */
+
 
  /*
   * Get any form variables...
@@ -65,12 +57,7 @@ main(int  argc,                              /* I - Number of command-line arguments */
   */
 
   cgiSetVariable("SECTION", "jobs");
-
- /*
-  * Get the request language...
-  */
-
-  language = cupsLangDefault();
+  cgiSetVariable("REFRESH_PAGE", "");
 
  /*
   * Connect to the HTTP server...
@@ -79,42 +66,43 @@ main(int  argc,                             /* I - Number of command-line arguments */
   http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
 
  /*
-  * Tell the client to expect UTF-8 encoded HTML...
+  * Get the job ID, if any...
   */
 
-  puts("Content-Type: text/html;charset=utf-8\n");
+  if ((job_id_var = cgiGetVariable("JOB_ID")) != NULL)
+    job_id = atoi(job_id_var);
+  else
+    job_id = 0;
 
  /*
-  * Send a standard header...
+  * Do the operation...
   */
 
-  cgiSetVariable("TITLE", _cupsLangString(language, _("Jobs")));
-
-  cgiSetServerVersion();
-
-  cgiCopyTemplateLang("header.tmpl");
-
-  if ((op = cgiGetVariable("OP")) != NULL)
+  if ((op = cgiGetVariable("OP")) != NULL && job_id > 0 && cgiIsPOST())
   {
    /*
     * Do the operation...
     */
 
     if (!strcmp(op, "cancel-job"))
-      do_job_op(http, language, IPP_CANCEL_JOB);
+      do_job_op(http, job_id, IPP_CANCEL_JOB);
     else if (!strcmp(op, "hold-job"))
-      do_job_op(http, language, IPP_HOLD_JOB);
+      do_job_op(http, job_id, IPP_HOLD_JOB);
+    else if (!strcmp(op, "move-job"))
+      cgiMoveJobs(http, NULL, job_id);
     else if (!strcmp(op, "release-job"))
-      do_job_op(http, language, IPP_RELEASE_JOB);
+      do_job_op(http, job_id, IPP_RELEASE_JOB);
     else if (!strcmp(op, "restart-job"))
-      do_job_op(http, language, IPP_RESTART_JOB);
+      do_job_op(http, job_id, IPP_RESTART_JOB);
     else
     {
      /*
       * Bad operation code...  Display an error...
       */
 
-      cgiCopyTemplateLang("job-op.tmpl");
+      cgiStartHTML(cgiText(_("Jobs")));
+      cgiCopyTemplateLang("error-op.tmpl");
+      cgiEndHTML();
     }
   }
   else
@@ -123,17 +111,16 @@ main(int  argc,                           /* I - Number of command-line arguments */
     * Show a list of jobs...
     */
 
+    cgiStartHTML(cgiText(_("Jobs")));
     cgiShowJobs(http, NULL);
+    cgiEndHTML();
   }
 
-  cgiCopyTemplateLang("trailer.tmpl");
-
  /*
   * Close the HTTP server connection...
   */
 
   httpClose(http);
-  cupsLangFree(language);
 
  /*
   * Return with no errors...
@@ -149,25 +136,14 @@ main(int  argc,                           /* I - Number of command-line arguments */
 
 static void
 do_job_op(http_t      *http,           /* I - HTTP connection */
-          cups_lang_t *language,       /* I - Client's language */
+          int         job_id,          /* I - Job ID */
          ipp_op_t    op)               /* I - Operation to perform */
 {
-  ipp_t                *request,               /* IPP request */
-               *response;              /* IPP response */
+  ipp_t                *request;               /* IPP request */
   char         uri[HTTP_MAX_URI];      /* Job URI */
-  const char   *job;                   /* Job ID */
-  ipp_status_t status;                 /* Operation status... */
+  const char   *user;                  /* Username */
 
 
-  if ((job = cgiGetVariable("JOB_ID")) != NULL)
-    snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%s", job);
-  else
-  {
-    cgiSetVariable("ERROR", ippErrorString(IPP_NOT_FOUND));
-    cgiCopyTemplateLang("error.tmpl");
-    return;
-  }
-
  /*
   * Build a job request, which requires the following
   * attributes:
@@ -180,39 +156,46 @@ do_job_op(http_t      *http,              /* I - HTTP connection */
 
   request = ippNewRequest(op);
 
+  snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
+
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
                NULL, uri);
 
-  if (getenv("REMOTE_USER") != NULL)
-  {
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
-                 NULL, getenv("REMOTE_USER"));
+  if ((user = getenv("REMOTE_USER")) == NULL)
+    user = "guest";
 
-    if (strcmp(getenv("REMOTE_USER"), "root"))
-      ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
-  }
-  else
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
-                 NULL, "unknown");
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+               "requesting-user-name", NULL, user);
 
  /*
   * Do the request and get back a response...
   */
 
-  if ((response = cupsDoRequest(http, request, "/jobs")) != NULL)
+  ippDelete(cupsDoRequest(http, request, "/jobs"));
+
+  if (cupsLastError() <= IPP_OK_CONFLICT && getenv("HTTP_REFERER"))
   {
-    status = response->request.status.status_code;
+   /*
+    * Redirect successful updates back to the parent page...
+    */
 
-    ippDelete(response);
-  }
-  else
-    status = cupsLastError();
+    char       url[1024];              /* Encoded URL */
 
-  if (status > IPP_OK_CONFLICT)
+
+    strlcpy(url, "5;URL=", sizeof(url));
+    cgiFormEncode(url + 6, getenv("HTTP_REFERER"), sizeof(url) - 6);
+    cgiSetVariable("refresh_page", url);
+  }
+  else if (cupsLastError() == IPP_NOT_AUTHORIZED)
   {
-    cgiSetVariable("ERROR", ippErrorString(status));
-    cgiCopyTemplateLang("error.tmpl");
+    puts("Status: 401\n");
+    exit(0);
   }
+
+  cgiStartHTML(cgiText(_("Jobs")));
+
+  if (cupsLastError() > IPP_OK_CONFLICT)
+    cgiShowIPPError(_("Job operation failed"));
   else if (op == IPP_CANCEL_JOB)
     cgiCopyTemplateLang("job-cancel.tmpl");
   else if (op == IPP_HOLD_JOB)
@@ -221,6 +204,8 @@ do_job_op(http_t      *http,                /* I - HTTP connection */
     cgiCopyTemplateLang("job-release.tmpl");
   else if (op == IPP_RESTART_JOB)
     cgiCopyTemplateLang("job-restart.tmpl");
+
+  cgiEndHTML();
 }