diff options
Diffstat (limited to 'perllib/FixMyStreet/Cobrand/UK.pm')
-rw-r--r-- | perllib/FixMyStreet/Cobrand/UK.pm | 92 |
1 files changed, 84 insertions, 8 deletions
diff --git a/perllib/FixMyStreet/Cobrand/UK.pm b/perllib/FixMyStreet/Cobrand/UK.pm index a42ff58a6..988458e0f 100644 --- a/perllib/FixMyStreet/Cobrand/UK.pm +++ b/perllib/FixMyStreet/Cobrand/UK.pm @@ -2,7 +2,11 @@ package FixMyStreet::Cobrand::UK; use base 'FixMyStreet::Cobrand::Default'; use strict; +use Encode; use JSON::MaybeXS; +use LWP::UserAgent; +use Path::Tiny; +use Time::Piece; use mySociety::MaPit; use mySociety::VotingArea; use Utils; @@ -397,9 +401,9 @@ sub link_to_council_cobrand { $handler->moniker ne $self->{c}->cobrand->moniker ) { my $url = sprintf("%s%s", $handler->base_url, $problem->url); - return sprintf("<a href='%s'>%s</a>", $url, $problem->body( $self->{c} )); + return sprintf("<a href='%s'>%s</a>", $url, $problem->body); } else { - return $problem->body( $self->{c} ); + return $problem->body; } } @@ -407,12 +411,6 @@ sub lookup_by_ref_regex { return qr/^\s*(\d+)\s*$/; } -sub category_extra_hidden { - my ($self, $meta) = @_; - return 1 if $meta->{code} eq 'usrn' || $meta->{code} eq 'asset_id'; - return $self->SUPER::category_extra_hidden($meta); -} - sub report_new_munge_before_insert { my ($self, $report) = @_; @@ -422,4 +420,82 @@ sub report_new_munge_before_insert { } } +# To use recaptcha, add a RECAPTCHA key to your config, with subkeys secret and +# site_key, taken from the recaptcha site. This shows it to non-UK IP addresses +# on alert and report pages. + +sub requires_recaptcha { + my $self = shift; + my $c = $self->{c}; + + return 0 if $c->user_exists; + return 0 if !FixMyStreet->config('RECAPTCHA'); + return 0 unless $c->action =~ /^(alert|report|around)/; + return 0 if $c->user_country eq 'GB'; + return 1; +} + +sub check_recaptcha { + my $self = shift; + my $c = $self->{c}; + + return unless $self->requires_recaptcha; + + my $url = 'https://www.google.com/recaptcha/api/siteverify'; + my $res = LWP::UserAgent->new->post($url, { + secret => FixMyStreet->config('RECAPTCHA')->{secret}, + response => $c->get_param('g-recaptcha-response'), + remoteip => $c->req->address, + }); + $res = decode_json($res->content); + $c->detach('/page_error_400_bad_request', ['Bad recaptcha']) + unless $res->{success}; +} + +sub public_holidays { + my $nation = shift || 'england-and-wales'; + my $json = _get_bank_holiday_json(); + return [ map { $_->{date} } @{$json->{$nation}{events}} ]; +} + +sub is_public_holiday { + my %args = @_; + $args{date} ||= localtime; + $args{date} = $args{date}->date; + $args{nation} ||= 'england-and-wales'; + my $json = _get_bank_holiday_json(); + for my $event (@{$json->{$args{nation}}{events}}) { + if ($event->{date} eq $args{date}) { + return 1; + } + } +} + +sub _get_bank_holiday_json { + my $file = 'bank-holidays.json'; + my $cache_file = path(FixMyStreet->path_to("../data/$file")); + my $js; + if (-s $cache_file && -M $cache_file <= 7 && !FixMyStreet->config('STAGING_SITE')) { + # uncoverable statement + $js = $cache_file->slurp_utf8; + } else { + $js = _fetch_url("https://www.gov.uk/$file"); + # uncoverable branch false + $js = decode_utf8($js) if !utf8::is_utf8($js); + if ($js && !FixMyStreet->config('STAGING_SITE')) { + # uncoverable statement + $cache_file->spew_utf8($js); + } + } + $js = JSON->new->decode($js) if $js; + return $js; +} + +sub _fetch_url { + my $url = shift; + my $ua = LWP::UserAgent->new; + $ua->timeout(5); + $ua->get($url)->content; +} + 1; |