2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2013-01-24 17:47:50 +01:00
|
|
|
#include "autofill.h"
|
2014-02-19 22:07:21 +01:00
|
|
|
#include "browserwindow.h"
|
2012-01-21 20:27:45 +01:00
|
|
|
#include "webpage.h"
|
2014-03-15 01:14:06 +01:00
|
|
|
#include "sqldatabase.h"
|
2012-01-21 20:27:45 +01:00
|
|
|
#include "tabbedwebview.h"
|
|
|
|
#include "popupwebview.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "mainapplication.h"
|
2011-03-03 15:54:12 +01:00
|
|
|
#include "autofillnotification.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2013-05-14 17:25:55 +02:00
|
|
|
#include "passwordmanager.h"
|
2014-09-29 16:03:22 +02:00
|
|
|
#include "qztools.h"
|
2015-08-31 18:56:52 +02:00
|
|
|
#include "scripts.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QXmlStreamWriter>
|
|
|
|
#include <QXmlStreamReader>
|
|
|
|
#include <QNetworkRequest>
|
2015-08-31 18:56:52 +02:00
|
|
|
#include <QWebEngineProfile>
|
|
|
|
#include <QWebEngineScriptCollection>
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2012-12-20 14:45:35 +01:00
|
|
|
#include <QUrlQuery>
|
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
AutoFill::AutoFill(QObject* parent)
|
2011-12-12 21:14:43 +01:00
|
|
|
: QObject(parent)
|
2013-05-14 17:25:55 +02:00
|
|
|
, m_manager(new PasswordManager(this))
|
2011-11-06 17:01:23 +01:00
|
|
|
, m_isStoring(false)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-01-21 20:27:45 +01:00
|
|
|
loadSettings();
|
2015-08-31 18:56:52 +02:00
|
|
|
|
|
|
|
// Setup AutoFill userscript
|
|
|
|
QWebEngineScript script;
|
|
|
|
script.setName(QSL("_qupzilla_autofill"));
|
|
|
|
script.setInjectionPoint(QWebEngineScript::DocumentReady);
|
|
|
|
script.setWorldId(QWebEngineScript::MainWorld);
|
|
|
|
script.setRunsOnSubFrames(true);
|
|
|
|
script.setSourceCode(Scripts::setupFormObserver());
|
|
|
|
mApp->webProfile()->scripts()->insert(script);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2013-05-15 20:05:45 +02:00
|
|
|
PasswordManager* AutoFill::passwordManager() const
|
|
|
|
{
|
|
|
|
return m_manager;
|
|
|
|
}
|
|
|
|
|
2013-01-24 17:47:50 +01:00
|
|
|
void AutoFill::loadSettings()
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-03-02 16:57:41 +01:00
|
|
|
settings.beginGroup("Web-Browser-Settings");
|
2011-11-02 12:52:55 +01:00
|
|
|
m_isStoring = settings.value("SavePasswordsOnSites", true).toBool();
|
2011-03-02 16:57:41 +01:00
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
2013-01-24 17:47:50 +01:00
|
|
|
bool AutoFill::isStored(const QUrl &url)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!isStoringEnabled(url)) {
|
2011-11-02 12:52:55 +01:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-11-02 12:52:55 +01:00
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
return !m_manager->getEntries(url).isEmpty();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 17:47:50 +01:00
|
|
|
bool AutoFill::isStoringEnabled(const QUrl &url)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!m_isStoring) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-11-02 12:52:55 +01:00
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QString server = url.host();
|
2012-01-21 20:27:45 +01:00
|
|
|
if (server.isEmpty()) {
|
|
|
|
server = url.toString();
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QSqlQuery query;
|
2012-08-23 15:37:43 +02:00
|
|
|
query.prepare("SELECT count(id) FROM autofill_exceptions WHERE server=?");
|
|
|
|
query.addBindValue(server);
|
|
|
|
query.exec();
|
2014-02-01 19:21:49 +01:00
|
|
|
|
|
|
|
if (!query.next()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-14 17:25:55 +02:00
|
|
|
|
|
|
|
return query.value(0).toInt() <= 0;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 13:02:53 +01:00
|
|
|
void AutoFill::blockStoringforUrl(const QUrl &url)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
QString server = url.host();
|
2012-01-21 20:27:45 +01:00
|
|
|
if (server.isEmpty()) {
|
|
|
|
server = url.toString();
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QSqlQuery query;
|
2011-12-08 21:52:03 +01:00
|
|
|
query.prepare("INSERT INTO autofill_exceptions (server) VALUES (?)");
|
|
|
|
query.addBindValue(server);
|
2014-03-07 18:03:42 +01:00
|
|
|
|
2014-03-15 01:14:06 +01:00
|
|
|
SqlDatabase::instance()->execAsync(query);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
QVector<PasswordEntry> AutoFill::getFormData(const QUrl &url)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2013-05-14 17:25:55 +02:00
|
|
|
return m_manager->getEntries(url);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2013-05-14 22:45:20 +02:00
|
|
|
QVector<PasswordEntry> AutoFill::getAllFormData()
|
|
|
|
{
|
|
|
|
return m_manager->getAllEntries();
|
|
|
|
}
|
|
|
|
|
2013-05-20 11:05:54 +02:00
|
|
|
void AutoFill::updateLastUsed(PasswordEntry &data)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2013-05-14 17:25:55 +02:00
|
|
|
m_manager->updateLastUsed(data);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
// HTTP Authorization
|
2013-01-24 17:47:50 +01:00
|
|
|
void AutoFill::addEntry(const QUrl &url, const QString &name, const QString &pass)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2013-05-14 17:25:55 +02:00
|
|
|
PasswordEntry entry;
|
2013-05-14 22:45:20 +02:00
|
|
|
entry.host = PasswordManager::createHost(url);
|
2013-05-14 17:25:55 +02:00
|
|
|
entry.username = name;
|
|
|
|
entry.password = pass;
|
2012-08-23 15:37:43 +02:00
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
m_manager->addEntry(entry);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
// WEB Form
|
2013-01-24 17:47:50 +01:00
|
|
|
void AutoFill::addEntry(const QUrl &url, const PageFormData &formData)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2013-05-14 17:25:55 +02:00
|
|
|
PasswordEntry entry;
|
2013-05-14 22:45:20 +02:00
|
|
|
entry.host = PasswordManager::createHost(url);
|
2013-05-14 17:25:55 +02:00
|
|
|
entry.username = formData.username;
|
|
|
|
entry.password = formData.password;
|
2013-05-15 20:05:45 +02:00
|
|
|
entry.data = formData.postData;
|
2013-02-04 12:11:27 +01:00
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
m_manager->addEntry(entry);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
// HTTP Authorization
|
2013-02-04 12:11:27 +01:00
|
|
|
void AutoFill::updateEntry(const QUrl &url, const QString &name, const QString &pass)
|
2013-01-31 23:11:57 +01:00
|
|
|
{
|
2013-05-14 17:25:55 +02:00
|
|
|
PasswordEntry entry;
|
2013-05-14 22:45:20 +02:00
|
|
|
entry.host = PasswordManager::createHost(url);
|
2013-05-14 17:25:55 +02:00
|
|
|
entry.username = name;
|
|
|
|
entry.password = pass;
|
2013-02-04 12:11:27 +01:00
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
m_manager->updateEntry(entry);
|
2013-02-04 12:11:27 +01:00
|
|
|
}
|
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
// WEB Form
|
2013-06-08 13:14:00 +02:00
|
|
|
bool AutoFill::updateEntry(const PasswordEntry &entry)
|
2013-02-04 12:11:27 +01:00
|
|
|
{
|
2013-06-08 13:14:00 +02:00
|
|
|
return m_manager->updateEntry(entry);
|
2013-01-31 23:11:57 +01:00
|
|
|
}
|
|
|
|
|
2013-05-14 22:45:20 +02:00
|
|
|
void AutoFill::removeEntry(const PasswordEntry &entry)
|
|
|
|
{
|
|
|
|
m_manager->removeEntry(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoFill::removeAllEntries()
|
|
|
|
{
|
|
|
|
m_manager->removeAllEntries();
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:00:27 +02:00
|
|
|
void AutoFill::saveForm(WebPage *page, const QUrl &frameUrl, const PageFormData &formData)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-07-10 22:15:07 +02:00
|
|
|
// Don't save in private browsing
|
2015-08-31 18:56:52 +02:00
|
|
|
if (mApp->isPrivate() || !page)
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-05 15:04:29 +01:00
|
|
|
|
2015-08-31 18:56:52 +02:00
|
|
|
if (!isStoringEnabled(frameUrl))
|
2014-09-19 13:16:02 +02:00
|
|
|
return;
|
2011-11-20 17:09:10 +01:00
|
|
|
|
2013-05-20 11:05:54 +02:00
|
|
|
PasswordEntry updateData;
|
2013-02-08 15:07:56 +01:00
|
|
|
|
2014-09-29 16:03:22 +02:00
|
|
|
if (isStored(frameUrl)) {
|
|
|
|
const QVector<PasswordEntry> &list = getFormData(frameUrl);
|
2013-01-31 23:11:57 +01:00
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
foreach (const PasswordEntry &data, list) {
|
2013-02-08 15:07:56 +01:00
|
|
|
if (data.username == formData.username) {
|
2013-05-20 11:05:54 +02:00
|
|
|
updateData = data;
|
|
|
|
updateLastUsed(updateData);
|
2013-01-31 23:11:57 +01:00
|
|
|
|
2013-02-08 15:07:56 +01:00
|
|
|
if (data.password == formData.password) {
|
2013-05-20 11:05:54 +02:00
|
|
|
updateData.password.clear();
|
2013-02-08 15:07:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
updateData.username = formData.username;
|
|
|
|
updateData.password = formData.password;
|
|
|
|
updateData.data = formData.postData;
|
2013-02-08 15:07:56 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-01-31 23:11:57 +01:00
|
|
|
}
|
|
|
|
|
2014-09-29 16:03:22 +02:00
|
|
|
AutoFillNotification* aWidget = new AutoFillNotification(frameUrl, formData, updateData);
|
2015-09-24 23:00:27 +02:00
|
|
|
page->view()->addNotification(aWidget);
|
2015-08-31 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns all saved passwords on this page
|
|
|
|
QVector<PasswordEntry> AutoFill::completePage(QWebEnginePage *page, const QUrl &frameUrl)
|
|
|
|
{
|
|
|
|
QVector<PasswordEntry> list;
|
|
|
|
|
|
|
|
if (!page || !isStored(frameUrl))
|
|
|
|
return list;
|
|
|
|
|
|
|
|
list = getFormData(frameUrl);
|
|
|
|
|
|
|
|
if (!list.isEmpty()) {
|
|
|
|
const PasswordEntry entry = list.first();
|
|
|
|
page->runJavaScript(Scripts::completeFormData(entry.data));
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2011-11-20 17:09:10 +01:00
|
|
|
|
2013-01-24 17:47:50 +01:00
|
|
|
QByteArray AutoFill::exportPasswords()
|
2012-02-06 20:10:50 +01:00
|
|
|
{
|
|
|
|
QByteArray output;
|
|
|
|
|
|
|
|
QXmlStreamWriter stream(&output);
|
|
|
|
stream.setCodec("UTF-8");
|
|
|
|
stream.setAutoFormatting(true);
|
|
|
|
|
|
|
|
stream.writeStartDocument();
|
|
|
|
stream.writeStartElement("passwords");
|
|
|
|
stream.writeAttribute("version", "1.0");
|
|
|
|
|
2013-05-22 15:43:44 +02:00
|
|
|
QVector<PasswordEntry> entries = m_manager->getAllEntries();
|
|
|
|
|
|
|
|
foreach (const PasswordEntry &entry, entries) {
|
2012-02-06 20:10:50 +01:00
|
|
|
stream.writeStartElement("entry");
|
2013-05-22 15:43:44 +02:00
|
|
|
stream.writeTextElement("server", entry.host);
|
|
|
|
stream.writeTextElement("username", entry.username);
|
|
|
|
stream.writeTextElement("password", entry.password);
|
|
|
|
stream.writeTextElement("data", entry.data);
|
2012-02-06 20:10:50 +01:00
|
|
|
stream.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2013-05-22 15:43:44 +02:00
|
|
|
QSqlQuery query;
|
2012-02-06 20:10:50 +01:00
|
|
|
query.exec("SELECT server FROM autofill_exceptions");
|
|
|
|
while (query.next()) {
|
|
|
|
stream.writeStartElement("exception");
|
|
|
|
stream.writeTextElement("server", query.value(0).toString());
|
|
|
|
stream.writeEndElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.writeEndElement();
|
|
|
|
stream.writeEndDocument();
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2013-01-24 17:47:50 +01:00
|
|
|
bool AutoFill::importPasswords(const QByteArray &data)
|
2012-02-06 20:10:50 +01:00
|
|
|
{
|
2012-03-23 14:23:54 +01:00
|
|
|
QSqlDatabase db = QSqlDatabase::database();
|
|
|
|
db.transaction();
|
|
|
|
|
2012-02-06 20:10:50 +01:00
|
|
|
QXmlStreamReader xml(data);
|
|
|
|
|
|
|
|
while (!xml.atEnd()) {
|
|
|
|
xml.readNext();
|
|
|
|
|
|
|
|
if (xml.isStartElement()) {
|
2012-09-04 12:42:45 +02:00
|
|
|
if (xml.name() == QLatin1String("entry")) {
|
2013-05-22 15:43:44 +02:00
|
|
|
PasswordEntry entry;
|
2012-02-06 20:10:50 +01:00
|
|
|
|
|
|
|
while (xml.readNext()) {
|
2012-09-04 12:42:45 +02:00
|
|
|
if (xml.name() == QLatin1String("server")) {
|
2013-05-22 15:43:44 +02:00
|
|
|
entry.host = xml.readElementText();
|
2012-02-06 20:10:50 +01:00
|
|
|
}
|
2012-09-04 12:42:45 +02:00
|
|
|
else if (xml.name() == QLatin1String("username")) {
|
2013-05-22 15:43:44 +02:00
|
|
|
entry.username = xml.readElementText();
|
2012-02-06 20:10:50 +01:00
|
|
|
}
|
2012-09-04 12:42:45 +02:00
|
|
|
else if (xml.name() == QLatin1String("password")) {
|
2013-05-22 15:43:44 +02:00
|
|
|
entry.password = xml.readElementText();
|
2012-02-06 20:10:50 +01:00
|
|
|
}
|
2012-09-04 12:42:45 +02:00
|
|
|
else if (xml.name() == QLatin1String("data")) {
|
2013-05-22 15:43:44 +02:00
|
|
|
entry.data = xml.readElementText().toUtf8();
|
2012-02-06 20:10:50 +01:00
|
|
|
}
|
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
if (xml.isEndElement() && xml.name() == QLatin1String("entry")) {
|
2012-02-06 20:10:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-22 15:43:44 +02:00
|
|
|
if (entry.isValid()) {
|
|
|
|
bool containsEntry = false;
|
2012-02-06 20:10:50 +01:00
|
|
|
|
2013-05-22 15:43:44 +02:00
|
|
|
foreach (const PasswordEntry &e, m_manager->getEntries(QUrl(entry.host))) {
|
|
|
|
if (e.username == entry.username) {
|
|
|
|
containsEntry = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!containsEntry) {
|
|
|
|
m_manager->addEntry(entry);
|
2012-02-06 20:10:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 12:42:45 +02:00
|
|
|
else if (xml.name() == QLatin1String("exception")) {
|
2012-02-06 20:10:50 +01:00
|
|
|
QString server;
|
|
|
|
|
|
|
|
while (xml.readNext()) {
|
2012-09-04 12:42:45 +02:00
|
|
|
if (xml.name() == QLatin1String("server")) {
|
2012-02-06 20:10:50 +01:00
|
|
|
server = xml.readElementText();
|
|
|
|
}
|
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
if (xml.isEndElement() && xml.name() == QLatin1String("exception")) {
|
2012-02-06 20:10:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!server.isEmpty()) {
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT id FROM autofill_exceptions WHERE server=?");
|
|
|
|
query.addBindValue(server);
|
|
|
|
query.exec();
|
|
|
|
|
|
|
|
if (!query.next()) {
|
|
|
|
query.prepare("INSERT INTO autofill_exceptions (server) VALUES (?)");
|
|
|
|
query.addBindValue(server);
|
|
|
|
query.exec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 15:43:44 +02:00
|
|
|
|
2012-03-23 14:23:54 +01:00
|
|
|
db.commit();
|
2012-02-06 20:10:50 +01:00
|
|
|
|
2012-03-23 14:23:54 +01:00
|
|
|
return !xml.hasError();
|
2012-02-06 20:10:50 +01:00
|
|
|
}
|