]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/classes.c
License change: Apache License, Version 2.0.
[thirdparty/cups.git] / cgi-bin / classes.c
index d7fd569e79a97c35391f5ab73f1f0818687bccc7..78ef08e22f1ab2ecbb08c5f81cd1c4da0d5ce415 100644 (file)
@@ -1,31 +1,10 @@
 /*
- * "$Id: classes.c 5202 2006-02-28 19:37:03Z mike $"
+ * Class status CGI for CUPS.
  *
- *   Class status CGI for the Common UNIX Printing System (CUPS).
+ * Copyright 2007-2016 by Apple Inc.
+ * Copyright 1997-2006 by Easy Software Products.
  *
- *   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
- *
- * Contents:
- *
- *   main()             - Main entry for CGI.
- *   show_all_classes() - Show all classes...
- *   show_class()       - Show a single class.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
  * Local functions...
  */
 
-void   show_all_classes(http_t *http, const char *username);
-void   show_class(http_t *http, const char *printer);
+static void    do_class_op(http_t *http, const char *printer, ipp_op_t op,
+                           const char *title);
+static void    show_all_classes(http_t *http, const char *username);
+static void    show_class(http_t *http, const char *printer);
 
 
 /*
@@ -48,8 +29,7 @@ void  show_class(http_t *http, const char *printer);
  */
 
 int                                    /* O - Exit status */
-main(int  argc,                                /* I - Number of command-line arguments */
-     char *argv[])                     /* I - Command-line arguments */
+main(void)
 {
   const char   *pclass;                /* Class name */
   const char   *user;                  /* Username */
@@ -78,6 +58,7 @@ main(int  argc,                               /* I - Number of command-line arguments */
   */
 
   cgiSetVariable("SECTION", "classes");
+  cgiSetVariable("REFRESH_PAGE", "");
 
  /*
   * See if we are displaying a printer or all classes...
@@ -89,14 +70,16 @@ main(int  argc,                             /* I - Number of command-line arguments */
 
     if (!*pclass)
       pclass = NULL;
+
+    if (pclass)
+      cgiSetVariable("PRINTER_NAME", pclass);
   }
 
  /*
   * See who is logged in...
   */
 
-  if ((user = getenv("REMOTE_USER")) == NULL)
-    user = "guest";
+  user = getenv("REMOTE_USER");
 
  /*
   * Connect to the HTTP server...
@@ -108,7 +91,7 @@ main(int  argc,                              /* I - Number of command-line arguments */
   * Get the default printer...
   */
 
-  if (!op)
+  if (!op || !cgiIsPOST())
   {
    /*
     * Get the default destination...
@@ -148,21 +131,54 @@ main(int  argc,                           /* I - Number of command-line arguments */
     else
       show_class(http, pclass);
   }
-  else if (!strcasecmp(op, "print-test-page") && pclass)
-    cgiPrintTestPage(http, pclass);
-  else if (!strcasecmp(op, "move-jobs") && pclass)
-    cgiMoveJobs(http, pclass, 0);
+  else if (pclass)
+  {
+    if (!*op)
+    {
+      const char *server_port = getenv("SERVER_PORT");
+                                       /* Port number string */
+      int      port = atoi(server_port ? server_port : "0");
+                                       /* Port number */
+      char     uri[1024];              /* URL */
+
+      httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri),
+                      getenv("HTTPS") ? "https" : "http", NULL,
+                      getenv("SERVER_NAME"), port, "/classes/%s", pclass);
+
+      printf("Location: %s\n\n", uri);
+    }
+    else if (!strcmp(op, "start-class"))
+      do_class_op(http, pclass, IPP_RESUME_PRINTER, cgiText(_("Resume Class")));
+    else if (!strcmp(op, "stop-class"))
+      do_class_op(http, pclass, IPP_PAUSE_PRINTER, cgiText(_("Pause Class")));
+    else if (!strcmp(op, "accept-jobs"))
+      do_class_op(http, pclass, CUPS_ACCEPT_JOBS, cgiText(_("Accept Jobs")));
+    else if (!strcmp(op, "reject-jobs"))
+      do_class_op(http, pclass, CUPS_REJECT_JOBS, cgiText(_("Reject Jobs")));
+    else if (!strcmp(op, "cancel-jobs"))
+      do_class_op(http, pclass, IPP_OP_CANCEL_JOBS, cgiText(_("Cancel Jobs")));
+    else if (!_cups_strcasecmp(op, "print-test-page"))
+      cgiPrintTestPage(http, pclass);
+    else if (!_cups_strcasecmp(op, "move-jobs"))
+      cgiMoveJobs(http, pclass, 0);
+    else
+    {
+     /*
+      * Unknown/bad operation...
+      */
+
+      cgiStartHTML(pclass);
+      cgiCopyTemplateLang("error-op.tmpl");
+      cgiEndHTML();
+    }
+  }
   else
   {
    /*
     * Unknown/bad operation...
     */
 
-    if (pclass)
-      cgiStartHTML(pclass);
-    else
-      cgiStartHTML(cgiText(_("Classes")));
-
+    cgiStartHTML(cgiText(_("Classes")));
     cgiCopyTemplateLang("error-op.tmpl");
     cgiEndHTML();
   }
