]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/classes.c
Update copyright notices, addresses, etc.
[thirdparty/cups.git] / cgi-bin / classes.c
1 /*
2 * "$Id$"
3 *
4 * Class status CGI for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2005 by Easy Software Products.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Main entry for CGI.
27 */
28
29 /*
30 * Include necessary headers...
31 */
32
33 #include "ipp-var.h"
34
35
36 /*
37 * 'main()' - Main entry for CGI.
38 */
39
40 int /* O - Exit status */
41 main(int argc, /* I - Number of command-line arguments */
42 char *argv[]) /* I - Command-line arguments */
43 {
44 cups_lang_t *language; /* Language information */
45 char *pclass; /* Printer class name */
46 http_t *http; /* Connection to the server */
47 ipp_t *request, /* IPP request */
48 *response; /* IPP response */
49 ipp_attribute_t *attr; /* IPP attribute */
50 ipp_status_t status; /* Operation status... */
51 char uri[HTTP_MAX_URI]; /* Printer URI */
52 const char *which_jobs; /* Which jobs to show */
53 const char *op; /* Operation to perform, if any */
54 static const char *def_attrs[] = /* Attributes for default printer */
55 {
56 "printer-name",
57 "printer-uri-supported"
58 };
59
60
61 /*
62 * Get any form variables...
63 */
64
65 cgiInitialize();
66 op = cgiGetVariable("OP");
67
68 /*
69 * Get the request language...
70 */
71
72 language = cupsLangDefault();
73
74 /*
75 * Connect to the HTTP server...
76 */
77
78 http = httpConnectEncrypt("localhost", ippPort(), cupsEncryption());
79
80 /*
81 * Tell the client to expect HTML...
82 */
83
84 printf("Content-Type: text/html;charset=%s\n\n", cupsLangEncoding(language));
85
86 /*
87 * See if we need to show a list of printers or the status of a
88 * single printer...
89 */
90
91 ippSetServerVersion();
92
93 pclass = argv[0];
94 if (strcmp(pclass, "/") == 0 || strcmp(pclass, "classes.cgi") == 0)
95 {
96 pclass = NULL;
97 cgiSetVariable("TITLE", cupsLangString(language, CUPS_MSG_CLASS));
98 }
99 else
100 cgiSetVariable("TITLE", pclass);
101
102 cgiCopyTemplateLang(stdout, TEMPLATES, "header.tmpl", getenv("LANG"));
103
104 if (op == NULL || strcasecmp(op, "print-test-page") != 0)
105 {
106 /*
107 * Get the default destination...
108 */
109
110 request = ippNew();
111 request->request.op.operation_id = CUPS_GET_DEFAULT;
112 request->request.op.request_id = 1;
113
114 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
115 "attributes-charset", NULL, cupsLangEncoding(language));
116
117 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
118 "attributes-natural-language", NULL, language->language);
119
120 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
121 "requested-attributes",
122 sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
123
124 if ((response = cupsDoRequest(http, request, "/")) != NULL)
125 {
126 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
127 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
128
129 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
130 {
131 char url[HTTP_MAX_URI]; /* New URL */
132
133
134 cgiSetVariable("DEFAULT_URI",
135 ippRewriteURL(attr->values[0].string.text,
136 url, sizeof(url), NULL));
137 }
138
139 ippDelete(response);
140 }
141
142 /*
143 * Get the class info...
144 */
145
146 request = ippNew();
147
148 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
149 "attributes-charset", NULL, cupsLangEncoding(language));
150
151 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
152 "attributes-natural-language", NULL, language->language);
153
154 if (pclass == NULL)
155 {
156 /*
157 * Build a CUPS_GET_CLASSES request, which requires the following
158 * attributes:
159 *
160 * attributes-charset
161 * attributes-natural-language
162 */
163
164 request->request.op.operation_id = CUPS_GET_CLASSES;
165 request->request.op.request_id = 1;
166 }
167 else
168 {
169 /*
170 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
171 * attributes:
172 *
173 * attributes-charset
174 * attributes-natural-language
175 * printer-uri
176 */
177
178 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
179 request->request.op.request_id = 1;
180
181 snprintf(uri, sizeof(uri), "ipp://%s/classes/%s", getenv("SERVER_NAME"),
182 pclass);
183 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
184 uri);
185 }
186
187 ippGetAttributes(request, TEMPLATES, "classes.tmpl", getenv("LANG"));
188
189 /*
190 * Do the request and get back a response...
191 */
192
193 if ((response = cupsDoRequest(http, request, "/")) != NULL)
194 {
195 ippSetCGIVars(response, NULL, NULL, NULL, 0);
196 ippDelete(response);
197 }
198
199 /*
200 * Write the report...
201 */
202
203 cgiCopyTemplateLang(stdout, TEMPLATES, "classes.tmpl", getenv("LANG"));
204
205 /*
206 * Get jobs for the specified class if a class has been chosen...
207 */
208
209 if (pclass != NULL)
210 {
211 /*
212 * Build an IPP_GET_JOBS request, which requires the following
213 * attributes:
214 *
215 * attributes-charset
216 * attributes-natural-language
217 * printer-uri
218 */
219
220 request = ippNew();
221
222 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
223 "attributes-charset", NULL, cupsLangEncoding(language));
224
225 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
226 "attributes-natural-language", NULL, language->language);
227
228 request->request.op.operation_id = IPP_GET_JOBS;
229 request->request.op.request_id = 1;
230
231 snprintf(uri, sizeof(uri), "ipp://%s/classes/%s", getenv("SERVER_NAME"),
232 pclass);
233 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
234 uri);
235
236 if ((which_jobs = cgiGetVariable("which_jobs")) != NULL)
237 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs",
238 NULL, which_jobs);
239
240 ippGetAttributes(request, TEMPLATES, "jobs.tmpl", getenv("LANG"));
241
242 /*
243 * Do the request and get back a response...
244 */
245
246 if ((response = cupsDoRequest(http, request, "/")) != NULL)
247 {
248 ippSetCGIVars(response, NULL, NULL, NULL, 0);
249 ippDelete(response);
250
251 cgiCopyTemplateLang(stdout, TEMPLATES, "jobs.tmpl", getenv("LANG"));
252 }
253 }
254 }
255 else
256 {
257 /*
258 * Print a test page...
259 */
260
261 snprintf(uri, sizeof(uri), "ipp://localhost/classes/%s", pclass);
262
263 /*
264 * Build an IPP_PRINT_JOB request, which requires the following
265 * attributes:
266 *
267 * attributes-charset
268 * attributes-natural-language
269 * printer-uri
270 * requesting-user-name
271 * document-format
272 */
273
274 request = ippNew();
275
276 request->request.op.operation_id = IPP_PRINT_JOB;
277 request->request.op.request_id = 1;
278
279 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
280 "attributes-charset", NULL, cupsLangEncoding(language));
281
282 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
283 "attributes-natural-language", NULL, language->language);
284
285 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
286 NULL, uri);
287
288 if (getenv("REMOTE_USER") != NULL)
289 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
290 NULL, getenv("REMOTE_USER"));
291 else
292 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
293 NULL, "root");
294
295 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
296 NULL, "Test Page");
297
298 ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
299 NULL, "application/postscript");
300
301 /*
302 * Do the request and get back a response...
303 */
304
305 if ((response = cupsDoFileRequest(http, request, uri + 15,
306 CUPS_DATADIR "/data/testprint.ps")) != NULL)
307 {
308 status = response->request.status.status_code;
309 ippSetCGIVars(response, NULL, NULL, NULL, 0);
310
311 ippDelete(response);
312 }
313 else
314 status = cupsLastError();
315
316 cgiSetVariable("PRINTER_NAME", pclass);
317
318 if (status > IPP_OK_CONFLICT)
319 {
320 cgiSetVariable("ERROR", ippErrorString(status));
321 cgiCopyTemplateLang(stdout, TEMPLATES, "error.tmpl", getenv("LANG"));
322 }
323 else
324 cgiCopyTemplateLang(stdout, TEMPLATES, "test-page.tmpl", getenv("LANG"));
325 }
326
327 cgiCopyTemplateLang(stdout, TEMPLATES, "trailer.tmpl", getenv("LANG"));
328
329 /*
330 * Close the HTTP server connection...
331 */
332
333 httpClose(http);
334 cupsLangFree(language);
335
336 /*
337 * Return with no errors...
338 */
339
340 return (0);
341 }
342
343
344 /*
345 * End of "$Id$".
346 */