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