@@ -181,27 +197,107 @@ main(int  argc,                          /* I - Number of command-line arguments */
 }
 
 
+/*
+ * 'do_class_op()' - Do a class operation.
+ */
+
+static void
+do_class_op(http_t      *http,         /* I - HTTP connection */
+            const char *printer,       /* I - Printer name */
+           ipp_op_t    op,             /* I - Operation to perform */
+           const char  *title)         /* I - Title of page */
+{
+  ipp_t                *request;               /* IPP request */
+  char         uri[HTTP_MAX_URI],      /* Printer URI */
+               resource[HTTP_MAX_URI]; /* Path for request */
+
+
+ /*
+  * Build a printer request, which requires the following
+  * attributes:
+  *
+  *    attributes-charset
+  *    attributes-natural-language
+  *    printer-uri
+  */
+
+  request = ippNewRequest(op);
+
+  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+                   "localhost", 0, "/classes/%s", printer);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
+               NULL, uri);
+
+ /*
+  * Do the request and get back a response...
+  */
+
+  snprintf(resource, sizeof(resource), "/classes/%s", printer);
+  ippDelete(cupsDoRequest(http, request, resource));
+
+  if (cupsLastError() == IPP_NOT_AUTHORIZED)
+  {
+    puts("Status: 401\n");
+    exit(0);
+  }
+  else if (cupsLastError() > IPP_OK_CONFLICT)
+  {
+    cgiStartHTML(title);
+    cgiShowIPPError(_("Unable to do maintenance command"));
+  }
+  else
+  {
+   /*
+    * Redirect successful updates back to the printer page...
+    */
+
+    char       url[1024],              /* Printer/class URL */
+               refresh[1024];          /* Refresh URL */
+
+
+    cgiRewriteURL(uri, url, sizeof(url), NULL);
+    cgiFormEncode(uri, url, sizeof(uri));
+    snprintf(refresh, sizeof(refresh), "5;URL=%s", uri);
+    cgiSetVariable("refresh_page", refresh);
+
+    cgiStartHTML(title);
+
+    cgiSetVariable("IS_CLASS", "YES");
+
+    if (op == IPP_PAUSE_PRINTER)
+      cgiCopyTemplateLang("printer-stop.tmpl");
+    else if (op == IPP_RESUME_PRINTER)
+      cgiCopyTemplateLang("printer-start.tmpl");
+    else if (op == CUPS_ACCEPT_JOBS)
+      cgiCopyTemplateLang("printer-accept.tmpl");
+    else if (op == CUPS_REJECT_JOBS)
+      cgiCopyTemplateLang("printer-reject.tmpl");
+    else if (op == IPP_OP_CANCEL_JOBS)
+      cgiCopyTemplateLang("printer-cancel-jobs.tmpl");
+  }
+
+  cgiEndHTML();
+}
+
+
 /*
  * 'show_all_classes()' - Show all classes...
  */
 
-void
+static void
 show_all_classes(http_t     *http,     /* I - Connection to server */
-                  const char *user)    /* I - Username */
+                 const char *user)     /* I - Username */
 {
   int                  i;              /* Looping var */
   ipp_t                        *request,       /* IPP request */
                        *response;      /* IPP response */
   cups_array_t         *classes;       /* Array of class objects */
   ipp_attribute_t      *pclass;        /* Class object */
-  int                  ascending,      /* Order of classes (0 = descending) */
-                       first,          /* First class to show */
+  int                  first,          /* First class to show */
                        count;          /* Number of classes */
   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                 val[1024];      /* Form variable */
 
 
  /*
@@ -221,8 +317,9 @@ show_all_classes(http_t     *http,  /* I - Connection to server */
 
   request = ippNewRequest(CUPS_GET_CLASSES);
 
-  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);
 
   cgiGetAttributes(request, "classes.tmpl");
 
