11 #include <boost/filesystem.hpp>
19 #include <Wt/WRegExpValidator>
20 #include <Wt/WValidator>
24 #include <Wt/Dbo/WtSqlTraits>
27 #include <Wt/Dbo/Session>
28 #include <Wt/Dbo/backend/Sqlite3>
30 #include <Wt/Auth/HashFunction>
31 #include <Wt/Mail/Client>
32 #include <Wt/Mail/Message>
34 #include <gdcore/gdCore.h>
35 #include <gdcore/gdLdap.h>
48 : WContainerWidget(parent), m_pLineEdit(0), m_pButSubscribe(0), sqlite3_(0)
50 addStyleClass(
"WsNewsLetter");
62 WContainerWidget::load();
64 WText* pTitle =
new WText(asString(
option(
"title")).toUTF8());
65 pTitle->addStyleClass(
"WsTitle");
67 WText* pSubTitle =
new WText(asString(
option(
"subTitle")).toUTF8());
68 pSubTitle->addStyleClass(
"WsSubTitle");
70 WRegExpValidator* validator =
new WRegExpValidator(
"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}");
78 m_pButSubscribe->addStyleClass(
"WsBtnSubscribe");
80 addWidget(m_pButSubscribe);
81 if ( asString(
option(
"useReadAll")) ==
"true" ) {
82 std::string link = asString(
option(
"link")).toUTF8();
83 std::string text = asString(
option(
"text")).toUTF8();
84 WAnchor* pAnchor =
new WAnchor(WLink(WLink::InternalPath, link), text);
85 pAnchor->addStyleClass(
"WsReadAll");
94 std::string dbPath = asString(
option(
"dbNewsLetter")).toUTF8();
95 sqlite3_ =
new dbo::backend::Sqlite3(dbPath);
96 sqlite3_->setProperty(
"show-queries",
"true");
101 }
catch (std::exception& e) {
102 wApp->log(
"notice") <<
"WsNewsLetter::initDB() sqlite3 createTables and database : using existing database error = " << e.what();
115 if (
m_pLineEdit->text().toUTF8().size() < 1 )
return;
121 gdCLdapServer m_cLdapServer;
122 m_cLdapServer.strServer = asString(
option(
"ldapServer")).narrow();
123 m_cLdapServer.strDisplayName = asString(
option(
"ldapName")).toUTF8();
124 m_cLdapServer.strLogon = asString(
option(
"ldapLogon")).toUTF8();
125 m_cLdapServer.strPassword = asString(
option(
"ldapSecret")).toUTF8();
126 if ( m_cLdapServer.TryServer() )
127 WApplication::instance()->log(
"notice") <<
" WsNewsLetter::isEmailExistInLdap() Ldap try ok !";
129 std::string error =
"WsNewsLetter::isEmailExistInLdap() Ldap try not ok !";
130 WApplication::instance()->log(
"notice") << error;
132 wApp->setInternalPath(
"/ws/Errors/ServerTempUnavailable.fhtml",
true);
135 gdCLdapAttributes cWitchAttrs;
136 cWitchAttrs.push_back(
new gdCLdapAttribute(
"givenName"));
137 cWitchAttrs.push_back(
new gdCLdapAttribute(
"sn"));
138 cWitchAttrs.push_back(
new gdCLdapAttribute(
"mail"));
139 cWitchAttrs.push_back(
new gdCLdapAttribute(
"dn"));
141 m_cLdap.setDebugging(
true);
142 m_cLdap.setManageDSAIT(
true);
143 gdCLdapFilter cFilter;
144 cFilter.pServer = &m_cLdapServer;
145 cFilter.strInitialSearchBase = asString(
option(
"ldapBase")).toUTF8();
146 cFilter.nScope = LDAP_SCOPE_SUB;
147 cFilter.strFilter =
"(&(mail=" + email +
"))";
148 gdCLdapEntries cEntries;
149 if ( !m_cLdap.GetInfo(cFilter, cWitchAttrs, cEntries) && m_cLdap.GetLastError() != CLdapErrNoEntries ) {
150 std::string notice =
"WsNewsLetter::isEmailExistInLdap() cannot get ldap info or no entries!";
151 WApplication::instance()->log(
"notice") << notice;
153 wApp->setInternalPath(
"/ws/Errors/ServerTempUnavailable.fhtml",
true);
157 if ( cEntries.size() == 0 ) {
162 if ( cEntries.size() > 1 ) {
166 std::string sGivenName;
170 for (
int nAttr = 0; nAttr < cEntries[0]->Attrs.size(); ++nAttr) {
171 if ( cEntries[0]->Attrs[nAttr]->Attr ==
"sn" ) sSN = cEntries[0]->Attrs[nAttr]->Values[0]->ToStr();
172 if ( cEntries[0]->Attrs[nAttr]->Attr ==
"givenName" ) sGivenName = cEntries[0]->Attrs[nAttr]->Values[0]->ToStr();
173 if ( cEntries[0]->Attrs[nAttr]->Attr ==
"mail" ) sEmail = cEntries[0]->Attrs[nAttr]->Values[0]->ToStr();
174 if ( cEntries[0]->Attrs[nAttr]->Attr ==
"dn" ) sDn = cEntries[0]->Attrs[nAttr]->Values[0]->ToStr();
178 gdCLdapAttribute updateAttr(
"EurListsMember");
179 updateAttr.bNewAttribute =
true;
180 updateAttr.addValue(
"Newsletter03");
182 if ( !m_cLdapServer.UpdateAttribute(entry, updateAttr) )
183 wApp->log(
"notice") <<
"WsNewsLetter::isEmailExistInLdap() Cannot update ldap entry : " << sDn <<
"Contain a correct value";
185 wApp->log(
"notice") <<
"WsNewsLetter::isEmailExistInLdap() Ldap entry : " << sDn <<
" correctly updated !";
190 wApp->log(
"notice") <<
"WsNewsLetter::emailMultiple() email " << email;
191 std::string sTo = asString(
option(
"NLEmailTo")).toUTF8();
192 std::string sSign = asString(
option(
"NLSignature")).toUTF8();
193 std::string sSubject =
"Muliple email detection when registering a NewsLetter";
194 std::string sBody =
"Dear web administrator,<br /><br /> pending a check of the email <b>"
196 +
"</b> I detect a multiple entry on our LDAP database,<br />"
197 +
" please check which correct action is required and process a manual response to our client<br /><br />"
198 +
"Thanks in advance.<br/><br/>"
200 std::string sBodyPlain =
"Dear web administrator,\n\npending a check of the email *"
202 +
"* I detect a multiple entry on our LDAP database,\n"
203 +
" please check which correct action is required and process a manual response to our client\n\n"
204 +
"Thanks in advance.\n\n"
206 sendEmail(sTo, sSubject, sBody, sBodyPlain);
209 void WsNewsLetter::sendEmail(
const std::string& email,
const std::string& sSubject,
const std::string& sBody,
const std::string& sBodyPlain)
211 std::string sSmtpServer = asString(
option(
"smtpServer")).toUTF8();
212 std::string sFrom = asString(
option(
"NLEmailFrom")).toUTF8();
213 Mail::Message message;
214 message.setFrom(Mail::Mailbox(sFrom));
215 if ( email.size() > 0 )
216 message.addRecipient(Mail::To, Mail::Mailbox(email));
217 message.setSubject(sSubject);
218 message.setBody(
"No html renderer in your mail client ");
219 message.addHtmlBody (sBodyPlain);
222 client.connect(
"localhost");
223 client.send(message);
271 std::string random = WRandom::generateId(16);
272 Auth::SHA1HashFunction hf;
273 std::string hash = hf.compute(random, std::string());
274 bool bMatchEmail =
false;
276 dbo::Transaction transaction(
session_);
278 if ( eTokens.size() != 0 ) {
279 std::string text =
"Email is already in db, nothing to do !";
280 new WText(text,
this);
281 wApp->log(
"notice") <<
"WsNewsLetter::emailNotInLdap() email exist in database " << email;
285 eToken->
email = email;
286 eToken->
token = hash;
291 wApp->log(
"notice") <<
"WsNewsLetter::emailNotInLdap() email does not exist in database " << email <<
" token = " << random <<
" hash = " << hash;
295 wApp->setInternalPath(
"/ws/Errors/AlreadyInDb.fhtml",
true);
298 std::string sSubject = asString(
option(
"NLSubject")).toUTF8();
299 std::string sBody = asString(
option(
"NLBody")).toUTF8();
300 std::string sBodyPlain = asString(
option(
"NLBodyPlain")).toUTF8();
302 std::string url = sServerUrl +
"/Newsletter/Subscribe/" + random;
303 wApp->log(
"notice") <<
"WsNewsLetter::emailNotInLdap() url created = " << url;
304 sBody +=
"<a href=\"" + url +
"\">Register</a>";
306 sendEmail(email, sSubject, sBody, sBodyPlain);
307 wApp->setInternalPath(
"/ws/ThanksSubscription.fhtml",
true);
312 if ( path.compare(0, 22,
"/Newsletter/Subscribe/") != 0 )
return path;
313 std::string token = path;
314 boost::algorithm::replace_all(token,
"/Newsletter/Subscribe/",
"");
316 Auth::SHA1HashFunction hf;
317 std::string hash = hf.compute(token, std::string());
318 dbo::Transaction transaction(
session_);
321 if ( eTokens.size() == 0 )
322 retPage =
"/ws/Errors/NotInDB.fhtml";
324 EmailTokens::const_iterator i = eTokens.begin();
325 if ( (*i)->registered == 0 ) {
328 retPage =
"/ws/Subscribe.form";
331 retPage =
"/ws/Errors/AlreadyRegistered.fhtml";
335 wApp->setInternalPath(retPage,
true);
dbo::backend::Sqlite3 * sqlite3_
Wt::Dbo::collection< Wt::Dbo::ptr< EmailToken > > EmailTokens
void emailNotInLdap(const std::string &email)
WsNewsLetter(Wt::WContainerWidget *parent=0)
CTor.
emailAvailability isEmailExistInLdap(const std::string &email)
void registerValidEmail(const std::string &email)
emailAvailability
descripte the result of the Ldap query
std::string checkPath(const std::string ¤tPath)
const boost::any & option(const std::string &attribute) const
Get an options value.
std::string get(const std::string §ion, const std::string &id, const std::string &def)
Wt::WLineEdit * m_pLineEdit
static WsGlobalProperties * instance()
void sendEmail(const std::string &email, const std::string &sSubject, const std::string &sBody, const std::string &sBodyPlain)
void multipleEmail(const std::string &email)
Wt::WPushButton * m_pButSubscribe