2013-02-23 11:43:42 -05:00
|
|
|
/*
|
|
|
|
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file is based on Keepassdroid, Copyright Brian Pellin.
|
|
|
|
|
|
|
|
Keepass2Android is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Keepass2Android is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
using System;
|
2013-06-20 03:19:07 -04:00
|
|
|
using System.Collections.Generic;
|
2013-02-23 11:43:42 -05:00
|
|
|
using System.Text.RegularExpressions;
|
2013-12-31 03:46:57 -05:00
|
|
|
using KeePass.Util.Spr;
|
2013-06-20 03:19:07 -04:00
|
|
|
using KeePassLib;
|
2013-02-23 11:43:42 -05:00
|
|
|
using KeePassLib.Collections;
|
|
|
|
using KeePassLib.Interfaces;
|
|
|
|
using KeePassLib.Utility;
|
|
|
|
|
|
|
|
namespace keepass2android
|
|
|
|
{
|
2013-06-15 16:02:48 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Helper class providing methods to search a given database for specific things
|
|
|
|
/// </summary>
|
2013-02-23 11:43:42 -05:00
|
|
|
public class SearchDbHelper
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
private readonly IKp2aApp _app;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
|
2013-06-14 00:14:50 -04:00
|
|
|
public SearchDbHelper(IKp2aApp app) {
|
2013-06-15 06:40:01 -04:00
|
|
|
_app = app;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public PwGroup SearchForText (Database database, string str)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
SearchParameters sp = new SearchParameters {SearchString = str};
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-19 15:31:18 -04:00
|
|
|
return Search(database, sp, null);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2014-01-02 14:30:15 -05:00
|
|
|
public PwGroup Search(Database database, SearchParameters sp, IDictionary<PwUuid, KeyValuePair<string, string>> resultContexts)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
if(sp.RegularExpression) // Validate regular expression
|
|
|
|
{
|
|
|
|
new Regex(sp.SearchString);
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
string strGroupName = _app.GetResourceString(UiStringKey.search_results) + " (\"" + sp.SearchString + "\")";
|
|
|
|
PwGroup pgResults = new PwGroup(true, true, strGroupName, PwIcon.EMailSearch) {IsVirtual = true};
|
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
PwObjectList<PwEntry> listResults = pgResults.Entries;
|
|
|
|
|
|
|
|
|
2013-06-19 15:31:18 -04:00
|
|
|
database.Root.SearchEntries(sp, listResults, resultContexts, new NullStatusLogger());
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
return pgResults;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public PwGroup SearchForExactUrl (Database database, string url)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
SearchParameters sp = SearchParameters.None;
|
|
|
|
sp.SearchInUrls = true;
|
|
|
|
sp.SearchString = url;
|
|
|
|
|
|
|
|
if(sp.RegularExpression) // Validate regular expression
|
|
|
|
{
|
|
|
|
new Regex(sp.SearchString);
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
string strGroupName = _app.GetResourceString(UiStringKey.search_results) + " (\"" + sp.SearchString + "\")";
|
|
|
|
PwGroup pgResults = new PwGroup(true, true, strGroupName, PwIcon.EMailSearch) {IsVirtual = true};
|
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
PwObjectList<PwEntry> listResults = pgResults.Entries;
|
|
|
|
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
database.Root.SearchEntries(sp, listResults, new NullStatusLogger());
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
return pgResults;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-15 16:02:48 -04:00
|
|
|
private static String ExtractHost(String url)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
return UrlUtil.GetHost(url.Trim());
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public PwGroup SearchForHost(Database database, String url, bool allowSubdomains)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 16:02:48 -04:00
|
|
|
String host = ExtractHost(url);
|
2013-06-15 06:40:01 -04:00
|
|
|
string strGroupName = _app.GetResourceString(UiStringKey.search_results) + " (\"" + host + "\")";
|
|
|
|
PwGroup pgResults = new PwGroup(true, true, strGroupName, PwIcon.EMailSearch) {IsVirtual = true};
|
2013-02-23 11:43:42 -05:00
|
|
|
if (String.IsNullOrWhiteSpace(host))
|
|
|
|
return pgResults;
|
2013-06-15 06:40:01 -04:00
|
|
|
foreach (PwEntry entry in database.Entries.Values)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-12-31 03:46:57 -05:00
|
|
|
string otherUrl = entry.Strings.ReadSafe(PwDefs.UrlField);
|
|
|
|
otherUrl = SprEngine.Compile(otherUrl, new SprContext(entry, database.KpDatabase, SprCompileFlags.References));
|
|
|
|
String otherHost = ExtractHost(otherUrl);
|
2013-03-23 16:19:37 -04:00
|
|
|
if ((allowSubdomains) && (otherHost.StartsWith("www.")))
|
|
|
|
otherHost = otherHost.Substring(4); //remove "www."
|
2013-02-23 11:43:42 -05:00
|
|
|
if (String.IsNullOrWhiteSpace(otherHost))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-16 02:15:56 -04:00
|
|
|
if (host.IndexOf(otherHost, StringComparison.InvariantCultureIgnoreCase) > -1)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
pgResults.AddEntry(entry, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pgResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|