4 * CGI <-> IPP variable routines for CUPS.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cgiGetAttributes() - Get the list of attributes that are needed by the
19 * cgiGetIPPObjects() - Get the objects in an IPP response.
20 * cgiMoveJobs() - Move one or more jobs.
21 * cgiPrintCommand() - Print a CUPS command job.
22 * cgiPrintTestPage() - Print a test page.
23 * cgiRewriteURL() - Rewrite a printer URI into a web browser URL...
24 * cgiSetIPPObjectVars() - Set CGI variables from an IPP object.
25 * cgiSetIPPVars() - Set CGI variables from an IPP response.
26 * cgiShowIPPError() - Show the last IPP error message.
27 * cgiShowJobs() - Show print jobs.
28 * cgiText() - Return localized text.
32 * Include necessary headers...
35 #include "cgi-private.h"
39 * 'cgiGetAttributes()' - Get the list of attributes that are needed
40 * by the template file.
44 cgiGetAttributes(ipp_t
*request
, /* I - IPP request */
45 const char *tmpl
) /* I - Base filename */
47 int num_attrs
; /* Number of attributes */
48 char *attrs
[1000]; /* Attributes */
49 int i
; /* Looping var */
50 char filename
[1024], /* Filename */
51 locale
[16]; /* Locale name */
52 const char *directory
, /* Directory */
54 FILE *in
; /* Input file */
55 int ch
; /* Character from file */
56 char name
[255], /* Name of variable */
57 *nameptr
; /* Pointer into name */
61 * Convert the language to a locale name...
64 if ((lang
= getenv("LANG")) != NULL
)
66 for (i
= 0; lang
[i
] && i
< 15; i
++)
67 if (isalnum(lang
[i
] & 255))
68 locale
[i
] = tolower(lang
[i
]);
78 * See if we have a template file for this language...
81 directory
= cgiGetTemplateDir();
83 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
84 if (access(filename
, 0))
88 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
89 if (access(filename
, 0))
90 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
94 * Open the template file...
97 if ((in
= fopen(filename
, "r")) == NULL
)
101 * Loop through the file adding attribute names as needed...
105 attrs
[0] = NULL
; /* Eliminate compiler warning */
107 while ((ch
= getc(in
)) != EOF
)
110 else if (ch
== '{' && num_attrs
< (sizeof(attrs
) / sizeof(attrs
[0])))
116 for (nameptr
= name
; (ch
= getc(in
)) != EOF
;)
117 if (strchr("}]<>=!~ \t\n", ch
))
119 else if (nameptr
> name
&& ch
== '?')
121 else if (nameptr
< (name
+ sizeof(name
) - 1))
131 if (!strncmp(name
, "printer_state_history", 21))
132 strcpy(name
, "printer_state_history");
135 * Possibly add it to the list of attributes...
138 for (i
= 0; i
< num_attrs
; i
++)
139 if (!strcmp(attrs
[i
], name
))
144 attrs
[num_attrs
] = strdup(name
);
150 * If we have attributes, add a requested-attributes attribute to the
156 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
157 "requested-attributes", num_attrs
, NULL
, (const char **)attrs
);
159 for (i
= 0; i
< num_attrs
; i
++)
168 * 'cgiGetIPPObjects()' - Get the objects in an IPP response.
171 cups_array_t
* /* O - Array of objects */
172 cgiGetIPPObjects(ipp_t
*response
, /* I - IPP response */
173 void *search
) /* I - Search filter */
175 int i
; /* Looping var */
176 cups_array_t
*objs
; /* Array of objects */
177 ipp_attribute_t
*attr
, /* Current attribute */
178 *first
; /* First attribute for object */
179 ipp_tag_t group
; /* Current group tag */
180 int add
; /* Add this object to the array? */
186 for (add
= 0, first
= NULL
, objs
= cupsArrayNew(NULL
, NULL
),
187 group
= IPP_TAG_ZERO
, attr
= response
->attrs
;
191 if (attr
->group_tag
!= group
)
193 group
= attr
->group_tag
;
195 if (group
!= IPP_TAG_ZERO
&& group
!= IPP_TAG_OPERATION
)
200 else if (add
&& first
)
202 cupsArrayAdd(objs
, first
);
209 if (attr
->name
&& attr
->group_tag
!= IPP_TAG_OPERATION
&& !add
)
214 * Add all objects if there is no search...
222 * Check the search string against the string and integer values.
225 switch (attr
->value_tag
)
227 case IPP_TAG_TEXTLANG
:
228 case IPP_TAG_NAMELANG
:
231 case IPP_TAG_KEYWORD
:
233 case IPP_TAG_MIMETYPE
:
234 for (i
= 0; !add
&& i
< attr
->num_values
; i
++)
235 if (cgiDoSearch(search
, attr
->values
[i
].string
.text
))
239 case IPP_TAG_INTEGER
:
240 for (i
= 0; !add
&& i
< attr
->num_values
; i
++)
242 char buf
[255]; /* Number buffer */
245 sprintf(buf
, "%d", attr
->values
[i
].integer
);
247 if (cgiDoSearch(search
, buf
))
260 cupsArrayAdd(objs
, first
);
267 * 'cgiMoveJobs()' - Move one or more jobs.
269 * At least one of dest or job_id must be non-zero/NULL.
273 cgiMoveJobs(http_t
*http
, /* I - Connection to server */
274 const char *dest
, /* I - Destination or NULL */
275 int job_id
) /* I - Job ID or 0 for all */
277 int i
; /* Looping var */
278 const char *user
; /* Username */
279 ipp_t
*request
, /* IPP request */
280 *response
; /* IPP response */
281 ipp_attribute_t
*attr
; /* Current attribute */
282 const char *name
; /* Destination name */
283 const char *job_printer_uri
; /* JOB_PRINTER_URI form variable */
284 char current_dest
[1024]; /* Current destination */
288 * Make sure we have a username...
291 if ((user
= getenv("REMOTE_USER")) == NULL
)
293 puts("Status: 401\n");
298 * See if the user has already selected a new destination...
301 if ((job_printer_uri
= cgiGetVariable("JOB_PRINTER_URI")) == NULL
)
304 * Make sure necessary form variables are set...
309 char temp
[255]; /* Temporary string */
312 sprintf(temp
, "%d", job_id
);
313 cgiSetVariable("JOB_ID", temp
);
317 cgiSetVariable("PRINTER_NAME", dest
);
320 * No new destination specified, show the user what the available
321 * printers/classes are...
327 * Get the current destination for job N...
330 char job_uri
[1024]; /* Job URI */
333 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
335 snprintf(job_uri
, sizeof(job_uri
), "ipp://localhost/jobs/%d", job_id
);
336 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
338 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
339 "requested-attributes", NULL
, "job-printer-uri");
341 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
343 if ((attr
= ippFindAttribute(response
, "job-printer-uri",
344 IPP_TAG_URI
)) != NULL
)
347 * Pull the name from the URI...
350 strlcpy(current_dest
, strrchr(attr
->values
[0].string
.text
, '/') + 1,
351 sizeof(current_dest
));
361 * Couldn't get the current destination...
364 cgiStartHTML(cgiText(_("Move Job")));
365 cgiShowIPPError(_("Unable to find destination for job"));
372 * Get the list of available destinations...
375 request
= ippNewRequest(CUPS_GET_PRINTERS
);
377 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
378 "requested-attributes", NULL
, "printer-uri-supported");
381 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
382 "requesting-user-name", NULL
, user
);
384 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type",
386 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask",
387 CUPS_PRINTER_SCANNER
);
389 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
391 for (i
= 0, attr
= ippFindAttribute(response
, "printer-uri-supported",
394 attr
= ippFindNextAttribute(response
, "printer-uri-supported",
398 * Pull the name from the URI...
401 name
= strrchr(attr
->values
[0].string
.text
, '/') + 1;
404 * If the name is not the same as the current destination, add it!
407 if (_cups_strcasecmp(name
, dest
))
409 cgiSetArray("JOB_PRINTER_URI", i
, attr
->values
[0].string
.text
);
410 cgiSetArray("JOB_PRINTER_NAME", i
, name
);
423 cgiStartHTML(cgiText(_("Move Job")));
425 cgiStartHTML(cgiText(_("Move All Jobs")));
427 if (cgiGetSize("JOB_PRINTER_NAME") > 0)
428 cgiCopyTemplateLang("job-move.tmpl");
432 cgiSetVariable("MESSAGE", cgiText(_("Unable to move job")));
434 cgiSetVariable("MESSAGE", cgiText(_("Unable to move jobs")));
436 cgiSetVariable("ERROR", cgiText(_("No destinations added.")));
437 cgiCopyTemplateLang("error.tmpl");
443 * Try moving the job or jobs...
446 char uri
[1024], /* Job/printer URI */
447 resource
[1024], /* Post resource */
448 refresh
[1024]; /* Refresh URL */
449 const char *job_printer_name
; /* New printer name */
452 request
= ippNewRequest(CUPS_MOVE_JOB
);
460 snprintf(resource
, sizeof(resource
), "/jobs/%d", job_id
);
462 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", job_id
);
463 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
469 * Move all active jobs on a destination...
472 snprintf(resource
, sizeof(resource
), "/%s/%s",
473 cgiGetVariable("SECTION"), dest
);
475 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
476 "localhost", ippPort(), "/%s/%s",
477 cgiGetVariable("SECTION"), dest
);
478 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
482 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-printer-uri",
483 NULL
, job_printer_uri
);
485 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
486 "requesting-user-name", NULL
, user
);
488 ippDelete(cupsDoRequest(http
, request
, resource
));
491 * Show the results...
494 job_printer_name
= strrchr(job_printer_uri
, '/') + 1;
496 if (cupsLastError() <= IPP_OK_CONFLICT
)
498 const char *path
= strstr(job_printer_uri
, "/printers/");
501 path
= strstr(job_printer_uri
, "/classes/");
502 cgiSetVariable("IS_CLASS", "YES");
507 cgiFormEncode(uri
, path
, sizeof(uri
));
508 snprintf(refresh
, sizeof(refresh
), "2;URL=%s", uri
);
509 cgiSetVariable("refresh_page", refresh
);
514 cgiStartHTML(cgiText(_("Move Job")));
516 cgiStartHTML(cgiText(_("Move All Jobs")));
518 if (cupsLastError() > IPP_OK_CONFLICT
)
521 cgiShowIPPError(_("Unable to move job"));
523 cgiShowIPPError(_("Unable to move jobs"));
527 cgiSetVariable("JOB_PRINTER_NAME", job_printer_name
);
528 cgiCopyTemplateLang("job-moved.tmpl");
537 * 'cgiPrintCommand()' - Print a CUPS command job.
541 cgiPrintCommand(http_t
*http
, /* I - Connection to server */
542 const char *dest
, /* I - Destination printer */
543 const char *command
, /* I - Command to send */
544 const char *title
) /* I - Page/job title */
546 int job_id
; /* Command file job */
547 char uri
[HTTP_MAX_URI
], /* Job URI */
548 resource
[1024], /* Printer resource path */
549 refresh
[1024], /* Refresh URL */
550 command_file
[1024]; /* Command "file" */
551 http_status_t status
; /* Document status */
552 cups_option_t hold_option
; /* job-hold-until option */
553 const char *user
; /* User name */
554 ipp_t
*request
, /* Get-Job-Attributes request */
555 *response
; /* Get-Job-Attributes response */
556 ipp_attribute_t
*attr
; /* Current job attribute */
557 static const char const *job_attrs
[] =/* Job attributes we want */
560 "job-printer-state-message"
565 * Create the CUPS command file...
568 snprintf(command_file
, sizeof(command_file
), "#CUPS-COMMAND\n%s\n", command
);
574 if (cgiSupportsMultipart())
578 cgiCopyTemplateLang("command.tmpl");
584 * Send the command file job...
587 hold_option
.name
= "job-hold-until";
588 hold_option
.value
= "no-hold";
590 if ((user
= getenv("REMOTE_USER")) != NULL
)
593 cupsSetUser("anonymous");
595 if ((job_id
= cupsCreateJob(http
, dest
, title
,
596 1, &hold_option
)) < 1)
598 cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver")));
599 cgiSetVariable("ERROR", cupsLastErrorString());
601 cgiCopyTemplateLang("error.tmpl");
604 if (cgiSupportsMultipart())
609 status
= cupsStartDocument(http
, dest
, job_id
, NULL
, CUPS_FORMAT_COMMAND
, 1);
610 if (status
== HTTP_CONTINUE
)
611 status
= cupsWriteRequestData(http
, command_file
,
612 strlen(command_file
));
613 if (status
== HTTP_CONTINUE
)
614 cupsFinishDocument(http
, dest
);
616 if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE
)
618 cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver")));
619 cgiSetVariable("ERROR", cupsLastErrorString());
621 cgiCopyTemplateLang("error.tmpl");
624 if (cgiSupportsMultipart())
627 cupsCancelJob(dest
, job_id
);
632 * Wait for the job to complete...
635 if (cgiSupportsMultipart())
640 * Get the current job state...
643 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", job_id
);
644 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
645 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
648 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
649 "requesting-user-name", NULL
, user
);
650 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
651 "requested-attributes", 2, NULL
, job_attrs
);
653 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
654 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
656 attr
= ippFindAttribute(response
, "job-state", IPP_TAG_ENUM
);
657 if (!attr
|| attr
->values
[0].integer
>= IPP_JOB_STOPPED
||
658 attr
->values
[0].integer
== IPP_JOB_HELD
)
665 * Job not complete, so update the status...
671 cgiCopyTemplateLang("command.tmpl");
680 * Send the final page that reloads the printer's page...
683 snprintf(resource
, sizeof(resource
), "/printers/%s", dest
);
685 cgiFormEncode(uri
, resource
, sizeof(uri
));
686 snprintf(refresh
, sizeof(refresh
), "5;URL=%s", uri
);
687 cgiSetVariable("refresh_page", refresh
);
690 cgiCopyTemplateLang("command.tmpl");
693 if (cgiSupportsMultipart())
699 * 'cgiPrintTestPage()' - Print a test page.
703 cgiPrintTestPage(http_t
*http
, /* I - Connection to server */
704 const char *dest
) /* I - Destination printer/class */
706 ipp_t
*request
, /* IPP request */
707 *response
; /* IPP response */
708 char uri
[HTTP_MAX_URI
], /* Printer URI */
709 resource
[1024], /* POST resource path */
710 refresh
[1024], /* Refresh URL */
711 filename
[1024]; /* Test page filename */
712 const char *datadir
; /* CUPS_DATADIR env var */
713 const char *user
; /* Username */
717 * See who is logged in...
720 user
= getenv("REMOTE_USER");
723 * Locate the test page file...
726 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
727 datadir
= CUPS_DATADIR
;
729 snprintf(filename
, sizeof(filename
), "%s/data/testprint", datadir
);
732 * Point to the printer/class...
735 snprintf(resource
, sizeof(resource
), "/%s/%s", cgiGetVariable("SECTION"),
738 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
739 "localhost", ippPort(), "/%s/%s", cgiGetVariable("SECTION"),
743 * Build an IPP_PRINT_JOB request, which requires the following
747 * attributes-natural-language
749 * requesting-user-name
752 request
= ippNewRequest(IPP_PRINT_JOB
);
754 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
758 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
759 "requesting-user-name", NULL
, user
);
761 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
765 * Do the request and get back a response...
768 if ((response
= cupsDoFileRequest(http
, request
, resource
,
771 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
776 if (cupsLastError() <= IPP_OK_CONFLICT
)
779 * Automatically reload the printer status page...
782 cgiFormEncode(uri
, resource
, sizeof(uri
));
783 snprintf(refresh
, sizeof(refresh
), "2;URL=%s", uri
);
784 cgiSetVariable("refresh_page", refresh
);
786 else if (cupsLastError() == IPP_NOT_AUTHORIZED
)
788 puts("Status: 401\n");
792 cgiStartHTML(cgiText(_("Print Test Page")));
794 if (cupsLastError() > IPP_OK_CONFLICT
)
795 cgiShowIPPError(_("Unable to print test page"));
798 cgiSetVariable("PRINTER_NAME", dest
);
800 cgiCopyTemplateLang("test-page.tmpl");
808 * 'cgiRewriteURL()' - Rewrite a printer URI into a web browser URL...
811 char * /* O - New URL */
812 cgiRewriteURL(const char *uri
, /* I - Current URI */
813 char *url
, /* O - New URL */
814 int urlsize
, /* I - Size of URL buffer */
815 const char *newresource
) /* I - Replacement resource */
817 char scheme
[HTTP_MAX_URI
],
818 userpass
[HTTP_MAX_URI
],
819 hostname
[HTTP_MAX_URI
],
820 rawresource
[HTTP_MAX_URI
],
821 resource
[HTTP_MAX_URI
],
822 /* URI components... */
823 *rawptr
, /* Pointer into rawresource */
824 *resptr
; /* Pointer into resource */
825 int port
; /* Port number */
826 static int ishttps
= -1; /* Using encryption? */
827 static const char *server
; /* Name of server */
828 static char servername
[1024];
829 /* Local server name */
830 static const char hexchars
[] = "0123456789ABCDEF";
831 /* Hexadecimal conversion characters */
835 * Check if we have been called before...
841 * No, initialize static vars for the conversion...
843 * First get the server name associated with the client interface as
844 * well as the locally configured hostname. We'll check *both* of
845 * these to see if the printer URL is local...
848 if ((server
= getenv("SERVER_NAME")) == NULL
)
851 httpGetHostname(NULL
, servername
, sizeof(servername
));
854 * Then flag whether we are using SSL on this connection...
857 ishttps
= getenv("HTTPS") != NULL
;
861 * Convert the URI to a URL...
864 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
,
865 sizeof(userpass
), hostname
, sizeof(hostname
), &port
,
866 rawresource
, sizeof(rawresource
));
868 if (!strcmp(scheme
, "ipp") ||
869 !strcmp(scheme
, "http") ||
870 !strcmp(scheme
, "https"))
875 * Force the specified resource name instead of the one in the URL...
878 strlcpy(resource
, newresource
, sizeof(resource
));
883 * Rewrite the resource string so it doesn't contain any
887 for (rawptr
= rawresource
, resptr
= resource
; *rawptr
; rawptr
++)
888 if ((*rawptr
& 128) || *rawptr
== '%' || *rawptr
== ' ' ||
889 *rawptr
== '#' || *rawptr
== '?' ||
890 *rawptr
== '.') /* For MSIE */
892 if (resptr
< (resource
+ sizeof(resource
) - 3))
895 *resptr
++ = hexchars
[(*rawptr
>> 4) & 15];
896 *resptr
++ = hexchars
[*rawptr
& 15];
899 else if (resptr
< (resource
+ sizeof(resource
) - 1))
906 * Map local access to a local URI...
909 if (!_cups_strcasecmp(hostname
, "127.0.0.1") ||
910 !_cups_strcasecmp(hostname
, "[::1]") ||
911 !_cups_strcasecmp(hostname
, "localhost") ||
912 !_cups_strncasecmp(hostname
, "localhost.", 10) ||
913 !_cups_strcasecmp(hostname
, server
) ||
914 !_cups_strcasecmp(hostname
, servername
))
917 * Make URI relative to the current server...
920 strlcpy(url
, resource
, urlsize
);
925 * Rewrite URI with HTTP/HTTPS scheme...
929 snprintf(url
, urlsize
, "%s://%s@%s:%d%s",
930 ishttps
? "https" : "http",
931 userpass
, hostname
, port
, resource
);
933 snprintf(url
, urlsize
, "%s://%s:%d%s",
934 ishttps
? "https" : "http",
935 hostname
, port
, resource
);
939 strlcpy(url
, uri
, urlsize
);
946 * 'cgiSetIPPObjectVars()' - Set CGI variables from an IPP object.
949 ipp_attribute_t
* /* O - Next object */
951 ipp_attribute_t
*obj
, /* I - Response data to be copied... */
952 const char *prefix
, /* I - Prefix for name or NULL */
953 int element
) /* I - Parent element number */
955 ipp_attribute_t
*attr
; /* Attribute in response... */
956 int i
; /* Looping var */
957 char name
[1024], /* Name of attribute */
958 *nameptr
, /* Pointer into name */
959 value
[16384], /* Value(s) */
960 *valptr
; /* Pointer into value */
961 struct tm
*date
; /* Date information */
964 fprintf(stderr
, "DEBUG2: cgiSetIPPObjectVars(obj=%p, prefix=\"%s\", "
966 obj
, prefix
? prefix
: "(null)", element
);
969 * Set common CGI template variables...
973 cgiSetServerVersion();
976 * Loop through the attributes and set them for the template...
979 for (attr
= obj
; attr
&& attr
->group_tag
!= IPP_TAG_ZERO
; attr
= attr
->next
)
982 * Copy the attribute name, substituting "_" for "-"...
990 snprintf(name
, sizeof(name
), "%s.", prefix
);
991 nameptr
= name
+ strlen(name
);
996 for (i
= 0; attr
->name
[i
] && nameptr
< (name
+ sizeof(name
) - 1); i
++)
997 if (attr
->name
[i
] == '-')
1000 *nameptr
++ = attr
->name
[i
];
1005 * Add "job_printer_name" variable if we have a "job_printer_uri"
1009 if (!strcmp(name
, "job_printer_uri"))
1011 if ((valptr
= strrchr(attr
->values
[0].string
.text
, '/')) == NULL
)
1016 cgiSetArray("job_printer_name", element
, valptr
);
1020 * Localize event names in "notify_events" variable...
1023 if (!strcmp(name
, "notify_events"))
1025 size_t remaining
; /* Remaining bytes in buffer */
1031 for (i
= 0; i
< attr
->num_values
; i
++)
1033 if (valptr
>= (value
+ sizeof(value
) - 3))
1042 remaining
= sizeof(value
) - (valptr
- value
);
1044 if (!strcmp(attr
->values
[i
].string
.text
, "printer-stopped"))
1045 strlcpy(valptr
, _("Printer Paused"), remaining
);
1046 else if (!strcmp(attr
->values
[i
].string
.text
, "printer-added"))
1047 strlcpy(valptr
, _("Printer Added"), remaining
);
1048 else if (!strcmp(attr
->values
[i
].string
.text
, "printer-modified"))
1049 strlcpy(valptr
, _("Printer Modified"), remaining
);
1050 else if (!strcmp(attr
->values
[i
].string
.text
, "printer-deleted"))
1051 strlcpy(valptr
, _("Printer Deleted"), remaining
);
1052 else if (!strcmp(attr
->values
[i
].string
.text
, "job-created"))
1053 strlcpy(valptr
, _("Job Created"), remaining
);
1054 else if (!strcmp(attr
->values
[i
].string
.text
, "job-completed"))
1055 strlcpy(valptr
, _("Job Completed"), remaining
);
1056 else if (!strcmp(attr
->values
[i
].string
.text
, "job-stopped"))
1057 strlcpy(valptr
, _("Job Stopped"), remaining
);
1058 else if (!strcmp(attr
->values
[i
].string
.text
, "job-config-changed"))
1059 strlcpy(valptr
, _("Job Options Changed"), remaining
);
1060 else if (!strcmp(attr
->values
[i
].string
.text
, "server-restarted"))
1061 strlcpy(valptr
, _("Server Restarted"), remaining
);
1062 else if (!strcmp(attr
->values
[i
].string
.text
, "server-started"))
1063 strlcpy(valptr
, _("Server Started"), remaining
);
1064 else if (!strcmp(attr
->values
[i
].string
.text
, "server-stopped"))
1065 strlcpy(valptr
, _("Server Stopped"), remaining
);
1066 else if (!strcmp(attr
->values
[i
].string
.text
, "server-audit"))
1067 strlcpy(valptr
, _("Server Security Auditing"), remaining
);
1069 strlcpy(valptr
, attr
->values
[i
].string
.text
, remaining
);
1071 valptr
+= strlen(valptr
);
1074 cgiSetArray("notify_events", element
, value
);
1079 * Add "notify_printer_name" variable if we have a "notify_printer_uri"
1083 if (!strcmp(name
, "notify_printer_uri"))
1085 if ((valptr
= strrchr(attr
->values
[0].string
.text
, '/')) == NULL
)
1090 cgiSetArray("notify_printer_name", element
, valptr
);
1094 * Add "notify_recipient_name" variable if we have a "notify_recipient_uri"
1095 * attribute, and rewrite recipient URI...
1098 if (!strcmp(name
, "notify_recipient_uri"))
1100 char uri
[1024], /* New URI */
1101 scheme
[32], /* Scheme portion of URI */
1102 userpass
[256], /* Username/password portion of URI */
1103 host
[1024], /* Hostname portion of URI */
1104 resource
[1024], /* Resource portion of URI */
1105 *options
; /* Options in URI */
1106 int port
; /* Port number */
1109 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
,
1110 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
1111 host
, sizeof(host
), &port
, resource
, sizeof(resource
));
1113 if (!strcmp(scheme
, "rss"))
1116 * RSS notification...
1119 if ((options
= strchr(resource
, '?')) != NULL
)
1125 * Link to remote feed...
1128 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "http",
1129 userpass
, host
, port
, resource
);
1130 strlcpy(name
, uri
, sizeof(name
));
1135 * Link to local feed...
1138 snprintf(uri
, sizeof(uri
), "/rss%s", resource
);
1139 strlcpy(name
, resource
+ 1, sizeof(name
));
1148 strlcpy(uri
, attr
->values
[0].string
.text
, sizeof(uri
));
1149 strlcpy(name
, resource
, sizeof(name
));
1152 cgiSetArray("notify_recipient_uri", element
, uri
);
1153 cgiSetArray("notify_recipient_name", element
, name
);
1158 * Add "admin_uri" variable if we have a "printer_uri_supported"
1162 if (!strcmp(name
, "printer_uri_supported"))
1164 cgiRewriteURL(attr
->values
[0].string
.text
, value
, sizeof(value
),
1167 cgiSetArray("admin_uri", element
, value
);
1174 value
[0] = '\0'; /* Initially an empty string */
1175 valptr
= value
; /* Start at the beginning */
1177 for (i
= 0; i
< attr
->num_values
; i
++)
1180 strlcat(valptr
, ", ", sizeof(value
) - (valptr
- value
));
1182 valptr
+= strlen(valptr
);
1184 switch (attr
->value_tag
)
1186 case IPP_TAG_INTEGER
:
1188 if (strncmp(name
, "time_at_", 8) == 0)
1190 time_t t
; /* Temporary time value */
1192 t
= (time_t)attr
->values
[i
].integer
;
1193 date
= localtime(&t
);
1195 strftime(valptr
, sizeof(value
) - (valptr
- value
), "%c", date
);
1198 snprintf(valptr
, sizeof(value
) - (valptr
- value
),
1199 "%d", attr
->values
[i
].integer
);
1202 case IPP_TAG_BOOLEAN
:
1203 snprintf(valptr
, sizeof(value
) - (valptr
- value
),
1204 "%d", attr
->values
[i
].boolean
);
1207 case IPP_TAG_NOVALUE
:
1208 strlcat(valptr
, "novalue", sizeof(value
) - (valptr
- value
));
1211 case IPP_TAG_RANGE
:
1212 snprintf(valptr
, sizeof(value
) - (valptr
- value
),
1213 "%d-%d", attr
->values
[i
].range
.lower
,
1214 attr
->values
[i
].range
.upper
);
1217 case IPP_TAG_RESOLUTION
:
1218 snprintf(valptr
, sizeof(value
) - (valptr
- value
),
1219 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
1220 attr
->values
[i
].resolution
.yres
,
1221 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
1226 if (strchr(attr
->values
[i
].string
.text
, ':') &&
1227 strcmp(name
, "device_uri"))
1233 if (!strcmp(name
, "member_uris"))
1235 char url
[1024]; /* URL for class member... */
1238 cgiRewriteURL(attr
->values
[i
].string
.text
, url
,
1241 snprintf(valptr
, sizeof(value
) - (valptr
- value
),
1242 "<A HREF=\"%s\">%s</A>", url
,
1243 strrchr(attr
->values
[i
].string
.text
, '/') + 1);
1246 cgiRewriteURL(attr
->values
[i
].string
.text
, valptr
,
1247 sizeof(value
) - (valptr
- value
), NULL
);
1251 case IPP_TAG_STRING
:
1254 case IPP_TAG_KEYWORD
:
1255 case IPP_TAG_CHARSET
:
1256 case IPP_TAG_LANGUAGE
:
1257 case IPP_TAG_MIMETYPE
:
1258 strlcat(valptr
, attr
->values
[i
].string
.text
,
1259 sizeof(value
) - (valptr
- value
));
1262 case IPP_TAG_BEGIN_COLLECTION
:
1263 snprintf(value
, sizeof(value
), "%s%d", name
, i
+ 1);
1264 cgiSetIPPVars(attr
->values
[i
].collection
, NULL
, NULL
, value
,
1269 break; /* anti-compiler-warning-code */
1274 * Add the element...
1277 if (attr
->value_tag
!= IPP_TAG_BEGIN_COLLECTION
)
1279 cgiSetArray(name
, element
, value
);
1281 fprintf(stderr
, "DEBUG2: %s[%d]=\"%s\"\n", name
, element
, value
);
1285 return (attr
? attr
->next
: NULL
);
1290 * 'cgiSetIPPVars()' - Set CGI variables from an IPP response.
1293 int /* O - Maximum number of elements */
1294 cgiSetIPPVars(ipp_t
*response
, /* I - Response data to be copied... */
1295 const char *filter_name
, /* I - Filter name */
1296 const char *filter_value
, /* I - Filter value */
1297 const char *prefix
, /* I - Prefix for name or NULL */
1298 int parent_el
) /* I - Parent element number */
1300 int element
; /* Element in CGI array */
1301 ipp_attribute_t
*attr
, /* Attribute in response... */
1302 *filter
; /* Filtering attribute */
1305 fprintf(stderr
, "DEBUG2: cgiSetIPPVars(response=%p, filter_name=\"%s\", "
1306 "filter_value=\"%s\", prefix=\"%s\", parent_el=%d)\n",
1307 response
, filter_name
? filter_name
: "(null)",
1308 filter_value
? filter_value
: "(null)",
1309 prefix
? prefix
: "(null)", parent_el
);
1312 * Set common CGI template variables...
1316 cgiSetServerVersion();
1319 * Loop through the attributes and set them for the template...
1322 attr
= response
->attrs
;
1325 while (attr
&& attr
->group_tag
== IPP_TAG_OPERATION
)
1328 for (element
= parent_el
; attr
; element
++)
1331 * Copy attributes to a separator...
1334 while (attr
&& attr
->group_tag
== IPP_TAG_ZERO
)
1343 filter
!= NULL
&& filter
->group_tag
!= IPP_TAG_ZERO
;
1344 filter
= filter
->next
)
1345 if (filter
->name
&& !strcmp(filter
->name
, filter_name
) &&
1346 (filter
->value_tag
== IPP_TAG_STRING
||
1347 (filter
->value_tag
>= IPP_TAG_TEXTLANG
&&
1348 filter
->value_tag
<= IPP_TAG_MIMETYPE
)) &&
1349 filter
->values
[0].string
.text
!= NULL
&&
1350 !_cups_strcasecmp(filter
->values
[0].string
.text
, filter_value
))
1354 return (element
+ 1);
1356 if (filter
->group_tag
== IPP_TAG_ZERO
)
1364 attr
= cgiSetIPPObjectVars(attr
, prefix
, element
);
1367 fprintf(stderr
, "DEBUG2: Returing %d from cgiSetIPPVars()...\n", element
);
1374 * 'cgiShowIPPError()' - Show the last IPP error message.
1376 * The caller must still call cgiStartHTML() and cgiEndHTML().
1380 cgiShowIPPError(const char *message
) /* I - Contextual message */
1382 cgiSetVariable("MESSAGE", cgiText(message
));
1383 cgiSetVariable("ERROR", cupsLastErrorString());
1384 cgiCopyTemplateLang("error.tmpl");
1389 * 'cgiShowJobs()' - Show print jobs.
1393 cgiShowJobs(http_t
*http
, /* I - Connection to server */
1394 const char *dest
) /* I - Destination name or NULL */
1396 int i
; /* Looping var */
1397 const char *which_jobs
; /* Which jobs to show */
1398 ipp_t
*request
, /* IPP request */
1399 *response
; /* IPP response */
1400 cups_array_t
*jobs
; /* Array of job objects */
1401 ipp_attribute_t
*job
; /* Job object */
1402 int ascending
, /* Order of jobs (0 = descending) */
1403 first
, /* First job to show */
1404 count
; /* Number of jobs */
1405 const char *var
, /* Form variable */
1406 *query
, /* Query string */
1407 *section
; /* Section in web interface */
1408 void *search
; /* Search data */
1409 char url
[1024], /* Printer URI */
1410 val
[1024]; /* Form variable */
1414 * Build an IPP_GET_JOBS request, which requires the following
1417 * attributes-charset
1418 * attributes-natural-language
1422 request
= ippNewRequest(IPP_GET_JOBS
);
1426 httpAssembleURIf(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "ipp", NULL
,
1427 "localhost", ippPort(), "/printers/%s", dest
);
1428 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1432 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1433 "ipp://localhost/");
1435 if ((which_jobs
= cgiGetVariable("which_jobs")) != NULL
)
1436 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "which-jobs",
1439 cgiGetAttributes(request
, "jobs.tmpl");
1442 * Do the request and get back a response...
1445 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1448 * Get a list of matching job objects.
1451 if ((query
= cgiGetVariable("QUERY")) != NULL
&&
1452 !cgiGetVariable("CLEAR"))
1453 search
= cgiCompileSearch(query
);
1460 jobs
= cgiGetIPPObjects(response
, search
);
1461 count
= cupsArrayCount(jobs
);
1464 cgiFreeSearch(search
);
1467 * Figure out which jobs to display...
1470 if ((var
= cgiGetVariable("FIRST")) != NULL
)
1476 first
= count
- CUPS_PAGE_MAX
;
1478 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
1483 if ((var
= cgiGetVariable("ORDER")) != NULL
)
1484 ascending
= !_cups_strcasecmp(var
, "asc");
1486 ascending
= !which_jobs
|| !_cups_strcasecmp(which_jobs
, "not-completed");
1488 section
= cgiGetVariable("SECTION");
1490 cgiClearVariables();
1493 cgiSetVariable("QUERY", query
);
1495 cgiSetVariable("ORDER", ascending
? "asc" : "dec");
1497 cgiSetVariable("SECTION", section
);
1499 sprintf(val
, "%d", count
);
1500 cgiSetVariable("TOTAL", val
);
1503 cgiSetVariable("WHICH_JOBS", which_jobs
);
1507 for (i
= 0, job
= (ipp_attribute_t
*)cupsArrayIndex(jobs
, first
);
1508 i
< CUPS_PAGE_MAX
&& job
;
1509 i
++, job
= (ipp_attribute_t
*)cupsArrayNext(jobs
))
1510 cgiSetIPPObjectVars(job
, NULL
, i
);
1514 for (i
= 0, job
= (ipp_attribute_t
*)cupsArrayIndex(jobs
, count
- first
- 1);
1515 i
< CUPS_PAGE_MAX
&& job
;
1516 i
++, job
= (ipp_attribute_t
*)cupsArrayPrev(jobs
))
1517 cgiSetIPPObjectVars(job
, NULL
, i
);
1521 * Save navigation URLs...
1526 snprintf(val
, sizeof(val
), "/%s/%s", section
, dest
);
1527 cgiSetVariable("PRINTER_NAME", dest
);
1528 cgiSetVariable("PRINTER_URI_SUPPORTED", val
);
1531 strlcpy(val
, "/jobs/", sizeof(val
));
1533 cgiSetVariable("THISURL", val
);
1537 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
1538 cgiSetVariable("PREV", val
);
1541 if ((first
+ CUPS_PAGE_MAX
) < count
)
1543 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
1544 cgiSetVariable("NEXT", val
);
1548 * Then show everything...
1552 cgiSetVariable("SEARCH_DEST", dest
);
1554 cgiCopyTemplateLang("search.tmpl");
1556 cgiCopyTemplateLang("jobs-header.tmpl");
1558 if (count
> CUPS_PAGE_MAX
)
1559 cgiCopyTemplateLang("pager.tmpl");
1561 cgiCopyTemplateLang("jobs.tmpl");
1563 if (count
> CUPS_PAGE_MAX
)
1564 cgiCopyTemplateLang("pager.tmpl");
1566 cupsArrayDelete(jobs
);
1567 ippDelete(response
);
1573 * 'cgiText()' - Return localized text.
1576 const char * /* O - Localized message */
1577 cgiText(const char *message
) /* I - Message */
1579 static cups_lang_t
*language
= NULL
;
1584 language
= cupsLangDefault();
1586 return (_cupsLangString(language
, message
));