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