summaryrefslogtreecommitdiff
path: root/bin/mozilla/login.pl
blob: 3a925bea298675a85427da9d56be530be905ff32 (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">Administration login</a></p>
  129. </center>
  130. </body>
  131. </html>|;
  132. }
  133. sub selectdataset {
  134. my ($login) = @_;
  135. if (-f "css/ledger-smb.css") {
  136. $form->{stylesheet} = "ledger-smb.css";
  137. }
  138. $form->header(1);
  139. print qq|
  140. <body class="login" onload="document.forms[0].password.focus()" />
  141. <br /><br />
  142. <center>
  143. <table class="login" border="3" cellpadding="20">
  144. <tr>
  145. <td class="login" align="center">
  146. <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>
  147. <h1 class="login" align="center">|.$locale->text('Version').qq| $form->{version}</h1>
  148. <p>
  149. <form method="post" action="$form->{script}">
  150. <input type="hidden" name="beenthere" value="1" />
  151. <input type="hidden" name="js" value="$form->{js}" />
  152. <input type="hidden" name="path" value="$form->{path}" />
  153. <table width="100%">
  154. <tr>
  155. <td align="center">
  156. <table>
  157. <tr>
  158. <th align="right">|.$locale->text('Name').qq|</th>
  159. <td>$form->{login}</td>
  160. </tr>
  161. <tr>
  162. <th align="right">|.$locale->text('Password').qq|</th>
  163. <td><input class="login" type="password" name="password" size="30" value="$form->{password}" /></td>
  164. </tr>
  165. <tr>
  166. <th align="right">|.$locale->text('Company').qq|</th>
  167. <td>|;
  168. $checked = "checked";
  169. foreach $login (sort { $login{$a} cmp $login{$b} } keys %{ $login }) {
  170. print qq| <br /><input class="login" type="radio" name="login" value="$login" $checked>$login{$login} |;
  171. $checked = "";
  172. }
  173. print qq|
  174. </td>
  175. </tr>
  176. </table>
  177. <br />
  178. <input type="submit" name="action" value="|.$locale->text('Login').qq|">
  179. </td>
  180. </tr>
  181. </table>
  182. </form>
  183. </td>
  184. </tr>
  185. </table>
  186. </center>
  187. </body>
  188. </html>|;
  189. }
  190. sub login {
  191. $form->{stylesheet} = "ledger-smb.css";
  192. $form->{favicon} = "favicon.ico";
  193. $form->error($locale->text('You did not enter a name!')) unless ($form->{login});
  194. if (! $form->{beenthere}) {
  195. open(FH, "$memberfile") or $form->error("$memberfile : $!");
  196. @a = <FH>;
  197. close(FH);
  198. foreach $item (@a) {
  199. if ($item =~ /^\[(.*?)\]/) {
  200. $login = $1;
  201. $found = 1;
  202. }
  203. if ($item =~ /^company=/) {
  204. if ($login =~ /$form->{login}\@/ && $found) {
  205. ($null, $name) = split /=/, $item, 2;
  206. $login{$login} = $name;
  207. }
  208. $found = 0;
  209. }
  210. }
  211. if (keys %login > 1) {
  212. &selectdataset(\%login);
  213. exit;
  214. }
  215. }
  216. $user = new User $memberfile, $form->{login};
  217. # if we get an error back, bale out
  218. if (($errno = $user->login(\%$form, $userspath)) <= -1) {
  219. $errno *= -1;
  220. $err[1] = $locale->text('Access Denied!');
  221. $err[2] = $locale->text('Incorrect Dataset version!');
  222. $err[3] = $locale->text('Dataset is newer than version!');
  223. if ($errno == 4) {
  224. # upgrade dataset and log in again
  225. open FH, ">$userspath/nologin" or $form->error($!);
  226. for (qw(dbname dbhost dbport dbdriver dbuser dbpasswd)) { $form->{$_} = $user->{$_} }
  227. $form->{dbpasswd} = unpack 'u', $form->{dbpasswd};
  228. $form->{dbupdate} = "db$user->{dbname}";
  229. $form->{$form->{dbupdate}} = 1;
  230. $form->header;
  231. print qq|<body>|;
  232. print $locale->text('Upgrading to Version')." $form->{version} ... ";
  233. # required for Oracle
  234. $form->{dbdefault} = $sid;
  235. $user->dbupdate(\%$form);
  236. # remove lock file
  237. unlink "$userspath/nologin";
  238. print $locale->text('done');
  239. 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>";
  240. print qq|</body>|;
  241. exit;
  242. }
  243. $form->error($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. } else {
  253. $form->{callback} .= "&main=company_logo";
  254. }
  255. } else {
  256. if ($user->{role} eq 'user') {
  257. $form->{callback} .= "&main=company_logo";
  258. } else {
  259. if ($user->check_recurring(\%$form)) {
  260. $form->{callback} .= "&main=recurring_transactions";
  261. } else {
  262. $form->{callback} .= "&main=company_logo";
  263. }
  264. }
  265. }
  266. $form->redirect;
  267. }
  268. sub logout {
  269. $form->{callback} = "$form->{script}?path=$form->{path}&login=$form->{login}";
  270. $form->{endsession} = 1;
  271. #delete the cookie in the browser manually (can't use session_destroy here unfortunately)
  272. print qq|Set-Cookie: LedgerSMB=; path=/;\n|;
  273. $form->redirect;
  274. }