4 * CGI <-> IPP variable routines for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
20 #include "cgi-private.h"
24 * 'cgiGetAttributes()' - Get the list of attributes that are needed
25 * by the template file.
29 cgiGetAttributes(ipp_t
*request
, /* I - IPP request */
30 const char *tmpl
) /* I - Base filename */
32 int num_attrs
; /* Number of attributes */
33 char *attrs
[1000]; /* Attributes */
34 int i
; /* Looping var */
35 char filename
[1024], /* Filename */
36 locale
[16]; /* Locale name */
37 const char *directory
, /* Directory */
39 FILE *in
; /* Input file */
40 int ch
; /* Character from file */
41 char name
[255], /* Name of variable */
42 *nameptr
; /* Pointer into name */
46 * Convert the language to a locale name...
49 if ((lang
= getenv("LANG")) != NULL
)
51 for (i
= 0; lang
[i
] && i
< 15; i
++)
52 if (isalnum(lang
[i
] & 255))
53 locale
[i
] = (char)tolower(lang
[i
]);
63 * See if we have a template file for this language...
66 directory
= cgiGetTemplateDir();
68 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
69 if (access(filename
, 0))
73 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
74 if (access(filename
, 0))
75 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
79 * Open the template file...
82 if ((in
= fopen(filename
, "r")) == NULL
)
86 * Loop through the file adding attribute names as needed...
90 attrs
[0] = NULL
; /* Eliminate compiler warning */
92 while ((ch
= getc(in
)) != EOF
)
95 else if (ch
== '{' && num_attrs
< (int)(sizeof(attrs
) / sizeof(attrs
[0])))
101 for (nameptr
= name
; (ch
= getc(in
)) != EOF
;)
102 if (strchr("}]<>=!~ \t\n", ch
))
104 else if (nameptr
> name
&& ch
== '?')
106 else if (nameptr
< (name
+ sizeof(name
) - 1))
111 *nameptr
++ = (char)ch
;
116 if (!strncmp(name
, "printer_state_history", 21))
117 strlcpy(name
, "printer_state_history", sizeof(name
));
120 * Possibly add it to the list of attributes...
123 for (i
= 0; i
< num_attrs
; i
++)
124 if (!strcmp(attrs
[i
], name
))
129 attrs
[num_attrs
] = strdup(name
);
135 * If we have attributes, add a requested-attributes attribute to the
141 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
142 "requested-attributes", num_attrs
, NULL
, (const char **)attrs
);
144 for (i
= 0; i
< num_attrs
; i
++)
153 * 'cgiGetIPPObjects()' - Get the objects in an IPP response.
156 cups_array_t
* /* O - Array of objects */
157 cgiGetIPPObjects(ipp_t
*response
, /* I - IPP response */
158 void *search
) /* I - Search filter */
160 int i
; /* Looping var */
161 cups_array_t
*objs
; /* Array of objects */
162 ipp_attribute_t
*attr
, /* Current attribute */
163 *first
; /* First attribute for object */
164 ipp_tag_t group
; /* Current group tag */
165 int add
; /* Add this object to the array? */
171 for (add
= 0, first
= NULL
, objs
= cupsArrayNew(NULL
, NULL
),
172 group
= IPP_TAG_ZERO
, attr
= response
->attrs
;
176 if (attr
->group_tag
!= group
)
178 group
= attr
->group_tag
;
180 if (group
!= IPP_TAG_ZERO
&& group
!= IPP_TAG_OPERATION
)
185 else if (add
&& first
)
187 cupsArrayAdd(objs
, first
);
194 if (attr
->name
&& attr
->group_tag
!= IPP_TAG_OPERATION
&& !add
)
199 * Add all objects if there is no search...
207 * Check the search string against the string and integer values.
210 switch (attr
->value_tag
)
212 case IPP_TAG_TEXTLANG
:
213 case IPP_TAG_NAMELANG
:
216 case IPP_TAG_KEYWORD
:
218 case IPP_TAG_MIMETYPE
:
219 for (i
= 0; !add
&& i
< attr
->num_values
; i
++)
220 if (cgiDoSearch(search
, attr
->values
[i
].string
.text
))
224 case IPP_TAG_INTEGER
:
225 for (i
= 0; !add
&& i
< attr
->num_values
; i
++)
227 char buf
[255]; /* Number buffer */
230 sprintf(buf
, "%d", attr
->values
[i
].integer
);
232 if (cgiDoSearch(search
, buf
))
245 cupsArrayAdd(objs
, first
);
252 * 'cgiMoveJobs()' - Move one or more jobs.
254 * At least one of dest or job_id must be non-zero/NULL.
258 cgiMoveJobs(http_t
*http
, /* I - Connection to server */
259 const char *dest
, /* I - Destination or NULL */
260 int job_id
) /* I - Job ID or 0 for all */
262 int i
; /* Looping var */
263 const char *user
; /* Username */
264 ipp_t
*request
, /* IPP request */
265 *response
; /* IPP response */
266 ipp_attribute_t
*attr
; /* Current attribute */
267 const char *name
; /* Destination name */
268 const char *job_printer_uri
; /* JOB_PRINTER_URI form variable */
269 char current_dest
[1024]; /* Current destination */
273 * Make sure we have a username...
276 if ((user
= getenv("REMOTE_USER")) == NULL
)
278 puts("Status: 401\n");
283 * See if the user has already selected a new destination...
286 if ((job_printer_uri
= cgiGetVariable("JOB_PRINTER_URI")) == NULL
)
289 * Make sure necessary form variables are set...
294 char temp
[255]; /* Temporary string */
297 sprintf(temp
, "%d", job_id
);
298 cgiSetVariable("JOB_ID", temp
);
302 cgiSetVariable("PRINTER_NAME", dest
);
305 * No new destination specified, show the user what the available
306 * printers/classes are...
312 * Get the current destination for job N...
315 char job_uri
[1024]; /* Job URI */
318 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
320 snprintf(job_uri
, sizeof(job_uri
), "ipp://localhost/jobs/%d", job_id
);
321 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
323 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
324 "requested-attributes", NULL
, "job-printer-uri");
326 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
328 if ((attr
= ippFindAttribute(response
, "job-printer-uri",
329 IPP_TAG_URI
)) != NULL
)
332 * Pull the name from the URI...
335 strlcpy(current_dest
, strrchr(attr
->values
[0].string
.text
, '/') + 1,
336 sizeof(current_dest
));
346 * Couldn't get the current destination...
349 cgiStartHTML(cgiText(_("Move Job")));
350 cgiShowIPPError(_("Unable to find destination for job"));
357 * Get the list of available destinations...
360 request
= ippNewRequest(CUPS_GET_PRINTERS
);
362 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
363 "requested-attributes", NULL
, "printer-uri-supported");
366 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
367 "requesting-user-name", NULL
, user
);
369 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type",
371 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask",
372 CUPS_PRINTER_SCANNER
);
374 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
376 for (i
= 0, attr
= ippFindAttribute(response
, "printer-uri-supported",
379 attr
= ippFindNextAttribute(response
, "printer-uri-supported",
383 * Pull the name from the URI...
386 name
= strrchr(attr
->values
[0].string
.text
, '/') + 1;
389 * If the name is not the same as the current destination, add it!
392 if (_cups_strcasecmp(name
, dest
))
394 cgiSetArray("JOB_PRINTER_URI", i
, attr
->values
[0].string
.text
);
395 cgiSetArray("JOB_PRINTER_NAME", i
, name
);
408 cgiStartHTML(cgiText(_("Move Job")));
410 cgiStartHTML(cgiText(_("Move All Jobs")));
412 if (cgiGetSize("JOB_PRINTER_NAME") > 0)
413 cgiCopyTemplateLang("job-move.tmpl");
417 cgiSetVariable("MESSAGE", cgiText(_("Unable to move job")));
419 cgiSetVariable("MESSAGE", cgiText(_("Unable to move jobs")));
421 cgiSetVariable("ERROR", cgiText(_("No destinations added.")));
422 cgiCopyTemplateLang("error.tmpl");
428 * Try moving the job or jobs...
431 char uri
[1024], /* Job/printer URI */
432 resource
[1024], /* Post resource */
433 refresh
[1024]; /* Refresh URL */
434 const char *job_printer_name
; /* New printer name */
437 request
= ippNewRequest(CUPS_MOVE_JOB
);
445 snprintf(resource
, sizeof(resource
), "/jobs/%d", job_id
);
447 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", job_id
);
448 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
454 * Move all active jobs on a destination...
457 snprintf(resource
, sizeof(resource
), "/%s/%s",
458 cgiGetVariable("SECTION"), dest
);
460 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
461 "localhost", ippPort(), "/%s/%s",
462 cgiGetVariable("SECTION"), dest
);
463 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
467 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-printer-uri",
468 NULL
, job_printer_uri
);
470 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
471 "requesting-user-name", NULL
, user
);
473 ippDelete(cupsDoRequest(http
, request
, resource
));
476 * Show the results...
479 job_printer_name
= strrchr(job_printer_uri
, '/') + 1;
481 if (cupsLastError() <= IPP_OK_CONFLICT
)
483 const char *path
= strstr(job_printer_uri
, "/printers/");
486 path
= strstr(job_printer_uri
, "/classes/");
487 cgiSetVariable("IS_CLASS", "YES");
492 cgiFormEncode(uri
, path
, sizeof(uri
));
493 snprintf(refresh
, sizeof(refresh
), "2;URL=%s", uri
);
494 cgiSetVariable("refresh_page", refresh
);
499 cgiStartHTML(cgiText(_("Move Job")));
501 cgiStartHTML(cgiText(_("Move All Jobs")));
503 if (cupsLastError() > IPP_OK_CONFLICT
)
506 cgiShowIPPError(_("Unable to move job"));
508 cgiShowIPPError(_("Unable to move jobs"));
512 cgiSetVariable("JOB_PRINTER_NAME", job_printer_name
);
513 cgiCopyTemplateLang("job-moved.tmpl");
522 * 'cgiPrintCommand()' - Print a CUPS command job.
526 cgiPrintCommand(http_t
*http
, /* I - Connection to server */
527 const char *dest
, /* I - Destination printer */
528 const char *command
, /* I - Command to send */
529 const char *title
) /* I - Page/job title */
531 int job_id
; /* Command file job */
532 char uri
[HTTP_MAX_URI
], /* Job URI */
533 resource
[1024], /* Printer resource path */
534 refresh
[1024], /* Refresh URL */
535 command_file
[1024]; /* Command "file" */
536 http_status_t status
; /* Document status */
537 cups_option_t hold_option
; /* job-hold-until option */
538 const char *user
; /* User name */
539 ipp_t
*request
, /* Get-Job-Attributes request */
540 *response
; /* Get-Job-Attributes response */
541 ipp_attribute_t
*attr
; /* Current job attribute */
542 static const char * const job_attrs
[] =/* Job attributes we want */
545 "job-printer-state-message"
550 * Create the CUPS command file...
553 snprintf(command_file
, sizeof(command_file
), "#CUPS-COMMAND\n%s\n", command
);
559 if (cgiSupportsMultipart())
563 cgiCopyTemplateLang("command.tmpl");
569 * Send the command file job...
572 hold_option
.name
= "job-hold-until";
573 hold_option
.value
= "no-hold";
575 if ((user
= getenv("REMOTE_USER")) != NULL
)
578 cupsSetUser("anonymous");
580 if ((job_id
= cupsCreateJob(http
, dest
, title
,
581 1, &hold_option
)) < 1)
583 cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver")));
584 cgiSetVariable("ERROR", cupsLastErrorString());
586 cgiCopyTemplateLang("error.tmpl");
589 if (cgiSupportsMultipart())
594 status
= cupsStartDocument(http
, dest
, job_id
, NULL
, CUPS_FORMAT_COMMAND
, 1);
595 if (status
== HTTP_CONTINUE
)
596 status
= cupsWriteRequestData(http
, command_file
,
597 strlen(command_file
));
598 if (status
== HTTP_CONTINUE
)
599 cupsFinishDocument(http
, dest
);
601 if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE
)
603 cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver")));
604 cgiSetVariable("ERROR", cupsLastErrorString());
606 cgiCopyTemplateLang("error.tmpl");
609 if (cgiSupportsMultipart())
612 cupsCancelJob(dest
, job_id
);
617 * Wait for the job to complete...
620 if (cgiSupportsMultipart())
625 * Get the current job state...
628 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", job_id
);
629 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
630 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
633 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
634 "requesting-user-name", NULL
, user
);
635 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
636 "requested-attributes", 2, NULL
, job_attrs
);
638 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
639 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
641 attr
= ippFindAttribute(response
, "job-state", IPP_TAG_ENUM
);
642 if (!attr
|| attr
->values
[0].integer
>= IPP_JOB_STOPPED
||
643 attr
->values
[0].integer
== IPP_JOB_HELD
)
650 * Job not complete, so update the status...
656 cgiCopyTemplateLang("command.tmpl");
665 * Send the final page that reloads the printer's page...
668 snprintf(resource
, sizeof(resource
), "/printers/%s", dest
);
670 cgiFormEncode(uri
, resource
, sizeof(uri
));
671 snprintf(refresh
, sizeof(refresh
), "5;URL=%s", uri
);
672 cgiSetVariable("refresh_page", refresh
);
675 cgiCopyTemplateLang("command.tmpl");
678 if (cgiSupportsMultipart())
684 * 'cgiPrintTestPage()' - Print a test page.
688 cgiPrintTestPage(http_t
*http
, /* I - Connection to server */
689 const char *dest
) /* I - Destination printer/class */
691 ipp_t
*request
, /* IPP request */
692 *response
; /* IPP response */
693 char uri
[HTTP_MAX_URI
], /* Printer URI */
694 resource
[1024], /* POST resource path */
695 refresh
[1024], /* Refresh URL */
696 filename
[1024]; /* Test page filename */
697 const char *datadir
; /* CUPS_DATADIR env var */
698 const char *user
; /* Username */
702 * See who is logged in...
705 user
= getenv("REMOTE_USER");
708 * Locate the test page file...
711 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
712 datadir
= CUPS_DATADIR
;
714 snprintf(filename
, sizeof(filename
), "%s/data/testprint", datadir
);
717 * Point to the printer/class...
720 snprintf(resource
, sizeof(resource
), "/%s/%s", cgiGetVariable("SECTION"),
723 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
724 "localhost", ippPort(), "/%s/%s", cgiGetVariable("SECTION"),
728 * Build an IPP_PRINT_JOB request, which requires the following
732 * attributes-natural-language
734 * requesting-user-name
737 request
= ippNewRequest(IPP_PRINT_JOB
);
739 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
743 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
744 "requesting-user-name", NULL
, user
);
746 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
750 * Do the request and get back a response...
753 if ((response
= cupsDoFileRequest(http
, request
, resource
,
756 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
761 if (cupsLastError() <= IPP_OK_CONFLICT
)
764 * Automatically reload the printer status page...
767 cgiFormEncode(uri
, resource
, sizeof(uri
));
768 snprintf(refresh
, sizeof(refresh
), "2;URL=%s", uri
);
769 cgiSetVariable("refresh_page", refresh
);
771 else if (cupsLastError() == IPP_NOT_AUTHORIZED
)
773 puts("Status: 401\n");
777 cgiStartHTML(cgiText(_("Print Test Page")));
779 if (cupsLastError() > IPP_OK_CONFLICT
)
780 cgiShowIPPError(_("Unable to print test page"));
783 cgiSetVariable("PRINTER_NAME", dest
);
785 cgiCopyTemplateLang("test-page.tmpl");
793 * 'cgiRewriteURL()' - Rewrite a printer URI into a web browser URL...
796 char * /* O - New URL */
797 cgiRewriteURL(const char *uri
, /* I - Current URI */
798 char *url
, /* O - New URL */
799 int urlsize
, /* I - Size of URL buffer */
800 const char *newresource
) /* I - Replacement resource */
802 char scheme
[HTTP_MAX_URI
],
803 userpass
[HTTP_MAX_URI
],
804 hostname
[HTTP_MAX_URI
],
805 rawresource
[HTTP_MAX_URI
],
806 resource
[HTTP_MAX_URI
],
807 /* URI components... */
808 *rawptr
, /* Pointer into rawresource */
809 *resptr
; /* Pointer into resource */
810 int port
; /* Port number */
811 static int ishttps
= -1; /* Using encryption? */
812 static const char *server
; /* Name of server */
813 static char servername
[1024];
814 /* Local server name */
815 static const char hexchars
[] = "0123456789ABCDEF";
816 /* Hexadecimal conversion characters */
820 * Check if we have been called before...
826 * No, initialize static vars for the conversion...
828 * First get the server name associated with the client interface as
829 * well as the locally configured hostname. We'll check *both* of
830 * these to see if the printer URL is local...
833 if ((server
= getenv("SERVER_NAME")) == NULL
)
836 httpGetHostname(NULL
, servername
, sizeof(servername
));
839 * Then flag whether we are using SSL on this connection...
842 ishttps
= getenv("HTTPS") != NULL
;
846 * Convert the URI to a URL...
849 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
,
850 sizeof(userpass
), hostname
, sizeof(hostname
), &port
,
851 rawresource
, sizeof(rawresource
));
853 if (!strcmp(scheme
, "ipp") ||
854 !strcmp(scheme
, "http") ||
855 !strcmp(scheme
, "https"))
860 * Force the specified resource name instead of the one in the URL...
863 strlcpy(resource
, newresource
, sizeof(resource
));
868 * Rewrite the resource string so it doesn't contain any
872 for (rawptr
= rawresource
, resptr
= resource
; *rawptr
; rawptr
++)
873 if ((*rawptr
& 128) || *rawptr
== '%' || *rawptr
== ' ' ||
874 *rawptr
== '#' || *rawptr
== '?' ||
875 *rawptr
== '.') /* For MSIE */
877 if (resptr
< (resource
+ sizeof(resource
) - 3))
880 *resptr
++ = hexchars
[(*rawptr
>> 4) & 15];
881 *resptr
++ = hexchars
[*rawptr
& 15];
884 else if (resptr
< (resource
+ sizeof(resource
) - 1))
891 * Map local access to a local URI...
894 if (!_cups_strcasecmp(hostname
, "127.0.0.1") ||
895 !_cups_strcasecmp(hostname
, "[::1]") ||
896 !_cups_strcasecmp(hostname
, "localhost") ||
897 !_cups_strncasecmp(hostname
, "localhost.", 10) ||
898 !_cups_strcasecmp(hostname
, server
) ||
899 !_cups_strcasecmp(hostname
, servername
))
902 * Make URI relative to the current server...
905 strlcpy(url
, resource
, (size_t)urlsize
);
910 * Rewrite URI with HTTP/HTTPS scheme...
914 snprintf(url
, (size_t)urlsize
, "%s://%s@%s:%d%s", ishttps
? "https" : "http", userpass
, hostname
, port
, resource
);
916 snprintf(url
, (size_t)urlsize
, "%s://%s:%d%s", ishttps
? "https" : "http", hostname
, port
, resource
);
920 strlcpy(url
, uri
, (size_t)urlsize
);
927 * 'cgiSetIPPObjectVars()' - Set CGI variables from an IPP object.
930 ipp_attribute_t
* /* O - Next object */
932 ipp_attribute_t
*obj
, /* I - Response data to be copied... */
933 const char *prefix
, /* I - Prefix for name or NULL */
934 int element
) /* I - Parent element number */
936 ipp_attribute_t
*attr
; /* Attribute in response... */
937 int i
; /* Looping var */
938 char name
[1024], /* Name of attribute */
939 *nameptr
, /* Pointer into name */
940 value
[16384], /* Value(s) */
941 *valptr
; /* Pointer into value */
942 struct tm
*date
; /* Date information */
945 fprintf(stderr
, "DEBUG2: cgiSetIPPObjectVars(obj=%p, prefix=\"%s\", "
947 obj
, prefix
? prefix
: "(null)", element
);
950 * Set common CGI template variables...
954 cgiSetServerVersion();
957 * Loop through the attributes and set them for the template...
960 for (attr
= obj
; attr
&& attr
->group_tag
!= IPP_TAG_ZERO
; attr
= attr
->next
)
963 * Copy the attribute name, substituting "_" for "-"...
971 snprintf(name
, sizeof(name
), "%s.", prefix
);
972 nameptr
= name
+ strlen(name
);
977 for (i
= 0; attr
->name
[i
] && nameptr
< (name
+ sizeof(name
) - 1); i
++)
978 if (attr
->name
[i
] == '-')
981 *nameptr
++ = attr
->name
[i
];
986 * Add "job_printer_name" variable if we have a "job_printer_uri"
990 if (!strcmp(name
, "job_printer_uri"))
992 if ((valptr
= strrchr(attr
->values
[0].string
.text
, '/')) == NULL
)
997 cgiSetArray("job_printer_name", element
, valptr
);
1001 * Localize event names in "notify_events" variable...
1004 if (!strcmp(name
, "notify_events"))
1006 size_t remaining
; /* Remaining bytes in buffer */
1012 for (i
= 0; i
< attr
->num_values
; i
++)
1014 if (valptr
>= (value
+ sizeof(value
) - 3))
1023 remaining
= sizeof(value
) - (size_t)(valptr
- value
);
1025 if (!strcmp(attr
->values
[i
].string
.text
, "printer-stopped"))
1026 strlcpy(valptr
, _("Printer Paused"), remaining
);
1027 else if (!strcmp(attr
->values
[i
].string
.text
, "printer-added"))
1028 strlcpy(valptr
, _("Printer Added"), remaining
);
1029 else if (!strcmp(attr
->values
[i
].string
.text
, "printer-modified"))
1030 strlcpy(valptr
, _("Printer Modified"), remaining
);
1031 else if (!strcmp(attr
->values
[i
].string
.text
, "printer-deleted"))
1032 strlcpy(valptr
, _("Printer Deleted"), remaining
);
1033 else if (!strcmp(attr
->values
[i
].string
.text
, "job-created"))
1034 strlcpy(valptr
, _("Job Created"), remaining
);
1035 else if (!strcmp(attr
->values
[i
].string
.text
, "job-completed"))
1036 strlcpy(valptr
, _("Job Completed"), remaining
);
1037 else if (!strcmp(attr
->values
[i
].string
.text
, "job-stopped"))
1038 strlcpy(valptr
, _("Job Stopped"), remaining
);
1039 else if (!strcmp(attr
->values
[i
].string
.text
, "job-config-changed"))
1040 strlcpy(valptr
, _("Job Options Changed"), remaining
);
1041 else if (!strcmp(attr
->values
[i
].string
.text
, "server-restarted"))
1042 strlcpy(valptr
, _("Server Restarted"), remaining
);
1043 else if (!strcmp(attr
->values
[i
].string
.text
, "server-started"))
1044 strlcpy(valptr
, _("Server Started"), remaining
);
1045 else if (!strcmp(attr
->values
[i
].string
.text
, "server-stopped"))
1046 strlcpy(valptr
, _("Server Stopped"), remaining
);
1047 else if (!strcmp(attr
->values
[i
].string
.text
, "server-audit"))
1048 strlcpy(valptr
, _("Server Security Auditing"), remaining
);
1050 strlcpy(valptr
, attr
->values
[i
].string
.text
, remaining
);
1052 valptr
+= strlen(valptr
);
1055 cgiSetArray("notify_events", element
, value
);
1060 * Add "notify_printer_name" variable if we have a "notify_printer_uri"
1064 if (!strcmp(name
, "notify_printer_uri"))
1066 if ((valptr
= strrchr(attr
->values
[0].string
.text
, '/')) == NULL
)
1071 cgiSetArray("notify_printer_name", element
, valptr
);
1075 * Add "notify_recipient_name" variable if we have a "notify_recipient_uri"
1076 * attribute, and rewrite recipient URI...
1079 if (!strcmp(name
, "notify_recipient_uri"))
1081 char uri
[1024], /* New URI */
1082 scheme
[32], /* Scheme portion of URI */
1083 userpass
[256], /* Username/password portion of URI */
1084 host
[1024], /* Hostname portion of URI */
1085 resource
[1024], /* Resource portion of URI */
1086 *options
; /* Options in URI */
1087 int port
; /* Port number */
1090 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
,
1091 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
1092 host
, sizeof(host
), &port
, resource
, sizeof(resource
));
1094 if (!strcmp(scheme
, "rss"))
1097 * RSS notification...
1100 if ((options
= strchr(resource
, '?')) != NULL
)
1106 * Link to remote feed...
1109 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "http",
1110 userpass
, host
, port
, resource
);
1111 strlcpy(name
, uri
, sizeof(name
));
1116 * Link to local feed...
1119 snprintf(uri
, sizeof(uri
), "/rss%s", resource
);
1120 strlcpy(name
, resource
+ 1, sizeof(name
));
1129 strlcpy(uri
, attr
->values
[0].string
.text
, sizeof(uri
));
1130 strlcpy(name
, resource
, sizeof(name
));
1133 cgiSetArray("notify_recipient_uri", element
, uri
);
1134 cgiSetArray("notify_recipient_name", element
, name
);
1139 * Add "admin_uri" variable if we have a "printer_uri_supported"
1143 if (!strcmp(name
, "printer_uri_supported"))
1145 cgiRewriteURL(attr
->values
[0].string
.text
, value
, sizeof(value
),
1148 cgiSetArray("admin_uri", element
, value
);
1155 value
[0] = '\0'; /* Initially an empty string */
1156 valptr
= value
; /* Start at the beginning */
1158 for (i
= 0; i
< attr
->num_values
; i
++)
1161 strlcat(valptr
, ", ", sizeof(value
) - (size_t)(valptr
- value
));
1163 valptr
+= strlen(valptr
);
1165 switch (attr
->value_tag
)
1167 case IPP_TAG_INTEGER
:
1169 if (strncmp(name
, "time_at_", 8) == 0)
1171 time_t t
; /* Temporary time value */
1173 t
= (time_t)attr
->values
[i
].integer
;
1174 date
= localtime(&t
);
1176 strftime(valptr
, sizeof(value
) - (size_t)(valptr
- value
), "%c", date
);
1179 snprintf(valptr
, sizeof(value
) - (size_t)(valptr
- value
),
1180 "%d", attr
->values
[i
].integer
);
1183 case IPP_TAG_BOOLEAN
:
1184 snprintf(valptr
, sizeof(value
) - (size_t)(valptr
- value
),
1185 "%d", attr
->values
[i
].boolean
);
1188 case IPP_TAG_NOVALUE
:
1189 strlcat(valptr
, "novalue", sizeof(value
) - (size_t)(valptr
- value
));
1192 case IPP_TAG_RANGE
:
1193 snprintf(valptr
, sizeof(value
) - (size_t)(valptr
- value
),
1194 "%d-%d", attr
->values
[i
].range
.lower
,
1195 attr
->values
[i
].range
.upper
);
1198 case IPP_TAG_RESOLUTION
:
1199 snprintf(valptr
, sizeof(value
) - (size_t)(valptr
- value
),
1200 "%dx%d%s", attr
->values
[i
].resolution
.xres
,
1201 attr
->values
[i
].resolution
.yres
,
1202 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
1207 if (strchr(attr
->values
[i
].string
.text
, ':') &&
1208 strcmp(name
, "device_uri"))
1214 if (!strcmp(name
, "member_uris"))
1216 char url
[1024]; /* URL for class member... */
1219 cgiRewriteURL(attr
->values
[i
].string
.text
, url
,
1222 snprintf(valptr
, sizeof(value
) - (size_t)(valptr
- value
),
1223 "<A HREF=\"%s\">%s</A>", url
,
1224 strrchr(attr
->values
[i
].string
.text
, '/') + 1);
1227 cgiRewriteURL(attr
->values
[i
].string
.text
, valptr
,
1228 (int)(sizeof(value
) - (size_t)(valptr
- value
)), NULL
);
1232 case IPP_TAG_STRING
:
1235 case IPP_TAG_KEYWORD
:
1236 case IPP_TAG_CHARSET
:
1237 case IPP_TAG_LANGUAGE
:
1238 case IPP_TAG_MIMETYPE
:
1239 strlcat(valptr
, attr
->values
[i
].string
.text
,
1240 sizeof(value
) - (size_t)(valptr
- value
));
1243 case IPP_TAG_BEGIN_COLLECTION
:
1244 snprintf(value
, sizeof(value
), "%s%d", name
, i
+ 1);
1245 cgiSetIPPVars(attr
->values
[i
].collection
, NULL
, NULL
, value
,
1250 break; /* anti-compiler-warning-code */
1255 * Add the element...
1258 if (attr
->value_tag
!= IPP_TAG_BEGIN_COLLECTION
)
1260 cgiSetArray(name
, element
, value
);
1262 fprintf(stderr
, "DEBUG2: %s[%d]=\"%s\"\n", name
, element
, value
);
1266 return (attr
? attr
->next
: NULL
);
1271 * 'cgiSetIPPVars()' - Set CGI variables from an IPP response.
1274 int /* O - Maximum number of elements */
1275 cgiSetIPPVars(ipp_t
*response
, /* I - Response data to be copied... */
1276 const char *filter_name
, /* I - Filter name */
1277 const char *filter_value
, /* I - Filter value */
1278 const char *prefix
, /* I - Prefix for name or NULL */
1279 int parent_el
) /* I - Parent element number */
1281 int element
; /* Element in CGI array */
1282 ipp_attribute_t
*attr
, /* Attribute in response... */
1283 *filter
; /* Filtering attribute */
1286 fprintf(stderr
, "DEBUG2: cgiSetIPPVars(response=%p, filter_name=\"%s\", "
1287 "filter_value=\"%s\", prefix=\"%s\", parent_el=%d)\n",
1288 response
, filter_name
? filter_name
: "(null)",
1289 filter_value
? filter_value
: "(null)",
1290 prefix
? prefix
: "(null)", parent_el
);
1293 * Set common CGI template variables...
1297 cgiSetServerVersion();
1300 * Loop through the attributes and set them for the template...
1303 attr
= response
->attrs
;
1306 while (attr
&& attr
->group_tag
== IPP_TAG_OPERATION
)
1309 for (element
= parent_el
; attr
; element
++)
1312 * Copy attributes to a separator...
1315 while (attr
&& attr
->group_tag
== IPP_TAG_ZERO
)
1324 filter
!= NULL
&& filter
->group_tag
!= IPP_TAG_ZERO
;
1325 filter
= filter
->next
)
1326 if (filter
->name
&& !strcmp(filter
->name
, filter_name
) &&
1327 (filter
->value_tag
== IPP_TAG_STRING
||
1328 (filter
->value_tag
>= IPP_TAG_TEXTLANG
&&
1329 filter
->value_tag
<= IPP_TAG_MIMETYPE
)) &&
1330 filter
->values
[0].string
.text
!= NULL
&&
1331 !_cups_strcasecmp(filter
->values
[0].string
.text
, filter_value
))
1335 return (element
+ 1);
1337 if (filter
->group_tag
== IPP_TAG_ZERO
)
1345 attr
= cgiSetIPPObjectVars(attr
, prefix
, element
);
1348 fprintf(stderr
, "DEBUG2: Returing %d from cgiSetIPPVars()...\n", element
);
1355 * 'cgiShowIPPError()' - Show the last IPP error message.
1357 * The caller must still call cgiStartHTML() and cgiEndHTML().
1361 cgiShowIPPError(const char *message
) /* I - Contextual message */
1363 cgiSetVariable("MESSAGE", cgiText(message
));
1364 cgiSetVariable("ERROR", cupsLastErrorString());
1365 cgiCopyTemplateLang("error.tmpl");
1370 * 'cgiShowJobs()' - Show print jobs.
1374 cgiShowJobs(http_t
*http
, /* I - Connection to server */
1375 const char *dest
) /* I - Destination name or NULL */
1377 int i
; /* Looping var */
1378 const char *which_jobs
; /* Which jobs to show */
1379 ipp_t
*request
, /* IPP request */
1380 *response
; /* IPP response */
1381 cups_array_t
*jobs
; /* Array of job objects */
1382 ipp_attribute_t
*job
; /* Job object */
1383 int ascending
, /* Order of jobs (0 = descending) */
1384 first
, /* First job to show */
1385 count
; /* Number of jobs */
1386 const char *var
, /* Form variable */
1387 *query
, /* Query string */
1388 *section
; /* Section in web interface */
1389 void *search
; /* Search data */
1390 char url
[1024], /* Printer URI */
1391 val
[1024]; /* Form variable */
1395 * Build an IPP_GET_JOBS request, which requires the following
1398 * attributes-charset
1399 * attributes-natural-language
1403 request
= ippNewRequest(IPP_GET_JOBS
);
1407 httpAssembleURIf(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "ipp", NULL
,
1408 "localhost", ippPort(), "/printers/%s", dest
);
1409 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1413 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1414 "ipp://localhost/");
1416 if ((which_jobs
= cgiGetVariable("which_jobs")) != NULL
&& *which_jobs
)
1417 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "which-jobs",
1420 cgiGetAttributes(request
, "jobs.tmpl");
1423 * Do the request and get back a response...
1426 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1429 * Get a list of matching job objects.
1432 if ((query
= cgiGetVariable("QUERY")) != NULL
&&
1433 !cgiGetVariable("CLEAR"))
1434 search
= cgiCompileSearch(query
);
1441 jobs
= cgiGetIPPObjects(response
, search
);
1442 count
= cupsArrayCount(jobs
);
1445 cgiFreeSearch(search
);
1448 * Figure out which jobs to display...
1451 if ((var
= cgiGetVariable("FIRST")) != NULL
)
1457 first
= count
- CUPS_PAGE_MAX
;
1459 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
1464 if ((var
= cgiGetVariable("ORDER")) != NULL
&& *var
)
1465 ascending
= !_cups_strcasecmp(var
, "asc");
1467 ascending
= !which_jobs
|| !*which_jobs
||
1468 !_cups_strcasecmp(which_jobs
, "not-completed");
1470 section
= cgiGetVariable("SECTION");
1472 cgiClearVariables();
1475 cgiSetVariable("QUERY", query
);
1477 cgiSetVariable("ORDER", ascending
? "asc" : "dec");
1479 cgiSetVariable("SECTION", section
);
1481 sprintf(val
, "%d", count
);
1482 cgiSetVariable("TOTAL", val
);
1485 cgiSetVariable("WHICH_JOBS", which_jobs
);
1489 for (i
= 0, job
= (ipp_attribute_t
*)cupsArrayIndex(jobs
, first
);
1490 i
< CUPS_PAGE_MAX
&& job
;
1491 i
++, job
= (ipp_attribute_t
*)cupsArrayNext(jobs
))
1492 cgiSetIPPObjectVars(job
, NULL
, i
);
1496 for (i
= 0, job
= (ipp_attribute_t
*)cupsArrayIndex(jobs
, count
- first
- 1);
1497 i
< CUPS_PAGE_MAX
&& job
;
1498 i
++, job
= (ipp_attribute_t
*)cupsArrayPrev(jobs
))
1499 cgiSetIPPObjectVars(job
, NULL
, i
);
1503 * Save navigation URLs...
1508 snprintf(val
, sizeof(val
), "/%s/%s", section
, dest
);
1509 cgiSetVariable("PRINTER_NAME", dest
);
1510 cgiSetVariable("PRINTER_URI_SUPPORTED", val
);
1513 strlcpy(val
, "/jobs/", sizeof(val
));
1515 cgiSetVariable("THISURL", val
);
1519 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
1520 cgiSetVariable("PREV", val
);
1523 if ((first
+ CUPS_PAGE_MAX
) < count
)
1525 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
1526 cgiSetVariable("NEXT", val
);
1530 * Then show everything...
1534 cgiSetVariable("SEARCH_DEST", dest
);
1536 cgiCopyTemplateLang("search.tmpl");
1538 cgiCopyTemplateLang("jobs-header.tmpl");
1540 if (count
> CUPS_PAGE_MAX
)
1541 cgiCopyTemplateLang("pager.tmpl");
1543 cgiCopyTemplateLang("jobs.tmpl");
1545 if (count
> CUPS_PAGE_MAX
)
1546 cgiCopyTemplateLang("pager.tmpl");
1548 cupsArrayDelete(jobs
);
1549 ippDelete(response
);
1555 * 'cgiText()' - Return localized text.
1558 const char * /* O - Localized message */
1559 cgiText(const char *message
) /* I - Message */
1561 static cups_lang_t
*language
= NULL
;
1566 language
= cupsLangDefault();
1568 return (_cupsLangString(language
, message
));