use strict;
use DBI;
use CGI;
use HTML::Template::Pro;
use Crypt::CBC;
use Storable qw/freeze thaw/;
use Data::Dumper;

our($dbh, $q, $curruser);
our(%rootlvl, %userlvl);

eval{
  main();
};
if($@){
 warn $@;
}

sub dnice{
 print "Content-type: text/html\n\n";
 my($msg) = @_;
 print $msg;
 die @_;
}

sub main{
  #$dbh ||= DBI->connect('...','','',{RaiseError => 1, AutoCommit => 0});
  $q = new CGI;
  checkuser();

  my $action;
  $action = $q->param('action') || $q->param('action','index');
  dnice('No such action') if $action !~ /^(\w+)$/;
  $action = 'ext_' . $1;
  my($hdrs, $result, $noncacheable);

  dnice 'No access' if !accessallowed($action, $curruser);

  eval{
    ($hdrs, $result, $noncacheable) = __PACKAGE__->$action;
  };
  dnice($@) if $@;

  print $q->header( -type => 'text/html',
                    -charset => 'cp1251',
		    @$hdrs);
  !$result && return;
  !ref $result && print($result) && return;
  
  my $tmpl = HTML::Template::Pro->new( filename          => $q->param('action') . '.tmpl',
				       path              => './tmpl',
				       associate         => $q,
				       loop_context_vars => 1,
				       global_vars       => 1
				       );
  $tmpl->param(@$result);
  print $tmpl->output();
}

#������ ������� ��������
sub ext_example{
 return [], [ dummy=> 1 ];
}

#������ ��������
sub ext_example_redirect{
 return [ -location => 'http://www.lala.com' ];
}

#������ ������������ action ������ ������ - �., ����������� ����������� ����������� �����, ���� �� �� ��� ��� �������
sub ext_example_substitute{
  $q->param('action', 'example');
  return ext_example();
}

#� ����� ��� ���� ������� ��������?
sub ext_example_plain{
 return [ -content_type => 'application/octet-stream' ], 'plain octet-stream';
}
#######################################################
sub checkuser{
 return 1;
}
sub accessallowed{
 return 1;
}