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