1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 17:42:10 +01:00
falkonOfficial/src/lib/autofill/autofillmodel.cpp

520 lines
15 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
#include <QXmlStreamWriter>
#include <QXmlStreamReader>
#include <QWebFrame>
#include <QNetworkRequest>
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;
}
2012-04-17 18:26:01 +02:00
const QByteArray &data = convertWebKitFormBoundaryIfNecessary(outgoingData);
2011-03-02 16:57:41 +01:00
QVariant v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100));
WebPage* webPage = static_cast<WebPage*>(v.value<void*>());
if (!WebPage::isPointerSafeToUse(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(data, inputElement);
if (!passwordValue.isEmpty() && dataContains(data, 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(data, element);
if (!usernameName.isEmpty() && !usernameValue.isEmpty()) {
break;
}
}
2011-03-02 16:57:41 +01:00
AutoFillNotification* aWidget = new AutoFillNotification(siteUrl, data, 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();
}
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;
}
2012-04-17 18:26:01 +02:00
QByteArray AutoFillModel::convertWebKitFormBoundaryIfNecessary(const QByteArray &data)
{
/* Sometimes, data are passed in this format:
------WebKitFormBoundary0bBp3bFMdGwqanMp
Content-Disposition: form-data; name="name-of-attribute"
value-of-attribute
------WebKitFormBoundary0bBp3bFMdGwqanMp--
So this function converts this format into url
*/
if (!data.contains(QByteArray("------WebKitFormBoundary"))) {
return data;
}
QByteArray formatedData;
QRegExp rx("name=\"(.*)------WebKitFormBoundary");
rx.setMinimal(true);
int pos = 0;
while ((pos = rx.indexIn(data, pos)) != -1) {
QString string = rx.cap(1);
pos += rx.matchedLength();
int endOfAttributeName = string.indexOf("\"");
if (endOfAttributeName == -1) {
continue;
}
QString attrName = string.left(endOfAttributeName);
QString attrValue = string.mid(endOfAttributeName + 1).trimmed().remove("\n");
if (attrName.isEmpty() || attrValue.isEmpty()) {
continue;
}
formatedData.append(attrName + "=" + attrValue + "&");
}
return formatedData;
}
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;
}
QByteArray AutoFillModel::exportPasswords()
{
QByteArray output;
QXmlStreamWriter stream(&output);
stream.setCodec("UTF-8");
stream.setAutoFormatting(true);
stream.writeStartDocument();
stream.writeStartElement("passwords");
stream.writeAttribute("version", "1.0");
QSqlQuery query;
query.exec("SELECT server, username, password, data FROM autofill");
while (query.next()) {
stream.writeStartElement("entry");
stream.writeTextElement("server", query.value(0).toString());
stream.writeTextElement("username", query.value(1).toString());
stream.writeTextElement("password", query.value(2).toString());
stream.writeTextElement("data", query.value(3).toString());
stream.writeEndElement();
}
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;
}
bool AutoFillModel::importPasswords(const QByteArray &data)
{
QSqlDatabase db = QSqlDatabase::database();
db.transaction();
QXmlStreamReader xml(data);
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == "entry") {
QString server;
QString username;
QString password;
QByteArray data;
while (xml.readNext()) {
if (xml.name() == "server") {
server = xml.readElementText();
}
else if (xml.name() == "username") {
username = xml.readElementText();
}
else if (xml.name() == "password") {
password = xml.readElementText();
}
else if (xml.name() == "data") {
data = xml.readElementText().toUtf8();
}
if (xml.isEndElement() && xml.name() == "entry") {
break;
}
}
if (!server.isEmpty() && !password.isEmpty() && !data.isEmpty()) {
QSqlQuery query;
query.prepare("SELECT id FROM autofill WHERE server=? AND password=? AND data=?");
query.addBindValue(server);
query.addBindValue(password);
query.addBindValue(data);
query.exec();
if (!query.next()) {
query.prepare("INSERT INTO autofill (server, username, password, data) VALUES (?,?,?,?)");
query.addBindValue(server);
query.addBindValue(username);
query.addBindValue(password);
query.addBindValue(data);
query.exec();
}
}
}
else if (xml.name() == "exception") {
QString server;
while (xml.readNext()) {
if (xml.name() == "server") {
server = xml.readElementText();
}
if (xml.isEndElement() && xml.name() == "exception") {
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();
}
}
}
}
}
db.commit();
return !xml.hasError();
}