so Search.tsf and Search.rdf work
[freeside.git] / sql-ledger / am.pl
1 #!/usr/bin/perl
2 #
3 ######################################################################
4 # SQL-Ledger Accounting
5 # Copyright (C) 2001
6 #
7 #  Author: Dieter Simader
8 #   Email: dsimader@sql-ledger.org
9 #     Web: http://www.sql-ledger.org
10 #
11 #  Contributors:
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #######################################################################
26 #
27 # this script is the frontend called from bin/$terminal/$script
28 # all the accounting modules are linked to this script which in
29 # turn execute the same script in bin/$terminal/
30 #
31 #######################################################################
32
33 # setup defaults, DO NOT CHANGE
34 $userspath = "users";
35 $spool = "spool";
36 $templates = "templates";
37 $memberfile = "users/members";
38 $sendmail = "| /usr/sbin/sendmail -t";
39 %printer = ( Printer => 'lpr' );
40 ########## end ###########################################
41
42
43 $| = 1;
44
45 use SL::Form;
46
47 eval { require "sql-ledger.conf"; };
48
49
50 $form = new Form;
51
52 # name of this script
53 $0 =~ tr/\\/\//;
54 $pos = rindex $0, '/';
55 $script = substr($0, $pos + 1);
56
57 # we use $script for the language module
58 $form->{script} = $script;
59 # strip .pl for translation files
60 $script =~ s/\.pl//;
61
62 # pull in DBI
63 use DBI qw(:sql_types);
64
65
66 # check for user config file, could be missing or ???
67 eval { require("$userspath/$form->{login}.conf"); };
68 if ($@) {
69   $locale = new Locale "$language", "$script";
70   
71   $form->{callback} = "";
72   $msg1 = $locale->text('You are logged out!');
73   $msg2 = $locale->text('Login');
74   $form->redirect("$msg1 <p><a href=login.pl target=_top>$msg2</a>");
75 }
76
77
78 # send warnings and errors to browser
79 $SIG{__WARN__} = sub { $form->info($_[0]) };
80 $SIG{__DIE__} = sub { $form->error($_[0]) };
81
82 $myconfig{dbpasswd} = unpack 'u', $myconfig{dbpasswd};
83 map { $form->{$_} = $myconfig{$_} } qw(stylesheet charset timeout) unless ($form->{type} eq 'preferences');
84
85 # locale messages
86 $locale = new Locale "$myconfig{countrycode}", "$script";
87
88 $form->{path} =~ s/\.\.\///g;
89 if ($form->{path} !~ /^bin\//) {
90   $form->error($locale->text('Invalid path!')."\n");
91 }
92
93 # did sysadmin lock us out
94 if (-f "$userspath/nologin") {
95   $form->error($locale->text('System currently down for maintenance!'));
96 }
97
98 # pull in the main code
99 require "$form->{path}/$form->{script}";
100
101 # customized scripts
102 if (-f "$form->{path}/custom_$form->{script}") {
103   eval { require "$form->{path}/custom_$form->{script}"; };
104 }
105
106 # customized scripts for login
107 if (-f "$form->{path}/$form->{login}_$form->{script}") {
108   eval { require "$form->{path}/$form->{login}_$form->{script}"; };
109 }
110
111   
112 if ($form->{action}) {
113   # window title bar, user info
114   $form->{titlebar} = "SQL-Ledger ".$locale->text('Version'). " $form->{version} - $myconfig{name} - $myconfig{dbname}";
115
116   &check_password;
117   
118   if (substr($form->{action}, 0, 1) =~ /( |\.)/) {
119     &{ $form->{nextsub} };
120   } else {
121     &{ $locale->findsub($form->{action}) };
122   }
123 } else {
124   $form->error($locale->text('action= not defined!'));
125 }
126
127 1;
128 # end
129
130
131 sub check_password {
132   
133   if ($myconfig{password}) {
134
135     require "$form->{path}/pw.pl";
136
137     if ($form->{password}) {
138       if ((crypt $form->{password}, substr($form->{login}, 0, 2)) ne $myconfig{password}) {
139         &getpassword;
140         exit;
141       }
142     } else {
143       if ($ENV{HTTP_USER_AGENT}) {
144         $ENV{HTTP_COOKIE} =~ s/;\s*/;/g;
145         %cookie = split /[=;]/, $ENV{HTTP_COOKIE};
146         
147         if ($form->{action} ne 'display') {
148           if ((! $cookie{"SQL-Ledger-$form->{login}"}) || $cookie{"SQL-Ledger-$form->{login}"} ne $form->{sessionid}) {
149             &getpassword(1);
150             exit;
151           }
152         }
153       }
154     }
155   }
156 }
157
158