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