summaryrefslogtreecommitdiff
path: root/bin/admin.pl
blob: 222237e6cd2b1e69b3b158a2b5d490acdc2d9dfd (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("Locale not loaded: $!\n");
  44. $locale->encoding('UTF-8');
  45. $form->{charset} = 'UTF-8';
  46. #$form->{charset} = $locale->encoding;
  47. eval { require DBI; };
  48. $form->error($locale->text('DBI not installed!')) if ($@);
  49. $form->{stylesheet} = "ledger-smb.css";
  50. $form->{favicon} = "favicon.ico";
  51. $form->{timeout} = 600;
  52. require "bin/pw.pl";
  53. # customization
  54. if (-f "bin/custom/$form->{script}") {
  55. eval { require "bin/custom/$form->{script}"; };
  56. $form->error($@) if ($@);
  57. }
  58. if ($form->{action}) {
  59. &check_password unless $form->{action} eq 'logout';
  60. &{ $form->{action} };
  61. } else {
  62. # if there are no drivers bail out
  63. $form->error($locale->text('No Database Drivers available!')) unless (LedgerSMB::User->dbdrivers);
  64. $root = LedgerSMB::User->new('admin');
  65. unless($root && $root->{password}){
  66. &setup_initial_password();
  67. exit;
  68. }
  69. &adminlogin;
  70. }
  71. 1;
  72. # end
  73. sub setup_initial_password {
  74. $form->header();
  75. print qq|
  76. <body class="admin" onload="sf()">
  77. <div align="center">
  78. <a href="http://www.ledgersmb.org/"><img src="ledger-smb.png" width="200" height="100" border="0" alt="LedgerSMB Logo" /></a>
  79. <h1 class="login">|.$locale->text('Version').qq| $form->{version} <br />|.$locale->text('Administration').qq|</h1>
  80. <fieldset><legend>|.$locale->text('Change password').qq|</legend>
  81. <p>|.$locale->text('This is your first time logging into LedgerSMB. Please set your administrative password').qq|</p>
  82. <form method="post" action="$form->{script}" name="admin">
  83. <table>
  84. <tr>
  85. <th align="right">|.$locale->text('Password').qq|</th>
  86. <td><input type="password" name="new_password" /></td>
  87. </tr>
  88. <tr>
  89. <th align="right">|.$locale->text('Confirm').qq|</th>
  90. <td><input type="password" name="confirm_password" /></td>
  91. </tr>
  92. </table>
  93. <input type="hidden" name="path" value="$form->{path}" />
  94. <input type="hidden" name="sessionid" value="$form->{sessionid}" />
  95. <p><button type="submit" class="submit" name="action" value="change_password">|.$locale->text('Change Password').qq|</button></p>
  96. </form>
  97. <a href="http://www.ledgersmb.org/">|.$locale->text('LedgerSMB website').qq|</a>
  98. </div>
  99. </body>
  100. </html>
  101. |;
  102. }
  103. sub adminlogin {
  104. $form->{title} = qq|LedgerSMB $form->{version} |.$locale->text('Administration');
  105. $myheaderadd = qq|
  106. <script language="JavaScript" type="text/javascript">
  107. <!--
  108. function sf(){
  109. document.admin.password.focus();
  110. }
  111. // End -->
  112. </script>
  113. |;
  114. $form->header(undef, $myheaderadd);
  115. print qq|
  116. <body class="admin" onload="sf()">
  117. <div align="center">
  118. <a href="http://www.ledgersmb.org/"><img src="ledger-smb.png" width="200" height="100" border="0" alt="LedgerSMB Logo" /></a>
  119. <h1 class="login">|.$locale->text('Version').qq| $form->{version} <br />|.$locale->text('Administration').qq|</h1>
  120. <form method="post" action="$form->{script}" name="admin">
  121. <table>
  122. <tr>
  123. <th>|.$locale->text('Password').qq|</th>
  124. <td><input type="password" name="password" /></td>
  125. <td><button type="submit" class="submit" name="action" value="login">|.$locale->text('Login').qq|</button></td>
  126. </tr>
  127. </table>
  128. <input type="hidden" name="action" value="login" />
  129. <input type="hidden" name="path" value="$form->{path}" />
  130. </form>
  131. <p><a href="login.pl"
  132. >|.$locale->text("Application Login").qq|</a></p>
  133. <a href="http://www.ledgersmb.org/">|.$locale->text('LedgerSMB website').qq|</a>
  134. </div>
  135. </body>
  136. </html>
  137. |;
  138. }
  139. sub login {
  140. &list_users;
  141. }
  142. sub logout {
  143. $form->{callback} = "$form->{script}?path=$form->{path}&amp;endsession=1";
  144. Session::session_destroy($form);
  145. $form->redirect($locale->text('You are logged out'));
  146. }
  147. sub add_user {
  148. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Add User');
  149. if (-f "css/ledger-smb.css") {
  150. $myconfig->{stylesheet} = "ledger-smb.css";
  151. }
  152. $myconfig->{vclimit} = 1000;
  153. $myconfig->{menuwidth} = 155;
  154. $myconfig->{timeout} = 3600;
  155. &form_header;
  156. &form_footer;
  157. }
  158. sub edit {
  159. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Edit User');
  160. $form->{edit} = 1;
  161. &form_header;
  162. &form_footer;
  163. }
  164. sub form_footer {
  165. if ($form->{edit}) {
  166. $delete = qq|<button type="submit" class="submit" name="action" value="delete">|.$locale->text('Delete').qq|</button>
  167. <input type="hidden" name="edit" value="1" />|;
  168. }
  169. print qq|
  170. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}&amp;sessionid=$form->{sessionid}" />
  171. <input type="hidden" name="path" value="$form->{path}" />
  172. <input type="hidden" name="sessionid" value="$form->{sessionid}" />
  173. <button type="submit" class="submit" name="action" value="save">|.$locale->text('Save').qq|</button>
  174. $delete
  175. </form>
  176. </body>
  177. </html>
  178. |;
  179. }
  180. sub list_users {
  181. #currently, this is disabled, but will set a value in the central db
  182. #$nologin = qq|<button type="submit" class="submit" name="action" value="lock_system">|.$locale->text('Lock System').qq|</button>|;
  183. #
  184. #if (-e "${LedgerSMB::Sysconfig::userspath}/nologin") {
  185. # $nologin = qq|<button type="submit" class="submit" name="action" value="unlock_system">|.$locale->text('Unlock System').qq|</button>|;
  186. #}
  187. while (<FH>) {
  188. chop;
  189. if (/^\[.*\]/) {
  190. $login = $_;
  191. $login =~ s/(\[|\])//g;
  192. }
  193. if (/^(name=|company=|templates=|dbuser=|dbdriver=|dbname=|dbhost=)/) {
  194. chop ($var = $&);
  195. ($null, $member{$login}{$var}) = split /=/, $_, 2;
  196. }
  197. }
  198. close(FH);
  199. # type=submit $locale->text('Pg Database Administration')
  200. # type=submit $locale->text('PgPP Database Administration')
  201. foreach $item (LedgerSMB::User->dbdrivers) {
  202. $dbdrivers .= qq|<button name="action" type="submit" class="submit" value="|.(lc $item).'_database_administration">'.$locale->text("$item Database Administration").qq|</button>|;
  203. }
  204. $column_header{login} = qq|<th>|.$locale->text('Login').qq|</th>|;
  205. $column_header{name} = qq|<th>|.$locale->text('Name').qq|</th>|;
  206. $column_header{company} = qq|<th>|.$locale->text('Company').qq|</th>|;
  207. $column_header{dbdriver} = qq|<th>|.$locale->text('Driver').qq|</th>|;
  208. $column_header{dbhost} = qq|<th>|.$locale->text('Host').qq|</th>|;
  209. $column_header{dataset} = qq|<th>|.$locale->text('Dataset').qq|</th>|;
  210. $column_header{templates} = qq|<th>|.$locale->text('Templates').qq|</th>|;
  211. @column_index = qw(login name company dbdriver dbhost dataset templates);
  212. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." ".$locale->text('Administration');
  213. $form->{login} = "admin";
  214. $form->header;
  215. print qq|
  216. <body class="admin">
  217. <form method="post" action="$form->{script}">
  218. <table width="100%">
  219. <tr class="listheading">
  220. <th>$form->{title}</th>
  221. </tr>
  222. <tr size="5"></tr>
  223. <tr>
  224. <td>
  225. <table width="100%">
  226. <tr class="listheading">|;
  227. for (@column_index) { print "$column_header{$_}\n" }
  228. print qq| </tr>|;
  229. foreach $key (sort keys %member) {
  230. $href = "$script?action=edit&amp;login=$key&amp;path=$form->{path}&amp;sessionid=$form->{sessionid}";
  231. $href =~ s/ /%20/g;
  232. $member{$key}{templates} =~ s/^${LedgerSMB::Sysconfig::templates}\///;
  233. $member{$key}{dbhost} = $locale->text('localhost') unless $member{$key}{dbhost};
  234. $column_data{login} = qq|<td><a href="$href">$key</a></td>|;
  235. $column_data{name} = qq|<td>$member{$key}{name}</td>|;
  236. $column_data{company} = qq|<td>$member{$key}{company}</td>|;
  237. $column_data{dbdriver} = qq|<td>$member{$key}{dbdriver}</td>|;
  238. $column_data{dbhost} = qq|<td>$member{$key}{dbhost}</td>|;
  239. $column_data{dataset} = qq|<td>$member{$key}{dbname}</td>|;
  240. $column_data{templates} = qq|<td>$member{$key}{templates}</td>|;
  241. $i++; $i %= 2;
  242. print qq| <tr class="listrow$i">|;
  243. for (@column_index) { print "$column_data{$_}\n"; }
  244. print qq| </tr>|;
  245. }
  246. print qq| </table>
  247. </td>
  248. </tr>
  249. <tr>
  250. <td><hr size="3" noshade /></td>
  251. </tr>
  252. </table>
  253. <input type="hidden" name="path" value="$form->{path}" />
  254. <input type="hidden" name="sessionid" value="$form->{sessionid}" />
  255. <br />
  256. <button type="submit" class="submit" name="action" value="add_user">|.$locale->text('Add User').qq|</button>
  257. <button type="submit" class="submit" name="action" value="change_admin_password">|.$locale->text('Change Admin Password').qq|</button>
  258. $dbdrivers
  259. $nologin
  260. <button type="submit" class="submit" name="action" value="logout">|.$locale->text('Logout').qq|</button>
  261. </form>
  262. |.$locale->text('Click on login name to edit!').qq|
  263. <br />
  264. |.$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|
  265. </body>
  266. </html>|;
  267. }
  268. sub form_header {
  269. # if there is a login, get user
  270. if ($form->{login}) {
  271. # get user
  272. %myconfig = %{LedgerSMB::User->fetch_config($form->{login})};
  273. for (qw(company address signature)) { $myconfig->{$_} = $form->quote($myconfig->{$_}) }
  274. for (qw(address signature)) { $myconfig->{$_} =~ s/\\n/\n/g }
  275. # strip basedir from templates directory
  276. $myconfig->{templates} =~ s/^${LedgerSMB::Sysconfig::templates}\///;
  277. $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
  278. }
  279. foreach $item (qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd)) {
  280. $dateformat .= ($item eq $myconfig->{dateformat}) ? "<option selected>$item</option>\n" : "<option>$item</option>\n";
  281. }
  282. my @formats = qw(1,000.00 1000.00 1.000,00 1000,00 1'000.00);
  283. push @formats, '1 000.00';
  284. foreach $item (@formats) {
  285. $numberformat .= ($item eq $myconfig->{numberformat}) ? "<option selected>$item</option>\n" : "<option>$item</option>\n";
  286. }
  287. %countrycodes = LedgerSMB::User->country_codes;
  288. $countrycodes = "";
  289. foreach $key (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
  290. $countrycodes .= ($myconfig->{countrycode} eq $key) ? qq|<option selected value="$key">$countrycodes{$key}</option>|
  291. : qq|<option value="$key">$countrycodes{$key}</option>|;
  292. }
  293. # is there a templates basedir
  294. if (! -d "${LedgerSMB::Sysconfig::templates}") {
  295. $form->error($locale->text('Directory [_1] does not exist', ${LedgerSMB::Sysconfig::templates}));
  296. }
  297. opendir TEMPLATEDIR, "${LedgerSMB::Sysconfig::templates}/." or $form->error("$templates : $!");
  298. @all = grep !/^\.\.?$/, readdir TEMPLATEDIR;
  299. closedir TEMPLATEDIR;
  300. @allhtml = sort grep /\.html/, @all;
  301. @alldir = ();
  302. for (@all) {
  303. if (-d "${LedgerSMB::Sysconfig::templates}/$_") {
  304. push @alldir, $_;
  305. }
  306. }
  307. @allhtml = reverse grep !/Default/, @allhtml;
  308. push @allhtml, 'Default';
  309. @allhtml = reverse @allhtml;
  310. foreach $item (sort @alldir) {
  311. if ($item eq $myconfig->{templates}) {
  312. $usetemplates .= qq|<option selected value="$item">$item</option>\n|;
  313. } else {
  314. $usetemplates .= qq|<option value="$item">$item</option>\n|;
  315. }
  316. }
  317. $lastitem = $allhtml[0];
  318. $lastitem =~ s/-.*//g;
  319. $mastertemplates = qq|<option value="$lastitem">$lastitem</option>\n|;
  320. foreach $item (@allhtml) {
  321. $item =~ s/-.*//g;
  322. if ($item ne $lastitem) {
  323. $mastertemplates .= qq|<option value="$item">$item</option>\n|;
  324. $lastitem = $item;
  325. }
  326. }
  327. opendir CSS, "css/.";
  328. @all = grep /.*\.css$/, readdir CSS;
  329. closedir CSS;
  330. foreach $item (@all) {
  331. if ($item eq $myconfig->{stylesheet}) {
  332. $selectstylesheet .= qq|<option selected value="$item">$item</option>\n|;
  333. } else {
  334. $selectstylesheet .= qq|<option value="$item">$item</option>\n|;
  335. }
  336. }
  337. $selectstylesheet .= "<option></option>\n";
  338. if (%{LedgerSMB::Sysconfig::printer} && ${LedgerSMB::Sysconfig::latex}) {
  339. $selectprinter = "<option></option>\n";
  340. foreach $item (sort keys %{LedgerSMB::Sysconfig::printer}) {
  341. if ($myconfig->{printer} eq $item) {
  342. $selectprinter .= qq|<option value="$item" selected>$item</option>\n|;
  343. } else {
  344. $selectprinter .= qq|<option value="$item">$item</option>\n|;
  345. }
  346. }
  347. $printer = qq|
  348. <tr>
  349. <th align="right">|.$locale->text('Printer').qq|</th>
  350. <td><select name="printer">$selectprinter</select></td>
  351. </tr>
  352. |;
  353. }
  354. $user = $form->{login};
  355. $form->{login} = "admin";
  356. $form->header;
  357. $form->{login} = $user;
  358. print qq|
  359. <body class="admin">
  360. <form method="post" action="$form->{script}">
  361. <table width="100%">
  362. <tr class="listheading"><th colspan="2">$form->{title}</th></tr>
  363. <tr size="5"></tr>
  364. <tr valign="top">
  365. <td>
  366. <table>
  367. <tr>
  368. <th align="right">|.$locale->text('Login').qq|</th>
  369. <td><input name="login" value="$myconfig->{login}" /></td>
  370. </tr>
  371. <tr>
  372. <th align="right">|.$locale->text('Password').qq|</th>
  373. <td><input type="password" name="new_password" size="8" value="$myconfig->{password}" /></td>
  374. <input type="hidden" name="old_password" value="$myconfig->{password}" />
  375. </tr>
  376. <tr>
  377. <th align="right">|.$locale->text('Name').qq|</th>
  378. <td><input name="name" size="15" value="$myconfig->{name}" /></td>
  379. </tr>
  380. <tr>
  381. <th align="right">|.$locale->text('E-mail').qq|</th>
  382. <td><input name="email" size="30" value="$myconfig->{email}" /></td>
  383. </tr>
  384. <tr valign="top">
  385. <th align="right">|.$locale->text('Signature').qq|</th>
  386. <td><textarea name="signature" rows="3" cols="35">$myconfig->{signature}</textarea></td>
  387. </tr>
  388. <tr>
  389. <th align="right">|.$locale->text('Phone').qq|</th>
  390. <td><input name="tel" size="14" value="$myconfig->{tel}" /></td>
  391. </tr>
  392. <tr>
  393. <th align="right">|.$locale->text('Fax').qq|</th>
  394. <td><input name="fax" size="14" value="$myconfig->{fax}" /></td>
  395. </tr>
  396. <tr>
  397. <th align="right">|.$locale->text('Company').qq|</th>
  398. <td><input name="company" size="35" value="$myconfig->{company}" /></td>
  399. </tr>
  400. <tr valign="top">
  401. <th align="right">|.$locale->text('Address').qq|</th>
  402. <td><textarea name="address" rows="4" cols="35">$myconfig->{address}</textarea></td>
  403. </tr>
  404. </table>
  405. </td>
  406. <td>
  407. <table>
  408. <tr>
  409. <th align="right">|.$locale->text('Date Format').qq|</th>
  410. <td><select name="dateformat">$dateformat</select></td>
  411. </tr>
  412. <tr>
  413. <th align="right">|.$locale->text('Number Format').qq|</th>
  414. <td><select name="numberformat">$numberformat</select></td>
  415. </tr>
  416. <tr>
  417. <th align="right">|.$locale->text('Dropdown Limit').qq|</th>
  418. <td><input name="vclimit" value="$myconfig->{vclimit}" /></td>
  419. </tr>
  420. <tr>
  421. <th align="right">|.$locale->text('Menu Width').qq|</th>
  422. <td><input name="menuwidth" value="$myconfig->{menuwidth}" /></td>
  423. </tr>
  424. <tr>
  425. <th align="right">|.$locale->text('Language').qq|</th>
  426. <td><select name="countrycode">$countrycodes</select></td>
  427. </tr>
  428. <tr>
  429. <th align="right">|.$locale->text('Session Timeout').qq|</th>
  430. <td><input name="newtimeout" value="$myconfig->{timeout}" /></td>
  431. </tr>
  432. <tr>
  433. <th align="right">|.$locale->text('Stylesheet').qq|</th>
  434. <td><select name="userstylesheet">$selectstylesheet</select></td>
  435. </tr>
  436. $printer
  437. <tr>
  438. <th align="right">|.$locale->text('Use Templates').qq|</th>
  439. <td><select name="usetemplates">$usetemplates</select></td>
  440. </tr>
  441. <tr>
  442. <th align="right">|.$locale->text('New Templates').qq|</th>
  443. <td><input name="newtemplates" /></td>
  444. </tr>
  445. <tr>
  446. <th align="right">|.$locale->text('Setup Templates').qq|</th>
  447. <td><select name="mastertemplates">$mastertemplates</select></td>
  448. </tr>
  449. </table>
  450. <input type="hidden" name="templates" value="$myconfig->{templates}" />
  451. </td>
  452. </tr>
  453. <tr class="listheading">
  454. <th colspan="2">|.$locale->text('Database').qq|</th>
  455. </tr>|;
  456. # list section for database drivers
  457. foreach $item (LedgerSMB::User->dbdrivers) {
  458. print qq|
  459. <tr>
  460. <td colspan="2">
  461. <table>
  462. <tr>|;
  463. $checked = "checked";
  464. if ($myconfig->{dbdriver} eq $item) {
  465. for (qw(dbhost dbport dbuser dbpasswd dbname sid)) { $form->{"${item}_$_"} = $myconfig->{$_} }
  466. $checked = "checked";
  467. }
  468. print qq|
  469. <th align="right">|.$locale->text('Driver').qq|</th>
  470. <td><input name="dbdriver" type="radio" class="radio" value="$item" $checked />&nbsp;$item</td>
  471. <th align="right">|.$locale->text('Host').qq|</th>
  472. <td><input name="${item}_dbhost" size="30" value="$form->{"${item}_dbhost"}" /></td>
  473. </tr>
  474. <tr>|;
  475. print qq|
  476. <th align="right">|.$locale->text('Dataset').qq|</th>
  477. <td><input name="${item}_dbname" size="15" value="$form->{"${item}_dbname"}" /></td>
  478. <th align="right">|.$locale->text('Port').qq|</th>
  479. <td><input name="${item}_dbport" size="4" value="$form->{"${item}_dbport"}" /></td>
  480. </tr>
  481. <tr>
  482. <th align="right">|.$locale->text('User').qq|</th>
  483. <td><input name="${item}_dbuser" size="15" value="$form->{"${item}_dbuser"}" /></td>
  484. <th align="right">|.$locale->text('Password').qq|</th>
  485. <td><input name="${item}_dbpasswd" type="password" size="10" value="$form->{"${item}_dbpasswd"}" /></td>
  486. </tr>|;
  487. print qq|
  488. </table>
  489. <input type="hidden" name="old_dbpasswd" value="$myconfig->{dbpasswd}" />
  490. </td>
  491. </tr>
  492. <tr>
  493. <td colspan="2"><hr size="2" noshade /></td>
  494. </tr>
  495. |;
  496. }
  497. # access control
  498. open(FH, $menufile) or $form->error("$menufile : $!");
  499. # scan for first menu level
  500. @a = <FH>;
  501. close(FH);
  502. if (open(FH, "custom_$menufile")) {
  503. push @a, <FH>;
  504. }
  505. close(FH);
  506. foreach $item (@a) {
  507. next unless $item =~ /\[\w+/;
  508. next if $item =~ /\#/;
  509. $item =~ s/(\[|\])//g;
  510. chop $item;
  511. if ($item =~ /--/) {
  512. ($level, $menuitem) = split /--/, $item, 2;
  513. } else {
  514. $level = $item;
  515. $menuitem = $item;
  516. push @acsorder, $item;
  517. }
  518. push @{ $acs{$level} }, $menuitem;
  519. }
  520. %role = ( 'admin' => $locale->text('Administrator'),
  521. 'user' => $locale->text('User'),
  522. 'supervisor' => $locale->text('Supervisor'),
  523. 'manager' => $locale->text('Manager'));
  524. $selectrole = "";
  525. foreach $item (qw(user admin supervisor manager)) {
  526. $selectrole .= ($myconfig->{role} eq $item) ? "<option selected value=\"$item\">$role{$item}</option>\n"
  527. : "<option value=\"$item\">$role{$item}</option>\n";
  528. }
  529. print qq|
  530. <tr class="listheading">
  531. <th colspan="2">|.$locale->text('Access Control').qq|</th>
  532. </tr>
  533. <tr>
  534. <td><select name="role">$selectrole</select></td>
  535. </tr>
  536. |;
  537. foreach $item (split /;/, $myconfig->{acs}) {
  538. ($key, $value) = split /--/, $item, 2;
  539. $excl{$key}{$value} = 1;
  540. }
  541. foreach $key (@acsorder) {
  542. $checked = "checked";
  543. if ($form->{login}) {
  544. $checked = ($excl{$key}{$key}) ? "" : "checked";
  545. }
  546. # can't have variable names with & and spaces
  547. $item = $form->escape("${key}--$key",1);
  548. $acsheading = $key;
  549. $acsheading =~ s/ /&nbsp;/g;
  550. $acsheading = qq|
  551. <td align="left" nowrap="nowrap" style="background-color: #C7E9F7" colspan="2">
  552. <input name="$item" class="checkbox" type="checkbox" value="1" $checked />&nbsp;$acsheading</td><tr><td>\n|;
  553. $menuitems .= "$item;";
  554. $acsdata = "<td style=\"background-color: #F0F0F0\">";
  555. foreach $item (@{ $acs{$key} }) {
  556. next if ($key eq $item);
  557. $checked = "checked";
  558. if ($form->{login}) {
  559. $checked = ($excl{$key}{$item}) ? "" : "checked";
  560. }
  561. $acsitem = $form->escape("${key}--$item",1);
  562. $acsdata .= qq|<br /><input name="$acsitem" class="checkbox" type="checkbox" value="1" $checked />&nbsp;$item|;
  563. $menuitems .= "$acsitem;";
  564. }
  565. $acsdata .= "
  566. </td>";
  567. print qq|
  568. <tr valign="top">$acsheading $acsdata
  569. </tr>
  570. |;
  571. }
  572. print qq|<input type="hidden" name="acs" value="$menuitems" />
  573. <tr>
  574. <td colspan="2"><hr size="3" noshade /></td>
  575. </tr>
  576. </table>
  577. </div>
  578. |;
  579. }
  580. sub save {
  581. # no driver checked
  582. $form->error($locale->text('Database Driver not checked!')) unless $form->{dbdriver};
  583. # no spaces allowed in login name
  584. $form->{login} =~ s/ //g;
  585. $form->isblank("login", $locale->text('Login name missing!'));
  586. # check for duplicates
  587. if (!$form->{edit}) {
  588. $temp = LedgerSMB::User->new($form->{login});
  589. if ($temp->{login}) {
  590. $form->error($locale->text('[_1] is already a member!', $form->{login}));
  591. }
  592. }
  593. # no spaces allowed in directories
  594. $form->{newtemplates} =~ s/( |\.\.|\*)//g;
  595. if ($form->{newtemplates} ne "") {
  596. $form->{templates} = $form->{newtemplates};
  597. } else {
  598. $form->{templates} = ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
  599. }
  600. # is there a basedir
  601. if (! -d "${LedgerSMB::Sysconfig::templates}") {
  602. $form->error($locale->text('Directory [_1] does not exist', ${LedgerSMB::Sysconfig::templates}));
  603. }
  604. # add base directory to $form->{templates}
  605. $form->{templates} = "${LedgerSMB::Sysconfig::templates}/$form->{templates}";
  606. $myconfig = LedgerSMB::User->new("${LedgerSMB::Sysconfig::memberfile}", "$form->{login}");
  607. # redo acs variable and delete all the acs codes
  608. @acs = split /;/, $form->{acs};
  609. $form->{acs} = "";
  610. foreach $item (@acs) {
  611. $item = $form->escape($item,1);
  612. if (!$form->{$item}) {
  613. $form->{acs} .= $form->unescape($form->unescape("$item")).";";
  614. }
  615. delete $form->{$item};
  616. }
  617. # check which database was filled in
  618. $form->{dbhost} = $form->{"$form->{dbdriver}_dbhost"};
  619. $form->{dbport} = $form->{"$form->{dbdriver}_dbport"};
  620. $form->{dbpasswd} = $form->{"$form->{dbdriver}_dbpasswd"};
  621. $form->{dbuser} = $form->{"$form->{dbdriver}_dbuser"};
  622. $form->{dbname} = $form->{"$form->{dbdriver}_dbname"};
  623. $form->isblank("dbname", $locale->text('Dataset missing!'));
  624. $form->isblank("dbuser", $locale->text('Database User missing!'));
  625. foreach $item (keys %{$form}) {
  626. $myconfig->{$item} = $form->{$item};
  627. }
  628. $myconfig->{password} = $form->{old_password};
  629. $myconfig->{password} = $form->{new_password} if $form->{new_password} ne $form->{old_password};
  630. $myconfig->{timeout} = $form->{newtimeout};
  631. delete $myconfig->{stylesheet};
  632. if ($form->{userstylesheet}) {
  633. $myconfig->{stylesheet} = $form->{userstylesheet};
  634. }
  635. $myconfig->{packpw} = 1;
  636. $myconfig->save_member($form);
  637. # create user template directory and copy master files
  638. if (! -d "$form->{templates}") {
  639. umask(002);
  640. if (mkdir "$form->{templates}", oct("771")) {
  641. umask(007);
  642. # copy templates to the directory
  643. opendir TEMPLATEDIR, "${LedgerSMB::Sysconfig::templates}/." or $form->error("$templates : $!");
  644. @templates = grep /$form->{mastertemplates}-/, readdir TEMPLATEDIR;
  645. closedir TEMPLATEDIR;
  646. foreach $file (@templates) {
  647. open(TEMP, "${LedgerSMB::Sysconfig::templates}/$file") or $form->error("$templates/$file : $!");
  648. $file =~ s/$form->{mastertemplates}-//;
  649. open(NEW, ">$form->{templates}/$file") or $form->error("$form->{templates}/$file : $!");
  650. while ($line = <TEMP>) {
  651. print NEW $line;
  652. }
  653. close(TEMP);
  654. close(NEW);
  655. }
  656. } else {
  657. $form->error("$form->{templates} : $!");
  658. }
  659. }
  660. $form->redirect($locale->text('User saved!'));
  661. }
  662. sub delete {
  663. $form->{templates} = ($form->{templates}) ? "${LedgerSMB::Sysconfig::templates}/$form->{templates}" : "$templates/$form->{login}";
  664. # scan %user for $templatedir
  665. foreach $login (keys %user) {
  666. last if ($found = ($form->{templates} eq $user{$login}));
  667. }
  668. # if found keep directory otherwise delete
  669. if (!$found) {
  670. # delete it if there is a template directory
  671. $dir = "$form->{templates}";
  672. if (-d "$dir") {
  673. unlink <$dir/*>;
  674. rmdir "$dir";
  675. }
  676. }
  677. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  678. #users_conf
  679. my $deleteUser = $dbh->prepare("DELETE FROM users_conf USING users WHERE users.username = ? and users.id = users_conf.id;");
  680. $deleteUser->execute($form->{login});
  681. #and now users
  682. $deleteUser = $dbh->prepare("DELETE FROM users WHERE username = ?;");
  683. $deleteUser->execute($form->{login});
  684. $form->redirect($locale->text('User deleted!'));
  685. }
  686. sub login_name {
  687. my $login = shift;
  688. $login =~ s/\[\]//g;
  689. return ($login) ? $login : undef;
  690. }
  691. sub change_admin_password {
  692. $form->{title} = qq|LedgerSMB |.$locale->text('Accounting')." ".$locale->text('Administration')." / ".$locale->text('Change Admin Password');
  693. $form->{login} = "root login";
  694. $form->header;
  695. print qq|
  696. <body class="admin">
  697. <form method="post" action="$form->{script}">
  698. <table>
  699. <tr class="listheading">
  700. <th>|.$locale->text('Change Password').qq|</th>
  701. </tr>
  702. <tr size="5"></tr>
  703. <tr>
  704. <td>
  705. <table width="100%">
  706. <tr>
  707. <th align="right">|.$locale->text('Password').qq|</th>
  708. <td><input type="password" name="new_password" /></td>
  709. </tr>
  710. <tr>
  711. <th align="right">|.$locale->text('Confirm').qq|</th>
  712. <td><input type="password" name="confirm_password" /></td>
  713. </tr>
  714. </table>
  715. </td>
  716. </tr>
  717. </table>
  718. <br />
  719. <hr size="3" noshade />
  720. <input type="hidden" name="path" value="$form->{path}" />
  721. <input type="hidden" name="sessionid" value="$form->{sessionid}" />
  722. <p><button type="submit" class="submit" name="action" value="change_password">|.$locale->text('Change Password').qq|</button></p>
  723. </form>
  724. </body>
  725. </html>
  726. |;
  727. }
  728. sub change_password {
  729. $form->error($locale->text('Passwords do not match!')) if $form->{new_password} ne $form->{confirm_password};
  730. $root->{password} = $form->{new_password};
  731. $root->{'root login'} = 1;
  732. $root->save_member();
  733. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}&amp;sessionid=$form->{sessionid}";
  734. $form->redirect($locale->text('Password changed!'));
  735. }
  736. sub check_password {
  737. $root = LedgerSMB::User->new('admin');
  738. if ($form->{password}) {
  739. $form->{callback} .= "&amp;password=$form->{password}" if $form->{callback};
  740. if ($root->{password} ne (Digest::MD5::md5_hex $form->{password}) ) {
  741. &getpassword;
  742. exit;
  743. }
  744. else{
  745. Session::session_create($root);
  746. }
  747. }
  748. else {
  749. $ENV{HTTP_COOKIE} =~ s/;\s*/;/g;
  750. @cookies = split /;/, $ENV{HTTP_COOKIE};
  751. foreach (@cookies) {
  752. ($name,$value) = split /=/, $_, 2;
  753. $cookie{$name} = $value;
  754. }
  755. if(!Session::session_check($cookie{"LedgerSMB"}, $root)){
  756. &getpassword(1);
  757. exit;
  758. }
  759. }
  760. }
  761. sub pg_database_administration {
  762. $form->{dbdriver} = 'Pg';
  763. &dbselect_source;
  764. }
  765. sub pgpp_database_administration {
  766. $form->{dbdriver} = 'PgPP';
  767. &dbselect_source;
  768. }
  769. sub dbdriver_defaults {
  770. # load some defaults for the selected driver
  771. %driverdefaults = ( 'Pg' => { dbport => '',
  772. dbuser => 'ledger-smb',
  773. dbdefault => 'template1',
  774. dbhost => '',
  775. connectstring => $locale->text('Connect to')
  776. } );
  777. $driverdefaults{PgPP} = $driverdefaults{Pg};
  778. for (keys %{ $driverdefaults{Pg} }) { $form->{$_} = $driverdefaults{$form->{dbdriver}}{$_} }
  779. }
  780. sub dbselect_source {
  781. &dbdriver_defaults;
  782. $form->{title} = "LedgerSMB ".$locale->text('Accounting')." / ".$locale->text('Database Administration');
  783. $form->{login} = "root login";
  784. $form->header;
  785. #an insane amount of table nesting here, this should be cleaned up.
  786. print qq|
  787. <body class="admin">
  788. <center>
  789. <h2>$form->{title}</h2>
  790. <form method="post" action="$form->{script}" />
  791. <table>
  792. <tr>
  793. <td>
  794. <table>
  795. <tr class="listheading">
  796. <th colspan="4">|.$locale->text('Database').qq|</th>
  797. </tr>
  798. <tr>
  799. <td>
  800. <table>
  801. <tr>
  802. <th align="right">|.$locale->text('Host').qq|</th>
  803. <td><input name="dbhost" size="25" value="$form->{dbhost}" /></td>
  804. <th align="right">|.$locale->text('Port').qq|</th>
  805. <td><input name="dbport" size="5" value="$form->{dbport}" /></td>
  806. </tr>
  807. <tr>
  808. <th align="right">|.$locale->text('User').qq|</th>
  809. <td><input name="dbuser" size="10" value="$form->{dbuser}" /></td>
  810. <th align="right">|.$locale->text('Password').qq|</th>
  811. <td><input type="password" name="dbpasswd" size="10" /></td>
  812. </tr>
  813. <tr>
  814. <th align="right">$form->{connectstring}</th>
  815. <td colspan="3"><input name="dbdefault" size="10" value="$form->{dbdefault}" /></td>
  816. </tr>
  817. <th align="right">|.$locale->text("Superuser").qq|</th>
  818. <td><input name="dbsuperuser" size="10" value="$form->{dbsuperuser}" /></td>
  819. <th align="right">|.$locale->text('Password').qq|</th>
  820. <td><input type="password" name="dbsuperpasswd" size="10" /></td>
  821. </table>
  822. </td>
  823. </tr>
  824. </table>
  825. </td>
  826. </tr>
  827. </table>
  828. <input type="hidden" name="dbdriver" value="$form->{dbdriver}" />
  829. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}&amp;sessionid=$form->{sessionid}" />
  830. <input type="hidden" name="path" value="$form->{path}" />
  831. <input type="hidden" name="sessionid" value="$form->{sessionid}" />
  832. <br />
  833. <button type="submit" class="submit" name="action" value="create_dataset">|.$locale->text('Create Dataset').qq|</button>
  834. <button type="submit" class="submit" name="action" value="delete_dataset">|.$locale->text('Delete Dataset').qq|</button>
  835. </form>
  836. <p>|.$locale->text('This is a preliminary check for existing sources. Nothing will be created or deleted at this stage!')
  837. .qq|</p>
  838. </center>
  839. </body>
  840. </html>
  841. |;
  842. }
  843. sub continue {
  844. &{ $form->{nextsub} };
  845. }
  846. sub dbupdate {
  847. LedgerSMB::User->dbupdate(\%$form);
  848. $form->redirect($locale->text('Dataset updated!'));
  849. }
  850. sub create_dataset {
  851. @dbsources = sort LedgerSMB::User->dbsources(\%$form);
  852. opendir SQLDIR, "sql/." or $form->error($!);
  853. foreach $item (sort grep /-chart\.sql/, readdir SQLDIR) {
  854. next if ($item eq 'Default-chart.sql');
  855. $item =~ s/-chart\.sql//;
  856. push @charts, qq|<input name="chart" class="radio" type="radio" value="$item" />$item|;
  857. }
  858. closedir SQLDIR;
  859. # add Default at beginning
  860. unshift @charts, qq|<input name="chart" class="radio" type="radio" value="Default" checked />Default|;
  861. $selectencoding = qq|<option></option>
  862. <option value="SQL_ASCII">ASCII</option>
  863. <option value="EUC_JP">Japanese EUC</option>
  864. <option value="EUC_CN">Chinese EUC</option>
  865. <option value="EUC_KR">Korean EUC</option>
  866. <option value="JOHAB">Korean EUC (Hangle base)</option>
  867. <option value="EUC_TW">Taiwan EUC</option>
  868. <option value="UNICODE">Unicode (UTF-8)</option>
  869. <option value="MULE_INTERNAL">Mule internal type</option>
  870. <option value="LATIN1">ISO 8859-1/ECMA 94 (Latin alphabet no. 1)</option>
  871. <option value="LATIN2">ISO 8859-2/ECMA 94 (Latin alphabet no. 2)</option>
  872. <option value="LATIN3">ISO 8859-3/ECMA 94 (Latin alphabet no. 3)</option>
  873. <option value="LATIN4">ISO 8859-4/ECMA 94 (Latin alphabet no. 4)</option>
  874. <option value="LATIN5">ISO 8859-9/ECMA 128 (Latin alphabet no. 5)</option>
  875. <option value="LATIN6">ISO 8859-10/ECMA 144 (Latin alphabet no. 6)</option>
  876. <option value="LATIN7">ISO 8859-13 (Latin alphabet no. 7)</option>
  877. <option value="LATIN8">ISO 8859-14 (Latin alphabet no. 8)</option>
  878. <option value="LATIN9">ISO 8859-15 (Latin alphabet no. 9)</option>
  879. <option value="LATIN10">ISO 8859-16/ASRO SR 14111 (Latin alphabet no. 10)</option>
  880. <option value="ISO_8859_5">ISO 8859-5/ECMA 113 (Latin/Cyrillic)</option>
  881. <option value="ISO_8859_6">ISO 8859-6/ECMA 114 (Latin/Arabic)</option>
  882. <option value="ISO_8859_7">ISO 8859-7/ECMA 118 (Latin/Greek)</option>
  883. <option value="ISO_8859_8">ISO 8859-8/ECMA 121 (Latin/Hebrew)</option>
  884. <option value="KOI8">KOI8-R(U)</option>
  885. <option value="WIN">Windows CP1251</option>
  886. <option value="ALT">Windows CP866</option>
  887. <option value="WIN1256">Windows CP1256 (Arabic)</option>
  888. <option value="TCVN">Windows CP1258 (Vietnamese)</option>
  889. <option value="WIN874">Windows CP874 (Thai)</option>
  890. |;
  891. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  892. ." ".$locale->text('Database Administration')
  893. ." / ".$locale->text('Create Dataset');
  894. $form->{login} = "root login";
  895. $form->header;
  896. print qq|
  897. <body class="admin">
  898. <center>
  899. <h2>$form->{title}</h2>
  900. <form method="post" action="$form->{script}" />
  901. <table width="100%">
  902. <tr class="listheading">
  903. <th colspan="2">&nbsp;</th>
  904. </tr>
  905. <tr>
  906. <th align="right" nowrap="nowrap">|.$locale->text('Existing Datasets').qq|</th>
  907. <td>
  908. |;
  909. for (@dbsources) { print "[&nbsp;$_&nbsp;] " }
  910. print qq|
  911. </td>
  912. </tr>
  913. <tr>
  914. <th align="right" nowrap="nowrap">|.$locale->text('Create Dataset').qq|</th>
  915. <td><input name="db" /></td>
  916. </tr>
  917. <tr>
  918. <th align="right" nowrap="nowrap">|.$locale->text('Multibyte Encoding').qq|</th>
  919. <td><select name="encoding">$selectencoding</select></td>
  920. </tr>
  921. <tr>
  922. <th align="right" nowrap="nowrap">|.$locale->text('Create Chart of Accounts').qq|</th>
  923. <td>
  924. <table>
  925. |;
  926. while (@charts) {
  927. print qq| <tr>|;
  928. for (0 .. 2) { print "<td>$charts[$_]</td>\n" }
  929. print qq| </tr>|;
  930. splice @charts, 0, 3;
  931. }
  932. print qq| </table>
  933. </td>
  934. </tr>
  935. <tr>
  936. <td colspan="2">
  937. <hr size="3" noshade />
  938. </td>
  939. </tr>
  940. </table>
  941. |;
  942. $form->hide_form(qw(dbdriver dbsuperuser dbsuperpasswd dbuser dbhost dbport dbpasswd dbdefault path sessionid));
  943. print qq|
  944. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}&amp;sessionid=$form->{sessionid}" />
  945. <input type="hidden" name="nextsub" value="dbcreate" />
  946. <br />
  947. <button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  948. </form>
  949. </body>
  950. </html>
  951. |;
  952. }
  953. sub dbcreate {
  954. $form->isblank("db", $locale->text('Dataset missing!'));
  955. LedgerSMB::User->dbcreate(\%$form);
  956. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  957. ." ".$locale->text('Database Administration')
  958. ." / ".$locale->text('Create Dataset');
  959. $form->{login} = "root login";
  960. $form->header;
  961. print qq|
  962. <body class="admin">
  963. <center>
  964. <h2>$form->{title}</h2>
  965. <form method="post" action="$form->{script}">|
  966. .$locale->text('Dataset [_1] successfully created!', $form->{db})
  967. .qq|
  968. <input type="hidden" name="path" value="$form->{path}" />
  969. <input type="hidden" name="sessionid" value="$form->{sessionid}" />
  970. <input type="hidden" name="nextsub" value="list_users" />
  971. <p><button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button></p>
  972. </form>
  973. </center>
  974. </body>
  975. </html>
  976. |;
  977. }
  978. sub delete_dataset {
  979. if (@dbsources = LedgerSMB::User->dbsources_unused(\%$form)) {
  980. foreach $item (sort @dbsources) {
  981. $dbsources .= qq|<input name="db" class="radio" type="radio" value="$item" />&nbsp;$item |;
  982. }
  983. } else {
  984. $form->error($locale->text('Nothing to delete!'));
  985. }
  986. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  987. ." ".$locale->text('Database Administration')
  988. ." / ".$locale->text('Delete Dataset');
  989. $form->{login} = "root login";
  990. $form->header;
  991. print qq|
  992. <body class="admin">
  993. <h2>$form->{title}</h2>
  994. <form method="post" action="$form->{script}" />
  995. <input type="hidden" name="dbdriver" value="$form->{dbdriver}" />
  996. <input type="hidden" name="dbuser" value="$form->{dbuser}" />
  997. <input type="hidden" name="dbhost" value="$form->{dbhost}" />
  998. <input type="hidden" name="dbport" value="$form->{dbport}" />
  999. <input type="hidden" name="dbpasswd" value="$form->{dbpasswd}" />
  1000. <input type="hidden" name="dbdefault" value="$form->{dbdefault}" />
  1001. <input name=callback type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}&amp;sessionid=$form->{sessionid}">
  1002. <input type="hidden" name="path" value="$form->{path}" />
  1003. <input type="hidden" name="sessionid" value="$form->{sessionid}" />
  1004. <input type="hidden" name="nextsub" value="dbdelete" />
  1005. <table width="100%">
  1006. <tr class="listheading">
  1007. <th>|.$locale->text('The following Datasets are not in use and can be deleted').qq|</th>
  1008. </tr>
  1009. <tr>
  1010. <td>
  1011. $dbsources
  1012. </td>
  1013. </tr>
  1014. <tr>
  1015. <td>
  1016. <hr size="3" noshade />
  1017. <br />
  1018. <button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  1019. </td>
  1020. </tr>
  1021. </table>
  1022. </form>
  1023. </body>
  1024. </html>
  1025. |;
  1026. }
  1027. sub dbdelete {
  1028. if (!$form->{db}) {
  1029. $form->error($locale->text('No Dataset selected!'));
  1030. }
  1031. LedgerSMB::User->dbdelete(\%$form);
  1032. $form->{title} = "LedgerSMB ".$locale->text('Accounting')
  1033. ." ".$locale->text('Database Administration')
  1034. ." / ".$locale->text('Delete Dataset');
  1035. $form->{login} = "root login";
  1036. $form->header;
  1037. print qq|
  1038. <body class="admin">
  1039. <center>
  1040. <h2>$form->{title}</h2>
  1041. $form->{db} |.$locale->text('successfully deleted!')
  1042. .qq|
  1043. <form method="post" action="$form->{script}" />
  1044. <input type="hidden" name="path" value="$form->{path}" />
  1045. <input type="hidden" name="sessionid" value="$form->{sessionid}" />
  1046. <input type="hidden" name="nextsub" value="list_users" />
  1047. <p><button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button></p>
  1048. </form>
  1049. </body>
  1050. </html>
  1051. |;
  1052. }
  1053. sub unlock_system {
  1054. # This needs to be done with a db tool
  1055. # unlink "${LedgerSMB::Sysconfig::userspath}/nologin";
  1056. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}&amp;sessionid=$form->{sessionid}";
  1057. $form->redirect($locale->text('Lockfile removed!'));
  1058. }
  1059. sub lock_system {
  1060. # This needs to be done with a db tool
  1061. #open(FH, ">${LedgerSMB::Sysconfig::userspath}/nologin") or $form->error($locale->text('Cannot create Lock!'));
  1062. #close(FH);
  1063. $form->{callback} = "$form->{script}?action=list_users&amp;path=$form->{path}&amp;sessionid=$form->{sessionid}";
  1064. $form->redirect($locale->text('Lockfile created!'));
  1065. }