2022-11-15 23:30:37 +01:00
|
|
|
package BurguillosInfo::Controller::Metrics;
|
|
|
|
|
|
|
|
use v5.34.1;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Data::Dumper;
|
|
|
|
|
|
|
|
use BurguillosInfo::Tracking;
|
|
|
|
|
2023-05-07 06:12:37 +02:00
|
|
|
use Mojo::Base 'Mojolicious::Controller', '-signatures';
|
2022-11-15 23:30:37 +01:00
|
|
|
|
|
|
|
use DateTime::Format::ISO8601;
|
|
|
|
use DateTime::Format::Mail;
|
2022-11-17 00:44:20 +01:00
|
|
|
use Crypt::Bcrypt qw/bcrypt bcrypt_check/;
|
2022-11-15 23:30:37 +01:00
|
|
|
|
|
|
|
my $tracking;
|
2022-11-17 00:44:20 +01:00
|
|
|
|
2023-11-09 17:19:46 +01:00
|
|
|
my $iso8601 = DateTime::Format::ISO8601->new;
|
|
|
|
|
2022-11-15 23:30:37 +01:00
|
|
|
sub request {
|
2023-11-09 17:19:46 +01:00
|
|
|
shift;
|
2023-07-28 22:38:22 +02:00
|
|
|
eval {
|
2023-11-09 17:19:46 +01:00
|
|
|
my $c = shift;
|
2023-07-28 22:38:22 +02:00
|
|
|
my $app = $c->app;
|
2023-11-09 17:19:46 +01:00
|
|
|
if ( !defined $tracking ) {
|
2023-07-28 22:38:22 +02:00
|
|
|
$tracking = BurguillosInfo::Tracking->new($app);
|
|
|
|
}
|
2023-11-09 17:19:46 +01:00
|
|
|
$tracking->register_request($c);
|
2023-07-28 22:38:22 +02:00
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
say STDERR $@;
|
|
|
|
}
|
2022-11-15 23:30:37 +01:00
|
|
|
}
|
2022-11-17 00:44:20 +01:00
|
|
|
|
|
|
|
sub stats {
|
2023-11-09 17:19:46 +01:00
|
|
|
my $self = shift;
|
|
|
|
if ( !$self->valid_login ) {
|
|
|
|
$self->res->headers->location('/stats/login');
|
|
|
|
$self->render( text => 'You must login', status => 302 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$self->session( expiration => 0 );
|
|
|
|
my $data = $tracking->get_global_data($self);
|
|
|
|
my $data_per_url = $tracking->get_data_for_urls($self);
|
|
|
|
my $google_data = $tracking->get_google_data($self);
|
|
|
|
$self->_filter_data_per_url($data_per_url);
|
|
|
|
$self->_filter_data_per_url($google_data);
|
|
|
|
$self->render(
|
|
|
|
tracking_data => $data,
|
|
|
|
tracking_by_url => $data_per_url,
|
|
|
|
google_data => $google_data
|
|
|
|
);
|
2022-11-17 00:44:20 +01:00
|
|
|
}
|
|
|
|
|
2023-11-09 17:19:46 +01:00
|
|
|
sub _filter_data_per_url ( $self, $data_per_url ) {
|
2023-05-07 06:12:37 +02:00
|
|
|
my $filter = $self->param('filter');
|
2023-11-09 17:19:46 +01:00
|
|
|
if ( !defined $filter ) {
|
2023-05-07 06:12:37 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-11-09 17:19:46 +01:00
|
|
|
my @new_data_per_url;
|
|
|
|
if ( $filter eq 'remove-extensions' ) {
|
2023-05-07 06:12:37 +02:00
|
|
|
for my $url (@$data_per_url) {
|
2023-11-09 17:19:46 +01:00
|
|
|
if ( $url->{path} =~ /\.\w+$/ ) {
|
2023-05-07 06:12:37 +02:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
push @new_data_per_url, $url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@$data_per_url = @new_data_per_url;
|
|
|
|
}
|
|
|
|
|
2022-11-17 00:44:20 +01:00
|
|
|
sub submit_login {
|
2023-11-09 17:19:46 +01:00
|
|
|
my $self = shift;
|
|
|
|
if ( $self->valid_login ) {
|
|
|
|
$self->res->headers->location('/stats');
|
|
|
|
$self->render( text => 'Already logged in.', status => 302 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
my $password = $self->param('password');
|
|
|
|
if ( !defined $password ) {
|
|
|
|
$self->render( text => 'No password passed.', status => 400 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
my $bcrypted_pass = $self->config->{bcrypt_pass_stats};
|
|
|
|
if ( !defined $bcrypted_pass ) {
|
|
|
|
warn "No bcrypt pass.";
|
|
|
|
$self->render( text => 'Server error.', status => 500 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
say $password;
|
|
|
|
say $bcrypted_pass;
|
|
|
|
if ( !bcrypt_check( $password, $bcrypted_pass ) ) {
|
|
|
|
$self->render( text => 'Wrong password', status => 401 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
say STDERR 'Login success.';
|
|
|
|
my $expiration_date = DateTime->now->add( days => 1 );
|
|
|
|
$self->session->{login} = "date_end_login:$expiration_date";
|
|
|
|
$self->res->headers->location('/stats');
|
|
|
|
$self->render( text => 'Login success.', status => 302 );
|
|
|
|
return;
|
2022-11-17 00:44:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub valid_login {
|
2023-11-09 17:19:46 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $login_cookie = $self->session->{login};
|
|
|
|
if ( !defined $login_cookie ) {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-17 00:44:20 +01:00
|
|
|
|
2023-11-09 17:19:46 +01:00
|
|
|
my ($date_text) = $login_cookie =~ /^date_end_login:(.*)$/;
|
|
|
|
my $date;
|
|
|
|
eval { $date = $iso8601->parse_datetime($date_text); };
|
|
|
|
if ($@) {
|
|
|
|
warn "Bad date in cookie $login_cookie.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
my $current_date = DateTime->now();
|
|
|
|
if ( $current_date > $date ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return 1;
|
2022-11-17 00:44:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub login {
|
2023-11-09 17:19:46 +01:00
|
|
|
my $self = shift;
|
|
|
|
if ( $self->valid_login ) {
|
|
|
|
$self->res->headers->location('/stats');
|
|
|
|
$self->render( text => 'You are already logged in.', status => 302 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$self->render;
|
2022-11-17 00:44:20 +01:00
|
|
|
}
|
2022-11-15 23:30:37 +01:00
|
|
|
1;
|