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