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