@@ -236,7 +333,8 @@ show_all_classes(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;
@@ -264,67 +362,36 @@ show_all_classes(http_t     *http,        /* I - Connection to server */
     if (first < 0)
       first = 0;
 
-    sprintf(url, "%d", count);
-    cgiSetVariable("TOTAL", url);
-
-    if ((var = cgiGetVariable("ORDER")) != NULL)
-      ascending = !strcasecmp(var, "asc");
-    else
-      ascending = 1;
+    sprintf(val, "%d", count);
+    cgiSetVariable("TOTAL", val);
 
-    if (ascending)
-    {
-      for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, first);
-          i < CUPS_PAGE_MAX && pclass;
-          i ++, pclass = (ipp_attribute_t *)cupsArrayNext(classes))
-        cgiSetIPPObjectVars(pclass, NULL, i);
-    }
-    else
-    {
-      for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, count - first - 1);
-          i < CUPS_PAGE_MAX && pclass;
-          i ++, pclass = (ipp_attribute_t *)cupsArrayPrev(classes))
-        cgiSetIPPObjectVars(pclass, NULL, i);
-    }
+    for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, first);
+        i < CUPS_PAGE_MAX && pclass;
+        i ++, pclass = (ipp_attribute_t *)cupsArrayNext(classes))
+      cgiSetIPPObjectVars(pclass, NULL, i);
 
    /*
     * Save navigation URLs...
     */
 
-    urlend = url + sizeof(url);
+    cgiSetVariable("THISURL", "/classes/");
 
-    if ((var = cgiGetVariable("QUERY")) != NULL)
-    {
-      strlcpy(url, "/classes/?QUERY=", sizeof(url));
-      urlptr = url + strlen(url);
-
-      cgiFormEncode(urlptr, var, urlend - urlptr);
-      urlptr += strlen(urlptr);
-
-      strlcpy(urlptr, "&", urlend - urlptr);
-      urlptr += strlen(urlptr);
-    }
-    else
+    if (first > 0)
     {
-      strlcpy(url, "/classes/?", sizeof(url));
-      urlptr = url + strlen(url);
+      sprintf(val, "%d", first - CUPS_PAGE_MAX);
+      cgiSetVariable("PREV", val);
     }
 
-    snprintf(urlptr, urlend - urlptr, "FIRST=%d", first);
-    cgiSetVariable("THISURL", url);
-
-    if (first > 0)
+    if ((first + CUPS_PAGE_MAX) < count)
     {
-      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("NEXT", val);
     }
 
-    if ((first + CUPS_PAGE_MAX) < count)
+    if (count > CUPS_PAGE_MAX)
     {
-      snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
-              first + CUPS_PAGE_MAX, ascending ? "asc" : "dec");
-      cgiSetVariable("NEXTURL", url);
+      snprintf(val, sizeof(val), "%d", CUPS_PAGE_MAX * (count / CUPS_PAGE_MAX));
+      cgiSetVariable("LAST", val);
     }
 
    /*
@@ -335,12 +402,12 @@ show_all_classes(http_t     *http,        /* I - Connection to server */
 
     cgiCopyTemplateLang("classes-header.tmpl");
 
-    if (count > 0)
+    if (count > CUPS_PAGE_MAX)
       cgiCopyTemplateLang("pager.tmpl");
 
     cgiCopyTemplateLang("classes.tmpl");
 
-    if (count > 0)
+    if (count > CUPS_PAGE_MAX)
       cgiCopyTemplateLang("pager.tmpl");
 
    /*
@@ -356,7 +423,7 @@ show_all_classes(http_t     *http,  /* I - Connection to server */
     * Show the error...
     */
 
-    cgiShowIPPError(_("Unable to get class list:"));
+    cgiShowIPPError(_("Unable to get class list"));
   }
 
    cgiEndHTML();
@@ -367,7 +434,7 @@ show_all_classes(http_t     *http,  /* I - Connection to server */
  * 'show_class()' - Show a single class.
  */
 
-void
+static void
 show_class(http_t     *http,           /* I - Connection to server */
            const char *pclass)         /* I - Name of class */
 {
@@ -394,7 +461,7 @@ show_class(http_t     *http,                /* I - Connection to server */
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
                uri);
 
-  cgiGetAttributes(request, "classes.tmpl");
+  cgiGetAttributes(request, "class.tmpl");
 
  /*
   * Do the request and get back a response...
@@ -419,7 +486,7 @@ show_class(http_t     *http,                /* I - Connection to server */
       */
 
       cgiFormEncode(uri, pclass, sizeof(uri));
-      snprintf(refresh, sizeof(refresh), "10;/classes/%s", uri);
+      snprintf(refresh, sizeof(refresh), "10;URL=/classes/%s", uri);
       cgiSetVariable("refresh_page", refresh);
     }
 
@@ -439,7 +506,7 @@ show_class(http_t     *http,                /* I - Connection to server */
     * Show the class status...
     */
 
-    cgiCopyTemplateLang("classes.tmpl");
+    cgiCopyTemplateLang("class.tmpl");
 
    /*
     * Show jobs for the specified class...
@@ -455,13 +522,8 @@ show_class(http_t     *http,               /* I - Connection to server */
     */
 
     cgiStartHTML(pclass);
-    cgiShowIPPError(_("Unable to get class status:"));
+    cgiShowIPPError(_("Unable to get class status"));
   }
 
    cgiEndHTML();
 }
-
-
-/*
- * End of "$Id: classes.c 5202 2006-02-28 19:37:03Z mike $".
- */