Skip to content

Commit f0fc344

Browse files
committed
继续完善
1 parent cc73fee commit f0fc344

9 files changed

Lines changed: 260 additions & 27 deletions

File tree

‎kbe/res/server/kbengine_defs.xml‎

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@
134134
<smtp_auth>2</smtp_auth>
135135
<username> kbengine@163.com </username>
136136
<password> test123 </password>
137-
<!-- 邮件验证有效期 -->
138-
<deadline> 259200 </deadline>
137+
<!-- 用户回调接口 -->
138+
<cb_port> 21103 </cb_port>
139139

140140
<email_activation>
141-
<!-- 用户激活回调接口 -->
142-
<cb_port> 21103 </cb_port>
143141
<subject>账号激活</subject>
142+
<!-- 邮件验证有效期 -->
143+
<deadline> 259200 </deadline>
144144
<message>
145145
<![CDATA[
146146
<html>
@@ -161,10 +161,38 @@
161161
</html>
162162
]]>
163163
</message>
164+
165+
<backlink_success_message>
166+
<![CDATA[
167+
<html>
168+
<body>
169+
<font size="7">账号激活成功!</font><br>
170+
----------------------------------------------------------------------<br><br>
171+
<a href="${backlink}">点击进入官网</a><br><br>
172+
感谢您的访问,祝您使用愉快!<br>
173+
</body>
174+
</html>
175+
]]>
176+
</backlink_success_message>
177+
178+
<backlink_fail_message>
179+
<![CDATA[
180+
<html>
181+
<body>
182+
<font size="7">账号激活失败(可能是由于失效或者已被注册等原因), 请重新发送激活邮件!</font><br>
183+
----------------------------------------------------------------------<br><br>
184+
<a href="${backlink}">点击进入官网</a><br><br>
185+
感谢您的访问,祝您使用愉快!<br>
186+
</body>
187+
</html>
188+
]]>
189+
</backlink_fail_message>
164190
</email_activation>
165191

166192
<email_resetpassword>
167193
<subject>密码重置</subject>
194+
<!-- 邮件验证有效期 -->
195+
<deadline> 3600 </deadline>
168196
<message>
169197
<![CDATA[
170198
<html>
@@ -183,7 +211,83 @@
183211
</html>
184212
]]>
185213
</message>
214+
215+
<backlink_success_message>
216+
<![CDATA[
217+
<html>
218+
<body>
219+
<font size="7">操作成功!</font><br>
220+
----------------------------------------------------------------------<br><br>
221+
<a href="${backlink}">点击进入官网</a><br><br>
222+
感谢您的访问,祝您使用愉快!<br>
223+
</body>
224+
</html>
225+
]]>
226+
</backlink_success_message>
227+
228+
<backlink_fail_message>
229+
<![CDATA[
230+
<html>
231+
<body>
232+
<font size="7">操作失败, 请重新发送邮件!</font><br>
233+
----------------------------------------------------------------------<br><br>
234+
<a href="${backlink}">点击进入官网</a><br><br>
235+
感谢您的访问,祝您使用愉快!<br>
236+
</body>
237+
</html>
238+
]]>
239+
</backlink_fail_message>
186240
</email_resetpassword>
241+
242+
<email_bind>
243+
<subject>邮箱绑定</subject>
244+
<!-- 邮件验证有效期 -->
245+
<deadline> 3600 </deadline>
246+
<message>
247+
<![CDATA[
248+
<html>
249+
<body>
250+
邮箱绑定<br><br>
251+
这封信是由kbengine发送的。<br>
252+
您收到这封邮件,是由于在kbengine的用户绑定了这个邮箱地址。如果您并没有访问过kbengine,
253+
或没有进行上述操作, 请忽略这封邮件。您不需要退订或进行其他进一步的操作。<br>
254+
----------------------------------------------------------------------<br>
255+
<font size="7">访问如下链接重置您的密码:</font><br>
256+
----------------------------------------------------------------------<br><br>
257+
以下链接有效期为3天。过期可以重新请求发送一封新的邮件进行重置:<br>
258+
<a href="${backlink}">${backlink}</a><br>
259+
(如果上面不是链接形式,请将该地址手工粘贴到浏览器地址栏再访问)<br><br>
260+
</body>
261+
</html>
262+
]]>
263+
</message>
264+
265+
<backlink_success_message>
266+
<![CDATA[
267+
<html>
268+
<body>
269+
<font size="7">操作成功!</font><br>
270+
----------------------------------------------------------------------<br><br>
271+
<a href="${backlink}">点击进入官网</a><br><br>
272+
感谢您的访问,祝您使用愉快!<br>
273+
</body>
274+
</html>
275+
]]>
276+
</backlink_success_message>
277+
278+
<backlink_fail_message>
279+
<![CDATA[
280+
<html>
281+
<body>
282+
<font size="7">操作失败, 请重新发送邮件!</font><br>
283+
----------------------------------------------------------------------<br><br>
284+
<a href="${backlink}">点击进入官网</a><br><br>
285+
感谢您的访问,祝您使用愉快!<br>
286+
</body>
287+
</html>
288+
]]>
289+
</backlink_fail_message>
290+
</email_bind>
187291
</email_server>
188292

