<commit_before>#!/usr/bin/perl -w
#
# Cobrand.pm:
# Cobranding for FixMyStreet.
#
# 
# Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved.
# Email: <EMAIL>. WWW: http://www.mysociety.org
#
# $Id: Cobrand.pm,v 1.45 2009-11-24 16:04:22 louise Exp $

package Cobrand;
use strict;
use Carp;

=item get_allowed_cobrands

Return an array reference of allowed cobrand subdomains

=cut
sub get_allowed_cobrands {
    my $allowed_cobrand_string = mySociety::Config::get('ALLOWED_COBRANDS');
    my @allowed_cobrands = split(/\|/, $allowed_cobrand_string);
    return \@allowed_cobrands;
}

=item cobrand_handle Q

Given a query that has the name of a site set, return a handle to the Util module for that
site, if one exists, or zero if not.

=cut
sub cobrand_handle {
    my $cobrand = shift;

    our %handles;

    # Once we have a handle defined, return it.
    return $handles{$cobrand} if defined $handles{$cobrand};

    my $cobrand_class = ucfirst($cobrand);
    my $class = "Cobrands::" . $cobrand_class . "::Util";
    eval "use $class";

    eval{ $handles{$cobrand} = $class->new };
    $handles{$cobrand} = 0 if $@;
    return $handles{$cobrand};
}


=item cobrand_page QUERY

Return a string containing the HTML to be rendered for a custom Cobranded page

=cut
sub cobrand_page {
    my $q = shift;
    my $cobrand = $q->{site};
    my $handle = cobrand_handle($cobrand);
    return 0 unless $handle;
    return $handle->page($q);	
}

=item site_restriction COBRAND COBRAND_DATA

Return a site restriction clause and a site key if the cobrand uses a subset of the FixMyStreet 
data. COBRAND_DATA is any extra data the cobrand needs. Returns an empty string and site key 0 
if the cobrand uses all the data.

=cut
sub site_restriction {
    my ($cobrand, $cobrand_data) = @_;
    my $site_restriction = '';
    my $site_id = 0;
    my $handle = cobrand_handle($cobrand);
    return ($site_restriction, $site_id) unless $handle;
    return $handle->site_restriction($cobrand_data);
}

=item contact_restriction COBRAND

Return a contact restriction clause if the cobrand uses a subset of the FixMyStreet contact data. 

=cut

sub contact_restriction { 
    my ($cobrand) = @_;
    my $handle;
    if ($cobrand){
        $handle = cobrand_handle($cobrand);
    }
    if ( !$cobrand || !$handle || ! $handle->can('contact_restriction')){
        return '';
    }{
        return $handle->contact_restriction();
    }   
}

=item base_url COBRAND

Return the base url for the cobranded version of the site

=cut
sub base_url {
    my $cobrand = shift;
    return mySociety::Config::get('BASE_URL') unless $cobrand;
    my $handle = cobrand_handle($cobrand);
    return mySociety::Config::get('BASE_URL') unless $handle;
    return $handle->base_url();
}

=item base_url_for_emails COBRAND

Return the base url to use in links in emails for the cobranded 
version of the site

=cut

sub base_url_for_emails {
    my ($cobrand, $cobrand_data) = @_;
    my $handle;
    if ($cobrand){
        $handle = cobrand_handle($cobrand);
    }
    if ( !$cobrand || !$handle || ! $handle->can('base_url_for_emails')){
        return base_url($cobrand);
    }{
        return $handle->base_url_for_emails($cobrand_data);
    }
}

=item admin_base_url COBRAND

Base URL for the admin interface.

=cut
sub admin_base_url {
    my ($cobrand) = @_;
    my $handle;
    if ($cobrand){
        $handle = cobrand_handle($cobrand);
    }
    if ( !$cobrand || !$handle || ! $handle->can('admin_base_url')){
        return 0;
    }{
        return $handle->admin_base_url();
    }

}

=item email_host COBRAND

Return the virtual host that sends email for this cobrand

=cut

