7 return $_[0]->{cgi
}->Vars();
11 return @
{ $_[0]->{upload_dirs
} };
18 foreach my $k (keys(%headers)) {
19 $self->{http_headers
}->{$k} = $headers{$k};
22 return $self->{http_headers
};
29 $self->http_headers( 'Set-Cookie', shift );
32 return $ENV{HTTP_COOKIE
};
36 $_[0]->http_headers( 'Status', $_[1] );
42 $self->http_headers('Location', $self->data->url(@_));
52 $self->{data
} = $data;
64 my $file = $self->{cgi
}->param($field);
65 my ($basename) = ($file =~ /([^\/\\]+)$/);
67 if (! grep(/^$dir$/, $self->upload_dirs())) {
68 croak
"Unauthorized upload directory $dir";
71 my $filename = $dir . '/' . $basename;
73 open F
, '>' . $filename or croak
"Can't write $filename";
85 my $obj = bless( { @_ }, $class );
87 $obj->{charset
} ||= 'UTF-8';
88 $obj->{min_size_for_gzip
} ||= 10000;
90 $obj->{http_headers
} = {
91 'Content-Type' => 'text/html; charset=' . $obj->{charset
},
92 'X-Gateway-Interface' => $ENV{'GATEWAY_INTERFACE'},
93 'X-Server-Name' => $ENV{'SERVER_NAME'}
96 $obj->{upload_dirs
} ||= [];
98 $obj->{cgi
} = CGI
->new();
107 my $data = $self->data();
108 my $vars = $self->vars();
110 $data->template->cgi_vars($vars);
112 if ($ENV{REMOTE_USER
} and my $u = $data->source->user($ENV{REMOTE_USER
})) {
115 elsif (my $cookie = $self->cookie()) {
116 if (my ($sid) = ($cookie =~ /sid\s*=\s*(\d+)/)) {
117 $data->auth_from_sid( $sid );
124 $st = uc($vars->{t
});
127 $st = 'INDEX' unless $st =~ /^[-\w0-9_]+$/;
129 # not identified nor users found?
130 if (!$data->auth() && ! $data->source->users()) {
132 # create the admin user
133 my $u = Gruta
::Data
::User
->new(
138 email
=> 'webmaster@localhost'
141 # set a random password (to be promptly changed)
142 $u->password(rand());
145 $data->source->insert_user($u);
147 # create a new session
148 my $session = Gruta
::Data
::Session
->new(user_id
=> 'admin');
149 $u->source->insert_session($session);
151 $self->cookie('sid=' . $session->get('id'));
154 $data->session($session);
161 eval { $body = $data->template->process( $st ) };
165 # $self->redirect('INDEX');
168 $body = "<h1>500 Internal Server Error</h1><pre>$@</pre>";
170 # main processing failed
174 $self->http_headers('X-BaseURL' => $self->data->base_url());
175 $self->http_headers('X-Powered-By' => 'Gruta ' . $self->data->version());
177 if (!$data->auth()) {
179 use Encode
qw(encode_utf8);
181 my $md5 = Digest
::MD5
->new();
182 $md5->add(encode_utf8
($body));
183 my $etag = $md5->hexdigest();
185 my $inm = $ENV{HTTP_IF_NONE_MATCH
} || '';
192 $self->http_headers('ETag' => $etag);
196 # does the client accept compression?
197 if (length($body) > $self->{min_size_for_gzip
} &&
198 $ENV{HTTP_ACCEPT_ENCODING
} =~ /gzip/) {
202 if (my $cbody = Compress
::Zlib
::memGzip
($body)) {
203 $self->http_headers('Content-encoding' => 'gzip');
208 my $h = $self->http_headers();
209 foreach my $k (keys(%{ $h })) {
210 print $k, ': ', $h->{$k}, "\n";