src/action_list_graphs.c: Properly escape HTML.
[collection4.git] / src / action_list_graphs.c
index 29e3d8b..964a436 100644 (file)
@@ -5,8 +5,9 @@
 
 #include "action_list_graphs.h"
 #include "graph.h"
+#include "graph_ident.h"
 #include "graph_list.h"
-#include "utils_params.h"
+#include "utils_cgi.h"
 
 #include <fcgiapp.h>
 #include <fcgi_stdio.h>
@@ -62,47 +63,95 @@ static int list_graphs_json (void) /* {{{ */
   return (0);
 } /* }}} int list_graphs_json */
 
+struct callback_data_s
+{
+  graph_config_t *cfg;
+  int limit;
+};
+typedef struct callback_data_s callback_data_t;
+
 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
     graph_instance_t *inst,
-    __attribute__((unused)) void *user_data)
+    void *user_data)
 {
+  callback_data_t *data = user_data;
   char params[1024];
   char desc[1024];
 
+  if (data->cfg != cfg)
+  {
+    if (data->cfg != NULL)
+      printf ("  </ul></li>\n");
+
+    memset (desc, 0, sizeof (desc));
+    graph_get_title (cfg, desc, sizeof (desc));
+    html_escape_buffer (desc, sizeof (desc));
+
+    printf ("  <li>%s\n  <ul>\n", desc);
+
+    data->cfg = cfg;
+  }
+
   memset (params, 0, sizeof (params));
   inst_get_params (cfg, inst, params, sizeof (params));
+  html_escape_buffer (params, sizeof (params));
 
   memset (desc, 0, sizeof (desc));
   inst_describe (cfg, inst, desc, sizeof (desc));
+  html_escape_buffer (desc, sizeof (desc));
+
+  printf ("    <li><a href=\"%s?action=graph;%s\">%s</a></li>\n",
+      script_name (), params, desc);
+
+  if (data->limit > 0)
+    data->limit--;
 
-  printf ("    <li><a href=\"test.fcgi?action=graph;%s\">%s</a></li>\n",
-      params, desc);
+  /* Abort scan if limit is reached. */
+  if (data->limit == 0)
+    return (1);
 
   return (0);
 } /* }}} int print_graph_inst_html */
 
-static int print_graph_html (graph_config_t *cfg, /* {{{ */
-    __attribute__((unused)) void *user_data)
+static int list_graphs_html (const char *term) /* {{{ */
 {
-  char buffer[1024];
+  callback_data_t data = { NULL, /* limit = */ 20 };
+  char *term_html;
 
-  memset (buffer, 0, sizeof (buffer));
-  graph_get_title (cfg, buffer, sizeof (buffer));
+  term_html = NULL;
+  if (term != NULL)
+    term_html = html_escape (term);
 
-  printf ("  <li>%s\n  <ul>\n", buffer);
-  gl_graph_instance_get_all (cfg, print_graph_inst_html, /* user_data = */ NULL);
-  printf ("  </ul></li>\n");
+  printf ("Content-Type: text/html\n\n");
 
-  return (0);
-} /* }}} int print_graph_html */
+  printf ("<html>\n  <head>\n");
+  if (term != NULL)
+    printf ("    <title>c4: Graphs matching &quot;%s&quot;</title>\n", term);
+  else
+    printf ("    <title>c4: List of all graphs</title>\n");
+  printf ("  </head>\n  <body>\n");
 
-static int list_graphs_html (void) /* {{{ */
-{
-  printf ("Content-Type: text/html\n\n");
+  printf ("<form action=\"%s\" method=\"get\">\n"
+      "  <input type=\"hidden\" name=\"action\" value=\"list_graphs\" />\n"
+      "  <input type=\"text\" name=\"search\" value=\"%s\" />\n"
+      "  <input type=\"submit\" name=\"button\" value=\"Search\" />\n"
+      "</form>\n",
+      script_name (), (term_html != NULL) ? term_html : "");
+
+  free (term_html);
+
+  printf ("    <ul>\n");
+  if (term == NULL)
+    gl_instance_get_all (print_graph_inst_html, /* user_data = */ &data);
+  else
+    gl_search (term, print_graph_inst_html, /* user_data = */ &data);
+
+  if (data.cfg != NULL)
+    printf ("      </ul></li>\n");
+
+  printf ("    </ul>\n");
 
-  printf ("<ul>\n");
-  gl_graph_get_all (print_graph_html, /* user_data = */ NULL);
-  printf ("</ul>\n");
+  printf ("  </body>\n</html>\n");
 
   return (0);
 } /* }}} int list_graphs_html */
@@ -120,7 +169,7 @@ int action_list_graphs (void) /* {{{ */
   if (strcmp ("json", format) == 0)
     return (list_graphs_json ());
   else
-    return (list_graphs_html ());
+    return (list_graphs_html (param ("search")));
 } /* }}} int action_list_graphs */
 
 /* vim: set sw=2 sts=2 et fdm=marker : */