2016-08-31 15:51:39 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
# NOT READY FOR PRODUCTION
|
|
|
|
|
|
|
|
use Mojolicious::Lite;
|
|
|
|
use Data::UUID;
|
|
|
|
use Devel::Dwarn;
|
2016-09-01 12:30:08 +00:00
|
|
|
use Mojo::JSON;
|
2016-08-31 15:51:39 +00:00
|
|
|
|
|
|
|
# connect to database
|
|
|
|
use DBI;
|
|
|
|
|
|
|
|
my $config = plugin Config => {file => 'myapp.conf'};
|
|
|
|
my $dbh = DBI->connect($config->{dsn},$config->{user},$config->{pass}) or die "Could not connect";
|
|
|
|
Dwarn $config;
|
|
|
|
|
|
|
|
# shortcut for use in template
|
|
|
|
helper db => sub { $dbh };
|
|
|
|
|
2016-09-02 11:09:04 +00:00
|
|
|
any '/' => sub {
|
|
|
|
my $self = shift;
|
2016-08-31 15:51:39 +00:00
|
|
|
|
2016-09-02 11:09:04 +00:00
|
|
|
$self->render(text => 'It did not kaboom!');
|
|
|
|
};
|
|
|
|
|
|
|
|
post '/upload' => sub {
|
2016-08-31 15:51:39 +00:00
|
|
|
my $self = shift;
|
2016-09-02 11:09:04 +00:00
|
|
|
# Fetch parameters to write to DB
|
2016-09-05 15:40:41 +00:00
|
|
|
# my $key = $self->param('key');
|
2016-09-02 11:09:04 +00:00
|
|
|
# This will include an if function to see if key matches
|
2016-09-05 15:40:41 +00:00
|
|
|
# unless ($key eq $config->{key}) {
|
|
|
|
# return $self->render( json => { success => Mojo::JSON->false }, status => 403 );
|
|
|
|
# }
|
2016-09-02 11:09:04 +00:00
|
|
|
my $username = $self->param('username');
|
|
|
|
my $company = $self->param('company');
|
|
|
|
my $currency = $self->param('currency');
|
|
|
|
my $file = $self->req->upload('file');
|
|
|
|
# Get image type and check extension
|
|
|
|
my $headers = $file->headers->content_type;
|
|
|
|
# Is content type wrong?
|
|
|
|
if ($headers ne 'image/jpeg') {
|
2016-09-05 15:40:41 +00:00
|
|
|
return $self->render( json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => 'Wrong image extension!',
|
|
|
|
});
|
2016-09-02 11:09:04 +00:00
|
|
|
};
|
|
|
|
# Rewrite header data
|
|
|
|
my $ext = '.jpg';
|
|
|
|
my $uuid = Data::UUID->new->create_str;
|
|
|
|
my $filename = $uuid . $ext;
|
|
|
|
# send photo to image folder on server
|
|
|
|
$file->move_to('images/' . $filename);
|
|
|
|
# send data to foodloop db
|
|
|
|
my $insert = $self->db->prepare('INSERT INTO foodloop (username, company, currency, filename) VALUES (?,?,?,?)');
|
|
|
|
$insert->execute($username, $company, $currency, $filename);
|
2016-08-31 15:51:39 +00:00
|
|
|
$self->render(text => 'It did not kaboom!');
|
2016-09-02 11:09:04 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
post '/register' => sub {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $json = $self->req->json;
|
|
|
|
|
|
|
|
my $account = $self->get_account_by_username( $json->{username} );
|
|
|
|
|
|
|
|
unless ( defined $account ) {
|
|
|
|
return $self->render( json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => 'Username not recognised, has your token expired?',
|
|
|
|
});
|
|
|
|
} elsif ( $account->{keyused} eq 't' ) {
|
|
|
|
return $self->render( json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => 'Token has already been used',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
my $insert = $self->db->prepare("UPDATE accounts SET 'name' = ?, email = ?, postcode = ?, age = ?, gender = ?, grouping = ?, password = ?, keyused = ? WHERE username = ?");
|
|
|
|
$insert->execute(
|
|
|
|
@{$json}{ qw/ name email postcode age gender grouping password / }, 'True', $account->{username},
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->render( json => { success => Mojo::JSON->true } );
|
|
|
|
};
|
|
|
|
|
2016-09-02 13:43:31 +00:00
|
|
|
post '/edit' => sub {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $json = $self->req->json;
|
|
|
|
|
|
|
|
my $account = $self->get_account_by_username( $json->{username} );
|
|
|
|
|
|
|
|
unless ( defined $account ) {
|
|
|
|
return $self->render( json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => 'Username not recognised, has your token expired?',
|
|
|
|
});
|
|
|
|
# PLUG SECURITY HOLE
|
|
|
|
} elsif ( $account->{keyused} ne 't' ) {
|
|
|
|
return $self->render( json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => 'Token has not been used yet!',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
my $insert = $self->db->prepare("UPDATE accounts SET 'name' = ?, postcode = ?, age = ?, gender = ?, WHERE username = ?");
|
|
|
|
$insert->execute(
|
|
|
|
@{$json}{ qw/ name postcode age gender / }, $account->{username},
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->render( json => { success => Mojo::JSON->true } );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-09-02 11:09:04 +00:00
|
|
|
post '/token' => sub {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $json = $self->req->json;
|
|
|
|
|
|
|
|
my $account = $self->get_account_by_token( $json->{token} );
|
|
|
|
|
|
|
|
# TODO change to proper boolean checks
|
|
|
|
if ( ! defined $account || $account->{keyused} eq 't' ) {
|
|
|
|
return $self->render( json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => 'Token is invalid or has already been used',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return $self->render( json => {
|
|
|
|
username => $account->{username},
|
|
|
|
success => Mojo::JSON->true,
|
|
|
|
});
|
2016-08-31 15:51:39 +00:00
|
|
|
};
|
|
|
|
|
2016-09-02 11:09:04 +00:00
|
|
|
sub get_account_by_token {
|
|
|
|
my ( $self, $token ) = @_;
|
|
|
|
|
|
|
|
return $self->db->selectrow_hashref(
|
|
|
|
'SELECT keyused, username FROM accounts WHERE idkey = ?',
|
|
|
|
{},
|
|
|
|
$token,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_account_by_username {
|
|
|
|
my ( $self, $username ) = @_;
|
|
|
|
|
|
|
|
return $self->db->selectrow_hashref(
|
|
|
|
'SELECT keyused, username FROM accounts WHERE username = ?',
|
|
|
|
{},
|
|
|
|
$username,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-31 15:51:39 +00:00
|
|
|
app->start;
|