summaryrefslogtreecommitdiff
path: root/bin/admin.pl
blob: 42022d36469e9e06e21e8738c64a64cd94e946e8 (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} = "ledger-smb.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="ledger-smb.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/ledger-smb.css" ) {
  143. $myconfig->{stylesheet} = "ledger-smb.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. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  741. my $found = $dbh->selectall_arrayref(qq|SELECT c.templates
  742. FROM users_conf c INNER JOIN users u ON (c.id = u.id)
  743. WHERE c.templates = ? AND u.username <> ?|,
  744. undef, $form->{templates}, $form->{login});
  745. # if found keep directory otherwise delete
  746. if ( !@$found ) {
  747. # delete it if there is a template directory
  748. $dir = "$form->{templates}";
  749. if ( -d "$dir" ) {
  750. unlink <$dir/*>;
  751. rmdir "$dir";
  752. }
  753. }
  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 Dataset')
  940. . qq|</button>
  941. </form>
  942. <p>|
  943. . $locale->text(
  944. 'This is a preliminary check for existing sources. Nothing will be created or deleted at this stage!'
  945. )
  946. . qq|</p>
  947. </center>
  948. </body>
  949. </html>
  950. |;
  951. }
  952. sub continue {
  953. &{ $form->{nextsub} };
  954. }
  955. sub dbupdate {
  956. $form->{callback} = "admin.pl?action=list_users";
  957. LedgerSMB::User->dbupdate( \%$form );
  958. $form->redirect( $locale->text('Dataset updated!') );
  959. }
  960. sub create_dataset {
  961. @dbsources = sort LedgerSMB::User->dbsources( \%$form );
  962. opendir SQLDIR, "sql/."
  963. or $form->error( __FILE__ . ':' . __LINE__ . ': ' . $! );
  964. foreach $item ( sort grep /-chart\.sql/, readdir SQLDIR ) {
  965. next if ( $item eq 'Default-chart.sql' );
  966. $item =~ s/-chart\.sql//;
  967. push @charts,
  968. qq|<input name="chart" class="radio" type="radio" value="$item" />$item|;
  969. }
  970. closedir SQLDIR;
  971. # add Default at beginning
  972. unshift @charts,
  973. qq|<input name="chart" class="radio" type="radio" value="Default" checked />Default|;
  974. $form->{title} =
  975. "LedgerSMB "
  976. . $locale->text('Accounting') . " "
  977. . $locale->text('Database Administration') . " / "
  978. . $locale->text('Create Dataset');
  979. $form->{login} = "admin";
  980. $form->header;
  981. print qq|
  982. <body class="admin">
  983. <center>
  984. <h2>$form->{title}</h2>
  985. <form method="post" action="$form->{script}" />
  986. <table width="100%">
  987. <tr class="listheading">
  988. <th colspan="2">&nbsp;</th>
  989. </tr>
  990. <tr>
  991. <th align="right" nowrap="nowrap">|
  992. . $locale->text('Existing Datasets')
  993. . qq|</th>
  994. <td>
  995. |;
  996. for (@dbsources) { print "[&nbsp;$_&nbsp;] " }
  997. print qq|
  998. </td>
  999. </tr>
  1000. <tr>
  1001. <th align="right" nowrap="nowrap">|
  1002. . $locale->text('Create Dataset')
  1003. . qq|</th>
  1004. <td><input name="db" /></td>
  1005. </tr>
  1006. <tr>
  1007. <th align="right" nowrap="nowrap">|
  1008. . $locale->text('Create Chart of Accounts')
  1009. . qq|</th>
  1010. <td>
  1011. <table>
  1012. |;
  1013. while (@charts) {
  1014. print qq| <tr>|;
  1015. for ( 0 .. 2 ) { print "<td>$charts[$_]</td>\n" }
  1016. print qq| </tr>|;
  1017. splice @charts, 0, 3;
  1018. }
  1019. print qq| </table>
  1020. </td>
  1021. </tr>
  1022. <tr>
  1023. <td colspan="2">
  1024. <hr size="3" noshade />
  1025. </td>
  1026. </tr>
  1027. </table>
  1028. |;
  1029. $form->hide_form(
  1030. qw(dbdriver dbsuperuser dbsuperpasswd dbuser dbhost dbport dbpasswd dbdefault path)
  1031. );
  1032. print qq|
  1033. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}" />
  1034. <input type="hidden" name="nextsub" value="dbcreate" />
  1035. <br />
  1036. <button type="submit" class="submit" name="action" value="continue">|
  1037. . $locale->text('Continue')
  1038. . qq|</button>
  1039. </form>
  1040. </body>
  1041. </html>
  1042. |;
  1043. }
  1044. sub dbcreate {
  1045. $form->isblank( "db", $locale->text('Dataset missing!') );
  1046. LedgerSMB::User->dbcreate( \%$form );
  1047. $form->{title} =
  1048. "LedgerSMB "
  1049. . $locale->text('Accounting') . " "
  1050. . $locale->text('Database Administration') . " / "
  1051. . $locale->text('Create Dataset');
  1052. $form->{login} = "admin";
  1053. $form->header;
  1054. print qq|
  1055. <body class="admin">
  1056. <center>
  1057. <h2>$form->{title}</h2>
  1058. <form method="post" action="$form->{script}">|
  1059. . $locale->text( 'Dataset [_1] successfully created!', $form->{db} ) . qq|
  1060. <input type="hidden" name="path" value="$form->{path}" />
  1061. <input type="hidden" name="nextsub" value="list_users" />
  1062. <p><button type="submit" class="submit" name="action" value="continue">|
  1063. . $locale->text('Continue')
  1064. . qq|</button></p>
  1065. </form>
  1066. </center>
  1067. </body>
  1068. </html>
  1069. |;
  1070. }
  1071. sub unlock_system {
  1072. # This needs to be done with a db tool
  1073. # unlink "${LedgerSMB::Sysconfig::userspath}/nologin";
  1074. $form->{callback} =
  1075. "$form->{script}?action=list_users&amp;path=$form->{path}";
  1076. $form->redirect( $locale->text('Lockfile removed!') );
  1077. }
  1078. sub lock_system {
  1079. # This needs to be done with a db tool
  1080. #open(FH, '>', "${LedgerSMB::Sysconfig::userspath}/nologin") or $form->error($locale->text('Cannot create Lock!'));
  1081. #close(FH);
  1082. $form->{callback} =
  1083. "$form->{script}?action=list_users&amp;path=$form->{path}";
  1084. $form->redirect( $locale->text('Lockfile created!') );
  1085. }