summaryrefslogtreecommitdiff
path: root/bin/admin.pl
blob: 5a436ea41a9351380b21bf98e1009923bc98ab73 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. #
  7. # Copyright (C) 2006
  8. # This work contains copyrighted information from a number of sources all used
  9. # with permission.
  10. #
  11. # This file contains source code included with or based on SQL-Ledger which
  12. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  13. # under the GNU General Public License version 2 or, at your option, any later
  14. # version. For a full list including contact information of contributors,
  15. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  16. #
  17. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  18. # Copyright (c) 2000
  19. #
  20. # Author: DWS Systems Inc.
  21. # Web: http://www.sql-ledger.org
  22. #
  23. # Contributors:
  24. #
  25. #
  26. #======================================================================
  27. #
  28. # This file has undergone whitespace cleanup.
  29. #
  30. #======================================================================
  31. #
  32. # setup module
  33. # add/edit/delete users
  34. #
  35. #======================================================================
  36. $menufile = "menu.ini";
  37. use LedgerSMB::Form;
  38. use LedgerSMB::Locale;
  39. use LedgerSMB::User;
  40. use LedgerSMB::Session;
  41. $form = new Form;
  42. $locale = LedgerSMB::Locale->get_handle(${LedgerSMB::Sysconfig::language}) or
  43. $form->error(__FILE__.':'.__LINE__.': '."Locale not loaded: $!\n");
  44. $locale->encoding('UTF-8');
  45. $form->{charset} = 'UTF-8';
  46. #$form->{charset} = $locale->encoding;
  47. eval { require DBI; };
  48. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('DBI not installed!')) if ($@);
  49. $form->{stylesheet} = "ledger-smb.css";
  50. $form->{favicon} = "favicon.ico";
  51. $form->{timeout} = 600;
  52. require "bin/pw.pl";
  53. # customization
  54. if (-f "bin/custom/$form->{script}") {
  55. eval { require "bin/custom/$form->{script}"; };
  56. $form->error(__FILE__.':'.__LINE__.': '.$@) if ($@);
  57. }
  58. if ($form->{action}) {
  59. &check_password unless $form->{action} eq 'logout';
  60. &{ $form->{action} };
  61. } else {
  62. # if there are no drivers bail out
  63. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('No Database Drivers available!')) unless (LedgerSMB::User->dbdrivers);
  64. $root = LedgerSMB::User->new('admin');
  65. &adminlogin;
  66. }
  67. 1;
  68. # end
  69. sub adminlogin {
  70. my ($errorMessage) = @_;
  71. $form->{title} = qq|LedgerSMB $form->{version} |.$locale->text('Administration');
  72. $myheaderadd = qq|
  73. <script language="JavaScript" type="text/javascript">
  74. <!--
  75. function sf(){
  76. document.admin.password.focus();
  77. }
  78. // End -->
  79. </script>
  80. |;
  81. $form->header(undef, $myheaderadd);
  82. print qq|
  83. <body class="admin" onload="sf()">
  84. <div align="center">
  85. <a href="http://www.ledgersmb.org/"><img src="ledger-smb.png" width="200" height="100" border="0" alt="LedgerSMB Logo" /></a>
  86. <h1 class="login">|.$locale->text('Version').qq| $form->{version} <br />|.$locale->text('Administration').qq|</h1>
  87. <form method="post" action="$form->{script}" name="admin">
  88. <table>
  89. <tr>
  90. <th>|.$locale->text('Password').qq|</th>
  91. <td><input type="password" name="password" /></td>
  92. <td><button type="submit" class="submit" name="action" value="login">|.$locale->text('Login').qq|</button></td>
  93. </tr>
  94. </table>
  95. <input type="hidden" name="action" value="login" />
  96. <input type="hidden" name="path" value="$form->{path}" />
  97. </form>
  98. |;
  99. if($errorMessage){
  100. print qq|<p><span style="font-weight:bold; color:red;">$errorMessage</span></p><br />|;
  101. }
  102. print qq|
  103. <br /><br />
  104. <p><a href="login.pl"
  105. >|.$locale->text("Application Login").qq|</a></p>
  106. <br /><br />
  107. <a style="font-size: 0.8em;" href="http://www.ledgersmb.org/">|.$locale->text('LedgerSMB website').qq|</a>
  108. </div>
  109. </body>
  110. </html>
  111. |;
  112. }
  113. sub login {
  114. &list_users;
  115. }
  116. sub logout {
  117. $form->{callback} = "admin.pl?action=adminlogin";
  118. Session::session_destroy($form);
  119. $form->redirect($locale->text('You are logged out'));
  120. }
  121. sub add_user {
  122. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Add User');
  123. if (-f "css/ledger-smb.css") {
  124. $myconfig->{stylesheet} = "ledger-smb.css";
  125. }
  126. $myconfig->{vclimit} = 1000;
  127. $myconfig->{menuwidth} = 155;
  128. $myconfig->{timeout} = 3600;
  129. &form_header;
  130. &form_footer;
  131. }
  132. sub edit {
  133. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Edit User');
  134. $form->{edit} = 1;
  135. &form_header;
  136. &form_footer;
  137. }
  138. sub form_footer {
  139. if ($form->{edit}) {
  140. $delete = qq|<button type="submit" class="submit" name="action" value="delete">|.$locale->text('Delete').qq|</button>
  141. <input type="hidden" name="edit" value="1" />|;
  142. }
  143. print qq|
  144. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}" />
  145. <input type="hidden" name="path" value="$form->{path}" />
  146. <button type="submit" class="submit" name="action" value="save">|.$locale->text('Save').qq|</button>
  147. $delete
  148. </form>
  149. </body>
  150. </html>
  151. |;
  152. }
  153. sub list_users {
  154. #currently, this is disabled, but will set a value in the central db
  155. #$nologin = qq|<button type="submit" class="submit" name="action" value="lock_system">|.$locale->text('Lock System').qq|</button>|;
  156. #
  157. #if (-e "${LedgerSMB::Sysconfig::userspath}/nologin") {
  158. # $nologin = qq|<button type="submit" class="submit" name="action" value="unlock_system">|.$locale->text('Unlock System').qq|</button>|;
  159. #}
  160. # use the central database handle
  161. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  162. my $fetchMembers = $dbh->selectall_arrayref("SELECT uc.name, uc.company, uc.templates,
  163. uc.dbuser, uc.dbdriver, uc.dbname,
  164. uc.dbhost, u.username
  165. FROM users as u, users_conf as uc
  166. WHERE u.id = uc.id
  167. AND u.id > 1
  168. ORDER BY u.username;", { Slice => {} });
  169. my @memberArray = ();
  170. my @member = ();
  171. foreach my $memberArray ( @$fetchMembers ) {
  172. $member{$memberArray->{username}} = $memberArray;
  173. }
  174. # type=submit $locale->text('Pg Database Administration')
  175. # type=submit $locale->text('PgPP Database Administration')
  176. foreach $item (LedgerSMB::User->dbdrivers) {
  177. $dbdrivers .= qq|<button name="action" type="submit" class="submit" value="|.(lc $item).'_database_administration">'.$locale->text("$item Database Administration").qq|</button>|;
  178. }
  179. $column_header{login} = qq|<th>|.$locale->text('Login').qq|</th>|;
  180. $column_header{name} = qq|<th>|.$locale->text('Name').qq|</th>|;
  181. $column_header{company} = qq|<th>|.$locale->text('Company').qq|</th>|;
  182. $column_header{dbdriver} = qq|<th>|.$locale->text('Driver').qq|</th>|;
  183. $column_header{dbhost} = qq|<th>|.$locale->text('Host').qq|</th>|;
  184. $column_header{dataset} = qq|<th>|.$locale->text('Dataset').qq|</th>|;
  185. $column_header{templates} = qq|<th>|.$locale->text('Templates').qq|</th>|;
  186. @column_index = qw(login name company dbdriver dbhost dataset templates);
  187. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." ".$locale->text('Administration');
  188. $form->{login} = "admin";
  189. $form->header;
  190. print qq|
  191. <body class="admin">
  192. <form method="post" action="$form->{script}">
  193. <table width="100%">
  194. <tr class="listheading">
  195. <th>$form->{title}</th>
  196. </tr>
  197. <tr size="5"></tr>
  198. <tr>
  199. <td>
  200. <table width="100%">
  201. <tr class="listheading">|;
  202. for (@column_index) { print "$column_header{$_}\n" }
  203. print qq| </tr>|;
  204. foreach $key (sort keys %member) {
  205. $href = "$script?action=edit&amp;login=$key&amp;path=$form->{path}";
  206. $href =~ s/ /%20/g;
  207. $member{$key}{templates} =~ s/^${LedgerSMB::Sysconfig::templates}\///;
  208. $member{$key}{dbhost} = $locale->text('localhost') unless $member{$key}{dbhost};
  209. $column_data{login} = qq|<td><a href="$href">$key</a></td>|;
  210. $column_data{name} = qq|<td>$member{$key}{name}</td>|;
  211. $column_data{company} = qq|<td>$member{$key}{company}</td>|;
  212. $column_data{dbdriver} = qq|<td>$member{$key}{dbdriver}</td>|;
  213. $column_data{dbhost} = qq|<td>$member{$key}{dbhost}</td>|;
  214. $column_data{dataset} = qq|<td>$member{$key}{dbname}</td>|;
  215. $column_data{templates} = qq|<td>$member{$key}{templates}</td>|;
  216. $i++; $i %= 2;
  217. print qq| <tr class="listrow$i">|;
  218. for (@column_index) { print "$column_data{$_}\n"; }
  219. print qq| </tr>|;
  220. }
  221. print qq| </table>
  222. </td>
  223. </tr>
  224. <tr>
  225. <td><hr size="3" noshade /></td>
  226. </tr>
  227. </table>
  228. <input type="hidden" name="path" value="$form->{path}" />
  229. <br />
  230. <button type="submit" class="submit" name="action" value="add_user">|.$locale->text('Add User').qq|</button>
  231. <button type="submit" class="submit" name="action" value="change_admin_password">|.$locale->text('Change Admin Password').qq|</button>
  232. $dbdrivers
  233. $nologin
  234. <button type="submit" class="submit" name="action" value="logout">|.$locale->text('Logout').qq|</button>
  235. </form>
  236. |.$locale->text('Click on login name to edit!').qq|
  237. <br />
  238. |.$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|
  239. </body>
  240. </html>|;
  241. }
  242. sub form_header {
  243. # if there is a login, get user
  244. if ($form->{login}) {
  245. # get user
  246. %{$myconfig} = %{LedgerSMB::User->fetch_config($form->{login})};
  247. for (qw(company address signature)) { $myconfig->{$_} = $form->quote($myconfig->{$_}) }
  248. for (qw(address signature)) { $myconfig->{$_} =~ s/\\n/\n/g }
  249. # strip basedir from templates directory
  250. $myconfig->{templates} =~ s/^${LedgerSMB::Sysconfig::templates}\///;
  251. }
  252. foreach $item (qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd)) {
  253. $dateformat .= ($item eq $myconfig->{dateformat}) ? "<option selected>$item</option>\n" : "<option>$item</option>\n";
  254. }
  255. my @formats = qw(1,000.00 1000.00 1.000,00 1000,00 1'000.00);
  256. push @formats, '1 000.00';
  257. foreach $item (@formats) {
  258. $numberformat .= ($item eq $myconfig->{numberformat}) ? "<option selected>$item</option>\n" : "<option>$item</option>\n";
  259. }
  260. %countrycodes = LedgerSMB::User->country_codes;
  261. $countrycodes = "";
  262. my $selectedcode = ($myconfig->{countrycode}) ?
  263. $myconfig->{countrycode} : 'en';
  264. foreach $key (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
  265. $countrycodes .= ($selectedcode eq $key) ? qq|<option selected value="$key">$countrycodes{$key}</option>|
  266. : qq|<option value="$key">$countrycodes{$key}</option>|;
  267. }
  268. # is there a templates basedir
  269. if (! -d "${LedgerSMB::Sysconfig::templates}") {
  270. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('Directory [_1] does not exist', ${LedgerSMB::Sysconfig::templates}));
  271. }
  272. opendir TEMPLATEDIR, "${LedgerSMB::Sysconfig::templates}/." or $form->error(__FILE__.':'.__LINE__.': '."$templates : $!");
  273. @all = grep !/^\.\.?$/, readdir TEMPLATEDIR;
  274. closedir TEMPLATEDIR;
  275. @allhtml = sort grep /\.html/, @all;
  276. @alldir = ();
  277. for (@all) {
  278. if (-d "${LedgerSMB::Sysconfig::templates}/$_") {
  279. push @alldir, $_;
  280. }
  281. }
  282. @allhtml = reverse grep !/Default/, @allhtml;
  283. push @allhtml, 'Default';
  284. @allhtml = reverse @allhtml;
  285. foreach $item (sort @alldir) {
  286. if ($item eq $myconfig->{templates}) {
  287. $usetemplates .= qq|<option selected value="$item">$item</option>\n|;
  288. } else {
  289. $usetemplates .= qq|<option value="$item">$item</option>\n|;
  290. }
  291. }
  292. $lastitem = $allhtml[0];
  293. $lastitem =~ s/-.*//g;
  294. $mastertemplates = qq|<option value="$lastitem">$lastitem</option>\n|;
  295. foreach $item (@allhtml) {
  296. $item =~ s/-.*//g;
  297. if ($item ne $lastitem) {
  298. $mastertemplates .= qq|<option value="$item">$item</option>\n|;
  299. $lastitem = $item;
  300. }
  301. }
  302. opendir CSS, "css/.";
  303. @all = grep /.*\.css$/, readdir CSS;
  304. closedir CSS;
  305. foreach $item (@all) {
  306. if ($item eq $myconfig->{stylesheet}) {
  307. $selectstylesheet .= qq|<option selected value="$item">$item</option>\n|;
  308. } else {
  309. $selectstylesheet .= qq|<option value="$item">$item</option>\n|;
  310. }
  311. }
  312. $selectstylesheet .= "<option></option>\n";
  313. if (%{LedgerSMB::Sysconfig::printer} && ${LedgerSMB::Sysconfig::latex}) {
  314. $selectprinter = "<option></option>\n";
  315. foreach $item (sort keys %{LedgerSMB::Sysconfig::printer}) {
  316. if ($myconfig->{printer} eq $item) {
  317. $selectprinter .= qq|<option value="$item" selected>$item</option>\n|;
  318. } else {
  319. $selectprinter .= qq|<option value="$item">$item</option>\n|;
  320. }
  321. }
  322. $printer = qq|
  323. <tr>
  324. <th align="right">|.$locale->text('Printer').qq|</th>
  325. <td><select name="printer">$selectprinter</select></td>
  326. </tr>
  327. |;
  328. }
  329. $user = $form->{login};
  330. $form->{login} = "admin";
  331. $form->header;
  332. $form->{login} = $user;
  333. print qq|
  334. <body class="admin">
  335. <form method="post" action="admin.pl">
  336. <table width="100%">
  337. <tr class="listheading"><th colspan="2">$form->{title}</th></tr>
  338. <tr size="5"></tr>
  339. <tr valign="top">
  340. <td>
  341. <table>
  342. <tr>
  343. <th align="right">|.$locale->text('Login').qq|</th>
  344. <td><input name="login" value="$myconfig->{login}" /></td>
  345. </tr>
  346. <tr>
  347. <th align="right">|.$locale->text('Password').qq|</th>
  348. <td><input type="password" name="new_password" size="8" value="$myconfig->{password}" /></td>
  349. </tr>
  350. <tr>
  351. <th align="right">|.$locale->text('Name').qq|</th>
  352. <td><input name="name" size="15" value="$myconfig->{name}" /></td>
  353. </tr>
  354. <tr>
  355. <th align="right">|.$locale->text('E-mail').qq|</th>
  356. <td><input name="email" size="30" value="$myconfig->{email}" /></td>
  357. </tr>
  358. <tr valign="top">
  359. <th align="right">|.$locale->text('Signature').qq|</th>
  360. <td><textarea name="signature" rows="3" cols="35">$myconfig->{signature}</textarea></td>
  361. </tr>
  362. <tr>
  363. <th align="right">|.$locale->text('Phone').qq|</th>
  364. <td><input name="tel" size="14" value="$myconfig->{tel}" /></td>
  365. </tr>
  366. <tr>
  367. <th align="right">|.$locale->text('Fax').qq|</th>
  368. <td><input name="fax" size="14" value="$myconfig->{fax}" /></td>
  369. </tr>
  370. <tr>
  371. <th align="right">|.$locale->text('Company').qq|</th>
  372. <td><input name="company" size="35" value="$myconfig->{company}" /></td>
  373. </tr>
  374. <tr valign="top">
  375. <th align="right">|.$locale->text('Address').qq|</th>
  376. <td><textarea name="address" rows="4" cols="35">$myconfig->{address}</textarea></td>
  377. </tr>
  378. </table>
  379. </td>
  380. <td>
  381. <table>
  382. <tr>
  383. <th align="right">|.$locale->text('Date Format').qq|</th>
  384. <td><select name="dateformat">$dateformat</select></td>
  385. </tr>
  386. <tr>
  387. <th align="right">|.$locale->text('Number Format').qq|</th>
  388. <td><select name="numberformat">$numberformat</select></td>
  389. </tr>
  390. <tr>
  391. <th align="right">|.$locale->text('Dropdown Limit').qq|</th>
  392. <td><input name="vclimit" value="$myconfig->{vclimit}" /></td>
  393. </tr>
  394. <tr>
  395. <th align="right">|.$locale->text('Menu Width').qq|</th>
  396. <td><input name="menuwidth" value="$myconfig->{menuwidth}" /></td>
  397. </tr>
  398. <tr>
  399. <th align="right">|.$locale->text('Language').qq|</th>
  400. <td><select name="countrycode">$countrycodes</select></td>
  401. </tr>
  402. <tr>
  403. <th align="right">|.$locale->text('Session Timeout').qq|</th>
  404. <td><input name="newtimeout" value="$myconfig->{timeout}" /></td>
  405. </tr>
  406. <tr>
  407. <th align="right">|.$locale->text('Stylesheet').qq|</th>
  408. <td><select name="userstylesheet">$selectstylesheet</select></td>
  409. </tr>
  410. $printer
  411. <tr>
  412. <th align="right">|.$locale->text('Use Templates').qq|</th>
  413. <td><select name="usetemplates">$usetemplates</select></td>
  414. </tr>
  415. <tr>
  416. <th align="right">|.$locale->text('New Templates').qq|</th>
  417. <td><input name="newtemplates" /></td>
  418. </tr>
  419. <tr>
  420. <th align="right">|.$locale->text('Setup Templates').qq|</th>
  421. <td><select name="mastertemplates">$mastertemplates</select></td>
  422. </tr>
  423. </table>
  424. <input type="hidden" name="templates" value="$myconfig->{templates}" />
  425. </td>
  426. </tr>
  427. <tr class="listheading">
  428. <th colspan="2">|.$locale->text('Database').qq|</th>
  429. </tr>|;
  430. # list section for database drivers
  431. foreach $item (LedgerSMB::User->dbdrivers) {
  432. print qq|
  433. <tr>
  434. <td colspan="2">
  435. <table>
  436. <tr>|;
  437. $checked = "checked";
  438. if ($myconfig->{dbdriver} eq $item) {
  439. for (qw(dbhost dbport dbuser dbpasswd dbname sid)) { $form->{"${item}_$_"} = $myconfig->{$_} }
  440. $checked = "checked";
  441. }
  442. print qq|
  443. <th align="right">|.$locale->text('Driver').qq|</th>
  444. <td><input name="dbdriver" type="radio" class="radio" value="$item" $checked />&nbsp;$item</td>
  445. <th align="right">|.$locale->text('Host').qq|</th>
  446. <td><input name="${item}_dbhost" size="30" value="$form->{"${item}_dbhost"}" /></td>
  447. </tr>
  448. <tr>|;
  449. print qq|
  450. <th align="right">|.$locale->text('Dataset').qq|</th>
  451. <td><input name="${item}_dbname" size="15" value="$form->{"${item}_dbname"}" /></td>
  452. <th align="right">|.$locale->text('Port').qq|</th>
  453. <td><input name="${item}_dbport" size="4" value="$form->{"${item}_dbport"}" /></td>
  454. </tr>
  455. <tr>
  456. <th align="right">|.$locale->text('User').qq|</th>
  457. <td><input name="${item}_dbuser" size="15" value="$form->{"${item}_dbuser"}" /></td>
  458. <th align="right">|.$locale->text('Password').qq|</th>
  459. <td><input name="${item}_dbpasswd" type="password" size="10" value="$form->{"${item}_dbpasswd"}" /></td>
  460. </tr>|;
  461. print qq|
  462. </table>
  463. <input type="hidden" name="old_dbpasswd" value="$myconfig->{dbpasswd}" />
  464. </td>
  465. </tr>
  466. <tr>
  467. <td colspan="2"><hr size="2" noshade /></td>
  468. </tr>
  469. |;
  470. }
  471. # access control
  472. open(FH, $menufile) or $form->error(__FILE__.':'.__LINE__.': '."$menufile : $!");
  473. # scan for first menu level
  474. @a = <FH>;
  475. close(FH);
  476. if (open(FH, "custom_$menufile")) {
  477. push @a, <FH>;
  478. }
  479. close(FH);
  480. foreach $item (@a) {
  481. next unless $item =~ /\[\w+/;
  482. next if $item =~ /\#/;
  483. $item =~ s/(\[|\])//g;
  484. chop $item;
  485. if ($item =~ /--/) {
  486. ($level, $menuitem) = split /--/, $item, 2;
  487. } else {
  488. $level = $item;
  489. $menuitem = $item;
  490. push @acsorder, $item;
  491. }
  492. push @{ $acs{$level} }, $menuitem;
  493. }
  494. %role = ( 'admin' => $locale->text('Administrator'),
  495. 'user' => $locale->text('User'),
  496. 'supervisor' => $locale->text('Supervisor'),
  497. 'manager' => $locale->text('Manager'));
  498. $selectrole = "";
  499. foreach $item (qw(user admin supervisor manager)) {
  500. $selectrole .= ($myconfig->{role} eq $item) ? "<option selected value=\"$item\">$role{$item}</option>\n"
  501. : "<option value=\"$item\">$role{$item}</option>\n";
  502. }
  503. print qq|
  504. <tr class="listheading">
  505. <th colspan="2">|.$locale->text('Access Control').qq|</th>
  506. </tr>
  507. <tr>
  508. <td><select name="role">$selectrole</select></td>
  509. </tr>
  510. |;
  511. foreach $item (split /;/, $myconfig->{acs}) {
  512. ($key, $value) = split /--/, $item, 2;
  513. $excl{$key}{$value} = 1;
  514. }
  515. foreach $key (@acsorder) {
  516. $checked = "checked";
  517. if ($form->{login}) {
  518. $checked = ($excl{$key}{$key}) ? "" : "checked";
  519. }
  520. # can't have variable names with & and spaces
  521. $item = $form->escape("${key}--$key",1);
  522. $acsheading = $key;
  523. $acsheading =~ s/ /&nbsp;/g;
  524. $acsheading = qq|
  525. <td align="left" nowrap="nowrap" style="background-color: #C7E9F7" colspan="2">
  526. <input name="$item" class="checkbox" type="checkbox" value="1" $checked />&nbsp;$acsheading</td><tr><td>\n|;
  527. $menuitems .= "$item;";
  528. $acsdata = "<td style=\"background-color: #F0F0F0\">";
  529. foreach $item (@{ $acs{$key} }) {
  530. next if ($key eq $item);
  531. $checked = "checked";
  532. if ($form->{login}) {
  533. $checked = ($excl{$key}{$item}) ? "" : "checked";
  534. }
  535. $acsitem = $form->escape("${key}--$item",1);
  536. $acsdata .= qq|<br /><input name="$acsitem" class="checkbox" type="checkbox" value="1" $checked />&nbsp;$item|;
  537. $menuitems .= "$acsitem;";
  538. }
  539. $acsdata .= "
  540. </td>";
  541. print qq|
  542. <tr valign="top">$acsheading $acsdata
  543. </tr>
  544. |;
  545. }
  546. print qq|<input type="hidden" name="acs" value="$menuitems" />
  547. <tr>
  548. <td colspan="2"><hr size="3" noshade /></td>
  549. </tr>
  550. </table>
  551. </div>
  552. |;
  553. }
  554. sub save {
  555. $form->{callback} = "admin.pl?action=list_users";
  556. # no driver checked
  557. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('Database Driver not checked!')) unless $form->{dbdriver};
  558. # no spaces allowed in login name
  559. $form->{login} =~ s/ //g;
  560. $form->isblank("login", $locale->text('Login name missing!'));
  561. # check for duplicates
  562. if (!$form->{edit}) {
  563. $temp = LedgerSMB::User->new($form->{login});
  564. if ($temp->{login}) {
  565. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('[_1] is already a member!', $form->{login}));
  566. }
  567. }
  568. # no spaces allowed in directories
  569. $form->{newtemplates} =~ s/( |\.\.|\*)//g;
  570. if ($form->{newtemplates} ne "") {
  571. $form->{templates} = $form->{newtemplates};
  572. } else {
  573. $form->{templates} = ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
  574. }
  575. # is there a basedir
  576. if (! -d "${LedgerSMB::Sysconfig::templates}") {
  577. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('Directory [_1] does not exist', ${LedgerSMB::Sysconfig::templates}));
  578. }
  579. # add base directory to $form->{templates}
  580. $form->{templates} = "${LedgerSMB::Sysconfig::templates}/$form->{templates}";
  581. $myconfig = LedgerSMB::User->new("${LedgerSMB::Sysconfig::memberfile}", "$form->{login}");
  582. # redo acs variable and delete all the acs codes
  583. @acs = split /;/, $form->{acs};
  584. $form->{acs} = "";
  585. foreach $item (@acs) {
  586. $item = $form->escape($item,1);
  587. if (!$form->{$item}) {
  588. $form->{acs} .= $form->unescape($form->unescape("$item")).";";
  589. }
  590. delete $form->{$item};
  591. }
  592. # check which database was filled in
  593. $form->{dbhost} = $form->{"$form->{dbdriver}_dbhost"};
  594. $form->{dbport} = $form->{"$form->{dbdriver}_dbport"};
  595. $form->{dbpasswd} = $form->{"$form->{dbdriver}_dbpasswd"};
  596. $form->{dbuser} = $form->{"$form->{dbdriver}_dbuser"};
  597. $form->{dbname} = $form->{"$form->{dbdriver}_dbname"};
  598. $form->isblank("dbname", $locale->text('Dataset missing!'));
  599. $form->isblank("dbuser", $locale->text('Database User missing!'));
  600. foreach $item (keys %{$form}) {
  601. $myconfig->{$item} = $form->{$item};
  602. }
  603. $myconfig->{password} = $form->{new_password};
  604. $myconfig->{timeout} = $form->{newtimeout};
  605. delete $myconfig->{stylesheet};
  606. if ($form->{userstylesheet}) {
  607. $myconfig->{stylesheet} = $form->{userstylesheet};
  608. }
  609. $myconfig->{packpw} = 1;
  610. $myconfig->save_member($form);
  611. # create user template directory and copy master files
  612. if (! -d "$form->{templates}") {
  613. umask(002);
  614. if (mkdir "$form->{templates}", oct("771")) {
  615. umask(007);
  616. # copy templates to the directory
  617. opendir TEMPLATEDIR, "${LedgerSMB::Sysconfig::templates}/." or $form->error(__FILE__.':'.__LINE__.': '."$templates : $!");
  618. @templates = grep /$form->{mastertemplates}-/, readdir TEMPLATEDIR;
  619. closedir TEMPLATEDIR;
  620. foreach $file (@templates) {
  621. open(TEMP, "${LedgerSMB::Sysconfig::templates}/$file") or $form->error(__FILE__.':'.__LINE__.': '."$templates/$file : $!");
  622. $file =~ s/$form->{mastertemplates}-//;
  623. open(NEW, ">$form->{templates}/$file") or $form->error(__FILE__.':'.__LINE__.': '."$form->{templates}/$file : $!");
  624. while ($line = <TEMP>) {
  625. print NEW $line;
  626. }
  627. close(TEMP);
  628. close(NEW);
  629. }
  630. } else {
  631. $form->error(__FILE__.':'.__LINE__.': '."$form->{templates} : $!");
  632. }
  633. }
  634. $form->redirect($locale->text('User saved!'));
  635. }
  636. sub delete {
  637. $form->{callback} = "admin.pl?action=list_users";
  638. $form->{templates} = ($form->{templates}) ? "${LedgerSMB::Sysconfig::templates}/$form->{templates}" : "$templates/$form->{login}";
  639. # scan %user for $templatedir
  640. foreach $login (keys %user) {
  641. last if ($found = ($form->{templates} eq $user{$login}));
  642. }
  643. # if found keep directory otherwise delete
  644. if (!$found) {
  645. # delete it if there is a template directory
  646. $dir = "$form->{templates}";
  647. if (-d "$dir") {
  648. unlink <$dir/*>;
  649. rmdir "$dir";
  650. }
  651. }
  652. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  653. #users_conf
  654. my $deleteUser = $dbh->prepare("DELETE FROM users_conf USING users WHERE users.username = ? and users.id = users_conf.id;");
  655. $deleteUser->execute($form->{login});
  656. #and now users
  657. $deleteUser = $dbh->prepare("DELETE FROM users WHERE username = ?;");
  658. $deleteUser->execute($form->{login});
  659. $form->redirect($locale->text('User deleted!'));
  660. }
  661. sub login_name {
  662. my $login = shift;
  663. $login =~ s/\[\]//g;
  664. return ($login) ? $login : undef;
  665. }
  666. sub change_admin_password {
  667. $form->{title} = qq|LedgerSMB |.$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Change Admin Password');
  668. $form->{login} = "admin";
  669. $form->header;
  670. print qq|
  671. <body class="admin">
  672. <form method="post" action="$form->{script}">
  673. <table>
  674. <tr class="listheading">
  675. <th>|.$locale->text('Change Password').qq|</th>
  676. </tr>
  677. <tr size="5"></tr>
  678. <tr>
  679. <td>
  680. <table width="100%">
  681. <tr>
  682. <th align="right">|.$locale->text('Password').qq|</th>
  683. <td><input type="password" name="new_password" /></td>
  684. </tr>
  685. <tr>
  686. <th align="right">|.$locale->text('Confirm').qq|</th>
  687. <td><input type="password" name="confirm_password" /></td>
  688. </tr>
  689. </table>
  690. </td>
  691. </tr>
  692. </table>
  693. <br />
  694. <hr size="3" noshade />
  695. <input type="hidden" name="path" value="$form->{path}" />
  696. <p><button type="submit" class="submit" name="action" value="change_password">|.$locale->text('Change Password').qq|</button></p>
  697. </form>
  698. </body>
  699. </html>
  700. |;
  701. }
  702. sub change_password {
  703. # Do we want to force a login after changing the password?
  704. $form->{callback} = "admin.pl?";
  705. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('Passwords do not match!')) if $form->{new_password} ne $form->{confirm_password};
  706. # use the central database handle
  707. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  708. my $updateAdminPassword = $dbh->prepare("UPDATE users_conf
  709. SET password = md5(?)
  710. WHERE id = 1");
  711. $updateAdminPassword->execute($form->{new_password});
  712. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}";
  713. $form->redirect($locale->text('Password changed!'));
  714. }
  715. sub check_password {
  716. $root = LedgerSMB::User->new('admin');
  717. if ($form->{password}) {
  718. $form->{callback} .= "&amp;password=$form->{password}" if $form->{callback};
  719. if ($root->{password} ne (Digest::MD5::md5_hex $form->{password}) ) {
  720. &adminlogin($locale->text('Access Denied!'));
  721. exit;
  722. }
  723. else{
  724. Session::session_create($root);
  725. }
  726. }
  727. else {
  728. $ENV{HTTP_COOKIE} =~ s/;\s*/;/g;
  729. @cookies = split /;/, $ENV{HTTP_COOKIE};
  730. foreach (@cookies) {
  731. ($name,$value) = split /=/, $_, 2;
  732. $cookie{$name} = $value;
  733. }
  734. if(!Session::session_check($cookie{"LedgerSMB"}, $root)){
  735. &adminlogin($locale->text('Session expired!'));
  736. exit;
  737. }
  738. }
  739. }
  740. sub pg_database_administration {
  741. $form->{dbdriver} = 'Pg';
  742. &dbselect_source;
  743. }
  744. sub pgpp_database_administration {
  745. $form->{dbdriver} = 'PgPP';
  746. &dbselect_source;
  747. }
  748. sub dbdriver_defaults {
  749. # load some defaults for the selected driver
  750. %driverdefaults = ( 'Pg' => { dbport => '',
  751. dbuser => 'ledger-smb',
  752. dbdefault => 'template1',
  753. dbhost => '',
  754. connectstring => $locale->text('Connect to')
  755. } );
  756. $driverdefaults{PgPP} = $driverdefaults{Pg};
  757. for (keys %{ $driverdefaults{Pg} }) { $form->{$_} = $driverdefaults{$form->{dbdriver}}{$_} }
  758. }
  759. sub dbselect_source {
  760. &dbdriver_defaults;
  761. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." / ".$locale->text('Database Administration');
  762. $form->{login} = "admin";
  763. $form->header;
  764. #an insane amount of table nesting here, this should be cleaned up.
  765. print qq|
  766. <body class="admin">
  767. <center>
  768. <h2>$form->{title}</h2>
  769. <form method="post" action="$form->{script}" />
  770. <table>
  771. <tr>
  772. <td>
  773. <table>
  774. <tr class="listheading">
  775. <th colspan="4">|.$locale->text('Database').qq|</th>
  776. </tr>
  777. <tr>
  778. <td>
  779. <table>
  780. <tr>
  781. <th align="right">|.$locale->text('Host').qq|</th>
  782. <td><input name="dbhost" size="25" value="$form->{dbhost}" /></td>
  783. <th align="right">|.$locale->text('Port').qq|</th>
  784. <td><input name="dbport" size="5" value="$form->{dbport}" /></td>
  785. </tr>
  786. <tr>
  787. <th align="right">|.$locale->text('User').qq|</th>
  788. <td><input name="dbuser" size="10" value="$form->{dbuser}" /></td>
  789. <th align="right">|.$locale->text('Password').qq|</th>
  790. <td><input type="password" name="dbpasswd" size="10" /></td>
  791. </tr>
  792. <tr>
  793. <th align="right">$form->{connectstring}</th>
  794. <td colspan="3"><input name="dbdefault" size="10" value="$form->{dbdefault}" /></td>
  795. </tr>
  796. <tr>
  797. <th align="right">|.$locale->text("Superuser").qq|</th>
  798. <td><input name="dbsuperuser" size="10" value="$form->{dbsuperuser}" /></td>
  799. <th align="right">|.$locale->text('Password').qq|</th>
  800. <td><input type="password" name="dbsuperpasswd" size="10" /></td>
  801. </tr>
  802. </table>
  803. </td>
  804. </tr>
  805. </table>
  806. </td>
  807. </tr>
  808. </table>
  809. <input type="hidden" name="dbdriver" value="$form->{dbdriver}" />
  810. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}" />
  811. <input type="hidden" name="path" value="$form->{path}" />
  812. <br />
  813. <button type="submit" class="submit" name="action" value="create_dataset">|.$locale->text('Create Dataset').qq|</button>
  814. <button type="submit" class="submit" name="action" value="delete_dataset">|.$locale->text('Delete Dataset').qq|</button>
  815. </form>
  816. <p>|.$locale->text('This is a preliminary check for existing sources. Nothing will be created or deleted at this stage!')
  817. .qq|</p>
  818. </center>
  819. </body>
  820. </html>
  821. |;
  822. }
  823. sub continue {
  824. &{ $form->{nextsub} };
  825. }
  826. sub dbupdate {
  827. $form->{callback} = "admin.pl?action=list_users";
  828. LedgerSMB::User->dbupdate(\%$form);
  829. $form->redirect($locale->text('Dataset updated!'));
  830. }
  831. sub create_dataset {
  832. @dbsources = sort LedgerSMB::User->dbsources(\%$form);
  833. opendir SQLDIR, "sql/." or $form->error(__FILE__.':'.__LINE__.': '.$!);
  834. foreach $item (sort grep /-chart\.sql/, readdir SQLDIR) {
  835. next if ($item eq 'Default-chart.sql');
  836. $item =~ s/-chart\.sql//;
  837. push @charts, qq|<input name="chart" class="radio" type="radio" value="$item" />$item|;
  838. }
  839. closedir SQLDIR;
  840. # add Default at beginning
  841. unshift @charts, qq|<input name="chart" class="radio" type="radio" value="Default" checked />Default|;
  842. $selectencoding = qq|<option></option>
  843. <option value="SQL_ASCII">ASCII</option>
  844. <option value="EUC_JP">Japanese EUC</option>
  845. <option value="EUC_CN">Chinese EUC</option>
  846. <option value="EUC_KR">Korean EUC</option>
  847. <option value="JOHAB">Korean EUC (Hangle base)</option>
  848. <option value="EUC_TW">Taiwan EUC</option>
  849. <option value="UNICODE">Unicode (UTF-8)</option>
  850. <option value="MULE_INTERNAL">Mule internal type</option>
  851. <option value="LATIN1">ISO 8859-1/ECMA 94 (Latin alphabet no. 1)</option>
  852. <option value="LATIN2">ISO 8859-2/ECMA 94 (Latin alphabet no. 2)</option>
  853. <option value="LATIN3">ISO 8859-3/ECMA 94 (Latin alphabet no. 3)</option>
  854. <option value="LATIN4">ISO 8859-4/ECMA 94 (Latin alphabet no. 4)</option>
  855. <option value="LATIN5">ISO 8859-9/ECMA 128 (Latin alphabet no. 5)</option>
  856. <option value="LATIN6">ISO 8859-10/ECMA 144 (Latin alphabet no. 6)</option>
  857. <option value="LATIN7">ISO 8859-13 (Latin alphabet no. 7)</option>
  858. <option value="LATIN8">ISO 8859-14 (Latin alphabet no. 8)</option>
  859. <option value="LATIN9">ISO 8859-15 (Latin alphabet no. 9)</option>
  860. <option value="LATIN10">ISO 8859-16/ASRO SR 14111 (Latin alphabet no. 10)</option>
  861. <option value="ISO_8859_5">ISO 8859-5/ECMA 113 (Latin/Cyrillic)</option>
  862. <option value="ISO_8859_6">ISO 8859-6/ECMA 114 (Latin/Arabic)</option>
  863. <option value="ISO_8859_7">ISO 8859-7/ECMA 118 (Latin/Greek)</option>
  864. <option value="ISO_8859_8">ISO 8859-8/ECMA 121 (Latin/Hebrew)</option>
  865. <option value="KOI8">KOI8-R(U)</option>
  866. <option value="WIN">Windows CP1251</option>
  867. <option value="ALT">Windows CP866</option>
  868. <option value="WIN1256">Windows CP1256 (Arabic)</option>
  869. <option value="TCVN">Windows CP1258 (Vietnamese)</option>
  870. <option value="WIN874">Windows CP874 (Thai)</option>
  871. |;
  872. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  873. ." ".$locale->text('Database Administration')
  874. ." / ".$locale->text('Create Dataset');
  875. $form->{login} = "admin";
  876. $form->header;
  877. print qq|
  878. <body class="admin">
  879. <center>
  880. <h2>$form->{title}</h2>
  881. <form method="post" action="$form->{script}" />
  882. <table width="100%">
  883. <tr class="listheading">
  884. <th colspan="2">&nbsp;</th>
  885. </tr>
  886. <tr>
  887. <th align="right" nowrap="nowrap">|.$locale->text('Existing Datasets').qq|</th>
  888. <td>
  889. |;
  890. for (@dbsources) { print "[&nbsp;$_&nbsp;] " }
  891. print qq|
  892. </td>
  893. </tr>
  894. <tr>
  895. <th align="right" nowrap="nowrap">|.$locale->text('Create Dataset').qq|</th>
  896. <td><input name="db" /></td>
  897. </tr>
  898. <tr>
  899. <th align="right" nowrap="nowrap">|.$locale->text('Multibyte Encoding').qq|</th>
  900. <td><select name="encoding">$selectencoding</select></td>
  901. </tr>
  902. <tr>
  903. <th align="right" nowrap="nowrap">|.$locale->text('Create Chart of Accounts').qq|</th>
  904. <td>
  905. <table>
  906. |;
  907. while (@charts) {
  908. print qq| <tr>|;
  909. for (0 .. 2) { print "<td>$charts[$_]</td>\n" }
  910. print qq| </tr>|;
  911. splice @charts, 0, 3;
  912. }
  913. print qq| </table>
  914. </td>
  915. </tr>
  916. <tr>
  917. <td colspan="2">
  918. <hr size="3" noshade />
  919. </td>
  920. </tr>
  921. </table>
  922. |;
  923. $form->hide_form(qw(dbdriver dbsuperuser dbsuperpasswd dbuser dbhost dbport dbpasswd dbdefault path));
  924. print qq|
  925. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}" />
  926. <input type="hidden" name="nextsub" value="dbcreate" />
  927. <br />
  928. <button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  929. </form>
  930. </body>
  931. </html>
  932. |;
  933. }
  934. sub dbcreate {
  935. $form->isblank("db", $locale->text('Dataset missing!'));
  936. LedgerSMB::User->dbcreate(\%$form);
  937. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  938. ." ".$locale->text('Database Administration')
  939. ." / ".$locale->text('Create Dataset');
  940. $form->{login} = "admin";
  941. $form->header;
  942. print qq|
  943. <body class="admin">
  944. <center>
  945. <h2>$form->{title}</h2>
  946. <form method="post" action="$form->{script}">|
  947. .$locale->text('Dataset [_1] successfully created!', $form->{db})
  948. .qq|
  949. <input type="hidden" name="path" value="$form->{path}" />
  950. <input type="hidden" name="nextsub" value="list_users" />
  951. <p><button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button></p>
  952. </form>
  953. </center>
  954. </body>
  955. </html>
  956. |;
  957. }
  958. sub delete_dataset {
  959. if (@dbsources = LedgerSMB::User->dbsources_unused(\%$form)) {
  960. foreach $item (sort @dbsources) {
  961. $dbsources .= qq|<input name="db" class="radio" type="radio" value="$item" />&nbsp;$item |;
  962. }
  963. } else {
  964. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('Nothing to delete!'));
  965. }
  966. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  967. ." ".$locale->text('Database Administration')
  968. ." / ".$locale->text('Delete Dataset');
  969. $form->{login} = "admin";
  970. $form->header;
  971. print qq|
  972. <body class="admin">
  973. <h2>$form->{title}</h2>
  974. <form method="post" action="$form->{script}" />
  975. <input type="hidden" name="dbdriver" value="$form->{dbdriver}" />
  976. <input type="hidden" name="dbuser" value="$form->{dbuser}" />
  977. <input type="hidden" name="dbhost" value="$form->{dbhost}" />
  978. <input type="hidden" name="dbport" value="$form->{dbport}" />
  979. <input type="hidden" name="dbpasswd" value="$form->{dbpasswd}" />
  980. <input type="hidden" name="dbdefault" value="$form->{dbdefault}" />
  981. <input name=callback type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}">
  982. <input type="hidden" name="path" value="$form->{path}" />
  983. <input type="hidden" name="nextsub" value="dbdelete" />
  984. <table width="100%">
  985. <tr class="listheading">
  986. <th>|.$locale->text('The following Datasets are not in use and can be deleted').qq|</th>
  987. </tr>
  988. <tr>
  989. <td>
  990. $dbsources
  991. </td>
  992. </tr>
  993. <tr>
  994. <td>
  995. <hr size="3" noshade />
  996. <br />
  997. <button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  998. </td>
  999. </tr>
  1000. </table>
  1001. </form>
  1002. </body>
  1003. </html>
  1004. |;
  1005. }
  1006. sub dbdelete {
  1007. if (!$form->{db}) {
  1008. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('No Dataset selected!'));
  1009. }
  1010. LedgerSMB::User->dbdelete(\%$form);
  1011. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  1012. ." ".$locale->text('Database Administration')
  1013. ." / ".$locale->text('Delete Dataset');
  1014. $form->{login} = "admin";
  1015. $form->header;
  1016. print qq|
  1017. <body class="admin">
  1018. <center>
  1019. <h2>$form->{title}</h2>
  1020. $form->{db} |.$locale->text('successfully deleted!')
  1021. .qq|
  1022. <form method="post" action="$form->{script}" />
  1023. <input type="hidden" name="path" value="$form->{path}" />
  1024. <input type="hidden" name="nextsub" value="list_users" />
  1025. <p><button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button></p>
  1026. </form>
  1027. </body>
  1028. </html>
  1029. |;
  1030. }
  1031. sub unlock_system {
  1032. # This needs to be done with a db tool
  1033. # unlink "${LedgerSMB::Sysconfig::userspath}/nologin";
  1034. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}";
  1035. $form->redirect($locale->text('Lockfile removed!'));
  1036. }
  1037. sub lock_system {
  1038. # This needs to be done with a db tool
  1039. #open(FH, ">${LedgerSMB::Sysconfig::userspath}/nologin") or $form->error($locale->text('Cannot create Lock!'));
  1040. #close(FH);
  1041. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}";
  1042. $form->redirect($locale->text('Lockfile created!'));
  1043. }