summaryrefslogtreecommitdiff
path: root/bin/admin.pl
blob: b5c19610169f5c4bd95658791194031f626290c3 (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. $root->{password} = $form->{new_password};
  699. $root->{'root login'} = 1;
  700. $root->save_member();
  701. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}";
  702. $form->redirect($locale->text('Password changed!'));
  703. }
  704. sub check_password {
  705. $root = LedgerSMB::User->new('admin');
  706. if ($form->{password}) {
  707. $form->{callback} .= "&amp;password=$form->{password}" if $form->{callback};
  708. if ($root->{password} ne (Digest::MD5::md5_hex $form->{password}) ) {
  709. &getpassword;
  710. exit;
  711. }
  712. else{
  713. Session::session_create($root);
  714. }
  715. }
  716. else {
  717. $ENV{HTTP_COOKIE} =~ s/;\s*/;/g;
  718. @cookies = split /;/, $ENV{HTTP_COOKIE};
  719. foreach (@cookies) {
  720. ($name,$value) = split /=/, $_, 2;
  721. $cookie{$name} = $value;
  722. }
  723. if(!Session::session_check($cookie{"LedgerSMB"}, $root)){
  724. &getpassword(1);
  725. exit;
  726. }
  727. }
  728. }
  729. sub pg_database_administration {
  730. $form->{dbdriver} = 'Pg';
  731. &dbselect_source;
  732. }
  733. sub pgpp_database_administration {
  734. $form->{dbdriver} = 'PgPP';
  735. &dbselect_source;
  736. }
  737. sub dbdriver_defaults {
  738. # load some defaults for the selected driver
  739. %driverdefaults = ( 'Pg' => { dbport => '',
  740. dbuser => 'ledger-smb',
  741. dbdefault => 'template1',
  742. dbhost => '',
  743. connectstring => $locale->text('Connect to')
  744. } );
  745. $driverdefaults{PgPP} = $driverdefaults{Pg};
  746. for (keys %{ $driverdefaults{Pg} }) { $form->{$_} = $driverdefaults{$form->{dbdriver}}{$_} }
  747. }
  748. sub dbselect_source {
  749. &dbdriver_defaults;
  750. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." / ".$locale->text('Database Administration');
  751. $form->{login} = "root login";
  752. $form->header;
  753. #an insane amount of table nesting here, this should be cleaned up.
  754. print qq|
  755. <body class="admin">
  756. <center>
  757. <h2>$form->{title}</h2>
  758. <form method="post" action="$form->{script}" />
  759. <table>
  760. <tr>
  761. <td>
  762. <table>
  763. <tr class="listheading">
  764. <th colspan="4">|.$locale->text('Database').qq|</th>
  765. </tr>
  766. <tr>
  767. <td>
  768. <table>
  769. <tr>
  770. <th align="right">|.$locale->text('Host').qq|</th>
  771. <td><input name="dbhost" size="25" value="$form->{dbhost}" /></td>
  772. <th align="right">|.$locale->text('Port').qq|</th>
  773. <td><input name="dbport" size="5" value="$form->{dbport}" /></td>
  774. </tr>
  775. <tr>
  776. <th align="right">|.$locale->text('User').qq|</th>
  777. <td><input name="dbuser" size="10" value="$form->{dbuser}" /></td>
  778. <th align="right">|.$locale->text('Password').qq|</th>
  779. <td><input type="password" name="dbpasswd" size="10" /></td>
  780. </tr>
  781. <tr>
  782. <th align="right">$form->{connectstring}</th>
  783. <td colspan="3"><input name="dbdefault" size="10" value="$form->{dbdefault}" /></td>
  784. </tr>
  785. <tr>
  786. <th align="right">|.$locale->text("Superuser").qq|</th>
  787. <td><input name="dbsuperuser" size="10" value="$form->{dbsuperuser}" /></td>
  788. <th align="right">|.$locale->text('Password').qq|</th>
  789. <td><input type="password" name="dbsuperpasswd" size="10" /></td>
  790. </tr>
  791. </table>
  792. </td>
  793. </tr>
  794. </table>
  795. </td>
  796. </tr>
  797. </table>
  798. <input type="hidden" name="dbdriver" value="$form->{dbdriver}" />
  799. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}" />
  800. <input type="hidden" name="path" value="$form->{path}" />
  801. <br />
  802. <button type="submit" class="submit" name="action" value="create_dataset">|.$locale->text('Create Dataset').qq|</button>
  803. <button type="submit" class="submit" name="action" value="delete_dataset">|.$locale->text('Delete Dataset').qq|</button>
  804. </form>
  805. <p>|.$locale->text('This is a preliminary check for existing sources. Nothing will be created or deleted at this stage!')
  806. .qq|</p>
  807. </center>
  808. </body>
  809. </html>
  810. |;
  811. }
  812. sub continue {
  813. &{ $form->{nextsub} };
  814. }
  815. sub dbupdate {
  816. $form->{callback} = "admin.pl?action=list_users";
  817. LedgerSMB::User->dbupdate(\%$form);
  818. $form->redirect($locale->text('Dataset updated!'));
  819. }
  820. sub create_dataset {
  821. @dbsources = sort LedgerSMB::User->dbsources(\%$form);
  822. opendir SQLDIR, "sql/." or $form->error(__FILE__.':'.__LINE__.': '.$!);
  823. foreach $item (sort grep /-chart\.sql/, readdir SQLDIR) {
  824. next if ($item eq 'Default-chart.sql');
  825. $item =~ s/-chart\.sql//;
  826. push @charts, qq|<input name="chart" class="radio" type="radio" value="$item" />$item|;
  827. }
  828. closedir SQLDIR;
  829. # add Default at beginning
  830. unshift @charts, qq|<input name="chart" class="radio" type="radio" value="Default" checked />Default|;
  831. $selectencoding = qq|<option></option>
  832. <option value="SQL_ASCII">ASCII</option>
  833. <option value="EUC_JP">Japanese EUC</option>
  834. <option value="EUC_CN">Chinese EUC</option>
  835. <option value="EUC_KR">Korean EUC</option>
  836. <option value="JOHAB">Korean EUC (Hangle base)</option>
  837. <option value="EUC_TW">Taiwan EUC</option>
  838. <option value="UNICODE">Unicode (UTF-8)</option>
  839. <option value="MULE_INTERNAL">Mule internal type</option>
  840. <option value="LATIN1">ISO 8859-1/ECMA 94 (Latin alphabet no. 1)</option>
  841. <option value="LATIN2">ISO 8859-2/ECMA 94 (Latin alphabet no. 2)</option>
  842. <option value="LATIN3">ISO 8859-3/ECMA 94 (Latin alphabet no. 3)</option>
  843. <option value="LATIN4">ISO 8859-4/ECMA 94 (Latin alphabet no. 4)</option>
  844. <option value="LATIN5">ISO 8859-9/ECMA 128 (Latin alphabet no. 5)</option>
  845. <option value="LATIN6">ISO 8859-10/ECMA 144 (Latin alphabet no. 6)</option>
  846. <option value="LATIN7">ISO 8859-13 (Latin alphabet no. 7)</option>
  847. <option value="LATIN8">ISO 8859-14 (Latin alphabet no. 8)</option>
  848. <option value="LATIN9">ISO 8859-15 (Latin alphabet no. 9)</option>
  849. <option value="LATIN10">ISO 8859-16/ASRO SR 14111 (Latin alphabet no. 10)</option>
  850. <option value="ISO_8859_5">ISO 8859-5/ECMA 113 (Latin/Cyrillic)</option>
  851. <option value="ISO_8859_6">ISO 8859-6/ECMA 114 (Latin/Arabic)</option>
  852. <option value="ISO_8859_7">ISO 8859-7/ECMA 118 (Latin/Greek)</option>
  853. <option value="ISO_8859_8">ISO 8859-8/ECMA 121 (Latin/Hebrew)</option>
  854. <option value="KOI8">KOI8-R(U)</option>
  855. <option value="WIN">Windows CP1251</option>
  856. <option value="ALT">Windows CP866</option>
  857. <option value="WIN1256">Windows CP1256 (Arabic)</option>
  858. <option value="TCVN">Windows CP1258 (Vietnamese)</option>
  859. <option value="WIN874">Windows CP874 (Thai)</option>
  860. |;
  861. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  862. ." ".$locale->text('Database Administration')
  863. ." / ".$locale->text('Create Dataset');
  864. $form->{login} = "root login";
  865. $form->header;
  866. print qq|
  867. <body class="admin">
  868. <center>
  869. <h2>$form->{title}</h2>
  870. <form method="post" action="$form->{script}" />
  871. <table width="100%">
  872. <tr class="listheading">
  873. <th colspan="2">&nbsp;</th>
  874. </tr>
  875. <tr>
  876. <th align="right" nowrap="nowrap">|.$locale->text('Existing Datasets').qq|</th>
  877. <td>
  878. |;
  879. for (@dbsources) { print "[&nbsp;$_&nbsp;] " }
  880. print qq|
  881. </td>
  882. </tr>
  883. <tr>
  884. <th align="right" nowrap="nowrap">|.$locale->text('Create Dataset').qq|</th>
  885. <td><input name="db" /></td>
  886. </tr>
  887. <tr>
  888. <th align="right" nowrap="nowrap">|.$locale->text('Multibyte Encoding').qq|</th>
  889. <td><select name="encoding">$selectencoding</select></td>
  890. </tr>
  891. <tr>
  892. <th align="right" nowrap="nowrap">|.$locale->text('Create Chart of Accounts').qq|</th>
  893. <td>
  894. <table>
  895. |;
  896. while (@charts) {
  897. print qq| <tr>|;
  898. for (0 .. 2) { print "<td>$charts[$_]</td>\n" }
  899. print qq| </tr>|;
  900. splice @charts, 0, 3;
  901. }
  902. print qq| </table>
  903. </td>
  904. </tr>
  905. <tr>
  906. <td colspan="2">
  907. <hr size="3" noshade />
  908. </td>
  909. </tr>
  910. </table>
  911. |;
  912. $form->hide_form(qw(dbdriver dbsuperuser dbsuperpasswd dbuser dbhost dbport dbpasswd dbdefault path));
  913. print qq|
  914. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}" />
  915. <input type="hidden" name="nextsub" value="dbcreate" />
  916. <br />
  917. <button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  918. </form>
  919. </body>
  920. </html>
  921. |;
  922. }
  923. sub dbcreate {
  924. $form->isblank("db", $locale->text('Dataset missing!'));
  925. LedgerSMB::User->dbcreate(\%$form);
  926. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  927. ." ".$locale->text('Database Administration')
  928. ." / ".$locale->text('Create Dataset');
  929. $form->{login} = "root login";
  930. $form->header;
  931. print qq|
  932. <body class="admin">
  933. <center>
  934. <h2>$form->{title}</h2>
  935. <form method="post" action="$form->{script}">|
  936. .$locale->text('Dataset [_1] successfully created!', $form->{db})
  937. .qq|
  938. <input type="hidden" name="path" value="$form->{path}" />
  939. <input type="hidden" name="nextsub" value="list_users" />
  940. <p><button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button></p>
  941. </form>
  942. </center>
  943. </body>
  944. </html>
  945. |;
  946. }
  947. sub delete_dataset {
  948. if (@dbsources = LedgerSMB::User->dbsources_unused(\%$form)) {
  949. foreach $item (sort @dbsources) {
  950. $dbsources .= qq|<input name="db" class="radio" type="radio" value="$item" />&nbsp;$item |;
  951. }
  952. } else {
  953. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('Nothing to delete!'));
  954. }
  955. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  956. ." ".$locale->text('Database Administration')
  957. ." / ".$locale->text('Delete Dataset');
  958. $form->{login} = "root login";
  959. $form->header;
  960. print qq|
  961. <body class="admin">
  962. <h2>$form->{title}</h2>
  963. <form method="post" action="$form->{script}" />
  964. <input type="hidden" name="dbdriver" value="$form->{dbdriver}" />
  965. <input type="hidden" name="dbuser" value="$form->{dbuser}" />
  966. <input type="hidden" name="dbhost" value="$form->{dbhost}" />
  967. <input type="hidden" name="dbport" value="$form->{dbport}" />
  968. <input type="hidden" name="dbpasswd" value="$form->{dbpasswd}" />
  969. <input type="hidden" name="dbdefault" value="$form->{dbdefault}" />
  970. <input name=callback type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}">
  971. <input type="hidden" name="path" value="$form->{path}" />
  972. <input type="hidden" name="nextsub" value="dbdelete" />
  973. <table width="100%">
  974. <tr class="listheading">
  975. <th>|.$locale->text('The following Datasets are not in use and can be deleted').qq|</th>
  976. </tr>
  977. <tr>
  978. <td>
  979. $dbsources
  980. </td>
  981. </tr>
  982. <tr>
  983. <td>
  984. <hr size="3" noshade />
  985. <br />
  986. <button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  987. </td>
  988. </tr>
  989. </table>
  990. </form>
  991. </body>
  992. </html>
  993. |;
  994. }
  995. sub dbdelete {
  996. if (!$form->{db}) {
  997. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('No Dataset selected!'));
  998. }
  999. LedgerSMB::User->dbdelete(\%$form);
  1000. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  1001. ." ".$locale->text('Database Administration')
  1002. ." / ".$locale->text('Delete Dataset');
  1003. $form->{login} = "root login";
  1004. $form->header;
  1005. print qq|
  1006. <body class="admin">
  1007. <center>
  1008. <h2>$form->{title}</h2>
  1009. $form->{db} |.$locale->text('successfully deleted!')
  1010. .qq|
  1011. <form method="post" action="$form->{script}" />
  1012. <input type="hidden" name="path" value="$form->{path}" />
  1013. <input type="hidden" name="nextsub" value="list_users" />
  1014. <p><button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button></p>
  1015. </form>
  1016. </body>
  1017. </html>
  1018. |;
  1019. }
  1020. sub unlock_system {
  1021. # This needs to be done with a db tool
  1022. # unlink "${LedgerSMB::Sysconfig::userspath}/nologin";
  1023. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}";
  1024. $form->redirect($locale->text('Lockfile removed!'));
  1025. }
  1026. sub lock_system {
  1027. # This needs to be done with a db tool
  1028. #open(FH, ">${LedgerSMB::Sysconfig::userspath}/nologin") or $form->error($locale->text('Cannot create Lock!'));
  1029. #close(FH);
  1030. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}";
  1031. $form->redirect($locale->text('Lockfile created!'));
  1032. }