2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* 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 "webview.h"
|
|
|
|
#include "mainapplication.h"
|
2011-03-03 15:54:12 +01:00
|
|
|
#include "autofillnotification.h"
|
2011-12-08 21:52:03 +01:00
|
|
|
#include "databasewriter.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-12-12 21:14:43 +01:00
|
|
|
AutoFillModel::AutoFillModel(QupZilla* mainClass, QObject* parent)
|
|
|
|
: QObject(parent)
|
2011-11-06 17:01:23 +01:00
|
|
|
, p_QupZilla(mainClass)
|
|
|
|
, m_isStoring(false)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
QTimer::singleShot(0, this, SLOT(loadSettings()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoFillModel::loadSettings()
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
QSettings settings(mApp->getActiveProfilPath() + "settings.ini", QSettings::IniFormat);
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AutoFillModel::isStored(const QUrl &url)
|
|
|
|
{
|
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
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QString server = url.host();
|
|
|
|
QSqlQuery query;
|
2011-11-06 17:01:23 +01:00
|
|
|
query.exec("SELECT count(id) FROM autofill WHERE server='" + server + "'");
|
2011-03-02 16:57:41 +01:00
|
|
|
query.next();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (query.value(0).toInt() > 0) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return true;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AutoFillModel::isStoringEnabled(const QUrl &url)
|
|
|
|
{
|
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();
|
|
|
|
QSqlQuery query;
|
2011-11-06 17:01:23 +01:00
|
|
|
query.exec("SELECT count(id) FROM autofill_exceptions WHERE server='" + server + "'");
|
2011-03-02 16:57:41 +01:00
|
|
|
query.next();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (query.value(0).toInt() > 0) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void AutoFillModel::blockStoringfor(const QUrl &url)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
QString server = url.host();
|
|
|
|
QSqlQuery query;
|
2011-12-08 21:52:03 +01:00
|
|
|
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();
|
|
|
|
QSqlQuery query;
|
2011-11-06 17:01:23 +01:00
|
|
|
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();
|
|
|
|
QSqlQuery query;
|
2011-11-06 17:01:23 +01:00
|
|
|
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
|
2011-12-08 21:52:03 +01:00
|
|
|
void AutoFillModel::addEntry(const QUrl &url, const QString &name, const QString &pass)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
QSqlQuery query;
|
2011-11-06 17:01:23 +01:00
|
|
|
query.exec("SELECT username FROM autofill WHERE server='" + url.host() + "'");
|
|
|
|
if (query.next()) {
|
2011-12-08 21:52:03 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
query.prepare("INSERT INTO autofill (server, username, password) VALUES (?,?,?)");
|
|
|
|
query.bindValue(0, url.host());
|
|
|
|
query.bindValue(1, name);
|
|
|
|
query.bindValue(2, pass);
|
2011-12-08 21:52:03 +01:00
|
|
|
mApp->dbWriter()->executeQuery(query);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///WEB Form
|
2011-12-08 21:52:03 +01:00
|
|
|
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;
|
2011-11-06 17:01:23 +01:00
|
|
|
query.exec("SELECT data FROM autofill WHERE server='" + url.host() + "'");
|
|
|
|
if (query.next()) {
|
2011-12-08 21:52:03 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-11-20 17:09:10 +01:00
|
|
|
query.prepare("INSERT INTO autofill (server, data, username, password) VALUES (?,?,?,?)");
|
2011-03-02 16:57:41 +01:00
|
|
|
query.bindValue(0, url.host());
|
|
|
|
query.bindValue(1, data);
|
2011-11-20 17:09:10 +01:00
|
|
|
query.bindValue(2, user);
|
|
|
|
query.bindValue(3, pass);
|
2011-12-08 21:52:03 +01:00
|
|
|
mApp->dbWriter()->executeQuery(query);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoFillModel::completePage(WebView* view)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!isStored(view->url())) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-05-05 16:27:48 +02:00
|
|
|
QWebElementCollection inputs;
|
|
|
|
QList<QWebFrame*> frames;
|
|
|
|
frames.append(view->page()->mainFrame());
|
|
|
|
while (!frames.isEmpty()) {
|
|
|
|
QWebFrame* frame = frames.takeFirst();
|
|
|
|
inputs.append(frame->findAllElements("input"));
|
|
|
|
frames += frame->childFrames();
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QSqlQuery query;
|
2011-11-06 17:01:23 +01:00
|
|
|
query.exec("SELECT data FROM autofill WHERE server='" + view->url().host() + "'");
|
2011-03-02 16:57:41 +01:00
|
|
|
query.next();
|
|
|
|
QByteArray data = query.value(0).toByteArray();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (data.isEmpty()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
QList<QPair<QString, QString> > arguments = QUrl::fromEncoded(QByteArray("http://bla.com/?" + data)).queryItems();
|
|
|
|
for (int i = 0; i < arguments.count(); i++) {
|
2011-12-04 16:54:57 +01:00
|
|
|
QString key = QUrl::fromEncoded(arguments.at(i).first.toUtf8()).toString();
|
|
|
|
QString value = QUrl::fromEncoded(arguments.at(i).second.toUtf8()).toString();
|
2011-05-05 16:27:48 +02:00
|
|
|
//key.replace("+"," ");
|
|
|
|
//value.replace("+"," ");
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
for (int i = 0; i < inputs.count(); i++) {
|
2011-03-02 16:57:41 +01:00
|
|
|
QWebElement element = inputs.at(i);
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (element.attribute("type") != "text" && element.attribute("type") != "password" && element.attribute("type") != "") {
|
2011-03-02 16:57:41 +01:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
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
|
2011-11-06 17:01:23 +01:00
|
|
|
if (mApp->webSettings()->testAttribute(QWebSettings::PrivateBrowsingEnabled)) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-11-05 15:04:29 +01:00
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
m_lastOutgoingData = outgoingData;
|
|
|
|
|
|
|
|
QVariant v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100));
|
|
|
|
QWebPage* webPage = (QWebPage*)(v.value<void*>());
|
|
|
|
v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 102));
|
|
|
|
WebView* webView = (WebView*)(v.value<void*>());
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!webPage || !webView) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 101));
|
|
|
|
QWebPage::NavigationType type = (QWebPage::NavigationType)v.toInt();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (type != QWebPage::NavigationTypeFormSubmitted) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-11-20 17:09:10 +01:00
|
|
|
QString usernameName;
|
|
|
|
QString usernameValue;
|
|
|
|
QString passwordName;
|
|
|
|
QString passwordValue;
|
2011-10-31 21:27:52 +01:00
|
|
|
QUrl siteUrl = webView->url();
|
2011-05-05 16:27:48 +02:00
|
|
|
|
2011-11-20 17:09:10 +01:00
|
|
|
if (!isStoringEnabled(siteUrl)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWebElementCollection allForms; // All form elements on page
|
|
|
|
QWebElement foundForm; // Sent form element
|
|
|
|
|
2011-05-05 16:27:48 +02:00
|
|
|
QList<QWebFrame*> frames;
|
2011-11-20 17:09:10 +01:00
|
|
|
frames.append(webPage->mainFrame()); // Find all form elements
|
2011-05-05 16:27:48 +02:00
|
|
|
while (!frames.isEmpty()) {
|
|
|
|
QWebFrame* frame = frames.takeFirst();
|
2011-11-20 17:09:10 +01:00
|
|
|
allForms.append(frame->findAllElements("form"));
|
2011-05-05 16:27:48 +02:00
|
|
|
frames += frame->childFrames();
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-11-20 17:09:10 +01:00
|
|
|
foreach(QWebElement formElement, allForms) {
|
|
|
|
foreach(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()) {
|
2011-05-05 16:27:48 +02:00
|
|
|
break;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-11-20 17:09:10 +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;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-05-05 16:27:48 +02:00
|
|
|
|
2011-11-20 17:09:10 +01:00
|
|
|
// 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(QWebElement element, foundForm.findAll("input[type=\"text\"]")) {
|
|
|
|
usernameName = element.attribute("name");
|
|
|
|
usernameValue = getValueFromData(outgoingData, element);
|
|
|
|
if (!usernameName.isEmpty() && !usernameValue.isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-11-20 17:09:10 +01:00
|
|
|
AutoFillNotification* aWidget = new AutoFillNotification(siteUrl, outgoingData, usernameValue, passwordValue);
|
2011-03-02 16:57:41 +01:00
|
|
|
webView->addNotification(aWidget);
|
|
|
|
}
|
2011-11-20 17:09:10 +01:00
|
|
|
|
|
|
|
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()) {
|
2011-11-26 16:54:44 +01:00
|
|
|
QueryItems queryItems = QUrl::fromEncoded("http://a.b/?" + data).queryItems();
|
2011-11-20 17:09:10 +01:00
|
|
|
for (int i = 0; i < queryItems.count(); i++) {
|
|
|
|
QueryItem item = queryItems.at(i);
|
|
|
|
|
|
|
|
if (item.first == name) {
|
2011-12-04 16:54:57 +01:00
|
|
|
value = item.second.toUtf8();
|
2011-11-20 17:09:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AutoFillModel::dataContains(const QByteArray &data, const QString &attributeName)
|
|
|
|
{
|
2011-11-26 16:54:44 +01:00
|
|
|
QueryItems queryItems = QUrl::fromEncoded("http://a.b/?" + data).queryItems();
|
2011-11-20 17:09:10 +01:00
|
|
|
for (int i = 0; i < queryItems.count(); i++) {
|
|
|
|
QueryItem item = queryItems.at(i);
|
|
|
|
|
|
|
|
if (item.first == attributeName) {
|
|
|
|
return !item.second.isEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|