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