<commit_before>#!/usr/bin/perl 

#CGI Interface to paste.debian.net
#Copyright (C) 2007  <NAME> <<EMAIL>>
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as
#published by the Free Software Foundation, either version 3 of the
#License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU Affero General Public License for more details.
#
#You should have received a copy of the GNU Affero General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.


use strict; 
use lib 'lib/';
use warnings;
use CGI qw(:standard);
use Template;
use POSIX;
use CGI::Carp qw(fatalsToBrowser); 
use CGI::Cookie;
use Digest::SHA1 qw (sha1_hex);
use Paste;
use subs qw(error);


my $template = Template->new ( { INCLUDE_PATH => 'templates', PLUGIN_BASE => 'Paste::Template::Plugin', } );


my $config_file = 'paste.conf'; 
my $paste;
eval {
	$paste = new Paste($config_file);
};
error("Fatal Error", $@) if $@;

my $dbname = $paste->get_config_key('database', 'dbname') || die "Databasename not specified";  
my $dbuser = $paste->get_config_key('database', 'dbuser') || die "Databaseuser not specified"; 
my $dbpass = $paste->get_config_key('database', 'dbpassword') || ''; 
#config 
my $base_url = $paste->get_config_key('www', 'base_url');


my $cgi = new CGI();


if ($cgi->param("plain")) {
	print_plain($cgi);
} elsif ($cgi->param("download")) {
	print_download($cgi);
} elsif ($cgi->param("show")) {
	print_show($cgi);
} elsif ($cgi->param("private")) {
	print_show($cgi,1); 
} elsif ($cgi->param("delete")){
	print_delete($cgi); 
} elsif ($cgi->param("comment")) {
	print_add_comment($cgi); 
} elsif ($cgi->param("show_template")) {
	print_template($cgi);
} else {
	print_paste($cgi);
}

exit;

sub print_plain {
	my ($cgi,$status) = (@_);
	my $id = ''; 
	if ($cgi->param("plain")) {
		 $id = $cgi->param("plain");
		 #sanitizing
		 $id =~ s/[^0-9]+//g;
	}
	my $paste = $paste->get_paste($id);
	if (! $paste) {
		 error("Entry not found", "Your requested paste entry '$id' could not be found");
	}
	print "Content-type: text/plain\r\n\r\n";
	print $paste->{code}; 
}

sub print_download {
	my ($cgi,$status) = (@_);
	my $id = ''; 
	if ($cgi->param("download")) {
		 $id = $cgi->param("download");
		 #sanitizing
		 $id =~ s/[^0-9]+//g;
	} else {
		print_paste($cgi);
	}

	my $paste = $paste->get_paste($id);

	if (! $paste) {
        error("Entry not found", "Your requested paste entry '$id' could not be found");
    }
	print "Content-type: text/plain\n";
	print "Content-Transfer-Encoding: text\n";
	print "Content-Disposition: attachment; filename=paste_$id\n";
	print "\r\n";
	print $paste->{code};
}

sub print_delete {
	my ($cgi) = (@_);
	my $digest = '';
	if ($cgi->param("delete")) {
		$digest = $cgi->param("delete"); 
	} else {
		print_paste($cgi);
	}

	my $id = $paste->delete_paste($digest); 
	if (! $paste->error) {
		print_header();
		$template->process('show_message', {    "dbname" => "dbi:Pg:dbname=$dbname",
				"dbuser" => $dbuser,
				"dbpass" => $dbpass,
				"title" => "Entry $id deleted",
				"message" => "The entry with the id $id has been deleted.",
				"round" => sub { return floor(@_); },
				"base_url" => $base_url,
			}
		) or die $template->error() . "\n";
	} else {
		error("Entry could not be deleted", $paste->error);
	}
}


sub print_add_comment {
	my ($cgi) = (@_);

	my $error; 
	my $comment = $cgi->param("comment") or $error = "Please add a comment"; 
	my $paste_id = $cgi->param("paste_id") or $error = "No Paste id found";
	my $name = $cgi->param("poster") || "anonymous";

	if ($error) {
		error ("Could not add comment: <br>\n". $error);
	}

	my $digest;

	$paste->add_comment($comment, $name, $paste_id); 
	if (! $paste->error) {
		print_header();
		$template->process('show', {    "dbname" => "dbi:Pg:dbname=$dbname",
				"dbuser" => $dbuser,
				"dbpass" => $dbpass,
				"show" => $paste_id, 
				"status" => "Your comment has been added to paste entry $paste_id.",
				"round" => sub { return floor(@_); },
				"base_url" => $base_url,
			}
		) or die $template->error() . "\n";
	} else {
		error("Comment could not be added", $paste->error);
	}
}

