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