1 const char cgiedit_rcs[] = "$Id: cgi.c,v 1.25 2001/09/16 15:02:35 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include:
11 * Copyright : Written by and Copyright (C) 2001 the SourceForge
12 * IJBSWA team. http://ijbswa.sourceforge.net
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
18 * This program is free software; you can redistribute it
19 * and/or modify it under the terms of the GNU General
20 * Public License as published by the Free Software
21 * Foundation; either version 2 of the License, or (at
22 * your option) any later version.
24 * This program is distributed in the hope that it will
25 * be useful, but WITHOUT ANY WARRANTY; without even the
26 * implied warranty of MERCHANTABILITY or FITNESS FOR A
27 * PARTICULAR PURPOSE. See the GNU General Public
28 * License for more details.
30 * The GNU General Public License should be included with
31 * this file. If not, you can view it at
32 * http://www.gnu.org/copyleft/gpl.html
33 * or write to the Free Software Foundation, Inc., 59
34 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39 **********************************************************************/
45 * FIXME: Following includes copied from cgi.c - which are actually needed?
49 #include <sys/types.h>
56 #define snprintf _snprintf
57 #endif /* def _WIN32 */
73 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
76 #ifdef FEATURE_CGI_EDIT_ACTIONS
79 /*********************************************************************
81 * Function : cgi_edit_actions_list
83 * Description : CGI function that edits the actions list.
86 * 1 : csp = Current client state (buffers, headers, etc...)
87 * 2 : rsp = http_response data structure for output
88 * 3 : parameters = map of cgi parameters
90 * CGI Parameters : None
94 *********************************************************************/
95 int cgi_edit_actions_list(struct client_state *csp, struct http_response *rsp,
96 struct map *parameters)
99 struct url_actions *actions;
101 char * next_actions_html;
102 char * section_template;
106 struct map * exports = default_exports(csp, NULL);
107 struct map * section_exports;
108 struct map * url_exports;
114 if (((fl = csp->actions_list) == NULL) || ((actions = fl->f) == NULL))
116 /* FIXME: Oops, no file to edit */
118 return cgi_default(csp, rsp, parameters);
121 /* Should do all global exports above this point */
123 section_template = template_load(csp, "edit-actions-list-section");
124 url_template = template_load(csp, "edit-actions-list-url");
126 template_fill(§ion_template, exports);
127 template_fill(&url_template, exports);
130 sections = strdup("");
133 actions = actions->next;
136 actions_html = actions_to_html(actions->action);
139 while (actions != NULL)
141 section_exports = new_map();
143 snprintf(buf, 50, "%d", urlid);
144 map(section_exports, "sectionid", 1, buf, 1);
146 map(section_exports, "actions", 1, actions_html, 1);
148 /* Should do all section-specific exports above this point */
153 next_actions_html = NULL;
156 freez(next_actions_html);
158 url_exports = new_map();
160 snprintf(buf, 50, "%d", urlid);
161 map(url_exports, "urlid", 1, buf, 1);
163 snprintf(buf, 50, "%d", url_1_2);
164 map(url_exports, "url-1-2", 1, buf, 1);
166 s = html_encode(actions->url->spec);
167 map(url_exports, "url", 1, s, 1);
169 s = strdup(url_template);
170 template_fill(&s, section_exports);
171 template_fill(&s, url_exports);
172 urls = strsav(urls, s);
173 free_map(url_exports);
176 url_1_2 = 3 - url_1_2;
177 actions = actions->next;
180 next_actions_html = actions_to_html(actions->action);
183 while (actions && (0 == strcmp(actions_html, next_actions_html)));
185 map(section_exports, "urls", 1, urls, 0);
187 /* Could also do section-specific exports here, but it wouldn't be as fast */
189 s = strdup(section_template);
190 template_fill(&s, section_exports);
191 sections = strsav(sections, s);
192 free_map(section_exports);
195 actions_html = next_actions_html;
198 map(exports, "sections", 1, sections, 0);
200 /* Could also do global exports here, but it wouldn't be as fast */
202 rsp->body = template_load(csp, "edit-actions-list");
203 template_fill(&rsp->body, exports);
210 /*********************************************************************
212 * Function : map_radio
214 * Description : Map a set of radio button values. E.g. if you have
215 * 3 radio buttons, declare them as:
216 * <option type="radio" name="xyz" @xyz-a@>
217 * <option type="radio" name="xyz" @xyz-b@>
218 * <option type="radio" name="xyz" @xyz-c@>
219 * Then map one of the @xyz-?@ variables to "checked"
220 * and all the others to empty by calling:
221 * map_radio(exports, "xyz", "abc", sel)
222 * Where 'sel' is 'a', 'b', or 'c'.
225 * 1 : exports = Exports map to modify.
226 * 2 : optionname = name for map
227 * 3 : values = null-terminated list of values;
228 * 4 : value = Selected value.
230 * CGI Parameters : None
232 * Returns : 0 on success, nonzero on error.
234 *********************************************************************/
235 static int map_radio(struct map * exports, const char * optionname,
236 const char * values, char value)
247 len = strlen(optionname);
248 buf = malloc(len + 3);
254 strcpy(buf, optionname);
259 while ((c = *values++) != '\0')
264 if (map(exports, buf, 1, "", 1))
273 if (map(exports, buf, 0, "checked", 1))
283 /*********************************************************************
285 * Function : actions_to_radio
287 * Description : Converts a actionsfile entry FIXME
290 * 1 : exports = FIXME
293 * Returns : 0 on success, nonzero on error.
295 *********************************************************************/
296 static int actions_to_radio(struct map * exports, struct action_spec *action)
298 unsigned mask = action->mask;
299 unsigned add = action->add;
303 /* sanity - prevents "-feature +feature" */
307 #define DEFINE_ACTION_BOOL(name, bit) \
310 map_radio(exports, name, "ynx", 'n'); \
312 else if (add & bit) \
314 map_radio(exports, name, "ynx", 'y'); \
318 map_radio(exports, name, "ynx", 'x'); \
321 #define DEFINE_ACTION_STRING(name, bit, index) \
322 DEFINE_ACTION_BOOL(name, bit); \
325 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
328 checked = !strcmp(action->string[index], value); \
332 checked = is_default; \
334 mapped_param |= checked; \
335 map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1);
337 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
338 map(exports, name "-param-custom", 1, \
339 ((!mapped_param) ? "checked" : ""), 1); \
340 map(exports, name "-param", 1, \
341 (((add & bit) && !mapped_param) ? \
342 action->string[index] : default_val), 1);
344 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
345 map(exports, name "-param", 1, \
346 ((add & bit) ? action->string[index] : default_val), 1);
348 #define DEFINE_ACTION_MULTI(name, index) \
349 if (action->multi_add[index]->first) \
351 map_radio(exports, name, "ynx", 'y'); \
353 else if (action->multi_remove_all[index]) \
355 map_radio(exports, name, "ynx", 'n'); \
357 else if (action->multi_remove[index]->first) \
359 map_radio(exports, name, "ynx", 'y'); \
363 map_radio(exports, name, "ynx", 'x'); \
366 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
368 #include "actionlist.h"
370 #undef DEFINE_ACTION_MULTI
371 #undef DEFINE_ACTION_STRING
372 #undef DEFINE_ACTION_BOOL
373 #undef DEFINE_ACTION_ALIAS
374 #undef DEFINE_CGI_PARAM_CUSTOM
375 #undef DEFINE_CGI_PARAM_RADIO
376 #undef DEFINE_CGI_PARAM_NO_RADIO
382 /*********************************************************************
384 * Function : cgi_edit_actions
386 * Description : CGI function that edits the Actions list.
389 * 1 : csp = Current client state (buffers, headers, etc...)
390 * 2 : rsp = http_response data structure for output
391 * 3 : parameters = map of cgi parameters
393 * CGI Parameters : None
397 *********************************************************************/
398 int cgi_edit_actions(struct client_state *csp, struct http_response *rsp,
399 struct map *parameters)
401 struct file_list *fl;
402 struct url_actions *actions;
403 struct map * exports = default_exports(csp, NULL);
409 if (((fl = csp->actions_list) == NULL) || ((actions = fl->f) == NULL))
411 /* FIXME: Oops, No file to edit */
413 return cgi_default(csp, rsp, parameters);
416 s = lookup(parameters, "section");
419 /* FIXME: Oops, No file to edit */
421 return cgi_default(csp, rsp, parameters);
425 while ((c = *s++) != '\0')
428 * (((unsigned)(-1)) >> 1) == MAXINT.
429 * (MAXINT - 9) / 10 is the largest number that
430 * can be safely multiplied by 10 then have 9 added.
432 if (c < '0' || c > '9' || sectionid > ((((unsigned)(-1)) >> 1) - 9) / 10)
434 /* FIXME: Oops, bad paramaters */
436 return cgi_default(csp, rsp, parameters);
440 sectionid += c - '0';
443 for (i = 0; actions != NULL && i < sectionid; i++)
445 actions = actions->next;
448 if (actions == NULL || i != sectionid || sectionid < 1)
451 return cgi_default(csp, rsp, parameters);
454 /* FIXME: need to fill in exports here */
455 actions_to_radio(exports, actions->action);
457 rsp->body = template_load(csp, "edit-actions");
458 template_fill(&rsp->body, exports);
465 /*********************************************************************
467 * Function : cgi_edit_actions_submit
469 * Description : CGI function that actually edits the Actions list.
472 * 1 : csp = Current client state (buffers, headers, etc...)
473 * 2 : rsp = http_response data structure for output
474 * 3 : parameters = map of cgi parameters
476 * CGI Parameters : None
480 *********************************************************************/
481 int cgi_edit_actions_submit(struct client_state *csp, struct http_response *rsp,
482 struct map *parameters)
484 struct map * exports = default_exports(csp, NULL);
486 map(exports, "cgi-parameters", 1, dump_map(parameters), 0);
488 rsp->body = template_load(csp, "edit-actions-submit");
489 template_fill(&rsp->body, exports);
496 #endif /* def FEATURE_CGI_EDIT_ACTIONS */