package SW::ProjectClass; use strict; use warnings; use base 'CGI::Application'; use CGI::Simple; use CGI::Application::Plugin::Config::MyNewt; use CGI::Application::Plugin::DBH (qw/dbh_config dbh/); use CGI::Application::Plugin::LogDispatch; use CGI::Application::Plugin::Session; use CGI::Application::Plugin::SQL::Library; use MIME::Lite; sub cgiapp_init { my $self = shift; my $app_path = File::Spec->catdir( $self->param('root'), $self->param('project'), $self->param('app') ); $self->tmpl_path( File::Spec->catdir($app_path, 'tmpl'), ); my $cnf_path = File::Spec->catdir($app_path, 'cnf'); my $cnf_file = join('.', $self->param('app'), 'cnf'); $self->config_init( File::Spec->catfile($cnf_path, $cnf_file), ); my $dsn = join ('', "DBI:mysql:;mysql_read_default_file=", File::Spec->catfile($cnf_path, 'db.cnf'), ); $self->dbh_config( $dsn, undef, undef, {RaiseError => 1} ); $self->sql_lib_config( File::Spec->catfile($cnf_path, 'sql.lib'), ); $self->session_config( CGI_SESSION_OPTIONS => [ "driver:MySQL;serializer:Storable", $self->query, {Handle=>$self->dbh} ], DEFAULT_EXPIRY => '+1w', COOKIE_PARAMS => { -expires => '+24h', -path => '/' }, SEND_COOKIE => 1, ); $self->log_config( LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::DBI', name => 'dbi', dbh => $self->dbh, table => 'log', min_level => 'debug', }, { module => 'Log::Dispatch::Email::MailSend', name => 'email', to => [qw(my@email.com)], subject => 'Log message from www.mywebsite.org.uk', min_level => 'emerg' } ] ); } sub cgiapp_get_query { my $self = shift; $CGI::Simple::DISABLE_UPLOADS = 0; # enable uploads $CGI::Simple::POST_MAX = 1_048_576; # allow 1MB uploads return CGI::Simple->new(); } sub mime_email { my ($self, $over_ride) = @_; my %default = ( From => 'somewhere@mail.com', To => 'webadmin@website.com', Subject => 'website enquiry', Type => 'text/html', Data => undef, ); @default{keys %{$over_ride}} = values %{$over_ride}; my $msg = MIME::Lite->new(%default,) or die "Error creating MIME body: $!\n"; $msg->send or die "Error sending MIME body: $!\n"; return \%default; } sub error_runmode{ my ($self, $msg) = @_; $self->log->emerg("Error: $msg"); my $tmpl_obj = $self->load_tmpl('error.html'); $tmpl_obj->param(msg => $msg,); return $tmpl_obj->output; } 1;