SA-MP Forums Archive
[Tutorial] Creating password changing system using dialogs - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Creating password changing system using dialogs (/showthread.php?tid=602819)



Creating password changing system using dialogs - Logic_ - 13.03.2016

Introduction
I have not seen any kind of tutorial related to password changing system with dialogs and nicely written code (with no loose indentation and bugs or bad coding skills). This code is fully written and tested by myself. And i will recommend everyone to read every single code and information instead of copy and pasting (which I'm personally sure that most people do). I basically made this for my gamemode, using the AndySedeyn's Y_INI tutorial and I will prefer, you guys to use the AndySedeyn's tutorial (link is given below).

Necessities
- A script which uses YSI library and whirlpool or a new script which uses AndySedeyn's tutorial : https://sampforum.blast.hk/showthread.php?tid=597639.
- The YSI library: https://sampforum.blast.hk/showthread.php?tid=570883
- Whirlpool: https://sampforum.blast.hk/showthread.php?tid=570945
- ZCMD command processor : https://sampforum.blast.hk/showthread.php?tid=91354

Script

PHP Code:
enum Dialog_Data // all of the data of the 'enum' initialiser, is saved in the memory.
{
    
DIALOG_CHANGEPS,
    
DIALOG_CHANGEPS2
}; 
As we are going to use dialogs for changing the password, I prefer using the dialog ids with 'enum' initialiser, for ease and no tension related to mixing/collision of the dialog ids.

PHP Code:
CMD:changepass(playeridparams[])
{
    
ShowPlayerDialog(playeridDIALOG_CHANGEPSDIALOG_STYLE_INPUT"Changing Password - Step 1""Input your current password, in order to change your password.""Next""Cancel"); // showing the player a dialog
    
return 1;

We have created a command using the ZCMD command processor, and we are showing the player a dialog, to enter his current password, so he/she can move on to the next and final step of changing password.

PHP Code:
public OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {
        case 
DIALOG_CHANGEPS:
        {
            if(
response)
            {
                new 
hashpass[129]; // the whirlpool uses a 129 length string for hashing the password.
                
WP_Hash(hashpasssizeof(hashpass), inputtext); // to hash the input data
                
if(!strcmp(hashpassPlayerInfo[playerid][Password])) // if the password matches, continue to step 2
                
{
                    
ShowPlayerDialog(playeridDIALOG_CHANGEPS2DIALOG_STYLE_INPUT"Changing Password - Step 2""Input your desired new password, in order to comple the process.""Okay""Cancel"); // showing the player a second and final dialog to enter his new password
                
}
                else
                {    
// if the password is incorrect, re-show them to enter the current and correct password
                    
SendClientMessage(playeriddarkred"ERROR: You have entered an incorrect password."); // no need of explanation
                    
ShowPlayerDialog(playeridDIALOG_CHANGEPSDIALOG_STYLE_INPUT"Changing Password - Step 1""Input your current password, in order to change your password.""Next""Cancel"); // showing player the dialog to enter his correct and current password
                
}
            }
            return 
1;
        } 
// end of the dialog part one 
read the commented lines in the code above.

PHP Code:
        case DIALOG_CHANGEPS2:
        {
            if(
response)
            {
                if(!
strlen(inputtext)) // if nothing is entered in the dialog
                
{
                    return 
SendClientMessage(playeriddarkred"ERROR: You must enter a password.");
                }
                new 
string[128];
                
                
format(stringsizeof(string), "Your password is: %s"inputtext); // we have formatted a string to include variables inside it
                
SendClientMessage(playerid, -1string); // sending the above string message 
                
                
new INI:file INI_Open(UserPath(playerid)); // opened the player's save file
                
WP_Hash(PlayerInfo[playerid][Password], 129inputtext); // hashed the password in the player enum
                
INI_WriteString(file"Password"PlayerInfo[playerid][Password]); // replaced the old hashed password with the new one
                
INI_Close(file);
                return 
1;
            }
        }
    }
    return 
0;
// On Dialog Response code end 
Read the commented lines in the code above.

And we are done. If you find any issue or bugs or typos, feel free to notify me. If you liked the tutorial and/or going to use it.



Re: Creating password changing system using dialogs - saffierr - 13.03.2016

Nice tutorial, keep it up!


Re: Creating password changing system using dialogs - AndySedeyn - 13.03.2016

I appreciate the reference to my tutorial. Thank you. However, if you don't mind I'd like to point a few things out:

You send the player's input to the player themselves in DIALOG_CHANGEP2. While it seems that only the player will be able to see the message, it is stored in a .txt file in the Documents library (C:\Users\NAME\Documents\GTA San Andreas User Files\SAMP\chatlog.txt) of the player and it stays there till the player reconnects to the same or to a different server. It's seems far sought but it still is a vulnerability.

In the same dialog, you open the user's file but you don't close it afterwards.

EDIT: another thing unrelated to the working of the code is the minor indentation errors in your php tags. I'm just assuming that it's a copy-paste gone slightly wrong. Other than that, good tutorial.