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