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"
|
2012-01-21 20:27:45 +01:00
|
|
|
#include "webpage.h"
|
|
|
|
#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"
|
2011-12-08 21:52:03 +01:00
|
|
|
#include "databasewriter.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QXmlStreamWriter>
|
|
|
|
#include <QXmlStreamReader>
|
|
|
|
#include <QWebFrame>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
|
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
|
|
|
{
|
2012-01-21 20:27:45 +01:00
|
|
|
loadSettings();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoFillModel::loadSettings()
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
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-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();
|
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-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();
|
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);
|
|
|
|
mApp->dbWriter()->executeQuery(query);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString AutoFillModel::getUsername(const QUrl &url)
|
|
|
|
{
|
|
|
|
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-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();
|
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-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
|
|
|
}
|
2012-01-21 20:27:45 +01:00
|
|
|
|
|
|
|
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 (?,?,?)");
|
2012-01-21 20:27:45 +01:00
|
|
|
query.bindValue(0, server);
|
2011-03-02 16:57:41 +01:00
|
|
|
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
|
|
|
|
2012-01-21 20:27:45 +01:00
|
|
|
QString server = url.host();
|
|
|
|
if (server.isEmpty()) {
|
|
|
|
server = url.toString();
|
|
|
|
}
|
|
|
|
|
2011-11-20 17:09:10 +01:00
|
|
|
query.prepare("INSERT INTO autofill (server, data, username, password) VALUES (?,?,?,?)");
|
2012-01-21 20:27:45 +01:00
|
|
|
query.bindValue(0, server);
|
2011-03-02 16:57:41 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-01-21 20:27:45 +01:00
|
|
|
void AutoFillModel::completePage(WebPage* page)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-01-21 20:27:45 +01:00
|
|
|
if (!page) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl pageUrl = page->url();
|
|
|
|
if (!isStored(pageUrl)) {
|
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;
|
2012-01-21 20:27:45 +01:00
|
|
|
frames.append(page->mainFrame());
|
2011-05-05 16:27:48 +02:00
|
|
|
while (!frames.isEmpty()) {
|
|
|
|
QWebFrame* frame = frames.takeFirst();
|
|
|
|
inputs.append(frame->findAllElements("input"));
|
|
|
|
frames += frame->childFrames();
|
|
|
|
}
|
|
|
|
|
2012-01-21 20:27:45 +01:00
|
|
|
QString server = pageUrl.host();
|
|
|
|
if (server.isEmpty()) {
|
|
|
|
server = pageUrl.toString();
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QSqlQuery query;
|
2012-01-21 20:27:45 +01:00
|
|
|
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();
|
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
|
|
|
|
2012-01-31 21:10:22 +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();
|
2011-11-06 17:01:23 +01:00
|
|
|
for (int i = 0; i < arguments.count(); i++) {
|
2012-01-31 21:10:22 +01:00
|
|
|
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
|
|
|
|
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
|
|
|
}
|
2012-02-17 18:55:34 +01:00
|
|
|
|
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
|
|
|
|
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));
|
2012-01-21 20:27:45 +01:00
|
|
|
WebPage* webPage = static_cast<WebPage*>(v.value<void*>());
|
2012-04-02 19:33:05 +02:00
|
|
|
if (!WebPage::isPointerSafeToUse(webPage)) {
|
2012-01-21 20:27:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
WebView* webView = qobject_cast<WebView*>(webPage->view());
|
|
|
|
if (!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;
|
2012-01-21 20:27:45 +01:00
|
|
|
QUrl siteUrl = webPage->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
|
|
|
|
2012-01-24 19:12:31 +01:00
|
|
|
foreach(const QWebElement & formElement, allForms) {
|
2012-06-15 17:43:19 +02:00
|
|
|
foreach(const QWebElement & inputElement, formElement.findAll("input[type=\"password\"]")) {
|
2011-11-20 17:09:10 +01:00
|
|
|
passwordName = inputElement.attribute("name");
|
2012-04-17 17:54:51 +02:00
|
|
|
passwordValue = getValueFromData(data, inputElement);
|
2011-11-20 17:09:10 +01:00
|
|
|
|
2012-04-17 17:54:51 +02:00
|
|
|
if (!passwordValue.isEmpty() && dataContains(data, passwordName)) {
|
2011-11-20 17:09:10 +01:00
|
|
|
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" ?
|
|
|
|
|
2012-06-15 17:43:19 +02:00
|
|
|
foreach(const QWebElement & element, foundForm.findAll("input[type=\"text\"]")) {
|
2011-11-20 17:09:10 +01:00
|
|
|
usernameName = element.attribute("name");
|
2012-04-17 17:54:51 +02:00
|
|
|
usernameValue = getValueFromData(data, element);
|
2011-11-20 17:09:10 +01:00
|
|
|
if (!usernameName.isEmpty() && !usernameValue.isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-04-17 17:54:51 +02:00
|
|
|
AutoFillNotification* aWidget = new AutoFillNotification(siteUrl, data, 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()) {
|
2012-04-17 17:54:51 +02:00
|
|
|
return QString();
|
2011-11-20 17:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-04-17 18:26:01 +02:00
|
|
|
QByteArray AutoFillModel::convertWebKitFormBoundaryIfNecessary(const QByteArray &data)
|
2012-04-17 17:54:51 +02:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2011-11-20 17:09:10 +01:00
|
|
|
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();
|
2012-04-17 17:54:51 +02:00
|
|
|
|
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;
|
|
|
|
}
|
2012-02-06 20:10:50 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
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()) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|