summaryrefslogtreecommitdiff
path: root/LedgerSMB/User.pm
blob: a020affb746f76eede9f147ed335e61b79e72b2a (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 undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # user related functions
  31. #
  32. #=====================================================================
  33. package LedgerSMB::User;
  34. use LedgerSMB::Sysconfig;
  35. use LedgerSMB::Session;
  36. use Data::Dumper;
  37. sub new {
  38. my ($type, $login) = @_;
  39. my $self = {};
  40. if ($login ne "") {
  41. # use central db
  42. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  43. # for now, this is querying the table directly... ugly
  44. my $fetchUserPrefs = $dbh->prepare("SELECT acs, address, businessnumber,
  45. company, countrycode, currency,
  46. dateformat, dbdriver, dbhost, dbname,
  47. dboptions, dbpasswd, dbport, dbuser,
  48. email, fax, menuwidth, name, numberformat,
  49. password, print, printer, role, sid,
  50. signature, stylesheet, tel, templates,
  51. timeout, vclimit, u.username
  52. FROM users_conf as uc, users as u
  53. WHERE u.username = ?
  54. AND u.id = uc.id;");
  55. $fetchUserPrefs->execute($login);
  56. my $userHashRef = $fetchUserPrefs->fetchrow_hashref;
  57. while ( my ($key, $value) = each(%{$userHashRef}) ) {
  58. $self->{$key} = $value;
  59. }
  60. chomp($self->{dbport});
  61. chomp($self->{dbname});
  62. chomp($self->{dbhost});
  63. if(! int($self->{dbport})){#in case there's a space or junk in the dbport
  64. $self->{dbport} = '5432';
  65. }
  66. $self->{dbconnect} = 'dbi:Pg:dbname='.$self->{dbname}.';host='.$self->{dbhost}.';port='.$self->{dbport};
  67. if($self->{username}){
  68. $self->{login} = $login;
  69. }
  70. }
  71. bless $self, $type;
  72. }
  73. sub country_codes {
  74. use Locale::Country;
  75. use Locale::Language;
  76. my %cc = ();
  77. # scan the locale directory and read in the LANGUAGE files
  78. opendir DIR, "${LedgerSMB::Sysconfig::localepath}";
  79. my @dir = grep !/^\..*$/, readdir DIR;
  80. foreach my $dir (@dir) {
  81. $dir = substr($dir, 0, -3);
  82. $cc{$dir} = code2language(substr($dir, 0, 2));
  83. $cc{$dir} .= ("/" . code2country(substr($dir, 3, 2)))
  84. if length($dir) > 2;
  85. $cc{$dir} .= (" " . substr($dir, 6)) if length($dir) > 5;
  86. }
  87. closedir(DIR);
  88. %cc;
  89. }
  90. sub fetch_config {
  91. #I'm hoping that this function will go and is a temporary bridge
  92. #until we get rid of %myconfig elsewhere in the code
  93. my ($self, $login) = @_;
  94. if ($login ne "") {
  95. # use central db
  96. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  97. # for now, this is querying the table directly... ugly
  98. my $fetchUserPrefs = $dbh->prepare("SELECT acs, address, businessnumber,
  99. company, countrycode, currency,
  100. dateformat, dbdriver, dbhost, dbname,
  101. dboptions, dbpasswd, dbport, dbuser,
  102. email, fax, menuwidth, name, numberformat,
  103. password, print, printer, role, sid,
  104. signature, stylesheet, tel, templates,
  105. timeout, vclimit, u.username
  106. FROM users_conf as uc, users as u
  107. WHERE u.username = ?
  108. AND u.id = uc.id;");
  109. $fetchUserPrefs->execute($login);
  110. my $userHashRef = $fetchUserPrefs->fetchrow_hashref;
  111. while ( my ($key, $value) = each(%{$userHashRef}) ) {
  112. $myconfig{$key} = $value;
  113. }
  114. if(! int($myconfig{'dbport'})){#in case there's a space or junk in the dbport
  115. $myconfig{'dbport'} = '5432';
  116. }
  117. $myconfig{'dbconnect'} = 'dbi:Pg:dbname='.$myconfig{'dbname'}.';host='.$myconfig{'dbhost'}.';port='.$myconfig{'dbport'};
  118. }
  119. return \%myconfig;
  120. }
  121. sub login {
  122. my ($self, $form) = @_;
  123. my $rc = -1;
  124. if ($self->{login} ne "") {
  125. if (! Session::password_check($form, $form->{login}, $form->{password})) {
  126. return -1;
  127. }
  128. #this is really dumb, but %myconfig will have to stay until 1.3
  129. while ( my ($key, $value) = each(%{$self}) ) {
  130. $myconfig{$key} = $value;
  131. }
  132. # check if database is down
  133. my $dbh = DBI->connect(
  134. $myconfig{dbconnect}, $myconfig{dbuser},
  135. $myconfig{dbpasswd})
  136. or $self->error(__FILE__.':'.__LINE__.': '.$DBI::errstr);
  137. # we got a connection, check the version
  138. my $query = qq|
  139. SELECT value FROM defaults
  140. WHERE setting_key = 'version'|;
  141. my $sth = $dbh->prepare($query);
  142. $sth->execute || $form->dberror(__FILE__.':'.__LINE__.$query);
  143. my ($dbversion) = $sth->fetchrow_array;
  144. $sth->finish;
  145. # add login to employee table if it does not exist
  146. # no error check for employee table, ignore if it does not exist
  147. my $login = $self->{login};
  148. $login =~ s/@.*//;
  149. $query = qq|SELECT id FROM employee WHERE login = ?|;
  150. $sth = $dbh->prepare($query);
  151. $sth->execute($login);
  152. my ($id) = $sth->fetchrow_array;
  153. $sth->finish;
  154. if (! $id) {
  155. my ($employeenumber) =
  156. $form->update_defaults(
  157. \%myconfig, "employeenumber", $dbh);
  158. $query = qq|
  159. INSERT INTO employee
  160. (login, employeenumber, name,
  161. workphone, role)
  162. VALUES (?, ?, ?, ?, ?)|;
  163. $sth = $dbh->prepare($query);
  164. $sth->execute(
  165. $login, $employeenumber, $myconfig{name},
  166. $myconfig{tel}, $myconfig{role});
  167. }
  168. $dbh->disconnect;
  169. $rc = 0;
  170. if ($form->{dbversion} ne $dbversion) {
  171. $rc = -3;
  172. $dbupdate = (calc_version($dbversion)
  173. < calc_version($form->{dbversion}));
  174. }
  175. if ($dbupdate) {
  176. $rc = -4;
  177. # if DB2 bale out
  178. if ($myconfig{dbdriver} eq 'DB2') {
  179. $rc = -2;
  180. }
  181. }
  182. }
  183. $rc;
  184. }
  185. sub check_recurring {
  186. my ($self, $form) = @_;
  187. my $dbh = DBI->connect(
  188. $self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd})
  189. or $form->dberror(__FILE__.':'.__LINE__);
  190. my $query = qq|
  191. SELECT count(*) FROM recurring
  192. WHERE enddate >= current_date AND nextdate <= current_date|;
  193. ($_) = $dbh->selectrow_array($query);
  194. $dbh->disconnect;
  195. $_;
  196. }
  197. sub dbconnect_vars {
  198. my ($form, $db) = @_;
  199. my %dboptions = (
  200. 'Pg' => {
  201. 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
  202. 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
  203. 'mm-dd-yy' => 'set DateStyle to \'POSTGRES, US\'',
  204. 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
  205. 'dd-mm-yy' => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
  206. 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
  207. }
  208. );
  209. $form->{dboptions} = $dboptions{$form->{dbdriver}}{$form->{dateformat}};
  210. $form->{dbconnect} = "dbi:$form->{dbdriver}:dbname=$db";
  211. if ($form->{dbhost}) {
  212. $form->{dbconnect} .= ";host=$form->{dbhost}";
  213. }
  214. if ($form->{dbport}) {
  215. $form->{dbconnect} .= ";port=$form->{dbport}";
  216. }
  217. }
  218. sub dbdrivers {
  219. my @drivers = DBI->available_drivers();
  220. # return (grep { /(Pg|Oracle|DB2)/ } @drivers);
  221. return (grep { /Pg$/ } @drivers);
  222. }
  223. sub dbsources {
  224. my ($self, $form) = @_;
  225. my @dbsources = ();
  226. my ($sth, $query);
  227. $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
  228. $form->{sid} = $form->{dbdefault};
  229. &dbconnect_vars($form, $form->{dbdefault});
  230. my $dbh = DBI->connect(
  231. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
  232. or $form->dberror(__FILE__.':'.__LINE__);
  233. if ($form->{dbdriver} eq 'Pg') {
  234. $query = qq|SELECT datname FROM pg_database|;
  235. $sth = $dbh->prepare($query);
  236. $sth->execute || $form->dberror(__FILE__.':'.__LINE__.$query);
  237. while (my ($db) = $sth->fetchrow_array) {
  238. if ($form->{only_acc_db}) {
  239. next if ($db =~ /^template/);
  240. &dbconnect_vars($form, $db);
  241. my $dbh = DBI->connect(
  242. $form->{dbconnect}, $form->{dbuser},
  243. $form->{dbpasswd})
  244. or $form->dberror(__FILE__.':'.__LINE__);
  245. $query = qq|
  246. SELECT tablename FROM pg_tables
  247. WHERE tablename = 'defaults'
  248. AND tableowner = ?|;
  249. my $sth = $dbh->prepare($query);
  250. $sth->execute($form->{dbuser})
  251. || $form->dberror(__FILE__.':'.__LINE__.$query);
  252. if ($sth->fetchrow_array) {
  253. push @dbsources, $db;
  254. }
  255. $sth->finish;
  256. $dbh->disconnect;
  257. next;
  258. }
  259. push @dbsources, $db;
  260. }
  261. }
  262. $sth->finish;
  263. $dbh->disconnect;
  264. return @dbsources;
  265. }
  266. sub dbcreate {
  267. my ($self, $form) = @_;
  268. my %dbcreate = ( 'Pg' => qq|CREATE DATABASE "$form->{db}"| );
  269. $dbcreate{Pg} .= " WITH ENCODING = '$form->{encoding}'"
  270. if $form->{encoding};
  271. $form->{sid} = $form->{dbdefault};
  272. &dbconnect_vars($form, $form->{dbdefault});
  273. # The below line connects to Template1 or another template file in order
  274. # to create the db. One must disconnect and reconnect later.
  275. if ($form->{dbsuperuser}){
  276. my $superdbh = DBI->connect(
  277. $form->{dbconnect},
  278. $form->{dbsuperuser},
  279. $form->{dbsuperpasswd})
  280. or $form->dberror(__FILE__.':'.__LINE__);
  281. my $query = qq|$dbcreate{$form->{dbdriver}}|;
  282. $superdbh->do($query) || $form->dberror(__FILE__.':'.__LINE__.$query);
  283. $superdbh->disconnect;
  284. }
  285. #Reassign for the work below
  286. &dbconnect_vars($form, $form->{db});
  287. my $dbh = DBI->connect(
  288. $form->{dbconnect},
  289. $form->{dbuser},
  290. $form->{dbpasswd})
  291. or $form->dberror(__FILE__.':'.__LINE__);
  292. if ($form->{dbsuperuser}){
  293. my $superdbh = DBI->connect(
  294. $form->{dbconnect},
  295. $form->{dbsuperuser},
  296. $form->{dbsuperpasswd})
  297. or $form->dberror(__FILE__.':'.__LINE__);
  298. # JD: We need to check for plpgsql,
  299. # if it isn't there create it, if we can't error
  300. # Good chance I will have to do this twice as I get
  301. # used to the way the code is structured
  302. my %langcreate = ( 'Pg' => qq|CREATE LANGUAGE plpgsql|);
  303. my $query = qq|$langcreate{$form->{dbdriver}}|;
  304. $superdbh->do($query);
  305. $superdbh->disconnect;
  306. }
  307. # create the tables
  308. my $dbdriver =
  309. ($form->{dbdriver} =~ /Pg/)
  310. ? 'Pg'
  311. : $form->{dbdriver};
  312. my $filename = qq|sql/Pg-database.sql|;
  313. $self->process_query($form, $dbh, $filename);
  314. # load gifi
  315. ($filename) = split /_/, $form->{chart};
  316. $filename =~ s/_//;
  317. $self->process_query($form, $dbh, "sql/${filename}-gifi.sql");
  318. # load chart of accounts
  319. $filename = qq|sql/$form->{chart}-chart.sql|;
  320. $self->process_query($form, $dbh, $filename);
  321. # create custom tables and functions
  322. my $item;
  323. foreach $item (qw(tables functions)) {
  324. $filename = "sql/${dbdriver}-custom_${item}.sql";
  325. if (-f "$filename") {
  326. $self->process_query($form, $dbh, $filename);
  327. }
  328. }
  329. $dbh->disconnect;
  330. }
  331. sub process_query {
  332. my ($self, $form, $dbh, $filename) = @_;
  333. return unless (-f $filename);
  334. open(FH, "$filename") or $form->error(__FILE__.':'.__LINE__.": $filename : $!\n");
  335. $ENV{PGPASSWORD} = $form->{dbpasswd};
  336. $ENV{PGUSER} = $form->{dbuser};
  337. $ENV{PGDATABASE} = $form->{db};
  338. open(PSQL, "| psql") or $form->error(__FILE__.':'.__LINE__.": psql : $! \n");
  339. print PSQL "\\o spool/log \n";
  340. while (<FH>){
  341. print PSQL $_;
  342. }
  343. close FH;
  344. }
  345. sub dbdelete {
  346. my ($self, $form) = @_;
  347. $form->{sid} = $form->{dbdefault};
  348. &dbconnect_vars($form, $form->{dbdefault});
  349. my $dbh = DBI->connect(
  350. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
  351. or $form->dberror(__FILE__.':'.__LINE__);
  352. my $query = qq|DROP DATABASE "$form->{db}"|;
  353. $dbh->do($query) || $form->dberror(__FILE__.':'.__LINE__.$query);
  354. $dbh->disconnect;
  355. }
  356. sub dbsources_unused {
  357. my ($self, $form, $memfile) = @_;
  358. my @dbexcl = ();
  359. my @dbsources = ();
  360. $form->error(__FILE__.':'.__LINE__.": $memfile locked!") if (-f "${memfile}.LCK");
  361. # open members file
  362. open(FH, "$memfile") or $form->error(__FILE__.':'.__LINE__.": $memfile : $!");
  363. while (<FH>) {
  364. if (/^dbname=/) {
  365. my ($null,$item) = split /=/;
  366. push @dbexcl, $item;
  367. }
  368. }
  369. close FH;
  370. $form->{only_acc_db} = 1;
  371. my @db = &dbsources("", $form);
  372. push @dbexcl, $form->{dbdefault};
  373. foreach $item (@db) {
  374. unless (grep /$item$/, @dbexcl) {
  375. push @dbsources, $item;
  376. }
  377. }
  378. return @dbsources;
  379. }
  380. sub dbneedsupdate {
  381. my ($self, $form) = @_;
  382. my %dbsources = ();
  383. my $query;
  384. $form->{sid} = $form->{dbdefault};
  385. &dbconnect_vars($form, $form->{dbdefault});
  386. my $dbh = DBI->connect(
  387. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
  388. or $form->dberror(__FILE__.':'.__LINE__);
  389. if ($form->{dbdriver} =~ /Pg/) {
  390. $query = qq|
  391. SELECT d.datname
  392. FROM pg_database d, pg_user u
  393. WHERE d.datdba = u.usesysid
  394. AND u.usename = ?|;
  395. my $sth = $dbh->prepare($query);
  396. $sth->execute($form->{dbuser}) || $form->dberror(__FILE__.':'.__LINE__.$query);
  397. while (my ($db) = $sth->fetchrow_array) {
  398. next if ($db =~ /^template/);
  399. &dbconnect_vars($form, $db);
  400. my $dbh = DBI->connect(
  401. $form->{dbconnect}, $form->{dbuser},
  402. $form->{dbpasswd})
  403. or $form->dberror(__FILE__.':'.__LINE__);
  404. $query = qq|
  405. SELECT tablename
  406. FROM pg_tables
  407. WHERE tablename = 'defaults'|;
  408. my $sth = $dbh->prepare($query);
  409. $sth->execute || $form->dberror(__FILE__.':'.__LINE__.$query);
  410. if ($sth->fetchrow_array) {
  411. $query = qq|
  412. SELECT value FROM defaults
  413. WHERE setting_key = 'version'|;
  414. my $sth = $dbh->prepare($query);
  415. $sth->execute;
  416. if (my ($version) = $sth->fetchrow_array) {
  417. $dbsources{$db} = $version;
  418. }
  419. $sth->finish;
  420. }
  421. $sth->finish;
  422. $dbh->disconnect;
  423. }
  424. $sth->finish;
  425. }
  426. $dbh->disconnect;
  427. %dbsources;
  428. }
  429. sub dbupdate {
  430. my ($self, $form) = @_;
  431. $form->{sid} = $form->{dbdefault};
  432. my @upgradescripts = ();
  433. my $query;
  434. my $rc = -2;
  435. if ($form->{dbupdate}) {
  436. # read update scripts into memory
  437. opendir SQLDIR, "sql/." or $form->error(__FILE__.':'.__LINE__.': '.$!);
  438. @upgradescripts =
  439. sort script_version
  440. grep /$form->{dbdriver}-upgrade-.*?\.sql$/,
  441. readdir SQLDIR;
  442. closedir SQLDIR;
  443. }
  444. foreach my $db (split / /, $form->{dbupdate}) {
  445. next unless $form->{$db};
  446. # strip db from dataset
  447. $db =~ s/^db//;
  448. &dbconnect_vars($form, $db);
  449. my $dbh = DBI->connect(
  450. $form->{dbconnect}, $form->{dbuser},
  451. $form->{dbpasswd}, {AutoCommit => 0})
  452. or $form->dberror(__FILE__.':'.__LINE__);
  453. # check version
  454. $query = qq|
  455. SELECT value FROM defaults
  456. WHERE setting_key = 'version'|;
  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 save_member {
  513. my ($self) = @_;
  514. # replace \r\n with \n
  515. for (qw(address signature)) { $self->{$_} =~ s/\r?\n/\\n/g }
  516. # use central db
  517. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  518. #check to see if the user exists already
  519. my $userCheck = $dbh->prepare("SELECT id FROM users WHERE username = ?");
  520. $userCheck->execute($self->{login});
  521. my ($userID) = $userCheck->fetchrow_array;
  522. if($userID){
  523. #got an id, check to see if it's in the users_conf table
  524. my $userConfCheck = $dbh->prepare("SELECT id FROM users_conf WHERE id = ?");
  525. $userConfCheck->execute($userID);
  526. if($userConfCheck->rows){
  527. my $userConfExists = 1;
  528. }
  529. }
  530. else{
  531. my $userConfAdd = $dbh->prepare("SELECT create_user(?);");
  532. $userConfAdd->execute($self->{login});
  533. ($userID) = $userConfAdd->fetchrow_array;
  534. }
  535. if($userConfExists){
  536. # for now, this is updating the table directly... ugly
  537. my $userConfUpdate = $dbh->prepare("UPDATE users_conf
  538. SET acs = ?, address = ?, businessnumber = ?,
  539. company = ?, countrycode = ?, currency = ?,
  540. dateformat = ?, dbdriver = ?,
  541. dbhost = ?, dbname = ?, dboptions = ?,
  542. dbpasswd = ?, dbport = ?, dbuser = ?,
  543. email = ?, fax = ?, menuwidth = ?,
  544. name = ?, numberformat = ?, password = md5(?),
  545. print = ?, printer = ?, role = ?,
  546. sid = ?, signature = ?, stylesheet = ?,
  547. tel = ?, templates = ?, timeout = ?,
  548. vclimit = ?
  549. WHERE id = ?;");
  550. $userConfUpdate->execute($self->{acs}, $self->{address}, $self->{businessnumber},
  551. $self->{company}, $self->{countrycode}, $self->{currency},
  552. $self->{dateformat}, $self->{dbdriver},
  553. $self->{dbhost}, $self->{dbname}, $self->{dboptions},
  554. $self->{dbpasswd}, $self->{dbport}, $self->{dbuser},
  555. $self->{email}, $self->{fax}, $self->{menuwidth},
  556. $self->{name}, $self->{numberformat}, $self->{password},
  557. $self->{print}, $self->{printer}, $self->{role},
  558. $self->{sid}, $self->{signature}, $self->{stylesheet},
  559. $self->{tel}, $self->{templates}, $self->{timeout},
  560. $self->{vclimit}, $userID);
  561. }
  562. else{
  563. my $userConfInsert = $dbh->prepare("INSERT INTO users_conf(acs, address, businessnumber,
  564. company, countrycode, currency,
  565. dateformat, dbdriver,
  566. dbhost, dbname, dboptions, dbpasswd,
  567. dbport, dbuser, email, fax, menuwidth,
  568. name, numberformat, print, printer, role,
  569. sid, signature, stylesheet, tel, templates,
  570. timeout, vclimit, id, password)
  571. VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  572. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  573. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, md5(?));");
  574. $userConfInsert->execute($self->{acs}, $self->{address}, $self->{businessnumber},
  575. $self->{company}, $self->{countrycode}, $self->{currency},
  576. $self->{dateformat}, $self->{dbdriver},
  577. $self->{dbhost}, $self->{dbname}, $self->{dboptions},
  578. $self->{dbpasswd}, $self->{dbport}, $self->{dbuser},
  579. $self->{email}, $self->{fax}, $self->{menuwidth},
  580. $self->{name}, $self->{numberformat},
  581. $self->{print}, $self->{printer}, $self->{role},
  582. $self->{sid}, $self->{signature}, $self->{stylesheet},
  583. $self->{tel}, $self->{templates}, $self->{timeout},
  584. $self->{vclimit}, $userID, $self->{password});
  585. }
  586. if (! $self->{'admin'}) {
  587. $self->{dbpasswd} =~ s/\\'/'/g;
  588. $self->{dbpasswd} =~ s/\\\\/\\/g;
  589. # format dbconnect and dboptions string
  590. &dbconnect_vars($self, $self->{dbname});
  591. # check if login is in database
  592. my $dbh = DBI->connect(
  593. $self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd},
  594. {AutoCommit => 0})
  595. or $self->error($DBI::errstr);
  596. # add login to employee table if it does not exist
  597. my $login = $self->{login};
  598. $login =~ s/@.*//;
  599. my $query = qq|SELECT id FROM employee WHERE login = '$login'|;
  600. my $sth = $dbh->prepare($query);
  601. $sth->execute;
  602. my ($id) = $sth->fetchrow_array;
  603. $sth->finish;
  604. my $employeenumber;
  605. my @values;
  606. if ($id) {
  607. $query = qq|UPDATE employee SET
  608. role = ?,
  609. email = ?,
  610. name = ?
  611. WHERE login = ?|;
  612. @values = ($self->{role}, $self->{email}, $self->{name}, $login);
  613. } else {
  614. my ($employeenumber) = Form::update_defaults(
  615. "", \%$self, "employeenumber", $dbh);
  616. $query = qq|
  617. INSERT INTO employee
  618. (login, employeenumber, name,
  619. workphone, role, email, sales)
  620. VALUES (?, ?, ?, ?, ?, ?, '1')|;
  621. @values = ($login, $employeenumber, $self->{name}, $self->{tel},
  622. $self->{role}, $self->{email})
  623. }
  624. $sth = $dbh->prepare($query);
  625. $sth->execute(@values);
  626. $dbh->commit;
  627. $dbh->disconnect;
  628. }
  629. }
  630. sub delete_login {
  631. my ($self, $form) = @_;
  632. my $dbh = DBI->connect(
  633. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd},
  634. {AutoCommit => 0})
  635. or $form->dberror(__FILE__.':'.__LINE__);
  636. my $login = $form->{login};
  637. $login =~ s/@.*//;
  638. my $query = qq|SELECT id FROM employee WHERE login = ?|;
  639. my $sth = $dbh->prepare($query);
  640. $sth->execute($login) || $form->dberror(__FILE__.':'.__LINE__.': '.$query);
  641. my ($id) = $sth->fetchrow_array;
  642. $sth->finish;
  643. my $query = qq|
  644. UPDATE employee
  645. SET login = NULL,
  646. enddate = current_date
  647. WHERE login = ?|;
  648. $sth = $dbh->prepare($query);
  649. $sth->execute($login);
  650. $dbh->commit;
  651. $dbh->disconnect;
  652. }
  653. sub config_vars {
  654. my @conf =
  655. qw(acs address businessnumber company countrycode
  656. currency dateformat dbconnect dbdriver dbhost dbname dboptions
  657. dbpasswd dbport dbuser email fax menuwidth name numberformat
  658. password printer role sid signature stylesheet tel templates
  659. timeout vclimit);
  660. @conf;
  661. }
  662. sub error {
  663. my ($self, $msg) = @_;
  664. if ($ENV{HTTP_USER_AGENT}) {
  665. print qq|Content-Type: text/html\n\n|.
  666. qq|<body bgcolor=ffffff>\n\n|.
  667. qq|<h2><font color=red>Error!</font></h2>\n|.
  668. qq|<p><b>$msg</b>|;
  669. }
  670. die "Error: $msg\n";
  671. }
  672. 1;