sub print_template {
    my ($cgi,$status) = (@_);
	my $tmpl;
	my @templates = qw(about clients);

	if ($cgi->param("show_template")) {
		$tmpl = $cgi->param("show_template");
		if (! grep /^$tmpl$/, @templates)
		{
			error("Page not found", "Page not found");
		}
	}
	print_header();
    $template->process($tmpl, {	"dbname" => "dbi:Pg:dbname=$dbname", 
									"dbuser" => $dbuser, 
									"dbpass" => $dbpass,
									"base_url" => $base_url, 
								} 
						) or die $template->error() . "\n";
}


sub print_show {
    my ($cgi,$hidden) = (@_);
	my $id = '';
	my $lines = 1;
	if ($cgi->param("show")) {
		$id = $cgi->param("show");
		#sanitizing
		$id =~ s/[^0-9]+//g;
	} elsif ($cgi->param("hidden")) {
		$id = ld($cgi->param("hidden"));
		$id =~ s/[^0-9a-f]//g; 
	}
	if (defined($cgi->param("lines"))) {
		$lines = $cgi->param("lines"); 
	}
	print_header();
	my $tmpl_name = $hidden ? "hidden" : "show";

    $template->process($tmpl_name, {	"dbname" => "dbi:Pg:dbname=$dbname", 
									"dbuser" => $dbuser, 
									"dbpass" => $<PASSWORD>,
									"show" => $id,
									"status" => $status, 
									"lines" => $lines,
									"round" => sub { return floor(@_); }, 
									"base_url" => $base_url, 
								} 
						) or die $template->error() . "\n";
}

