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