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