From 37e2c84b2b657200d1b28b72bbda897feec10912 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Wed, 7 Oct 2009 22:30:16 +0200 Subject: [PATCH] Install formhistory resources, don't fill in passwords Reading post data is there but nothing is written to disk yet. --- data/autosuggestcontrol.js | 6 +- extensions/formhistory.c | 135 ++++++++++++++++++++++++++++++------- wscript | 2 + 3 files changed, 118 insertions(+), 25 deletions(-) diff --git a/data/autosuggestcontrol.js b/data/autosuggestcontrol.js index 0b583360..fc0d251b 100644 --- a/data/autosuggestcontrol.js +++ b/data/autosuggestcontrol.js @@ -341,6 +341,9 @@ FormSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*: var aSuggestions = []; var sTextboxValue = oAutoSuggestControl.textbox.value; + if (!this.suggestions) + return; + if (sTextboxValue.length > 0){ //search for matching suggestions for (var i=0; i < this.suggestions.length; i++) { @@ -361,6 +364,7 @@ function initSuggestions () { var eid = inputs[i].getAttribute("id"); if (!ename && eid) ename=eid; - var smth = new AutoSuggestControl(inputs[i], new FormSuggestions(ename)); + if (inputs[i].type == "text") + var smth = new AutoSuggestControl(inputs[i], new FormSuggestions(ename)); } }; diff --git a/extensions/formhistory.c b/extensions/formhistory.c index 5907f783..5b6de326 100644 --- a/extensions/formhistory.c +++ b/extensions/formhistory.c @@ -5,12 +5,13 @@ modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - */ +#define MAXCHARS 20 +#define MINCHARS 2 + #include -#include #include "config.h" #include @@ -18,20 +19,28 @@ #include #endif +static GHashTable* global_keys; static gchar* jsforms; -static void +static gboolean formhistory_prepare_js () { gchar* autosuggest; gchar* style; guint i; + gchar* file; - /* FIXME: Don't hardcode paths */ - g_file_get_contents ("/usr/local/share/midori/autosuggestcontrol.js", &autosuggest, NULL, NULL); + gchar* data_path = g_build_filename (MDATADIR, PACKAGE_NAME, NULL); + file = g_build_filename (data_path,"/autosuggestcontrol.js",NULL); + if (!g_file_test (file, G_FILE_TEST_EXISTS)) + return FALSE; + g_file_get_contents (file, &autosuggest, NULL, NULL); g_strchomp (autosuggest); - g_file_get_contents ("/usr/local/share/midori/autosuggestcontrol.css", &style, NULL, NULL); + file = g_build_filename (data_path,"/autosuggestcontrol.css",NULL); + if (!g_file_test (file, G_FILE_TEST_EXISTS)) + return FALSE; + g_file_get_contents (file, &style, NULL, NULL); g_strchomp (style); i = 0; while (style[i]) @@ -40,7 +49,6 @@ formhistory_prepare_js () style[i] = ' '; i++; } - g_print ("%s\n", style); jsforms = g_strdup_printf ( "%s" @@ -57,15 +65,23 @@ formhistory_prepare_js () autosuggest, style); g_strstrip (jsforms); + g_free (data_path); + g_free (file); g_free (style); g_free (autosuggest); + return TRUE; } static gchar* formhistory_build_js () { - const gchar* suggestions = "arr[\"txt1\"] = [\"Alabama\", \"Alaska\", \"Arizona\", \"Arkansas\"];" - "arr[\"txt2\"] = [\"Alabama\", \"Alaska\", \"Arizona\", \"Arkansas\"];"; + const gchar* suggestions = ""; + GHashTableIter iter; + gpointer key, value; + g_hash_table_iter_init (&iter, global_keys); + while (g_hash_table_iter_next (&iter, &key, &value)) + suggestions = g_strdup_printf ("%s arr[\"%s\"] = [%s]; ", + suggestions, (char*)key, (char*)value); gchar* script = g_strdup_printf ("function FormSuggestions(eid) { " "arr = new Array();" "%s" @@ -76,6 +92,40 @@ formhistory_build_js () return script; } +static void +formhistory_update_main_hash (GHashTable* keys) +{ + GHashTableIter iter; + gchar* tmp = ""; + gchar* new_value = ""; + gchar* key = ""; + gchar* value = ""; + g_hash_table_iter_init (&iter, keys); + while (g_hash_table_iter_next (&iter, (gpointer)&key, (gpointer)&value)) + { + if (value && *value && (strlen (value) > MAXCHARS || strlen (value) < MINCHARS)) + return; + + tmp = g_hash_table_lookup (global_keys, (gpointer)key); + if (tmp) + { + gchar* rvalue = g_strdup_printf ("\"%s\"",value); + if (!g_regex_match_simple (rvalue, tmp, + G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY)) + { + new_value = g_strdup_printf ("%s%s,", tmp, rvalue); + g_hash_table_replace (global_keys, key, new_value); + } + g_free (rvalue); + } + else + { + new_value = g_strdup_printf ("\"%s\",",value); + g_hash_table_insert (global_keys, key, new_value); + } + } +} + static void formhistory_session_request_queued_cb (SoupSession* session, SoupMessage* msg) @@ -84,17 +134,12 @@ formhistory_session_request_queued_cb (SoupSession* session, if (method[0] == 'P' && method[1] == 'O' && method[2] == 'S') { SoupMessageHeaders* hdrs = msg->request_headers; - SoupMessageHeadersIter iter; - const gchar* name, *value; + /* FIXME: Need a permanent storage implementation */ + const char* referer = soup_message_headers_get_one (hdrs, "Referer"); SoupMessageBody* body = msg->request_body; - - soup_message_headers_iter_init (&iter, hdrs); - while (soup_message_headers_iter_next (&iter, &name, &value)) - { - g_warning ("%s=%s\n", name, value); - } soup_buffer_free (soup_message_body_flatten (body)); - g_warning ("BODY: %s\n", body->data); + GHashTable* keys = soup_form_decode (body->data); + formhistory_update_main_hash (keys); } g_free (method); } @@ -118,7 +163,6 @@ formhistory_add_tab_cb (MidoriBrowser* browser, SoupSession *session = webkit_get_default_session (); g_signal_connect (web_view, "window-object-cleared", G_CALLBACK (formhistory_window_object_cleared_cb), 0); - /* FIXME: Deactivate request-queued on unload */ g_signal_connect (session, "request-queued", G_CALLBACK (formhistory_session_request_queued_cb), 0); } @@ -151,10 +195,13 @@ formhistory_deactivate_tabs (MidoriView* view, MidoriBrowser* browser) { GtkWidget* web_view = gtk_bin_get_child (GTK_BIN (view)); + SoupSession *session = webkit_get_default_session (); g_signal_handlers_disconnect_by_func ( browser, formhistory_add_tab_cb, 0); g_signal_handlers_disconnect_by_func ( web_view, formhistory_window_object_cleared_cb, 0); + g_signal_handlers_disconnect_by_func ( + session, formhistory_session_request_queued_cb, 0); } static void @@ -168,6 +215,10 @@ formhistory_deactivate_cb (MidoriExtension* extension, g_signal_handlers_disconnect_by_func ( app, formhistory_app_add_browser_cb, extension); midori_browser_foreach (browser, (GtkCallback)formhistory_deactivate_tabs, browser); + + jsforms = ""; + if (global_keys) + g_hash_table_destroy (global_keys); } static void @@ -178,7 +229,9 @@ formhistory_activate_cb (MidoriExtension* extension, MidoriBrowser* browser; guint i; - formhistory_prepare_js (); + global_keys = g_hash_table_new_full (g_str_hash, g_str_equal, + (GDestroyNotify)g_free, + (GDestroyNotify)g_free); browsers = katze_object_get_object (app, "browsers"); i = 0; while ((browser = katze_array_get_nth_item (browsers, i++))) @@ -189,17 +242,51 @@ formhistory_activate_cb (MidoriExtension* extension, g_object_unref (browsers); } +#if G_ENABLE_DEBUG +/* + + + autosuggest testcase + + +
+

+

+ +
+ + */ +#endif + MidoriExtension* extension_init (void) { + gboolean should_init = TRUE; + gchar* ver; + gchar* desc; + if (formhistory_prepare_js ()) + { + ver = "0.1"; + desc = g_strdup (_("Stores history of entered form data")); + } + else + { + desc = g_strdup_printf (_("Not available: %s"), + _("Resource files not installed")); + ver = NULL; + should_init = FALSE; + } MidoriExtension* extension = g_object_new (MIDORI_TYPE_EXTENSION, "name", _("Form history filler"), - "description", _("Stores history of entered form data"), - "version", "0.1", + "description", desc, + "version", ver, "authors", "Alexander V. Butenko ", NULL); - g_signal_connect (extension, "activate", - G_CALLBACK (formhistory_activate_cb), NULL); + g_free (desc); + + if (should_init) + g_signal_connect (extension, "activate", + G_CALLBACK (formhistory_activate_cb), NULL); return extension; } diff --git a/wscript b/wscript index 00191824..32204d3b 100644 --- a/wscript +++ b/wscript @@ -453,6 +453,8 @@ def build (bld): bld.install_files ('${MDATADIR}/' + APPNAME + '/res', 'data/speeddial-head.html') bld.install_files ('${MDATADIR}/' + APPNAME + '/res', 'data/speeddial.json') bld.install_files ('${MDATADIR}/' + APPNAME + '/res', 'data/mootools.js') + bld.install_files ('${MDATADIR}/' + APPNAME, 'data/autosuggestcontrol.js') + bld.install_files ('${MDATADIR}/' + APPNAME, 'data/autosuggestcontrol.css') # FIXME: Determine the library naming for other platforms if Options.platform == 'linux':