This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / old / sql-ledger / bin / mozilla / admin.pl
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (c) 2002
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #======================================================================
23 #
24 # setup module
25 # add/edit/delete users
26 #
27 #======================================================================
28
29 $menufile = "menu.ini";
30
31 use SL::Form;
32 use SL::User;
33
34
35 $form = new Form;
36
37 $locale = new Locale $language, "admin";
38   
39 eval { require DBI; };
40 $form->error($locale->text('DBI not installed!')) if ($@);
41
42
43 # customization
44 if (-f "$form->{path}/custom_$form->{script}") {
45   eval { require "$form->{path}/custom_$form->{script}"; };
46   $form->error($@) if ($@);
47 }
48
49
50 if (-f "css/sql-ledger.css") {
51   $form->{stylesheet} = "sql-ledger.css";
52 }
53
54
55 if ($form->{action}) {
56
57   $subroutine = $locale->findsub($form->{action});
58   
59   if ($subroutine eq 'login') {
60     if ($form->{rpw}) {
61       $form->{rpw} = crypt $form->{rpw}, "ro";
62     }
63   }
64   
65   &check_password;
66   
67   &$subroutine;
68     
69 } else {
70
71   # if there are no drivers bail out
72   $form->error($locale->text('No Database Drivers available!')) unless (User->dbdrivers);
73
74   # create memberfile
75   if (! -f $memberfile) {
76     open(FH, ">$memberfile") or $form->error("$memberfile : $!");
77     print FH qq|# SQL-Ledger Accounting members
78
79 [root login]
80 password=
81
82 |;
83     close(FH);
84   }
85
86   &adminlogin;
87
88 }
89
90 1;
91 # end
92
93
94 sub adminlogin {
95
96   $form->{title} = qq|SQL-Ledger $form->{version} |.$locale->text('Administration');
97
98   $form->header;
99   
100   print qq|
101 <body class=admin>
102
103
104 <div align=center>
105
106 <a href="http://www.sql-ledger.org"><img src=sql-ledger.png border=0></a>
107 <h1 class=login>|.$locale->text('Version').qq| $form->{version}<p>|.$locale->text('Administration').qq|</h1>
108
109 <form method=post action="$form->{script}">
110
111 <table>
112   <tr>
113     <th>|.$locale->text('Password').qq|</th>
114     <td><input type=password name=rpw></td>
115     <td><input type=submit class=submit name=action value="|.$locale->text('Login').qq|"></td>
116   </tr>
117 <input type=hidden name=action value=login>
118 <input type=hidden name=root value="root login">
119 <input type=hidden name=path value=$form->{path}>
120 </table>
121
122
123 </form>
124
125 <a href=http://www.sql-ledger.org>SQL-Ledger |.$locale->text('website').qq|</a>
126
127 </div>
128
129 </body>
130 </html>
131 |;
132
133 }
134
135
136
137
138 sub login {
139
140   &list_users;
141
142 }
143
144
145
146 sub add_user {
147   
148   $form->{title} = "SQL-Ledger ".$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Add User');
149
150   $form->{Oracle_sid} = $sid;
151   $form->{Oracle_dbport} = '1521';
152   $form->{Oracle_dbhost} = `hostname`;
153
154   if (-f "css/sql-ledger.css") {
155     $myconfig->{stylesheet} = "sql-ledger.css";
156   }
157   $myconfig->{vclimit} = 200;
158   
159   &form_header;
160   &form_footer;
161   
162 }
163
164
165
166 sub edit {
167
168   $form->{title} = "SQL-Ledger ".$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Edit User');
169   $form->{edit} = 1;
170
171   &form_header;
172   &form_footer;
173
174 }
175
176
177 sub form_footer {
178
179   if ($form->{edit}) {
180     $delete = qq|<input type=submit class=submit name=action value="|.$locale->text('Delete').qq|">
181 <input type=hidden name=edit value=1>|;
182   }
183
184   print qq|
185
186 <input name=callback type=hidden value="$form->{script}?action=list_users&path=$form->{path}&root=$form->{root}&rpw=$form->{rpw}">
187 <input type=hidden name=root value="$form->{root}">
188 <input type=hidden name=path value=$form->{path}>
189 <input type=hidden name=rpw value=$form->{rpw}>
190
191 <input type=submit class=submit name=action value="|.$locale->text('Save').qq|">
192 $delete
193
194 </form>
195
196 </body>
197 </html>
198 |;
199
200 }
201
202
203 sub list_users {
204
205   $form->error("$memberfile : ".$locale->text('locked!')) if (-f "${memberfile}.LCK");
206
207   open(FH, "$memberfile") or $form->error("$memberfile : $!");
208   
209   while (<FH>) {
210     chop;
211     
212     if (/^\[.*\]/) {
213       $login = $_;
214       $login =~ s/(\[|\])//g;
215     }
216
217     if (/^(name=|company=|templates=|dbuser=|dbdriver=|dbname=|dbhost=)/) {
218       chop ($var = $&);
219       ($null, $member{$login}{$var}) = split /=/, $_, 2;
220     }
221   }
222   
223   close(FH);
224
225 # type=submit $locale->text('Pg Database Administration')
226 # type=submit $locale->text('Oracle Database Administration')
227
228   foreach $item (User->dbdrivers) {
229     $dbdrivers .= qq|<input name=action type=submit class=submit value="|.$locale->text("$item Database Administration").qq|">|;
230   }
231
232
233   $column_header{login} = qq|<th>|.$locale->text('Login').qq|</th>|;
234   $column_header{name} = qq|<th>|.$locale->text('Name').qq|</th>|;
235   $column_header{company} = qq|<th>|.$locale->text('Company').qq|</th>|;
236   $column_header{dbdriver} = qq|<th>|.$locale->text('Driver').qq|</th>|;
237   $column_header{dbhost} = qq|<th>|.$locale->text('Host').qq|</th>|;
238   $column_header{dataset} = qq|<th>|.$locale->text('Dataset').qq|</th>|;
239   $column_header{templates} = qq|<th>|.$locale->text('Templates').qq|</th>|;
240
241   @column_index = qw(login name company dbdriver dbhost dataset templates);
242
243   $form->{title} = "SQL-Ledger ".$locale->text('Accounting')." ".$locale->text('Administration');
244
245   $form->header;
246
247   print qq|
248 <body class=admin>
249
250 <form method=post action=$form->{script}>
251
252 <table width=100%>
253   <tr>
254   <tr class=listheading>
255     <th>$form->{title}</th>
256   </tr>
257   <tr size=5></tr>
258   <tr>
259     <td>
260       <table width=100%>
261         <tr class=listheading>|;
262
263   map { print "$column_header{$_}\n" } @column_index;
264   
265   print qq|
266         </tr>
267 |;
268
269 foreach $key (sort keys %member) {
270   $href = "$script?action=edit&login=$key&path=$form->{path}&root=$form->{root}&rpw=$form->{rpw}";
271   $href =~ s/ /%20/g;
272   
273   $member{$key}{templates} =~ s/^$templates\///;
274   $member{$key}{dbhost} = $locale->text('localhost') unless $member{$key}{dbhost};
275   $member{$key}{dbname} = $member{$key}{dbuser} if ($member{$key}{dbdriver} eq 'Oracle');
276
277   $column_data{login} = qq|<td><a href=$href>$key</a></td>|;
278   $column_data{name} = qq|<td>$member{$key}{name}</td>|;
279   $column_data{company} = qq|<td>$member{$key}{company}</td>|;
280   $column_data{dbdriver} = qq|<td>$member{$key}{dbdriver}</td>|;
281   $column_data{dbhost} = qq|<td>$member{$key}{dbhost}</td>|;
282   $column_data{dataset} = qq|<td>$member{$key}{dbname}</td>|;
283   $column_data{templates} = qq|<td>$member{$key}{templates}</td>|;
284   
285   $i++; $i %= 2;
286   print qq|
287         <tr class=listrow$i>|;
288
289   map { print "$column_data{$_}\n" } @column_index;
290
291   print qq|
292         </tr>|;
293 }
294
295
296 print qq|
297       </table>
298     </td>
299   </tr>
300   <tr>
301     <td><hr size=3 noshade></td>
302   </tr>
303 </table>
304
305 <input type=hidden name=path value=$form->{path}>
306 <input type=hidden name=rpw value=$form->{rpw}>
307 <input type=hidden name=root value="$form->{root}">
308
309 <br><input type=submit class=submit name=action value="|.$locale->text('Add User').qq|">
310 <input type=submit class=submit name=action value="|.$locale->text('Change Admin Password').qq|">
311
312 $dbdrivers
313
314 </form>
315
316 |.$locale->text('Click on login name to edit!').qq|
317 <br>
318 |.$locale->text('To add a user to a group edit a name, change the login name and save.  A new user with the same variables will then be saved under the new login name.').qq|
319
320 <p>
321
322 <form method=post action=login.pl>
323
324 <table border=0 width=100%>
325   <tr class=listheading>
326     <th>SQL-Ledger |.$locale->text('Accounting')." ".$locale->text('Login').qq|</th>
327   </tr>
328   <tr>
329     <td>
330       <table>
331         <tr>
332           <th align=right>|.$locale->text('Name').qq|</th>
333           <td><input class=login name=login></td>
334           <td>&nbsp;</td>
335         </tr>
336         <tr>
337           <th align=right>|.$locale->text('Password').qq|</th>
338           <td><input class=login type=password name=password></td>
339           <td><input type=submit name=action value="|.$locale->text('Login').qq|"></td>
340         </tr>
341 <input type=hidden name=path value=$form->{path}>
342       </table>
343     </td>
344   </tr>
345 </table>
346
347 </form>
348
349 </body>
350 </html>
351 |;
352
353 }
354
355
356
357
358 sub form_header {
359
360   # if there is a login, get user
361   if ($form->{login}) {
362     # get user
363     $myconfig = new User "$memberfile", "$form->{login}";
364
365     $myconfig->{signature} =~ s/\\n/\r\n/g;
366     $myconfig->{address} =~ s/\\n/\r\n/g;
367
368     # strip basedir from templates directory
369     $myconfig->{templates} =~ s/^$templates\///;
370
371     $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
372   }
373
374
375   foreach $item (qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd)) {
376     $dateformat .= ($item eq $myconfig->{dateformat}) ? "<option selected>$item\n" : "<option>$item\n";
377   }
378
379   foreach $item (qw(1,000.00 1000.00 1.000,00 1000,00)) {
380     $numberformat .= ($item eq $myconfig->{numberformat}) ? "<option selected>$item\n" : "<option>$item\n";
381   }
382
383
384   %countrycodes = User->country_codes;
385   $countrycodes = "";
386   foreach $key (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
387     $countrycodes .= ($myconfig->{countrycode} eq $key) ? "<option selected value=$key>$countrycodes{$key}" : "<option value=$key>$countrycodes{$key}";
388   }
389   $countrycodes = qq|<option value="">American English\n$countrycodes|;
390
391   # is there a templates basedir
392   if (! -d "$templates") {
393     $form->error($locale->text('Directory').": $templates ".$locale->text('does not exist'));
394   }
395
396   opendir TEMPLATEDIR, "$templates/." or $form->error("$templates : $!");
397   @all = grep !/^\.\.?$/, readdir TEMPLATEDIR;
398   closedir TEMPLATEDIR;
399
400   @allhtml = sort grep /\.html/, @all;
401   @alldir = grep !/\.(html|tex)$/, @all;
402   
403   @allhtml = reverse grep !/Default/, @allhtml;
404   push @allhtml, 'Default';
405   @allhtml = reverse @allhtml;
406   
407   foreach $item (@alldir) {
408     if ($item eq $myconfig->{templates}) {
409       $usetemplates .= qq|<option selected>$item\n|;
410     } else {
411       $usetemplates .= qq|<option>$item\n|;
412     }
413   }
414   
415   $lastitem = $allhtml[0];
416   $lastitem =~ s/-.*//g;
417   $mastertemplates = qq|<option>$lastitem\n|;
418   foreach $item (@allhtml) {
419     $item =~ s/-.*//g;
420     
421     if ($item ne $lastitem) {
422       $mastertemplates .= qq|<option>$item\n|;
423       $lastitem = $item;
424     }
425   }
426
427   
428   $form->header;
429  
430   print qq|
431 <body class=admin>
432
433 <form method=post action=$form->{script}>
434
435 <table width=100%>
436   <tr class=listheading><th colspan=2>$form->{title}</th></tr>
437   <tr size=5></tr>
438   <tr valign=top>
439     <td>
440       <table>
441         <tr>
442           <th align=right>|.$locale->text('Login').qq|</th>
443           <td><input name=login value="$myconfig->{login}"></td>
444         </tr>
445         <tr>
446           <th align=right>|.$locale->text('Password').qq|</th>
447           <td><input type=password name=password size=8 value=$myconfig->{password}></td>
448           <input type=hidden name=old_password value=$myconfig->{password}>
449         </tr>
450         <tr>
451           <th align=right>|.$locale->text('Name').qq|</th>
452           <td><input name=name size=15 value="$myconfig->{name}"></td>
453         </tr>
454         <tr>
455           <th align=right>|.$locale->text('E-mail').qq|</th>
456           <td><input name=email size=30 value="$myconfig->{email}"></td>
457         </tr>
458         <tr valign=top>
459           <th align=right>|.$locale->text('Signature').qq|</th>
460           <td><textarea name=signature rows=3 cols=35>$myconfig->{signature}</textarea></td>
461         </tr>
462         <tr>
463           <th align=right>|.$locale->text('Phone').qq|</th>
464           <td><input name=tel size=14 value="$myconfig->{tel}"></td>
465         </tr>
466         <tr>
467           <th align=right>|.$locale->text('Fax').qq|</th>
468           <td><input name=fax size=14 value="$myconfig->{fax}"></td>
469         </tr>
470         <tr>
471           <th align=right>|.$locale->text('Company').qq|</th>
472           <td><input name=company size=35 value="$myconfig->{company}"></td>
473         </tr>
474         <tr valign=top>
475           <th align=right>|.$locale->text('Address').qq|</th>
476           <td><textarea name=address rows=4 cols=35>$myconfig->{address}</textarea></td>
477         </tr>
478       </table>
479     </td>
480     <td>
481       <table>
482         <tr>
483           <th align=right>|.$locale->text('Date Format').qq|</th>
484           <td><select name=dateformat>$dateformat</select></td>
485         </tr>
486         <tr>
487           <th align=right>|.$locale->text('Number Format').qq|</th>
488           <td><select name=numberformat>$numberformat</select></td>
489         </tr>
490         <tr>
491           <th align=right>|.$locale->text('Dropdown Limit').qq|</th>
492           <td><input name=vclimit value="$myconfig->{vclimit}"></td>
493         </tr>
494         <tr>
495           <th align=right>|.$locale->text('Language').qq|</th>
496           <td><select name=countrycode>$countrycodes</select></td>
497         </tr>
498         <tr>
499           <th align=right>|.$locale->text('Character Set').qq|</th>
500           <td><input name=charset value="$myconfig->{charset}"></td>
501         </tr>
502         <tr>
503           <th align=right>|.$locale->text('Stylesheet').qq|</th>
504           <td><input name=userstylesheet value="$myconfig->{stylesheet}"></td>
505         </tr>
506         <tr>
507           <th align=right>|.$locale->text('Printer').qq|</th>
508           <td><input name=printer size=20 value="$myconfig->{printer}"></td>
509         </tr>
510         <tr>
511           <th align=right>|.$locale->text('Ship via').qq|</th>
512           <td><input name=shippingpoint size=15 value="$myconfig->{shippingpoint}"></td>
513         </tr>
514         <tr>
515           <th align=right>|.$locale->text('Use Templates').qq|</th>
516           <td><select name=usetemplates>$usetemplates</select></td>
517         </tr>
518         <tr>
519           <th align=right>|.$locale->text('New Templates').qq|</th>
520           <td><input name=newtemplates></td>
521         </tr>
522         <tr>
523           <th align=right>|.$locale->text('Setup Templates').qq|</th>
524           <td><select name=mastertemplates>$mastertemplates</select></td>
525         </tr>
526         <input type=hidden name=templates value=$myconfig->{templates}>
527       </table>
528     </td>
529   </tr>
530   <tr class=listheading>
531     <th colspan=2>|.$locale->text('Database').qq|</th>
532   </tr>|;
533
534     # list section for database drivers
535     foreach $item (User->dbdrivers) {
536       
537     print qq|
538   <tr>
539     <td colspan=2>
540       <table>
541         <tr>|;
542
543     $checked = "";
544     if ($myconfig->{dbdriver} eq $item) {
545       map { $form->{"${item}_$_"} = $myconfig->{$_} } qw(dbhost dbport dbuser dbpasswd dbname sid);
546       $checked = "checked";
547     }
548
549     print qq|
550           <th align=right>|.$locale->text('Driver').qq|</th>
551           <td><input name=dbdriver type=radio class=radio value=$item $checked>&nbsp;$item</td>
552           <th align=right>|.$locale->text('Host').qq|</th>
553           <td><input name="${item}_dbhost" value=$form->{"${item}_dbhost"}></td>
554         </tr>
555         <tr>|;
556
557     if ($item eq 'Pg') {
558       print qq|
559           <th align=right>|.$locale->text('Dataset').qq|</th>
560           <td><input name=Pg_dbname size=10 value=$form->{Pg_dbname}></td>
561           <th align=right>|.$locale->text('Port').qq|</th>
562           <td><input name=Pg_dbport size=4 value=$form->{Pg_dbport}></td>
563         </tr>
564         <tr>
565           <th align=right>|.$locale->text('User').qq|</th>
566           <td><input name="${item}_dbuser" size=10 value=$form->{"${item}_dbuser"}></td>
567           <th align=right>|.$locale->text('Password').qq|</th>
568           <td><input name="${item}_dbpasswd" type=password size=10 value=$form->{"${item}_dbpasswd"}></td>
569         </tr>|;
570
571     }
572
573     if ($item eq 'Oracle') {
574       print qq|
575           <th align=right>SID</th>
576           <td><input name=Oracle_sid value=$form->{Oracle_sid}></td>
577           <th align=right>|.$locale->text('Port').qq|</th>
578           <td><input name=Oracle_dbport size=4 value=$form->{Oracle_dbport}></td>
579         </tr>
580         <tr>
581           <th align=right>|.$locale->text('Dataset').qq|</th>
582           <td><input name="${item}_dbuser" size=10 value=$form->{"${item}_dbuser"}></td>
583           <th align=right>|.$locale->text('Password').qq|</th>
584           <td><input name="${item}_dbpasswd" type=password size=10 value=$form->{"${item}_dbpasswd"}></td>
585           
586         </tr>|;
587     }
588     
589       
590     print qq|
591         <input type=hidden name=old_dbpasswd value=$myconfig->{dbpasswd}>
592       </table>
593     </td>
594   </tr>
595   <tr>
596     <td colspan=2><hr size=2 noshade></td>
597   </tr>
598 |;
599
600   }
601
602
603   # access control
604   open(FH, $menufile) or $form->error("$menufile : $!");
605   # scan for first menu level
606   @a = <FH>;
607   close(FH);
608   
609   if (open(FH, "custom_$menufile")) {
610     push @a, <FH>;
611   }
612   close(FH);
613
614   foreach $item (@a) {
615     next unless $item =~ /\[/;
616     next if $item =~ /\#/;
617
618     $item =~ s/(\[|\])//g;
619     chop $item;
620
621     if ($item =~ /--/) {
622       ($level, $menuitem) = split /--/, $item, 2;
623     } else {
624       $level = $item;
625       $menuitem = $item;
626       push @acsorder, $item;
627     }
628
629     push @{ $acs{$level} }, $menuitem;
630
631   }
632   
633   $admincheck = "checked" if $myconfig->{admin};
634   
635   print qq|
636   <tr class=listheading>
637     <th colspan=2>|.$locale->text('Access Control').qq|</th>
638   </tr>
639   <tr>
640     <td><input name=admin type=checkbox class=checkbox value=1 $admincheck>&nbsp;<b>|.$locale->text('Administrator').qq|</b></td>
641   </tr>
642 |;
643   
644   foreach $item (split /;/, $myconfig->{acs}) {
645     ($key, $value) = split /--/, $item, 2;
646     $excl{$key}{$value} = 1;
647   }
648   
649   foreach $key (@acsorder) {
650
651     $checked = "checked";
652     if ($form->{login}) {
653       $checked = ($excl{$key}{$key}) ? "" : "checked";
654     }
655     
656     # can't have variable names with spaces
657     # the 1 is for apache 2
658     $item = $form->escape("${key}--$key", 1);
659     
660     $acsheading = $key;
661     $acsheading =~ s/ /&nbsp;/g;
662     
663     $acsheading = qq|
664     <th align=left><input name="$item" class=checkbox type=checkbox value=1 $checked>&nbsp;$acsheading</th>\n|;
665     $menuitems .= "$item;";
666     $acsdata = "
667     <td>";
668
669     foreach $item (@{ $acs{$key} }) {
670       next if ($key eq $item);
671
672       $checked = "checked";
673       if ($form->{login}) {
674         $checked = ($excl{$key}{$item}) ? "" : "checked";
675       }
676
677       $acsitem = $form->escape("${key}--$item", 1);
678       
679       $acsdata .= qq|
680     <br><input name="$acsitem" class=checkbox type=checkbox value=1 $checked>&nbsp;$item|;
681       $menuitems .= "$acsitem;";
682     }
683
684     $acsdata .= "
685     </td>";
686
687     print qq|
688   <tr valign=top>$acsheading $acsdata
689   </tr>
690 |;
691   }
692   
693   print qq|<input type=hidden name=acs value="$menuitems">
694   
695    <tr>
696     <td colspan=2><hr size=3 noshade></td>
697   </tr>
698   <tr>
699     <td>
700     </td>
701   </tr>
702 </table>
703 |;
704
705 }
706
707
708 sub save {
709
710   # no driver checked
711   $form->error($locale->text('Database Driver not checked!')) unless $form->{dbdriver};
712
713   # no spaces allowed in login name
714   ($form->{login}) = split / /, $form->{login};
715   
716   # check for duplicates
717   if (!$form->{edit}) {
718     $temp = new User "$memberfile", "$form->{login}";
719    
720     if ($temp->{login}) {
721       $form->error("$form->{login} ".$locale->text('is already a member!'));
722     }
723   }
724   
725   # does stylesheet exist
726   if ($form->{userstylesheet}) {
727     $form->error($locale->text('Stylesheet').": css/$form->{userstylesheet} ".$locale->text('does not exist')) unless (-f "css/$form->{userstylesheet}");
728   }
729   
730   # no spaces allowed in directories
731   ($form->{newtemplates}) = split / /, $form->{newtemplates};
732   
733   if ($form->{newtemplates}) {
734     $form->{templates} = $form->{newtemplates};
735   } else {
736     $form->{templates} = ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
737   }
738   
739   
740   # is there a basedir
741   if (! -d "$templates") {
742     $form->error($locale->text('Directory').": $templates ".$locale->text('does not exist'));
743   }
744
745   # add base directory to $form->{templates}
746   $form->{templates} = "$templates/$form->{templates}";
747
748
749   $myconfig = new User "$memberfile", "$form->{login}";
750
751   # redo acs variable and delete all the acs codes
752   @acs = split /;/, $form->{acs};
753
754   $form->{acs} = "";
755   foreach $item (@acs) {
756     $item = $form->escape($item, 1);
757
758     if (!$form->{$item}) {
759       $form->{acs} .= $form->unescape($form->unescape($item)).";";
760     }
761     delete $form->{$item};
762   }
763
764   # check which database was filled in
765   if ($form->{dbdriver} eq 'Oracle') {
766     $form->{sid} = $form->{Oracle_sid}, ;
767     $form->{dbhost} = $form->{Oracle_dbhost}, ;
768     $form->{dbport} = $form->{Oracle_dbport};
769     $form->{dbpasswd} = $form->{Oracle_dbpasswd};
770     $form->{dbuser} = $form->{Oracle_dbuser};
771     $form->{dbname} = $form->{Oracle_dbuser};
772
773     $form->isblank("dbhost", $locale->text('Hostname missing!'));
774     $form->isblank("dbport", $locale->text('Port missing!'));
775     $form->isblank("dbuser", $locale->text('Dataset missing!'));
776   }
777   if ($form->{dbdriver} eq 'Pg') {
778     $form->{dbhost} = $form->{Pg_dbhost};
779     $form->{dbport} = $form->{Pg_dbport};
780     $form->{dbpasswd} = $form->{Pg_dbpasswd};
781     $form->{dbuser} = $form->{Pg_dbuser};
782     $form->{dbname} = $form->{Pg_dbname};
783     
784     $form->isblank("dbname", $locale->text('Dataset missing!'));
785     $form->isblank("dbuser", $locale->text('Database User missing!'));
786   }
787     
788   # set admin
789   $form->{admin} = "" unless $form->{admin};
790   
791   foreach $item (keys %{$form}) {
792     $myconfig->{$item} = $form->{$item};
793   }
794
795   delete $myconfig->{stylesheet};
796   if ($form->{userstylesheet}) {
797     $myconfig->{stylesheet} = $form->{userstylesheet};
798   }
799   
800   $myconfig->save_member($memberfile, $userspath);
801
802   # create user template directory and copy master files
803   if (! -d "$form->{templates}") {
804     umask(002);
805     
806     if (mkdir "$form->{templates}", oct("771")) {
807       
808       umask(007);
809       
810       # copy templates to the directory
811       opendir TEMPLATEDIR, "$templates/." or $form-error("$templates : $!");
812       @templates = grep /$form->{mastertemplates}.*?\.(html|tex)$/, readdir TEMPLATEDIR;
813       closedir TEMPLATEDIR;
814
815       foreach $file (@templates) {
816         open(TEMP, "$templates/$file") or $form->error("$templates/$file : $!");
817         
818         $file =~ s/$form->{mastertemplates}-//;
819         open(NEW, ">$form->{templates}/$file") or $form->error("$form->{templates}/$file : $!");
820           
821         while ($line = <TEMP>) {
822           print NEW $line;
823         }
824         close(TEMP);
825         close(NEW);
826       }
827     } else {
828       $form->error("$!: $form->{templates}");
829     }
830   }
831
832   $form->redirect($locale->text('User saved!'));
833   
834 }
835
836
837 sub delete {
838
839   $form->{templates} = ($form->{templates}) ? "$templates/$form->{templates}" : "$templates/$form->{login}";
840   
841   $form->error("$memberfile : ".$locale->text('locked!')) if (-f ${memberfile}.LCK);
842   open(FH, ">${memberfile}.LCK") or $form->error("${memberfile}.LCK : $!");
843   close(FH);
844   
845   open(CONF, "+<$memberfile") or $form->error("$memberfile : $!");
846
847   @config = <CONF>;
848
849   seek(CONF, 0, 0);
850   truncate(CONF, 0);
851   
852   while ($line = shift @config) {
853
854     if ($line =~ /^\[/) {
855       last if ($line =~ /\[$form->{login}\]/);
856       $login = &login_name($line);
857     }
858     
859     if ($line =~ /^templates=/) {
860       $user{$login} = &get_value($line);
861     }
862
863     print CONF $line;
864   }
865
866   # remove everything up to next login or EOF
867   # and save template variable
868   while ($line = shift @config) {
869     if ($line =~ /^templates=/) {
870       $templatedir = &get_value($line);
871     }
872     last if ($line =~ /^\[/);
873   }
874
875   # this one is either the next login or EOF
876   print CONF $line;
877
878   $login = &login_name($line);
879   
880
881   while ($line = shift @config) {
882     if ($line =~ /^\[/) {
883       $login = &login_name($line);
884     }
885     
886     if ($line =~ /^templates=/) {
887       $user{$login} = &get_value($line);
888     }
889     
890     print CONF $line;
891   }
892
893   close(CONF);
894   unlink "${memberfile}.LCK";
895
896   # scan %user for $templatedir
897   foreach $login (keys %user) {
898     last if ($found = ($templatedir eq $user{$login}));
899   }
900
901   # if found keep directory otherwise delete
902   if (!$found) {
903     # delete it if there is a template directory
904     $dir = "$form->{templates}";
905     if (-d "$dir") {
906       unlink <$dir/*.html>;
907       unlink <$dir/*.tex>;
908       rmdir "$dir";
909     }
910   }
911   
912   # delete config file for user
913   unlink "$userspath/$form->{login}.conf";
914     
915   $form->redirect($locale->text('User deleted!'));
916   
917 }
918
919
920 sub login_name {
921   my $login = shift;
922   
923   $login =~ s/\[\]//g;
924   return ($login) ? $login : undef;
925   
926 }
927
928
929
930 sub get_value {
931   my $line = shift;
932   
933   my ($null, $value) = split(/=/, $line, 2);
934
935   # remove comments
936   $value =~ s/\s#.*//g;
937       
938   # remove any trailing whitespace
939   $value =~ s/^\s*(.*?)\s*$/$1/;
940
941   $value;
942 }
943
944
945
946 sub change_admin_password {
947
948   $form->{title} = qq|SQL-Ledger |.$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Change Admin Password');
949   
950   $form->header;
951
952   print qq|
953 <body class=admin>
954
955
956 <h2>|.$locale->text('Change Admin Password').qq|</h2>
957
958 <form method=post action=$form->{script}>
959
960 <b>|.$locale->text('Password').qq|</b> <input type=password name=password size=8>
961
962 <input type=hidden name=root value="$form->{root}">
963 <input type=hidden name=path value=$form->{path}>
964 <input type=hidden name=rpw value=$form->{rpw}>
965
966 <p>
967 <input type=submit class=submit name=action value="|.$locale->text('Change Password').qq|">
968
969 </form>
970
971 </body>
972 </html>
973 |;
974
975 }
976
977
978 sub change_password {
979
980   $root->{password} = $form->{password};
981   
982   $root->{'root login'} = 1;
983   $root->save_member($memberfile);
984
985   $form->{callback} = "$form->{script}?action=list_users&path=$form->{path}&root=$form->{root}&rpw=$root->{password}";
986
987   $form->redirect($locale->text('Password changed!'));
988
989 }
990
991
992 sub check_password {
993
994   $root = new User "$memberfile", $form->{root};
995
996   if ($root->{password}) {
997     if ($root->{password} ne $form->{rpw}) {
998       $form->error($locale->text('Incorrect Password!'));
999     }
1000   }
1001
1002 }
1003
1004
1005 sub pg_database_administration {
1006
1007   $form->{dbdriver} = 'Pg';
1008   &dbselect_source;
1009
1010 }
1011
1012
1013 sub oracle_database_administration {
1014   
1015   $form->{dbdriver} = 'Oracle';
1016   &dbselect_source;
1017
1018 }
1019
1020
1021 sub dbdriver_defaults {
1022
1023   # load some defaults for the selected driver
1024   %driverdefaults = ( 'Pg' => { dbport => '',
1025                                 dbuser => 'sql-ledger',
1026                              dbdefault => 'template1',
1027                                 dbhost => '',
1028                          connectstring => $locale->text('Connect to')
1029                               },
1030                   'Oracle' => { dbport => '1521',
1031                                 dbuser => 'oralin',
1032                              dbdefault => $sid,
1033                                 dbhost => `hostname`,
1034                          connectstring => 'SID'
1035                               }
1036                     );
1037
1038   map { $form->{$_} = $driverdefaults{$form->{dbdriver}}{$_} } keys %{ $driverdefaults{Pg} };
1039   
1040 }
1041   
1042
1043 sub dbselect_source {
1044
1045   &dbdriver_defaults;
1046
1047   $msg{Pg} = $locale->text('Leave host and port field empty unless you want to make a remote connection.');
1048   $msg{Oracle} = $locale->text('You must enter a host and port for local and remote connections!');
1049   
1050
1051  $form->{title} = "SQL-Ledger ".$locale->text('Accounting')." / ".$locale->text('Database Administration');
1052   
1053
1054   $form->header;
1055
1056   print qq|
1057 <body class=admin>
1058
1059
1060 <center>
1061 <h2>$form->{title}</h2>
1062
1063 <form method=post action=$form->{script}>
1064
1065 <table>
1066 <tr><td>
1067
1068 <table>
1069
1070   <tr class=listheading>
1071     <th colspan=4>|.$locale->text('Database').qq|</th>
1072   </tr>
1073
1074 <input type=hidden name=dbdriver value=$form->{dbdriver}>
1075
1076   <tr><td>
1077    <table>
1078
1079   <tr>
1080
1081     <th align=right>|.$locale->text('Host').qq|</th>
1082     <td><input name=dbhost size=25 value=$form->{dbhost}></td>
1083     <th align=right>|.$locale->text('Port').qq|</th>
1084     <td><input name=dbport size=5 value=$form->{dbport}></td>
1085
1086   </tr>
1087
1088   <tr>
1089
1090     <th align=right>|.$locale->text('User').qq|</th>
1091     <td><input name=dbuser size=10 value=$form->{dbuser}></td>
1092     <th align=right>|.$locale->text('Password').qq|</th>
1093     <td><input type=password name=dbpasswd size=10></td>
1094
1095   </tr>
1096   
1097   <tr>
1098
1099     <th align=right>$form->{connectstring}</th>
1100     <td colspan=3><input name=dbdefault size=10 value=$form->{dbdefault}></td>
1101
1102   </tr>
1103
1104 </table>
1105
1106 </td></tr>
1107 </table>
1108
1109 <input name=callback type=hidden value="$form->{script}?action=list_users&path=$form->{path}&root=$form->{root}&rpw=$form->{rpw}">
1110 <input type=hidden name=root value="$form->{root}">
1111 <input type=hidden name=path value=$form->{path}>
1112 <input type=hidden name=rpw value=$form->{rpw}>
1113
1114 <hr size=3 noshade>
1115 <br>
1116
1117 <input type=submit class=submit name=action value="|.$locale->text('Create Dataset').qq|">
1118 <input type=submit class=submit name=action value="|.$locale->text('Update Dataset').qq|">
1119 <input type=submit class=submit name=action value="|.$locale->text('Delete Dataset').qq|">
1120
1121 </form>
1122
1123 </td></tr>
1124 </table>
1125
1126 <p>|.$locale->text('This is a preliminary check for existing sources. Nothing will be created or deleted at this stage!')
1127
1128 .qq|
1129 <br>$msg{$form->{dbdriver}}
1130
1131
1132 </body>
1133 </html>
1134 |;
1135
1136 }
1137
1138
1139 sub continue {
1140
1141   &{ $form->{nextsub} };
1142
1143 }
1144
1145
1146 sub update_dataset {
1147
1148   %needsupdate = User->dbneedsupdate(\%$form);
1149
1150   $form->{title} = "SQL-Ledger ".$locale->text('Accounting')." ".$locale->text('Database Administration')." / ".$locale->text('Update Dataset');
1151   
1152   $form->header;
1153
1154   print qq|
1155 <body class=admin>
1156
1157
1158 <center>
1159 <h2>$form->{title}</h2>
1160 |;
1161
1162
1163   foreach $key (sort keys %needsupdate) {
1164     if ($needsupdate{$key} lt $form->{dbversion}) {
1165       $upd .= qq|<br><input name="db$key" type=checkbox value=1 checked> $key\n|;
1166       $form->{dbupdate} .= "db$key ";
1167     }
1168   }
1169
1170   chop $form->{dbupdate};
1171
1172
1173   if ($form->{dbupdate}) {
1174
1175     print qq|
1176 <table>
1177 <form method=post action=$form->{script}>
1178
1179 <input type=hidden name=dbdriver value=$form->{dbdriver}>
1180 <input type=hidden name=dbhost value=$form->{dbhost}>
1181 <input type=hidden name=dbport value=$form->{dbport}>
1182 <input type=hidden name=dbuser value=$form->{dbuser}>
1183 <input type=hidden name=dbpasswd value=$form->{dbpasswd}>
1184 <input type=hidden name=dbdefault value=$form->{dbdefault}>
1185
1186 <tr class=listheading>
1187   <th>|.$locale->text('The following Datasets need to be updated').qq|</th>
1188 </tr>
1189 <tr>
1190 <td>
1191
1192 $upd
1193
1194 </td></tr>
1195 <tr><td>
1196
1197 <input name=dbupdate type=hidden value="$form->{dbupdate}">
1198
1199 <input name=callback type=hidden value="$form->{script}?action=list_users&path=$form->{path}&root=$form->{root}&rpw=$form->{rpw}">
1200
1201 <input type=hidden name=root value="$form->{root}">
1202 <input type=hidden name=path value=$form->{path}>
1203 <input type=hidden name=rpw value=$form->{rpw}>
1204
1205 <input type=hidden name=nextsub value=dbupdate>
1206
1207 <input type=submit class=submit name=action value="|.$locale->text('Continue').qq|">
1208
1209 </td></tr>
1210 </table>
1211 </form>
1212 |;
1213
1214   } else {
1215
1216     print $locale->text('All Datasets up to date!');
1217
1218   }
1219   
1220   print qq|
1221
1222 </body>
1223 </html>
1224 |;
1225
1226 }
1227
1228
1229 sub dbupdate {
1230
1231   User->dbupdate(\%$form);
1232
1233   $form->redirect($locale->text('Dataset updated!'));
1234   
1235 }
1236
1237
1238 sub create_dataset {
1239
1240   foreach $item (sort User->dbsources(\%$form)) {
1241     $dbsources .= "[$item] ";
1242   }
1243
1244   opendir SQLDIR, "sql/." or $form-error($!);
1245   foreach $item (sort grep /-chart\.sql/, readdir SQLDIR) {
1246     next if ($item eq 'Default-chart.sql');
1247     $item =~ s/-chart\.sql//;
1248     push @charts, qq| <input name=chart class=radio type=radio value="$item">&nbsp;$item|;
1249   }
1250   closedir SQLDIR;
1251
1252   # add Default at beginning
1253   @charts = (qq|<input name=chart class=radio type=radio value="Default" checked>&nbsp;Default|, @charts);
1254
1255   $selectencoding = qq|<option> 
1256   <option value=SQL_ASCII>ASCII
1257   <option value=EUC_JP>Japanese Extended UNIX Code
1258   <option value=EUC_CN>Chinese Extended UNIX Code 
1259   <option value=EUC_KR>Korean Extended UNIX Code
1260   <option value=EUC_TW>Taiwan Extended UNIX Code
1261   <option value=UNICODE>UTF-8 Unicode 
1262   <option value=MULE_INTERNAL>Mule internal type
1263   <option value=LATIN1>ISO 8859-1
1264   <option value=LATIN2>ISO 8859-2 
1265   <option value=LATIN3>ISO 8859-3
1266   <option value=LATIN4>ISO 8859-4
1267   <option value=LATIN5>ISO 8859-5
1268   <option value=KOI8>KOI8-R
1269   <option value=WIN>Windows CP1251
1270   <option value=ALT>Windows CP866
1271   |;
1272   
1273   $form->{title} = "SQL-Ledger ".$locale->text('Accounting')." ".$locale->text('Database Administration')." / ".$locale->text('Create Dataset');
1274   
1275   $form->header;
1276
1277   print qq|
1278 <body class=admin>
1279
1280
1281 <center>
1282 <h2>$form->{title}</h2>
1283
1284 <form method=post action=$form->{script}>
1285
1286 <table>
1287   <tr class=listheading>
1288     <th colspan=2>&nbsp;</th>
1289   </tr>
1290
1291   <tr>
1292
1293     <th align=right nowrap>|.$locale->text('Existing Datasets').qq|</th>
1294     <td>$dbsources</td>
1295
1296   </tr>
1297   
1298   <tr>
1299
1300     <th align=right nowrap>|.$locale->text('Create Dataset').qq|</th>
1301     <td><input name=db></td>
1302
1303   </tr>
1304   
1305   <tr>
1306
1307     <th align=right nowrap>|.$locale->text('Multibyte Encoding').qq|</th>
1308     <td><select name=encoding>$selectencoding</select></td>
1309
1310   </tr>
1311  
1312   <tr>
1313
1314     <th align=right>|.$locale->text('Create Chart of Accounts').qq|</th>
1315     <td>@charts</td>
1316
1317   </tr>
1318  
1319   <tr><td colspan=2>
1320 <hr size=3 noshade>
1321 <br>
1322 <input type=hidden name=dbdriver value=$form->{dbdriver}>
1323 <input type=hidden name=dbuser value=$form->{dbuser}>
1324 <input type=hidden name=dbhost value=$form->{dbhost}>
1325 <input type=hidden name=dbport value=$form->{dbport}>
1326 <input type=hidden name=dbpasswd value=$form->{dbpasswd}>
1327 <input type=hidden name=dbdefault value=$form->{dbdefault}>
1328
1329 <input name=callback type=hidden value="$form->{script}?action=list_users&path=$form->{path}&root=$form->{root}&rpw=$form->{rpw}">
1330
1331 <input type=hidden name=root value="$form->{root}">
1332 <input type=hidden name=path value=$form->{path}>
1333 <input type=hidden name=rpw value=$form->{rpw}>
1334
1335 <input type=hidden name=nextsub value=dbcreate>
1336
1337 <input type=submit class=submit name=action value="|.$locale->text('Continue').qq|">
1338
1339   </td></tr>
1340 </table>
1341
1342 </form>
1343
1344
1345 </body>
1346 </html>
1347 |;
1348
1349 }
1350
1351
1352 sub dbcreate {
1353
1354   $form->isblank("db", $locale->text('Dataset missing!'));
1355
1356   User->dbcreate(\%$form);
1357   
1358   $form->{title} = "SQL-Ledger ".$locale->text('Accounting')." ".$locale->text('Database Administration')." / ".$locale->text('Create Dataset');
1359
1360   $form->header;
1361
1362   print qq|
1363 <body class=admin>
1364
1365
1366 <center>
1367 <h2>$form->{title}</h2>
1368
1369 <form method=post action=$form->{script}>|
1370
1371 .$locale->text('Dataset')." $form->{db} ".$locale->text('successfully created!')
1372
1373 .qq|
1374
1375 <input type=hidden name=root value="$form->{root}">
1376 <input type=hidden name=path value="$form->{path}">
1377 <input type=hidden name=rpw value="$form->{rpw}">
1378
1379 <input type=hidden name=nextsub value=list_users>
1380
1381 <p><input type=submit class=submit name=action value="|.$locale->text('Continue').qq|">
1382 </form>
1383
1384
1385 </body>
1386 </html>
1387 |;
1388
1389 }
1390
1391
1392 sub delete_dataset {
1393
1394   if (@dbsources = User->dbsources_unused(\%$form, $memberfile)) {
1395     foreach $item (sort @dbsources) {
1396       $dbsources .= qq|<input name=db class=radio type=radio value=$item>&nbsp;$item\n<br>|;
1397     }
1398   } else {
1399     $form->error($locale->text('Nothing to delete!'));
1400   }
1401
1402   $form->{title} = "SQL-Ledger ".$locale->text('Accounting')." ".$locale->text('Database Administration')." / ".$locale->text('Delete Dataset');
1403
1404   $form->header;
1405
1406   print qq|
1407 <body class=admin>
1408
1409
1410 <center>
1411 <h2>$form->{title}</h2>
1412
1413 <form method=post action=$form->{script}>
1414
1415 <table>
1416   <tr class=listheading>
1417     <th>|.$locale->text('The following Datasets are not in use and can be deleted').qq|</th>
1418   </tr>
1419
1420   <tr>
1421     <td>
1422     $dbsources
1423     </td>
1424   </tr>
1425   
1426   <tr><td>
1427
1428 <hr size=3 noshade>
1429 <br>
1430 <input type=hidden name=dbdriver value=$form->{dbdriver}>
1431 <input type=hidden name=dbuser value=$form->{dbuser}>
1432 <input type=hidden name=dbhost value=$form->{dbhost}>
1433 <input type=hidden name=dbport value=$form->{dbport}>
1434 <input type=hidden name=dbpasswd value=$form->{dbpasswd}>
1435 <input type=hidden name=dbdefault value=$form->{dbdefault}>
1436
1437 <input name=callback type=hidden value="$form->{script}?action=list_users&path=$form->{path}&root=$form->{root}&rpw=$form->{rpw}">
1438
1439 <input type=hidden name=root value="$form->{root}">
1440 <input type=hidden name=path value="$form->{path}">
1441 <input type=hidden name=rpw value="$form->{rpw}">
1442
1443 <input type=hidden name=nextsub value=dbdelete>
1444
1445 <input type=submit class=submit name=action value="|.$locale->text('Continue').qq|">
1446
1447   </td></tr>
1448 </table>
1449
1450 </form>
1451
1452 <p>|.$locale->text('Select a Dataset to delete and press "Continue"')
1453
1454 .qq|
1455
1456
1457 </body>
1458 </html>
1459 |;
1460
1461 }
1462
1463
1464 sub dbdelete {
1465
1466   if (!$form->{db}) {
1467     $form->error($locale->text('No Dataset selected!'));
1468   }
1469
1470   User->dbdelete(\%$form);
1471
1472   $form->{title} = "SQL-Ledger ".$locale->text('Accounting')." ".$locale->text('Database Administration')." / ".$locale->text('Delete Dataset');
1473
1474   $form->header;
1475
1476   print qq|
1477 <body class=admin>
1478
1479
1480 <center>
1481 <h2>$form->{title}</h2>
1482
1483 <form method=post action=$form->{script}>
1484
1485 $form->{db} |.$locale->text('successfully deleted!')
1486
1487 .qq|
1488
1489 <input type=hidden name=root value="$form->{root}">
1490 <input type=hidden name=path value="$form->{path}">
1491 <input type=hidden name=rpw value="$form->{rpw}">
1492
1493 <input type=hidden name=nextsub value=list_users>
1494
1495 <p><input type=submit class=submit name=action value="|.$locale->text('Continue').qq|">
1496 </form>
1497
1498
1499 </body>
1500 </html>
1501 |;
1502
1503 }
1504