WEBVTT

00:00.170 --> 00:06.860
So we have almost reached the end of this project and this video is optional.

00:06.890 --> 00:13.790
But before you decide if you want to watch it or not, let me explain what it is about.

00:13.790 --> 00:22.700
So we are at the guestbook page and you can see I am displaying the names and emails in the open.

00:22.730 --> 00:23.870
That's not good.

00:23.900 --> 00:33.080
We don't want the emails that people have sent to us to be picked up by any robots, or any automated

00:33.080 --> 00:37.970
bots that can use those emails to spam those people.

00:38.000 --> 00:43.340
And in this video, we will obfuscate this data inside the view.

00:43.370 --> 00:50.540
So that's why I say this is optional, but it might be an interesting exercise on how do you work with

00:50.540 --> 00:51.380
strings.

00:51.380 --> 00:55.280
So either stay or you can skip this one.

00:56.000 --> 00:57.740
Now I think you are staying.

00:57.740 --> 01:02.430
So let's jump to the guestbook router.

01:03.330 --> 01:06.900
So we're not gonna be doing anything in here.

01:06.900 --> 01:12.270
I think we can actually add some view logic.

01:13.110 --> 01:20.940
So in here with the messages I'm going to add some functions that will obfuscate the email and the name.

01:20.940 --> 01:27.120
And we're just going to use those functions every single time we render something.

01:27.930 --> 01:30.420
By the way let's have the docs open.

01:30.420 --> 01:33.960
And let's begin by obfuscating the name.

01:33.960 --> 01:38.730
Let me add a function called obfuscate name.

01:38.730 --> 01:45.480
I'm starting with this one because it's just would be easier to return a string from it.

01:47.190 --> 01:54.090
So we need to use the str Len which is checking the length of a string.

01:54.090 --> 01:57.790
If this is less or equal to.

01:57.820 --> 02:03.070
Then you shouldn't really obfuscate anything as there is no point.

02:03.220 --> 02:06.490
I think there are some names that are this short.

02:06.520 --> 02:14.620
It might be a nickname, or I think some Asian names are this short now.

02:15.130 --> 02:20.320
In the other case, first we're gonna just get the first letter of the name.

02:20.320 --> 02:26.650
For that, we're gonna use another function called substring, which basically lets us get the part

02:26.680 --> 02:27.700
of a string.

02:28.690 --> 02:33.670
So from name we are starting at an offset which is zero based.

02:34.240 --> 02:40.360
So I get the position zero which is the first letter and the length is one.

02:40.600 --> 02:47.560
Then I'm gonna generate some characters using str repeat.

02:47.560 --> 02:54.370
This allows me to construct a string from a character or from a string in general.

02:55.940 --> 02:59.870
So first a string to repeat that would be an asterisk.

03:00.500 --> 03:03.620
How many times we'd like to repeat this one?

03:03.860 --> 03:10.100
Well, maybe let's use str Len again.

03:10.100 --> 03:17.570
And I'm going to get the length of the name minus two.

03:17.600 --> 03:22.970
That's because I'd like to display the first and the last name.

03:23.750 --> 03:31.490
This means we need to finish off with another substring call for the name, and I can use negative offset

03:31.490 --> 03:37.280
which would basically move to the end of the string if I want the last letter.

03:37.310 --> 03:44.660
And I don't need to pass the length here because minus one is essentially moving from the end to, let's

03:44.660 --> 03:45.530
say this point.

03:45.530 --> 03:52.400
So from this string string I'm moving before G and then I'm going till the end.

03:52.430 --> 03:56.270
So that way I'm just getting the last letter.

03:57.080 --> 04:01.970
Let's add a semicolon in here and let's see if this works.

04:01.970 --> 04:03.080
Where is the name?

04:03.380 --> 04:08.060
The name is here we use HTML special chars as well.

04:08.090 --> 04:12.680
And I'm going to obfuscate the name.

04:13.100 --> 04:15.020
Now let's jump back to our app.

04:15.020 --> 04:17.330
And this is how it looks like.

04:18.710 --> 04:20.360
Okay so this was fine.

04:20.390 --> 04:25.310
Next up let's work on obfuscating the email.

04:25.310 --> 04:28.970
So we do obfuscate email.

04:30.590 --> 04:36.440
We need to get the string argument and we return the string as well.

04:36.860 --> 04:42.200
So here well we need to be wary of the domain.

04:42.200 --> 04:45.680
So let's get the parts of this email.

04:45.680 --> 04:52.470
First we're going to be using explode because with emails we are sure that there is an ad character,

04:52.470 --> 04:53.850
it has to be there.

04:53.910 --> 04:58.800
So we separate the obfuscate that part and this part.

04:59.850 --> 05:03.480
So from the email we are getting the parts.

05:03.510 --> 05:15.270
Now the username is the first part which would be indexed with zero and domain is the second part.

05:17.220 --> 05:23.910
So just to be sure we ask for the part with index one or if there is none.

05:24.180 --> 05:27.510
Somehow someone sneaked in an incorrect email.

05:27.540 --> 05:30.270
We get the default, which is an empty string.

05:31.440 --> 05:36.450
Okay, now let's obfuscate the username because I think we can keep the domain.

05:36.450 --> 05:38.280
That's not secret.

05:40.290 --> 05:42.360
So I can overwrite username.

05:42.360 --> 05:44.070
I think this is fine.

05:44.100 --> 05:49.350
So maybe let's get first two characters from the username.

05:49.350 --> 05:58.450
So starting at the index zero and taking two characters, we're going to join that with an STR repeat

05:58.450 --> 06:02.350
call where we repeat the asterisk.

06:02.470 --> 06:05.890
And then again we're going to get the str Len.

06:05.890 --> 06:12.550
So the length of the username minus two.

06:12.580 --> 06:19.930
We do that because here we just got the first two characters from the username.

06:19.960 --> 06:25.180
Now it might happen that the username is shorter than this.

06:25.180 --> 06:33.490
And for that cases not to have a negative number we're going to use the max function to pick the value

06:33.520 --> 06:34.270
that is bigger.

06:34.270 --> 06:39.100
That would be either zero or the length of the string minus two.

06:39.130 --> 06:48.110
So in case this ends up being negative, we just say that we repeat this asterisk zero Times.

06:49.640 --> 06:50.420
Okay.

06:50.450 --> 06:52.250
Let's add a semicolon.

06:52.760 --> 07:01.280
And finally we just need to return the username at domain.

07:02.060 --> 07:03.140
And we are done.

07:03.170 --> 07:05.300
Now let's see if this works fine.

07:05.330 --> 07:07.880
So where is the email.

07:08.630 --> 07:09.470
It's here.

07:09.470 --> 07:14.630
So I'm calling the obfuscate email and passing the data.

07:17.390 --> 07:19.910
And this is how it looks like I think it's fine.

07:19.910 --> 07:25.190
We just get two characters from the email and the rest are the asterisks.

07:25.190 --> 07:31.880
We don't need to match the amount of characters ideally, because it doesn't really matter.

07:32.060 --> 07:39.920
The most important part is that we hide some sensitive information to which we have access to, but

07:39.920 --> 07:43.070
our visitors don't need that.

07:44.570 --> 07:46.400
Okay, we are done.
