]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/classes.c
3e103b846475b7a5604ec9cadaeb461f7ce6338c
[thirdparty/cups.git] / cgi-bin / classes.c
1 /*
2 * "$Id: classes.c,v 1.18.2.5 2002/12/10 19:14:04 mike Exp $"
3 *
4 * Class status CGI for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2002 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-3111 USA
19 *
20 * Voice: (301) 373-9603
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];
52 /* Printer URI */
53 const char *which_jobs; /* Which jobs to show */
54 const char *op; /* Operation to perform, if any */
55
56
57 /*
58 * Get any form variables...
59 */
60
61 cgiInitialize();
62 op = cgiGetVariable("OP");
63
64 /*
65 * Get the request language...
66 */
67
68 language = cupsLangDefault();
69
70 /*
71 * Connect to the HTTP server...
72 */
73
74 http = httpConnectEncrypt("localhost", ippPort(), cupsEncryption());
75
76 /*
77 * Tell the client to expect HTML...
78 */
79
80 printf("Content-Type: text/html;charset=%s\n\n", cupsLangEncoding(language));
81
82 /*
83 * See if we need to show a list of printers or the status of a
84 * single printer...
85 */
86
87 ippSetServerVersion();
88
89 pclass = argv[0];
90 if (strcmp(pclass, "/") == 0 || strcmp(pclass, "classes.cgi") == 0)
91 {
92 pclass = NULL;
93 cgiSetVariable("TITLE", cupsLangString(language, CUPS_MSG_CLASS));
94 }
95 else
96 cgiSetVariable("TITLE", pclass);
97
98 cgiCopyTemplateLang(stdout, TEMPLATES, "header.tmpl", getenv("LANG"));
99
100 if (op == NULL || strcasecmp(op, "print-test-page") != 0)
101 {
102 /*
103 * Get the default destination...
104 */
105
106 request = ippNew();
107 request->request.op.operation_id = CUPS_GET_DEFAULT;
108 request->request.op.request_id = 1;
109
110 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
111 "attributes-charset", NULL, cupsLangEncoding(language));
112
113 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
114 "attributes-natural-language", NULL, language->language);
115
116 if ((response = cupsDoRequest(http, request, "/")) != NULL)
117 {
118 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
119 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
120
121 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
122 {
123 char method[HTTP_MAX_URI],
124 username[HTTP_MAX_URI],
125 hostname[HTTP_MAX_URI],
126 resource[HTTP_MAX_URI],
127 uri[HTTP_MAX_URI];
128 int port; /* URI data */
129 const char *server; /* Name of server */
130
131
132 /*
133 * Map localhost access to localhost...
134 */
135
136 server = getenv("SERVER_NAME");
137
138 httpSeparate(attr->values[0].string.text, method, username,
139 hostname, &port, resource);
140
141 if (strcasecmp(hostname, server) == 0 &&
142 (strcmp(getenv("REMOTE_HOST"), "127.0.0.1") == 0 ||
143 strcmp(getenv("REMOTE_HOST"), "localhost") == 0 ||
144 strcmp(getenv("REMOTE_HOST"), server) == 0))
145 strcpy(hostname, "localhost");
146
147 /*
148 * Rewrite URI with HTTP address...
149 */
150
151 snprintf(uri, sizeof(uri), "http://%s:%d%s", hostname, port,
152 resource);
153
154 cgiSetVariable("DEFAULT_URI", uri);
155 }
156
157 ippDelete(response);
158 }
159
160 /*
161 * Get the class info...
162 */
163
164 request = ippNew();
165
166 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
167 "attributes-charset", NULL, cupsLangEncoding(language));
168
169 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
170 "attributes-natural-language", NULL, language->language);
171
172 if (pclass == NULL)
173 {
174 /*
175 * Build a CUPS_GET_CLASSES request, which requires the following
176 * attributes:
177 *
178 * attributes-charset
179 * attributes-natural-language
180 */
181
182 request->request.op.operation_id = CUPS_GET_CLASSES;
183 request->request.op.request_id = 1;
184 }
185 else
186 {
187 /*
188 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
189 * attributes:
190 *
191 * attributes-charset
192 * attributes-natural-language
193 * printer-uri
194 */
195
196 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
197 request->request.op.request_id = 1;
198
199 snprintf(uri, sizeof(uri), "ipp://%s/classes/%s", getenv("SERVER_NAME"),
200 pclass);
201 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
202 uri);
203 }
204
205 /*
206 * Do the request and get back a response...
207 */
208
209 if ((response = cupsDoRequest(http, request, "/")) != NULL)
210 {
211 ippSetCGIVars(response, NULL, NULL);
212 ippDelete(response);
213 }
214
215 /*
216 * Write the report...
217 */
218
219 cgiCopyTemplateLang(stdout, TEMPLATES, "classes.tmpl", getenv("LANG"));
220
221 /*
222 * Get jobs for the specified class if a class has been chosen...
223 */
224
225 if (pclass != NULL)
226 {
227 /*
228 * Build an IPP_GET_JOBS request, which requires the following
229 * attributes:
230 *
231 * attributes-charset
232 * attributes-natural-language
233 * printer-uri
234 */
235
236 request = ippNew();
237
238 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
239 "attributes-charset", NULL, cupsLangEncoding(language));
240
241 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
242 "attributes-natural-language", NULL, language->language);
243
244 request->request.op.operation_id = IPP_GET_JOBS;
245 request->request.op.request_id = 1;
246
247 snprintf(uri, sizeof(uri), "ipp://%s/classes/%s", getenv("SERVER_NAME"),
248 pclass);
249 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
250 uri);
251
252 if ((which_jobs = cgiGetVariable("which_jobs")) != NULL)
253 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs",
254 NULL, which_jobs);
255
256 /*
257 * Do the request and get back a response...
258 */
259
260 if ((response = cupsDoRequest(http, request, "/")) != NULL)
261 {
262 ippSetCGIVars(response, NULL, NULL);
263 ippDelete(response);
264
265 cgiCopyTemplateLang(stdout, TEMPLATES, "jobs.tmpl", getenv("LANG"));
266 }
267 }
268 }
269 else
270 {
271 /*
272 * Print a test page...
273 */
274
275 snprintf(uri, sizeof(uri), "ipp://localhost/classes/%s", pclass);
276
277 /*
278 * Build an IPP_PRINT_JOB request, which requires the following
279 * attributes:
280 *
281 * attributes-charset
282 * attributes-natural-language
283 * printer-uri
284 * requesting-user-name
285 * document-format
286 */
287
288 request = ippNew();
289
290 request->request.op.operation_id = IPP_PRINT_JOB;
291 request->request.op.request_id = 1;
292
293 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
294 "attributes-charset", NULL, cupsLangEncoding(language));
295
296 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
297 "attributes-natural-language", NULL, language->language);
298
299 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
300 NULL, uri);
301
302 if (getenv("REMOTE_USER") != NULL)
303 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
304 NULL, getenv("REMOTE_USER"));
305 else
306 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
307 NULL, "root");
308
309 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
310 NULL, "Test Page");
311
312 ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
313 NULL, "application/postscript");
314
315 /*
316 * Do the request and get back a response...
317 */
318
319 if ((response = cupsDoFileRequest(http, request, uri + 15,
320 CUPS_DATADIR "/data/testprint.ps")) != NULL)
321 {
322 status = response->request.status.status_code;
323 ippSetCGIVars(response, NULL, NULL);
324
325 ippDelete(response);
326 }
327 else
328 status = cupsLastError();
329
330 cgiSetVariable("PRINTER_NAME", pclass);
331
332 if (status > IPP_OK_CONFLICT)
333 {
334 cgiSetVariable("ERROR", ippErrorString(status));
335 cgiCopyTemplateLang(stdout, TEMPLATES, "error.tmpl", getenv("LANG"));
336 }
337 else
338 cgiCopyTemplateLang(stdout, TEMPLATES, "test-page.tmpl", getenv("LANG"));
339 }
340
341 cgiCopyTemplateLang(stdout, TEMPLATES, "trailer.tmpl", getenv("LANG"));
342
343 /*
344 * Close the HTTP server connection...
345 */
346
347 httpClose(http);
348 cupsLangFree(language);
349
350 /*
351 * Return with no errors...
352 */
353
354 return (0);
355 }
356
357
358 /*
359 * End of "$Id: classes.c,v 1.18.2.5 2002/12/10 19:14:04 mike Exp $".
360 */