#include "cgi-private.h"
#include <errno.h>
void print_command(http_t *http, const char *printer, const char *command);
void show_all_printers(http_t *http, const char *username);
void show_printer(http_t *http, const char *printer);
int
main(int argc,
char *argv[])
{
const char *printer;
const char *user;
http_t *http;
ipp_t *request,
*response;
ipp_attribute_t *attr;
const char *op;
static const char *def_attrs[] =
{
"printer-name",
"printer-uri-supported"
};
cgiInitialize();
op = cgiGetVariable("OP");
cgiSetVariable("SECTION", "printers");
if ((printer = getenv("PATH_INFO")) != NULL)
{
printer ++;
if (!*printer)
printer = NULL;
}
user = getenv("REMOTE_USER");
http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
if (!op)
{
request = ippNewRequest(CUPS_GET_DEFAULT);
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes",
sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
{
char url[HTTP_MAX_URI];
cgiSetVariable("DEFAULT_URI",
cgiRewriteURL(attr->values[0].string.text,
url, sizeof(url), NULL));
}
ippDelete(response);
}
if (!printer)
show_all_printers(http, user);
else
show_printer(http, printer);
}
else if (!strcasecmp(op, "print-self-test-page") && printer)
print_command(http, printer, "PrintSelfTestPage");
else if (!strcasecmp(op, "clean-print-heads") && printer)
print_command(http, printer, "Clean all");
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)
cgiStartHTML(printer);
else
cgiStartHTML(cgiText(_("Printers")));
cgiCopyTemplateLang("error-op.tmpl");
cgiEndHTML();
}
httpClose(http);
return (0);
}
void
print_command(http_t *http,
const char *printer,
const char *command)
{
cups_file_t *fp;
char filename[1024];
ipp_t *request,
*response;
char uri[HTTP_MAX_URI],
resource[1024],
refresh[1024];
const char *user;
if ((user = getenv("REMOTE_USER")) == NULL)
user = "guest";
if ((fp = cupsTempFile2(filename, sizeof(filename))) == NULL)
{
cgiStartHTML(cgiText(_("Printer Maintenance")));
cgiSetVariable("MESSAGE", _("Unable to create temporary file:"));
cgiSetVariable("ERROR", strerror(errno));
cgiCopyTemplateLang("error.tmpl");
cgiEndHTML();
return;
}
cupsFilePuts(fp, "#CUPS-COMMAND\n");
cupsFilePrintf(fp, "%s\n", command);
cupsFileClose(fp);
snprintf(resource, sizeof(resource), "/printers/%s", printer);
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", ippPort(), "/printers/%s", printer);
request = ippNewRequest(IPP_PRINT_JOB);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, user);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
NULL, "Printer Maintenance");
ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
NULL, "application/postscript");
if ((response = cupsDoFileRequest(http, request, resource,
filename)) != NULL)
{
cgiSetIPPVars(response, NULL, NULL, NULL, 0);
ippDelete(response);
}
unlink(filename);
if (cupsLastError() <= IPP_OK_CONFLICT)
{
cgiFormEncode(uri, resource, sizeof(uri));
snprintf(refresh, sizeof(refresh), "2;URL=%s", uri);
cgiSetVariable("refresh_page", refresh);
}
else if (cupsLastError() == IPP_NOT_AUTHORIZED)
{
puts("Status: 401\n");
exit(0);
}
cgiStartHTML(cgiText(_("Printer Maintenance")));
if (cupsLastError() > IPP_OK_CONFLICT)
cgiShowIPPError(_("Unable to send maintenance job:"));
else
{
cgiSetVariable("PRINTER_NAME", printer);
cgiCopyTemplateLang("maintenance.tmpl");
}
cgiEndHTML();
}
void
show_all_printers(http_t *http,
const char *user)
{
int i;
ipp_t *request,
*response;
cups_array_t *printers;
ipp_attribute_t *printer,
*attr;
int ascending,
first,
count;
const char *var;
void *search;
char url[1024],
*urlptr,
*urlend;
fprintf(stderr, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
http, user ? user : "(null)");
cgiStartHTML(cgiText(_("Printers")));
request = ippNewRequest(CUPS_GET_PRINTERS);
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
"printer-type", 0);
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
"printer-type-mask", CUPS_PRINTER_CLASS);
if (user)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, user);
cgiGetAttributes(request, "printers.tmpl");
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if ((var = cgiGetVariable("QUERY")) != NULL)
search = cgiCompileSearch(var);
else
search = NULL;
printers = cgiGetIPPObjects(response, search);
count = cupsArrayCount(printers);
if (search)
cgiFreeSearch(search);
if ((var = cgiGetVariable("FIRST")) != NULL)
first = atoi(var);
else
first = 0;
if (first >= count)
first = count - CUPS_PAGE_MAX;
first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
if (first < 0)
first = 0;
sprintf(url, "%d", count);
cgiSetVariable("TOTAL", url);
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);
cgiSetArray("cupscommand", i, "0");
for (attr = printer; attr; attr = attr->next)
if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
break;
else if (!strcmp(attr->name, "printer-type"))
{
if (attr->values[0].integer & CUPS_PRINTER_COMMANDS)
cgiSetArray("cupscommand", i, "1");
break;
}
}
}
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);
cgiSetArray("cupscommand", i, "0");
for (attr = printer; attr; attr = attr->next)
if (attr->group_tag == IPP_TAG_ZERO || !attr->name)
break;
else if (!strcmp(attr->name, "printer-type"))
{
if (attr->values[0].integer & CUPS_PRINTER_COMMANDS)
cgiSetArray("cupscommand", i, "1");
break;
}
}
}
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);
strlcpy(urlptr, "&", urlend - urlptr);
urlptr += strlen(urlptr);
}
else
{
strlcpy(url, "/printers/?", sizeof(url));
urlptr = url + strlen(url);
}
snprintf(urlptr, urlend - urlptr, "FIRST=%d", first);
cgiSetVariable("THISURL", url);
if (first > 0)
{
snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
first - CUPS_PAGE_MAX, ascending ? "asc" : "dec");
cgiSetVariable("PREVURL", url);
}
if ((first + CUPS_PAGE_MAX) < count)
{
snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
first + CUPS_PAGE_MAX, ascending ? "asc" : "dec");
cgiSetVariable("NEXTURL", url);
}
cgiCopyTemplateLang("search.tmpl");
cgiCopyTemplateLang("printers-header.tmpl");
if (count > 0)
cgiCopyTemplateLang("pager.tmpl");
cgiCopyTemplateLang("printers.tmpl");
if (count > 0)
cgiCopyTemplateLang("pager.tmpl");
cupsArrayDelete(printers);
ippDelete(response);
}
else
{
cgiShowIPPError(_("Unable to get printer list:"));
}
cgiEndHTML();
}
void
show_printer(http_t *http,
const char *printer)
{
ipp_t *request,
*response;
ipp_attribute_t *attr;
char uri[HTTP_MAX_URI];
char refresh[1024];
fprintf(stderr, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
http, printer ? printer : "(null)");
request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
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);
cgiGetAttributes(request, "printers.tmpl");
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
cgiSetIPPVars(response, NULL, NULL, NULL, 0);
if ((attr = ippFindAttribute(response, "printer-type",
IPP_TAG_ENUM)) != NULL)
{
cgiSetVariable("cupscommand",
(attr->values[0].integer & CUPS_PRINTER_COMMANDS) ?
"1" : "0");
}
if (printer && (attr = ippFindAttribute(response, "printer-state",
IPP_TAG_ENUM)) != NULL &&
attr->values[0].integer == IPP_PRINTER_PROCESSING)
{
cgiFormEncode(uri, printer, sizeof(uri));
snprintf(refresh, sizeof(refresh), "10;URL=/printers/%s", uri);
cgiSetVariable("refresh_page", refresh);
}
ippDelete(response);
cgiStartHTML(printer);
cgiCopyTemplateLang("printers.tmpl");
cgiCopyTemplateLang("printer-jobs-header.tmpl");
cgiShowJobs(http, printer);
}
else
{
cgiStartHTML(printer);
cgiShowIPPError(_("Unable to get printer status:"));
}
cgiEndHTML();
}