summaryrefslogtreecommitdiff
path: root/bin/login.pl
blob: a8e47a0ec370241094d560958f2052eb0a263230 (plain)
  1. #=====================================================================
  2. # LedgerSMB Small Medium Business Accounting
  3. # http://www.ledgersmb.org/
  4. #
  5. # Copyright (C) 2006
  6. # This work contains copyrighted information from a number of sources all used
  7. # with permission.
  8. #
  9. # This file contains source code included with or based on SQL-Ledger which
  10. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  11. # under the GNU General Public License version 2 or, at your option, any later
  12. # version. For a full list including contact information of contributors,
  13. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  14. #
  15. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  16. # Copyright (c) 2000
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. #
  22. # This program is free software; you can redistribute it and/or modify
  23. # it under the terms of the GNU General Public License as published by
  24. # the Free Software Foundation; either version 2 of the License, or
  25. # (at your option) any later version.
  26. #
  27. # This program is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU General Public License for more details.
  31. # You should have received a copy of the GNU General Public License
  32. # along with this program; if not, write to the Free Software
  33. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34. #======================================================================
  35. 1;
  36. use DBI;
  37. use LedgerSMB::User;
  38. use LedgerSMB::Form;
  39. use LedgerSMB::Locale;
  40. use LedgerSMB::Session;
  41. ## will need this later when session_destroy will be used
  42. #use LedgerSMB::Session;
  43. $form = new Form;
  44. # For 1.3, this logic should be in LedgerSMB.pm
  45. if ( $form->{path} ) {
  46. if ( $form->{path} ne 'bin/lynx' ) { $form->{path} = 'bin/mozilla'; }
  47. }
  48. else {
  49. $form->{terminal} = "lynx";
  50. if ( $ENV{HTTP_USER_AGENT} !~ /lynx/i ) {
  51. $form->{terminal} = "mozilla";
  52. }
  53. $form->{path} = "bin/$form->{terminal}";
  54. }
  55. $locale = LedgerSMB::Locale->get_handle( ${LedgerSMB::Sysconfig::language} )
  56. or $form->error( __FILE__ . ':' . __LINE__ . ": Locale not loaded: $!\n" );
  57. $locale->encoding('UTF-8');
  58. $form->{charset} = 'UTF-8';
  59. #$form->{charset} = $locale->encoding;
  60. # customization
  61. if ( -f "bin/custom/$form->{script}" ) {
  62. eval { require "bin/custom/$form->{script}"; };
  63. $form->error( __FILE__ . ':' . __LINE__ . ': ' . $@ ) if ($@);
  64. }
  65. # window title bar, user info
  66. $form->{titlebar} =
  67. "LedgerSMB " . $locale->text('Version') . " $form->{version}";
  68. if ( $form->{action} ) {
  69. $form->{titlebar} .= " - $myconfig{name} - $myconfig{dbname}";
  70. &{ $form->{action} };
  71. }
  72. else {
  73. &login_screen;
  74. }
  75. 1;
  76. sub login_screen {
  77. $form->{stylesheet} = "ledger-smb.css";
  78. $form->{favicon} = "favicon.ico";
  79. $form->{endsession} = 1;
  80. if ( $form->{login} ) {
  81. $sf = q|function sf() { document.login.password.focus(); }|;
  82. }
  83. else {
  84. $sf = q|function sf() { document.login.login.focus(); }|;
  85. }
  86. my $headeradd = qq|
  87. <script language="JavaScript" type="text/javascript">
  88. <!--
  89. var agt = navigator.userAgent.toLowerCase();
  90. var is_major = parseInt(navigator.appVersion);
  91. var is_nav = ((agt.indexOf('mozilla') != -1) && (agt.indexOf('spoofer') == -1)
  92. && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera') == -1)
  93. && (agt.indexOf('webtv') == -1));
  94. var is_nav4lo = (is_nav && (is_major <= 4));
  95. function jsp() {
  96. if (is_nav4lo){
  97. document.login.js.value = "0";
  98. } else {
  99. document.login.js.value = "1";
  100. }
  101. }
  102. $sf
  103. // End -->
  104. </script>|;
  105. $form->header( 1, $headeradd );
  106. print qq|
  107. <body class="login" onload="jsp(); sf();">
  108. <br /><br />
  109. <center>
  110. <table class="login" border="3" cellpadding="20">
  111. <tr>
  112. <td class="login" align="center">
  113. <a href="http://www.ledgersmb.org/" target="_top"><img src="ledger-smb.png" width="200" heith="100" border="0" alt="LedgerSMB Logo" /></a>
  114. <h1 class="login" align="center">|
  115. . $locale->text('Version')
  116. . qq| $form->{version}</h1>
  117. <p>
  118. <form method="post" action="login.pl" name="login">
  119. <table width="100%">
  120. <tr>
  121. <td align="center">
  122. <table>
  123. <tr>
  124. <th align="right">| . $locale->text('Name') . qq|</th>
  125. <td><input class="login" name="login" size="30" value="$form->{login}" /></td>
  126. </tr>
  127. <tr>
  128. <th align="right">| . $locale->text('Password') . qq|</th>
  129. <td><input class="login" type="password" name="password" size="30" /></td>
  130. </tr>
  131. </table>
  132. <br />
  133. </td>
  134. </tr>
  135. </table>
  136. <input type="hidden" name="path" value="$form->{path}" />
  137. <input type="hidden" name="js" value="$form->{js}" />
  138. <button type="submit" name="action" value="login">|
  139. . $locale->text('Login')
  140. . qq|</button>
  141. </form>
  142. </p>
  143. </td>
  144. </tr>
  145. </table>
  146. <p><a href="admin.pl"
  147. >| . $locale->text("Administrative login") . qq|</a></p>
  148. </center>
  149. </body>
  150. </html>|;
  151. }
  152. sub selectdataset {
  153. my ($login) = @_;
  154. if ( -f "css/ledger-smb.css" ) {
  155. $form->{stylesheet} = "ledger-smb.css";
  156. }
  157. $form->header(1);
  158. print qq|
  159. <body class="login" onload="document.forms[0].password.focus()">
  160. <br /><br />
  161. <center>
  162. <table class="login" border="3" cellpadding="20">
  163. <tr>
  164. <td class="login" align="center">
  165. <a href="http://www.ledgersmb.org/" target="_top"><img src="ledger-smb.png" width="100" heith="100" border="0" alt="LedgerSMB Logo" /></a>
  166. <h1 class="login" align="center">|
  167. . $locale->text('Version')
  168. . qq| $form->{version}</h1>
  169. <p>
  170. <form method="post" action="$form->{script}">
  171. <input type="hidden" name="beenthere" value="1" />
  172. <input type="hidden" name="js" value="$form->{js}" />
  173. <input type="hidden" name="path" value="$form->{path}" />
  174. <table width="100%">
  175. <tr>
  176. <td align="center">
  177. <table>
  178. <tr>
  179. <th align="right">| . $locale->text('Name') . qq|</th>
  180. <td>$form->{login}</td>
  181. </tr>
  182. <tr>
  183. <th align="right">| . $locale->text('Password') . qq|</th>
  184. <td><input class="login" type="password" name="password" size="30" value="$form->{password}" /></td>
  185. </tr>
  186. <tr>
  187. <th align="right">| . $locale->text('Company') . qq|</th>
  188. <td>|;
  189. $checked = "checked";
  190. foreach $login ( sort { $login{$a} cmp $login{$b} } keys %{$login} ) {
  191. print
  192. qq| <br /><input class="login" type="radio" name="login" value="$login" $checked>$login{$login} |;
  193. $checked = "";
  194. }
  195. print qq|
  196. </td>
  197. </tr>
  198. </table>
  199. <br />
  200. <button type="submit" name="action" value="login">|
  201. . $locale->text('Login')
  202. . qq|</button>
  203. </td>
  204. </tr>
  205. </table>
  206. </form>
  207. </td>
  208. </tr>
  209. </table>
  210. </center>
  211. </body>
  212. </html>|;
  213. }
  214. sub login {
  215. $form->{stylesheet} = "ledger-smb.css";
  216. $form->{favicon} = "favicon.ico";
  217. $form->error( __FILE__ . ':' . __LINE__ . ': '
  218. . $locale->text('You did not enter a name!') )
  219. unless ( $form->{login} );
  220. #this needs to be done via db
  221. #if (! $form->{beenthere}) {
  222. # open(FH, '<', "${LedgerSMB::Sysconfig::memberfile}") or $form->error(__FILE__.':'.__LINE__.": $memberfile : $!");
  223. # @a = <FH>;
  224. # close(FH);
  225. #
  226. # foreach $item (@a) {
  227. #
  228. # if ($item =~ /^\[(.*?)\]/) {
  229. # $login = $1;
  230. # $found = 1;
  231. # }
  232. #
  233. # if ($item =~ /^company=/) {
  234. # if ($login =~ /$form->{login}\@/ && $found) {
  235. # ($null, $name) = split /=/, $item, 2;
  236. # $login{$login} = $name;
  237. # }
  238. # $found = 0;
  239. # }
  240. # }
  241. #
  242. # if (keys %login > 1) {
  243. # &selectdataset(\%login);
  244. # exit;
  245. # }
  246. #}
  247. if ( !${LedgerSMB::Sysconfig::GLOBALDBH} ) {
  248. $locale->text("No GlobalDBH Configured or Could not Connect");
  249. }
  250. $user = LedgerSMB::User->new( $form->{login} );
  251. # if we get an error back, bale out
  252. if ( ( $errno = $user->login( \%$form ) ) <= -1 ) {
  253. $errno *= -1;
  254. $err[1] = $locale->text('Access Denied!');
  255. $err[2] = $locale->text('Incorrect Dataset version!');
  256. $err[3] = $locale->text('Dataset is newer than version!');
  257. if ( $errno == 4 ) {
  258. # upgrade dataset and log in again
  259. #locking needs to be done via db function
  260. #open FH, '>', "${LedgerSMB::Sysconfig::userspath}/nologin" or $form->error($!);
  261. for (qw(dbname dbhost dbport dbdriver dbuser dbpasswd)) {
  262. $form->{$_} = $user->{$_};
  263. }
  264. $form->{dbupdate} = "db$user->{dbname}";
  265. $form->{ $form->{dbupdate} } = 1;
  266. $form->header;
  267. print qq|<body>|;
  268. print $locale->text( 'Upgrading to Version [_1] ...',
  269. $form->{version} );
  270. # required for Oracle
  271. $form->{dbdefault} = $sid;
  272. $user->dbupdate( \%$form );
  273. # remove lock
  274. #unlink "${LedgerSMB::Sysconfig::userspath}/nologin";
  275. print $locale->text('done');
  276. print
  277. "<p><a href=\"menu.pl?login=$form->{login}&amp;sessionid=$form->{sessionid}&amp;path=$form->{path}&amp;action=display&amp;main=company_logo&amp;js=$form->{js}>\">"
  278. . $locale->text('Continue') . "</a>";
  279. print qq|</body>|;
  280. exit;
  281. }
  282. $form->error( __FILE__ . ':' . __LINE__ . ': ' . $err[$errno] );
  283. }
  284. # made it this far, setup callback for the menu
  285. $form->{callback} = "menu.pl?action=display&password=$form->{password}";
  286. for (qw(login path js)) { $form->{callback} .= "&$_=$form->{$_}" }
  287. # check for recurring transactions
  288. if ( $user->{acs} !~ /Recurring Transactions/ ) {
  289. if ( $user->check_recurring( \%$form ) ) {
  290. $form->{callback} .= "&main=recurring_transactions";
  291. }
  292. else {
  293. $form->{callback} .= "&main=company_logo";
  294. }
  295. }
  296. else {
  297. if ( $user->{role} eq 'user' ) {
  298. $form->{callback} .= "&main=company_logo";
  299. }
  300. else {
  301. if ( $user->check_recurring( \%$form ) ) {
  302. $form->{callback} .= "&main=recurring_transactions";
  303. }
  304. else {
  305. $form->{callback} .= "&main=company_logo";
  306. }
  307. }
  308. }
  309. Session::session_create($form);
  310. $form->redirect;
  311. }
  312. sub logout {
  313. $form->{callback} = "";
  314. $form->{endsession} = 1;
  315. Session::session_destroy($form);
  316. $form->redirect;
  317. }