#!/usr/bin/perl use strict; use Saffron; use DBU; use CGI; use Daemon::Utils; use HTML::Template; use Date::Manip; # Prepare variables my $cgi = new CGI; my %in = $cgi->Vars; my $cookie = 0; eval { # Connect my $db = &Saffron::db_get_write(); $db->connect(); if ( !$in{C} ) { &show_form( \%in, $db ); } elsif ( $in{C} eq 'confirm' ) { &confirm( \%in, $db ); } elsif ( $in{C} eq 'cancel' ) { &cancel( \%in, $db ); } $db->disconnect(); }; if ($@) { &Saffron::fatal_error("$ENV{SCRIPT_NAME}: $@"); } ##### Subs #################### sub show_form { my ($in, $db) = @_; my $tmpl = HTML::Template->new(filename => 'show_form.html', path => 'tmpl/cancel', die_on_bad_params => 0); print $cgi->header, $tmpl->output; } sub confirm { my ($in, $db) = @_; my $user = _verify($in, $db); if ( $user->{SUBPLAN} eq 'TERM' || $user->{SUBPLAN} eq 'TRIAL' || $user->{SUBPLAN} eq 'SECOND' ) { my $charge = &Saffron::load_charge_from_plan($db, $user->{PLAN}, 'AFTER'); if ( $charge->{AMOUNT} ) { ( $user->{_after}, $user->{_amount}, $user->{_len} ) = ( 1, $charge->{AMOUNT}, $charge->{LEN} ) } } # Get the other sites he gets for free and show them before he cancels my @other = (); my $sth = $db->query("select NAME, DESCR, ICON, MEMBERS_URL from Sites where CODE in ('WCP', 'SP', 'PLX', 'JOP', 'DL', 'SL')"); while (my $one = $sth->fetchrow_hashref) { push @other, $one } $sth->finish; my $tmpl = HTML::Template->new(filename => 'confirm.html', path => 'tmpl/cancel', die_on_bad_params => 0); $tmpl->param( sites => &Saffron::user_site_access($db, $user), other => \@other, %{$user} ); print $cgi->header, $tmpl->output; } sub cancel { my ($in, $db) = @_; my $user = _verify($in, $db); my $now = time; my $reason = $db->quote($in->{reason}); if ( $in->{after} ) { $db->do("update Users set SUBPLAN = 'AFTER', CANCEL_DT = $now where ID = $user->{ID}"); } else { $db->do("update Users set CANCELLED = 1, CANCEL_DT = $now, CANCEL_WHY = $reason where ID = $user->{ID}"); } my $tmpl = HTML::Template->new(filename => 'cancel.html', path => 'tmpl/cancel', die_on_bad_params => 0); $tmpl->param( sites => &Saffron::user_site_access($db, $user), _edt => &UnixDate(&ParseDate("epoch $user->{EDT}"), "%m/%d/%Y"), _cancel => $in{after} ? 0 : 1, %{$user} ); print $cgi->header, $tmpl->output; } sub _verify { my ($in, $db) = @_; # Error checking if ( ! $in->{username} ) { &error('Enter your username') } if ( ! $in->{password} ) { &error('Enter your password') } if ( ! $in->{email} ) { &error('Enter your email address') } my $user = &Saffron::load_something($db, "select * from Users where USERNAME = '$in->{username}' order by DT desc limit 0, 1"); if (!$user->{USERNAME}) { &error("Username not found."); } # Find if we are responsible for rebills and cancellations my $merchant = &Saffron::load_something($db, "select * from Merchants where CODE = '$user->{MER}'"); if (! $merchant->{WE_REBILL}) { my $tmpl = HTML::Template->new(filename => 'thirdparty.html', path => 'tmpl/cancel', die_on_bad_params => 0); $tmpl->param(%{$merchant}); print $cgi->header, $tmpl->output; &Saffron::quit(); } if ( $user->{PASSWORD} ne $in->{password} ) { &error('Wrong password') } if ( lc($user->{EMAIL}) ne lc($in->{email}) ) { &error('The email address is not the one on record', 'tmpl/cancel/error.html') } if ( $user->{CANCELLED} ) { &error('Your membership is already cancelled and your access to the site will stop on ' . &UnixDate(&ParseDate("epoch $user->{EDT}"), "%l")) } return $user; } sub error ($) { my ($error) = @_; my $tmpl = HTML::Template->new(filename => 'error.html', path => 'tmpl/cancel', die_on_bad_params => 0); $tmpl->param( error => $error, ); print $cgi->header, $tmpl->output; &Saffron::quit; }