<commit_before>package CoGe::Accessory::Web;
use v5.10;

use strict;
use base 'Class::Accessor';
use Data::Dumper;
use Data::Validate::URI qw(is_uri);
use Carp qw(cluck);
use CoGeX;
use DBIxProfiler;
use CGI::Carp('fatalsToBrowser');
use CGI;
use CGI::Cookie;
use File::Path;
use File::Basename;
use File::Temp;
use LWP::Simple qw(!get !head !getprint !getstore !mirror);
use LWP::UserAgent;
use JSON;
use HTTP::Request;
use XML::Simple;
use CoGe::Accessory::LogUser qw(get_cookie_session);
use Digest::MD5 qw(md5_base64);
use POSIX qw(!tmpnam !tmpfile);
use Mail::Mailer;
use URI;

=head1 NAME

Web

=head1 SYNOPSIS

use Web

=head1 DESCRIPTION

=head1 AUTHOR

<NAME>

=head1 COPYRIGHT

The full text of the license can be found in the
LICENSE file included with this module.

=head1 SEE ALSO

=cut

our($CONF, $VERSION, @ISA, @EXPORT, @EXPORT_OK, $Q, $TEMPDIR, $BASEDIR,
    $PAYLOAD_ERROR, $NOT_FOUND);

BEGIN {
    require Exporter;

    $BASEDIR = ( $ENV{COGE_HOME} ? $ENV{COGE_HOME} : '/opt/apache/coge/web/' );
    $VERSION = 0.1;
    $TEMPDIR = $BASEDIR . "tmp";
    @ISA     = ( qw (Exporter Class::Accessor) );
    @EXPORT  = qw( get_session_id );
    @EXPORT_OK = qw( check_filename_taint check_taint gunzip gzip send_email
                     get_defaults set_defaults url_for get_job schedule_job );

    $PAYLOAD_ERROR = "The request could not be decoded";
    $NOT_FOUND = "The action could not be found";

    __PACKAGE__->mk_accessors(
        'restricted_orgs', 'basefilename', 'basefile', 'logfile',
        'sqlitefile'
    );
}

# TODO: instead of returning a list, this routine should return a "page object"
sub init {
    my ( $self, %opts ) = self_or_default(@_);
    my $ticket = $opts{ticket}; # optional cas ticket for retrieving user
    my $url    = $opts{url};    # optional redirect url to pass to cas authentication
    my $cgi    = $opts{cgi};    # optional CGI object
    my $debug  = $opts{debug};  # optional flag for enabling debugging messages
    my $page_title = $opts{page_title}; # optional page title
    my $ticket_type = $opts{ticket_type}; #optional ticket type (saml or proxy)

    if ($cgi) {
    	$ticket = $cgi->param('ticket') || undef;
    	$url    = $cgi->url;
    }

    # Get config
    $CONF = get_defaults();

    # Connec to DB
    my $db = CoGeX->dbconnect($CONF);
    if ($debug) { # enable ORM debugging if requested
		$db->storage->debugobj(new DBIxProfiler());
		$db->storage->debug(1);
    }

    # Get user
    my $user;
    if ($ticket) {
    	if (defined $ticket_type and $ticket_type eq 'proxy') { # mdb added 12/10/13 hackathon1
    		($user) = login_cas_proxy(
		    	cookie_name => $CONF->{COOKIE_NAME},
		        ticket   => $ticket,
		        coge     => $db,
		        this_url => $url
		    );
    	}
    	else {
		    ($user) = login_cas_saml(
		    	cookie_name => $CONF->{COOKIE_NAME},
		        ticket   => $ticket,
		        coge     => $db,
		        this_url => $url
		    );
    	}
    }
    ($user) = CoGe::Accessory::LogUser->get_user(
        cookie_name => $CONF->{COOKIE_NAME},
        coge        => $db
    ) unless $user;

	my $link;
    if ($page_title) { # This is a page access and not a web service request
        # Make tmp directory
        my $tempdir = $CONF->{TEMPDIR} . '/' . $page_title . '/';
        mkpath( $tempdir, 0, 0777 ) unless -d $tempdir;

		# Skip tiny link generation and auto-logging if ajax request
		if (not is_ajax($cgi)) {
	        # Get tiny link
	        $link = get_tiny_link(
	            url => 'http://' . $ENV{SERVER_NAME} . $ENV{REQUEST_URI},
	        );

            # erb 10/07/2014 - remove unused generic logging issue 516
	        # Log this page access
            #CoGe::Accessory::Web::log_history(
		    #    db          => $db,
		    #    user_id     => $user->id,
		    #   	page        => $page_title,
		    #  	description => 'page access',
		    #    link        => $link
		    #);
		}
    }

    print STDERR "Web::init ticket=" . ($ticket ? $ticket : '') . " url=" . ($url ? $url : '') . " page_title=" . ($page_title ? $page_title : '') . " user=" . ($user ? $user->name : '') . "\n";

    return ( $db, $user, $CONF, $link );
}

