From 152218ae18c000e8bb2e9a5982353e92cf6fa25c Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Fri, 1 Nov 2019 14:22:53 +0100 Subject: Just giving the executables file-endings --- form | 81 ------------------------------------------------------------ form.cgi | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ redirect | 43 -------------------------------- redirect.cgi | 43 ++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 124 deletions(-) delete mode 100755 form create mode 100755 form.cgi delete mode 100755 redirect create mode 100755 redirect.cgi diff --git a/form b/form deleted file mode 100755 index e10386d..0000000 --- a/form +++ /dev/null @@ -1,81 +0,0 @@ -#!/bin/perl -wT -# (c) 2019 Dennis Eriksen - -use strict; -use warnings; - -use CGI; -use DBI; - -my $q = CGI->new; # create CGI object - -print $q->header(-charset=>'utf-8'); -print < - - PURL - - -HTML -my $user = scalar $ENV{'REMOTE_USER'}; - -# If form has already been submitted -if ($q->param('url')) { - my $url = scalar $q->param('url'); - my $short; - - if ($q->param('short')) { - $short = scalar $q->param('short'); - } else { - $short = genshort(); - } - - # create database handler - my $dbh = DBI->connect("dbi:Pg:dbname=purl") or die $DBI::errstr; - - # SQL Query - my $query = qq(SELECT shorts.url FROM shorts WHERE shorts.short = ?;); - - # Check if short exists. If it does, generate a new one. - while (my $url = $dbh->selectrow_array($query, undef, $short)) { - print "Your short exists. Generating new.
\n"; - $short = genshort(); - last; - } - - # Insert short and url into database - $query = qq(INSERT INTO shorts (url, short, created_by) VALUES (?, ?, ?);); - my $sth = $dbh->prepare( $query ); - my $rv = $sth->execute($url, $short, $user) or die $sth->errstr; - $dbh->disconnect(); - - - my $proto = $ENV{'HTTPS'} eq "on" ? 'https://' : 'http://'; - my $shortURL = $proto . $ENV{'SERVER_NAME'} . '/' . $short; - print "URL: $url
\n"; - print "Shortened to: $shortURL
\n"; - print "
\n"; -} - -# print form -print < - Username: $user
- URL to shorten:
- Custom short:
- - - - - -FORM - -exit(0); - -sub genshort { - my $random_number = int(rand(3)) +2; # random length of short - my @chars = ("A".."Z", "a".."z", 0..9); - my $short; - $short .= $chars[rand @chars] for 1..$random_number; # generate short - return $short; -} diff --git a/form.cgi b/form.cgi new file mode 100755 index 0000000..e10386d --- /dev/null +++ b/form.cgi @@ -0,0 +1,81 @@ +#!/bin/perl -wT +# (c) 2019 Dennis Eriksen + +use strict; +use warnings; + +use CGI; +use DBI; + +my $q = CGI->new; # create CGI object + +print $q->header(-charset=>'utf-8'); +print < + + PURL + + +HTML +my $user = scalar $ENV{'REMOTE_USER'}; + +# If form has already been submitted +if ($q->param('url')) { + my $url = scalar $q->param('url'); + my $short; + + if ($q->param('short')) { + $short = scalar $q->param('short'); + } else { + $short = genshort(); + } + + # create database handler + my $dbh = DBI->connect("dbi:Pg:dbname=purl") or die $DBI::errstr; + + # SQL Query + my $query = qq(SELECT shorts.url FROM shorts WHERE shorts.short = ?;); + + # Check if short exists. If it does, generate a new one. + while (my $url = $dbh->selectrow_array($query, undef, $short)) { + print "Your short exists. Generating new.
\n"; + $short = genshort(); + last; + } + + # Insert short and url into database + $query = qq(INSERT INTO shorts (url, short, created_by) VALUES (?, ?, ?);); + my $sth = $dbh->prepare( $query ); + my $rv = $sth->execute($url, $short, $user) or die $sth->errstr; + $dbh->disconnect(); + + + my $proto = $ENV{'HTTPS'} eq "on" ? 'https://' : 'http://'; + my $shortURL = $proto . $ENV{'SERVER_NAME'} . '/' . $short; + print "URL: $url
\n"; + print "Shortened to: $shortURL
\n"; + print "
\n"; +} + +# print form +print < + Username: $user
+ URL to shorten:
+ Custom short:
+ + + + + +FORM + +exit(0); + +sub genshort { + my $random_number = int(rand(3)) +2; # random length of short + my @chars = ("A".."Z", "a".."z", 0..9); + my $short; + $short .= $chars[rand @chars] for 1..$random_number; # generate short + return $short; +} diff --git a/redirect b/redirect deleted file mode 100755 index c471831..0000000 --- a/redirect +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/perl -wT -# (c) 2019 Dennis Eriksen - -use strict; -use warnings; - -use CGI; -use DBI; - -my $q = CGI->new; # create CGI object - -# create database handler -my $dbh = DBI->connect("dbi:Pg:dbname=purl") or die $DBI::errstr; - -# set the short -my $short = $ENV{REQUEST_URI}; -$short =~ s/^\///; - -# SQL Query -my $query = qq(SELECT shorts.url FROM shorts WHERE shorts.short = ?;); - -# run the query, and do an if on it -if (my $url = $dbh->selectrow_array($query, undef, $short)) { - $query = qq(UPDATE shorts SET count = count + 1, last_visited = now() WHERE short = ?;); - my $sth = $dbh->prepare( $query ); - my $rv = $sth->execute( $short ) or die $sth->errstr; - print $q->redirect($url); -} -else { - print $q->header(-status=>'404 Not found',-charset=>'utf-8'); - print < -404 Not Found - -

404 Not Found

-
nginx
- - -notfound -} - -# disconnect from database -$dbh->disconnect(); diff --git a/redirect.cgi b/redirect.cgi new file mode 100755 index 0000000..c471831 --- /dev/null +++ b/redirect.cgi @@ -0,0 +1,43 @@ +#!/bin/perl -wT +# (c) 2019 Dennis Eriksen + +use strict; +use warnings; + +use CGI; +use DBI; + +my $q = CGI->new; # create CGI object + +# create database handler +my $dbh = DBI->connect("dbi:Pg:dbname=purl") or die $DBI::errstr; + +# set the short +my $short = $ENV{REQUEST_URI}; +$short =~ s/^\///; + +# SQL Query +my $query = qq(SELECT shorts.url FROM shorts WHERE shorts.short = ?;); + +# run the query, and do an if on it +if (my $url = $dbh->selectrow_array($query, undef, $short)) { + $query = qq(UPDATE shorts SET count = count + 1, last_visited = now() WHERE short = ?;); + my $sth = $dbh->prepare( $query ); + my $rv = $sth->execute( $short ) or die $sth->errstr; + print $q->redirect($url); +} +else { + print $q->header(-status=>'404 Not found',-charset=>'utf-8'); + print < +404 Not Found + +

404 Not Found

+
nginx
+ + +notfound +} + +# disconnect from database +$dbh->disconnect(); -- cgit v1.2.3