2 * CGI <-> IPP variable routines for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cgi-private.h"
18 * 'cgiGetAttributes()' - Get the list of attributes that are needed
19 * by the template file.
23 cgiGetAttributes(ipp_t
*request
, /* I - IPP request */
24 const char *tmpl
) /* I - Base filename */
26 int num_attrs
; /* Number of attributes */
27 char *attrs
[1000]; /* Attributes */
28 int i
; /* Looping var */
29 char filename
[1024], /* Filename */
30 locale
[16]; /* Locale name */
31 const char *directory
, /* Directory */
33 FILE *in
; /* Input file */
34 int ch
; /* Character from file */
35 char name
[255], /* Name of variable */
36 *nameptr
; /* Pointer into name */
40 * Convert the language to a locale name...
43 if ((lang
= getenv("LANG")) != NULL
)
45 for (i
= 0; lang
[i
] && i
< 15; i
++)
46 if (isalnum(lang
[i
] & 255))
47 locale
[i
] = (char)tolower(lang
[i
]);
57 * See if we have a template file for this language...
60 directory
= cgiGetTemplateDir();
62 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
63 if (access(filename
, 0))
67 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
68 if (access(filename
, 0))
69 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
73 * Open the template file...
76 if ((in
= fopen(filename
, "r")) == NULL
)
80 * Loop through the file adding attribute names as needed...
84 attrs
[0] = NULL
; /* Eliminate compiler warning */
86 while ((ch
= getc(in
)) != EOF
)
89 else if (ch
== '{' && num_attrs
< (int)(sizeof(attrs
) / sizeof(attrs
[0])))
95 for (nameptr
= name
; (ch
= getc(in
)) != EOF
;)
96 if (strchr("}]<>=!~ \t\n", ch
))
98 else if (nameptr
> name
&& ch
== '?')
100 else if (nameptr
< (name
+ sizeof(name
) - 1))
105 *nameptr
++ = (char)ch
;
110 if (!strncmp(name
, "printer_state_history", 21))
111 strlcpy(name
, "printer_state_history", sizeof(name
));
114 * Possibly add it to the list of attributes...
117 for (i
= 0; i
< num_attrs
; i
++)
118 if (!strcmp(attrs
[i
], name
))
123 attrs
[num_attrs
] = strdup(name
);
129 * If we have attributes, add a requested-attributes attribute to the
135 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
136 "requested-attributes", num_attrs
, NULL
, (const char **)attrs
);
138 for (i
= 0; i
< num_attrs
; i
++)
147 * 'cgiGetIPPObjects()' - Get the objects in an IPP response.
150 cups_array_t
* /* O - Array of objects */
151 cgiGetIPPObjects(ipp_t
*response
, /* I - IPP response */
152 void *search
) /* I - Search filter */
154 int i
; /* Looping var */
155 cups_array_t
*objs
; /* Array of objects */
156 ipp_attribute_t
*attr
, /* Current attribute */
157 *first
; /* First attribute for object */
158 ipp_tag_t group
; /* Current group tag */
159 int add
; /* Add this object to the array? */
165 for (add
= 0, first
= NULL
, objs
= cupsArrayNew(NULL
, NULL
),
166 group
= IPP_TAG_ZERO
, attr
= response
->attrs
;
170 if (attr
->group_tag
!= group
)
172 group
= attr
->group_tag
;
174 if (group
!= IPP_TAG_ZERO
&& group
!= IPP_TAG_OPERATION
)
179 else if (add
&& first
)
181 cupsArrayAdd(objs
, first
);
188 if (attr
->name
&& attr
->group_tag
!= IPP_TAG_OPERATION
&& !add
)
193 * Add all objects if there is no search...
201 * Check the search string against the string and integer values.
204 switch (attr
->value_tag
)
206 case IPP_TAG_TEXTLANG
:
207 case IPP_TAG_NAMELANG
:
210 case IPP_TAG_KEYWORD
:
212 case IPP_TAG_MIMETYPE
:
213 for (i
= 0; !add
&& i
< attr
->num_values
; i
++)
214 if (cgiDoSearch(search
, attr
->values
[i
].string
.text
))
218 case IPP_TAG_INTEGER
:
219 if (!strncmp(ippGetName(attr
), "time-at-", 8))
220 break; /* Ignore time-at-xxx */
222 for (i
= 0; !add
&& i
< attr
->num_values
; i
++)
224 char buf
[255]; /* Number buffer */
227 sprintf(buf
, "%d", attr
->values
[i
].integer
);
229 if (cgiDoSearch(search
, buf
))
242 cupsArrayAdd(objs
, first
);
249 * 'cgiMoveJobs()' - Move one or more jobs.
251 * At least one of dest or job_id must be non-zero/NULL.
255 cgiMoveJobs(http_t
*http
, /* I - Connection to server */
256 const char *dest
, /* I - Destination or NULL */
257 int job_id
) /* I - Job ID or 0 for all */
259 int i
; /* Looping var */
260 const char *user
; /* Username */
261 ipp_t
*request
, /* IPP request */
262 *response
; /* IPP response */
263 ipp_attribute_t
*attr
; /* Current attribute */
264 const char *name
; /* Destination name */
265 const char *job_printer_uri
; /* JOB_PRINTER_URI form variable */
266 char current_dest
[1024]; /* Current destination */
270 * Make sure we have a username...
273 if ((user
= getenv("REMOTE_USER")) == NULL
)
275 puts("Status: 401\n");
280 * See if the user has already selected a new destination...
283 if ((job_printer_uri
= cgiGetVariable("JOB_PRINTER_URI")) == NULL
)
286 * Make sure necessary form variables are set...
291 char temp
[255]; /* Temporary string */
294 sprintf(temp
, "%d", job_id
);
295 cgiSetVariable("JOB_ID", temp
);
299 cgiSetVariable("PRINTER_NAME", dest
);
302 * No new destination specified, show the user what the available
303 * printers/classes are...
309 * Get the current destination for job N...
312 char job_uri
[1024]; /* Job URI */
315 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
317 snprintf(job_uri
, sizeof(job_uri
), "ipp://localhost/jobs/%d", job_id
);
318 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
320 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
321 "requested-attributes", NULL
, "job-printer-uri");
323 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
325 if ((attr
= ippFindAttribute(response
, "job-printer-uri",
326 IPP_TAG_URI
)) != NULL
)
329 * Pull the name from the URI...
332 strlcpy(current_dest
, strrchr(attr
->values
[0].string
.text
, '/') + 1,
333 sizeof(current_dest
));
343 * Couldn't get the current destination...
346 cgiStartHTML(cgiText(_("Move Job")));
347 cgiShowIPPError(_("Unable to find destination for job"));
354 * Get the list of available destinations...
357 request
= ippNewRequest(CUPS_GET_PRINTERS
);
359 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
360 "requested-attributes", NULL
, "printer-uri-supported");
363 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
364 "requesting-user-name", NULL
, user
);
366 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type",
368 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask",
369 CUPS_PRINTER_SCANNER
);
371 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
373 for (i
= 0, attr
= ippFindAttribute(response
, "printer-uri-supported",
376 attr
= ippFindNextAttribute(response
, "printer-uri-supported",
380 * Pull the name from the URI...
383 name
= strrchr(attr
->values
[0].string
.text
, '/') + 1;
386 * If the name is not the same as the current destination, add it!
389 if (_cups_strcasecmp(name
, dest
))
391 cgiSetArray("JOB_PRINTER_URI", i
, attr
->values
[0].string
.text
);
392 cgiSetArray("JOB_PRINTER_NAME", i
, name
);
405 cgiStartHTML(cgiText(_("Move Job")));
407 cgiStartHTML(cgiText(_("Move All Jobs")));
409 if (cgiGetSize("JOB_PRINTER_NAME") > 0)
410 cgiCopyTemplateLang("job-move.tmpl");
414 cgiSetVariable("MESSAGE", cgiText(_("Unable to move job")));
416 cgiSetVariable("MESSAGE", cgiText(_("Unable to move jobs")));
418 cgiSetVariable("ERROR", cgiText(_("No destinations added.")));
419 cgiCopyTemplateLang("error.tmpl");
425 * Try moving the job or jobs...
428 char uri
[1024], /* Job/printer URI */
429 resource
[1024], /* Post resource */
430 refresh
[1024]; /* Refresh URL */
431 const char *job_printer_name
; /* New printer name */
434 request
= ippNewRequest(CUPS_MOVE_JOB
);
442 snprintf(resource
, sizeof(resource
), "/jobs/%d", job_id
);
444 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", job_id
);
445 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
451 * Move all active jobs on a destination...
454 snprintf(resource
, sizeof(resource
), "/%s/%s",
455 cgiGetVariable("SECTION"), dest
);
457 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
458 "localhost", ippPort(), "/%s/%s",
459 cgiGetVariable("SECTION"), dest
);
460 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
464 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-printer-uri",
465 NULL
, job_printer_uri
);
467 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
468 "requesting-user-name", NULL
, user
);
470 ippDelete(cupsDoRequest(http
, request
, resource
));
473 * Show the results...
476 job_printer_name
= strrchr(job_printer_uri
, '/') + 1;
478 if (cupsLastError() <= IPP_OK_CONFLICT
)
480 const char *path
= strstr(job_printer_uri
, "/printers/");
483 path
= strstr(job_printer_uri
, "/classes/");
484 cgiSetVariable("IS_CLASS", "YES");
489 cgiFormEncode(uri
, path
, sizeof(uri
));
490 snprintf(refresh
, sizeof(refresh
), "2;URL=%s", uri
);
491 cgiSetVariable("refresh_page", refresh
);
496 cgiStartHTML(cgiText(_("Move Job")));
498 cgiStartHTML(cgiText(_("Move All Jobs")));
500 if (cupsLastError() > IPP_OK_CONFLICT
)
503 cgiShowIPPError(_("Unable to move job"));
505 cgiShowIPPError(_("Unable to move jobs"));
509 cgiSetVariable("JOB_PRINTER_NAME", job_printer_name
);
510 cgiCopyTemplateLang("job-moved.tmpl");
519 * 'cgiPrintCommand()' - Print a CUPS command job.
523 cgiPrintCommand(http_t
*http
, /* I - Connection to server */
524 const char *dest
, /* I - Destination printer */
525 const char *command
, /* I - Command to send */
526 const char *title
) /* I - Page/job title */
528 int job_id
; /* Command file job */
529 char uri
[HTTP_MAX_URI
], /* Job URI */
530 resource
[1024], /* Printer resource path */
531 refresh
[1024], /* Refresh URL */
532 command_file
[1024]; /* Command "file" */
533 http_status_t status
; /* Document status */
534 cups_option_t hold_option
; /* job-hold-until option */
535 const char *user
; /* User name */
536 ipp_t
*request
, /* Get-Job-Attributes request */
537 *response
; /* Get-Job-Attributes response */
538 ipp_attribute_t
*attr
; /* Current job attribute */
539 static const char * const job_attrs
[] =/* Job attributes we want */
542 "job-printer-state-message"
547 * Create the CUPS command file...
550 snprintf(command_file
, sizeof(command_file
), "#CUPS-COMMAND\n%s\n", command
);
556 if (cgiSupportsMultipart())
560 cgiCopyTemplateLang("command.tmpl");
566 * Send the command file job...
569 hold_option
.name
= "job-hold-until";
570 hold_option
.value
= "no-hold";
572 if ((user
= getenv("REMOTE_USER")) != NULL
)
575 cupsSetUser("anonymous");
577 if ((job_id
= cupsCreateJob(http
, dest
, title
,
578 1, &hold_option
)) < 1)
580 cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver")));
581 cgiSetVariable("ERROR", cupsLastErrorString());
583 cgiCopyTemplateLang("error.tmpl");
586 if (cgiSupportsMultipart())
591 status
= cupsStartDocument(http
, dest
, job_id
, NULL
, CUPS_FORMAT_COMMAND
, 1);
592 if (status
== HTTP_CONTINUE
)
593 status
= cupsWriteRequestData(http
, command_file
,
594 strlen(command_file
));
595 if (status
== HTTP_CONTINUE
)
596 cupsFinishDocument(http
, dest
);
598 if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE
)
600 cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver")));
601 cgiSetVariable("ERROR", cupsLastErrorString());
603 cgiCopyTemplateLang("error.tmpl");
606 if (cgiSupportsMultipart())
609 cupsCancelJob(dest
, job_id
);
614 * Wait for the job to complete...
617 if (cgiSupportsMultipart())
622 * Get the current job state...
625 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", job_id
);
626 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
627 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
630 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
631 "requesting-user-name", NULL
, user
);
632 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
633 "requested-attributes", 2, NULL
, job_attrs
);
635 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
636 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
638 attr
= ippFindAttribute(response
, "job-state", IPP_TAG_ENUM
);
639 if (!attr
|| attr
->values
[0].integer
>= IPP_JOB_STOPPED
||
640 attr
->values
[0].integer
== IPP_JOB_HELD
)
647 * Job not complete, so update the status...
653 cgiCopyTemplateLang("command.tmpl");
662 * Send the final page that reloads the printer's page...
665 snprintf(resource
, sizeof(resource
), "/printers/%s", dest
);
667 cgiFormEncode(uri
, resource
, sizeof(uri
));
668 snprintf(refresh
, sizeof(refresh
), "5;URL=%s", uri
);
669 cgiSetVariable("refresh_page", refresh
);
672 cgiCopyTemplateLang("command.tmpl");
675 if (cgiSupportsMultipart())
681 * 'cgiPrintTestPage()' - Print a test page.
685 cgiPrintTestPage(http_t
*http
, /* I - Connection to server */
686 const char *dest
) /* I - Destination printer/class */
688 ipp_t
*request
, /* IPP request */
689 *response
; /* IPP response */
690 char uri
[HTTP_MAX_URI
], /* Printer URI */
691 resource
[1024], /* POST resource path */
692 refresh
[1024], /* Refresh URL */
693 filename
[1024]; /* Test page filename */
694 const char *datadir
; /* CUPS_DATADIR env var */
695 const char *user
; /* Username */
699 * See who is logged in...
702 user
= getenv("REMOTE_USER");
705 * Locate the test page file...
708 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
709 datadir
= CUPS_DATADIR
;
711 snprintf(filename
, sizeof(filename
), "%s/data/testprint", datadir
);
714 * Point to the printer/class...
717 snprintf(resource
, sizeof(resource
), "/%s/%s", cgiGetVariable("SECTION"),
720 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
721 "localhost", ippPort(), "/%s/%s", cgiGetVariable("SECTION"),
725 * Build an IPP_PRINT_JOB request, which requires the following
729 * attributes-natural-language
731 * requesting-user-name
734 request
= ippNewRequest(IPP_PRINT_JOB
);
736 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
740 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
741 "requesting-user-name", NULL
, user
);
743 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
747 * Do the request and get back a response...
750 if ((response
= cupsDoFileRequest(http
, request
, resource
,
753 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
758 if (cupsLastError() <= IPP_OK_CONFLICT
)
761 * Automatically reload the printer status page...
764 cgiFormEncode(uri
, resource
, sizeof(uri
));
765 snprintf(refresh
, sizeof(refresh
), "2;URL=%s", uri
);
766 cgiSetVariable("refresh_page", refresh
);
768 else if (cupsLastError() == IPP_NOT_AUTHORIZED
)
770 puts("Status: 401\n");
774 cgiStartHTML(cgiText(_("Print Test Page")));
776 if (cupsLastError() > IPP_OK_CONFLICT
)
777 cgiShowIPPError(_("Unable to print test page"));
780 cgiSetVariable("PRINTER_NAME", dest
);
782 cgiCopyTemplateLang("test-page.tmpl");
790 * 'cgiRewriteURL()' - Rewrite a printer URI into a web browser URL...
793 char * /* O - New URL */
794 cgiRewriteURL(const char *uri
, /* I - Current URI */
795 char *url
, /* O - New URL */
796 int urlsize
, /* I - Size of URL buffer */
797 const char *newresource
) /* I - Replacement resource */
799 char scheme
[HTTP_MAX_URI
],
800 userpass
[HTTP_MAX_URI
],
801 hostname
[HTTP_MAX_URI
],
802 rawresource
[HTTP_MAX_URI
],
803 resource
[HTTP_MAX_URI
],
804 /* URI components... */
805 *rawptr
, /* Pointer into rawresource */
806 *resptr
; /* Pointer into resource */
807 int port
; /* Port number */
808 static int ishttps
= -1; /* Using encryption? */
809 static const char *server
; /* Name of server */
810 static char servername
[1024];
811 /* Local server name */
812 static const char hexchars
[] = "0123456789ABCDEF";
813 /* Hexadecimal conversion characters */
817 * Check if we have been called before...
823 * No, initialize static vars for the conversion...
825 * First get the server name associated with the client interface as
826 * well as the locally configured hostname. We'll check *both* of
827 * these to see if the printer URL is local...
830 if ((server
= getenv("SERVER_NAME")) == NULL
)
833 httpGetHostname(NULL
, servername
, sizeof(servername
));
836 * Then flag whether we are using SSL on this connection...
839 ishttps
= getenv("HTTPS") != NULL
;
843 * Convert the URI to a URL...
846 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
,
847 sizeof(userpass
), hostname
, sizeof(hostname
), &port
,
848 rawresource
, sizeof(rawresource
));
850 if (!strcmp(scheme
, "ipp") ||
851 !strcmp(scheme
, "http") ||
852 !strcmp(scheme
, "https"))
857 * Force the specified resource name instead of the one in the URL...
860 strlcpy(resource
, newresource
, sizeof(resource
));
865 * Rewrite the resource string so it doesn't contain any
869 for (rawptr
= rawresource
, resptr
= resource
; *rawptr
; rawptr
++)
870 if ((*rawptr
& 128) || *rawptr
== '%' || *rawptr
== ' ' ||
871 *rawptr
== '#' || *rawptr
== '?' ||
872 *rawptr
== '.') /* For MSIE */
874 if (resptr
< (resource
+ sizeof(resource
) - 3))
877 *resptr
++ = hexchars
[(*rawptr
>> 4) & 15];
878 *resptr
++ = hexchars
[*rawptr
& 15];
881 else if (resptr
< (resource
+ sizeof(resource
) - 1))
888 * Map local access to a local URI...
891 if (!_cups_strcasecmp(hostname
, "127.0.0.1") ||
892 !_cups_strcasecmp(hostname
, "[::1]") ||
893 !_cups_strcasecmp(hostname
, "localhost") ||
894 !_cups_strncasecmp(hostname
, "localhost.", 10) ||
895 !_cups_strcasecmp(hostname
, server
) ||
896 !_cups_strcasecmp(hostname
, servername
))
899 * Make URI relative to the current server...
902 strlcpy(url
, resource
, (size_t)urlsize
);
907 * Rewrite URI with HTTP/HTTPS scheme...
911 snprintf(url
, (size_t)urlsize
, "%s://%s@%s:%d%s", ishttps
? "https" : "http", userpass
, hostname
, port
, resource
);
913 snprintf(url
, (size_t)urlsize
, "%s://%s:%d%s", ishttps
? "https" : "http", hostname
, port
, resource
);
917 strlcpy(url
, uri
, (size_t)urlsize
);
924 * 'cgiSetIPPObjectVars()' - Set CGI variables from an IPP object.
927 ipp_attribute_t
* /* O - Next object */
929 ipp_attribute_t
*obj
, /* I - Response data to be copied... */
930 const char *prefix
, /* I - Prefix for name or NULL */
931 int element
) /* I - Parent element number */
933 ipp_attribute_t
*attr
; /* Attribute in response... */
934 int i
; /* Looping var */
935 char name
[1024], /* Name of attribute */
936 *nameptr
, /* Pointer into name */
937 value
[16384], /* Value(s) */
938 *valptr
; /* Pointer into value */
941 fprintf(stderr
, "DEBUG2: cgiSetIPPObjectVars(obj=%p, prefix=\"%s\", "
943 obj
, prefix
? prefix
: "(null)", element
);
946 * Set common CGI template variables...
950 cgiSetServerVersion();
953 * Loop through the attributes and set them for the template...
956 for (attr
= obj
; attr
&& attr
->group_tag
!= IPP_TAG_ZERO
; attr
= attr
->next
)
959 * Copy the attribute name, substituting "_" for "-"...
967 snprintf(name
, sizeof(name
), "%s.", prefix
);
968 nameptr
= name
+ strlen(name
);
973 for (i
= 0; attr
->name
[i
] && nameptr
< (name
+ sizeof(name
) - 1); i
++)
974 if (attr
->name
[i
] == '-')
977 *nameptr
++ = attr
->name
[i
];
982 * Add "job_printer_name" variable if we have a "job_printer_uri"
986 if (!strcmp(name
, "job_printer_uri"))
988 if ((valptr
= strrchr(attr
->values
[0].string
.text
, '/')) == NULL
)
993 cgiSetArray("job_printer_name", element
, valptr
);
997 * Localize event names in "notify_events" variable...
1000 if (!strcmp(name
, "notify_events"))
1002 size_t remaining
; /* Remaining bytes in buffer */
1008 for (i
= 0; i
< attr
->num_values
; i
++)
1010 if (valptr
>= (value
+ sizeof(value
) - 3))
1019 remaining
= sizeof(value
) - (size_t)(valptr
- value
);
1021 if (!strcmp(attr
->values
[i
].string
.text
, "printer-stopped"))
1022 strlcpy(valptr
, _("Printer Paused"), remaining
);
1023 else if (!strcmp(attr
->values
[i
].string
.text
, "printer-added"))
1024 strlcpy(valptr
, _("Printer Added"), remaining
);
1025 else if (!strcmp(attr
->values
[i
].string
.text
, "printer-modified"))
1026 strlcpy(valptr
, _("Printer Modified"), remaining
);
1027 else if (!strcmp(attr
->values
[i
].string
.text
, "printer-deleted"))
1028 strlcpy(valptr
, _("Printer Deleted"), remaining
);
1029 else if (!strcmp(attr
->values
[i
].string
.text
, "job-created"))
1030 strlcpy(valptr
, _("Job Created"), remaining
);
1031 else if (!strcmp(attr
->values
[i
].string
.text
, "job-completed"))
1032 strlcpy(valptr
, _("Job Completed"), remaining
);
1033 else if (!strcmp(attr
->values
[i
].string
.text
, "job-stopped"))
1034 strlcpy(valptr
, _("Job Stopped"), remaining
);
1035 else if (!strcmp(attr
->values
[i
].string
.text
, "job-config-changed"))
1036 strlcpy(valptr
, _("Job Options Changed"), remaining
);
1037 else if (!strcmp(attr
->values
[i
].string
.text
, "server-restarted"))
1038 strlcpy(valptr
, _("Server Restarted"), remaining
);
1039 else if (!strcmp(attr
->values
[i
].string
.text
, "server-started"))
1040 strlcpy(valptr
, _("Server Started"), remaining
);
1041 else if (!strcmp(attr
->values
[i
].string
.text
, "server-stopped"))
1042 strlcpy(valptr
, _("Server Stopped"), remaining
);
1043 else if (!strcmp(attr
->values
[i
].string
.text
, "server-audit"))
1044 strlcpy(valptr
, _("Server Security Auditing"), remaining
);
1046 strlcpy(valptr
, attr
->values
[i
].string
.text
, remaining
);
1048 valptr
+= strlen(valptr
);
1051 cgiSetArray("notify_events", element
, value
);
1056 * Add "notify_printer_name" variable if we have a "notify_printer_uri"
1060 if (!strcmp(name
, "notify_printer_uri"))
1062 if ((valptr
= strrchr(attr
->values
[0].string
.text
, '/')) == NULL
)
1067 cgiSetArray("notify_printer_name", element
, valptr
);
1071 * Add "notify_recipient_name" variable if we have a "notify_recipient_uri"
1072 * attribute, and rewrite recipient URI...
1075 if (!strcmp(name
, "notify_recipient_uri"))
1077 char uri
[1024], /* New URI */
1078 scheme
[32], /* Scheme portion of URI */
1079 userpass
[256], /* Username/password portion of URI */
1080 host
[1024], /* Hostname portion of URI */
1081 resource
[1024], /* Resource portion of URI */
1082 *options
; /* Options in URI */
1083 int port
; /* Port number */
1086 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
,
1087 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
1088 host
, sizeof(host
), &port
, resource
, sizeof(resource
));
1090 if (!strcmp(scheme
, "rss"))
1093 * RSS notification...
1096 if ((options
= strchr(resource
, '?')) != NULL
)
1102 * Link to remote feed...
1105 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "http",
1106 userpass
, host
, port
, resource
);
1107 strlcpy(name
, uri
, sizeof(name
));
1112 * Link to local feed...
1115 snprintf(uri
, sizeof(uri
), "/rss%s", resource
);
1116 strlcpy(name
, resource
+ 1, sizeof(name
));
1125 strlcpy(uri
, attr
->values
[0].string
.text
, sizeof(uri
));
1126 strlcpy(name
, resource
, sizeof(name
));
1129 cgiSetArray("notify_recipient_uri", element
, uri
);
1130 cgiSetArray("notify_recipient_name", element
, name
);
1135 * Add "admin_uri" variable if we have a "printer_uri_supported"
1139 if (!strcmp(name
, "printer_uri_supported"))
1141 cgiRewriteURL(attr
->values
[0].string
.text
, value
, sizeof(value
),
1144 cgiSetArray("admin_uri", element
, value
);
1151 value
[0] = '\0'; /* Initially an empty string */
1152 valptr
= value
; /* Start at the beginning */
1154 for (i
= 0; i
< attr
->num_values
; i
++)
1157 strlcat(valptr
, ", ", sizeof(value
) - (size_t)(valptr
- value
));
1159 valptr
+= strlen(valptr
);
1161 switch (attr
->value_tag
)
1163 case IPP_TAG_INTEGER
:
1165 if (strncmp(name
, "time_at_", 8) == 0)
1166 _cupsStrDate(valptr
, sizeof(value
) - (size_t)(valptr
- value
), (time_t)ippGetInteger(attr
, i
));
1168 snprintf(valptr
, sizeof(value
) - (size_t)(valptr
- value
), "%d", ippGetInteger(attr
, i
));
1171 case IPP_TAG_BOOLEAN
:
1172 snprintf(valptr
, sizeof(value
) - (size_t)(valptr
- value
),
1173 "%d", attr
->values
[i
].boolean
);
1176 case IPP_TAG_NOVALUE
:
1177 strlcat(valptr
, "novalue", sizeof(value
) - (size_t)(valptr
- value
));
1180 case IPP_TAG_RANGE
:
1181 snprintf(valptr
, sizeof(value
) - (size_t)(valptr
- value
),
1182 "%d-%d", attr
->values
[i
].range
.lower
,
1183 attr
->values
[i
].range
.upper
);
1186 case IPP_TAG_RESOLUTION
:
1187 snprintf(valptr
, sizeof(value
) - (size_t)(valptr
- value
),
1188 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
1189 attr
->values
[i
].resolution
.yres
,
1190 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
1195 if (strchr(attr
->values
[i
].string
.text
, ':') &&
1196 strcmp(name
, "device_uri"))
1202 cgiRewriteURL(attr
->values
[i
].string
.text
, valptr
, (int)(sizeof(value
) - (size_t)(valptr
- value
)), NULL
);
1206 case IPP_TAG_STRING
:
1209 case IPP_TAG_KEYWORD
:
1210 case IPP_TAG_CHARSET
:
1211 case IPP_TAG_LANGUAGE
:
1212 case IPP_TAG_MIMETYPE
:
1213 strlcat(valptr
, attr
->values
[i
].string
.text
,
1214 sizeof(value
) - (size_t)(valptr
- value
));
1217 case IPP_TAG_BEGIN_COLLECTION
:
1218 snprintf(value
, sizeof(value
), "%s%d", name
, i
+ 1);
1219 cgiSetIPPVars(attr
->values
[i
].collection
, NULL
, NULL
, value
,
1224 break; /* anti-compiler-warning-code */
1229 * Add the element...
1232 if (attr
->value_tag
!= IPP_TAG_BEGIN_COLLECTION
)
1234 cgiSetArray(name
, element
, value
);
1236 fprintf(stderr
, "DEBUG2: %s[%d]=\"%s\"\n", name
, element
, value
);
1240 return (attr
? attr
->next
: NULL
);
1245 * 'cgiSetIPPVars()' - Set CGI variables from an IPP response.
1248 int /* O - Maximum number of elements */
1249 cgiSetIPPVars(ipp_t
*response
, /* I - Response data to be copied... */
1250 const char *filter_name
, /* I - Filter name */
1251 const char *filter_value
, /* I - Filter value */
1252 const char *prefix
, /* I - Prefix for name or NULL */
1253 int parent_el
) /* I - Parent element number */
1255 int element
; /* Element in CGI array */
1256 ipp_attribute_t
*attr
, /* Attribute in response... */
1257 *filter
; /* Filtering attribute */
1260 fprintf(stderr
, "DEBUG2: cgiSetIPPVars(response=%p, filter_name=\"%s\", "
1261 "filter_value=\"%s\", prefix=\"%s\", parent_el=%d)\n",
1262 response
, filter_name
? filter_name
: "(null)",
1263 filter_value
? filter_value
: "(null)",
1264 prefix
? prefix
: "(null)", parent_el
);
1267 * Set common CGI template variables...
1271 cgiSetServerVersion();
1274 * Loop through the attributes and set them for the template...
1277 attr
= response
->attrs
;
1280 while (attr
&& attr
->group_tag
== IPP_TAG_OPERATION
)
1283 for (element
= parent_el
; attr
; element
++)
1286 * Copy attributes to a separator...
1289 while (attr
&& attr
->group_tag
== IPP_TAG_ZERO
)
1298 filter
!= NULL
&& filter
->group_tag
!= IPP_TAG_ZERO
;
1299 filter
= filter
->next
)
1300 if (filter
->name
&& !strcmp(filter
->name
, filter_name
) &&
1301 (filter
->value_tag
== IPP_TAG_STRING
||
1302 (filter
->value_tag
>= IPP_TAG_TEXTLANG
&&
1303 filter
->value_tag
<= IPP_TAG_MIMETYPE
)) &&
1304 filter
->values
[0].string
.text
!= NULL
&&
1305 !_cups_strcasecmp(filter
->values
[0].string
.text
, filter_value
))
1309 return (element
+ 1);
1311 if (filter
->group_tag
== IPP_TAG_ZERO
)
1319 attr
= cgiSetIPPObjectVars(attr
, prefix
, element
);
1322 fprintf(stderr
, "DEBUG2: Returning %d from cgiSetIPPVars()...\n", element
);
1329 * 'cgiShowIPPError()' - Show the last IPP error message.
1331 * The caller must still call cgiStartHTML() and cgiEndHTML().
1335 cgiShowIPPError(const char *message
) /* I - Contextual message */
1337 cgiSetVariable("MESSAGE", cgiText(message
));
1338 cgiSetVariable("ERROR", cupsLastErrorString());
1339 cgiCopyTemplateLang("error.tmpl");
1344 * 'cgiShowJobs()' - Show print jobs.
1348 cgiShowJobs(http_t
*http
, /* I - Connection to server */
1349 const char *dest
) /* I - Destination name or NULL */
1351 int i
; /* Looping var */
1352 const char *which_jobs
; /* Which jobs to show */
1353 ipp_t
*request
, /* IPP request */
1354 *response
; /* IPP response */
1355 cups_array_t
*jobs
; /* Array of job objects */
1356 ipp_attribute_t
*job
; /* Job object */
1357 int first
, /* First job to show */
1358 count
; /* Number of jobs */
1359 const char *var
, /* Form variable */
1360 *query
, /* Query string */
1361 *section
; /* Section in web interface */
1362 void *search
; /* Search data */
1363 char url
[1024], /* Printer URI */
1364 val
[1024]; /* Form variable */
1368 * Build an IPP_GET_JOBS request, which requires the following
1371 * attributes-charset
1372 * attributes-natural-language
1376 request
= ippNewRequest(IPP_GET_JOBS
);
1380 httpAssembleURIf(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "ipp", NULL
,
1381 "localhost", ippPort(), "/printers/%s", dest
);
1382 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1386 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1387 "ipp://localhost/");
1389 if ((which_jobs
= cgiGetVariable("which_jobs")) != NULL
&& *which_jobs
)
1390 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "which-jobs",
1393 if ((var
= cgiGetVariable("FIRST")) != NULL
)
1395 if ((first
= atoi(var
)) < 0)
1402 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "first-index", first
+ 1);
1404 cgiGetAttributes(request
, "jobs.tmpl");
1407 * Do the request and get back a response...
1410 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1413 * Get a list of matching job objects.
1416 if ((query
= cgiGetVariable("QUERY")) != NULL
&&
1417 !cgiGetVariable("CLEAR"))
1418 search
= cgiCompileSearch(query
);
1425 jobs
= cgiGetIPPObjects(response
, search
);
1426 count
= cupsArrayCount(jobs
) + first
;
1429 cgiFreeSearch(search
);
1432 * Figure out which jobs to display...
1435 section
= cgiGetVariable("SECTION");
1437 cgiClearVariables();
1440 cgiSetVariable("QUERY", query
);
1442 cgiSetVariable("SECTION", section
);
1444 sprintf(val
, "%d", count
);
1445 cgiSetVariable("TOTAL", val
);
1448 cgiSetVariable("WHICH_JOBS", which_jobs
);
1450 for (i
= 0, job
= (ipp_attribute_t
*)cupsArrayFirst(jobs
);
1451 i
< CUPS_PAGE_MAX
&& job
;
1452 i
++, job
= (ipp_attribute_t
*)cupsArrayNext(jobs
))
1453 cgiSetIPPObjectVars(job
, NULL
, i
);
1456 * Save navigation URLs...
1461 snprintf(val
, sizeof(val
), "/%s/%s", section
, dest
);
1462 cgiSetVariable("PRINTER_NAME", dest
);
1463 cgiSetVariable("PRINTER_URI_SUPPORTED", val
);
1466 strlcpy(val
, "/jobs/", sizeof(val
));
1468 cgiSetVariable("THISURL", val
);
1472 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
1473 cgiSetVariable("PREV", val
);
1476 if ((first
+ CUPS_PAGE_MAX
) < count
)
1478 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
1479 cgiSetVariable("NEXT", val
);
1482 if (count
> CUPS_PAGE_MAX
)
1484 snprintf(val
, sizeof(val
), "%d", CUPS_PAGE_MAX
* (count
/ CUPS_PAGE_MAX
));
1485 cgiSetVariable("LAST", val
);
1489 * Then show everything...
1493 cgiSetVariable("SEARCH_DEST", dest
);
1495 cgiCopyTemplateLang("search.tmpl");
1497 cgiCopyTemplateLang("jobs-header.tmpl");
1499 if (count
> CUPS_PAGE_MAX
)
1500 cgiCopyTemplateLang("pager.tmpl");
1502 cgiCopyTemplateLang("jobs.tmpl");
1504 if (count
> CUPS_PAGE_MAX
)
1505 cgiCopyTemplateLang("pager.tmpl");
1507 cupsArrayDelete(jobs
);
1508 ippDelete(response
);
1514 * 'cgiText()' - Return localized text.
1517 const char * /* O - Localized message */
1518 cgiText(const char *message
) /* I - Message */
1520 static cups_lang_t
*language
= NULL
;
1525 language
= cupsLangDefault();
1527 return (_cupsLangString(language
, message
));