123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #include "intrabocal.h"
- #include <QtWebKit>
-
- IntraBocal::IntraBocal(QObject *parent) : QObject(parent)
- {
- m_mgr = new QNetworkAccessManager(this);
- }
-
- QList<City> IntraBocal::getCities()
- {
- QList<QPair<QString, QString> > cities;
-
- cities.append(City("", ""));
- cities.append(City("bdx", "Bordeaux"));
- cities.append(City("lil", "Lille"));
- cities.append(City("lyo", "Lyon"));
- cities.append(City("msl", "Marseille"));
- cities.append(City("mpl", "Montpellier"));
- cities.append(City("ncy", "Nancy"));
- cities.append(City("nts", "Nantes"));
- cities.append(City("nce", "Nice"));
- cities.append(City("prs", "Paris"));
- cities.append(City("rns", "Rennes"));
- cities.append(City("stg", "Strasbourg"));
- cities.append(City("tls", "Toulouse"));
-
- return cities;
- }
-
- void IntraBocal::login(QString user, QString pass)
- {
- if(!m_phpSessId.name().isEmpty())
- {
- emit logged();
- return;
- }
- QNetworkRequest req(QUrl("https://intra-bocal.epitech.eu/index.php"));
- QHttpMultiPart* data = new QHttpMultiPart(QHttpMultiPart::FormDataType, this);
- addPart(data, "logout", "");
- addPart(data, "login", user);
- addPart(data, "pass", pass);
- addPart(data, "submit", "Envoyer");
- req.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=" + QString(data->boundary()));
- QNetworkReply* reply = m_mgr->post(req, data);
- connect(reply, SIGNAL(destroyed()), data, SLOT(deleteLater()));
- connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(loginError(QNetworkReply::NetworkError)));
- connect(reply, SIGNAL(finished()), this, SLOT(loginFinished()));
- connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(loginSslError(QList<QSslError>)));
- }
-
- void IntraBocal::searchUser(QString lastName, QString firstName, QString login, QString promo, QString school, QString city)
- {
- QNetworkRequest req(QUrl("https://intra-bocal.epitech.eu/index.php"));
- QUrl data;
- data.addQueryItem("pgid", "userinfo");
- data.addQueryItem("m_userinfo_user_name", lastName);
- data.addQueryItem("m_userinfo_user_firstname", firstName);
- data.addQueryItem("m_userinfo_user_login", login);
- data.addQueryItem("m_userinfo_user_prom", promo);
- data.addQueryItem("m_userinfo_user_school", school);
- data.addQueryItem("m_userinfo_user_town", city);
- data.addQueryItem("m_userinfo_submit", "Rechercher");
-
- req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
- QVariant cookies;
- cookies.setValue(QList<QNetworkCookie>() << m_phpSessId);
- req.setHeader(QNetworkRequest::CookieHeader, cookies);
-
- QNetworkReply* reply = m_mgr->post(req, data.toString().remove(0, 1).toStdString().c_str());
- connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(searchError(QNetworkReply::NetworkError)));
- connect(reply, SIGNAL(finished()), this, SLOT(searchFinished()));
- connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(searchSslError(QList<QSslError>)));
- }
-
- void IntraBocal::loginError(QNetworkReply::NetworkError e)
- {
- emit error(NetworkError);
- }
-
- void IntraBocal::loginFinished()
- {
- QNetworkReply* rep = qobject_cast<QNetworkReply*>(sender());
- if(rep == 0)
- return;
-
- QList<QNetworkCookie> cookies = qvariant_cast<QList<QNetworkCookie> >(rep->header(QNetworkRequest::SetCookieHeader));
- if(cookies.size() == 3)
- {
- for(int i = 0; i < cookies.size(); ++i)
- if(cookies.at(i).name() == "PHPSESSID")
- {
- m_phpSessId = cookies.at(i);
- emit logged();
- return;
- }
- }
- else if(!m_phpSessId.name().isEmpty() && cookies.size() == 2)
- emit logged();
- else
- emit error(BadLogin);
- }
-
- void IntraBocal::loginSslError(QList<QSslError>)
- {
- emit error(SslError);
- }
-
- void IntraBocal::searchError(QNetworkReply::NetworkError)
- {
- emit error(NetworkError);
- }
-
- void IntraBocal::searchFinished()
- {
- QNetworkReply* rep = qobject_cast<QNetworkReply*>(sender());
- if(rep == 0)
- return;
-
- QString all = rep->readAll();
-
- QWebView view;
- view.hide();
- view.setHtml(all);
- QWebElement e = view.page()->mainFrame()->documentElement();
- QWebElement nf = e.findFirst("div#main-copy div[id=\"error\"]");
- if(!nf.isNull())
- {
- emit searchFinished(QList<QList<QString> >());
- return;
- }
- e = e.findFirst("div#main-copy table[class=\"list_user\"] tbody");
- if(e.isNull())
- emit error(ParseError);
- else
- {
- e = e.firstChild();
- QList<QList<QString> > users;
- while(!e.isNull())
- {
- QList<QString> user;
- QWebElement c = e.firstChild();
- while(!c.isNull())
- {
- user.append(c.toPlainText());
- c = c.nextSibling();
- }
- users.append(user);
- e = e.nextSibling();
- }
- emit searchFinished(users);
- }
- }
-
- void IntraBocal::searchSslError(QList<QSslError>)
- {
- emit error(SslError);
- }
-
- void IntraBocal::addPart(QHttpMultiPart *data, QString name, QString body)
- {
- QHttpPart part;
- part.setBody(QByteArray(body.toStdString().c_str()));
- part.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"" + name + "\"");
- data->append(part);
- }
|