sub email_host {
    my ($cobrand) = @_;
    my $email_vhost;
    if ($cobrand eq '') {
        $email_vhost = mySociety::Config::get('EMAIL_VHOST');
    } else { 
        $email_vhost = mySociety::Config::get('EMAIL_VHOST_'. uc($cobrand));<commit_msg>Allow rewriting of WTT url where referenced<commit_after>#!/usr/bin/perl -w
#
# Cobrand.pm:
# Cobranding for FixMyStreet.
#
# 
# Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved.
# Email: <EMAIL>. WWW: http://www.mysociety.org
#
# $Id: Cobrand.pm,v 1.46 2009-11-25 18:27:25 louise Exp $

package Cobrand;
use strict;
use Carp;

=item get_allowed_cobrands

Return an array reference of allowed cobrand subdomains

=cut
sub get_allowed_cobrands {
    my $allowed_cobrand_string = mySociety::Config::get('ALLOWED_COBRANDS');
    my @allowed_cobrands = split(/\|/, $allowed_cobrand_string);
    return \@allowed_cobrands;
}

=item cobrand_handle Q

Given a query that has the name of a site set, return a handle to the Util module for that
site, if one exists, or zero if not.

=cut
sub cobrand_handle {
    my $cobrand = shift;

    our %handles;

    # Once we have a handle defined, return it.
    return $handles{$cobrand} if defined $handles{$cobrand};

    my $cobrand_class = ucfirst($cobrand);
    my $class = "Cobrands::" . $cobrand_class . "::Util";
    eval "use $class";

    eval{ $handles{$cobrand} = $class->new };
    $handles{$cobrand} = 0 if $@;
    return $handles{$cobrand};
}


=item cobrand_page QUERY

Return a string containing the HTML to be rendered for a custom Cobranded page

=cut
sub cobrand_page {
    my $q = shift;
    my $cobrand = $q->{site};
    my $handle = cobrand_handle($cobrand);
    return 0 unless $handle;
    return $handle->page($q);	
}

=item site_restriction COBRAND COBRAND_DATA

Return a site restriction clause and a site key if the cobrand uses a subset of the FixMyStreet 
data. COBRAND_DATA is any extra data the cobrand needs. Returns an empty string and site key 0 
if the cobrand uses all the data.

=cut
sub site_restriction {
    my ($cobrand, $cobrand_data) = @_;
    my $site_restriction = '';
    my $site_id = 0;
    my $handle = cobrand_handle($cobrand);
    return ($site_restriction, $site_id) unless $handle;
    return $handle->site_restriction($cobrand_data);
}

=item contact_restriction COBRAND

Return a contact restriction clause if the cobrand uses a subset of the FixMyStreet contact data. 

=cut

sub contact_restriction { 
    my ($cobrand) = @_;
    my $handle;
    if ($cobrand){
        $handle = cobrand_handle($cobrand);
    }
    if ( !$cobrand || !$handle || ! $handle->can('contact_restriction')){
        return '';
    }{
        return $handle->contact_restriction();
    }   
}

=item base_url COBRAND

Return the base url for the cobranded version of the site

=cut
sub base_url {
    my $cobrand = shift;
    return mySociety::Config::get('BASE_URL') unless $cobrand;
    my $handle = cobrand_handle($cobrand);
    return mySociety::Config::get('BASE_URL') unless $handle;
    return $handle->base_url();
}

=item base_url_for_emails COBRAND

Return the base url to use in links in emails for the cobranded 
version of the site

=cut

sub base_url_for_emails {
    my ($cobrand, $cobrand_data) = @_;
    my $handle;
    if ($cobrand){
        $handle = cobrand_handle($cobrand);
    }
    if ( !$cobrand || !$handle || ! $handle->can('base_url_for_emails')){
        return base_url($cobrand);
    }{
        return $handle->base_url_for_emails($cobrand_data);
    }
}

=item admin_base_url COBRAND

Base URL for the admin interface.

=cut
sub admin_base_url {
    my ($cobrand) = @_;
    my $handle;
    if ($cobrand){
        $handle = cobrand_handle($cobrand);
    }
    if ( !$cobrand || !$handle || ! $handle->can('admin_base_url')){
        return 0;
    }{
        return $handle->admin_base_url();
    }

}

=item writetothem_url COBRAND COBRAND_DATA

URL for writetothem

=cut
sub writetothem_url {
    my ($cobrand, $cobrand_data) = @_;
    my $handle;
    if ($cobrand){
        $handle = cobrand_handle($cobrand);
    }
    if ( !$cobrand || !$handle || ! $handle->can('writetothem_url')){
        return 0;
    }{
        return $handle->writetothem_url($cobrand_data);
    }

}

=item email_host COBRAND

Return the virtual host that sends email for this cobrand

=cut

sub email_host {
    my ($cobrand) = @_;
    my $email_vhost;
    if ($cobrand eq '') {
        $email_vhost = mySociety::Config::get('EMAIL_VHOST');
    } else { 
        $email_vhost = mySociety::Config::get('EMAIL_VHOST_'. uc($cobrand));
{'fraction_non_alphanumeric': 0.12125984251968504, 'fraction_numerical': 0.006420351302241066, 'mean_word_length': 3.4434876210979546, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5351582', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2901, 'n_words': 822}