189293
<billingSystem>

‎kbe/src/lib/cstdkbe/platform.hpp‎

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
5757
#include <windows.h>
5858
#include <unordered_map>
5959
#include <functional>
60-
#include <regex>
6160
#else
6261
// linux include
6362
#include <errno.h>
@@ -79,7 +78,6 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
7978
#include <tr1/unordered_map>
8079
#include <tr1/functional>
8180
#include <tr1/memory>
82-
#include <tr1/regex>
8381
#include <linux/types.h>
8482
#include <sys/time.h>
8583
#include <sys/types.h>
@@ -529,6 +527,89 @@ inline const char * getUsername()
529527
#endif
530528
}
531529

530+
/** 检查用户名合法性 */
531+
inline bool validName(const char* name, int size)
532+
{
533+
if(size >= 256)
534+
return false;
535+
536+
for(int i=0; i<size; i++)
537+
{
538+
char ch = name[i];
539+
if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9' || ch == '_')
540+
continue;
541+
542+
return false;
543+
}
544+
545+
return true;
546+
}
547+
548+
inline bool validName(const std::string& name)
549+
{
550+
return validName(name.c_str(), name.size());
551+
}
552+
553+
/** 检查email地址合法性
554+
严格匹配请用如下表达式
555+
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
556+
*/
557+
#ifdef USE_REGEX
558+
#include <regex>
559+
#endif
560+
561+
inline bool email_isvalid(const char *address)
562+
{
563+
#ifdef USE_REGEX
564+
std::tr1::regex _mail_pattern("([a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)");
565+
return std::tr1::regex_match(accountName, _mail_pattern);
566+
#endif
567+
int len = strlen(address);
568+
if(len <= 3)
569+
return false;
570+
571+
char ch = address[len - 1];
572+
if(!(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9'))
573+
return false;
574+
575+
int count = 0;
576+
const char *c, *domain;
577+
static char *rfc822_specials = "()<>@,;:\\\"[]";
578+
579+
/* first we validate the name portion (name@domain) */
580+
for (c = address; *c; c++) {
581+
if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) ==
582+
'\"')) {
583+
while (*++c) {
584+
if (*c == '\"') break;
585+
if (*c == '\\' && (*++c == ' ')) continue;
586+
if (*c <= ' ' || *c >= 127) return false;
587+
}
588+
if (!*c++) return false;
589+
if (*c == '@') break;
590+
if (*c != '.') return false;
591+
continue;
592+
}
593+
if (*c == '@') break;
594+
if (*c <= ' ' || *c >= 127) return false;
595+
if (strchr(rfc822_specials, *c)) return false;
596+
}
597+
if (c == address || *(c - 1) == '.') return false;
598+
599+
/* next we validate the domain portion (name@domain) */
600+
if (!*(domain = ++c)) return false;
601+
do {
602+
if (*c == '.') {
603+
if (c == domain || *(c - 1) == '.') return false;
604+
count++;
605+
}
606+
if (*c <= ' ' || *c >= 127) return false;
607+
if (strchr(rfc822_specials, *c)) return false;
608+
} while (*++c);
609+
610+
return (count >= 1);
611+
}
612+
532613
/** 获取进程ID */
533614
inline int32 getProcessPID()
534615
{

‎kbe/src/lib/db_mysql/kbe_table_mysql.cpp‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,11 @@ bool KBEEmailVerificationTableMysql::queryAccount(DBInterface * dbi, int8 type,
462462

463463
sqlstr += tbuf;
464464

465+
sqlstr += "\" and type=";
466+
kbe_snprintf(tbuf, MAX_BUF, "%d", type);
467+
sqlstr += tbuf;
468+
465469
SAFE_RELEASE_ARRAY(tbuf);
466-
sqlstr += "\"";
467470

468471
if(!dbi->query(sqlstr.c_str(), sqlstr.size(), false))
469472
return false;
@@ -489,7 +492,7 @@ bool KBEEmailVerificationTableMysql::queryAccount(DBInterface * dbi, int8 type,
489492
//-------------------------------------------------------------------------------------
490493
bool KBEEmailVerificationTableMysql::logAccount(DBInterface * dbi, int8 type, ACCOUNT_INFOS& info)
491494
{
492-
std::string sqlstr = "insert into kbe_email_verification (accountName, datas, code, logtime) values(";
495+
std::string sqlstr = "insert into kbe_email_verification (accountName, type, datas, code, logtime) values(";
493496

494497
char* tbuf = new char[MAX_BUF * 3];
495498

@@ -500,6 +503,9 @@ bool KBEEmailVerificationTableMysql::logAccount(DBInterface * dbi, int8 type, AC
500503
sqlstr += tbuf;
501504
sqlstr += "\",";
502505

506+
kbe_snprintf(tbuf, MAX_BUF, "%d,", type);
507+
sqlstr += tbuf;
508+
503509
mysql_real_escape_string(static_cast<DBInterfaceMysql*>(dbi)->mysql(),
504510
tbuf, info.password.c_str(), info.password.size());
505511

@@ -539,13 +545,16 @@ bool KBEEmailVerificationTableMysql::activateAccount(DBInterface * dbi, const st
539545

540546
sqlstr += tbuf;
541547

548+
sqlstr += "\" and type=";
549+
kbe_snprintf(tbuf, MAX_BUF, "%d", (int)KBEEmailVerificationTable::V_TYPE_CREATEACCOUNT);
550+
sqlstr += tbuf;
551+
542552
SAFE_RELEASE_ARRAY(tbuf);
543-
sqlstr += "\"";
544553

545554
if(!dbi->query(sqlstr.c_str(), sqlstr.size(), false))
546555
return false;
547556

548-
uint64 logtime;
557+
uint64 logtime = 1;
549558

550559
MYSQL_RES * pResult = mysql_store_result(static_cast<DBInterfaceMysql*>(dbi)->mysql());
551560
if(pResult)
@@ -651,11 +660,10 @@ bool KBEEmailVerificationTableMysql::delAccount(DBInterface * dbi, int8 type, co
651660

652661
sqlstr += "\"";
653662
sqlstr += tbuf;
654-
sqlstr += "\", and type=";
663+
sqlstr += "\" and type=";
655664

656665
kbe_snprintf(tbuf, MAX_BUF, "%d", type);
657666
sqlstr += tbuf;
658-
sqlstr += ")";
659667

660668
SAFE_RELEASE_ARRAY(tbuf);
661669

@@ -684,15 +692,21 @@ bool KBEEmailVerificationTableMysql::syncToDB(DBInterface* dbi)
684692
// 删除xx小时之前的记录
685693
sqlstr = (boost::format("delete from kbe_email_verification where logtime<%1% and type=%2%") %
686694
KBEngine::StringConv::val2str(time(NULL) - g_kbeSrvConfig.emailAtivationInfo_.deadline) %
687-
((int8)KBEEmailVerificationTable::V_TYPE_CREATEACCOUNT)).str();
695+
((int)KBEEmailVerificationTable::V_TYPE_CREATEACCOUNT)).str();
696+
697+
ret = dbi->query(sqlstr.c_str(), sqlstr.size(), true);
698+
KBE_ASSERT(ret);
688699

689700
sqlstr = (boost::format("delete from kbe_email_verification where logtime<%1% and type=%2%") %
690701
KBEngine::StringConv::val2str(time(NULL) - g_kbeSrvConfig.emailResetPasswordInfo_.deadline) %
691-
((int8)KBEEmailVerificationTable::V_TYPE_RESETPASSWORD)).str();
702+
((int)KBEEmailVerificationTable::V_TYPE_RESETPASSWORD)).str();
703+
704+
ret = dbi->query(sqlstr.c_str(), sqlstr.size(), true);
705+
KBE_ASSERT(ret);
692706

693707
sqlstr = (boost::format("delete from kbe_email_verification where logtime<%1% and type=%2%") %
694708
KBEngine::StringConv::val2str(time(NULL) - g_kbeSrvConfig.emailBindInfo_.deadline) %
695-
((int8)KBEEmailVerificationTable::V_TYPE_BIND_MAIL)).str();
709+
((int)KBEEmailVerificationTable::V_TYPE_BIND_MAIL)).str();
696710

697711
ret = dbi->query(sqlstr.c_str(), sqlstr.size(), true);
698712
KBE_ASSERT(ret);

‎kbe/src/lib/server/serverconfig.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ bool ServerConfig::loadConfig(std::string fileName)
205205
childnode1 = xml->enterNode(rootNode1, "deadline");
206206
if(childnode1)
207207
emailAtivationInfo_.deadline = xml->getValInt(childnode1);
208+
209+
childnode1 = xml->enterNode(rootNode1, "backlink_success_message");
210+
if(childnode1)
211+
emailAtivationInfo_.backlink_success_message = childnode1->ToText()->Value();
212+
213+
childnode1 = xml->enterNode(rootNode1, "backlink_fail_message");
214+
if(childnode1)
215+
emailAtivationInfo_.backlink_fail_message = childnode1->ToText()->Value();
208216
}
209217

210218
rootNode1 = xml->enterNode(rootNode, "email_resetpassword");

‎kbe/src/lib/server/serverconfig.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ struct EmailSendInfo
9393
{
9494
std::string subject;
9595
std::string message;
96+
std::string backlink_success_message, backlink_fail_message;
97+
9698
uint32 deadline;
9799
};
98100

‎kbe/src/server/dbmgr/dbtasks.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,9 +872,7 @@ bool DBTaskAccountLogin::db_thread_process()
872872
deadline_ = info.deadline;
873873
if(ACCOUNT_TYPE(g_kbeSrvConfig.getLoginApp().account_type) != ACCOUNT_TYPE_NORMAL)
874874
{
875-
// 据说regex不是线程安全的?
876-
std::tr1::regex mail_pattern("([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)");
877-
if (std::tr1::regex_match(accountName_, mail_pattern))
875+
if (email_isvalid(accountName_.c_str()))
878876
{
879877
ERROR_MSG(boost::format("DBTaskAccountLogin::db_thread_process(): account[%1%] is email, autocreate failed!\n") %
880878
accountName_);

‎kbe/src/server/loginapp/account_activate_handler.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,25 @@ int AccountActivateHandler::handleInputNotification(int fd)
216216
return 0;
217217
}
218218

219+
//-------------------------------------------------------------------------------------
220+
void AccountActivateHandler::onAccountActivated(std::string& code, bool success)
221+
{
222+
std::map< int, CLIENT >::iterator iter = clients_.begin();
223+
for(; iter != clients_.end(); iter++)
224+
{
225+
if(iter->second.code == code)
226+
{
227+
if(!iter->second.endpoint->good())
228+
continue;
229+
230+
if(success)
231+
iter->second.endpoint->send(g_kbeSrvConfig.emailAtivationInfo_.backlink_success_message.c_str(), g_kbeSrvConfig.emailAtivationInfo_.backlink_success_message.size());
232+
else
233+
iter->second.endpoint->send(g_kbeSrvConfig.emailAtivationInfo_.backlink_fail_message.c_str(), g_kbeSrvConfig.emailAtivationInfo_.backlink_fail_message.size());
234+
iter->second.endpoint->close();
235+
}
236+
}
237+
}
238+
219239
//-------------------------------------------------------------------------------------
220240
}

0 commit comments

Comments
 (0)