summaryrefslogtreecommitdiff
path: root/bin/login.pl
blob: b9cfd65c6fb14c422ff1e6016cfaa92b9b84041e (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. $locale = LedgerSMB::Locale->get_handle( ${LedgerSMB::Sysconfig::language} )
  45. or $form->error( __FILE__ . ':' . __LINE__ . ": Locale not loaded: $!\n" );
  46. $locale->encoding('UTF-8');
  47. $form->{charset} = 'UTF-8';
  48. #$form->{charset} = $locale->encoding;
  49. # customization
  50. if ( -f "bin/custom/$form->{script}" ) {
  51. eval { require "bin/custom/$form->{script}"; };
  52. $form->error( __FILE__ . ':' . __LINE__ . ': ' . $@ ) if ($@);
  53. }
  54. # per login customization
  55. if ( -f "bin/custom/$form->{login}_$form->{script}" ) {
  56. eval { require "bin/custom/$form->{login}_$form->{script}"; };
  57. $form->error( __FILE__ . ':' . __LINE__ . ': ' . $@ ) if ($@);
  58. }
  59. # window title bar, user info
  60. $form->{titlebar} =
  61. "LedgerSMB " . $locale->text('Version') . " $form->{version}";
  62. if ( $form->{action} ) {
  63. $form->{titlebar} .= " - $myconfig{name} - $myconfig{dbname}";
  64. &{ $form->{action} };
  65. }
  66. else {
  67. &login_screen;
  68. }
  69. 1;
  70. sub login_screen {
  71. $form->{stylesheet} = "ledgersmb.css";
  72. $form->{favicon} = "favicon.ico";
  73. $form->{endsession} = 1;
  74. if ( $form->{login} ) {
  75. $sf = q|function sf() { document.login.password.focus(); }|;
  76. }
  77. else {
  78. $sf = q|function sf() { document.login.login.focus(); }|;
  79. }
  80. my $headeradd = qq|
  81. <script language="JavaScript" type="text/javascript">
  82. <!--
  83. var agt = navigator.userAgent.toLowerCase();
  84. var is_major = parseInt(navigator.appVersion);
  85. var is_nav = ((agt.indexOf('mozilla') != -1) && (agt.indexOf('spoofer') == -1)
  86. && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera') == -1)
  87. && (agt.indexOf('webtv') == -1));
  88. var is_nav4lo = (is_nav && (is_major <= 4));
  89. function jsp() {
  90. if (is_nav4lo){
  91. document.login.js.value = "0";
  92. } else {
  93. document.login.js.value = "1";
  94. }
  95. }
  96. $sf
  97. // End -->
  98. </script>|;
  99. $form->header( 1, $headeradd );
  100. print qq|
  101. <body class="login" onload="jsp(); sf();">
  102. <br /><br />
  103. <center>
  104. <table class="login" border="3" cellpadding="20">
  105. <tr>
  106. <td class="login" align="center">
  107. <a href="http://www.ledgersmb.org/" target="_top"><img src="images/ledgersmb.png" width="200" heith="100" border="0" alt="LedgerSMB Logo" /></a>
  108. <h1 class="login" align="center">|
  109. . $locale->text('Version')
  110. . qq| $form->{version}</h1>
  111. <p>
  112. <form method="post" action="$form->{script}" name="login">
  113. <table width="100%">
  114. <tr>
  115. <td align="center">
  116. <table>
  117. <tr>
  118. <th align="right">| . $locale->text('Name') . qq|</th>
  119. <td><input class="login" name="login" size="30" value="$form->{login}" /></td>
  120. </tr>
  121. <tr>
  122. <th align="right">| . $locale->text('Password') . qq|</th>
  123. <td><input class="login" type="password" name="password" size="30" /></td>
  124. </tr>
  125. </table>
  126. <br />
  127. </td>
  128. </tr>
  129. </table>
  130. <input type="hidden" name="path" value="$form->{path}" />
  131. <input type="hidden" name="js" value="$form->{js}" />
  132. <button type="submit" name="action" value="login">|
  133. . $locale->text('Login')
  134. . qq|</button>
  135. </form>
  136. </p>
  137. </td>
  138. </tr>
  139. </table>
  140. <p><a href="admin.pl"
  141. >| . $locale->text("Administrative login") . qq|</a></p>
  142. </center>
  143. </body>
  144. </html>|;
  145. }
  146. sub selectdataset {
  147. my ($login) = @_;
  148. if ( -f "css/ledgersmb.css" ) {
  149. $form->{stylesheet} = "ledgersmb.css";
  150. }
  151. $form->header(1);
  152. print qq|
  153. <body class="login" onload="document.forms[0].password.focus()" />
  154. <br /><br />
  155. <center>
  156. <table class="login" border="3" cellpadding="20">
  157. <tr>
  158. <td class="login" align="center">
  159. <a href="http://www.ledgersmb.org/" target="_top"><img src="images/ledgersmb.png" width="100" heith="100" border="0" alt="LedgerSMB Logo" /></a>
  160. <h1 class="login" align="center">|
  161. . $locale->text('Version')
  162. . qq| $form->{version}</h1>
  163. <p>
  164. <form method="post" action="$form->{script}">
  165. <input type="hidden" name="beenthere" value="1" />
  166. <input type="hidden" name="js" value="$form->{js}" />
  167. <input type="hidden" name="path" value="$form->{path}" />
  168. <table width="100%">
  169. <tr>
  170. <td align="center">
  171. <table>
  172. <tr>
  173. <th align="right">| . $locale->text('Name') . qq|</th>
  174. <td>$form->{login}</td>
  175. </tr>
  176. <tr>
  177. <th align="right">| . $locale->text('Password') . qq|</th>
  178. <td><input class="login" type="password" name="password" size="30" value="$form->{password}" /></td>
  179. </tr>
  180. <tr>
  181. <th align="right">| . $locale->text('Company') . qq|</th>
  182. <td>|;
  183. $checked = "checked";
  184. foreach $login ( sort { $login{$a} cmp $login{$b} } keys %{$login} ) {
  185. print
  186. qq| <br /><input class="login" type="radio" name="login" value="$login" $checked>$login{$login} |;
  187. $checked = "";
  188. }
  189. print qq|
  190. </td>
  191. </tr>
  192. </table>
  193. <br />
  194. <button type="submit" name="action" value="login">|
  195. . $locale->text('Login')
  196. . qq|</button>
  197. </td>
  198. </tr>
  199. </table>
  200. </form>
  201. </td>
  202. </tr>
  203. </table>
  204. </center>
  205. </body>
  206. </html>|;
  207. }
  208. sub login {
  209. $form->{stylesheet} = "ledgersmb.css";
  210. $form->{favicon} = "favicon.ico";
  211. $form->error( __FILE__ . ':' . __LINE__ . ': '
  212. . $locale->text('You did not enter a name!') )
  213. unless ( $form->{login} );
  214. if ( !${LedgerSMB::Sysconfig::GLOBALDBH} ) {
  215. $locale->text("No GlobalDBH Configured or Could not Connect");
  216. }
  217. $user = LedgerSMB::User->new( $form->{login} );
  218. if ( ( $errno = $user->login( \%$form ) ) <= -1 ) {
  219. $errno *= -1;
  220. $err[1] = $locale->text('Access Denied!');
  221. $err[2] = $locale->text('Incorrect Dataset version!');
  222. $err[3] = $locale->text('Dataset is newer than version!');
  223. if ( $errno == 4 ) {
  224. # upgrade dataset and log in again
  225. if ( !$LedgerSMB::Sysconfig::db_autoupdate ) {
  226. $form->error( $locale->text("Dabase Version too Old") );
  227. }
  228. for (qw(dbname dbhost dbport dbdriver dbuser dbpasswd)) {
  229. $form->{$_} = $user->{$_};
  230. }
  231. $form->{dbpasswd} = unpack 'u', $form->{dbpasswd};
  232. $form->{dbupdate} = "db$user->{dbname}";
  233. $form->{ $form->{dbupdate} } = 1;
  234. $form->header;
  235. print qq|<body>|;
  236. print $locale->text( 'Upgrading to Version [_1] ...',
  237. $form->{version} );
  238. $user->dbupdate( \%$form );
  239. # remove lock
  240. #unlink "${LedgerSMB::Sysconfig::userspath}/nologin";
  241. print $locale->text('done');
  242. print
  243. "<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}>\">"
  244. . $locale->text('Continue') . "</a>";
  245. print qq|</body>|;
  246. exit;
  247. }
  248. $form->error( __FILE__ . ':' . __LINE__ . ': ' . $err[$errno] );
  249. }
  250. # made it this far, setup callback for the menu
  251. $form->{callback} = "menu.pl?action=display&password=$form->{password}";
  252. for (qw(login path js)) { $form->{callback} .= "&$_=$form->{$_}" }
  253. # check for recurring transactions
  254. if ( $user->{acs} !~ /Recurring Transactions/ ) {
  255. if ( $user->check_recurring( \%$form ) ) {
  256. $form->{callback} .= "&main=recurring_transactions";
  257. }
  258. else {
  259. $form->{callback} .= "&main=company_logo";
  260. }
  261. }
  262. else {
  263. if ( $user->{role} eq 'user' ) {
  264. $form->{callback} .= "&main=company_logo";
  265. }
  266. else {
  267. if ( $user->check_recurring( \%$form ) ) {
  268. $form->{callback} .= "&main=recurring_transactions";
  269. }
  270. else {
  271. $form->{callback} .= "&main=company_logo";
  272. }
  273. }
  274. }
  275. Session::session_create($form);
  276. $form->redirect;
  277. }
  278. sub logout {
  279. $form->{callback} = "";
  280. $form->{endsession} = 1;
  281. Session::session_destroy($form);
  282. $form->redirect;
  283. }