#!/usr/bin/perl # # callpricer.cgi -- call pricer for the Dialabroad Europe cheap call # service. # # Copyright 2009-2010, Nicholas Johnston # # Development history: # - 16 May 2010: optionally include calling from country too, to improve # SEO. # - 11 February 2009: fixed a bug in producing the error page after # Alistair spotted it. # - 7 February 2009: development started, based on callpricer.cgi used for # the UK service. # BEGIN { use CGI::Carp qw(fatalsToBrowser); unshift @INC, "/home/deeplake/www/spaincc/lib"; # for extra modules } use strict; use CheapcallsEurope; use CGI; # Some destinations (currently Nigeria only) can't be called when you # top-up online, so make this clear. my @NOT_CALLABLE_ONLINE_TOPUP_DESTS = qw(Nigeria); use constant TOPUP_VALUE => 500; # i.e. 5 EUR use constant CONNECTION_FEE => 5; # i.e. 0.05 EUR my $cgi = CGI->new; my $destination = $cgi->param('destination'); my $from = $cgi->param('from'); my $from_iso; if (!$destination) { error("Invalid destination."); } if ($from) { $from_iso = $from; $from = CheapcallsEurope::calls_available_from($from) || ''; $from_iso = '' unless $from; } # Get destinations my %dests = CheapcallsEurope::get_destinations_for_callpricer(); # Build up a country to ITU country code mapping my %dialling_codes = CheapcallsEurope::get_country_to_itu_dialling_code_mapping(); my $dialling_code = $dialling_codes{$destination}; $dialling_code = "00 $dialling_code" if $dialling_code; # Check that the destination is sane if (! exists $dests{$destination}) { error("Invalid destination."); } # Get the rates my ($landline_rate, @other_dests) = process_destinations($destination, %dests); my $no_online_topup = 0; foreach my $bad_dest (@NOT_CALLABLE_ONLINE_TOPUP_DESTS) { $bad_dest = quotemeta $bad_dest; if ($destination =~ /$bad_dest/i) { $no_online_topup = 1; } } # How many minutes can they get for their topup? my $num_minutes = int( (TOPUP_VALUE - CONNECTION_FEE) / $landline_rate); # Create the page my $template = HTML::Template->new( filename => $from ? 'template_callpricer_with_calling_from.tmpl' : 'template_callpricer.tmpl', die_on_bad_params => 0, loop_context_vars => 1, ); CheapcallsEurope::_populate_standard_template_items($template, $from); $template->param(country => $destination); $template->param(dialling_code => $dialling_code); $template->param(landline_rate => $landline_rate); $template->param(other_dests => \@other_dests); $template->param(has_other_dests => @other_dests ? 1 : 0); # HTML::Template # only accepts # refs for loops $template->param(num_minutes => $num_minutes); if ($from) { $template->param(from => $from); $template->param(access_number => CheapcallsEurope::get_access_number_by_country($from_iso)); } print "content-type: text/html\n\n" . $template->output(); sub process_destinations { my ($destination, %dests) = @_; my (@other_dests, $landline_rate); foreach my $curr_dest (@{ $dests{$destination} }) { if ($curr_dest->{dest} eq $destination) { # Figure out the landline rate $landline_rate = round($curr_dest->{rate}); } else { # And get details of the other destinations (e.g. mobiles, cheaper # cities and so on) within the country. push @other_dests, { other_dest => $curr_dest->{dest}, rate => round($curr_dest->{rate}), } } } return ($landline_rate, @other_dests); } sub error { my $message = $_[0]; my $template = HTML::Template->new( filename => 'template_general_page.tmpl', loop_context_vars => 1, die_on_bad_params => 0, ); CheapcallsEurope::_populate_standard_template_items($template); $template->param(heading => "Error"); $template->param(content => $message); print "content-type: text/html\n\n" . $template->output(); exit; } sub round { # Handle prices in either cents or fractions of whole Euro return sprintf '%0.2f', ($_[0] > 1 ? $_[0] / 100 : $_[0]); }