+ /* FIXME: Incomplete */
+
+ return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : cgi_edit_actions_list
+ *
+ * Description : CGI function that edits the actions list.
+ * FIXME: This function shouldn't FATAL ever.
+ * FIXME: This function doesn't check the retval of map()
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : rsp = http_response data structure for output
+ * 3 : parameters = map of cgi parameters
+ *
+ * CGI Parameters : filename
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out-of-memory
+ * JB_ERR_FILE if the file cannot be opened or
+ * contains no data
+ * JB_ERR_CGI_PARAMS if "filename" was not specified
+ * or is not valid.
+ *
+ *********************************************************************/
+jb_err cgi_edit_actions_list(struct client_state *csp,
+ struct http_response *rsp,
+ const struct map *parameters)
+{
+ char * section_template;
+ char * url_template;
+ char * sections;
+ char * urls;
+ char buf[150];
+ char * s;
+ struct map * exports;
+ struct map * section_exports;
+ struct map * url_exports;
+ struct editable_file * file;
+ struct file_line * cur_line;
+ unsigned line_number = 0;
+ unsigned prev_section_line_number = ((unsigned) (-1));
+ int i, url_1_2;
+ struct file_list * fl;
+ struct url_actions * b;
+ char * buttons = NULL;
+ jb_err err;
+
+ if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
+ {
+ return cgi_error_disabled(csp, rsp);
+ }
+
+ if (NULL == (exports = default_exports(csp, NULL)))
+ {
+ return JB_ERR_MEMORY;
+ }
+
+ /* Load actions file */
+ err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
+ if (err)
+ {
+ /* No filename specified, can't read file, or out of memory. */
+ free_map(exports);
+ return (err == JB_ERR_FILE ? JB_ERR_OK : err);
+ }
+
+ /* Find start of actions in file */
+ cur_line = file->lines;
+ line_number = 1;
+ while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
+ {
+ cur_line = cur_line->next;
+ line_number++;
+ }
+
+ /*
+ * Conventional actions files should have a match all block
+ * at the start:
+ * cur_line = {...global actions...}
+ * cur_line->next = /
+ * cur_line->next->next = {...actions...} or EOF
+ */
+ if ( (cur_line != NULL)
+ && (cur_line->type == FILE_LINE_ACTION)
+ && (cur_line->next != NULL)
+ && (cur_line->next->type == FILE_LINE_URL)
+ && (0 == strcmp(cur_line->next->unprocessed, "/"))
+ && ( (cur_line->next->next == NULL)
+ || (cur_line->next->next->type != FILE_LINE_URL)
+ ) )
+ {
+ /*
+ * Generate string with buttons to set actions for "/" to
+ * any predefined set of actions (named standard.*, probably
+ * residing in standard.action).
+ */
+
+ err = template_load(csp, §ion_template, "edit-actions-list-button", 0);
+ if (err)
+ {
+ edit_free_file(file);
+ free_map(exports);
+ if (err == JB_ERR_FILE)
+ {
+ return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
+ }
+ return err;
+ }
+
+ err = template_fill(§ion_template, exports);
+ if (err)
+ {
+ edit_free_file(file);
+ free_map(exports);
+ return err;
+ }
+
+ buttons = strdup("");
+ for (i = 0; i < MAX_AF_FILES; i++)
+ {
+ if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
+ {
+ for (b = b->next; NULL != b; b = b->next)
+ {
+ if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
+ {
+ if (err)
+ {
+ freez(buttons);
+ free(section_template);
+ edit_free_file(file);
+ free_map(exports);
+ return JB_ERR_MEMORY;
+ }
+
+ section_exports = new_map();
+ err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
+
+ if (err || (NULL == (s = strdup(section_template))))
+ {
+ free_map(section_exports);
+ freez(buttons);
+ free(section_template);
+ edit_free_file(file);
+ free_map(exports);
+ return JB_ERR_MEMORY;
+ }
+
+ if (!err) err = template_fill(&s, section_exports);
+ free_map(section_exports);
+ if (!err) err = string_join(&buttons, s);
+ }
+ }
+ }
+ }
+ freez(section_template);
+ if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
+
+ /*
+ * Conventional actions file, supply extra editing help.
+ * (e.g. don't allow them to make it an unconventional one).
+ */
+ if (!err) err = map_conditional(exports, "all-urls-present", 1);
+
+ snprintf(buf, sizeof(buf), "%u", line_number);
+ if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
+ snprintf(buf, sizeof(buf), "%u", line_number + 2);
+ if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
+ if (!err) err = map(exports, "all-urls-actions", 1,
+ actions_to_html(csp, cur_line->data.action), 0);
+
+ /* Skip the 2 lines */
+ cur_line = cur_line->next->next;
+ line_number += 2;
+
+ /*
+ * Note that prev_section_line_number is NOT set here.
+ * This is deliberate and not a bug. It stops a "Move up"
+ * option appearing on the next section. Clicking "Move
+ * up" would make the actions file unconventional, which
+ * we don't want, so we hide this option.
+ */
+ }
+ else
+ {
+ /*
+ * Non-standard actions file - does not begin with
+ * the "All URLs" section.
+ */
+ if (!err) err = map_conditional(exports, "all-urls-present", 0);
+ }
+
+ /* Set up global exports */
+
+ if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
+ if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
+ if (!err) err = map(exports, "v", 1, file->version_str, 1);
+
+ /* Discourage private additions to default.action */
+
+ if (!err) err = map_conditional(exports, "default-action",
+ (strstr("default.action", file->filename) != NULL));
+ if (err)
+ {
+ edit_free_file(file);
+ free_map(exports);
+ return err;
+ }
+
+ /* Should do all global exports above this point */
+
+ /* Load templates */
+
+ err = template_load(csp, §ion_template, "edit-actions-list-section", 0);
+ if (err)
+ {
+ edit_free_file(file);
+ free_map(exports);
+ if (err == JB_ERR_FILE)
+ {
+ return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
+ }
+ return err;
+ }
+
+ err = template_load(csp, &url_template, "edit-actions-list-url", 0);
+ if (err)
+ {
+ free(section_template);
+ edit_free_file(file);
+ free_map(exports);
+ if (err == JB_ERR_FILE)
+ {
+ return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
+ }
+ return err;
+ }
+
+ err = template_fill(§ion_template, exports);
+ if (err)
+ {
+ free(url_template);
+ edit_free_file(file);
+ free_map(exports);
+ return err;
+ }
+
+ err = template_fill(&url_template, exports);
+ if (err)
+ {
+ free(section_template);
+ edit_free_file(file);
+ free_map(exports);
+ return err;
+ }
+
+ if (NULL == (sections = strdup("")))
+ {
+ free(section_template);
+ free(url_template);
+ edit_free_file(file);
+ free_map(exports);
+ return JB_ERR_MEMORY;
+ }
+
+ while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
+ {
+ section_exports = new_map();
+
+ snprintf(buf, sizeof(buf), "%u", line_number);
+ err = map(section_exports, "s", 1, buf, 1);
+ if (!err) err = map(section_exports, "actions", 1,
+ actions_to_html(csp, cur_line->data.action), 0);
+
+ if ((!err)
+ && (cur_line->next != NULL)
+ && (cur_line->next->type == FILE_LINE_URL))
+ {
+ /* This section contains at least one URL, don't allow delete */
+ err = map_block_killer(section_exports, "empty-section");
+ }
+ else
+ {
+ if (!err) err = map_block_keep(section_exports, "empty-section");
+ }
+
+ if (prev_section_line_number != ((unsigned)(-1)))
+ {
+ /* Not last section */
+ snprintf(buf, sizeof(buf), "%u", prev_section_line_number);
+ if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
+ if (!err) err = map_block_keep(section_exports, "s-prev-exists");
+ }
+ else
+ {
+ /* Last section */
+ if (!err) err = map_block_killer(section_exports, "s-prev-exists");
+ }
+ prev_section_line_number = line_number;
+
+ if (err)
+ {
+ free(sections);
+ free(section_template);
+ free(url_template);
+ edit_free_file(file);
+ free_map(exports);
+ free_map(section_exports);
+ return err;
+ }
+
+ /* Should do all section-specific exports above this point */
+
+ if (NULL == (urls = strdup("")))
+ {
+ free(sections);
+ free(section_template);
+ free(url_template);
+ edit_free_file(file);
+ free_map(exports);
+ free_map(section_exports);
+ return JB_ERR_MEMORY;
+ }
+
+ url_1_2 = 2;
+
+ cur_line = cur_line->next;
+ line_number++;
+
+ while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
+ {
+ url_exports = new_map();
+
+ snprintf(buf, sizeof(buf), "%u", line_number);
+ err = map(url_exports, "p", 1, buf, 1);
+
+ snprintf(buf, sizeof(buf), "%d", url_1_2);
+ if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
+
+ if (!err) err = map(url_exports, "url-html", 1,
+ html_encode(cur_line->unprocessed), 0);
+ if (!err) err = map(url_exports, "url", 1,
+ url_encode(cur_line->unprocessed), 0);
+
+ if (err)
+ {
+ free(urls);
+ free(sections);
+ free(section_template);
+ free(url_template);
+ edit_free_file(file);
+ free_map(exports);
+ free_map(section_exports);
+ free_map(url_exports);
+ return err;
+ }
+
+ if (NULL == (s = strdup(url_template)))
+ {
+ free(urls);
+ free(sections);
+ free(section_template);
+ free(url_template);
+ edit_free_file(file);
+ free_map(exports);
+ free_map(section_exports);
+ free_map(url_exports);
+ return JB_ERR_MEMORY;
+ }
+
+ err = template_fill(&s, section_exports);
+ if (!err) err = template_fill(&s, url_exports);
+ if (!err) err = string_append(&urls, s);
+
+ free_map(url_exports);
+ freez(s);
+
+ if (err)
+ {
+ freez(urls);
+ free(sections);
+ free(section_template);
+ free(url_template);
+ edit_free_file(file);
+ free_map(exports);
+ free_map(section_exports);
+ return err;
+ }
+
+ url_1_2 = 3 - url_1_2;
+
+ cur_line = cur_line->next;
+ line_number++;
+ }
+
+ err = map(section_exports, "urls", 1, urls, 0);
+
+ /* Could also do section-specific exports here, but it wouldn't be as fast */
+
+ snprintf(buf, sizeof(buf), "%u", line_number);
+ if (!err) err = map(section_exports, "s-next", 1, buf, 1);
+
+ if ((cur_line != NULL)
+ && (cur_line->type == FILE_LINE_ACTION))
+ {
+ /* Not last section */
+ if (!err) err = map_block_keep(section_exports, "s-next-exists");
+ }
+ else
+ {
+ /* Last section */
+ if (!err) err = map_block_killer(section_exports, "s-next-exists");
+ }
+
+ if (err)
+ {
+ free(sections);
+ free(section_template);
+ free(url_template);
+ edit_free_file(file);
+ free_map(exports);
+ free_map(section_exports);
+ return err;
+ }
+
+ if (NULL == (s = strdup(section_template)))
+ {
+ free(sections);
+ free(section_template);
+ free(url_template);
+ edit_free_file(file);
+ free_map(exports);
+ free_map(section_exports);
+ return JB_ERR_MEMORY;
+ }
+
+ err = template_fill(&s, section_exports);
+ if (!err) err = string_append(§ions, s);
+
+ freez(s);
+ free_map(section_exports);
+
+ if (err)
+ {
+ freez(sections);
+ free(section_template);
+ free(url_template);
+ edit_free_file(file);
+ free_map(exports);
+ return err;
+ }
+ }
+
+ edit_free_file(file);
+ free(section_template);
+ free(url_template);