sub print_paste {
	my ($cgi,$status) = (@_);<commit_msg>its called hidden now<commit_after>#!/usr/bin/perl 

#CGI Interface to paste.debian.net
#Copyright (C) 2007  <NAME> <<EMAIL>>
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as
#published by the Free Software Foundation, either version 3 of the
#License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU Affero General Public License for more details.
#
#You should have received a copy of the GNU Affero General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.


use strict; 
use lib 'lib/';
use warnings;
use CGI qw(:standard);
use Template;
use POSIX;
use CGI::Carp qw(fatalsToBrowser); 
use CGI::Cookie;
use Digest::SHA1 qw (sha1_hex);
use Paste;
use subs qw(error);


my $template = Template->new ( { INCLUDE_PATH => 'templates', PLUGIN_BASE => 'Paste::Template::Plugin', } );


my $config_file = 'paste.conf'; 
my $paste;
eval {
	$paste = new Paste($config_file);
};
error("Fatal Error", $@) if $@;

my $dbname = $paste->get_config_key('database', 'dbname') || die "Databasename not specified";  
my $dbuser = $paste->get_config_key('database', 'dbuser') || die "Databaseuser not specified"; 
my $dbpass = $paste->get_config_key('database', 'dbpassword') || ''; 
#config 
my $base_url = $paste->get_config_key('www', 'base_url');


my $cgi = new CGI();


if ($cgi->param("plain")) {
	print_plain($cgi);
} elsif ($cgi->param("download")) {
	print_download($cgi);
} elsif ($cgi->param("show")) {
	print_show($cgi);
} elsif ($cgi->param("hidden")) {
	print_show($cgi,1); 
} elsif ($cgi->param("delete")){
	print_delete($cgi); 
} elsif ($cgi->param("comment")) {
	print_add_comment($cgi); 
} elsif ($cgi->param("show_template")) {
	print_template($cgi);
} else {
	print_paste($cgi);
}

exit;

sub print_plain {
	my ($cgi,$status) = (@_);
	my $id = ''; 
	if ($cgi->param("plain")) {
		 $id = $cgi->param("plain");
		 #sanitizing
		 $id =~ s/[^0-9]+//g;
	}
	my $paste = $paste->get_paste($id);
	if (! $paste) {
		 error("Entry not found", "Your requested paste entry '$id' could not be found");
	}
	print "Content-type: text/plain\r\n\r\n";
	print $paste->{code}; 
}

sub print_download {
	my ($cgi,$status) = (@_);
	my $id = ''; 
	if ($cgi->param("download")) {
		 $id = $cgi->param("download");
		 #sanitizing
		 $id =~ s/[^0-9]+//g;
	} else {
		print_paste($cgi);
	}

	my $paste = $paste->get_paste($id);

	if (! $paste) {
        error("Entry not found", "Your requested paste entry '$id' could not be found");
    }
	print "Content-type: text/plain\n";
	print "Content-Transfer-Encoding: text\n";
	print "Content-Disposition: attachment; filename=paste_$id\n";
	print "\r\n";
	print $paste->{code};
}

sub print_delete {
	my ($cgi) = (@_);
	my $digest = '';
	if ($cgi->param("delete")) {
		$digest = $cgi->param("delete"); 
	} else {
		print_paste($cgi);
	}

	my $id = $paste->delete_paste($digest); 
	if (! $paste->error) {
		print_header();
		$template->process('show_message', {    "dbname" => "dbi:Pg:dbname=$dbname",
				"dbuser" => $dbuser,
				"dbpass" => $dbpass,
				"title" => "Entry $id deleted",
				"message" => "The entry with the id $id has been deleted.",
				"round" => sub { return floor(@_); },
				"base_url" => $base_url,
			}
		) or die $template->error() . "\n";
	} else {
		error("Entry could not be deleted", $paste->error);
	}
}


sub print_add_comment {
	my ($cgi) = (@_);

	my $error; 
	my $comment = $cgi->param("comment") or $error = "Please add a comment"; 
	my $paste_id = $cgi->param("paste_id") or $error = "No Paste id found";
	my $name = $cgi->param("poster") || "anonymous";

	if ($error) {
		error ("Could not add comment: <br>\n". $error);
	}

	my $digest;

	$paste->add_comment($comment, $name, $paste_id); 
	if (! $paste->error) {
		print_header();
		$template->process('show', {    "dbname" => "dbi:Pg:dbname=$dbname",
				"dbuser" => $dbuser,
				"dbpass" => $dbpass,
				"show" => $paste_id, 
				"status" => "Your comment has been added to paste entry $paste_id.",
				"round" => sub { return floor(@_); },
				"base_url" => $base_url,
			}
		) or die $template->error() . "\n";
	} else {
		error("Comment could not be added", $paste->error);
	}
}

sub print_template {
    my ($cgi,$status) = (@_);
	my $tmpl;
	my @templates = qw(about clients);

	if ($cgi->param("show_template")) {
		$tmpl = $cgi->param("show_template");
		if (! grep /^$tmpl$/, @templates)
		{
			error("Page not found", "Page not found");
		}
	}
	print_header();
    $template->process($tmpl, {	"dbname" => "dbi:Pg:dbname=$dbname", 
									"dbuser" => $dbuser, 
									"dbpass" => $<PASSWORD>,
									"base_url" => $base_url, 
								} 
						) or die $template->error() . "\n";
}


sub print_show {
    my ($cgi,$hidden) = (@_);
	my $id = '';
	my $status; 
	my $lines = 1;
	if ($cgi->param("show")) {
		$id = $cgi->param("show");
		#sanitizing
		$id =~ s/[^0-9]+//g;
	} elsif ($cgi->param("hidden")) {
		$id = ld($cgi->param("hidden"));
		$id =~ s/[^0-9a-f]//g; 
	}
	if (defined($cgi->param("lines"))) {
		$lines = $cgi->param("lines"); 
	}
	print_header();
	my $tmpl_name = $hidden ? "hidden" : "show";

    $template->process($tmpl_name, {	"dbname" => "dbi:Pg:dbname=$dbname", 
									"dbuser" => $dbuser, 
									"dbpass" => $<PASSWORD>,
									"show" => $id,
									"status" => $status, 
									"lines" => $lines,
									"round" => sub { return floor(@_); }, 
									"base_url" => $base_url, 
								} 
						) or die $template->error() . "\n";
}

sub print_paste {
	my ($cgi,$status) = (@_);
{'fraction_non_alphanumeric': 0.20663820890321458, 'fraction_numerical': 0.0029619304817492813, 'mean_word_length': 3.6402586903799516, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 166, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6897207', 'n_tokens_mistral': 4814, 'n_tokens_neox': 4197, 'n_words': 1059}