summaryrefslogtreecommitdiff
path: root/LedgerSMB/User.pm
blob: d0ccfc61a2efbd9e6bdf5c05c7eec55dc214c9ef (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://sourceforge.net/projects/ledger-smb/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2000
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors: Jim Rawlings <jim@your-dba.com>
  23. #
  24. #======================================================================
  25. #
  26. # This file has NOT undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # user related functions
  31. #
  32. #=====================================================================
  33. package User;
  34. sub new {
  35. my ($type, $memfile, $login) = @_;
  36. my $self = {};
  37. if ($login ne "") {
  38. &error("", "$memfile locked!") if (-f "${memfile}.LCK");
  39. open(MEMBER, "$memfile") or &error("", "$memfile : $!");
  40. while (<MEMBER>) {
  41. if (/^\[$login\]/) {
  42. while (<MEMBER>) {
  43. last if /^\[/;
  44. next if /^(#|\s)/;
  45. # remove comments
  46. s/^\s*#.*//g;
  47. # remove any trailing whitespace
  48. s/^\s*(.*?)\s*$/$1/;
  49. ($key, $value) = split /=/, $_, 2;
  50. $self->{$key} = $value;
  51. }
  52. $self->{login} = $login;
  53. last;
  54. }
  55. }
  56. close MEMBER;
  57. }
  58. bless $self, $type;
  59. }
  60. sub country_codes {
  61. my %cc = ();
  62. my @language = ();
  63. # scan the locale directory and read in the LANGUAGE files
  64. opendir DIR, "locale";
  65. my @dir = grep !/(^\.\.?$|\..*)/, readdir DIR;
  66. foreach my $dir (@dir) {
  67. next unless open(FH, "locale/$dir/LANGUAGE");
  68. @language = <FH>;
  69. close FH;
  70. $cc{$dir} = "@language";
  71. }
  72. closedir(DIR);
  73. %cc;
  74. }
  75. sub login {
  76. my ($self, $form, $userspath) = @_;
  77. my $rc = -1;
  78. if ($self->{login} ne "") {
  79. if ($self->{password} ne "") {
  80. my $password = crypt $form->{password}, substr($self->{login}, 0, 2);
  81. if ($self->{password} ne $password) {
  82. return -1;
  83. }
  84. }
  85. #there shouldn't be any harm in always doing this. It might even un-bork things.
  86. $self->create_config("$userspath/$self->{login}.conf");
  87. do "$userspath/$self->{login}.conf";
  88. $myconfig{dbpasswd} = unpack 'u', $myconfig{dbpasswd};
  89. # check if database is down
  90. my $dbh = DBI->connect($myconfig{dbconnect}, $myconfig{dbuser}, $myconfig{dbpasswd}) or $self->error($DBI::errstr);
  91. # we got a connection, check the version
  92. my $query = qq|SELECT version FROM defaults|;
  93. my $sth = $dbh->prepare($query);
  94. $sth->execute || $form->dberror($query);
  95. my ($dbversion) = $sth->fetchrow_array;
  96. $sth->finish;
  97. # add login to employee table if it does not exist
  98. # no error check for employee table, ignore if it does not exist
  99. my $login = $self->{login};
  100. $login =~ s/@.*//;
  101. $query = qq|SELECT id FROM employee WHERE login = '$login'|;
  102. $sth = $dbh->prepare($query);
  103. $sth->execute;
  104. my ($id) = $sth->fetchrow_array;
  105. $sth->finish;
  106. if (! $id) {
  107. my ($employeenumber) = $form->update_defaults(\%myconfig, "employeenumber", $dbh);
  108. $query = qq|INSERT INTO employee (login, employeenumber, name, workphone,
  109. role)
  110. VALUES ('$login', '$employeenumber', '$myconfig{name}',
  111. '$myconfig{tel}', '$myconfig{role}')|;
  112. $dbh->do($query);
  113. }
  114. $dbh->disconnect;
  115. $rc = 0;
  116. if ($form->{dbversion} ne $dbversion) {
  117. $rc = -3;
  118. $dbupdate = (calc_version($dbversion) < calc_version($form->{dbversion}));
  119. }
  120. if ($dbupdate) {
  121. $rc = -4;
  122. # if DB2 bale out
  123. if ($myconfig{dbdriver} eq 'DB2') {
  124. $rc = -2;
  125. }
  126. }
  127. }
  128. $rc;
  129. }
  130. sub check_recurring {
  131. my ($self, $form) = @_;
  132. $self->{dbpasswd} = unpack 'u', $self->{dbpasswd};
  133. my $dbh = DBI->connect($self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd}) or $form->dberror;
  134. my $query = qq|SELECT count(*) FROM recurring
  135. WHERE enddate >= current_date AND nextdate <= current_date|;
  136. ($_) = $dbh->selectrow_array($query);
  137. $dbh->disconnect;
  138. $_;
  139. }
  140. sub dbconnect_vars {
  141. my ($form, $db) = @_;
  142. my %dboptions = (
  143. 'Pg' => {
  144. 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
  145. 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
  146. 'mm-dd-yy' => 'set DateStyle to \'POSTGRES, US\'',
  147. 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
  148. 'dd-mm-yy' => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
  149. 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
  150. },
  151. 'Oracle' => {
  152. 'yy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YY-MM-DD\'',
  153. 'mm/dd/yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM/DD/YY\'',
  154. 'mm-dd-yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM-DD-YY\'',
  155. 'dd/mm/yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD/MM/YY\'',
  156. 'dd-mm-yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD-MM-YY\'',
  157. 'dd.mm.yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD.MM.YY\'',
  158. }
  159. );
  160. $form->{dboptions} = $dboptions{$form->{dbdriver}}{$form->{dateformat}};
  161. if ($form->{dbdriver} =~ /Pg/) {
  162. $form->{dbconnect} = "dbi:$form->{dbdriver}:dbname=$db";
  163. }
  164. if ($form->{dbdriver} eq 'Oracle') {
  165. $form->{dbconnect} = "dbi:Oracle:sid=$form->{sid}";
  166. }
  167. if ($form->{dbhost}) {
  168. $form->{dbconnect} .= ";host=$form->{dbhost}";
  169. }
  170. if ($form->{dbport}) {
  171. $form->{dbconnect} .= ";port=$form->{dbport}";
  172. }
  173. }
  174. sub dbdrivers {
  175. my @drivers = DBI->available_drivers();
  176. # return (grep { /(Pg|Oracle|DB2)/ } @drivers);
  177. return (grep { /Pg$/ } @drivers);
  178. }
  179. sub dbsources {
  180. my ($self, $form) = @_;
  181. my @dbsources = ();
  182. my ($sth, $query);
  183. $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
  184. $form->{sid} = $form->{dbdefault};
  185. &dbconnect_vars($form, $form->{dbdefault});
  186. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  187. if ($form->{dbdriver} eq 'Pg') {
  188. $query = qq|SELECT datname FROM pg_database|;
  189. $sth = $dbh->prepare($query);
  190. $sth->execute || $form->dberror($query);
  191. while (my ($db) = $sth->fetchrow_array) {
  192. if ($form->{only_acc_db}) {
  193. next if ($db =~ /^template/);
  194. &dbconnect_vars($form, $db);
  195. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  196. $query = qq|SELECT tablename FROM pg_tables
  197. WHERE tablename = 'defaults'
  198. AND tableowner = '$form->{dbuser}'|;
  199. my $sth = $dbh->prepare($query);
  200. $sth->execute || $form->dberror($query);
  201. if ($sth->fetchrow_array) {
  202. push @dbsources, $db;
  203. }
  204. $sth->finish;
  205. $dbh->disconnect;
  206. next;
  207. }
  208. push @dbsources, $db;
  209. }
  210. }
  211. $sth->finish;
  212. $dbh->disconnect;
  213. return @dbsources;
  214. }
  215. sub dbcreate {
  216. my ($self, $form) = @_;
  217. my %dbcreate = ( 'Pg' => qq|CREATE DATABASE "$form->{db}"| );
  218. $dbcreate{Pg} .= " WITH ENCODING = '$form->{encoding}'" if $form->{encoding};
  219. $form->{sid} = $form->{dbdefault};
  220. &dbconnect_vars($form, $form->{dbdefault});
  221. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  222. my $query = qq|$dbcreate{$form->{dbdriver}}|;
  223. $dbh->do($query) || $form->dberror($query);
  224. # JD: We need to check for plpgsql, if it isn't there create it, if we can't error
  225. # Good chance I will have to do this twice as I get used to the way the code is
  226. # structured
  227. my %langcreate = ( 'Pg' => qq|CREATE LANGUAGE plpgsql|);
  228. my $query = qq|$langcreate{$form->{dbdriver}}|;
  229. $dbh->do($query) || $form->dberror($query);
  230. #Reassign for the work below
  231. &dbconnect_vars($form, $form->{db});
  232. # create the tables
  233. my $dbdriver = ($form->{dbdriver} =~ /Pg/) ? 'Pg' : $form->{dbdriver};
  234. my $filename = qq|sql/${dbdriver}-tables.sql|;
  235. $self->process_query($form, $dbh, $filename);
  236. # create functions
  237. $filename = qq|sql/${dbdriver}-functions.sql|;
  238. $self->process_query($form, $dbh, $filename);
  239. # load gifi
  240. ($filename) = split /_/, $form->{chart};
  241. $filename =~ s/_//;
  242. $self->process_query($form, $dbh, "sql/${filename}-gifi.sql");
  243. # load chart of accounts
  244. $filename = qq|sql/$form->{chart}-chart.sql|;
  245. $self->process_query($form, $dbh, $filename);
  246. # create indices
  247. $filename = qq|sql/${dbdriver}-indices.sql|;
  248. $self->process_query($form, $dbh, $filename);
  249. # create custom tables and functions
  250. my $item;
  251. foreach $item (qw(tables functions)) {
  252. $filename = "sql/${dbdriver}-custom_${item}.sql";
  253. if (-f "$filename") {
  254. $self->process_query($form, $dbh, $filename);
  255. }
  256. }
  257. $dbh->disconnect;
  258. }
  259. sub process_query {
  260. my ($self, $form, $dbh, $filename) = @_;
  261. return unless (-f $filename);
  262. open(FH, "$filename") or $form->error("$filename : $!\n");
  263. my $query = "";
  264. my $loop = 0;
  265. my $sth;
  266. while (<FH>) {
  267. if ($loop && /^--\s*end\s*(procedure|function|trigger)/i) {
  268. $loop = 0;
  269. $sth = $dbh->prepare($query);
  270. $sth->execute || $form->dberror($query);
  271. $sth->finish;
  272. $query = "";
  273. next;
  274. }
  275. if ($loop || /^create *(or replace)? *(procedure|function|trigger)/i) {
  276. $loop = 1;
  277. next if /^(--.*|\s+)$/;
  278. $query .= $_;
  279. next;
  280. }
  281. # don't add comments or empty lines
  282. next if /^(--.*|\s+)$/;
  283. # anything else, add to query
  284. $query .= $_;
  285. if (/;\s*$/) {
  286. # strip ;... Oracle doesn't like it
  287. $query =~ s/;\s*$//;
  288. $query =~ s/\\'/''/g;
  289. $sth = $dbh->prepare($query);
  290. $sth->execute || $form->dberror($query);
  291. $sth->finish;
  292. $query = "";
  293. }
  294. }
  295. close FH;
  296. }
  297. sub dbdelete {
  298. my ($self, $form) = @_;
  299. my %dbdelete = ( 'Pg' => qq|DROP DATABASE "$form->{db}"|,
  300. 'Oracle' => qq|DROP USER $form->{db} CASCADE|
  301. );
  302. $form->{sid} = $form->{dbdefault};
  303. &dbconnect_vars($form, $form->{dbdefault});
  304. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  305. my $query = qq|$dbdelete{$form->{dbdriver}}|;
  306. $dbh->do($query) || $form->dberror($query);
  307. $dbh->disconnect;
  308. }
  309. sub dbsources_unused {
  310. my ($self, $form, $memfile) = @_;
  311. my @dbexcl = ();
  312. my @dbsources = ();
  313. $form->error("$memfile locked!") if (-f "${memfile}.LCK");
  314. # open members file
  315. open(FH, "$memfile") or $form->error("$memfile : $!");
  316. while (<FH>) {
  317. if (/^dbname=/) {
  318. my ($null,$item) = split /=/;
  319. push @dbexcl, $item;
  320. }
  321. }
  322. close FH;
  323. $form->{only_acc_db} = 1;
  324. my @db = &dbsources("", $form);
  325. push @dbexcl, $form->{dbdefault};
  326. foreach $item (@db) {
  327. unless (grep /$item$/, @dbexcl) {
  328. push @dbsources, $item;
  329. }
  330. }
  331. return @dbsources;
  332. }
  333. sub dbneedsupdate {
  334. my ($self, $form) = @_;
  335. my %dbsources = ();
  336. my $query;
  337. $form->{sid} = $form->{dbdefault};
  338. &dbconnect_vars($form, $form->{dbdefault});
  339. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  340. if ($form->{dbdriver} =~ /Pg/) {
  341. $query = qq|SELECT d.datname FROM pg_database d, pg_user u
  342. WHERE d.datdba = u.usesysid
  343. AND u.usename = '$form->{dbuser}'|;
  344. my $sth = $dbh->prepare($query);
  345. $sth->execute || $form->dberror($query);
  346. while (my ($db) = $sth->fetchrow_array) {
  347. next if ($db =~ /^template/);
  348. &dbconnect_vars($form, $db);
  349. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  350. $query = qq|SELECT tablename FROM pg_tables
  351. WHERE tablename = 'defaults'|;
  352. my $sth = $dbh->prepare($query);
  353. $sth->execute || $form->dberror($query);
  354. if ($sth->fetchrow_array) {
  355. $query = qq|SELECT version FROM defaults|;
  356. my $sth = $dbh->prepare($query);
  357. $sth->execute;
  358. if (my ($version) = $sth->fetchrow_array) {
  359. $dbsources{$db} = $version;
  360. }
  361. $sth->finish;
  362. }
  363. $sth->finish;
  364. $dbh->disconnect;
  365. }
  366. $sth->finish;
  367. }
  368. if ($form->{dbdriver} eq 'Oracle') {
  369. $query = qq|SELECT owner FROM dba_objects
  370. WHERE object_name = 'DEFAULTS'
  371. AND object_type = 'TABLE'|;
  372. $sth = $dbh->prepare($query);
  373. $sth->execute || $form->dberror($query);
  374. while (my ($db) = $sth->fetchrow_array) {
  375. $form->{dbuser} = $db;
  376. &dbconnect_vars($form, $db);
  377. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  378. $query = qq|SELECT version FROM defaults|;
  379. my $sth = $dbh->prepare($query);
  380. $sth->execute;
  381. if (my ($version) = $sth->fetchrow_array) {
  382. $dbsources{$db} = $version;
  383. }
  384. $sth->finish;
  385. $dbh->disconnect;
  386. }
  387. $sth->finish;
  388. }
  389. # JJR
  390. if ($form->{dbdriver} eq 'DB2') {
  391. $query = qq|SELECT tabschema FROM syscat.tables WHERE tabname = 'DEFAULTS'|;
  392. $sth = $dbh->prepare($query);
  393. $sth->execute || $form->dberror($query);
  394. while (my ($db) = $sth->fetchrow_array) {
  395. &dbconnect_vars($form, $db);
  396. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  397. $query = qq|SELECT version FROM defaults|;
  398. my $sth = $dbh->prepare($query);
  399. $sth->execute;
  400. if (my ($version) = $sth->fetchrow_array) {
  401. $dbsources{$db} = $version;
  402. }
  403. $sth->finish;
  404. $dbh->disconnect;
  405. }
  406. $sth->finish;
  407. }
  408. # End JJR
  409. # code for DB2 is not used, keep for future reference
  410. # DS, Oct. 28, 2003
  411. $dbh->disconnect;
  412. %dbsources;
  413. }
  414. sub dbupdate {
  415. my ($self, $form) = @_;
  416. $form->{sid} = $form->{dbdefault};
  417. my @upgradescripts = ();
  418. my $query;
  419. my $rc = -2;
  420. if ($form->{dbupdate}) {
  421. # read update scripts into memory
  422. opendir SQLDIR, "sql/." or $form->error($!);
  423. @upgradescripts = sort script_version grep /$form->{dbdriver}-upgrade-.*?\.sql$/, readdir SQLDIR;
  424. closedir SQLDIR;
  425. }
  426. foreach my $db (split / /, $form->{dbupdate}) {
  427. next unless $form->{$db};
  428. # strip db from dataset
  429. $db =~ s/^db//;
  430. &dbconnect_vars($form, $db);
  431. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, {AutoCommit => 0}) or $form->dberror;
  432. # check version
  433. $query = qq|SELECT version FROM defaults|;
  434. my $sth = $dbh->prepare($query);
  435. # no error check, let it fall through
  436. $sth->execute;
  437. my $version = $sth->fetchrow_array;
  438. $sth->finish;
  439. next unless $version;
  440. $version = calc_version($version);
  441. my $dbversion = calc_version($form->{dbversion});
  442. foreach my $upgradescript (@upgradescripts) {
  443. my $a = $upgradescript;
  444. $a =~ s/(^$form->{dbdriver}-upgrade-|\.sql$)//g;
  445. my ($mindb, $maxdb) = split /-/, $a;
  446. $mindb = calc_version($mindb);
  447. $maxdb = calc_version($maxdb);
  448. next if ($version >= $maxdb);
  449. # exit if there is no upgrade script or version == mindb
  450. last if ($version < $mindb || $version >= $dbversion);
  451. # apply upgrade
  452. $self->process_query($form, $dbh, "sql/$upgradescript");
  453. $dbh->commit;
  454. $version = $maxdb;
  455. }
  456. $rc = 0;
  457. $dbh->disconnect;
  458. }
  459. $rc;
  460. }
  461. sub calc_version {
  462. my @v = split /\./, $_[0];
  463. my $version = 0;
  464. my $i;
  465. for ($i = 0; $i <= $#v; $i++) {
  466. $version *= 1000;
  467. $version += $v[$i];
  468. }
  469. return $version;
  470. }
  471. sub script_version {
  472. my ($my_a, $my_b) = ($a, $b);
  473. my ($a_from, $a_to, $b_from, $b_to);
  474. my ($res_a, $res_b, $i);
  475. $my_a =~ s/.*-upgrade-//;
  476. $my_a =~ s/.sql$//;
  477. $my_b =~ s/.*-upgrade-//;
  478. $my_b =~ s/.sql$//;
  479. ($a_from, $a_to) = split(/-/, $my_a);
  480. ($b_from, $b_to) = split(/-/, $my_b);
  481. $res_a = calc_version($a_from);
  482. $res_b = calc_version($b_from);
  483. if ($res_a == $res_b) {
  484. $res_a = calc_version($a_to);
  485. $res_b = calc_version($b_to);
  486. }
  487. return $res_a <=> $res_b;
  488. }
  489. sub create_config {
  490. my ($self, $filename) = @_;
  491. @config = &config_vars;
  492. open(CONF, ">$filename") or $self->error("$filename : $!");
  493. # create the config file
  494. print CONF qq|# configuration file for $self->{login}
  495. \%myconfig = (
  496. |;
  497. foreach $key (sort @config) {
  498. $self->{$key} =~ s/\\/\\\\/g;
  499. $self->{$key} =~ s/'/\\'/g;
  500. #remaining conversion from SL
  501. $self->{$key} =~ s/sql-ledger([^.]*)\.css/ledger-smb$1.css/g;
  502. print CONF qq| $key => '$self->{$key}',\n|;
  503. }
  504. print CONF qq|);\n\n|;
  505. close CONF;
  506. }
  507. sub save_member {
  508. my ($self, $memberfile, $userspath) = @_;
  509. # format dbconnect and dboptions string
  510. &dbconnect_vars($self, $self->{dbname});
  511. $self->error("$memberfile locked!") if (-f "${memberfile}.LCK");
  512. open(FH, ">${memberfile}.LCK") or $self->error("${memberfile}.LCK : $!");
  513. close(FH);
  514. if (! open(CONF, "+<$memberfile")) {
  515. unlink "${memberfile}.LCK";
  516. $self->error("$memberfile : $!");
  517. }
  518. @config = <CONF>;
  519. seek(CONF, 0, 0);
  520. truncate(CONF, 0);
  521. while ($line = shift @config) {
  522. last if ($line =~ /^\[$self->{login}\]/);
  523. #remaining conversion from SL
  524. $line =~ s/sql-ledger([^.]*)\.css/ledger-smb$1.css/g;
  525. print CONF $line;
  526. }
  527. # remove everything up to next login or EOF
  528. while ($line = shift @config) {
  529. last if ($line =~ /^\[/);
  530. }
  531. # this one is either the next login or EOF
  532. print CONF $line;
  533. while ($line = shift @config) {
  534. print CONF $line;
  535. }
  536. print CONF qq|[$self->{login}]\n|;
  537. if ($self->{packpw}) {
  538. $self->{dbpasswd} = pack 'u', $self->{dbpasswd};
  539. chop $self->{dbpasswd};
  540. }
  541. if ($self->{password} ne $self->{old_password}) {
  542. $self->{password} = crypt $self->{password}, substr($self->{login}, 0, 2) if $self->{password};
  543. }
  544. if ($self->{'root login'}) {
  545. @config = qw(password);
  546. } else {
  547. @config = &config_vars;
  548. }
  549. # replace \r\n with \n
  550. for (qw(address signature)) { $self->{$_} =~ s/\r?\n/\\n/g }
  551. for (sort @config) {
  552. print CONF qq|$_=$self->{$_}\n|
  553. }
  554. print CONF "\n";
  555. close CONF;
  556. unlink "${memberfile}.LCK";
  557. # create conf file
  558. if (! $self->{'root login'}) {
  559. $self->create_config("$userspath/$self->{login}.conf");
  560. $self->{dbpasswd} =~ s/\\'/'/g;
  561. $self->{dbpasswd} =~ s/\\\\/\\/g;
  562. $self->{dbpasswd} = unpack 'u', $self->{dbpasswd};
  563. # check if login is in database
  564. my $dbh = DBI->connect($self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd}, {AutoCommit => 0}) or $self->error($DBI::errstr);
  565. # add login to employee table if it does not exist
  566. my $login = $self->{login};
  567. $login =~ s/@.*//;
  568. my $query = qq|SELECT id FROM employee WHERE login = '$login'|;
  569. my $sth = $dbh->prepare($query);
  570. $sth->execute;
  571. my ($id) = $sth->fetchrow_array;
  572. $sth->finish;
  573. if ($id) {
  574. $query = qq|UPDATE employee SET
  575. role = '$self->{role}',
  576. email = '$self->{email}',
  577. name = '$self->{name}'
  578. WHERE login = '$login'|;
  579. } else {
  580. my ($employeenumber) = Form::update_defaults("", \%$self, "employeenumber", $dbh);
  581. $query = qq|INSERT INTO employee (login, employeenumber, name, workphone, role, email, sales)
  582. VALUES ('$login', '$employeenumber', '$self->{name}',
  583. '$self->{tel}', '$self->{role}', '$self->{email}', '1')|;
  584. }
  585. $dbh->do($query);
  586. $dbh->commit;
  587. $dbh->disconnect;
  588. }
  589. }
  590. sub delete_login {
  591. my ($self, $form) = @_;
  592. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, {AutoCommit} => 0) or $form->dberror;
  593. my $login = $form->{login};
  594. $login =~ s/@.*//;
  595. my $query = qq|SELECT id FROM employee
  596. WHERE login = '$login'|;
  597. my $sth = $dbh->prepare($query);
  598. $sth->execute || $form->dberror($query);
  599. my ($id) = $sth->fetchrow_array;
  600. $sth->finish;
  601. my $query = qq|UPDATE employee SET
  602. login = NULL,
  603. enddate = current_date
  604. WHERE login = '$login'|;
  605. $dbh->do($query);
  606. $dbh->commit;
  607. $dbh->disconnect;
  608. }
  609. sub config_vars {
  610. my @conf = qw(acs address businessnumber company countrycode
  611. currency dateformat dbconnect dbdriver dbhost dbname dboptions
  612. dbpasswd dbport dbuser email fax menuwidth name numberformat
  613. password printer role sid signature stylesheet tel
  614. templates timeout vclimit);
  615. @conf;
  616. }
  617. sub error {
  618. my ($self, $msg) = @_;
  619. if ($ENV{HTTP_USER_AGENT}) {
  620. print qq|Content-Type: text/html
  621. <body bgcolor=ffffff>
  622. <h2><font color=red>Error!</font></h2>
  623. <p><b>$msg</b>|;
  624. }
  625. die "Error: $msg\n";
  626. }
  627. 1;