source: bearmail/lib/BearMail/Web.pm @ 500

Last change on this file since 500 was 500, checked in by zecrazytux, 10 years ago

Added a lock and enhanced mailmap writing mechanism

File size: 2.8 KB
Line 
1# Copyright (C) 2009 Bearstech - http://bearstech.com/
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16package BearMail::Web;
17use strict;
18use base 'CGI::Application';
19use CGI::Application::Plugin::AutoRunmode;
20use CGI::Application::Plugin::Session;
21use CGI::Application::Plugin::Redirect;
22use CGI::Application::Plugin::ConfigAuto qw/cfg_file cfg/;
23use CGI::Application::Plugin::DebugScreen;  # DebugScreen is done when $ENV{CGI_APP_DEBUG} is set
24use BearMail::Backend;
25use Data::Dumper;
26
27sub setup {
28    my $self = shift;
29
30    # CGI.pm sets a default ISO-8859-1 charset in the Content-Type header.
31    # We prefer omitting it and let browsers honor the <meta> in page <header>'s.
32    $self->query->charset('');
33
34    # Give some interesting characteristics to our HTML::Template's
35    $self->add_callback('load_tmpl', \&_my_load_tmpl);
36
37    # Instanciate the backend
38    $self->{b} = BearMail::Backend::backend( @{$self->cfg('backend')} );
39}
40
41sub cgiapp_init {
42    my $self = shift;
43
44    $self->session_config (
45      COOKIE_PARAMS => { -name => 'bearmail', -expires => '+8days' },
46      SEND_COOKIE   => 1,
47    );
48    CGI::Session->name('bearmail');
49}
50
51sub cgiapp_prerun {
52    my $self = shift;
53
54    my $rm   = $self->get_current_runmode;
55    my $user = $self->session->param('user');
56
57    if ((not $rm  =~ /login|reminder/) and (not defined $user)) {
58      $self->session->param('intent', $ENV{PATH_INFO}."/".$rm); #FIXME: should be something like path/runmode?args i guess. or should we drop the runmode ?
59      return $self->redirect($self->url('login'));
60    }
61}
62
63sub url {
64    my ($self, $path) = @_;
65
66    my $url = $self->query->url(-absolute => 1).'/'.($path || '');
67    $url =~ s:/+:/:g;
68    return $url;
69}
70
71sub _my_load_tmpl {
72    my ($self, $ht_params, $tmpl_params, $tmpl_file) = @_;
73
74    $ht_params ||= {};
75
76    # Fix template links (generate proper absolute hrefs).
77    # The convention is to use href="#/module_name/mode/..." in templates.
78    $ht_params->{'filter'} = sub { ${$_[0]} =~ s:"#/(.+?)":'"'.$self->url($1).'"':eg };
79
80    # As a safe default, escape all parameters. If we have to push something
81    # which is rather URL, JS or none, we'll handle it as an exception.
82    $ht_params->{'default_escape'} = 'HTML';
83}
84
851;
Note: See TracBrowser for help on using the repository browser.