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