Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "intrabocal.h"
  2. #include <QtWebKit>
  3. IntraBocal::IntraBocal(QObject *parent) : QObject(parent)
  4. {
  5. m_mgr = new QNetworkAccessManager(this);
  6. }
  7. QList<City> IntraBocal::getCities()
  8. {
  9. QList<QPair<QString, QString> > cities;
  10. cities.append(City("", ""));
  11. cities.append(City("bdx", "Bordeaux"));
  12. cities.append(City("lil", "Lille"));
  13. cities.append(City("lyo", "Lyon"));
  14. cities.append(City("msl", "Marseille"));
  15. cities.append(City("mpl", "Montpellier"));
  16. cities.append(City("ncy", "Nancy"));
  17. cities.append(City("nts", "Nantes"));
  18. cities.append(City("nce", "Nice"));
  19. cities.append(City("prs", "Paris"));
  20. cities.append(City("rns", "Rennes"));
  21. cities.append(City("stg", "Strasbourg"));
  22. cities.append(City("tls", "Toulouse"));
  23. return cities;
  24. }
  25. void IntraBocal::login(QString user, QString pass)
  26. {
  27. if(!m_phpSessId.name().isEmpty())
  28. {
  29. emit logged();
  30. return;
  31. }
  32. QNetworkRequest req(QUrl("https://intra-bocal.epitech.eu/index.php"));
  33. QHttpMultiPart* data = new QHttpMultiPart(QHttpMultiPart::FormDataType, this);
  34. addPart(data, "logout", "");
  35. addPart(data, "login", user);
  36. addPart(data, "pass", pass);
  37. addPart(data, "submit", "Envoyer");
  38. req.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=" + QString(data->boundary()));
  39. QNetworkReply* reply = m_mgr->post(req, data);
  40. connect(reply, SIGNAL(destroyed()), data, SLOT(deleteLater()));
  41. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(loginError(QNetworkReply::NetworkError)));
  42. connect(reply, SIGNAL(finished()), this, SLOT(loginFinished()));
  43. connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(loginSslError(QList<QSslError>)));
  44. }
  45. void IntraBocal::searchUser(QString lastName, QString firstName, QString login, QString promo, QString school, QString city)
  46. {
  47. QNetworkRequest req(QUrl("https://intra-bocal.epitech.eu/index.php"));
  48. QUrl data;
  49. data.addQueryItem("pgid", "userinfo");
  50. data.addQueryItem("m_userinfo_user_name", lastName);
  51. data.addQueryItem("m_userinfo_user_firstname", firstName);
  52. data.addQueryItem("m_userinfo_user_login", login);
  53. data.addQueryItem("m_userinfo_user_prom", promo);
  54. data.addQueryItem("m_userinfo_user_school", school);
  55. data.addQueryItem("m_userinfo_user_town", city);
  56. data.addQueryItem("m_userinfo_submit", "Rechercher");
  57. req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
  58. QVariant cookies;
  59. cookies.setValue(QList<QNetworkCookie>() << m_phpSessId);
  60. req.setHeader(QNetworkRequest::CookieHeader, cookies);
  61. QNetworkReply* reply = m_mgr->post(req, data.toString().remove(0, 1).toStdString().c_str());
  62. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(searchError(QNetworkReply::NetworkError)));
  63. connect(reply, SIGNAL(finished()), this, SLOT(searchFinished()));
  64. connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(searchSslError(QList<QSslError>)));
  65. }
  66. void IntraBocal::loginError(QNetworkReply::NetworkError)
  67. {
  68. emit error(NetworkError);
  69. }
  70. void IntraBocal::loginFinished()
  71. {
  72. QNetworkReply* rep = qobject_cast<QNetworkReply*>(sender());
  73. if(rep == 0)
  74. return;
  75. QList<QNetworkCookie> cookies = qvariant_cast<QList<QNetworkCookie> >(rep->header(QNetworkRequest::SetCookieHeader));
  76. if(cookies.size() == 3)
  77. {
  78. for(int i = 0; i < cookies.size(); ++i)
  79. if(cookies.at(i).name() == "PHPSESSID")
  80. {
  81. m_phpSessId = cookies.at(i);
  82. emit logged();
  83. return;
  84. }
  85. }
  86. else if(!m_phpSessId.name().isEmpty() && cookies.size() == 2)
  87. emit logged();
  88. else
  89. emit error(BadLogin);
  90. }
  91. void IntraBocal::loginSslError(QList<QSslError>)
  92. {
  93. emit error(SslError);
  94. }
  95. void IntraBocal::searchError(QNetworkReply::NetworkError)
  96. {
  97. emit error(NetworkError);
  98. }
  99. void IntraBocal::searchFinished()
  100. {
  101. QNetworkReply* rep = qobject_cast<QNetworkReply*>(sender());
  102. if(rep == 0)
  103. return;
  104. QString all = rep->readAll();
  105. QWebView view;
  106. view.hide();
  107. view.setHtml(all);
  108. QWebElement e = view.page()->mainFrame()->documentElement();
  109. QWebElement nf = e.findFirst("div#main-copy div[id=\"error\"]");
  110. if(!nf.isNull())
  111. {
  112. emit searchFinished(QList<QList<QString> >());
  113. return;
  114. }
  115. e = e.findFirst("div#main-copy table[class=\"list_user\"] tbody");
  116. if(e.isNull())
  117. emit error(ParseError);
  118. else
  119. {
  120. e = e.firstChild();
  121. QList<QList<QString> > users;
  122. while(!e.isNull())
  123. {
  124. QList<QString> user;
  125. QWebElement c = e.firstChild();
  126. while(!c.isNull())
  127. {
  128. user.append(c.toPlainText());
  129. c = c.nextSibling();
  130. }
  131. users.append(user);
  132. e = e.nextSibling();
  133. }
  134. emit searchFinished(users);
  135. }
  136. }
  137. void IntraBocal::searchSslError(QList<QSslError>)
  138. {
  139. emit error(SslError);
  140. }
  141. void IntraBocal::addPart(QHttpMultiPart *data, QString name, QString body)
  142. {
  143. QHttpPart part;
  144. part.setBody(QByteArray(body.toStdString().c_str()));
  145. part.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"" + name + "\"");
  146. data->append(part);
  147. }