#!/usr/bin/perl -w # # CrossSell.pm: # Adverts from FixMyStreet to another site. # # Unlike the PHP crosssell script, returns strings rather than prints them; # and currently displays the same advert if e.g. there's a connection problem. # # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org. WWW: http://www.mysociety.org # # $Id: CrossSell.pm,v 1.10 2008-02-07 17:53:52 matthew Exp $ # Config parameters site needs set to call these functions: # OPTION_AUTH_SHARED_SECRET # OPTION_HEARFROMYOURMP_BASE_URL package CrossSell; use strict; use LWP::Simple qw($ua get); $ua->timeout(5); use URI::Escape; use mySociety::AuthToken; use mySociety::Web qw(ent); sub display_random_hfymp_advert { my ($email, $name, $text) = @_; $name ||= ''; my $auth_signature = mySociety::AuthToken::sign_with_shared_secret($email, mySociety::Config::get('AUTH_SHARED_SECRET')); # See if already signed up my $url = mySociety::Config::get('HEARFROMYOURMP_BASE_URL'); my $already_signed = get($url . '/authed?email=' . uri_escape($email) . "&sign=" . uri_escape($auth_signature)); # Different from PHP version; display this advert if e.g. connection problem return '' if $already_signed && $already_signed eq 'already signed'; $email = ent($email); $name = ent($name); $auth_signature = ent($auth_signature); $text =~ s#\[form\]#

#; return ''; } sub display_random_gny_advert { my ($email, $name, $text) = @_; return ''; } sub display_random_twfy_alerts_advert { my ($email, $name, $text) = @_; my $auth_signature = mySociety::AuthToken::sign_with_shared_secret($email, mySociety::Config::get('AUTH_SHARED_SECRET')); $text =~ s#\[button\]#

#; return ''; } sub display_hfyc_cheltenham_advert { my ($email, $name) = @_; $name ||= ''; my $auth_signature = mySociety::AuthToken::sign_with_shared_secret($email, mySociety::Config::get('AUTH_SHARED_SECRET')); # See if already signed up my $already_signed = get('http://cheltenham.hearfromyourcouncillor.com/authed?email=' . uri_escape($email) . "&sign=" . uri_escape($auth_signature)); # Different from PHP version; display this advert if e.g. connection problem return '' if $already_signed && $already_signed eq 'already signed'; # If not, display advert $email = ent($email); $name = ent($name); $auth_signature = ent($auth_signature); my $out = < EOF return ($out, "cheltenhamhfyc$rand"); } # Choose appropriate advert and display it. # $this_site is to stop a site advertising itself. sub display_advert ($$;$%) { my ($q, $email, $name, %data) = @_; if (defined $data{council} && $data{council} eq '2326') { my ($out, $ad) = display_hfyc_cheltenham_advert($email, $name); if ($out) { $q->{scratch} = "advert=$ad"; return $out; } } my @adverts = ( [ 'gny0', '

Are you a member of a local group…

…which uses the internet to coordinate itself, such as a neighbourhood watch? If so, please help the charity that runs FixMyStreet by adding some information about it to our new site, GroupsNearYou.' ], # Since you're interested in your local area, why not # start a long term relationship with your MP? [ 'hfymp0', '

Get email from your MP in the future

and have a chance to discuss what they say in a public forum [form]Sign up to HearFromYourMP[/form]' ], [ 'twfy_alerts0', '

Get emailed every time your MP says something in Parliament

[button]Keep an eye on them for free![/button]' ], ); while (@adverts) { my $rand = int(rand(scalar @adverts)); next unless $adverts[$rand]; my ($advert_id, $advert_text) = @{$adverts[$rand]}; (my $advert_site = $advert_id) =~ s/\d+$//; my $func = "display_random_${advert_site}_advert"; no strict 'refs'; my $out = &$func($email, $name, $advert_text); use strict 'refs'; if ($out) { $q->{scratch} = "advert=$advert_id"; return $out; } for (my $i=0; $i<@adverts; $i++) { (my $a = $adverts[$i][0]) =~ s/\d+$//; delete $adverts[$i] if $advert_site eq $a; } } $q->{scratch} = 'advert=pb'; return <