· 5 years ago · Dec 01, 2020, 05:38 PM
1#include <amxmodx>
2#include <amxmisc>
3#include <engine>
4#include <sqlx>
5
6#pragma semicolon 1
7
8new const TableName[] = "Regsys_table";
9
10enum _:Text_properties{
11 Chat[128],
12 Menu[128]
13};
14new Handle:SQLtuple;
15new const constText[][Text_properties] ={
16 {"^1Nem írtál semmit a névhez!", "Regisztálj/Jelentkezz be!"},
17 {"^1A beírt név túl rövid! (Minimum: %i)", "Regisztráció"},
18 {"^1Nem írtál semmit a jelszóhoz!", "Felhasználónév: %s"},
19 {"^1A beírt jelszó túl rövid, vagy túl hosszú! (Minimum:^3 %i^1 - Maximum:^3 %i^1)", "Bejelentkezés"},
20 {"^1A két jelszó nem egyezik meg!", "Jelszó: %s"},
21 {"^1Regisztráció folyamatban... kérlek várj!", "Jelszó újra: %s"},
22 {"^1Ez a felhasználónév már foglalt!", ""},
23 {"^1Sikeres regisztráció, jelentkezz be!", ""},
24 {"^1Ez a felhasználónév már foglalt!", ""},
25 {"^1Nem sikerült bejelentkezni, rossz felhasználó vagy jelszó!", ""},
26 {"^1Sikeresen bejelentkeztél!", ""},
27};
28enum _:Reg_properties{
29 Username[64],
30 Password[64],
31 RePassword[64],
32};
33new g_sReg[33][Reg_properties];
34enum _:Account_properties{
35 AccountId,
36 Logged,
37 Ip[64],
38 SteamId[64],
39 LastlogDate[64],
40 Regdate[64],
41 bool:Banned,
42 BannedReason[64],
43 bool:LoggedIn,
44};
45new g_Account[33][Account_properties];
46new bool:g_bMenu[33];
47new Forward_Load;
48new g_cChatPrefix, g_cMenuPrefix, g_cUsernameLength, g_cPasswordMinLength, g_cPasswordMaxLength;
49public plugin_init()
50{
51 register_plugin("Sex18+_Regisztracios_Rendszer", "0.1.0", "Turán*");
52
53 SQLtuple = SQL_MakeDbTuple("db.synhosting.eu", "kornelka", "Adatbazis", "kornelka");
54 p_SQLCreateTable();
55
56 register_impulse(201, "p_checkloggedin");
57 register_clcmd("type_username", "p_typeusername");
58 register_clcmd("type_password", "p_typepassword");
59 register_clcmd("type_repassword", "p_typerepassword");
60
61 Forward_Load = CreateMultiForward("LoadData", ET_IGNORE, FP_CELL);
62
63 g_cChatPrefix = register_cvar("chat_prefix", "^4[Sex+18]^3 »");
64 g_cMenuPrefix = register_cvar("menu_prefix", "\r[Sex+18]\w »\d");
65 g_cUsernameLength = register_cvar("username_length", "4");
66 g_cPasswordMinLength = register_cvar("password_minlength", "4");
67 g_cPasswordMaxLength = register_cvar("password_maxlength", "12");
68}
69public plugin_natives()
70{
71 register_native("reg_get_user_loggedin","p_reg_get_user_loggedin", 1);
72 register_native("reg_get_user_id","p_reg_get_user_id", 1);
73}
74public p_reg_get_user_loggedin(id)
75 return g_Account[id][Logged];
76
77public p_reg_get_user_id(id)
78 return g_Account[id][AccountId];
79
80public p_regmainmenu(id)
81{
82 new sText[128], iMenu;
83 formatex(sText, charsmax(sText), "%s %s", show_menuprefix(), constText[0][Menu]);
84 iMenu = menu_create(sText, "p_regmainmenu_handler");
85
86 menu_additem(iMenu, constText[1][Menu], "0", 0);
87 menu_additem(iMenu, constText[3][Menu], "1", 0);
88
89 menu_display(id, iMenu, 0);
90}
91public p_regmainmenu_handler(id, iMenu, iItem)
92{
93 if(iItem == MENU_EXIT){ menu_destroy(iMenu); return PLUGIN_HANDLED;}
94
95 new sData[9], sName[MAX_NAME_LENGTH], iAccess, iCallback, iKey;
96 menu_item_getinfo(iMenu, iItem, iAccess, sData, charsmax(sData), sName, charsmax(sName), iCallback);
97 iKey = str_to_num(sData);
98
99 switch(iKey)
100 {
101 case 0: {p_regmenu(id); g_bMenu[id] = true;}
102 case 1: {p_logmenu(id); g_bMenu[id] = false;}
103 }
104 return PLUGIN_HANDLED;
105}
106public p_checkloggedin(id)
107{
108 if(!g_Account[id][LoggedIn])
109 p_regmainmenu(id);
110}
111public p_regmenu(id)
112{
113 new sText[128], iMenu;
114 formatex(sText, charsmax(sText), "%s %s", show_menuprefix(), constText[1][Menu]);
115 iMenu = menu_create(sText, "p_regmenu_handler");
116
117 formatex(sText, charsmax(sText), constText[2][Menu], g_sReg[id][Username]);
118 menu_additem(iMenu, sText, "0", 0);
119 formatex(sText, charsmax(sText), constText[4][Menu], g_sReg[id][Password]);
120 menu_additem(iMenu, sText, "1", 0);
121 formatex(sText, charsmax(sText), constText[5][Menu], g_sReg[id][RePassword]);
122 menu_additem(iMenu, sText, "2", 0);
123
124 formatex(sText, charsmax(sText), "%s", constText[1][Menu]);
125 menu_additem(iMenu, sText, "3", 0);
126
127 menu_display(id, iMenu, 0);
128}
129public p_regmenu_handler(id, iMenu, iItem)
130{
131 if(iItem == MENU_EXIT){ menu_destroy(iMenu); return PLUGIN_HANDLED; }
132
133 new sData[9], sName[MAX_NAME_LENGTH], iAccess, iCallback, iKey;
134 menu_item_getinfo(iMenu, iItem, iAccess, sData, charsmax(sData), sName, charsmax(sName), iCallback);
135 iKey = str_to_num(sData);
136
137 switch(iKey)
138 {
139 case 0: client_cmd(id, "messagemode type_username");
140 case 1: client_cmd(id, "messagemode type_password");
141 case 2: client_cmd(id, "messagemode type_repassword");
142 case 3: p_checkpassword(id);
143 }
144 return PLUGIN_HANDLED;
145}
146public p_checkpassword(id)
147{
148 if(!equal(g_sReg[id][Password], g_sReg[id][RePassword]))
149 {
150 client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[4][Chat]);
151 g_sReg[id][Password] = "";
152 g_sReg[id][RePassword] = "";
153 p_regmenu(id);
154 return PLUGIN_HANDLED;
155 }
156 else
157 {
158 set_task(5.0, "p_SQLUsernameCheck", id);
159 client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[5][Chat]);
160 }
161 return PLUGIN_HANDLED;
162}
163public p_logmenu(id)
164{
165 new sText[128], iMenu;
166 formatex(sText, charsmax(sText), "%s %s", show_menuprefix(), constText[3][Menu]);
167 iMenu = menu_create(sText, "p_logmenu_handler");
168
169 formatex(sText, charsmax(sText), constText[2][Menu], g_sReg[id][Username]);
170 menu_additem(iMenu, sText, "0", 0);
171 formatex(sText, charsmax(sText), constText[4][Menu], g_sReg[id][Password]);
172 menu_additem(iMenu, sText, "1", 0);
173
174 formatex(sText, charsmax(sText), "%s", constText[3][Menu]);
175 menu_additem(iMenu, sText, "2", 0);
176
177 menu_display(id, iMenu, 0);
178}
179public p_logmenu_handler(id, iMenu, iItem)
180{
181 if(iItem == MENU_EXIT){ menu_destroy(iMenu); return PLUGIN_HANDLED; }
182
183 new sData[9], sName[MAX_NAME_LENGTH], iAccess, iCallback, iKey;
184 menu_item_getinfo(iMenu, iItem, iAccess, sData, charsmax(sData), sName, charsmax(sName), iCallback);
185 iKey = str_to_num(sData);
186
187 switch(iKey)
188 {
189 case 0: client_cmd(id, "messagemode type_username");
190 case 1: client_cmd(id, "messagemode type_password");
191 case 2: p_checkSQL(id);
192 }
193 return PLUGIN_HANDLED;
194}
195public p_checkSQL(id)
196{
197 p_SQLload(id);
198}
199public p_typeusername(id)
200{
201 new sText[64], iLen;
202 read_args(sText, charsmax(sText));
203 remove_quotes(sText);
204
205 iLen = get_pcvar_num(g_cUsernameLength);
206
207 if(strlen(sText) == 0)
208 {
209 client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[0][Chat]);
210 show_menubool(id);
211 return PLUGIN_HANDLED;
212 }
213
214 if(strlen(sText) < iLen)
215 {
216 client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[1][Chat], iLen);
217 show_menubool(id);
218 }
219 else
220 {
221 copy(g_sReg[id][Username], charsmax(g_sReg[]), sText);
222 show_menubool(id);
223 }
224 return PLUGIN_HANDLED;
225}
226public p_typepassword(id)
227{
228 new sText[64], iLen[2];
229 read_args(sText, charsmax(sText));
230 remove_quotes(sText);
231
232 iLen[0] = get_pcvar_num(g_cPasswordMinLength);
233 iLen[1] = get_pcvar_num(g_cPasswordMaxLength);
234
235 if(strlen(sText) == 0)
236 {
237 client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[2][Chat]);
238 show_menubool(id);
239 return PLUGIN_HANDLED;
240 }
241
242 if(strlen(sText) < iLen[0] || strlen(sText) > iLen[1])
243 {
244 client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[3][Chat], iLen[0], iLen[1]);
245 show_menubool(id);
246 return PLUGIN_HANDLED;
247 }
248
249 copy(g_sReg[id][Password], charsmax(g_sReg[]), sText);
250 show_menubool(id);
251
252 return PLUGIN_HANDLED;
253}
254public p_typerepassword(id)
255{
256 new sText[64], iLen[2];
257 read_args(sText, charsmax(sText));
258 remove_quotes(sText);
259
260 iLen[0] = get_pcvar_num(g_cPasswordMinLength);
261 iLen[1] = get_pcvar_num(g_cPasswordMaxLength);
262
263 if(strlen(sText) == 0)
264 {
265 client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[2][Chat]);
266 show_menubool(id);
267 return PLUGIN_HANDLED;
268 }
269
270 if(strlen(sText) < iLen[0] || strlen(sText) > iLen[1])
271 {
272 client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[3][Chat], iLen[0], iLen[1]);
273 show_menubool(id);
274 return PLUGIN_HANDLED;
275 }
276
277 g_sReg[id][RePassword] = sText;
278 show_menubool(id);
279
280 return PLUGIN_HANDLED;
281}
282p_SQLCreateTable()
283{
284 static sQuery[512];
285 new iLen;
286
287 iLen += formatex(sQuery[iLen], charsmax(sQuery), "CREATE TABLE IF NOT EXISTS `%s` ", TableName);
288 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "( ");
289 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`AccId` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, ");
290 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`Username` varchar(64) NOT NULL,");
291 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`Password` varchar(64) NOT NULL,");
292 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`Regdate` varchar(64) NOT NULL,");
293 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`LastLogDate` varchar(64) NOT NULL,");
294 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`UserIp` varchar(64) NOT NULL,");
295 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`UserSteamid` varchar(64) NOT NULL,");
296 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`Banned` INT(1) NOT NULL,");
297 iLen += formatex(sQuery[iLen], charsmax(sQuery)-iLen, "`BannedReason` varchar(64) NOT NULL)");
298
299 SQL_ThreadQuery(SQLtuple, "p_SQLCreateTableThr", sQuery);
300}
301public p_SQLUsernameCheck(id)
302{
303 static sQuery[512];
304 new sData[2];
305
306 sData[0] = id;
307 sData[1] = get_user_userid(id);
308
309 formatex(sQuery, charsmax(sQuery), "SELECT * FROM `%s` WHERE `Username`='%s';", TableName, g_sReg[id][Username]);
310 SQL_ThreadQuery(SQLtuple, "p_SQLUsernameCheckthr", sQuery, sData, 2);
311}
312public p_SQLUsernameCheckthr(iFailState, Handle:sQuery, sError[], iErrcode, sData[], iDataSize)
313{
314 if(iFailState == TQUERY_CONNECT_FAILED)
315 set_fail_state("*DEBUG* [UsernameCheck] Nem sikerult csatlakozni az adatbazishoz.");
316 else if(iFailState == TQUERY_QUERY_FAILED)
317 set_fail_state("*DEBUG* [UsernameCheck] Lekerdezesi hiba");
318 if(iErrcode)
319 log_amx("*DEBUG* [UsernameCheck] - %s", sError);
320
321 new id = sData[0];
322
323 if(sData[1] != get_user_userid(id)) return;
324
325 new iRowsFound = SQL_NumRows(sQuery);
326
327 if(iRowsFound > 0)
328 {
329 client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[6][Chat]);
330 show_menubool(id);
331 }
332 else p_SQLinsertTable(id);
333}
334public p_SQLinsertTable(id)
335{
336 static sQuery[512];
337 new sHashedPassword[64], sData[2], iTime;
338
339 hash_string(g_sReg[id][Password], Hash_Sha256, sHashedPassword, charsmax(sHashedPassword));
340 iTime = get_systime();
341 format_time(g_Account[id][Regdate], charsmax(g_Account[]), "%Y.%m.%d - %H:%M:%S", iTime);
342 format_time(g_Account[id][LastlogDate], charsmax(g_Account[]), "%Y.%m.%d - %H:%M:%S", iTime);
343
344 get_user_ip(id, g_Account[id][Ip], charsmax(g_Account[]));
345 get_user_authid(id, g_Account[id][SteamId], charsmax(g_Account[]));
346 sData[0] = id;
347 sData[1] = get_user_userid(id);
348
349 formatex(sQuery, charsmax(sQuery), "INSERT INTO `%s` (Username,Password,Regdate,LastLogDate,UserIp,UserSteamid,Banned,BannedReason) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s') ", TableName, g_sReg[id][Username], sHashedPassword, g_Account[id][Regdate], g_Account[id][LastlogDate], g_Account[id][Ip], g_Account[id][SteamId], g_Account[id][Banned], g_Account[id][BannedReason]);
350
351 SQL_ThreadQuery(SQLtuple, "p_SQLinsertTablethr", sQuery, sData, 2);
352}
353public p_SQLinsertTablethr(iFailState, Handle:sQuery, sError[], iErrcode, sData[], iDataSize)
354{
355 if(iFailState == TQUERY_CONNECT_FAILED || iFailState == TQUERY_QUERY_FAILED)
356 {
357 log_amx("*DEBUG* [Insert] - %s", sError);
358 return;
359 }
360
361 new id = sData[0];
362
363 if(sData[1] != get_user_userid(id)) return;
364
365 client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[7][Chat]);
366}
367public p_SQLload(id)
368{
369 static sQuery[512];
370 new sHashedPassword[64], sData[2];
371 hash_string(g_sReg[id][Password], Hash_Sha256, sHashedPassword, charsmax(sHashedPassword));
372 sData[0] = id;
373 sData[1] = get_user_userid(id);
374
375 formatex(sQuery, charsmax(sQuery), "SELECT * FROM `%s` WHERE Username = ^"%s^" AND Password = ^"%s^";", TableName, g_sReg[id][Username], sHashedPassword);
376 SQL_ThreadQuery(SQLtuple, "p_SQLloadthr", sQuery, sData, 2);
377}
378public p_SQLloadthr(iFailState, Handle:sQuery, sError[], iErrcode, sData[], iDataSize)
379{
380 if(iFailState == TQUERY_CONNECT_FAILED)
381 set_fail_state("*DEBUG* [UsernameCheck] Nem sikerult csatlakozni az adatbazishoz.");
382 else if(iFailState == TQUERY_QUERY_FAILED)
383 set_fail_state("*DEBUG* [UsernameCheck] Lekerdezesi hiba");
384 if(iErrcode)
385 log_amx("*DEBUG* [UsernameCheck] - %s", sError);
386
387 new id = sData[0];
388 if(get_user_userid(id) != sData[1]) return;
389
390 new iRowsFound = SQL_NumRows(sQuery);
391
392 if(iRowsFound > 0)
393 {
394 new iTaken = 0;
395 if(g_Account[id][AccountId] == SQL_ReadResult(sQuery, SQL_FieldNameToNum(sQuery, "AccId")))
396 {
397 for(new i = 1; i < 32; i++)
398 {
399 iTaken = 1;
400 g_Account[id][AccountId] = 0;
401 client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[8][Chat]);
402 break;
403 }
404 }
405
406 if(iTaken == 1) return;
407
408 g_Account[id][AccountId] = SQL_ReadResult(sQuery, SQL_FieldNameToNum(sQuery, "AccId"));
409 new Forward_Testret;
410 ExecuteForward(Forward_Load, Forward_Testret, id);
411 g_Account[id][LoggedIn] = true;
412 p_SQLUpdate(id);
413 client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[10][Chat]);
414 }
415 else{client_print_color(id, print_team_default, "%s %s", show_chatprefix(), constText[9][Chat]); return;}
416}
417public p_SQLCreateTableThr(iFailState, Handle:sQuery, sError[], iErrcode, sData[])
418{
419 if(iFailState == TQUERY_CONNECT_FAILED)
420 set_fail_state("*DEBUG* [CreateTable] Nem sikerult csatlakozni az adatbazishoz.");
421 else if(iFailState == TQUERY_QUERY_FAILED)
422 set_fail_state("*DEBUG* [CreateTable] Lekerdezesi hiba");
423 if(iErrcode)
424 log_amx("*DEBUG* [CreateTable] - %s", sError);
425}
426public p_SQLUpdate(id)
427{
428 static sQuery[512];
429 new sData[2], iTime;
430 sData[0] = id;
431 sData[1] = get_user_userid(id);
432 iTime = get_systime();
433
434 format_time(g_Account[id][LastlogDate], charsmax(g_Account[]), "%Y.%m.%d - %H:%M:%S", iTime);
435
436 formatex(sQuery, charsmax(sQuery), "UPDATE `%s` SET LastLogDate = ^"%s^" WHERE AccId = ^"%d^";", TableName, g_Account[id][LastlogDate], g_Account[id][AccountId]);
437 SQL_ThreadQuery(SQLtuple, "p_SQLUpdatethr", sQuery, sData, 2);
438}
439public p_SQLUpdatethr(iFailState, Handle:sQuery, sError[], iErrcode, sData[], iDataSize)
440{
441 if(iFailState == TQUERY_CONNECT_FAILED || iFailState == TQUERY_QUERY_FAILED)
442 {
443 log_amx("*DEBUG* [Update] - %s", sError);return;
444 }
445 new id = sData[0];
446 if(sData[1] != get_user_userid(id)) return;
447}
448public client_authorized(id)
449{
450 if(is_user_bot(id))
451 return PLUGIN_HANDLED;
452
453 g_sReg[id][Username] = "";
454 g_sReg[id][Password] = "";
455 g_sReg[id][RePassword] = "";
456
457 g_Account[id][AccountId] = 0;
458 g_Account[id][Ip] = "";
459 g_Account[id][SteamId] = "";
460 g_Account[id][LastlogDate] = "";
461 g_Account[id][Regdate] = 0;
462 g_Account[id][Banned] = false;
463 g_Account[id][BannedReason] = "";
464 g_Account[id][LoggedIn] = false;
465
466 return PLUGIN_HANDLED;
467}
468public client_disconnected(id)
469{
470 if(is_user_bot(id))
471 return PLUGIN_HANDLED;
472
473 g_sReg[id][Username] = "";
474 g_sReg[id][Password] = "";
475 g_sReg[id][RePassword] = "";
476
477 g_Account[id][AccountId] = 0;
478 g_Account[id][Ip] = "";
479 g_Account[id][SteamId] = "";
480 g_Account[id][LastlogDate] = "";
481 g_Account[id][Regdate] = 0;
482 g_Account[id][Banned] = false;
483 g_Account[id][BannedReason] = "";
484 g_Account[id][LoggedIn] = false;
485
486 return PLUGIN_HANDLED;
487}
488show_menubool(id)
489{
490 if(g_bMenu[id]) p_regmenu(id); else p_logmenu(id);
491}
492show_chatprefix()
493{
494 new sChatPrefix[64];
495 get_pcvar_string(g_cChatPrefix, sChatPrefix, charsmax(sChatPrefix));
496 return sChatPrefix;
497}
498show_menuprefix()
499{
500 new sMenuPrefix[64];
501 get_pcvar_string(g_cMenuPrefix, sMenuPrefix, charsmax(sMenuPrefix));
502 return sMenuPrefix;
503}