1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-22 18:22:10 +02:00
falkonOfficial/src/autofill/autofillmodel.cpp

346 lines
9.9 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 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/>.
* ============================================================ */
2011-03-02 16:57:41 +01:00
#include "autofillmodel.h"
#include "qupzilla.h"
#include "webpage.h"
#include "tabbedwebview.h"
#include "popupwebview.h"
2011-03-02 16:57:41 +01:00
#include "mainapplication.h"
#include "autofillnotification.h"
#include "databasewriter.h"
#include "settings.h"
2011-03-02 16:57:41 +01:00
AutoFillModel::AutoFillModel(QupZilla* mainClass, QObject* parent)
: QObject(parent)
, p_QupZilla(mainClass)
, m_isStoring(false)
2011-03-02 16:57:41 +01:00
{
loadSettings();
2011-03-02 16:57:41 +01:00
}
void AutoFillModel::loadSettings()
{
Settings settings;
2011-03-02 16:57:41 +01:00
settings.beginGroup("Web-Browser-Settings");
m_isStoring = settings.value("SavePasswordsOnSites", true).toBool();
2011-03-02 16:57:41 +01:00
settings.endGroup();
}
bool AutoFillModel::isStored(const QUrl &url)
{
if (!isStoringEnabled(url)) {
return false;
}
2011-03-02 16:57:41 +01:00
QString server = url.host();
if (server.isEmpty()) {
server = url.toString();
}
2011-03-02 16:57:41 +01:00
QSqlQuery query;
query.exec("SELECT count(id) FROM autofill WHERE server='" + server + "'");
2011-03-02 16:57:41 +01:00
query.next();
if (query.value(0).toInt() > 0) {
2011-03-02 16:57:41 +01:00
return true;
}
2011-03-02 16:57:41 +01:00
return false;
}
bool AutoFillModel::isStoringEnabled(const QUrl &url)
{
if (!m_isStoring) {
2011-03-02 16:57:41 +01:00
return false;
}
2011-03-02 16:57:41 +01:00
QString server = url.host();
if (server.isEmpty()) {
server = url.toString();
}
2011-03-02 16:57:41 +01:00
QSqlQuery query;
query.exec("SELECT count(id) FROM autofill_exceptions WHERE server='" + server + "'");
2011-03-02 16:57:41 +01:00
query.next();
if (query.value(0).toInt() > 0) {
2011-03-02 16:57:41 +01:00
return false;
}
2011-03-02 16:57:41 +01:00
return true;
}
void AutoFillModel::blockStoringfor(const QUrl &url)
2011-03-02 16:57:41 +01:00
{
QString server = url.host();
if (server.isEmpty()) {
server = url.toString();
}
2011-03-02 16:57:41 +01:00
QSqlQuery query;
query.prepare("INSERT INTO autofill_exceptions (server) VALUES (?)");
query.addBindValue(server);
mApp->dbWriter()->executeQuery(query);
2011-03-02 16:57:41 +01:00
}
QString AutoFillModel::getUsername(const QUrl &url)
{
QString server = url.host();
if (server.isEmpty()) {
server = url.toString();
}
2011-03-02 16:57:41 +01:00
QSqlQuery query;
query.exec("SELECT username FROM autofill WHERE server='" + server + "'");
2011-03-02 16:57:41 +01:00
query.next();
return query.value(0).toString();
}
QString AutoFillModel::getPassword(const QUrl &url)
{
QString server = url.host();
if (server.isEmpty()) {
server = url.toString();
}
2011-03-02 16:57:41 +01:00
QSqlQuery query;
query.exec("SELECT password FROM autofill WHERE server='" + server + "'");
2011-03-02 16:57:41 +01:00
query.next();
return query.value(0).toString();
}
///HTTP Authorization
void AutoFillModel::addEntry(const QUrl &url, const QString &name, const QString &pass)
2011-03-02 16:57:41 +01:00
{
QSqlQuery query;
query.exec("SELECT username FROM autofill WHERE server='" + url.host() + "'");
if (query.next()) {
return;
}
QString server = url.host();
if (server.isEmpty()) {
server = url.toString();
}
2011-03-02 16:57:41 +01:00
query.prepare("INSERT INTO autofill (server, username, password) VALUES (?,?,?)");
query.bindValue(0, server);
2011-03-02 16:57:41 +01:00
query.bindValue(1, name);
query.bindValue(2, pass);
mApp->dbWriter()->executeQuery(query);
2011-03-02 16:57:41 +01:00
}
///WEB Form
void AutoFillModel::addEntry(const QUrl &url, const QByteArray &data, const QString &user, const QString &pass)
2011-03-02 16:57:41 +01:00
{
QSqlQuery query;
query.exec("SELECT data FROM autofill WHERE server='" + url.host() + "'");
if (query.next()) {
return;
}
2011-03-02 16:57:41 +01:00
QString server = url.host();
if (server.isEmpty()) {
server = url.toString();
}
query.prepare("INSERT INTO autofill (server, data, username, password) VALUES (?,?,?,?)");
query.bindValue(0, server);
2011-03-02 16:57:41 +01:00
query.bindValue(1, data);
query.bindValue(2, user);
query.bindValue(3, pass);
mApp->dbWriter()->executeQuery(query);
2011-03-02 16:57:41 +01:00
}
void AutoFillModel::completePage(WebPage* page)
2011-03-02 16:57:41 +01:00
{
if (!page) {
return;
}
QUrl pageUrl = page->url();
if (!isStored(pageUrl)) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
QWebElementCollection inputs;
QList<QWebFrame*> frames;
frames.append(page->mainFrame());
while (!frames.isEmpty()) {
QWebFrame* frame = frames.takeFirst();
inputs.append(frame->findAllElements("input"));
frames += frame->childFrames();
}
QString server = pageUrl.host();
if (server.isEmpty()) {
server = pageUrl.toString();
}
2011-03-02 16:57:41 +01:00
QSqlQuery query;
query.prepare("SELECT data FROM autofill WHERE server=?");
query.addBindValue(server);
query.exec();
2011-03-02 16:57:41 +01:00
query.next();
QByteArray data = query.value(0).toByteArray();
if (data.isEmpty()) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
// Why not to use encodedQueryItems = QByteArrays ?
// Because we need to filter "+" characters that must be spaces
// (not real "+" characters "%2B")
QueryItems arguments = QUrl::fromEncoded("http://bla.com/?" + data).queryItems();
for (int i = 0; i < arguments.count(); i++) {
QString key = arguments.at(i).first;
QString value = arguments.at(i).second;
key.replace("+", " ");
value.replace("+", " ");
key = QUrl::fromEncoded(key.toUtf8()).toString();
value = QUrl::fromEncoded(value.toUtf8()).toString();
2011-03-02 16:57:41 +01:00
for (int i = 0; i < inputs.count(); i++) {
2011-03-02 16:57:41 +01:00
QWebElement element = inputs.at(i);
if (element.attribute("type") != "text" && element.attribute("type") != "password" && element.attribute("type") != "") {
2011-03-02 16:57:41 +01:00
continue;
}
if (key == element.attribute("name")) {
element.setAttribute("value", value);
}
2011-03-02 16:57:41 +01:00
}
}
}
void AutoFillModel::post(const QNetworkRequest &request, const QByteArray &outgoingData)
{
//Dont save in private browsing
if (mApp->webSettings()->testAttribute(QWebSettings::PrivateBrowsingEnabled)) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
m_lastOutgoingData = outgoingData;
QVariant v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100));
WebPage* webPage = static_cast<WebPage*>(v.value<void*>());
if (!webPage) {
return;
}
WebView* webView = qobject_cast<WebView*>(webPage->view());
if (!webView) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 101));
QWebPage::NavigationType type = (QWebPage::NavigationType)v.toInt();
if (type != QWebPage::NavigationTypeFormSubmitted) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
QString usernameName;
QString usernameValue;
QString passwordName;
QString passwordValue;
QUrl siteUrl = webPage->url();
if (!isStoringEnabled(siteUrl)) {
return;
}
QWebElementCollection allForms; // All form elements on page
QWebElement foundForm; // Sent form element
QList<QWebFrame*> frames;
frames.append(webPage->mainFrame()); // Find all form elements
while (!frames.isEmpty()) {
QWebFrame* frame = frames.takeFirst();
allForms.append(frame->findAllElements("form"));
frames += frame->childFrames();
}
2011-03-02 16:57:41 +01:00
foreach(const QWebElement & formElement, allForms) {
foreach(const QWebElement &inputElement, formElement.findAll("input[type=\"password\"]")) {
passwordName = inputElement.attribute("name");
passwordValue = getValueFromData(outgoingData, inputElement);
if (!passwordValue.isEmpty() && dataContains(outgoingData, passwordName)) {
foundForm = formElement;
break;
}
}
if (!foundForm.isNull()) {
break;
}
2011-03-02 16:57:41 +01:00
}
// Return if data for this page is already stored or no password element found in sent data
if (foundForm.isNull() || isStored(siteUrl)) {
2011-03-02 16:57:41 +01:00
return;
}
// We need to find username, we suppose that username is first not empty input[type=text] in form
// Tell me better solution. Maybe first try to find name="user", name="username" ?
foreach(const QWebElement &element, foundForm.findAll("input[type=\"text\"]")) {
usernameName = element.attribute("name");
usernameValue = getValueFromData(outgoingData, element);
if (!usernameName.isEmpty() && !usernameValue.isEmpty()) {
break;
}
}
2011-03-02 16:57:41 +01:00
AutoFillNotification* aWidget = new AutoFillNotification(siteUrl, outgoingData, usernameValue, passwordValue);
2011-03-02 16:57:41 +01:00
webView->addNotification(aWidget);
}
QString AutoFillModel::getValueFromData(const QByteArray &data, QWebElement element)
{
QString name = element.attribute("name");
if (name.isEmpty()) {
return "";
}
QString value = element.evaluateJavaScript("this.value").toString();
if (value.isEmpty()) {
QueryItems queryItems = QUrl::fromEncoded("http://a.b/?" + data).queryItems();
for (int i = 0; i < queryItems.count(); i++) {
QueryItem item = queryItems.at(i);
if (item.first == name) {
value = item.second.toUtf8();
}
}
}
return value;
}
bool AutoFillModel::dataContains(const QByteArray &data, const QString &attributeName)
{
QueryItems queryItems = QUrl::fromEncoded("http://a.b/?" + data).queryItems();
for (int i = 0; i < queryItems.count(); i++) {
QueryItem item = queryItems.at(i);
if (item.first == attributeName) {
return !item.second.isEmpty();
}
}
return false;
}