summaryrefslogtreecommitdiff
path: root/bin/admin.pl
blob: 409790636b17e8acbf2a25a7a12b5cc9f79ef674 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. #
  7. # Copyright (C) 2006
  8. # This work contains copyrighted information from a number of sources all used
  9. # with permission.
  10. #
  11. # This file contains source code included with or based on SQL-Ledger which
  12. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  13. # under the GNU General Public License version 2 or, at your option, any later
  14. # version. For a full list including contact information of contributors,
  15. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  16. #
  17. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  18. # Copyright (c) 2000
  19. #
  20. # Author: DWS Systems Inc.
  21. # Web: http://www.sql-ledger.org
  22. #
  23. # Contributors:
  24. #
  25. #
  26. #======================================================================
  27. #
  28. # This file has undergone whitespace cleanup.
  29. #
  30. #======================================================================
  31. #
  32. # setup module
  33. # add/edit/delete users
  34. #
  35. #======================================================================
  36. $menufile = "menu.ini";
  37. use LedgerSMB::Form;
  38. use LedgerSMB::Locale;
  39. use LedgerSMB::User;
  40. use LedgerSMB::Session;
  41. $form = new Form;
  42. $locale = LedgerSMB::Locale->get_handle(${LedgerSMB::Sysconfig::language}) or
  43. $form->error(__FILE__.':'.__LINE__.': '."Locale not loaded: $!\n");
  44. $locale->encoding('UTF-8');
  45. $form->{charset} = 'UTF-8';
  46. eval { require DBI; };
  47. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('DBI not installed!')) if ($@);
  48. $form->{stylesheet} = "ledger-smb.css";
  49. $form->{favicon} = "favicon.ico";
  50. $form->{timeout} = 600;
  51. require "bin/pw.pl";
  52. # customization
  53. if (-f "bin/custom/$form->{script}") {
  54. eval { require "bin/custom/$form->{script}"; };
  55. $form->error(__FILE__.':'.__LINE__.': '.$@) if ($@);
  56. }
  57. if ($form->{action}) {
  58. &check_password unless $form->{action} eq 'logout';
  59. &{ $form->{action} };
  60. } else {
  61. # if there are no drivers bail out
  62. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('No Database Drivers available!')) unless (LedgerSMB::User->dbdrivers);
  63. $root = LedgerSMB::User->new('admin');
  64. &adminlogin;
  65. }
  66. 1;
  67. # end
  68. sub adminlogin {
  69. my ($errorMessage) = @_;
  70. $form->{title} = qq|LedgerSMB $form->{version} |.$locale->text('Administration');
  71. $myheaderadd = qq|
  72. <script language="JavaScript" type="text/javascript">
  73. <!--
  74. function sf(){
  75. document.admin.password.focus();
  76. }
  77. // End -->
  78. </script>
  79. |;
  80. $form->header(undef, $myheaderadd);
  81. print qq|
  82. <body class="admin" onload="sf()">
  83. <div align="center">
  84. <a href="http://www.ledgersmb.org/"><img src="ledger-smb.png" width="200" height="100" border="0" alt="LedgerSMB Logo" /></a>
  85. <h1 class="login">|.$locale->text('Version').qq| $form->{version} <br />|.$locale->text('Administration').qq|</h1>
  86. <form method="post" action="admin.pl" name="admin">
  87. <table>
  88. <tr>
  89. <th>|.$locale->text('Password').qq|</th>
  90. <td><input type="password" name="password" /></td>
  91. <td><button type="submit" class="submit" name="action" value="login">|.$locale->text('Login').qq|</button></td>
  92. </tr>
  93. </table>
  94. <input type="hidden" name="action" value="login" />
  95. <input type="hidden" name="path" value="$form->{path}" />
  96. </form>
  97. |;
  98. if($errorMessage){
  99. print qq|<p><span style="font-weight:bold; color:red;">$errorMessage</span></p><br />|;
  100. }
  101. print qq|
  102. <br /><br />
  103. <p><a href="login.pl"
  104. >|.$locale->text("Application Login").qq|</a></p>
  105. <br /><br />
  106. <a style="font-size: 0.8em;" href="http://www.ledgersmb.org/">|.$locale->text('LedgerSMB website').qq|</a>
  107. </div>
  108. </body>
  109. </html>
  110. |;
  111. }
  112. sub login {
  113. &list_users;
  114. }
  115. sub logout {
  116. $form->{login} = 'admin';
  117. $form->{callback} = "admin.pl?action=adminlogin";
  118. Session::session_destroy($form);
  119. $form->redirect($locale->text('You are logged out'));
  120. }
  121. sub add_user {
  122. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Add User');
  123. if (-f "css/ledger-smb.css") {
  124. $myconfig->{stylesheet} = "ledger-smb.css";
  125. }
  126. $myconfig->{vclimit} = 1000;
  127. $myconfig->{menuwidth} = 155;
  128. $myconfig->{timeout} = 3600;
  129. &form_header;
  130. &form_footer;
  131. }
  132. sub edit {
  133. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Edit User');
  134. $form->{edit} = 1;
  135. &form_header;
  136. &form_footer;
  137. }
  138. sub form_footer {
  139. if ($form->{edit}) {
  140. $delete = qq|<button type="submit" class="submit" name="action" value="delete">|.$locale->text('Delete').qq|</button>
  141. <input type="hidden" name="edit" value="1" />|;
  142. }
  143. print qq|
  144. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}" />
  145. <input type="hidden" name="path" value="$form->{path}" />
  146. <button type="submit" class="submit" name="action" value="save">|.$locale->text('Save').qq|</button>
  147. $delete
  148. </form>
  149. </body>
  150. </html>
  151. |;
  152. }
  153. sub list_users {
  154. # use the central database handle
  155. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  156. my $fetchMembers = $dbh->selectall_arrayref("SELECT uc.name, uc.company, uc.templates,
  157. uc.dbuser, uc.dbdriver, uc.dbname,
  158. uc.dbhost, u.username
  159. FROM users as u, users_conf as uc
  160. WHERE u.id = uc.id
  161. AND u.id > 1
  162. ORDER BY u.username;", { Slice => {} });
  163. my @memberArray = ();
  164. my @member = ();
  165. foreach my $memberArray ( @$fetchMembers ) {
  166. $member{$memberArray->{username}} = $memberArray;
  167. }
  168. # type=submit $locale->text('Pg Database Administration')
  169. # type=submit $locale->text('PgPP Database Administration')
  170. foreach $item (LedgerSMB::User->dbdrivers) {
  171. $dbdrivers .= qq|<button name="action" type="submit" class="submit" value="|.(lc $item).'_database_administration">'.$locale->text("$item Database Administration").qq|</button>|;
  172. }
  173. $column_header{login} = qq|<th>|.$locale->text('Login').qq|</th>|;
  174. $column_header{name} = qq|<th>|.$locale->text('Name').qq|</th>|;
  175. $column_header{company} = qq|<th>|.$locale->text('Company').qq|</th>|;
  176. $column_header{dbdriver} = qq|<th>|.$locale->text('Driver').qq|</th>|;
  177. $column_header{dbhost} = qq|<th>|.$locale->text('Host').qq|</th>|;
  178. $column_header{dataset} = qq|<th>|.$locale->text('Dataset').qq|</th>|;
  179. $column_header{templates} = qq|<th>|.$locale->text('Templates').qq|</th>|;
  180. @column_index = qw(login name company dbdriver dbhost dataset templates);
  181. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." ".$locale->text('Administration');
  182. $form->{login} = "admin";
  183. $form->header;
  184. print qq|
  185. <body class="admin">
  186. <form method="post" action="$form->{script}">
  187. <table width="100%">
  188. <tr class="listheading">
  189. <th>$form->{title}</th>
  190. </tr>
  191. <tr size="5"></tr>
  192. <tr>
  193. <td>
  194. <table width="100%">
  195. <tr class="listheading">|;
  196. for (@column_index) { print "$column_header{$_}\n" }
  197. print qq| </tr>|;
  198. foreach $key (sort keys %member) {
  199. $href = "$script?action=edit&amp;login=$key&amp;path=$form->{path}";
  200. $href =~ s/ /%20/g;
  201. $member{$key}{templates} =~ s/^${LedgerSMB::Sysconfig::templates}\///;
  202. $column_data{login} = qq|<td><a href="$href">$key</a></td>|;
  203. $column_data{name} = qq|<td>$member{$key}{name}</td>|;
  204. $column_data{company} = qq|<td>$member{$key}{company}</td>|;
  205. $column_data{dbdriver} = qq|<td>$member{$key}{dbdriver}</td>|;
  206. $column_data{dbhost} = qq|<td>$member{$key}{dbhost}</td>|;
  207. $column_data{dataset} = qq|<td>$member{$key}{dbname}</td>|;
  208. $column_data{templates} = qq|<td>$member{$key}{templates}</td>|;
  209. $i++; $i %= 2;
  210. print qq| <tr class="listrow$i">|;
  211. for (@column_index) { print "$column_data{$_}\n"; }
  212. print qq| </tr>|;
  213. }
  214. print qq| </table>
  215. </td>
  216. </tr>
  217. <tr>
  218. <td><hr size="3" noshade /></td>
  219. </tr>
  220. </table>
  221. <input type="hidden" name="path" value="$form->{path}" />
  222. <br />
  223. <button type="submit" class="submit" name="action" value="add_user">|.$locale->text('Add User').qq|</button>
  224. <button type="submit" class="submit" name="action" value="change_admin_password">|.$locale->text('Change Admin Password').qq|</button>
  225. $dbdrivers
  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 !/(^\.\.?$|^\.svn)/, 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("$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. &adminlogin($locale->text('Access Denied!'));
  713. exit;
  714. }
  715. else{
  716. $form->{login} = 'admin';
  717. Session::session_create($form);
  718. }
  719. }
  720. else {
  721. $ENV{HTTP_COOKIE} =~ s/;\s*/;/g;
  722. @cookies = split /;/, $ENV{HTTP_COOKIE};
  723. foreach (@cookies) {
  724. ($name,$value) = split /=/, $_, 2;
  725. $cookie{$name} = $value;
  726. }
  727. if(!Session::session_check($cookie{"LedgerSMB"}, $root)){
  728. &adminlogin($locale->text('Session expired!'));
  729. exit;
  730. }
  731. }
  732. }
  733. sub pg_database_administration {
  734. $form->{dbdriver} = 'Pg';
  735. &dbselect_source;
  736. }
  737. sub pgpp_database_administration {
  738. $form->{dbdriver} = 'PgPP';
  739. &dbselect_source;
  740. }
  741. sub dbdriver_defaults {
  742. # load some defaults for the selected driver
  743. %driverdefaults = ( 'Pg' => { dbport => '5432',
  744. dbuser => '',
  745. dbdefault => 'template1',
  746. dbhost => 'localhost',
  747. connectstring => $locale->text('Connect to')
  748. } );
  749. $driverdefaults{PgPP} = $driverdefaults{Pg};
  750. for (keys %{ $driverdefaults{Pg} }) { $form->{$_} = $driverdefaults{$form->{dbdriver}}{$_} }
  751. }
  752. sub dbselect_source {
  753. &dbdriver_defaults;
  754. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." / ".$locale->text('Database Administration');
  755. $form->{login} = "admin";
  756. $form->header;
  757. #an insane amount of table nesting here, this should be cleaned up.
  758. print qq|
  759. <body class="admin">
  760. <center>
  761. <h2>$form->{title}</h2>
  762. <form method="post" action="$form->{script}" />
  763. <table>
  764. <tr>
  765. <td>
  766. <table>
  767. <tr class="listheading">
  768. <th colspan="4">|.$locale->text('Database').qq|</th>
  769. </tr>
  770. <tr>
  771. <td>
  772. <table>
  773. <tr>
  774. <th align="right">|.$locale->text('Host').qq|</th>
  775. <td><input name="dbhost" size="25" value="$form->{dbhost}" /></td>
  776. <th align="right">|.$locale->text('Port').qq|</th>
  777. <td><input name="dbport" size="5" value="$form->{dbport}" /></td>
  778. </tr>
  779. <tr>
  780. <th align="right">|.$locale->text('User').qq|</th>
  781. <td><input name="dbuser" size="10" value="$form->{dbuser}" /></td>
  782. <th align="right">|.$locale->text('Password').qq|</th>
  783. <td><input type="password" name="dbpasswd" size="10" /></td>
  784. </tr>
  785. <tr>
  786. <th align="right">$form->{connectstring}</th>
  787. <td colspan="3"><input name="dbdefault" size="10" value="$form->{dbdefault}" /></td>
  788. </tr>
  789. <tr>
  790. <th align="right">|.$locale->text("Superuser").qq|</th>
  791. <td><input name="dbsuperuser" size="10" value="$form->{dbsuperuser}" /></td>
  792. <th align="right">|.$locale->text('Password').qq|</th>
  793. <td><input type="password" name="dbsuperpasswd" size="10" /></td>
  794. </tr>
  795. </table>
  796. </td>
  797. </tr>
  798. </table>
  799. </td>
  800. </tr>
  801. </table>
  802. <input type="hidden" name="dbdriver" value="$form->{dbdriver}" />
  803. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}" />
  804. <input type="hidden" name="path" value="$form->{path}" />
  805. <br />
  806. <button type="submit" class="submit" name="action" value="create_dataset">|.$locale->text('Create 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. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  835. ." ".$locale->text('Database Administration')
  836. ." / ".$locale->text('Create Dataset');
  837. $form->{login} = "admin";
  838. $form->header;
  839. print qq|
  840. <body class="admin">
  841. <center>
  842. <h2>$form->{title}</h2>
  843. <form method="post" action="$form->{script}" />
  844. <table width="100%">
  845. <tr class="listheading">
  846. <th colspan="2">&nbsp;</th>
  847. </tr>
  848. <tr>
  849. <th align="right" nowrap="nowrap">|.$locale->text('Existing Datasets').qq|</th>
  850. <td>
  851. |;
  852. for (@dbsources) { print "[&nbsp;$_&nbsp;] " }
  853. print qq|
  854. </td>
  855. </tr>
  856. <tr>
  857. <th align="right" nowrap="nowrap">|.$locale->text('Create Dataset').qq|</th>
  858. <td><input name="db" /></td>
  859. </tr>
  860. <tr>
  861. <th align="right" nowrap="nowrap">|.$locale->text('Create Chart of Accounts').qq|</th>
  862. <td>
  863. <table>
  864. |;
  865. while (@charts) {
  866. print qq| <tr>|;
  867. for (0 .. 2) { print "<td>$charts[$_]</td>\n" }
  868. print qq| </tr>|;
  869. splice @charts, 0, 3;
  870. }
  871. print qq| </table>
  872. </td>
  873. </tr>
  874. <tr>
  875. <td colspan="2">
  876. <hr size="3" noshade />
  877. </td>
  878. </tr>
  879. </table>
  880. |;
  881. $form->hide_form(qw(dbdriver dbsuperuser dbsuperpasswd dbuser dbhost dbport dbpasswd dbdefault path));
  882. print qq|
  883. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}" />
  884. <input type="hidden" name="nextsub" value="dbcreate" />
  885. <br />
  886. <button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  887. </form>
  888. </body>
  889. </html>
  890. |;
  891. }
  892. sub dbcreate {
  893. $form->isblank("db", $locale->text('Dataset missing!'));
  894. LedgerSMB::User->dbcreate(\%$form);
  895. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  896. ." ".$locale->text('Database Administration')
  897. ." / ".$locale->text('Create Dataset');
  898. $form->{login} = "admin";
  899. $form->header;
  900. print qq|
  901. <body class="admin">
  902. <center>
  903. <h2>$form->{title}</h2>
  904. <form method="post" action="$form->{script}">|
  905. .$locale->text('Dataset [_1] successfully created!', $form->{db})
  906. .qq|
  907. <input type="hidden" name="path" value="$form->{path}" />
  908. <input type="hidden" name="nextsub" value="list_users" />
  909. <p><button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button></p>
  910. </form>
  911. </center>
  912. </body>
  913. </html>
  914. |;
  915. }
  916. sub unlock_system {
  917. # This needs to be done with a db tool
  918. # unlink "${LedgerSMB::Sysconfig::userspath}/nologin";
  919. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}";
  920. $form->redirect($locale->text('Lockfile removed!'));
  921. }
  922. sub lock_system {
  923. # This needs to be done with a db tool
  924. #open(FH, '>', "${LedgerSMB::Sysconfig::userspath}/nologin") or $form->error($locale->text('Cannot create Lock!'));
  925. #close(FH);
  926. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}";
  927. $form->redirect($locale->text('Lockfile created!'));
  928. }