commit 47b0ba16de6a9d7bfe19b570454241c50ab1ba67 from: monaco via: GitHub date: Fri Jun 23 20:21:42 2023 UTC Create paste.pl commit - 4243115b63795f5668d0002d76c736d6543d2488 commit + 47b0ba16de6a9d7bfe19b570454241c50ab1ba67 blob - /dev/null blob + 0bd76e451a4501c623731295031a734b83d5121d (mode 644) --- /dev/null +++ paste.pl @@ -0,0 +1,181 @@ +#!/usr/bin/env perl + +use Mojolicious::Lite; +use DBI; +use File::Slurp; + +# set up the sqlite database +my $dbh = DBI->connect("dbi:SQLite:dbname=pastes.db","","", { RaiseError => 1, AutoCommit => 1 }); +$dbh->do("CREATE TABLE IF NOT EXISTS pastes (id INTEGER PRIMARY KEY, content TEXT, expires TIMESTAMP)"); + +# set up the routes +get '/' => sub { + my $c = shift; + $c->render(template => 'index'); +}; + +post '/' => sub { + my $c = shift; + my $content = $c->param('content'); + + # Trim leading and trailing whitespace + $content =~ s/^\s+|\s+$//g; + + if ($content =~ /\w/) { # Check if content contains any word character + my $expires = time() + (60 * 60 * 24 * 60); # 60 days from now + my $id = int(rand(1000000)); + my $sth = $dbh->prepare("INSERT INTO pastes (id, content, expires) VALUES (?, ?, ?)"); + $sth->execute($id, $content, $expires); + write_file("pastes/$id.txt", $content); + $c->redirect_to("/$id"); + } else { + $c->render(text => "Invalid paste content"); + } +}; + +get '/:id' => sub { + my $c = shift; + my $id = $c->param('id'); + my $sth = $dbh->prepare("SELECT * FROM pastes WHERE id = ?"); + $sth->execute($id); + my $paste = $sth->fetchrow_hashref; + if ($paste) { + $paste->{expires_human} = format_expires($paste->{expires}); + $c->render(template => 'paste', paste => $paste); + } else { + $c->render(text => "Paste not found"); + } +}; + +get '/raw/:id' => sub { + my $c = shift; + my $id = $c->param('id'); + my $sth = $dbh->prepare("SELECT content FROM pastes WHERE id = ?"); + $sth->execute($id); + my ($content) = $sth->fetchrow_array; + if ($content) { + $c->res->headers->content_type('text/plain'); + $c->render(text => $content); + } else { + $c->render(text => "Paste not found"); + } +}; + +# start the app +app->start; + +sub format_expires { + my $expires = shift; + my $current_time = time(); + + if ($expires > $current_time) { + my $remaining_seconds = $expires - $current_time; + my $remaining_days = int($remaining_seconds / (60 * 60 * 24)); + return "In $remaining_days days"; + } else { + return "Expired"; + } +} + +__DATA__ + +@@ index.html.ep + + + + KISSmo Perl Version 0.7 stable + + + + +
+

KISSmo Perl Version 0.7 stable

+
+
+ +
+
+ +
+
+
+
+

This pastebin script has been developed by Arianit. The source code can be found at GitHub.

+
+ + + + + +@@ paste.html.ep + + + + Paste + + + + +
+

Paste

+
<%= $paste->{content} %>
+ RAW +

Expires: <%= $paste->{expires_human} %>

+
+
+

This pastebin script has been developed by Arianit. The source code can be found at GitHub.

+
+ + + +