Load cups into easysw/current.
[thirdparty/cups.git] / cgi-bin / classes.c
1 /*
2  * "$Id: classes.c 4921 2006-01-12 21:26:26Z mike $"
3  *
4  *   Class status CGI for the Common UNIX Printing System (CUPS).
5  *
6  *   Copyright 1997-2006 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 "cgi-private.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   * Set the web interface section...
70   */
71
72   cgiSetVariable("SECTION", "classes");
73
74  /*
75   * Get the request language...
76   */
77
78   language = cupsLangDefault();
79
80  /*
81   * Connect to the HTTP server...
82   */
83
84   http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
85
86  /*
87   * Tell the client to expect UTF-8 encoded HTML...
88   */
89
90   puts("Content-Type: text/html;charset=utf-8\n");
91
92  /*
93   * See if we need to show a list of printers or the status of a
94   * single printer...
95   */
96
97   cgiSetServerVersion();
98
99   pclass = argv[0];
100   if (strcmp(pclass, "/") == 0 || strcmp(pclass, "classes.cgi") == 0)
101   {
102     pclass = NULL;
103     cgiSetVariable("TITLE", _cupsLangString(language, _("Class")));
104   }
105   else
106     cgiSetVariable("TITLE", pclass);
107
108   if (op == NULL || strcasecmp(op, "print-test-page") != 0)
109   {
110    /*
111     * Show the standard header...
112     */
113
114     cgiCopyTemplateLang("header.tmpl");
115
116    /*
117     * Get the default destination...
118     */
119
120     request = ippNew();
121     request->request.op.operation_id = CUPS_GET_DEFAULT;
122     request->request.op.request_id   = 1;
123
124     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
125                  "attributes-charset", NULL, cupsLangEncoding(language));
126
127     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
128                  "attributes-natural-language", NULL, language->language);
129
130     ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
131                   "requested-attributes",
132                   sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
133
134     if ((response = cupsDoRequest(http, request, "/")) != NULL)
135     {
136       if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
137         cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
138
139       if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
140       {
141         char    url[HTTP_MAX_URI];      /* New URL */
142
143
144         cgiSetVariable("DEFAULT_URI",
145                        cgiRewriteURL(attr->values[0].string.text,
146                                      url, sizeof(url), NULL));
147       }
148
149       ippDelete(response);
150     }
151
152    /*
153     * Get the class info...
154     */
155
156     request = ippNew();
157
158     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
159                  "attributes-charset", NULL, cupsLangEncoding(language));
160
161     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
162                  "attributes-natural-language", NULL, language->language);
163
164     if (pclass == NULL)
165     {
166      /*
167       * Build a CUPS_GET_CLASSES request, which requires the following
168       * attributes:
169       *
170       *    attributes-charset
171       *    attributes-natural-language
172       */
173
174       request->request.op.operation_id = CUPS_GET_CLASSES;
175       request->request.op.request_id   = 1;
176
177       if (getenv("REMOTE_USER") != NULL)
178         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
179                      NULL, getenv("REMOTE_USER"));
180     }
181     else
182     {
183      /*
184       * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
185       * attributes:
186       *
187       *    attributes-charset
188       *    attributes-natural-language
189       *    printer-uri
190       */
191
192       request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
193       request->request.op.request_id   = 1;
194
195       httpAssembleURIf(uri, sizeof(uri), "ipp", NULL, "localhost", 0,
196                        "/classes/%s", pclass);
197       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
198                    uri);
199     }
200
201     cgiGetAttributes(request, "classes.tmpl");
202
203    /*
204     * Do the request and get back a response...
205     */
206
207     if ((response = cupsDoRequest(http, request, "/")) != NULL)
208     {
209       cgiSetIPPVars(response, NULL, NULL, NULL, 0);
210       ippDelete(response);
211     }
212
213    /*
214     * Write the report...
215     */
216
217     cgiCopyTemplateLang("classes.tmpl");
218
219    /*
220     * Get jobs for the specified class if a class has been chosen...
221     */
222
223     if (pclass != NULL)
224     {
225      /*
226       * Build an IPP_GET_JOBS request, which requires the following
227       * attributes:
228       *
229       *    attributes-charset
230       *    attributes-natural-language
231       *    printer-uri
232       */
233
234       request = ippNew();
235
236       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
237                    "attributes-charset", NULL, cupsLangEncoding(language));
238
239       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
240                    "attributes-natural-language", NULL, language->language);
241
242       request->request.op.operation_id = IPP_GET_JOBS;
243       request->request.op.request_id   = 1;
244
245       httpAssembleURIf(uri, sizeof(uri), "ipp", NULL, "localhost", 0,
246                        "/classes/%s", pclass);
247       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
248                    uri);
249
250       if ((which_jobs = cgiGetVariable("which_jobs")) != NULL)
251         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs",
252                      NULL, which_jobs);
253
254       if (getenv("REMOTE_USER") != NULL)
255       {
256         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
257                      NULL, getenv("REMOTE_USER"));
258
259         if (strcmp(getenv("REMOTE_USER"), "root"))
260           ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
261       }
262       else
263         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
264                      NULL, "unknown");
265
266       cgiGetAttributes(request, "jobs.tmpl");
267
268      /*
269       * Do the request and get back a response...
270       */
271
272       if ((response = cupsDoRequest(http, request, "/")) != NULL)
273       {
274         cgiSetIPPVars(response, NULL, NULL, NULL, 0);
275         ippDelete(response);
276
277         cgiCopyTemplateLang("jobs.tmpl");
278       }
279     }
280   }
281   else
282   {
283    /*
284     * Print a test page...
285     */
286
287     char        filename[1024];         /* Test page filename */
288     const char  *datadir;               /* CUPS_DATADIR env var */
289     char        refresh[1024];          /* Refresh URL */
290
291
292     cgiFormEncode(uri, pclass, sizeof(uri));
293     snprintf(refresh, sizeof(refresh), "2;/classes/%s", uri);
294     cgiSetVariable("refresh_page", refresh);
295
296     if ((datadir = getenv("CUPS_DATADIR")) == NULL)
297       datadir = CUPS_DATADIR;
298
299     snprintf(filename, sizeof(filename), "%s/data/testprint.ps", datadir);
300     httpAssembleURIf(uri, sizeof(uri), "ipp", NULL, "localhost", 0,
301                      "/classes/%s", pclass);
302
303    /*
304     * Build an IPP_PRINT_JOB request, which requires the following
305     * attributes:
306     *
307     *    attributes-charset
308     *    attributes-natural-language
309     *    printer-uri
310     *    requesting-user-name
311     *    document-format
312     */
313
314     request = ippNew();
315
316     request->request.op.operation_id = IPP_PRINT_JOB;
317     request->request.op.request_id   = 1;
318
319     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
320                  "attributes-charset", NULL, cupsLangEncoding(language));
321
322     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
323                  "attributes-natural-language", NULL, language->language);
324
325     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
326                  NULL, uri);
327
328     if (getenv("REMOTE_USER") != NULL)
329       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
330                    NULL, getenv("REMOTE_USER"));
331     else
332       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
333                    NULL, "root");
334
335     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
336                  NULL, "Test Page");
337
338     ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
339                  NULL, "application/postscript");
340
341    /*
342     * Do the request and get back a response...
343     */
344
345     if ((response = cupsDoFileRequest(http, request, uri + 15,
346                                       filename)) != NULL)
347     {
348       status = response->request.status.status_code;
349       cgiSetIPPVars(response, NULL, NULL, NULL, 0);
350
351       ippDelete(response);
352     }
353     else
354       status = cupsLastError();
355
356     cgiSetVariable("PRINTER_NAME", pclass);
357
358    /*
359     * Show the standard header...
360     */
361
362     cgiCopyTemplateLang("header.tmpl");
363
364    /*
365     * Show the result...
366     */
367
368     if (status > IPP_OK_CONFLICT)
369     {
370       cgiSetVariable("ERROR", ippErrorString(status));
371       cgiCopyTemplateLang("error.tmpl");
372     }
373     else
374       cgiCopyTemplateLang("test-page.tmpl");
375   }
376
377   cgiCopyTemplateLang("trailer.tmpl");
378
379  /*
380   * Close the HTTP server connection...
381   */
382
383   httpClose(http);
384   cupsLangFree(language);
385
386  /*
387   * Return with no errors...
388   */
389
390   return (0);
391 }
392
393
394 /*
395  * End of "$Id: classes.c 4921 2006-01-12 21:26:26Z mike $".
396  */