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