1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.1 2001/09/16 15:47:37 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.
38 * Revision 1.1 2001/09/16 15:47:37 jongfoster
39 * First version of CGI-based edit interface. This is very much a
40 * work-in-progress, and you can't actually use it to edit anything
41 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
45 **********************************************************************/
51 * FIXME: Following includes copied from cgi.c - which are actually needed?
55 #include <sys/types.h>
62 #define snprintf _snprintf
63 #endif /* def _WIN32 */
78 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
81 #ifdef FEATURE_CGI_EDIT_ACTIONS
84 /*********************************************************************
86 * Function : cgi_edit_actions_list
88 * Description : CGI function that edits the actions list.
91 * 1 : csp = Current client state (buffers, headers, etc...)
92 * 2 : rsp = http_response data structure for output
93 * 3 : parameters = map of cgi parameters
95 * CGI Parameters : None
99 *********************************************************************/
100 int cgi_edit_actions_list(struct client_state *csp, struct http_response *rsp,
101 struct map *parameters)
103 struct file_list *fl;
104 struct url_actions *actions;
106 char * next_actions_html;
107 char * section_template;
111 struct map * exports = default_exports(csp, NULL);
112 struct map * section_exports;
113 struct map * url_exports;
119 if (((fl = csp->actions_list) == NULL) || ((actions = fl->f) == NULL))
121 /* FIXME: Oops, no file to edit */
123 return cgi_default(csp, rsp, parameters);
126 /* Should do all global exports above this point */
128 section_template = template_load(csp, "edit-actions-list-section");
129 url_template = template_load(csp, "edit-actions-list-url");
131 template_fill(§ion_template, exports);
132 template_fill(&url_template, exports);
135 sections = strdup("");
138 actions = actions->next;
141 actions_html = actions_to_html(actions->action);
144 while (actions != NULL)
146 section_exports = new_map();
148 snprintf(buf, 50, "%d", urlid);
149 map(section_exports, "sectionid", 1, buf, 1);
151 map(section_exports, "actions", 1, actions_html, 1);
153 /* Should do all section-specific exports above this point */
158 next_actions_html = NULL;
161 freez(next_actions_html);
163 url_exports = new_map();
165 snprintf(buf, 50, "%d", urlid);
166 map(url_exports, "urlid", 1, buf, 1);
168 snprintf(buf, 50, "%d", url_1_2);
169 map(url_exports, "url-1-2", 1, buf, 1);
171 s = html_encode(actions->url->spec);
172 map(url_exports, "url", 1, s, 1);
174 s = strdup(url_template);
175 template_fill(&s, section_exports);
176 template_fill(&s, url_exports);
177 urls = strsav(urls, s);
178 free_map(url_exports);
181 url_1_2 = 3 - url_1_2;
182 actions = actions->next;
185 next_actions_html = actions_to_html(actions->action);
188 while (actions && (0 == strcmp(actions_html, next_actions_html)));
190 map(section_exports, "urls", 1, urls, 0);
192 /* Could also do section-specific exports here, but it wouldn't be as fast */
194 s = strdup(section_template);
195 template_fill(&s, section_exports);
196 sections = strsav(sections, s);
197 free_map(section_exports);
200 actions_html = next_actions_html;
203 map(exports, "sections", 1, sections, 0);
205 /* Could also do global exports here, but it wouldn't be as fast */
207 rsp->body = template_load(csp, "edit-actions-list");
208 template_fill(&rsp->body, exports);
215 /*********************************************************************
217 * Function : map_radio
219 * Description : Map a set of radio button values. E.g. if you have
220 * 3 radio buttons, declare them as:
221 * <option type="radio" name="xyz" @xyz-a@>
222 * <option type="radio" name="xyz" @xyz-b@>
223 * <option type="radio" name="xyz" @xyz-c@>
224 * Then map one of the @xyz-?@ variables to "checked"
225 * and all the others to empty by calling:
226 * map_radio(exports, "xyz", "abc", sel)
227 * Where 'sel' is 'a', 'b', or 'c'.
230 * 1 : exports = Exports map to modify.
231 * 2 : optionname = name for map
232 * 3 : values = null-terminated list of values;
233 * 4 : value = Selected value.
235 * CGI Parameters : None
237 * Returns : 0 on success, nonzero on error.
239 *********************************************************************/
240 static int map_radio(struct map * exports, const char * optionname,
241 const char * values, char value)
252 len = strlen(optionname);
253 buf = malloc(len + 3);
259 strcpy(buf, optionname);
264 while ((c = *values++) != '\0')
269 if (map(exports, buf, 1, "", 1))
278 if (map(exports, buf, 0, "checked", 1))
288 /*********************************************************************
290 * Function : actions_to_radio
292 * Description : Converts a actionsfile entry FIXME
295 * 1 : exports = FIXME
298 * Returns : 0 on success, nonzero on error.
300 *********************************************************************/
301 static int actions_to_radio(struct map * exports, struct action_spec *action)
303 unsigned mask = action->mask;
304 unsigned add = action->add;
308 /* sanity - prevents "-feature +feature" */
312 #define DEFINE_ACTION_BOOL(name, bit) \
315 map_radio(exports, name, "ynx", 'n'); \
317 else if (add & bit) \
319 map_radio(exports, name, "ynx", 'y'); \
323 map_radio(exports, name, "ynx", 'x'); \
326 #define DEFINE_ACTION_STRING(name, bit, index) \
327 DEFINE_ACTION_BOOL(name, bit); \
330 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
333 checked = !strcmp(action->string[index], value); \
337 checked = is_default; \
339 mapped_param |= checked; \
340 map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1);
342 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
343 map(exports, name "-param-custom", 1, \
344 ((!mapped_param) ? "checked" : ""), 1); \
345 map(exports, name "-param", 1, \
346 (((add & bit) && !mapped_param) ? \
347 action->string[index] : default_val), 1);
349 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
350 map(exports, name "-param", 1, \
351 ((add & bit) ? action->string[index] : default_val), 1);
353 #define DEFINE_ACTION_MULTI(name, index) \
354 if (action->multi_add[index]->first) \
356 map_radio(exports, name, "ynx", 'y'); \
358 else if (action->multi_remove_all[index]) \
360 map_radio(exports, name, "ynx", 'n'); \
362 else if (action->multi_remove[index]->first) \
364 map_radio(exports, name, "ynx", 'y'); \
368 map_radio(exports, name, "ynx", 'x'); \
371 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
373 #include "actionlist.h"
375 #undef DEFINE_ACTION_MULTI
376 #undef DEFINE_ACTION_STRING
377 #undef DEFINE_ACTION_BOOL
378 #undef DEFINE_ACTION_ALIAS
379 #undef DEFINE_CGI_PARAM_CUSTOM
380 #undef DEFINE_CGI_PARAM_RADIO
381 #undef DEFINE_CGI_PARAM_NO_RADIO
387 /*********************************************************************
389 * Function : cgi_edit_actions
391 * Description : CGI function that edits the Actions list.
394 * 1 : csp = Current client state (buffers, headers, etc...)
395 * 2 : rsp = http_response data structure for output
396 * 3 : parameters = map of cgi parameters
398 * CGI Parameters : None
402 *********************************************************************/
403 int cgi_edit_actions(struct client_state *csp, struct http_response *rsp,
404 struct map *parameters)
406 struct file_list *fl;
407 struct url_actions *actions;
408 struct map * exports = default_exports(csp, NULL);
414 if (((fl = csp->actions_list) == NULL) || ((actions = fl->f) == NULL))
416 /* FIXME: Oops, No file to edit */
418 return cgi_default(csp, rsp, parameters);
421 s = lookup(parameters, "section");
424 /* FIXME: Oops, No file to edit */
426 return cgi_default(csp, rsp, parameters);
430 while ((c = *s++) != '\0')
433 * (((unsigned)(-1)) >> 1) == MAXINT.
434 * (MAXINT - 9) / 10 is the largest number that
435 * can be safely multiplied by 10 then have 9 added.
437 if (c < '0' || c > '9' || sectionid > ((((unsigned)(-1)) >> 1) - 9) / 10)
439 /* FIXME: Oops, bad paramaters */
441 return cgi_default(csp, rsp, parameters);
445 sectionid += c - '0';
448 for (i = 0; actions != NULL && i < sectionid; i++)
450 actions = actions->next;
453 if (actions == NULL || i != sectionid || sectionid < 1)
456 return cgi_default(csp, rsp, parameters);
459 /* FIXME: need to fill in exports here */
460 actions_to_radio(exports, actions->action);
462 rsp->body = template_load(csp, "edit-actions");
463 template_fill(&rsp->body, exports);
470 /*********************************************************************
472 * Function : cgi_edit_actions_submit
474 * Description : CGI function that actually edits the Actions list.
477 * 1 : csp = Current client state (buffers, headers, etc...)
478 * 2 : rsp = http_response data structure for output
479 * 3 : parameters = map of cgi parameters
481 * CGI Parameters : None
485 *********************************************************************/
486 int cgi_edit_actions_submit(struct client_state *csp, struct http_response *rsp,
487 struct map *parameters)
489 struct map * exports = default_exports(csp, NULL);
491 map(exports, "cgi-parameters", 1, dump_map(parameters), 0);
493 rsp->body = template_load(csp, "edit-actions-submit");
494 template_fill(&rsp->body, exports);
501 #endif /* def FEATURE_CGI_EDIT_ACTIONS */