/*
- * "$Id: classes.c 5160 2006-02-24 01:14:18Z mike $"
+ * "$Id$"
*
- * Class status CGI for the Common UNIX Printing System (CUPS).
+ * Class status CGI for CUPS.
*
- * Copyright 1997-2006 by Easy Software Products.
+ * Copyright 2007-2014 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
- *
- * Contents:
- *
- * main() - Main entry for CGI.
- * show_all_classes() - Show all classes...
- * show_class() - Show a single class.
+ * 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/".
*/
/*
* 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);
/*
*/
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 */
*/
cgiSetVariable("SECTION", "classes");
+ cgiSetVariable("REFRESH_PAGE", "");
/*
* See if we are displaying a printer or all classes...
*/
if ((pclass = getenv("PATH_INFO")) != NULL)
+ {
pclass ++;
+ 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...
* Get the default printer...
*/
- if (!op)
+ if (!op || !cgiIsPOST())
{
/*
* Get the default destination...
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();
}
}
+/*
+ * '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 */
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 */
/*
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");
* 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;
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");
+ if ((var = cgiGetVariable("ORDER")) != NULL && *var)
+ ascending = !_cups_strcasecmp(var, "asc");
else
ascending = 1;
* Save navigation URLs...
*/
- urlend = url + sizeof(url);
-
- 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
- {
- strlcpy(url, "/classes/?", sizeof(url));
- urlptr = url + strlen(url);
- }
-
- snprintf(urlptr, urlend - urlptr, "FIRST=%d", first);
- cgiSetVariable("THISURL", url);
+ cgiSetVariable("THISURL", "/classes/");
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);
}
/*
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");
/*
* Show the error...
*/
- cgiShowIPPError(_("Unable to get class list:"));
+ cgiShowIPPError(_("Unable to get class list"));
}
cgiEndHTML();
* '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 */
{
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...
*/
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);
}
* Show the class status...
*/
- cgiCopyTemplateLang("classes.tmpl");
+ cgiCopyTemplateLang("class.tmpl");
/*
* Show jobs for the specified class...
*/
cgiStartHTML(pclass);
- cgiShowIPPError(_("Unable to get class status:"));
+ cgiShowIPPError(_("Unable to get class status"));
}
cgiEndHTML();
/*
- * End of "$Id: classes.c 5160 2006-02-24 01:14:18Z mike $".
+ * End of "$Id$".
*/