]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/jobs.c
a0d7c4b222289a418d19303f0ee1c6f884d176f3
[thirdparty/cups.git] / cgi-bin / jobs.c
1 /*
2 * Job status CGI for CUPS.
3 *
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
6 *
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
12 */
13
14 /*
15 * Include necessary headers...
16 */
17
18 #include "cgi-private.h"
19
20
21 /*
22 * Local functions...
23 */
24
25 static void do_job_op(http_t *http, int job_id, ipp_op_t op);
26
27
28 /*
29 * 'main()' - Main entry for CGI.
30 */
31
32 int /* O - Exit status */
33 main(void)
34 {
35 http_t *http; /* Connection to the server */
36 const char *op; /* Operation name */
37 const char *job_id_var; /* Job ID form variable */
38 int job_id; /* Job ID */
39
40
41 /*
42 * Get any form variables...
43 */
44
45 cgiInitialize();
46
47 /*
48 * Set the web interface section...
49 */
50
51 cgiSetVariable("SECTION", "jobs");
52 cgiSetVariable("REFRESH_PAGE", "");
53
54 /*
55 * Connect to the HTTP server...
56 */
57
58 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
59
60 /*
61 * Get the job ID, if any...
62 */
63
64 if ((job_id_var = cgiGetVariable("JOB_ID")) != NULL)
65 job_id = atoi(job_id_var);
66 else
67 job_id = 0;
68
69 /*
70 * Do the operation...
71 */
72
73 if ((op = cgiGetVariable("OP")) != NULL && job_id > 0 && cgiIsPOST())
74 {
75 /*
76 * Do the operation...
77 */
78
79 if (!strcmp(op, "cancel-job"))
80 do_job_op(http, job_id, IPP_CANCEL_JOB);
81 else if (!strcmp(op, "hold-job"))
82 do_job_op(http, job_id, IPP_HOLD_JOB);
83 else if (!strcmp(op, "move-job"))
84 cgiMoveJobs(http, NULL, job_id);
85 else if (!strcmp(op, "release-job"))
86 do_job_op(http, job_id, IPP_RELEASE_JOB);
87 else if (!strcmp(op, "restart-job"))
88 do_job_op(http, job_id, IPP_RESTART_JOB);
89 else
90 {
91 /*
92 * Bad operation code... Display an error...
93 */
94
95 cgiStartHTML(cgiText(_("Jobs")));
96 cgiCopyTemplateLang("error-op.tmpl");
97 cgiEndHTML();
98 }
99 }
100 else
101 {
102 /*
103 * Show a list of jobs...
104 */
105
106 cgiStartHTML(cgiText(_("Jobs")));
107 cgiShowJobs(http, NULL);
108 cgiEndHTML();
109 }
110
111 /*
112 * Close the HTTP server connection...
113 */
114
115 httpClose(http);
116
117 /*
118 * Return with no errors...
119 */
120
121 return (0);
122 }
123
124
125 /*
126 * 'do_job_op()' - Do a job operation.
127 */
128
129 static void
130 do_job_op(http_t *http, /* I - HTTP connection */
131 int job_id, /* I - Job ID */
132 ipp_op_t op) /* I - Operation to perform */
133 {
134 ipp_t *request; /* IPP request */
135 char uri[HTTP_MAX_URI]; /* Job URI */
136 const char *user; /* Username */
137
138
139 /*
140 * Build a job request, which requires the following
141 * attributes:
142 *
143 * attributes-charset
144 * attributes-natural-language
145 * job-uri or printer-uri (purge-jobs)
146 * requesting-user-name
147 */
148
149 request = ippNewRequest(op);
150
151 snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
152
153 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
154 NULL, uri);
155
156 if ((user = getenv("REMOTE_USER")) == NULL)
157 user = "guest";
158
159 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
160 "requesting-user-name", NULL, user);
161
162 /*
163 * Do the request and get back a response...
164 */
165
166 ippDelete(cupsDoRequest(http, request, "/jobs"));
167
168 if (cupsLastError() <= IPP_OK_CONFLICT && getenv("HTTP_REFERER"))
169 {
170 /*
171 * Redirect successful updates back to the parent page...
172 */
173
174 char url[1024]; /* Encoded URL */
175
176
177 strlcpy(url, "5;URL=", sizeof(url));
178 cgiFormEncode(url + 6, getenv("HTTP_REFERER"), sizeof(url) - 6);
179 cgiSetVariable("refresh_page", url);
180 }
181 else if (cupsLastError() == IPP_NOT_AUTHORIZED)
182 {
183 puts("Status: 401\n");
184 exit(0);
185 }
186
187 cgiStartHTML(cgiText(_("Jobs")));
188
189 if (cupsLastError() > IPP_OK_CONFLICT)
190 cgiShowIPPError(_("Job operation failed"));
191 else if (op == IPP_CANCEL_JOB)
192 cgiCopyTemplateLang("job-cancel.tmpl");
193 else if (op == IPP_HOLD_JOB)
194 cgiCopyTemplateLang("job-hold.tmpl");
195 else if (op == IPP_RELEASE_JOB)
196 cgiCopyTemplateLang("job-release.tmpl");
197 else if (op == IPP_RESTART_JOB)
198 cgiCopyTemplateLang("job-restart.tmpl");
199
200 cgiEndHTML();
201 }