summaryrefslogtreecommitdiff
path: root/bin/admin.pl
blob: c182371bdfbb11fa25d09327d31b8463eeea7fb6 (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->{callback} = "admin.pl?action=adminlogin";
  117. Session::session_destroy($form);
  118. $form->redirect($locale->text('You are logged out'));
  119. }
  120. sub add_user {
  121. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Add User');
  122. if (-f "css/ledger-smb.css") {
  123. $myconfig->{stylesheet} = "ledger-smb.css";
  124. }
  125. $myconfig->{vclimit} = 1000;
  126. $myconfig->{menuwidth} = 155;
  127. $myconfig->{timeout} = 3600;
  128. &form_header;
  129. &form_footer;
  130. }
  131. sub edit {
  132. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Edit User');
  133. $form->{edit} = 1;
  134. &form_header;
  135. &form_footer;
  136. }
  137. sub form_footer {
  138. if ($form->{edit}) {
  139. $delete = qq|<button type="submit" class="submit" name="action" value="delete">|.$locale->text('Delete').qq|</button>
  140. <input type="hidden" name="edit" value="1" />|;
  141. }
  142. print qq|
  143. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}" />
  144. <input type="hidden" name="path" value="$form->{path}" />
  145. <button type="submit" class="submit" name="action" value="save">|.$locale->text('Save').qq|</button>
  146. $delete
  147. </form>
  148. </body>
  149. </html>
  150. |;
  151. }
  152. sub list_users {
  153. # use the central database handle
  154. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  155. my $fetchMembers = $dbh->selectall_arrayref("SELECT uc.name, uc.company, uc.templates,
  156. uc.dbuser, uc.dbdriver, uc.dbname,
  157. uc.dbhost, u.username
  158. FROM users as u, users_conf as uc
  159. WHERE u.id = uc.id
  160. AND u.id > 1
  161. ORDER BY u.username;", { Slice => {} });
  162. my @memberArray = ();
  163. my @member = ();
  164. foreach my $memberArray ( @$fetchMembers ) {
  165. $member{$memberArray->{username}} = $memberArray;
  166. }
  167. # type=submit $locale->text('Pg Database Administration')
  168. # type=submit $locale->text('PgPP Database Administration')
  169. foreach $item (LedgerSMB::User->dbdrivers) {
  170. $dbdrivers .= qq|<button name="action" type="submit" class="submit" value="|.(lc $item).'_database_administration">'.$locale->text("$item Database Administration").qq|</button>|;
  171. }
  172. $column_header{login} = qq|<th>|.$locale->text('Login').qq|</th>|;
  173. $column_header{name} = qq|<th>|.$locale->text('Name').qq|</th>|;
  174. $column_header{company} = qq|<th>|.$locale->text('Company').qq|</th>|;
  175. $column_header{dbdriver} = qq|<th>|.$locale->text('Driver').qq|</th>|;
  176. $column_header{dbhost} = qq|<th>|.$locale->text('Host').qq|</th>|;
  177. $column_header{dataset} = qq|<th>|.$locale->text('Dataset').qq|</th>|;
  178. $column_header{templates} = qq|<th>|.$locale->text('Templates').qq|</th>|;
  179. @column_index = qw(login name company dbdriver dbhost dataset templates);
  180. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." ".$locale->text('Administration');
  181. $form->{login} = "admin";
  182. $form->header;
  183. print qq|
  184. <body class="admin">
  185. <form method="post" action="$form->{script}">
  186. <table width="100%">
  187. <tr class="listheading">
  188. <th>$form->{title}</th>
  189. </tr>
  190. <tr size="5"></tr>
  191. <tr>
  192. <td>
  193. <table width="100%">
  194. <tr class="listheading">|;
  195. for (@column_index) { print "$column_header{$_}\n" }
  196. print qq| </tr>|;
  197. foreach $key (sort keys %member) {
  198. $href = "$script?action=edit&amp;login=$key&amp;path=$form->{path}";
  199. $href =~ s/ /%20/g;
  200. $member{$key}{templates} =~ s/^${LedgerSMB::Sysconfig::templates}\///;
  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 !/(^\.\.?$|^\.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("${LedgerSMB::Sysconfig::memberfile}", "$form->{login}");
  574. # redo acs variable and delete all the acs codes
  575. @acs = split /;/, $form->{acs};
  576. $form->{acs} = "";
  577. foreach $item (@acs) {
  578. $item = $form->escape($item,1);
  579. if (!$form->{$item}) {
  580. $form->{acs} .= $form->unescape($form->unescape("$item")).";";
  581. }
  582. delete $form->{$item};
  583. }
  584. # check which database was filled in
  585. $form->{dbhost} = $form->{"$form->{dbdriver}_dbhost"};
  586. $form->{dbport} = $form->{"$form->{dbdriver}_dbport"};
  587. $form->{dbpasswd} = $form->{"$form->{dbdriver}_dbpasswd"};
  588. $form->{dbuser} = $form->{"$form->{dbdriver}_dbuser"};
  589. $form->{dbname} = $form->{"$form->{dbdriver}_dbname"};
  590. $form->isblank("dbname", $locale->text('Dataset missing!'));
  591. $form->isblank("dbuser", $locale->text('Database User missing!'));
  592. foreach $item (keys %{$form}) {
  593. $myconfig->{$item} = $form->{$item};
  594. }
  595. $myconfig->{password} = $form->{new_password};
  596. $myconfig->{timeout} = $form->{newtimeout};
  597. delete $myconfig->{stylesheet};
  598. if ($form->{userstylesheet}) {
  599. $myconfig->{stylesheet} = $form->{userstylesheet};
  600. }
  601. $myconfig->{packpw} = 1;
  602. $myconfig->save_member($form);
  603. # create user template directory and copy master files
  604. if (! -d "$form->{templates}") {
  605. umask(002);
  606. if (mkdir "$form->{templates}", oct("771")) {
  607. umask(007);
  608. # copy templates to the directory
  609. opendir TEMPLATEDIR, "${LedgerSMB::Sysconfig::templates}/." or $form->error(__FILE__.':'.__LINE__.': '."$templates : $!");
  610. @templates = grep /$form->{mastertemplates}-/, readdir TEMPLATEDIR;
  611. closedir TEMPLATEDIR;
  612. foreach $file (@templates) {
  613. open(TEMP, '<', "${LedgerSMB::Sysconfig::templates}/$file") or $form->error(__FILE__.':'.__LINE__.': '."$templates/$file : $!");
  614. $file =~ s/$form->{mastertemplates}-//;
  615. open(NEW, '>', "$form->{templates}/$file") or $form->error(__FILE__.':'.__LINE__.': '."$form->{templates}/$file : $!");
  616. while ($line = <TEMP>) {
  617. print NEW $line;
  618. }
  619. close(TEMP);
  620. close(NEW);
  621. }
  622. } else {
  623. $form->error(__FILE__.':'.__LINE__.': '."$form->{templates} : $!");
  624. }
  625. }
  626. $form->redirect($locale->text('User saved!'));
  627. }
  628. sub delete {
  629. $form->{callback} = "admin.pl?action=list_users";
  630. $form->{templates} = ($form->{templates}) ? "${LedgerSMB::Sysconfig::templates}/$form->{templates}" : "$templates/$form->{login}";
  631. # scan %user for $templatedir
  632. foreach $login (keys %user) {
  633. last if ($found = ($form->{templates} eq $user{$login}));
  634. }
  635. # if found keep directory otherwise delete
  636. if (!$found) {
  637. # delete it if there is a template directory
  638. $dir = "$form->{templates}";
  639. if (-d "$dir") {
  640. unlink <$dir/*>;
  641. rmdir "$dir";
  642. }
  643. }
  644. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  645. #users_conf
  646. my $deleteUser = $dbh->prepare("DELETE FROM users_conf USING users WHERE users.username = ? and users.id = users_conf.id;");
  647. $deleteUser->execute($form->{login});
  648. #and now users
  649. $deleteUser = $dbh->prepare("DELETE FROM users WHERE username = ?;");
  650. $deleteUser->execute($form->{login});
  651. $form->redirect($locale->text('User deleted!'));
  652. }
  653. sub login_name {
  654. my $login = shift;
  655. $login =~ s/\[\]//g;
  656. return ($login) ? $login : undef;
  657. }
  658. sub change_admin_password {
  659. $form->{title} = qq|LedgerSMB |.$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Change Admin Password');
  660. $form->{login} = "admin";
  661. $form->header;
  662. print qq|
  663. <body class="admin">
  664. <form method="post" action="$form->{script}">
  665. <table>
  666. <tr class="listheading">
  667. <th>|.$locale->text('Change Password').qq|</th>
  668. </tr>
  669. <tr size="5"></tr>
  670. <tr>
  671. <td>
  672. <table width="100%">
  673. <tr>
  674. <th align="right">|.$locale->text('Password').qq|</th>
  675. <td><input type="password" name="new_password" /></td>
  676. </tr>
  677. <tr>
  678. <th align="right">|.$locale->text('Confirm').qq|</th>
  679. <td><input type="password" name="confirm_password" /></td>
  680. </tr>
  681. </table>
  682. </td>
  683. </tr>
  684. </table>
  685. <br />
  686. <hr size="3" noshade />
  687. <input type="hidden" name="path" value="$form->{path}" />
  688. <p><button type="submit" class="submit" name="action" value="change_password">|.$locale->text('Change Password').qq|</button></p>
  689. </form>
  690. </body>
  691. </html>
  692. |;
  693. }
  694. sub change_password {
  695. # Do we want to force a login after changing the password?
  696. $form->{callback} = "admin.pl?";
  697. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('Passwords do not match!')) if $form->{new_password} ne $form->{confirm_password};
  698. # use the central database handle
  699. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  700. my $updateAdminPassword = $dbh->prepare("UPDATE users_conf
  701. SET password = md5(?)
  702. WHERE id = 1");
  703. $updateAdminPassword->execute($form->{new_password});
  704. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}";
  705. $form->redirect($locale->text('Password changed!'));
  706. }
  707. sub check_password {
  708. $root = LedgerSMB::User->new('admin');
  709. if ($form->{password}) {
  710. $form->{callback} .= "&amp;password=$form->{password}" if $form->{callback};
  711. if ($root->{password} ne (Digest::MD5::md5_hex $form->{password}) ) {
  712. &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. <button type="submit" class="submit" name="action" value="delete_dataset">|.$locale->text('Delete Dataset').qq|</button>
  808. </form>
  809. <p>|.$locale->text('This is a preliminary check for existing sources. Nothing will be created or deleted at this stage!')
  810. .qq|</p>
  811. </center>
  812. </body>
  813. </html>
  814. |;
  815. }
  816. sub continue {
  817. &{ $form->{nextsub} };
  818. }
  819. sub dbupdate {
  820. $form->{callback} = "admin.pl?action=list_users";
  821. LedgerSMB::User->dbupdate(\%$form);
  822. $form->redirect($locale->text('Dataset updated!'));
  823. }
  824. sub create_dataset {
  825. @dbsources = sort LedgerSMB::User->dbsources(\%$form);
  826. opendir SQLDIR, "sql/." or $form->error(__FILE__.':'.__LINE__.': '.$!);
  827. foreach $item (sort grep /-chart\.sql/, readdir SQLDIR) {
  828. next if ($item eq 'Default-chart.sql');
  829. $item =~ s/-chart\.sql//;
  830. push @charts, qq|<input name="chart" class="radio" type="radio" value="$item" />$item|;
  831. }
  832. closedir SQLDIR;
  833. # add Default at beginning
  834. unshift @charts, qq|<input name="chart" class="radio" type="radio" value="Default" checked />Default|;
  835. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  836. ." ".$locale->text('Database Administration')
  837. ." / ".$locale->text('Create Dataset');
  838. $form->{login} = "admin";
  839. $form->header;
  840. print qq|
  841. <body class="admin">
  842. <center>
  843. <h2>$form->{title}</h2>
  844. <form method="post" action="$form->{script}" />
  845. <table width="100%">
  846. <tr class="listheading">
  847. <th colspan="2">&nbsp;</th>
  848. </tr>
  849. <tr>
  850. <th align="right" nowrap="nowrap">|.$locale->text('Existing Datasets').qq|</th>
  851. <td>
  852. |;
  853. for (@dbsources) { print "[&nbsp;$_&nbsp;] " }
  854. print qq|
  855. </td>
  856. </tr>
  857. <tr>
  858. <th align="right" nowrap="nowrap">|.$locale->text('Create Dataset').qq|</th>
  859. <td><input name="db" /></td>
  860. </tr>
  861. <tr>
  862. <th align="right" nowrap="nowrap">|.$locale->text('Create Chart of Accounts').qq|</th>
  863. <td>
  864. <table>
  865. |;
  866. while (@charts) {
  867. print qq| <tr>|;
  868. for (0 .. 2) { print "<td>$charts[$_]</td>\n" }
  869. print qq| </tr>|;
  870. splice @charts, 0, 3;
  871. }
  872. print qq| </table>
  873. </td>
  874. </tr>
  875. <tr>
  876. <td colspan="2">
  877. <hr size="3" noshade />
  878. </td>
  879. </tr>
  880. </table>
  881. |;
  882. $form->hide_form(qw(dbdriver dbsuperuser dbsuperpasswd dbuser dbhost dbport dbpasswd dbdefault path));
  883. print qq|
  884. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}" />
  885. <input type="hidden" name="nextsub" value="dbcreate" />
  886. <br />
  887. <button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  888. </form>
  889. </body>
  890. </html>
  891. |;
  892. }
  893. sub dbcreate {
  894. $form->isblank("db", $locale->text('Dataset missing!'));
  895. LedgerSMB::User->dbcreate(\%$form);
  896. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  897. ." ".$locale->text('Database Administration')
  898. ." / ".$locale->text('Create Dataset');
  899. $form->{login} = "admin";
  900. $form->header;
  901. print qq|
  902. <body class="admin">
  903. <center>
  904. <h2>$form->{title}</h2>
  905. <form method="post" action="$form->{script}">|
  906. .$locale->text('Dataset [_1] successfully created!', $form->{db})
  907. .qq|
  908. <input type="hidden" name="path" value="$form->{path}" />
  909. <input type="hidden" name="nextsub" value="list_users" />
  910. <p><button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button></p>
  911. </form>
  912. </center>
  913. </body>
  914. </html>
  915. |;
  916. }
  917. sub delete_dataset {
  918. if (@dbsources = LedgerSMB::User->dbsources_unused(\%$form)) {
  919. foreach $item (sort @dbsources) {
  920. $dbsources .= qq|<input name="db" class="radio" type="radio" value="$item" />&nbsp;$item |;
  921. }
  922. } else {
  923. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('Nothing to delete!'));
  924. }
  925. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  926. ." ".$locale->text('Database Administration')
  927. ." / ".$locale->text('Delete Dataset');
  928. $form->{login} = "admin";
  929. $form->header;
  930. print qq|
  931. <body class="admin">
  932. <h2>$form->{title}</h2>
  933. <form method="post" action="$form->{script}" />
  934. <input type="hidden" name="dbdriver" value="$form->{dbdriver}" />
  935. <input type="hidden" name="dbuser" value="$form->{dbuser}" />
  936. <input type="hidden" name="dbhost" value="$form->{dbhost}" />
  937. <input type="hidden" name="dbport" value="$form->{dbport}" />
  938. <input type="hidden" name="dbpasswd" value="$form->{dbpasswd}" />
  939. <input type="hidden" name="dbdefault" value="$form->{dbdefault}" />
  940. <input name=callback type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}">
  941. <input type="hidden" name="path" value="$form->{path}" />
  942. <input type="hidden" name="nextsub" value="dbdelete" />
  943. <table width="100%">
  944. <tr class="listheading">
  945. <th>|.$locale->text('The following Datasets are not in use and can be deleted').qq|</th>
  946. </tr>
  947. <tr>
  948. <td>
  949. $dbsources
  950. </td>
  951. </tr>
  952. <tr>
  953. <td>
  954. <hr size="3" noshade />
  955. <br />
  956. <button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  957. </td>
  958. </tr>
  959. </table>
  960. </form>
  961. </body>
  962. </html>
  963. |;
  964. }
  965. sub dbdelete {
  966. if (!$form->{db}) {
  967. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('No Dataset selected!'));
  968. }
  969. LedgerSMB::User->dbdelete(\%$form);
  970. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  971. ." ".$locale->text('Database Administration')
  972. ." / ".$locale->text('Delete Dataset');
  973. $form->{login} = "admin";
  974. $form->header;
  975. print qq|
  976. <body class="admin">
  977. <center>
  978. <h2>$form->{title}</h2>
  979. $form->{db} |.$locale->text('successfully deleted!')
  980. .qq|
  981. <form method="post" action="$form->{script}" />
  982. <input type="hidden" name="path" value="$form->{path}" />
  983. <input type="hidden" name="nextsub" value="list_users" />
  984. <p><button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button></p>
  985. </form>
  986. </body>
  987. </html>
  988. |;
  989. }
  990. sub unlock_system {
  991. # This needs to be done with a db tool
  992. # unlink "${LedgerSMB::Sysconfig::userspath}/nologin";
  993. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}";
  994. $form->redirect($locale->text('Lockfile removed!'));
  995. }
  996. sub lock_system {
  997. # This needs to be done with a db tool
  998. #open(FH, '>', "${LedgerSMB::Sysconfig::userspath}/nologin") or $form->error($locale->text('Cannot create Lock!'));
  999. #close(FH);
  1000. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}";
  1001. $form->redirect($locale->text('Lockfile created!'));
  1002. }