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