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