sub get_defaults {
    return $CONF if ($CONF);

    my ( $self, $param_file ) = self_or_default(@_);
    $param_file = $BASEDIR . "/coge.conf" unless defined $param_file;
#    print STDERR "Web::get_defaults $param_file\n";
    unless ( -r $param_file ) {
        print STDERR
qq{Either no parameter file specified or unable to read paramer file ($param_file).
A valid parameter file must be specified or very little will work!};
        return 0;
    }
    open( IN, $param_file );
    my %items;
    while (<IN>) {
        chomp;
        next if /^#/;<commit_msg>add render_template for easy template rendering<commit_after>package CoGe::Accessory::Web;
use v5.10;

use strict;
use base 'Class::Accessor';
use Data::Dumper;
use Data::Validate::URI qw(is_uri);
use Carp qw(cluck);
use CoGeX;
use DBIxProfiler;
use CGI::Carp('fatalsToBrowser');
use CGI;
use CGI::Cookie;
use File::Path;
use File::Basename;
use File::Temp;
use File::Spec::Functions;
use HTML::Template;
use LWP::Simple qw(!get !head !getprint !getstore !mirror);
use LWP::UserAgent;
use JSON;
use HTTP::Request;
use XML::Simple;
use CoGe::Accessory::LogUser qw(get_cookie_session);
use Digest::MD5 qw(md5_base64);
use POSIX qw(!tmpnam !tmpfile);
use Mail::Mailer;
use URI;

=head1 NAME

Web

=head1 SYNOPSIS

use Web

=head1 DESCRIPTION

=head1 AUTHOR

<NAME>

=head1 COPYRIGHT

The full text of the license can be found in the
LICENSE file included with this module.

=head1 SEE ALSO

=cut

our($CONF, $VERSION, @ISA, @EXPORT, @EXPORT_OK, $Q, $TEMPDIR, $BASEDIR,
    $PAYLOAD_ERROR, $NOT_FOUND);

BEGIN {
    require Exporter;

    $BASEDIR = ( $ENV{COGE_HOME} ? $ENV{COGE_HOME} : '/opt/apache/coge/web/' );
    $VERSION = 0.1;
    $TEMPDIR = $BASEDIR . "tmp";
    @ISA     = ( qw (Exporter Class::Accessor) );
    @EXPORT  = qw( get_session_id );
    @EXPORT_OK = qw( check_filename_taint check_taint gunzip gzip send_email
                     get_defaults set_defaults url_for get_job schedule_job
                     render_template );

    $PAYLOAD_ERROR = "The request could not be decoded";
    $NOT_FOUND = "The action could not be found";

    __PACKAGE__->mk_accessors(
        'restricted_orgs', 'basefilename', 'basefile', 'logfile',
        'sqlitefile'
    );
}

# TODO: instead of returning a list, this routine should return a "page object"
sub init {
    my ( $self, %opts ) = self_or_default(@_);
    my $ticket = $opts{ticket}; # optional cas ticket for retrieving user
    my $url    = $opts{url};    # optional redirect url to pass to cas authentication
    my $cgi    = $opts{cgi};    # optional CGI object
    my $debug  = $opts{debug};  # optional flag for enabling debugging messages
    my $page_title = $opts{page_title}; # optional page title
    my $ticket_type = $opts{ticket_type}; #optional ticket type (saml or proxy)

    if ($cgi) {
    	$ticket = $cgi->param('ticket') || undef;
    	$url    = $cgi->url;
    }

    # Get config
    $CONF = get_defaults();

    # Connec to DB
    my $db = CoGeX->dbconnect($CONF);
    if ($debug) { # enable ORM debugging if requested
		$db->storage->debugobj(new DBIxProfiler());
		$db->storage->debug(1);
    }

    # Get user
    my $user;
    if ($ticket) {
    	if (defined $ticket_type and $ticket_type eq 'proxy') { # mdb added 12/10/13 hackathon1
    		($user) = login_cas_proxy(
		    	cookie_name => $CONF->{COOKIE_NAME},
		        ticket   => $ticket,
		        coge     => $db,
		        this_url => $url
		    );
    	}
    	else {
		    ($user) = login_cas_saml(
		    	cookie_name => $CONF->{COOKIE_NAME},
		        ticket   => $ticket,
		        coge     => $db,
		        this_url => $url
		    );
    	}
    }
    ($user) = CoGe::Accessory::LogUser->get_user(
        cookie_name => $CONF->{COOKIE_NAME},
        coge        => $db
    ) unless $user;

	my $link;
    if ($page_title) { # This is a page access and not a web service request
        # Make tmp directory
        my $tempdir = $CONF->{TEMPDIR} . '/' . $page_title . '/';
        mkpath( $tempdir, 0, 0777 ) unless -d $tempdir;

		# Skip tiny link generation and auto-logging if ajax request
		if (not is_ajax($cgi)) {
	        # Get tiny link
	        $link = get_tiny_link(
	            url => 'http://' . $ENV{SERVER_NAME} . $ENV{REQUEST_URI},
	        );

            # erb 10/07/2014 - remove unused generic logging issue 516
	        # Log this page access
            #CoGe::Accessory::Web::log_history(
		    #    db          => $db,
		    #    user_id     => $user->id,
		    #   	page        => $page_title,
		    #  	description => 'page access',
		    #    link        => $link
		    #);
		}
    }

    print STDERR "Web::init ticket=" . ($ticket ? $ticket : '') . " url=" . ($url ? $url : '') . " page_title=" . ($page_title ? $page_title : '') . " user=" . ($user ? $user->name : '') . "\n";

    return ( $db, $user, $CONF, $link );
}

sub render_template {
    my ($template_name, $opts) = @_;

    my $template_file = catfile(get_defaults()->{TMPLDIR}, $template_name);

    unless(-r $template_file) {
        cluck "error: template=$template_file could not be found";
        return;
    }

    my $template = HTML::Template->new( filename => $template_file );

    $template->param($opts);
    return $template->output;
}

sub get_defaults {
    return $CONF if ($CONF);

    my ( $self, $param_file ) = self_or_default(@_);
    $param_file = $BASEDIR . "/coge.conf" unless defined $param_file;
#    print STDERR "Web::get_defaults $param_file\n";
    unless ( -r $param_file ) {
        print STDERR
qq{Either no parameter file specified or unable to read paramer file ($param_file).
A valid parameter file must be specified or very little will work!};
        return 0;
    }
    open( IN, $param_file );
    my %items;
    while (<IN>) {
        chomp;
        next if /^#/;
{'fraction_non_alphanumeric': 0.14135654261704683, 'fraction_numerical': 0.008003201280512205, 'mean_word_length': 2.6472090477927765, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1358674', 'n_tokens_mistral': 3623, 'n_tokens_neox': 3392, 'n_words': 949}