#!/usr/bin/perl

print "Content-type: text/plain\n\n";
print( "Taken down temporarily due to spamming" );
exit();
use strict;
use DBI;
#use CGI;

our $subjects = {'chip'=>"Chip Template Engine",
								'spawn'=>"Spawn SQL",
								'page'=>"this webpage"};
our $categories = {'bug'=>"bugs",
								'feature'=>"feature requests"};

push( @INC, "/var/www/localhost/code" );
require "common.pm";
require "chip_gui.pm";

	#print "Content-type: text/html\n\n";
local our $gui = new Chip;
	#while( (my $key, my $value)=each(%{ $Chip::_template_cache }) )
	#{
	#	print( "template::$key -->\n$value\n\n\n\n" );
	#}
	#exit();
$gui->{'gui_directory'} = "/var/www/localhost/code/gui";
$gui->{'pl_directory'} = "/var/www/localhost/code/perl";

# Parse the required files
$gui->parse_file( "chip" );
$gui->parse_file( "wishlist" );

# Set up the DB
local our $dbh = DBI->connect( "dbi:mysql:code;localhost", "code", "antikkkx" );

our %http_vars = &std::get_http_vars;

# Perform based on the variable "a"
if( $http_vars{'a'}==1 ){ print_form(); }
elsif( $http_vars{'a'}==2 ){ add_item(); }
elsif( $http_vars{'a'}==3 ){ vote(); }
else{ list_items(); }

$dbh->disconnect();

sub print_form()
{
	print "Content-type: text/html\n\n";
	
	$gui->set_var( "subjects", $subjects );
	$gui->set_var( "categories", $categories );
	$gui->{'variables'}->{'action'} = $ENV{'SCRIPT_NAME'}."?a=2";
	$gui->output_template( "form" );
}
sub add_item()
{
	my $query = "INSERT INTO wishlist (name, email, body, date, subject, category) VALUES(?, ?, ?, ?, ?, ?);";

	$dbh->do( $query, undef, $http_vars{'name'},
														$http_vars{'email'},
														$http_vars{'body'},
														time(),
														$http_vars{'subject'},
														$http_vars{'category'} );

	print( "Location: $ENV{'SCRIPT_NAME'}\n\n" );
}
sub list_items()
{
	#print "Content-type: text/html\n\n";
	#while( (my $key, my $value)=each(%{ $Chip::_template_cache }) )
	#{
	#	print( "template::$key -->\n$value\n\n\n\n" );
	#}
	#while( (my $key, my $value)=each(%{ $gui->{'variables'} }) )
	#{
	#	print( "variable::$key-->$value\n\n" );
	#}
	#exit();

	# Prepare the query
	my $query = "SELECT * FROM wishlist";
	my $where = "";
	
	# Check to see if we should narrow down the subject 
	if( $http_vars{'subject'} and !($http_vars{'subject'} eq "all") )
	{
		$where.= "subject=\"$http_vars{'subject'}\"";
		$gui->set_var( "current_subject", $http_vars{'subject'} );
	}
	else{ $gui->set_var( "current_subject", "all" ); }
	if( $http_vars{'category'} and !($http_vars{'category'} eq "all")  )
	{
		$where.= " AND" if( $where );
		$where.= " category=\"$http_vars{'category'}\"";
		$gui->set_var( "current_category", $http_vars{'category'} );
	}
	else{ $gui->set_var( "current_category", "all" ); }
	
	$query.= " WHERE ".$where if( $where );
		
	my $query_result = $dbh->prepare( $query );
	$query_result->execute();

	$gui->{'variables'}->{'vote_url'} = append_url( "a", 3, "subject", $http_vars{'subject'},
																												"category", $http_vars{'category'} );

	$gui->{'variables'}->{'noitem'} = 1;
	while( my $row = $query_result->fetchrow_hashref() )
	{
		$gui->{'variables'}->{'noitem'} = 0;
		$row->{'email'}=~ s/@/ at /;
		$row->{'email'}=~ s/\./ dot /;
		$row->{'date'} = gmtime( $row->{'date'} );
		$row->{'subject'} = $subjects->{ $row->{'subject'} };
		$row->{'category'} = $categories->{ $row->{'category'} };
		$gui->set_var( "item", $row );
		$gui->parse_template( "list_item" );
	}

	$query_result->finish();

	$subjects->{'all'} = "All";
	$categories->{'all'} = "All";
	$gui->set_var( "subjects", $subjects );
	$gui->set_var( "categories", $categories );
	$gui->{'variables'}->{'add_item_url'} = $ENV{'SCRIPT_NAME'}."?a=1";
	$gui->{'variables'}->{'subject_change_url'} = $ENV{'SCRIPT_NAME'};

	print "Content-type: text/html\n\n";
	$gui->output_template( "list" );
}
sub vote()
{
	if( $http_vars{'is_important'} eq "yes" )
	{
		$dbh->do( "UPDATE wishlist SET votes_yes=votes_yes+1 WHERE wishlist_id=$http_vars{'wishlist_id'}" );
	}
	elsif( $http_vars{'is_important'} eq "no" )
	{
		$dbh->do( "UPDATE wishlist SET votes_no=votes_no+1 WHERE wishlist_id=$http_vars{'wishlist_id'}" );
	}

	my $relocate_url = append_url( "subject", $http_vars{'subject'},
																	"category", $http_vars{'category'} );

	print( "Location: $relocate_url\n\n" );
}


sub append_url
{
	my %url_extra = @_;

	my $url_cache = "";

	# Break it up some
	while( (my $key, my $value) = each( %url_extra ) )
	{
		if( $value )
		{
			$url_cache.= "&" if( length($url_cache) );
			$url_cache.= encode_url($key)."=".encode_url($value);
		}
	}

	return( $ENV{'SCRIPT_NAME'}. ( length($url_cache) ? "?".$url_cache : "" ) );
}

sub encode_url
{
	$_ = $_[0];

	s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/eg;

	$_;
}

