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