Improved INSTALL guide.
[gruta.git] / Gruta / CGI.pm
blob8ba19d14b5cb18a4e1cdf5167b8363883605a170
1 package Gruta::CGI;
3 use CGI;
4 use Carp;
6 sub vars {
7 return $_[0]->{cgi}->Vars();
10 sub upload_dirs {
11 return @{ $_[0]->{upload_dirs} };
14 sub http_headers {
15 my $self = shift;
16 my %headers = @_;
18 foreach my $k (keys(%headers)) {
19 $self->{http_headers}->{$k} = $headers{$k};
22 return $self->{http_headers};
25 sub cookie {
26 my $self = shift;
28 if (@_) {
29 $self->http_headers( 'Set-Cookie', shift );
32 return $ENV{HTTP_COOKIE};
35 sub status {
36 $_[0]->http_headers( 'Status', $_[1] );
39 sub redirect {
40 my $self = shift;
42 $self->http_headers('Location', $self->data->url(@_));
44 return $self;
47 sub data {
48 my $self = shift;
49 my $data = shift;
51 if (defined($data)) {
52 $self->{data} = $data;
55 return $self->{data};
59 sub upload {
60 my $self = shift;
61 my $dir = shift;
62 my $field = shift;
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";
74 while(<$file>) {
75 print F $_;
78 close F;
82 sub new {
83 my $class = shift;
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();
100 return $obj;
104 sub run {
105 my $self = shift;
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})) {
113 $data->auth( $u );
115 elsif (my $cookie = $self->cookie()) {
116 if (my ($sid) = ($cookie =~ /sid\s*=\s*(\d+)/)) {
117 $data->auth_from_sid( $sid );
121 my $st = 'INDEX';
123 if ($vars->{t}) {
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(
134 id => 'admin',
135 is_admin => 1,
136 can_upload => 1,
137 username => 'Admin',
138 email => 'webmaster@localhost'
141 # set a random password (to be promptly changed)
142 $u->password(rand());
144 # insert the user
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'));
153 $data->auth($u);
154 $data->session($session);
156 $st = 'ADMIN';
159 my $body = undef;
161 eval { $body = $data->template->process( $st ) };
163 if ($@) {
164 $data->log($@);
165 # $self->redirect('INDEX');
167 $self->status(500);
168 $body = "<h1>500 Internal Server Error</h1><pre>$@</pre>";
170 # main processing failed
171 $self->{error} = 1;
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()) {
178 use Digest::MD5;
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} || '';
187 if ($inm eq $etag) {
188 $self->status(304);
189 $body = '';
191 else {
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/) {
199 # compress!!
200 use Compress::Zlib;
202 if (my $cbody = Compress::Zlib::memGzip($body)) {
203 $self->http_headers('Content-encoding' => 'gzip');
204 $body = $cbody;
208 my $h = $self->http_headers();
209 foreach my $k (keys(%{ $h })) {
210 print $k, ': ', $h->{$k}, "\n";
212 print "\n";
214 print $body;