/*
- * "$Id: printers.c 5104 2006-02-15 03:21:04Z mike $"
+ * Printer status CGI for CUPS.
*
- * Printer 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_printers() - Show all printers...
- * show_printer() - Show a single printer.
+ * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
*/
/*
*/
#include "cgi-private.h"
+#include <errno.h>
/*
* Local functions...
*/
-void show_all_printers(http_t *http, const char *username);
-void show_printer(http_t *http, const char *printer);
+static void do_printer_op(http_t *http, const char *printer, ipp_op_t op,
+ const char *title);
+static void show_all_printers(http_t *http, const char *username);
+static void show_printer(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 *printer; /* Printer name */
const char *user; /* Username */
*/
cgiSetVariable("SECTION", "printers");
+ cgiSetVariable("REFRESH_PAGE", "");
/*
* See if we are displaying a printer or all printers...
*/
- if (!strcmp(argv[0], "/") || strstr(argv[0], "printers.cgi"))
- printer = NULL;
- else
- printer = argv[0];
+ if ((printer = getenv("PATH_INFO")) != NULL)
+ {
+ printer ++;
+
+ if (!*printer)
+ printer = NULL;
+
+ if (printer)
+ cgiSetVariable("PRINTER_NAME", printer);
+ }
/*
* 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_printer(http, printer);
}
- else if (!strcasecmp(op, "print-test-page") && printer)
- cgiPrintTestPage(http, printer);
- else if (!strcasecmp(op, "move-jobs") && printer)
- cgiMoveJobs(http, printer, 0);
+ else if (printer)
+ {
+ 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, "/printers/%s", printer);
+
+ printf("Location: %s\n\n", uri);
+ }
+ else if (!strcmp(op, "start-printer"))
+ do_printer_op(http, printer, IPP_RESUME_PRINTER,
+ cgiText(_("Resume Printer")));
+ else if (!strcmp(op, "stop-printer"))
+ do_printer_op(http, printer, IPP_PAUSE_PRINTER,
+ cgiText(_("Pause Printer")));
+ else if (!strcmp(op, "accept-jobs"))
+ do_printer_op(http, printer, CUPS_ACCEPT_JOBS, cgiText(_("Accept Jobs")));
+ else if (!strcmp(op, "reject-jobs"))
+ do_printer_op(http, printer, CUPS_REJECT_JOBS, cgiText(_("Reject Jobs")));
+ else if (!strcmp(op, "cancel-jobs"))
+ do_printer_op(http, printer, IPP_OP_CANCEL_JOBS, cgiText(_("Cancel Jobs")));
+ else if (!_cups_strcasecmp(op, "print-self-test-page"))
+ cgiPrintCommand(http, printer, "PrintSelfTestPage",
+ cgiText(_("Print Self-Test Page")));
+ else if (!_cups_strcasecmp(op, "clean-print-heads"))
+ cgiPrintCommand(http, printer, "Clean all",
+ cgiText(_("Clean Print Heads")));
+ else if (!_cups_strcasecmp(op, "print-test-page"))
+ cgiPrintTestPage(http, printer);
+ else if (!_cups_strcasecmp(op, "move-jobs"))
+ cgiMoveJobs(http, printer, 0);
+ else
+ {
+ /*
+ * Unknown/bad operation...
+ */
+
+ cgiStartHTML(printer);
+ cgiCopyTemplateLang("error-op.tmpl");
+ cgiEndHTML();
+ }
+ }
else
{
/*
* Unknown/bad operation...
*/
- if (printer)
- cgiStartHTML(printer);
- else
- cgiStartHTML(cgiText(_("Printers")));
-
+ cgiStartHTML(cgiText(_("Printers")));
cgiCopyTemplateLang("error-op.tmpl");
cgiEndHTML();
}
}
+/*
+ * 'do_printer_op()' - Do a printer operation.
+ */
+
+static void
+do_printer_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, "/printers/%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), "/printers/%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);
+
+ 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_printers()' - Show all printers...
*/
-void
+static void
show_all_printers(http_t *http, /* I - Connection to server */
const char *user) /* I - Username */
{
*response; /* IPP response */
cups_array_t *printers; /* Array of printer objects */
ipp_attribute_t *printer; /* Printer object */
- int ascending, /* Order of printers (0 = descending) */
- first, /* First printer to show */
+ int first, /* First printer to show */
count; /* Number of printers */
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 */
fprintf(stderr, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
- http, user);
+ http, user ? user : "(null)");
/*
* Show the standard header...
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
"printer-type-mask", CUPS_PRINTER_CLASS);
- 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, "printers.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");
- else
- ascending = 1;
-
- if (ascending)
- {
- for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, first);
- i < CUPS_PAGE_MAX && printer;
- i ++, printer = (ipp_attribute_t *)cupsArrayNext(printers))
- cgiSetIPPObjectVars(printer, NULL, i);
- }
- else
- {
- for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, count - first - 1);
- i < CUPS_PAGE_MAX && printer;
- i ++, printer = (ipp_attribute_t *)cupsArrayPrev(printers))
- cgiSetIPPObjectVars(printer, NULL, i);
- }
+ for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, first);
+ i < CUPS_PAGE_MAX && printer;
+ i ++, printer = (ipp_attribute_t *)cupsArrayNext(printers))
+ cgiSetIPPObjectVars(printer, NULL, i);
/*
* Save navigation URLs...
*/
- urlend = url + sizeof(url);
-
- if ((var = cgiGetVariable("QUERY")) != NULL)
- {
- strlcpy(url, "/printers/?QUERY=", sizeof(url));
- urlptr = url + strlen(url);
-
- cgiFormEncode(urlptr, var, urlend - urlptr);
- urlptr += strlen(urlptr);
+ cgiSetVariable("THISURL", "/printers/");
- strlcpy(urlptr, "&", urlend - urlptr);
- urlptr += strlen(urlptr);
- }
- else
+ if (first > 0)
{
- strlcpy(url, "/printers/?", 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);
}
/*
cgiCopyTemplateLang("printers-header.tmpl");
- if (count > 0)
+ if (count > CUPS_PAGE_MAX)
cgiCopyTemplateLang("pager.tmpl");
cgiCopyTemplateLang("printers.tmpl");
- if (count > 0)
+ if (count > CUPS_PAGE_MAX)
cgiCopyTemplateLang("pager.tmpl");
/*
* Show the error...
*/
- cgiShowIPPError(_("Unable to get printer list:"));
+ cgiShowIPPError(_("Unable to get printer list"));
}
cgiEndHTML();
* 'show_printer()' - Show a single printer.
*/
-void
+static void
show_printer(http_t *http, /* I - Connection to server */
const char *printer) /* I - Name of printer */
{
fprintf(stderr, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
- http, printer);
+ http, printer ? printer : "(null)");
/*
* Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
uri);
- cgiGetAttributes(request, "printers.tmpl");
+ cgiGetAttributes(request, "printer.tmpl");
/*
* Do the request and get back a response...
*/
cgiFormEncode(uri, printer, sizeof(uri));
- snprintf(refresh, sizeof(refresh), "10;/printers/%s", uri);
+ snprintf(refresh, sizeof(refresh), "10;URL=/printers/%s", uri);
cgiSetVariable("refresh_page", refresh);
}
* Show the printer status...
*/
- cgiCopyTemplateLang("printers.tmpl");
+ cgiCopyTemplateLang("printer.tmpl");
/*
* Show jobs for the specified printer...
*/
cgiStartHTML(printer);
- cgiShowIPPError(_("Unable to get printer status:"));
+ cgiShowIPPError(_("Unable to get printer status"));
}
cgiEndHTML();
}
-
-
-/*
- * End of "$Id: printers.c 5104 2006-02-15 03:21:04Z mike $".
- */