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