summaryrefslogtreecommitdiff
path: root/bin/login.pl
blob: a1f225d814508f331d6edf1d63525b37457eeb06 (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} = "ledger-smb.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="ledger-smb.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/ledger-smb.css") {
  142. $form->{stylesheet} = "ledger-smb.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="ledger-smb.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} = "ledger-smb.css";
  198. $form->{favicon} = "favicon.ico";
  199. $form->error(__FILE__.':'.__LINE__.': '.$locale->text('You did not enter a name!')) unless ($form->{login});
  200. #this needs to be done via db
  201. #if (! $form->{beenthere}) {
  202. # open(FH, "${LedgerSMB::Sysconfig::memberfile}") or $form->error(__FILE__.':'.__LINE__.": $memberfile : $!");
  203. # @a = <FH>;
  204. # close(FH);
  205. #
  206. # foreach $item (@a) {
  207. #
  208. # if ($item =~ /^\[(.*?)\]/) {
  209. # $login = $1;
  210. # $found = 1;
  211. # }
  212. #
  213. # if ($item =~ /^company=/) {
  214. # if ($login =~ /$form->{login}\@/ && $found) {
  215. # ($null, $name) = split /=/, $item, 2;
  216. # $login{$login} = $name;
  217. # }
  218. # $found = 0;
  219. # }
  220. # }
  221. #
  222. # if (keys %login > 1) {
  223. # &selectdataset(\%login);
  224. # exit;
  225. # }
  226. #}
  227. $user = LedgerSMB::User->new($form->{login});
  228. # if we get an error back, bale out
  229. if (($errno = $user->login(\%$form)) <= -1) {
  230. $errno *= -1;
  231. $err[1] = $locale->text('Access Denied!');
  232. $err[2] = $locale->text('Incorrect Dataset version!');
  233. $err[3] = $locale->text('Dataset is newer than version!');
  234. if ($errno == 4) {
  235. # upgrade dataset and log in again
  236. #locking needs to be done via db function
  237. #open FH, ">${LedgerSMB::Sysconfig::userspath}/nologin" or $form->error($!);
  238. for (qw(dbname dbhost dbport dbdriver dbuser dbpasswd)) { $form->{$_} = $user->{$_} }
  239. $form->{dbpasswd} = unpack 'u', $form->{dbpasswd};
  240. $form->{dbupdate} = "db$user->{dbname}";
  241. $form->{$form->{dbupdate}} = 1;
  242. $form->header;
  243. print qq|<body>|;
  244. print $locale->text('Upgrading to Version [_1] ...', $form->{version});
  245. # required for Oracle
  246. $form->{dbdefault} = $sid;
  247. $user->dbupdate(\%$form);
  248. # remove lock
  249. #unlink "${LedgerSMB::Sysconfig::userspath}/nologin";
  250. print $locale->text('done');
  251. 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>";
  252. print qq|</body>|;
  253. exit;
  254. }
  255. $form->error(__FILE__.':'.__LINE__.': '.$err[$errno]);
  256. }
  257. # made it this far, setup callback for the menu
  258. $form->{callback} = "menu.pl?action=display&password=$form->{password}";
  259. for (qw(login path js)) { $form->{callback} .= "&$_=$form->{$_}" }
  260. # check for recurring transactions
  261. if ($user->{acs} !~ /Recurring Transactions/) {
  262. if ($user->check_recurring(\%$form)) {
  263. $form->{callback} .= "&main=recurring_transactions";
  264. } else {
  265. $form->{callback} .= "&main=company_logo";
  266. }
  267. } else {
  268. if ($user->{role} eq 'user') {
  269. $form->{callback} .= "&main=company_logo";
  270. } else {
  271. if ($user->check_recurring(\%$form)) {
  272. $form->{callback} .= "&main=recurring_transactions";
  273. } else {
  274. $form->{callback} .= "&main=company_logo";
  275. }
  276. }
  277. }
  278. $form->redirect;
  279. }
  280. sub logout {
  281. $form->{callback} = "$form->{script}?path=$form->{path}&login=$form->{login}";
  282. $form->{endsession} = 1;
  283. Session::session_destroy($form);
  284. $form->redirect;
  285. }