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