/doc/examples/softwaresite/

mb/diff/bin/admin.pl?id=f1756b69854a21b50da387f32f394d4b6fb80be7'>diff
path: root/bin/admin.pl
blob: 5a89da020d3a6618c7a9cf70d439cd491c57b263 (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. use LedgerSMB::Form;
  37. use LedgerSMB::Locale;
  38. use LedgerSMB::User;
  39. use LedgerSMB::Session;
  40. $form = new Form;
  41. $locale = LedgerSMB::Locale->get_handle( ${LedgerSMB::Sysconfig::language} )
  42. 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(
  48. __FILE__ . ':' . __LINE__ . ': ' . $locale->text('DBI not installed!') )
  49. if ($@);
  50. $form->{stylesheet} = "ledgersmb.css";
  51. $form->{favicon} = "favicon.ico";
  52. $form->{timeout} = 600;
  53. require "bin/pw.pl";
  54. # customization
  55. if ( -f "bin/custom/$form->{script}" ) {
  56. eval { require "bin/custom/$form->{script}"; };
  57. $form->error( __FILE__ . ':' . __LINE__ . ': ' . $@ ) if ($@);
  58. }
  59. if ( $form->{action} ) {
  60. &check_password unless $form->{action} eq 'logout';
  61. &{ $form->{action} };
  62. }
  63. else {
  64. # if there are no drivers bail out
  65. $form->error( __FILE__ . ':' . __LINE__ . ': '
  66. . $locale->text('No Database Drivers available!') )
  67. unless ( LedgerSMB::User->dbdrivers );
  68. $root = LedgerSMB::User->new('admin');
  69. &adminlogin;
  70. }
  71. 1;
  72. # end
  73. sub adminlogin {
  74. my ($errorMessage) = @_;
  75. $form->{title} =
  76. qq|LedgerSMB $form->{version} | . $locale->text('Administration');
  77. $myheaderadd = qq|
  78. <script language="JavaScript" type="text/javascript">
  79. <!--
  80. function sf(){
  81. document.admin.password.focus();
  82. }
  83. // End -->
  84. </script>
  85. |;
  86. $form->header( undef, $myheaderadd );
  87. print qq|
  88. <body class="admin" onload="sf()">
  89. <div align="center">
  90. <a href="http://www.ledgersmb.org/"><img src="images/ledgersmb.png" width="200" height="100" border="0" alt="LedgerSMB Logo" /></a>
  91. <h1 class="login">|
  92. . $locale->text('Version')
  93. . qq| $form->{version} <br />|
  94. . $locale->text('Administration')
  95. . qq|</h1>
  96. <form method="post" action="admin.pl" name="admin">
  97. <table>
  98. <tr>
  99. <th>| . $locale->text('Password') . qq|</th>
  100. <td><input type="password" name="password" /></td>
  101. <td><button type="submit" class="submit" name="action" value="login">|
  102. . $locale->text('Login')
  103. . qq|</button></td>
  104. </tr>
  105. </table>
  106. <input type="hidden" name="action" value="login" />
  107. <input type="hidden" name="path" value="$form->{path}" />
  108. </form>
  109. |;
  110. if ($errorMessage) {
  111. print
  112. qq|<p><span style="font-weight:bold; color:red;">$errorMessage</span></p><br />|;
  113. }
  114. print qq|
  115. <br /><br />
  116. <p><a href="login.pl"
  117. >| . $locale->text("Application Login") . qq|</a></p>
  118. <br /><br />
  119. <a style="font-size: 0.8em;" href="http://www.ledgersmb.org/">|
  120. . $locale->text('LedgerSMB website') . qq|</a>
  121. </div>
  122. </body>
  123. </html>
  124. |;
  125. }
  126. sub login {
  127. &list_users;
  128. }
  129. sub logout {
  130. $form->{login} = 'admin';
  131. $form->{callback} = "admin.pl?action=adminlogin";
  132. Session::session_destroy($form);
  133. $form->redirect( $locale->text('You are logged out') );
  134. }
  135. sub add_user {
  136. $form->{title} =
  137. "LedgerSMB "
  138. . $locale->text('Accounting') . " "
  139. . $locale->text('Administration') . " / "
  140. . $locale->text('Add User');
  141. if ( -f "css/ledgersmb.css" ) {
  142. $myconfig->{stylesheet} = "ledgersmb.css";
  143. }
  144. $myconfig->{vclimit} = 1000;
  145. $myconfig->{menuwidth} = 155;
  146. $myconfig->{timeout} = 3600;
  147. &form_header;
  148. &form_footer;
  149. }
  150. sub edit {
  151. $form->{title} =
  152. "LedgerSMB "
  153. . $locale->text('Accounting') . " "
  154. . $locale->text('Administration') . " / "
  155. . $locale->text('Edit User');
  156. $form->{edit} = 1;
  157. &form_header;
  158. &form_footer;
  159. }
  160. sub form_footer {
  161. if ( $form->{edit} ) {
  162. $delete =
  163. qq|<button type="submit" class="submit" name="action" value="delete">|
  164. . $locale->text('Delete')
  165. . qq|</button>
  166. <input type="hidden" name="edit" value="1" />|;
  167. }
  168. print qq|
  169. <input name="callback" type="hidden" value="$form->{script}?action=list_users&amp;path=$form->{path}" />
  170. <input type="hidden" name="path" value="$form->{path}" />
  171. <button type="submit" class="submit" name="action" value="save">|
  172. . $locale->text('Save')
  173. . qq|</button>
  174. $delete
  175. </form>
  176. </body>
  177. </html>
  178. |;
  179. }
  180. sub list_users {
  181. # use the central database handle
  182. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  183. my $fetchMembers = $dbh->selectall_arrayref(
  184. "SELECT uc.name, uc.company, uc.templates,
  185. uc.dbuser, uc.dbdriver, uc.dbname,
  186. uc.dbhost, u.username
  187. FROM users as u, users_conf as uc
  188. WHERE u.id = uc.id
  189. AND u.id > 1
  190. ORDER BY u.username;", { Slice => {} }
  191. );
  192. my @memberArray = ();
  193. my @member = ();
  194. foreach my $memberArray (@$fetchMembers) {
  195. $member{ $memberArray->{username} } = $memberArray;
  196. }
  197. # type=submit $locale->text('Pg Database Administration')
  198. # type=submit $locale->text('PgPP Database Administration')
  199. foreach $item ( LedgerSMB::User->dbdrivers ) {
  200. $dbdrivers .=
  201. qq|<button name="action" type="submit" class="submit" value="|
  202. . ( lc $item )
  203. . '_database_administration">'
  204. . $locale->text("$item Database Administration")
  205. . qq|</button>|;
  206. }
  207. $column_header{login} = qq|<th>| . $locale->text('Login') . qq|</th>|;
  208. $column_header{name} = qq|<th>| . $locale->text('Name') . qq|</th>|;
  209. $column_header{company} = qq|<th>| . $locale->text('Company') . qq|</th>|;
  210. $column_header{dbdriver} = qq|<th>| . $locale->text('Driver') . qq|</th>|;
  211. $column_header{dbhost} = qq|<th>| . $locale->text('Host') . qq|</th>|;
  212. $column_header{dataset} = qq|<th>| . $locale->text('Dataset') . qq|</th>|;
  213. $column_header{templates} =
  214. qq|<th>| . $locale->text('Templates') . qq|</th>|;
  215. @column_index = qw(login name company dbdriver dbhost dataset templates);
  216. $form->{title} =
  217. "LedgerSMB "
  218. . $locale->text('Accounting') . " "
  219. . $locale->text('Administration');
  220. $form->{login} = "admin";
  221. $form->header;
  222. print qq|
  223. <body class="admin">
  224. <form method="post" action="$form->{script}">
  225. <table width="100%">
  226. <tr class="listheading">
  227. <th>$form->{title}</th>
  228. </tr>
  229. <tr size="5"></tr>
  230. <tr>
  231. <td>
  232. <table width="100%">
  233. <tr class="listheading">|;
  234. for (@column_index) { print "$column_header{$_}\n" }
  235. print qq| </tr>|;
  236. foreach $key ( sort keys %member ) {
  237. $href = "$script?action=edit&amp;login=$key&amp;path=$form->{path}";
  238. $href =~ s/ /%20/g;
  239. $member{$key}{templates} =~ s/^${LedgerSMB::Sysconfig::templates}\///;
  240. $column_data{login} = qq|<td><a href="$href">$key</a></td>|;
  241. $column_data{name} = qq|<td>$member{$key}{name}</td>|;
  242. $column_data{company} = qq|<td>$member{$key}{company}</td>|;
  243. $column_data{dbdriver} = qq|<td>$member{$key}{dbdriver}</td>|;
  244. $column_data{dbhost} = qq|<td>$member{$key}{dbhost}</td>|;
  245. $column_data{dataset} = qq|<td>$member{$key}{dbname}</td>|;
  246. $column_data{templates} = qq|<td>$member{$key}{templates}</td>|;
  247. $i++;
  248. $i %= 2;
  249. print qq| <tr class="listrow$i">|;
  250. for (@column_index) { print "$column_data{$_}\n"; }
  251. print qq| </tr>|;
  252. }
  253. print qq| </table>
  254. </td>
  255. </tr>
  256. <tr>
  257. <td><hr size="3" noshade /></td>
  258. </tr>
  259. </table>
  260. <input type="hidden" name="path" value="$form->{path}" />
  261. <br />
  262. <button type="submit" class="submit" name="action" value="add_user">|
  263. . $locale->text('Add User')
  264. . qq|</button>
  265. <button type="submit" class="submit" name="action" value="change_admin_password">|
  266. . $locale->text('Change Admin Password')
  267. . qq|</button>
  268. $dbdrivers
  269. <button type="submit" class="submit" name="action" value="logout">|
  270. . $locale->text('Logout')
  271. . qq|</button>
  272. </form>
  273. | . $locale->text('Click on login name to edit!') . qq|
  274. <br />
  275. |
  276. . $locale->text(
  277. '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.'
  278. )
  279. . qq|
  280. </body>
  281. </html>|;
  282. }
  283. sub form_header {
  284. # if there is a login, get user
  285. if ( $form->{login} ) {
  286. # get user
  287. %{$myconfig} = %{ LedgerSMB::User->fetch_config( $form->{login} ) };
  288. for (qw(company address signature)) {
  289. $myconfig->{$_} = $form->quote( $myconfig->{$_} );
  290. }
  291. for (qw(address signature)) { $myconfig->{$_} =~ s/\\n/\n/g }
  292. # strip basedir from templates directory
  293. $myconfig->{templates} =~ s/^${LedgerSMB::Sysconfig::templates}\///;
  294. }
  295. foreach $item (qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd))
  296. {
  297. $dateformat .=
  298. ( $item eq $myconfig->{dateformat} )
  299. ? "<option selected>$item</option>\n"
  300. : "<option>$item</option>\n";
  301. }
  302. my @formats = qw(1,000.00 1000.00 1.000,00 1000,00 1'000.00);
  303. push @formats, '1 000.00';
  304. foreach $item (@formats) {
  305. $numberformat .=
  306. ( $item eq $myconfig->{numberformat} )
  307. ? "<option selected>$item</option>\n"
  308. : "<option>$item</option>\n";
  309. }
  310. %countrycodes = LedgerSMB::User->country_codes;
  311. $countrycodes = "";
  312. my $selectedcode =
  313. ( $myconfig->{countrycode} ) ? $myconfig->{countrycode} : 'en';
  314. foreach $key ( sort { $countrycodes{$a} cmp $countrycodes{$b} }
  315. keys %countrycodes )
  316. {
  317. $countrycodes .=
  318. ( $selectedcode eq $key )
  319. ? qq|<option selected value="$key">$countrycodes{$key}</option>|
  320. : qq|<option value="$key">$countrycodes{$key}</option>|;
  321. }
  322. # is there a templates basedir
  323. if ( !-d "${LedgerSMB::Sysconfig::templates}" ) {
  324. $form->error(
  325. __FILE__ . ':' . __LINE__ . ': '
  326. . $locale->text(
  327. 'Directory [_1] does not exist',
  328. ${LedgerSMB::Sysconfig::templates}
  329. )
  330. );
  331. }
  332. opendir TEMPLATEDIR, "${LedgerSMB::Sysconfig::templates}/."
  333. or $form->error( __FILE__ . ':' . __LINE__ . ': ' . "$templates : $!" );
  334. @all = grep !/(^\.\.?$|^\.svn)/, readdir TEMPLATEDIR;
  335. closedir TEMPLATEDIR;
  336. @allhtml = sort grep /\.html/, @all;
  337. @alldir = ();
  338. for (@all) {
  339. if ( -d "${LedgerSMB::Sysconfig::templates}/$_" ) {
  340. push @alldir, $_;
  341. }
  342. }
  343. @allhtml = reverse grep !/Default/, @allhtml;
  344. push @allhtml, 'Default';
  345. @allhtml = reverse @allhtml;
  346. foreach $item ( sort @alldir ) {
  347. if ( $item eq $myconfig->{templates} ) {
  348. $usetemplates .=
  349. qq|<option selected value="$item">$item</option>\n|;
  350. }
  351. else {
  352. $usetemplates .= qq|<option value="$item">$item</option>\n|;
  353. }
  354. }
  355. $lastitem = $allhtml[0];
  356. $lastitem =~ s/-.*//g;
  357. $mastertemplates = qq|<option value="$lastitem">$lastitem</option>\n|;
  358. foreach $item (@allhtml) {
  359. $item =~ s/-.*//g;
  360. if ( $item ne $lastitem ) {
  361. $mastertemplates .= qq|<option value="$item">$item</option>\n|;
  362. $lastitem = $item;
  363. }
  364. }
  365. opendir CSS, "css/.";
  366. @all = grep /.*\.css$/, readdir CSS;
  367. closedir CSS;
  368. foreach $item (@all) {
  369. if ( $item eq $myconfig->{stylesheet} ) {
  370. $selectstylesheet .=
  371. qq|<option selected value="$item">$item</option>\n|;
  372. }
  373. else {
  374. $selectstylesheet .= qq|<option value="$item">$item</option>\n|;
  375. }
  376. }
  377. $selectstylesheet .= "<option></option>\n";
  378. if ( %{LedgerSMB::Sysconfig::printer} && ${LedgerSMB::Sysconfig::latex} ) {
  379. $selectprinter = "<option></option>\n";
  380. foreach $item ( sort keys %{LedgerSMB::Sysconfig::printer} ) {
  381. if ( $myconfig->{printer} eq $item ) {
  382. $selectprinter .=
  383. qq|<option value="$item" selected>$item</option>\n|;
  384. }
  385. else {
  386. $selectprinter .= qq|<option value="$item">$item</option>\n|;
  387. }
  388. }
  389. $printer = qq|
  390. <tr>
  391. <th align="right">| . $locale->text('Printer') . qq|</th>
  392. <td><select name="printer">$selectprinter</select></td>
  393. </tr>
  394. |;
  395. }
  396. $user = $form->{login};
  397. $form->{login} = "admin";
  398. $form->header;
  399. $form->{login} = $user;
  400. print qq|
  401. <body class="admin">
  402. <form method="post" action="admin.pl">
  403. <table width="100%">
  404. <tr class="listheading"><th colspan="2">$form->{title}</th></tr>
  405. <tr size="5"></tr>
  406. <tr valign="top">
  407. <td>
  408. <table>
  409. <tr>
  410. <th align="right">| . $locale->text('Login') . qq|</th>
  411. <td><input name="login" value="$myconfig->{login}" /></td>
  412. </tr>
  413. <tr>
  414. <th align="right">| . $locale->text('Password') . qq|</th>
  415. <td><input type="password" name="new_password" size="8" value="$myconfig->{password}" /></td>
  416. </tr>
  417. <tr>
  418. <th align="right">| . $locale->text('Name') . qq|</th>
  419. <td><input name="name" size="15" value="$myconfig->{name}" /></td>
  420. </tr>
  421. <tr>
  422. <th align="right">| . $locale->text('E-mail') . qq|</th>
  423. <td><input name="email" size="30" value="$myconfig->{email}" /></td>
  424. </tr>
  425. <tr valign="top">
  426. <th align="right">| . $locale->text('Signature') . qq|</th>
  427. <td><textarea name="signature" rows="3" cols="35">$myconfig->{signature}</textarea></td>
  428. </tr>
  429. <tr>
  430. <th align="right">| . $locale->text('Phone') . qq|</th>
  431. <td><input name="tel" size="14" value="$myconfig->{tel}" /></td>
  432. </tr>
  433. <tr>
  434. <th align="right">| . $locale->text('Fax') . qq|</th>
  435. <td><input name="fax" size="14" value="$myconfig->{fax}" /></td>
  436. </tr>
  437. <tr>
  438. <th align="right">| . $locale->text('Company') . qq|</th>
  439. <td><input name="company" size="35" value="$myconfig->{company}" /></td>
  440. </tr>
  441. <tr valign="top">
  442. <th align="right">| . $locale->text('Address') . qq|</th>
  443. <td><textarea name="address" rows="4" cols="35">$myconfig->{address}</textarea></td>
  444. </tr>
  445. </table>
  446. </td>
  447. <td>
  448. <table>
  449. <tr>
  450. <th align="right">| . $locale->text('Date Format') . qq|</th>
  451. <td><select name="dateformat">$dateformat</select></td>
  452. </tr>
  453. <tr>
  454. <th align="right">| . $locale->text('Number Format') . qq|</th>
  455. <td><select name="numberformat">$numberformat</select></td>
  456. </tr>
  457. <tr>
  458. <th align="right">| . $locale->text('Dropdown Limit') . qq|</th>
  459. <td><input name="vclimit" value="$myconfig->{vclimit}" /></td>
  460. </tr>
  461. <tr>
  462. <th align="right">| . $locale->text('Menu Width') . qq|</th>
  463. <td><input name="menuwidth" value="$myconfig->{menuwidth}" /></td>
  464. </tr>
  465. <tr>
  466. <th align="right">| . $locale->text('Language') . qq|</th>
  467. <td><select name="countrycode">$countrycodes</select></td>
  468. </tr>
  469. <tr>
  470. <th align="right">| . $locale->text('Session Timeout') . qq|</th>
  471. <td><input name="newtimeout" value="$myconfig->{timeout}" /></td>
  472. </tr>
  473. <tr>
  474. <th align="right">| . $locale->text('Stylesheet') . qq|</th>
  475. <td><select name="userstylesheet">$selectstylesheet</select></td>
  476. </tr>
  477. $printer
  478. <tr>
  479. <th align="right">| . $locale->text('Use Templates') . qq|</th>
  480. <td><select name="usetemplates">$usetemplates</select></td>
  481. </tr>
  482. <tr>
  483. <th align="right">| . $locale->text('New Templates') . qq|</th>
  484. <td><input name="newtemplates" /></td>
  485. </tr>
  486. <tr>
  487. <th align="right">| . $locale->text('Setup Templates') . qq|</th>
  488. <td><select name="mastertemplates">$mastertemplates</select></td>
  489. </tr>
  490. </table>
  491. <input type="hidden" name="templates" value="$myconfig->{templates}" />
  492. </td>
  493. </tr>
  494. <tr class="listheading">
  495. <th colspan="2">| . $locale->text('Database') . qq|</th>
  496. </tr>|;
  497. # list section for database drivers
  498. foreach $item ( LedgerSMB::User->dbdrivers ) {
  499. print qq|
  500. <tr>
  501. <td colspan="2">
  502. <table>
  503. <tr>|;
  504. $checked = "checked";
  505. if ( $myconfig->{dbdriver} eq $item ) {
  506. for (qw(dbhost dbport dbuser dbpasswd dbname sid)) {
  507. $form->{"${item}_$_"} = $myconfig->{$_};
  508. }
  509. $checked = "checked";
  510. }
  511. print qq|
  512. <th align="right">| . $locale->text('Driver') . qq|</th>
  513. <td><input name="dbdriver" type="radio" class="radio" value="$item" $checked />&nbsp;$item</td>
  514. <th align="right">| . $locale->text('Host') . qq|</th>
  515. <td><input name="${item}_dbhost" size="30" value="$form->{"${item}_dbhost"}" /></td>
  516. </tr>
  517. <tr>|;
  518. print qq|
  519. <th align="right">| . $locale->text('Dataset') . qq|</th>