-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathKeePassFaviconDownloaderExt.cs
542 lines (465 loc) · 22.7 KB
/
KeePassFaviconDownloaderExt.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
KeePass Favicon Downloader - KeePass plugin that downloads and stores
favicons for entries with web URLs.
Copyright (C) 2009-2014 Chris Tomlinson <luckyrat@users.sourceforge.net>
Thanks to mausoma and psproduction for their contributions
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Uses HtmlAgilityPack under MS-PL license: http://htmlagilitypack.codeplex.com/
*/
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Net;
using System.Drawing;
using KeePass.Plugins;
using KeePass.Forms;
using KeePass.Resources;
using KeePassLib;
using KeePassLib.Security;
using KeePassLib.Interfaces;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
namespace KeePassFaviconDownloader
{
public sealed class KeePassFaviconDownloaderExt : Plugin
{
// The plugin remembers its host in this variable.
private IPluginHost m_host = null;
public override string UpdateUrl
{
get { return "https://raw.github.com/luckyrat/KeePass-Favicon-Downloader/master/versionInfo.txt"; }
}
private ToolStripSeparator m_tsSeparator1 = null;
private ToolStripSeparator m_tsSeparator2 = null;
private ToolStripSeparator m_tsSeparator3 = null;
private ToolStripMenuItem menuDownloadFavicons = null;
private ToolStripMenuItem menuDownloadGroupFavicons = null;
private ToolStripMenuItem menuDownloadEntryFavicons = null;
/// <summary>
/// Initializes the plugin using the specified KeePass host.
/// </summary>
/// <param name="host">The plugin host.</param>
/// <returns></returns>
public override bool Initialize(IPluginHost host)
{
Debug.Assert(host != null);
if(host == null) return false;
m_host = host;
// Add a seperator and menu item to the 'Tools' menu
ToolStripItemCollection tsMenu = m_host.MainWindow.ToolsMenu.DropDownItems;
m_tsSeparator1 = new ToolStripSeparator();
tsMenu.Add(m_tsSeparator1);
menuDownloadFavicons = new ToolStripMenuItem();
menuDownloadFavicons.Text = "Download Favicons for all entries";
menuDownloadFavicons.Click += OnMenuDownloadFavicons;
tsMenu.Add(menuDownloadFavicons);
// Add a seperator and menu item to the group context menu
ContextMenuStrip gcm = m_host.MainWindow.GroupContextMenu;
m_tsSeparator2 = new ToolStripSeparator();
gcm.Items.Add(m_tsSeparator2);
menuDownloadGroupFavicons = new ToolStripMenuItem();
menuDownloadGroupFavicons.Text = "Download Favicons";
menuDownloadGroupFavicons.Click += OnMenuDownloadGroupFavicons;
gcm.Items.Add(menuDownloadGroupFavicons);
// Add a seperator and menu item to the entry context menu
ContextMenuStrip ecm = m_host.MainWindow.EntryContextMenu;
m_tsSeparator3 = new ToolStripSeparator();
ecm.Items.Add(m_tsSeparator3);
menuDownloadEntryFavicons = new ToolStripMenuItem();
menuDownloadEntryFavicons.Text = "Download Favicons";
menuDownloadEntryFavicons.Click += OnMenuDownloadEntryFavicons;
ecm.Items.Add(menuDownloadEntryFavicons);
return true; // Initialization successful
}
/// <summary>
/// Terminates this instance.
/// </summary>
public override void Terminate()
{
// Remove 'Tools' menu items
ToolStripItemCollection tsMenu = m_host.MainWindow.ToolsMenu.DropDownItems;
tsMenu.Remove(m_tsSeparator1);
tsMenu.Remove(menuDownloadFavicons);
// Remove group context menu items
ContextMenuStrip gcm = m_host.MainWindow.GroupContextMenu;
gcm.Items.Remove(m_tsSeparator2);
gcm.Items.Remove(menuDownloadGroupFavicons);
// Remove entry context menu items
ContextMenuStrip ecm = m_host.MainWindow.EntryContextMenu;
ecm.Items.Remove(m_tsSeparator3);
ecm.Items.Remove(menuDownloadEntryFavicons);
}
/// <summary>
/// Downloads favicons for every entry in the database
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnMenuDownloadFavicons(object sender, EventArgs e)
{
if(!m_host.Database.IsOpen)
{
MessageBox.Show("Please open a database first.", "Favicon downloader");
return;
}
KeePassLib.Collections.PwObjectList<PwEntry> output;
output = m_host.Database.RootGroup.GetEntries(true);
downloadSomeFavicons(output);
}
/// <summary>
/// Downloads favicons for every entry in the selected groups
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnMenuDownloadGroupFavicons(object sender, EventArgs e)
{
PwGroup pg = m_host.MainWindow.GetSelectedGroup();
Debug.Assert(pg != null); if (pg == null) return;
downloadSomeFavicons(pg.Entries);
}
/// <summary>
/// Downloads favicons for every selected entry
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnMenuDownloadEntryFavicons(object sender, EventArgs e)
{
PwEntry[] pwes = m_host.MainWindow.GetSelectedEntries();
Debug.Assert(pwes != null); if (pwes == null || pwes.Length == 0) return;
downloadSomeFavicons(KeePassLib.Collections.PwObjectList<PwEntry>.FromArray(pwes));
}
/// <summary>
/// Downloads some favicons.
/// </summary>
/// <param name="entries">The entries.</param>
private void downloadSomeFavicons(KeePassLib.Collections.PwObjectList<PwEntry> entries)
{
StatusProgressForm progressForm = new StatusProgressForm();
progressForm.InitEx("Downloading Favicons", true, false, m_host.MainWindow);
progressForm.Show();
progressForm.SetProgress(0);
float progress = 0;
float outputLength = (float)entries.UCount;
int downloadsCompleted = 0;
string errorMessage = "";
int errorCount = 0;
foreach (PwEntry pwe in entries)
{
string message = "";
progressForm.SetText("Title: " + pwe.Strings.ReadSafe("Title") + "; User Name: " + pwe.Strings.ReadSafe("UserName"),LogStatusType.Info);
downloadOneFavicon(pwe, ref message);
if (message != "")
{
errorMessage = "For an entry with URL '"+pwe.Strings.ReadSafe("URL")+"': " + message;
errorCount++;
}
downloadsCompleted++;
progress = (downloadsCompleted / outputLength) * 100;
progressForm.SetProgress((uint)Math.Floor(progress));
System.Threading.Thread.Sleep(100);
if (progressForm.UserCancelled)
break;
}
progressForm.Hide();
progressForm.Close();
if (errorMessage != "")
{
if (errorCount == 1)
MessageBox.Show(errorMessage, "Download error");
else
MessageBox.Show(errorCount + " errors occurred. The last error message is shown here. To see the other messages, select a smaller group of entries and use the right click menu to start the download.\n" + errorMessage, "Download errors");
}
m_host.MainWindow.UpdateUI(false, null, false, null,
true, null, true);
m_host.MainWindow.UpdateTrayIcon();
}
/// <summary>
/// Downloads one favicon and attaches it to the entry
/// </summary>
/// <param name="pwe">The entry for which we want to download the favicon</param>
private void downloadOneFavicon(PwEntry pwe, ref string message)
{
// TODO: create async jobs instead?
string url = pwe.Strings.ReadSafe("URL");
if (string.IsNullOrEmpty(url))
url = pwe.Strings.ReadSafe("Title");
// If we still have no URL, quit
if (string.IsNullOrEmpty(url))
return;
// If we have a URL with specific scheme that is not http or https, quit
if (!url.StartsWith("http://") && !url.StartsWith("https://")
&& url.Contains("://"))
return;
int dotIndex = url.IndexOf(".");
if (dotIndex >= 0)
{
Uri fullURI = null;
try {
fullURI = new Uri((url.StartsWith("http://")||url.StartsWith("https://"))?url:"http://"+url,UriKind.Absolute);
}
catch (Exception ex)
{
message += url + "\n" + ex.Message;
return;
}
MemoryStream ms = null;
Uri lastURI = getFromFaviconExplicitLocation(fullURI, ref ms, ref message);
bool success = (lastURI != null) && lastURI.OriginalString.Equals("http://success");
if (!success)
{
success = getFavicon(new Uri((lastURI==null)?fullURI:lastURI,"/favicon.ico"), ref ms, ref message);
}
if (!success)
return;
// If we found an icon then we don't care whether one particular download method failed.
message = "";
byte[] msByteArray = ms.ToArray();
foreach (PwCustomIcon item in m_host.Database.CustomIcons)
{
// re-use existing custom icon if it's already in the database
// (This will probably fail if database is used on
// both 32 bit and 64 bit machines - not sure why...)
if (KeePassLib.Utility.MemUtil.ArraysEqual(msByteArray, item.ImageDataPng))
{
pwe.CustomIconUuid = item.Uuid;
pwe.Touch(true);
m_host.Database.UINeedsIconUpdate = true;
return;
}
}
// Create a new custom icon for use with this entry
PwCustomIcon pwci = new PwCustomIcon(new PwUuid(true),
ms.ToArray());
m_host.Database.CustomIcons.Add(pwci);
pwe.CustomIconUuid = pwci.Uuid;
pwe.Touch(true);
m_host.Database.UINeedsIconUpdate = true;
}
}
private Uri getMetaRefreshLink(Uri uri, HtmlAgilityPack.HtmlDocument hdoc)
{
HtmlNodeCollection metas = hdoc.DocumentNode.SelectNodes("/html/head/meta");
string redirect = null;
if (metas == null)
{
return null;
}
for (int i = 0; i < metas.Count; i++)
{
HtmlNode node = metas[i];
try
{
HtmlAttribute httpeq = node.Attributes["http-equiv"];
HtmlAttribute content = node.Attributes["content"];
if (httpeq.Value.ToLower().Equals("location") || httpeq.Value.ToLower().Equals("refresh"))
{
if (content.Value.ToLower().Contains("url"))
{
Match match = Regex.Match(content.Value.ToLower(), @".*?url[\s=]*(\S+)");
if (match.Success)
{
redirect = match.Captures[0].ToString();
redirect = match.Groups[1].ToString();
}
}
}
}
catch (Exception) { }
}
if (String.IsNullOrEmpty(redirect))
{
return null;
}
return new Uri(uri, redirect);
}
bool PreRequest_EventHandler(HttpWebRequest request)
{
request.CookieContainer = new System.Net.CookieContainer();
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Headers.Add(HttpRequestHeader.AcceptLanguage, "*");
return true;
}
/// <summary>
/// Gets a memory stream representing an image from an explicit favicon location.
/// </summary>
/// <param name="fullURI">The URI.</param>
/// <param name="ms">The memory stream (output).</param>
/// <param name="message">Any error message is sent back through this string.</param>
/// <returns></returns>
private Uri getFromFaviconExplicitLocation(Uri fullURI, ref MemoryStream ms, ref string message)
{
HtmlWeb hw = new HtmlWeb();
hw.UserAgent = "Mozilla/5.0 (Windows 6.1; rv:27.0) Gecko/20100101 Firefox/27.0";
HtmlAgilityPack.HtmlDocument hdoc = null;
Uri responseURI = null;
try
{
int counter = 0; // Protection from cyclic redirect
Uri nextUri = fullURI;
do
{
// A cookie container is needed for some sites to work
hw.PreRequest += PreRequest_EventHandler;
// HtmlWeb.Load will follow 302 and 302 redirects to alternate URIs
hdoc = hw.Load(nextUri.AbsoluteUri);
responseURI = hw.ResponseUri;
// Old school meta refreshes need to parsed
nextUri = getMetaRefreshLink(responseURI, hdoc);
counter++;
} while (nextUri != null && counter < 16); // Sixteen redirects would be more than enough.
}
catch (Exception)
{
return responseURI;
}
if (hdoc == null)
return responseURI;
string faviconLocation = "";
try
{
HtmlNodeCollection links = hdoc.DocumentNode.SelectNodes("/html/head/link");
for (int i = 0; i < links.Count; i++)
{
HtmlNode node = links[i];
try
{
HtmlAttribute r = node.Attributes["rel"];
string val = r.Value.ToLower().Replace("shortcut","").Trim();
if (val == "icon")
{
try
{
faviconLocation = node.Attributes["href"].Value;
// Don't break the loop, because there could be many <link rel="icon"> nodes
// We should read the last one, like web browsers do
}
catch (Exception)
{
}
}
}
catch (Exception)
{
}
}
}
catch (Exception)
{
}
if (String.IsNullOrEmpty(faviconLocation))
{
return responseURI;
}
return (getFavicon(new Uri(responseURI, faviconLocation), ref ms, ref message))?new Uri("http://success"):responseURI;
}
/// <summary>
/// Gets a memory stream representing an image from a standard favicon location.
/// </summary>
/// <param name="uri">The URI.</param>
/// <param name="ms">The memory stream (output).</param>
/// <param name="message">Any error message is sent back through this string.</param>
/// <returns></returns>
private bool getFavicon(Uri uri, ref MemoryStream ms, ref string message)
{
Stream s = null;
Image img = null;
MemoryStream memStream = new MemoryStream();
try
{
WebRequest webreq = WebRequest.Create(uri);
((HttpWebRequest)webreq).UserAgent = "Mozilla/5.0 (Windows 6.1; rv:27.0) Gecko/20100101 Firefox/27.0";
((HttpWebRequest)webreq).CookieContainer = new System.Net.CookieContainer();
((HttpWebRequest)webreq).Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
((HttpWebRequest)webreq).Headers.Add(HttpRequestHeader.AcceptLanguage, "*");
webreq.Timeout = 10000; // don't think it's expecting too much for a few KB to be delivered inside 10 seconds.
WebResponse response = webreq.GetResponse();
if( response==null )
{
message += "Could not download favicon(s). This may be a temporary problem so you may want to try again later or post the contents of this error message on the KeePass Favicon Download forums at http://sourceforge.net/projects/keepass-favicon/support. Technical information which may help diagnose the problem is listed below, you can copy it to your clipboard by just clicking on this message and pressing CTRL-C.\n - No response from server";
return false;
}
if( uri != response.ResponseUri )
{
//Redirect ?
return getFavicon(response.ResponseUri, ref ms, ref message);
}
s = response.GetResponseStream();
int count = 0;
byte[] buffer = new byte[4097];
do
{
count = s.Read(buffer, 0, buffer.Length);
memStream.Write(buffer, 0, count);
if (count == 0)
break;
}
while (true);
memStream.Position = 0;
// END change
try
{
Icon icon = new Icon(memStream);
icon = new Icon(icon, 16, 16);
img = icon.ToBitmap();
}
catch (Exception)
{
// This shouldn't be useful unless someone has messed up their favicon format
try { img = Image.FromStream(memStream); }
catch (Exception) { throw; }
}
}
catch (WebException webException)
{
// don't show this everytime a website has a missing favicon - it could get old fast.
message += "Could not download favicon(s). This may be a temporary problem so you may want to try again later or post the contents of this error message on the KeePass Favicon Download forums at http://sourceforge.net/projects/keepass-favicon/support. Technical information which may help diagnose the problem is listed below, you can copy it to your clipboard by just clicking on this message and pressing CTRL-C.\n" + webException.Status + ": " + webException.Message + ": " + webException.Response;
if (s != null)
s.Close();
return false;
}
catch (Exception generalException)
{
// don't show this everytime a website has an invalid favicon - it could get old fast.
message += "Could not download favicon(s). This may be a temporary problem so you may want to try again later or post the contents of this error message on the KeePass Favicon Download forums at http://sourceforge.net/projects/keepass-favicon/support. Technical information which may help diagnose the problem is listed below, you can copy it to your clipboard by just clicking on this message and pressing CTRL-C.\n" + generalException.Message + ".";
if (s != null)
s.Close();
return false;
}
try
{
Bitmap imgNew = new Bitmap(16, 16);
imgNew.SetResolution(img.HorizontalResolution, img.VerticalResolution);
using (Graphics g = Graphics.FromImage(imgNew))
{
// set the resize quality modes to high quality
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawImage(img, 0, 0, imgNew.Width, imgNew.Height);
}
ms = new MemoryStream();
imgNew.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return true;
}
catch (Exception ex)
{
message += "Could not process downloaded favicon. This may be a temporary problem so you may want to try again later or post the contents of this error message on the KeePass Favicon Download forums at http://sourceforge.net/projects/keepass-favicon/support. Technical information which may help diagnose the problem is listed below, you can copy it to your clipboard by just clicking on this message and pressing CTRL-C.\n" + ex.Message + ".";
if (s != null)
s.Close();
return false;
}
}
}
}