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

Last change on this file since 354 was 354, checked in by zerodeux, 11 years ago

Configuration mod: passing backend parameters from bearmail.conf up to backend + warning fixes

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
27
28
29sub setup {
30    my $self = shift;
31
32    # CGI.pm sets a default ISO-8859-1 charset in the Content-Type header.
33    # We prefer omitting it and let browsers honor the <meta> in page <header>'s.
34    $self->query->charset('');
35
36    # Give some interesting characteristics to our HTML::Template's
37    $self->add_callback('load_tmpl', \&_my_load_tmpl);
38
39    # Instanciate the backend
40    $self->{b} = BearMail::Backend::backend( @{$self->cfg('backend')} );
41}
42
43sub cgiapp_init {
44    my $self = shift;
45
46    $self->session_config (
47      COOKIE_PARAMS => { -name => 'bearmail', -expires => '+8days' },
48      SEND_COOKIE   => 1,
49    );
50    CGI::Session->name('bearmail');
51}
52
53sub cgiapp_prerun {
54    my $self = shift;
55
56    my $rm   = $self->get_current_runmode;
57    my $user = $self->session->param('user');
58
59    if ((not $rm  =~ /login|reminder/) and (not defined $user)) {
60      $self->session->param('intent', $ENV{PATH_INFO}."/".$rm); #FIXME: should be something like path/runmode?args i guess. or should we drop the runmode ?
61      return $self->redirect($self->url('login'));
62    }
63}
64
65sub url {
66    my ($self, $path) = @_;
67
68    my $url = $self->query->url(-absolute => 1).'/'.($path || '');
69    $url =~ s:/+:/:g;
70    return $url;
71}
72
73sub _my_load_tmpl {
74    my ($self, $ht_params, $tmpl_params, $tmpl_file) = @_;
75
76    $ht_params ||= {};
77
78    # Fix template links (generate proper absolute hrefs).
79    # The convention is to use href="#/module_name/mode/..." in templates.
80    $ht_params->{'filter'} = sub { ${$_[0]} =~ s:"#/(.+?)":'"'.$self->url($1).'"':eg };
81
82    # As a safe default, escape all parameters. If we have to push something
83    # which is rather URL, JS or none, we'll handle it as an exception.
84    $ht_params->{'default_escape'} = 'HTML';
85}
86
871;
Note: See TracBrowser for help on using the repository browser.