summaryrefslogtreecommitdiff
path: root/LedgerSMB/User.pm
blob: e0f2bc6b51ed09ab2b2b88eb57e8dbc5c4517a0e (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. # The below line connects to Template1 or another template file in order
  222. # to create the db. One must disconnect and reconnect later.
  223. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  224. my $query = qq|$dbcreate{$form->{dbdriver}}|;
  225. $dbh->do($query) || $form->dberror($query);
  226. $dbh->disconnect;
  227. #Reassign for the work below
  228. &dbconnect_vars($form, $form->{db});
  229. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  230. # JD: We need to check for plpgsql, if it isn't there create it, if we can't error
  231. # Good chance I will have to do this twice as I get used to the way the code is
  232. # structured
  233. my %langcreate = ( 'Pg' => qq|CREATE LANGUAGE plpgsql|);
  234. my $query = qq|$langcreate{$form->{dbdriver}}|;
  235. $dbh->do($query) || $form->dberror($query);
  236. # create the tables
  237. my $dbdriver = ($form->{dbdriver} =~ /Pg/) ? 'Pg' : $form->{dbdriver};
  238. my $filename = qq|sql/${dbdriver}-tables.sql|;
  239. $self->process_query($form, $dbh, $filename);
  240. # create functions
  241. $filename = qq|sql/${dbdriver}-functions.sql|;
  242. $self->process_query($form, $dbh, $filename);
  243. # load gifi
  244. ($filename) = split /_/, $form->{chart};
  245. $filename =~ s/_//;
  246. $self->process_query($form, $dbh, "sql/${filename}-gifi.sql");
  247. # load chart of accounts
  248. $filename = qq|sql/$form->{chart}-chart.sql|;
  249. $self->process_query($form, $dbh, $filename);
  250. # create indices
  251. $filename = qq|sql/${dbdriver}-indices.sql|;
  252. $self->process_query($form, $dbh, $filename);
  253. # create custom tables and functions
  254. my $item;
  255. foreach $item (qw(tables functions)) {
  256. $filename = "sql/${dbdriver}-custom_${item}.sql";
  257. if (-f "$filename") {
  258. $self->process_query($form, $dbh, $filename);
  259. }
  260. }
  261. $dbh->disconnect;
  262. }
  263. sub process_query {
  264. my ($self, $form, $dbh, $filename) = @_;
  265. return unless (-f $filename);
  266. open(FH, "$filename") or $form->error("$filename : $!\n");
  267. my $query = "";
  268. my $loop = 0;
  269. my $sth;
  270. while (<FH>) {
  271. if ($loop && /^--\s*end\s*(procedure|function|trigger)/i) {
  272. $loop = 0;
  273. $sth = $dbh->prepare($query);
  274. $sth->execute || $form->dberror($query);
  275. $sth->finish;
  276. $query = "";
  277. next;
  278. }
  279. if ($loop || /^create *(or replace)? *(procedure|function|trigger)/i) {
  280. $loop = 1;
  281. next if /^(--.*|\s+)$/;
  282. $query .= $_;
  283. next;
  284. }
  285. # don't add comments or empty lines
  286. next if /^(--.*|\s+)$/;
  287. # anything else, add to query
  288. $query .= $_;
  289. if (/;\s*$/) {
  290. # strip ;... Oracle doesn't like it
  291. $query =~ s/;\s*$//;
  292. $query =~ s/\\'/''/g;
  293. $sth = $dbh->prepare($query);
  294. $sth->execute || $form->dberror($query);
  295. $sth->finish;
  296. $query = "";
  297. }
  298. }
  299. close FH;
  300. }
  301. sub dbdelete {
  302. my ($self, $form) = @_;
  303. my %dbdelete = ( 'Pg' => qq|DROP DATABASE "$form->{db}"|,
  304. 'Oracle' => qq|DROP USER $form->{db} CASCADE|
  305. );
  306. $form->{sid} = $form->{dbdefault};
  307. &dbconnect_vars($form, $form->{dbdefault});
  308. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  309. my $query = qq|$dbdelete{$form->{dbdriver}}|;
  310. $dbh->do($query) || $form->dberror($query);
  311. $dbh->disconnect;
  312. }
  313. sub dbsources_unused {
  314. my ($self, $form, $memfile) = @_;
  315. my @dbexcl = ();
  316. my @dbsources = ();
  317. $form->error("$memfile locked!") if (-f "${memfile}.LCK");
  318. # open members file
  319. open(FH, "$memfile") or $form->error("$memfile : $!");
  320. while (<FH>) {
  321. if (/^dbname=/) {
  322. my ($null,$item) = split /=/;
  323. push @dbexcl, $item;
  324. }
  325. }
  326. close FH;
  327. $form->{only_acc_db} = 1;
  328. my @db = &dbsources("", $form);
  329. push @dbexcl, $form->{dbdefault};
  330. foreach $item (@db) {
  331. unless (grep /$item$/, @dbexcl) {
  332. push @dbsources, $item;
  333. }
  334. }
  335. return @dbsources;
  336. }
  337. sub dbneedsupdate {
  338. my ($self, $form) = @_;
  339. my %dbsources = ();
  340. my $query;
  341. $form->{sid} = $form->{dbdefault};
  342. &dbconnect_vars($form, $form->{dbdefault});
  343. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  344. if ($form->{dbdriver} =~ /Pg/) {
  345. $query = qq|SELECT d.datname FROM pg_database d, pg_user u
  346. WHERE d.datdba = u.usesysid
  347. AND u.usename = '$form->{dbuser}'|;
  348. my $sth = $dbh->prepare($query);
  349. $sth->execute || $form->dberror($query);
  350. while (my ($db) = $sth->fetchrow_array) {
  351. next if ($db =~ /^template/);
  352. &dbconnect_vars($form, $db);
  353. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  354. $query = qq|SELECT tablename FROM pg_tables
  355. WHERE tablename = 'defaults'|;
  356. my $sth = $dbh->prepare($query);
  357. $sth->execute || $form->dberror($query);
  358. if ($sth->fetchrow_array) {
  359. $query = qq|SELECT version FROM defaults|;
  360. my $sth = $dbh->prepare($query);
  361. $sth->execute;
  362. if (my ($version) = $sth->fetchrow_array) {
  363. $dbsources{$db} = $version;
  364. }
  365. $sth->finish;
  366. }
  367. $sth->finish;
  368. $dbh->disconnect;
  369. }
  370. $sth->finish;
  371. }
  372. if ($form->{dbdriver} eq 'Oracle') {
  373. $query = qq|SELECT owner FROM dba_objects
  374. WHERE object_name = 'DEFAULTS'
  375. AND object_type = 'TABLE'|;
  376. $sth = $dbh->prepare($query);
  377. $sth->execute || $form->dberror($query);
  378. while (my ($db) = $sth->fetchrow_array) {
  379. $form->{dbuser} = $db;
  380. &dbconnect_vars($form, $db);
  381. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  382. $query = qq|SELECT version FROM defaults|;
  383. my $sth = $dbh->prepare($query);
  384. $sth->execute;
  385. if (my ($version) = $sth->fetchrow_array) {
  386. $dbsources{$db} = $version;
  387. }
  388. $sth->finish;
  389. $dbh->disconnect;
  390. }
  391. $sth->finish;
  392. }
  393. # JJR
  394. if ($form->{dbdriver} eq 'DB2') {
  395. $query = qq|SELECT tabschema FROM syscat.tables WHERE tabname = 'DEFAULTS'|;
  396. $sth = $dbh->prepare($query);
  397. $sth->execute || $form->dberror($query);
  398. while (my ($db) = $sth->fetchrow_array) {
  399. &dbconnect_vars($form, $db);
  400. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror;
  401. $query = qq|SELECT version FROM defaults|;
  402. my $sth = $dbh->prepare($query);
  403. $sth->execute;
  404. if (my ($version) = $sth->fetchrow_array) {
  405. $dbsources{$db} = $version;
  406. }
  407. $sth->finish;
  408. $dbh->disconnect;
  409. }
  410. $sth->finish;
  411. }
  412. # End JJR
  413. # code for DB2 is not used, keep for future reference
  414. # DS, Oct. 28, 2003
  415. $dbh->disconnect;
  416. %dbsources;
  417. }
  418. sub dbupdate {
  419. my ($self, $form) = @_;
  420. $form->{sid} = $form->{dbdefault};
  421. my @upgradescripts = ();
  422. my $query;
  423. my $rc = -2;
  424. if ($form->{dbupdate}) {
  425. # read update scripts into memory
  426. opendir SQLDIR, "sql/." or $form->error($!);
  427. @upgradescripts = sort script_version grep /$form->{dbdriver}-upgrade-.*?\.sql$/, readdir SQLDIR;
  428. closedir SQLDIR;
  429. }
  430. foreach my $db (split / /, $form->{dbupdate}) {
  431. next unless $form->{$db};
  432. # strip db from dataset
  433. $db =~ s/^db//;
  434. &dbconnect_vars($form, $db);
  435. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, {AutoCommit => 0}) or $form->dberror;
  436. # check version
  437. $query = qq|SELECT version FROM defaults|;
  438. my $sth = $dbh->prepare($query);
  439. # no error check, let it fall through
  440. $sth->execute;
  441. my $version = $sth->fetchrow_array;
  442. $sth->finish;
  443. next unless $version;
  444. $version = calc_version($version);
  445. my $dbversion = calc_version($form->{dbversion});
  446. foreach my $upgradescript (@upgradescripts) {
  447. my $a = $upgradescript;
  448. $a =~ s/(^$form->{dbdriver}-upgrade-|\.sql$)//g;
  449. my ($mindb, $maxdb) = split /-/, $a;
  450. $mindb = calc_version($mindb);
  451. $maxdb = calc_version($maxdb);
  452. next if ($version >= $maxdb);
  453. # exit if there is no upgrade script or version == mindb
  454. last if ($version < $mindb || $version >= $dbversion);
  455. # apply upgrade
  456. $self->process_query($form, $dbh, "sql/$upgradescript");
  457. $dbh->commit;
  458. $version = $maxdb;
  459. }
  460. $rc = 0;
  461. $dbh->disconnect;
  462. }
  463. $rc;
  464. }
  465. sub calc_version {
  466. my @v = split /\./, $_[0];
  467. my $version = 0;
  468. my $i;
  469. for ($i = 0; $i <= $#v; $i++) {
  470. $version *= 1000;
  471. $version += $v[$i];
  472. }
  473. return $version;
  474. }
  475. sub script_version {
  476. my ($my_a, $my_b) = ($a, $b);
  477. my ($a_from, $a_to, $b_from, $b_to);
  478. my ($res_a, $res_b, $i);
  479. $my_a =~ s/.*-upgrade-//;
  480. $my_a =~ s/.sql$//;
  481. $my_b =~ s/.*-upgrade-//;
  482. $my_b =~ s/.sql$//;
  483. ($a_from, $a_to) = split(/-/, $my_a);
  484. ($b_from, $b_to) = split(/-/, $my_b);
  485. $res_a = calc_version($a_from);
  486. $res_b = calc_version($b_from);
  487. if ($res_a == $res_b) {
  488. $res_a = calc_version($a_to);
  489. $res_b = calc_version($b_to);
  490. }
  491. return $res_a <=> $res_b;
  492. }
  493. sub create_config {
  494. my ($self, $filename) = @_;
  495. @config = &config_vars;
  496. open(CONF, ">$filename") or $self->error("$filename : $!");
  497. # create the config file
  498. print CONF qq|# configuration file for $self->{login}
  499. \%myconfig = (
  500. |;
  501. foreach $key (sort @config) {
  502. $self->{$key} =~ s/\\/\\\\/g;
  503. $self->{$key} =~ s/'/\\'/g;
  504. #remaining conversion from SL
  505. $self->{$key} =~ s/sql-ledger([^.]*)\.css/ledger-smb$1.css/g;
  506. print CONF qq| $key => '$self->{$key}',\n|;
  507. }
  508. print CONF qq|);\n\n|;
  509. close CONF;
  510. }
  511. sub save_member {
  512. my ($self, $memberfile, $userspath) = @_;
  513. # format dbconnect and dboptions string
  514. &dbconnect_vars($self, $self->{dbname});
  515. $self->error("$memberfile locked!") if (-f "${memberfile}.LCK");
  516. open(FH, ">${memberfile}.LCK") or $self->error("${memberfile}.LCK : $!");
  517. close(FH);
  518. if (! open(CONF, "+<$memberfile")) {
  519. unlink "${memberfile}.LCK";
  520. $self->error("$memberfile : $!");
  521. }
  522. @config = <CONF>;
  523. seek(CONF, 0, 0);
  524. truncate(CONF, 0);
  525. while ($line = shift @config) {
  526. last if ($line =~ /^\[$self->{login}\]/);
  527. #remaining conversion from SL
  528. $line =~ s/sql-ledger([^.]*)\.css/ledger-smb$1.css/g;
  529. print CONF $line;
  530. }
  531. # remove everything up to next login or EOF
  532. while ($line = shift @config) {
  533. last if ($line =~ /^\[/);
  534. }
  535. # this one is either the next login or EOF
  536. print CONF $line;
  537. while ($line = shift @config) {
  538. print CONF $line;
  539. }
  540. print CONF qq|[$self->{login}]\n|;
  541. if ($self->{packpw}) {
  542. $self->{dbpasswd} = pack 'u', $self->{dbpasswd};
  543. chop $self->{dbpasswd};
  544. }
  545. if ($self->{password} ne $self->{old_password}) {
  546. $self->{password} = crypt $self->{password}, substr($self->{login}, 0, 2) if $self->{password};
  547. }
  548. if ($self->{'root login'}) {
  549. @config = qw(password);
  550. } else {
  551. @config = &config_vars;
  552. }
  553. # replace \r\n with \n
  554. for (qw(address signature)) { $self->{$_} =~ s/\r?\n/\\n/g }
  555. for (sort @config) {
  556. print CONF qq|$_=$self->{$_}\n|
  557. }
  558. print CONF "\n";
  559. close CONF;
  560. unlink "${memberfile}.LCK";
  561. # create conf file
  562. if (! $self->{'root login'}) {
  563. $self->create_config("$userspath/$self->{login}.conf");
  564. $self->{dbpasswd} =~ s/\\'/'/g;
  565. $self->{dbpasswd} =~ s/\\\\/\\/g;
  566. $self->{dbpasswd} = unpack 'u', $self->{dbpasswd};
  567. # check if login is in database
  568. my $dbh = DBI->connect($self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd}, {AutoCommit => 0}) or $self->error($DBI::errstr);
  569. # add login to employee table if it does not exist
  570. my $login = $self->{login};
  571. $login =~ s/@.*//;
  572. my $query = qq|SELECT id FROM employee WHERE login = '$login'|;
  573. my $sth = $dbh->prepare($query);
  574. $sth->execute;
  575. my ($id) = $sth->fetchrow_array;
  576. $sth->finish;
  577. if ($id) {
  578. $query = qq|UPDATE employee SET
  579. role = '$self->{role}',
  580. email = '$self->{email}',
  581. name = '$self->{name}'
  582. WHERE login = '$login'|;
  583. } else {
  584. my ($employeenumber) = Form::update_defaults("", \%$self, "employeenumber", $dbh);
  585. $query = qq|INSERT INTO employee (login, employeenumber, name, workphone, role, email, sales)
  586. VALUES ('$login', '$employeenumber', '$self->{name}',
  587. '$self->{tel}', '$self->{role}', '$self->{email}', '1')|;
  588. }
  589. $dbh->do($query);
  590. $dbh->commit;
  591. $dbh->disconnect;
  592. }
  593. }
  594. sub delete_login {
  595. my ($self, $form) = @_;
  596. my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, {AutoCommit} => 0) or $form->dberror;
  597. my $login = $form->{login};
  598. $login =~ s/@.*//;
  599. my $query = qq|SELECT id FROM employee
  600. WHERE login = '$login'|;
  601. my $sth = $dbh->prepare($query);
  602. $sth->execute || $form->dberror($query);
  603. my ($id) = $sth->fetchrow_array;
  604. $sth->finish;
  605. my $query = qq|UPDATE employee SET
  606. login = NULL,
  607. enddate = current_date
  608. WHERE login = '$login'|;
  609. $dbh->do($query);
  610. $dbh->commit;
  611. $dbh->disconnect;
  612. }
  613. sub config_vars {
  614. my @conf = qw(acs address businessnumber company countrycode
  615. currency dateformat dbconnect dbdriver dbhost dbname dboptions
  616. dbpasswd dbport dbuser email fax menuwidth name numberformat
  617. password printer role sid signature stylesheet tel
  618. templates timeout vclimit);
  619. @conf;
  620. }
  621. sub error {
  622. my ($self, $msg) = @_;
  623. if ($ENV{HTTP_USER_AGENT}) {
  624. print qq|Content-Type: text/html
  625. <body bgcolor=ffffff>
  626. <h2><font color=red>Error!</font></h2>
  627. <p><b>$msg</b>|;
  628. }
  629. die "Error: $msg\n";
  630. }
  631. 1;