Apache-Voodoo

 view release on metacpan or  search on metacpan

lib/Apache/Voodoo/Engine.pm  view on Meta::CPAN

}

sub get_apps {
	my $self = shift;

	return keys %{$self->{'apps'}};
}

sub is_devel_mode {
	my $self = shift;
	return ($self->_app->config->{'devel_mode'})?1:0;
}

sub set_request {
	my $self = shift;
	$self->{'mp'}->set_request(shift);
}

sub init_app {
	my $self = shift;

	my $id = shift || $self->{'mp'}->get_app_id();

	$self->{'app_id'} = $id;

	unless (defined($id)) {
		Apache::Voodoo::Exception::Application->throw(
			"PerlSetVar ID not present in configuration.  Giving up."
		);
	}

	# app exists?
	unless ($self->valid_app($id)) {
		Apache::Voodoo::Exception::Application->throw(
			"Application id '$id' unknown. Valid ids are: ".join(",",$self->get_apps())
		);
	}

	if ($self->_app->{'dynamic_loading'}) {
		$self->_app->refresh();
	}

	if ($self->_app->{'DEAD'}) {
		Apache::Voodoo::Exception::Application->throw("Application $id failed to load.");
	}


	return 1;
}

sub begin_run {
	my $self = shift;

	$self->{'mp'}->register_cleanup($self,\&finish);

	# setup debugging
	$debug = $self->_app->{'debug_handler'};
	$debug->init($self->{'mp'});
	$debug->mark(Time::HiRes::time,"START");

	$self->{'dbh'} = $self->attach_db();

	$self->{'session_handler'} = $self->attach_session();
	$self->{'session'} = $self->{'session_handler'}->session;

	$debug->session_id($self->{'session_handler'}->{'id'});
	$debug->mark(Time::HiRes::time,'Session Attachment');


	$debug->mark(Time::HiRes::time,'DB Connect');

	return 1;
}

sub attach_db {
	my $self = shift;

	my $db = undef;
	foreach (@{$self->_app->databases}) {
		eval {
			$db = DBI->connect_cached(@{$_});
		};
		last if $db;

		Apache::Voodoo::Exception::DBIConnect->throw($DBI::errstr);
	}

	return $db;
}

sub parse_params {
	my $self = shift;

	my $params = $self->{mp}->parse_params($self->_app->config->{'upload_size_max'});
	unless (ref($params)) {
		Apache::Voodoo::Exception::ParamParse->throw($params);
	}
	$debug->mark(Time::HiRes::time,"Parameter parsing");
	$debug->params($params);

	return $params;
}

sub status {
	my $self   = shift;
	my $status = shift;

	if (defined($debug)) {
		$debug->status($status);
		$debug->session($self->{'session'});
	}

	if (defined($self->_app) && defined($self->{'session_handler'})) {
		if ($self->{'p'}->{'uri'} =~ /\/?logout(_[^\/]+)?$/) {
			$self->{'mp'}->set_cookie($self->_app->config->{'cookie_name'},'!','now');
			$self->{'session_handler'}->destroy();
		}
		else {
			$self->{'session_handler'}->disconnect();
		}
		$debug->mark(Time::HiRes::time,'Session detachment');
	}
}

sub finish {
	my $self = shift;

	$debug->mark(Time::HiRes::time,'Cleaning up.');

	delete $self->{'app_id'};
	delete $self->{'session_handler'};
	delete $self->{'session'};
	delete $self->{'p'};
	delete $self->{'dbh'};

	if (defined($debug)) {
		$debug->mark(Time::HiRes::time,'END');
		$debug->shutdown();
	}
}

sub attach_session {
	my $self = shift;

	my $conf = $self->_app->config;

	my $session_id = $self->{'mp'}->get_cookie($conf->{'cookie_name'});

	my $session = $self->_app->{'session_handler'}->attach($session_id,$self->{'dbh'});

	if (!defined($session_id) || $session->id() ne $session_id) {
		# This is a new session, or there was an old cookie from a previous sesion,
		$self->{'mp'}->set_cookie($conf->{'cookie_name'},$session->id());
	}
	elsif ($session->has_expired($conf->{'session_timeout'})) {
		# the session has expired
		$self->{'mp'}->set_cookie($conf->{'cookie_name'},'!','now');
		$session->destroy;

		Apache::Voodoo::Exception::Application::SessionTimeout->throw(
			target  => $self->_adjust_url("/timeout"),
			error => "Session has expired"
		);
	}

	# update the session timer
	$session->touch();

	return $session;
}

sub history_capture {
	my $self   = shift;
	my $uri    = shift;
	my $params = shift;

	my $session = $self->{'session'};

	$uri = "/".$uri if $uri !~ /^\//;

	# don't put the login page in the referrer queue
	return if $uri eq "/login";

	if (!defined($session->{'history'}) ||
		$session->{'history'}->[0]->{'uri'} ne $uri) {

		# queue is empty or this is a new page
		unshift(@{$session->{'history'}}, {'uri' => $uri, 'params' => join("&",map { $_."=".$params->{$_} } keys %{$params})});
	}
	else {
		# re-entrant call to page, update the params
		$session->{'history'}->[0]->{'params'} = join("&",map { $_."=".$params->{$_} } keys %{$params});
	}

	if (scalar(@{$session->{'history'}}) > 30) {
		# keep the queue at 10 items
		pop @{$session->{'history'}};
	}

	$debug->mark(Time::HiRes::time,"history capture");
}

sub get_model {
	my $self   = shift;

	my $app_id = shift;
	my $model  = shift;

	unless ($self->valid_app($app_id)) {



( run in 0.611 second using v1.01-cache-2.11-cpan-e1769b4cff6 )