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