Merge branch 'master' of ssh://lx-office/~/lx-office-erp
[kivitendo-erp.git] / bin / mozilla / login.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 ######################################################################
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #######################################################################
29
30 use DBI;
31 use SL::Auth;
32 use SL::User;
33 use SL::Form;
34
35 require "bin/mozilla/common.pl";
36 require "bin/mozilla/todo.pl";
37
38 use strict;
39
40 # This is required because the am.pl in the root directory
41 # is not scanned by locales.pl:
42 # $form->parse_html_template('login/password_error')
43
44 our $form = new Form;
45
46 if (! -f 'config/authentication.pl') {
47   show_error('login/authentication_pl_missing');
48 }
49
50 our $locale = new Locale $main::language, "login";
51
52 our $auth = SL::Auth->new();
53 if (!$auth->session_tables_present()) {
54   show_error('login/auth_db_unreachable');
55 }
56 $auth->expire_sessions();
57 my $session_result = $main::auth->restore_session();
58
59 # customization
60 if (-f "bin/mozilla/custom_$form->{script}") {
61   eval { require "bin/mozilla/custom_$form->{script}"; };
62   $form->error($@) if ($@);
63 }
64
65 # per login customization
66 if (-f "bin/mozilla/$form->{login}_$form->{script}") {
67   eval { require "bin/mozilla/$form->{login}_$form->{script}"; };
68   $form->error($@) if ($@);
69 }
70
71 # window title bar, user info
72 $form->{titlebar} = "Lx-Office " . $locale->text('Version') . " $form->{version}";
73
74 if (SL::Auth::SESSION_EXPIRED == $session_result) {
75   $form->{error_message} = $locale->text('The session is invalid or has expired.');
76   login_screen();
77   exit;
78 }
79
80 my $action = $form->{action};
81
82 if (!$action && $auth->{SESSION}->{login}) {
83   $action = 'login';
84 }
85
86 if ($action) {
87   our %myconfig = $auth->read_user($form->{login}) if ($form->{login});
88
89   if (!$myconfig{login} || (SL::Auth::OK != $auth->authenticate($form->{login}, $form->{password}, 0))) {
90     $form->{error_message} = $locale->text('Incorrect Password!');
91     login_screen();
92     exit;
93   }
94
95   $auth->set_session_value('login', $form->{login}, 'password', $form->{password});
96   $auth->create_or_refresh_session();
97
98   $form->{titlebar} .= " - $myconfig{name} - $myconfig{dbname}";
99   call_sub($locale->findsub($action));
100
101 } else {
102   login_screen();
103 }
104
105 1;
106
107 sub login_screen {
108   $main::lxdebug->enter_sub();
109   my ($msg) = @_;
110
111   if (-f "css/lx-office-erp.css") {
112     $form->{stylesheet} = "lx-office-erp.css";
113   }
114
115   $form->{msg} = $msg;
116   $form->header();
117
118   print $form->parse_html_template('login/login_screen');
119
120   $main::lxdebug->leave_sub();
121 }
122
123 sub login {
124   $main::lxdebug->enter_sub();
125
126   unless ($form->{login}) {
127     login_screen($locale->text('You did not enter a name!'));
128     exit;
129   }
130
131   my $user = new User $form->{login};
132
133   # if we get an error back, bale out
134   my $result;
135   if (($result = $user->login($form)) <= -1) {
136     exit if $result == -2;
137     login_screen($locale->text('Incorrect username or password!'));
138     exit;
139   }
140
141   my %style_to_script_map = ( 'v3'  => 'v3',
142                               'neu' => 'new',
143                               'v4' => 'v4',
144                               'xml' => 'XML',
145     );
146
147   my $menu_script = $style_to_script_map{$user->{menustyle}} || '';
148
149   # made it this far, execute the menu
150   $form->{callback} = build_std_url("script=menu${menu_script}.pl", 'action=display', "callback=" . $form->escape($form->{callback}));
151
152   $main::auth->set_cookie_environment_variable();
153
154   $form->redirect();
155
156   $main::lxdebug->leave_sub();
157 }
158
159 sub logout {
160   $main::lxdebug->enter_sub();
161
162   $main::auth->destroy_session();
163
164   # remove the callback to display the message
165   $form->{callback} = "login.pl?action=";
166   $form->redirect($locale->text('You are logged out!'));
167
168   $main::lxdebug->leave_sub();
169 }
170
171 sub company_logo {
172   $main::lxdebug->enter_sub();
173
174   my %myconfig = %main::myconfig;
175   $locale             =  new Locale $myconfig{countrycode}, "login" if ($main::language ne $myconfig{countrycode});
176
177   $form->{todo_list}  =  create_todo_list('login_screen' => 1) if (!$form->{no_todo_list});
178
179   $form->{stylesheet} =  $myconfig{stylesheet};
180   $form->{title}      =  $locale->text('About');
181
182   # create the logo screen
183   $form->header() unless $form->{noheader};
184
185   print $form->parse_html_template('login/company_logo');
186
187   $main::lxdebug->leave_sub();
188 }
189
190 sub show_error {
191   my $template           = shift;
192   my %myconfig = %main::myconfig;
193   $locale                = Locale->new($main::language, 'all');
194   $myconfig{countrycode} = $main::language;
195   $form->{stylesheet}    = 'css/lx-office-erp.css';
196
197   $form->header();
198   print $form->parse_html_template($template);
199
200   # $form->parse_html_template('login/auth_db_unreachable');
201   # $form->parse_html_template('login/authentication_pl_missing');
202
203   exit;
204 }
205