summaryrefslogtreecommitdiff
path: root/bin/mozilla/login.pl
blob: 8697fe33da75512dd51fb86f56b64249f0cc6b20 (plain)
  1. #=====================================================================
  2. # LedgerSMB Small Medium Business Accounting
  3. # Copyright (C) 2006
  4. # This work contains copyrighted information from a number of sources all used
  5. # with permission.
  6. #
  7. # This file contains source code included with or based on SQL-Ledger which
  8. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  9. # under the GNU General Public License version 2 or, at your option, any later
  10. # version. For a full list including contact information of contributors,
  11. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  12. #
  13. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  14. # Copyright (c) 2000
  15. #
  16. # Author: DWS Systems Inc.
  17. # Web: http://www.sql-ledger.org
  18. #
  19. #
  20. # This program is free software; you can redistribute it and/or modify
  21. # it under the terms of the GNU General Public License as published by
  22. # the Free Software Foundation; either version 2 of the License, or
  23. # (at your option) any later version.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU General Public License for more details.
  29. # You should have received a copy of the GNU General Public License
  30. # along with this program; if not, write to the Free Software
  31. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. #======================================================================
  33. 1;
  34. use DBI;
  35. use LedgerSMB::User;
  36. use LedgerSMB::Form;
  37. ## will need this later when session_destroy will be used
  38. #use LedgerSMB::Session;
  39. $form = new Form;
  40. $locale = new Locale $language, "login";
  41. $form->{charset} = $locale->{charset};
  42. # customization
  43. if (-f "$form->{path}/custom_$form->{script}") {
  44. eval { require "$form->{path}/custom_$form->{script}"; };
  45. $form->error($@) if ($@);
  46. }
  47. # per login customization
  48. if (-f "$form->{path}/$form->{login}_$form->{script}") {
  49. eval { require "$form->{path}/$form->{login}_$form->{script}"; };
  50. $form->error($@) if ($@);
  51. }
  52. # window title bar, user info
  53. $form->{titlebar} = "LedgerSMB ".$locale->text('Version'). " $form->{version}";
  54. if ($form->{action}) {
  55. $form->{titlebar} .= " - $myconfig{name} - $myconfig{dbname}";
  56. &{ $locale->findsub($form->{action}) };
  57. } else {
  58. &login_screen;
  59. }
  60. 1;
  61. sub login_screen {
  62. $form->{stylesheet} = "ledger-smb.css";
  63. $form->{favicon} = "favicon.ico";
  64. $form->{endsession} = 1;
  65. if ($form->{login}) {
  66. $sf = q|function sf() { document.login.password.focus(); }|;
  67. } else {
  68. $sf = q|function sf() { document.login.login.focus(); }|;
  69. }
  70. my $headeradd = qq|
  71. <script language="JavaScript" type="text/javascript">
  72. <!--
  73. var agt = navigator.userAgent.toLowerCase();
  74. var is_major = parseInt(navigator.appVersion);
  75. var is_nav = ((agt.indexOf('mozilla') != -1) && (agt.indexOf('spoofer') == -1)
  76. && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera') == -1)
  77. && (agt.indexOf('webtv') == -1));
  78. var is_nav4lo = (is_nav && (is_major <= 4));
  79. function jsp() {
  80. if (is_nav4lo){
  81. document.login.js.value = "0";
  82. } else {
  83. document.login.js.value = "1";
  84. }
  85. }
  86. $sf
  87. // End -->
  88. </script>|;
  89. $form->header(1, $headeradd);
  90. print qq|
  91. <body class="login" onload="jsp(); sf();">
  92. <br /><br />
  93. <center>
  94. <table class="login" border="3" cellpadding="20">
  95. <tr>
  96. <td class="login" align="center">
  97. <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>
  98. <h1 class="login" align="center">|.$locale->text('Version').qq| $form->{version}</h1>
  99. <p>
  100. <form method="post" action="$form->{script}" name="login">
  101. <table width="100%">
  102. <tr>
  103. <td align="center">
  104. <table>
  105. <tr>
  106. <th align="right">|.$locale->text('Name').qq|</th>
  107. <td><input class="login" name="login" size="30" value="$form->{login}" /></td>
  108. </tr>
  109. <tr>
  110. <th align="right">|.$locale->text('Password').qq|</th>
  111. <td><input class="login" type="password" name="password" size="30" /></td>
  112. </tr>
  113. </table>
  114. <br />
  115. </td>
  116. </tr>
  117. </table>
  118. <input type="hidden" name="path" value="$form->{path}" />
  119. <input type="hidden" name="js" value="$form->{js}" />
  120. <input type="submit" name="action" value="|.$locale->text('Login').qq|" />
  121. </form>
  122. </p>
  123. </td>
  124. </tr>
  125. </table>
  126. </center>
  127. </body>
  128. </html>|;
  129. }
  130. sub selectdataset {
  131. my ($login) = @_;
  132. if (-f "css/ledger-smb.css") {
  133. $form->{stylesheet} = "ledger-smb.css";
  134. }
  135. $form->header(1);
  136. print qq|
  137. <body class="login" onload="document.forms[0].password.focus()" />
  138. <br /><br />
  139. <center>
  140. <table class="login" border="3" cellpadding="20">
  141. <tr>
  142. <td class="login" align="center">
  143. <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>
  144. <h1 class="login" align="center">|.$locale->text('Version').qq| $form->{version}</h1>
  145. <p>
  146. <form method="post" action="$form->{script}">
  147. <input type="hidden" name="beenthere" value="1" />
  148. <input type="hidden" name="js" value="$form->{js}" />
  149. <input type="hidden" name="path" value="$form->{path}" />
  150. <table width="100%">
  151. <tr>
  152. <td align="center">
  153. <table>
  154. <tr>
  155. <th align="right">|.$locale->text('Name').qq|</th>
  156. <td>$form->{login}</td>
  157. </tr>
  158. <tr>
  159. <th align="right">|.$locale->text('Password').qq|</th>
  160. <td><input class="login" type="password" name="password" size="30" value="$form->{password}" /></td>
  161. </tr>
  162. <tr>
  163. <th align="right">|.$locale->text('Company').qq|</th>
  164. <td>|;
  165. $checked = "checked";
  166. foreach $login (sort { $login{$a} cmp $login{$b} } keys %{ $login }) {
  167. print qq| <br /><input class="login" type="radio" name="login" value="$login" $checked>$login{$login} |;
  168. $checked = "";
  169. }
  170. print qq|
  171. </td>
  172. </tr>
  173. </table>
  174. <br />
  175. <input type="submit" name="action" value="|.$locale->text('Login').qq|">
  176. </td>
  177. </tr>
  178. </table>
  179. </form>
  180. </td>
  181. </tr>
  182. </table>
  183. </center>
  184. </body>
  185. </html>|;
  186. }
  187. sub login {
  188. $form->{stylesheet} = "ledger-smb.css";
  189. $form->{favicon} = "favicon.ico";
  190. $form->error($locale->text('You did not enter a name!')) unless ($form->{login});
  191. if (! $form->{beenthere}) {
  192. open(FH, "$memberfile") or $form->error("$memberfile : $!");
  193. @a = <FH>;
  194. close(FH);
  195. foreach $item (@a) {
  196. if ($item =~ /^\[(.*?)\]/) {
  197. $login = $1;
  198. $found = 1;
  199. }
  200. if ($item =~ /^company=/) {
  201. if ($login =~ /$form->{login}\@/ && $found) {
  202. ($null, $name) = split /=/, $item, 2;
  203. $login{$login} = $name;
  204. }
  205. $found = 0;
  206. }
  207. }
  208. if (keys %login > 1) {
  209. &selectdataset(\%login);
  210. exit;
  211. }
  212. }
  213. $user = new User $memberfile, $form->{login};
  214. # if we get an error back, bale out
  215. if (($errno = $user->login(\%$form, $userspath)) <= -1) {
  216. $errno *= -1;
  217. $err[1] = $locale->text('Access Denied!');
  218. $err[2] = $locale->text('Incorrect Dataset version!');
  219. $err[3] = $locale->text('Dataset is newer than version!');
  220. if ($errno == 4) {
  221. # upgrade dataset and log in again
  222. open FH, ">$userspath/nologin" or $form->error($!);
  223. for (qw(dbname dbhost dbport dbdriver dbuser dbpasswd)) { $form->{$_} = $user->{$_} }
  224. $form->{dbpasswd} = unpack 'u', $form->{dbpasswd};
  225. $form->{dbupdate} = "db$user->{dbname}";
  226. $form->{$form->{dbupdate}} = 1;
  227. $form->header;
  228. print qq|<body>|;
  229. print $locale->text('Upgrading to Version')." $form->{version} ... ";
  230. # required for Oracle
  231. $form->{dbdefault} = $sid;
  232. $user->dbupdate(\%$form);
  233. # remove lock file
  234. unlink "$userspath/nologin";
  235. print $locale->text('done');
  236. 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>";
  237. print qq|</body>|;
  238. exit;
  239. }
  240. $form->error($err[$errno]);
  241. }
  242. # made it this far, setup callback for the menu
  243. $form->{callback} = "menu.pl?action=display&password=$form->{password}";
  244. for (qw(login path js)) { $form->{callback} .= "&$_=$form->{$_}" }
  245. # check for recurring transactions
  246. if ($user->{acs} !~ /Recurring Transactions/) {
  247. if ($user->check_recurring(\%$form)) {
  248. $form->{callback} .= "&main=recurring_transactions";
  249. } else {
  250. $form->{callback} .= "&main=company_logo";
  251. }
  252. } else {
  253. if ($user->{role} eq 'user') {
  254. $form->{callback} .= "&main=company_logo";
  255. } else {
  256. if ($user->check_recurring(\%$form)) {
  257. $form->{callback} .= "&main=recurring_transactions";
  258. } else {
  259. $form->{callback} .= "&main=company_logo";
  260. }
  261. }
  262. }
  263. $form->redirect;
  264. }
  265. sub logout {
  266. $form->{callback} = "$form->{script}?path=$form->{path}&login=$form->{login}";
  267. $form->{endsession} = 1;
  268. #delete the cookie in the browser manually (can't use session_destroy here unfortunately)
  269. print qq|Set-Cookie: LedgerSMB=; path=/;\n|;
  270. $form->redirect;
  271. }