Zwei neue Features:
[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 # This is required because the am.pl in the root directory
39 # is not scanned by locales.pl:
40 # $form->parse_html_template('login/password_error')
41
42 $form = new Form;
43
44 if (! -f 'config/authentication.pl') {
45   show_error('login/authentication_pl_missing');
46 }
47
48 $locale = new Locale $language, "login";
49
50 our $auth = SL::Auth->new();
51 if (!$auth->session_tables_present()) {
52   show_error('login/auth_db_unreachable');
53 }
54 $auth->expire_sessions();
55 $auth->restore_session();
56
57 # customization
58 if (-f "bin/mozilla/custom_$form->{script}") {
59   eval { require "bin/mozilla/custom_$form->{script}"; };
60   $form->error($@) if ($@);
61 }
62
63 # per login customization
64 if (-f "bin/mozilla/$form->{login}_$form->{script}") {
65   eval { require "bin/mozilla/$form->{login}_$form->{script}"; };
66   $form->error($@) if ($@);
67 }
68
69 # window title bar, user info
70 $form->{titlebar} = "Lx-Office " . $locale->text('Version') . " $form->{version}";
71
72 my $action = $form->{action};
73
74 if (!$action && $auth->{SESSION}->{login}) {
75   $action = 'login';
76 }
77
78 if ($action) {
79   our %myconfig = $auth->read_user($form->{login}) if ($form->{login});
80
81   if (!$myconfig{login} || (SL::Auth::OK != $auth->authenticate($form->{login}, $form->{password}, 0))) {
82     $form->{error_message} = $locale->text('Incorrect Password!');
83     login_screen();
84     exit;
85   }
86
87   $auth->set_session_value('login', $form->{login}, 'password', $form->{password});
88   $auth->create_or_refresh_session();
89
90   $form->{titlebar} .= " - $myconfig{name} - $myconfig{dbname}";
91   call_sub($locale->findsub($action));
92
93 } else {
94   login_screen();
95 }
96
97 1;
98
99 sub login_screen {
100   $lxdebug->enter_sub();
101   my ($msg) = @_;
102
103   if (-f "css/lx-office-erp.css") {
104     $form->{stylesheet} = "lx-office-erp.css";
105   }
106
107   $form->{msg}   = $msg;
108   $form->{fokus} = "loginscreen.login";
109   $form->header;
110
111   print $form->parse_html_template('login/login_screen');
112
113   $lxdebug->leave_sub();
114 }
115
116 sub login {
117   $lxdebug->enter_sub();
118
119   unless ($form->{login}) {
120     login_screen($locale->text('You did not enter a name!'));
121     exit;
122   }
123
124   $user = new User $form->{login};
125
126   # if we get an error back, bale out
127   if (($result = $user->login($form)) <= -1) {
128     exit if $result == -2;
129     login_screen($locale->text('Incorrect username or password!'));
130     exit;
131   }
132
133   my %style_to_script_map = ( 'v3'  => 'v3',
134                               'neu' => 'new',
135                               'xml' => 'XML',
136     );
137
138   my $menu_script = $style_to_script_map{$user->{menustyle}} || '';
139
140   # made it this far, execute the menu
141   $form->{callback} = build_std_url("script=menu${menu_script}.pl", 'action=display');
142
143   $auth->set_cookie_environment_variable();
144
145   $form->redirect();
146
147   $lxdebug->leave_sub();
148 }
149
150 sub logout {
151   $lxdebug->enter_sub();
152
153   $auth->destroy_session();
154
155   # remove the callback to display the message
156   $form->{callback} = "login.pl?action=";
157   $form->redirect($locale->text('You are logged out!'));
158
159   $lxdebug->leave_sub();
160 }
161
162 sub company_logo {
163   $lxdebug->enter_sub();
164
165   $locale             =  new Locale $myconfig{countrycode}, "login" if ($language ne $myconfig{countrycode});
166
167   $form->{todo_list}  = create_todo_list();
168
169   $form->{stylesheet} =  $myconfig{stylesheet};
170   $form->{title}      =  $locale->text('About');
171
172   # create the logo screen
173   $form->header() unless $form->{noheader};
174
175   print $form->parse_html_template('login/company_logo');
176
177   $lxdebug->leave_sub();
178 }
179
180 sub show_error {
181   my $template           = shift;
182   $locale                = Locale->new($language, 'all');
183   $myconfig{countrycode} = $language;
184   $form->{stylesheet}    = 'css/lx-office-erp.css';
185
186   $form->header();
187   print $form->parse_html_template($template);
188
189   # $form->parse_html_template('login/auth_db_unreachable');
190   # $form->parse_html_template('login/authentication_pl_missing');
191
192   exit;
193 }
194