summaryrefslogtreecommitdiff
path: root/LedgerSMB/User.pm
blob: 1427ad535d18959f9a6f6c98dd2b6e89b07bb1cd (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. if (! $myconfig{'dbhost'}){ # necessary to avoid many issues
  118. $myconfig{'dbhost'} = 'localhost';
  119. }
  120. $myconfig{'login'} = $login;
  121. $myconfig{'dbconnect'} = 'dbi:Pg:dbname='.$myconfig{'dbname'}.';host='.$myconfig{'dbhost'}.';port='.$myconfig{'dbport'};
  122. }
  123. return \%myconfig;
  124. }
  125. sub login {
  126. my ($self, $form) = @_;
  127. my $rc = -1;
  128. if ($self->{login} ne "") {
  129. if (! Session::password_check($form, $form->{login}, $form->{password})) {
  130. return -1;
  131. }
  132. #this is really dumb, but %myconfig will have to stay until 1.3
  133. while ( my ($key, $value) = each(%{$self}) ) {
  134. $myconfig{$key} = $value;
  135. }
  136. # check if database is down
  137. my $dbh = DBI->connect(
  138. $myconfig{dbconnect}, $myconfig{dbuser},
  139. $myconfig{dbpasswd})
  140. or $self->error(__FILE__.':'.__LINE__.': '.$DBI::errstr);
  141. # we got a connection, check the version
  142. my $query = qq|
  143. SELECT value FROM defaults
  144. WHERE setting_key = 'version'|;
  145. my $sth = $dbh->prepare($query);
  146. $sth->execute || $form->dberror(__FILE__.':'.__LINE__.$query);
  147. my ($dbversion) = $sth->fetchrow_array;
  148. $sth->finish;
  149. # add login to employee table if it does not exist
  150. # no error check for employee table, ignore if it does not exist
  151. my $login = $self->{login};
  152. $login =~ s/@.*//;
  153. $query = qq|SELECT id FROM employee WHERE login = ?|;
  154. $sth = $dbh->prepare($query);
  155. $sth->execute($login);
  156. my ($id) = $sth->fetchrow_array;
  157. $sth->finish;
  158. if (! $id) {
  159. my ($employeenumber) =
  160. $form->update_defaults(
  161. \%myconfig, "employeenumber", $dbh);
  162. $query = qq|
  163. INSERT INTO employee
  164. (login, employeenumber, name,
  165. workphone, role)
  166. VALUES (?, ?, ?, ?, ?)|;
  167. $sth = $dbh->prepare($query);
  168. $sth->execute(
  169. $login, $employeenumber, $myconfig{name},
  170. $myconfig{tel}, $myconfig{role});
  171. }
  172. $dbh->disconnect;
  173. $rc = 0;
  174. if ($form->{dbversion} ne $dbversion) {
  175. $rc = -3;
  176. $dbupdate = (calc_version($dbversion)
  177. < calc_version($form->{dbversion}));
  178. }
  179. if ($dbupdate) {
  180. $rc = -4;
  181. # if DB2 bale out
  182. if ($myconfig{dbdriver} eq 'DB2') {
  183. $rc = -2;
  184. }
  185. }
  186. }
  187. $rc;
  188. }
  189. sub check_recurring {
  190. my ($self, $form) = @_;
  191. my $dbh = DBI->connect(
  192. $self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd})
  193. or $form->dberror(__FILE__.':'.__LINE__);
  194. my $query = qq|
  195. SELECT count(*) FROM recurring
  196. WHERE enddate >= current_date AND nextdate <= current_date|;
  197. ($_) = $dbh->selectrow_array($query);
  198. $dbh->disconnect;
  199. $_;
  200. }
  201. sub dbconnect_vars {
  202. my ($form, $db) = @_;
  203. my %dboptions = (
  204. 'Pg' => {
  205. 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
  206. 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
  207. 'mm-dd-yy' => 'set DateStyle to \'POSTGRES, US\'',
  208. 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
  209. 'dd-mm-yy' => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
  210. 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
  211. }
  212. );
  213. $form->{dboptions} = $dboptions{$form->{dbdriver}}{$form->{dateformat}};
  214. $form->{dbconnect} = "dbi:$form->{dbdriver}:dbname=$db";
  215. if ($form->{dbhost}) {
  216. $form->{dbconnect} .= ";host=$form->{dbhost}";
  217. } else {
  218. $form->{dbconnect} .= ";host=localhost";
  219. }
  220. if ($form->{dbport}) {
  221. $form->{dbconnect} .= ";port=$form->{dbport}";
  222. } else {
  223. $form->{dbconnect} .= ";port=5432";
  224. }
  225. }
  226. sub dbdrivers {
  227. my @drivers = DBI->available_drivers();
  228. # return (grep { /(Pg|Oracle|DB2)/ } @drivers);
  229. return (grep { /Pg$/ } @drivers);
  230. }
  231. sub dbsources {
  232. my ($self, $form) = @_;
  233. my @dbsources = ();
  234. my ($sth, $query);
  235. $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
  236. $form->{sid} = $form->{dbdefault};
  237. &dbconnect_vars($form, $form->{dbdefault});
  238. my $dbh = DBI->connect(
  239. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
  240. or $form->dberror(__FILE__.':'.__LINE__);
  241. if ($form->{dbdriver} eq 'Pg') {
  242. $query = qq|SELECT datname FROM pg_database|;
  243. $sth = $dbh->prepare($query);
  244. $sth->execute || $form->dberror(__FILE__.':'.__LINE__.$query);
  245. while (my ($db) = $sth->fetchrow_array) {
  246. if ($form->{only_acc_db}) {
  247. next if ($db =~ /^template/);
  248. &dbconnect_vars($form, $db);
  249. my $dbh = DBI->connect(
  250. $form->{dbconnect}, $form->{dbuser},
  251. $form->{dbpasswd})
  252. or $form->dberror(__FILE__.':'.__LINE__);
  253. $query = qq|
  254. SELECT tablename FROM pg_tables
  255. WHERE tablename = 'defaults'
  256. AND tableowner = ?|;
  257. my $sth = $dbh->prepare($query);
  258. $sth->execute($form->{dbuser})
  259. || $form->dberror(__FILE__.':'.__LINE__.$query);
  260. if ($sth->fetchrow_array) {
  261. push @dbsources, $db;
  262. }
  263. $sth->finish;
  264. $dbh->disconnect;
  265. next;
  266. }
  267. push @dbsources, $db;
  268. }
  269. }
  270. $sth->finish;
  271. $dbh->disconnect;
  272. return @dbsources;
  273. }
  274. sub dbcreate {
  275. my ($self, $form) = @_;
  276. my %dbcreate = ( 'Pg' => qq|CREATE DATABASE "$form->{db}"| );
  277. $dbcreate{Pg} .= " WITH ENCODING = '$form->{encoding}'"
  278. if $form->{encoding};
  279. $form->{sid} = $form->{dbdefault};
  280. &dbconnect_vars($form, $form->{dbdefault});
  281. # The below line connects to Template1 or another template file in order
  282. # to create the db. One must disconnect and reconnect later.
  283. if ($form->{dbsuperuser}){
  284. my $superdbh = DBI->connect(
  285. $form->{dbconnect},
  286. $form->{dbsuperuser},
  287. $form->{dbsuperpasswd})
  288. or $form->dberror(__FILE__.':'.__LINE__);
  289. my $query = qq|$dbcreate{$form->{dbdriver}}|;
  290. $superdbh->do($query) || $form->dberror(__FILE__.':'.__LINE__.$query);
  291. $superdbh->disconnect;
  292. }
  293. #Reassign for the work below
  294. &dbconnect_vars($form, $form->{db});
  295. my $dbh = DBI->connect(
  296. $form->{dbconnect},
  297. $form->{dbuser},
  298. $form->{dbpasswd})
  299. or $form->dberror(__FILE__.':'.__LINE__);
  300. if ($form->{dbsuperuser}){
  301. my $superdbh = DBI->connect(
  302. $form->{dbconnect},
  303. $form->{dbsuperuser},
  304. $form->{dbsuperpasswd})
  305. or $form->dberror(__FILE__.':'.__LINE__);
  306. # JD: We need to check for plpgsql,
  307. # if it isn't there create it, if we can't error
  308. # Good chance I will have to do this twice as I get
  309. # used to the way the code is structured
  310. my %langcreate = ( 'Pg' => qq|CREATE LANGUAGE plpgsql|);
  311. my $query = qq|$langcreate{$form->{dbdriver}}|;
  312. $superdbh->do($query);
  313. $superdbh->disconnect;
  314. }
  315. # create the tables
  316. my $dbdriver =
  317. ($form->{dbdriver} =~ /Pg/)
  318. ? 'Pg'
  319. : $form->{dbdriver};
  320. my $filename = qq|sql/Pg-database.sql|;
  321. $self->process_query($form, $dbh, $filename);
  322. # load gifi
  323. ($filename) = split /_/, $form->{chart};
  324. $filename =~ s/_//;
  325. $self->process_query($form, $dbh, "sql/${filename}-gifi.sql");
  326. # load chart of accounts
  327. $filename = qq|sql/$form->{chart}-chart.sql|;
  328. $self->process_query($form, $dbh, $filename);
  329. # create custom tables and functions
  330. my $item;
  331. foreach $item (qw(tables functions)) {
  332. $filename = "sql/${dbdriver}-custom_${item}.sql";
  333. if (-f "$filename") {
  334. $self->process_query($form, $dbh, $filename);
  335. }
  336. }
  337. $dbh->disconnect;
  338. }
  339. sub process_query {
  340. my ($self, $form, $dbh, $filename) = @_;
  341. return unless (-f $filename);
  342. open(FH, "$filename") or $form->error(__FILE__.':'.__LINE__.": $filename : $!\n");
  343. $ENV{PGPASSWORD} = $form->{dbpasswd};
  344. $ENV{PGUSER} = $form->{dbuser};
  345. $ENV{PGDATABASE} = $form->{db};
  346. open(PSQL, "| psql") or $form->error(__FILE__.':'.__LINE__.": psql : $! \n");
  347. print PSQL "\\o spool/log \n";
  348. print PSQL "BEGIN;\n";
  349. while (<FH>){
  350. print PSQL $_;
  351. }
  352. print PSQL "COMMIT;\n";
  353. close FH;
  354. }
  355. sub dbdelete {
  356. my ($self, $form) = @_;
  357. $form->{sid} = $form->{dbdefault};
  358. &dbconnect_vars($form, $form->{dbdefault});
  359. my $dbh = DBI->connect(
  360. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
  361. or $form->dberror(__FILE__.':'.__LINE__);
  362. my $query = qq|DROP DATABASE "$form->{db}"|;
  363. $dbh->do($query) || $form->dberror(__FILE__.':'.__LINE__.$query);
  364. $dbh->disconnect;
  365. }
  366. sub dbsources_unused {
  367. my ($self, $form, $memfile) = @_;
  368. my @dbexcl = ();
  369. my @dbsources = ();
  370. $form->error(__FILE__.':'.__LINE__.": $memfile locked!") if (-f "${memfile}.LCK");
  371. # open members file
  372. open(FH, "$memfile") or $form->error(__FILE__.':'.__LINE__.": $memfile : $!");
  373. while (<FH>) {
  374. if (/^dbname=/) {
  375. my ($null,$item) = split /=/;
  376. push @dbexcl, $item;
  377. }
  378. }
  379. close FH;
  380. $form->{only_acc_db} = 1;
  381. my @db = &dbsources("", $form);
  382. push @dbexcl, $form->{dbdefault};
  383. foreach $item (@db) {
  384. unless (grep /$item$/, @dbexcl) {
  385. push @dbsources, $item;
  386. }
  387. }
  388. return @dbsources;
  389. }
  390. sub dbneedsupdate {
  391. my ($self, $form) = @_;
  392. my %dbsources = ();
  393. my $query;
  394. $form->{sid} = $form->{dbdefault};
  395. &dbconnect_vars($form, $form->{dbdefault});
  396. my $dbh = DBI->connect(
  397. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
  398. or $form->dberror(__FILE__.':'.__LINE__);
  399. if ($form->{dbdriver} =~ /Pg/) {
  400. $query = qq|
  401. SELECT d.datname
  402. FROM pg_database d, pg_user u
  403. WHERE d.datdba = u.usesysid
  404. AND u.usename = ?|;
  405. my $sth = $dbh->prepare($query);
  406. $sth->execute($form->{dbuser}) || $form->dberror(__FILE__.':'.__LINE__.$query);
  407. while (my ($db) = $sth->fetchrow_array) {
  408. next if ($db =~ /^template/);
  409. &dbconnect_vars($form, $db);
  410. my $dbh = DBI->connect(
  411. $form->{dbconnect}, $form->{dbuser},
  412. $form->{dbpasswd})
  413. or $form->dberror(__FILE__.':'.__LINE__);
  414. $query = qq|
  415. SELECT tablename
  416. FROM pg_tables
  417. WHERE tablename = 'defaults'|;
  418. my $sth = $dbh->prepare($query);
  419. $sth->execute || $form->dberror(__FILE__.':'.__LINE__.$query);
  420. if ($sth->fetchrow_array) {
  421. $query = qq|
  422. SELECT value FROM defaults
  423. WHERE setting_key = 'version'|;
  424. my $sth = $dbh->prepare($query);
  425. $sth->execute;
  426. if (my ($version) = $sth->fetchrow_array) {
  427. $dbsources{$db} = $version;
  428. }
  429. $sth->finish;
  430. }
  431. $sth->finish;
  432. $dbh->disconnect;
  433. }
  434. $sth->finish;
  435. }
  436. $dbh->disconnect;
  437. %dbsources;
  438. }
  439. sub dbupdate {
  440. my ($self, $form) = @_;
  441. $form->{sid} = $form->{dbdefault};
  442. my @upgradescripts = ();
  443. my $query;
  444. my $rc = -2;
  445. if ($form->{dbupdate}) {
  446. # read update scripts into memory
  447. opendir SQLDIR, "sql/." or $form->error(__FILE__.':'.__LINE__.': '.$!);
  448. @upgradescripts =
  449. sort script_version
  450. grep /$form->{dbdriver}-upgrade-.*?\.sql$/,
  451. readdir SQLDIR;
  452. closedir SQLDIR;
  453. }
  454. foreach my $db (split / /, $form->{dbupdate}) {
  455. next unless $form->{$db};
  456. # strip db from dataset
  457. $db =~ s/^db//;
  458. &dbconnect_vars($form, $db);
  459. my $dbh = DBI->connect(
  460. $form->{dbconnect}, $form->{dbuser},
  461. $form->{dbpasswd}, {AutoCommit => 0})
  462. or $form->dberror(__FILE__.':'.__LINE__);
  463. # check version
  464. $query = qq|
  465. SELECT value FROM defaults
  466. WHERE setting_key = 'version'|;
  467. my $sth = $dbh->prepare($query);
  468. # no error check, let it fall through
  469. $sth->execute;
  470. my $version = $sth->fetchrow_array;
  471. $sth->finish;
  472. next unless $version;
  473. $version = calc_version($version);
  474. my $dbversion = calc_version($form->{dbversion});
  475. foreach my $upgradescript (@upgradescripts) {
  476. my $a = $upgradescript;
  477. $a =~ s/(^$form->{dbdriver}-upgrade-|\.sql$)//g;
  478. my ($mindb, $maxdb) = split /-/, $a;
  479. $mindb = calc_version($mindb);
  480. $maxdb = calc_version($maxdb);
  481. next if ($version >= $maxdb);
  482. # exit if there is no upgrade script or version == mindb
  483. last if ($version < $mindb || $version >= $dbversion);
  484. # apply upgrade
  485. $self->process_query($form, $dbh, "sql/$upgradescript");
  486. $dbh->commit;
  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 save_member {
  523. my ($self) = @_;
  524. # replace \r\n with \n
  525. for (qw(address signature)) { $self->{$_} =~ s/\r?\n/\\n/g }
  526. # use central db
  527. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  528. #check to see if the user exists already
  529. my $userCheck = $dbh->prepare("SELECT id FROM users WHERE username = ?");
  530. $userCheck->execute($self->{login});
  531. my ($userID) = $userCheck->fetchrow_array;
  532. my $userConfExists = 0;
  533. if($userID){
  534. #got an id, check to see if it's in the users_conf table
  535. my $userConfCheck = $dbh->prepare("SELECT password, 1 FROM users_conf WHERE id = ?");
  536. $userConfCheck->execute($userID);
  537. ($oldPassword, $userConfExists) = $userConfCheck->fetchrow_array;
  538. }
  539. else{
  540. my $userConfAdd = $dbh->prepare("SELECT create_user(?);");
  541. $userConfAdd->execute($self->{login});
  542. ($userID) = $userConfAdd->fetchrow_array;
  543. }
  544. if($userConfExists){
  545. # for now, this is updating the table directly... ugly
  546. my $userConfUpdate = $dbh->prepare("UPDATE users_conf
  547. SET acs = ?, address = ?, businessnumber = ?,
  548. company = ?, countrycode = ?, currency = ?,
  549. dateformat = ?, dbdriver = ?,
  550. dbhost = ?, dbname = ?, dboptions = ?,
  551. dbpasswd = ?, dbport = ?, dbuser = ?,
  552. email = ?, fax = ?, menuwidth = ?,
  553. name = ?, numberformat = ?,
  554. print = ?, printer = ?, role = ?,
  555. sid = ?, signature = ?, stylesheet = ?,
  556. tel = ?, templates = ?, timeout = ?,
  557. vclimit = ?
  558. WHERE id = ?;");
  559. $userConfUpdate->execute($self->{acs}, $self->{address}, $self->{businessnumber},
  560. $self->{company}, $self->{countrycode}, $self->{currency},
  561. $self->{dateformat}, $self->{dbdriver},
  562. $self->{dbhost}, $self->{dbname}, $self->{dboptions},
  563. $self->{dbpasswd}, $self->{dbport}, $self->{dbuser},
  564. $self->{email}, $self->{fax}, $self->{menuwidth},
  565. $self->{name}, $self->{numberformat},
  566. $self->{print}, $self->{printer}, $self->{role},
  567. $self->{sid}, $self->{signature}, $self->{stylesheet},
  568. $self->{tel}, $self->{templates}, $self->{timeout},
  569. $self->{vclimit}, $userID);
  570. if($oldPassword ne $self->{password}){
  571. # if they're supplying a 32 char password that matches their old password
  572. # assume they don't want to change passwords
  573. $userConfUpdate = $dbh->prepare("UPDATE users_conf
  574. SET password = md5(?)
  575. WHERE id = ?");
  576. $userConfUpdate->execute($self->{password}, $userID);
  577. }
  578. }
  579. else{
  580. my $userConfInsert = $dbh->prepare("INSERT INTO users_conf(acs, address, businessnumber,
  581. company, countrycode, currency,
  582. dateformat, dbdriver,
  583. dbhost, dbname, dboptions, dbpasswd,
  584. dbport, dbuser, email, fax, menuwidth,
  585. name, numberformat, print, printer, role,
  586. sid, signature, stylesheet, tel, templates,
  587. timeout, vclimit, id, password)
  588. VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  589. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  590. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, md5(?));");
  591. $userConfInsert->execute($self->{acs}, $self->{address}, $self->{businessnumber},
  592. $self->{company}, $self->{countrycode}, $self->{currency},
  593. $self->{dateformat}, $self->{dbdriver},
  594. $self->{dbhost}, $self->{dbname}, $self->{dboptions},
  595. $self->{dbpasswd}, $self->{dbport}, $self->{dbuser},
  596. $self->{email}, $self->{fax}, $self->{menuwidth},
  597. $self->{name}, $self->{numberformat},
  598. $self->{print}, $self->{printer}, $self->{role},
  599. $self->{sid}, $self->{signature}, $self->{stylesheet},
  600. $self->{tel}, $self->{templates}, $self->{timeout},
  601. $self->{vclimit}, $userID, $self->{password});
  602. }
  603. if (! $self->{'admin'}) {
  604. $self->{dbpasswd} =~ s/\\'/'/g;
  605. $self->{dbpasswd} =~ s/\\\\/\\/g;
  606. # format dbconnect and dboptions string
  607. &dbconnect_vars($self, $self->{dbname});
  608. # check if login is in database
  609. my $dbh = DBI->connect(
  610. $self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd},
  611. {AutoCommit => 0})
  612. or $self->error($DBI::errstr);
  613. # add login to employee table if it does not exist
  614. my $login = $self->{login};
  615. $login =~ s/@.*//;
  616. my $sth = $dbh->prepare("SELECT id FROM employee WHERE login = ?;");
  617. $sth->execute($login);
  618. my ($id) = $sth->fetchrow_array;
  619. $sth->finish;
  620. my $employeenumber;
  621. my @values;
  622. if ($id) {
  623. $query = qq|UPDATE employee SET
  624. role = ?,
  625. email = ?,
  626. name = ?
  627. WHERE login = ?|;
  628. @values = ($self->{role}, $self->{email}, $self->{name}, $login);
  629. } else {
  630. my ($employeenumber) = Form::update_defaults(
  631. "", \%$self, "employeenumber", $dbh);
  632. $query = qq|
  633. INSERT INTO employee
  634. (login, employeenumber, name,
  635. workphone, role, email, sales)
  636. VALUES (?, ?, ?, ?, ?, ?, '1')|;
  637. @values = ($login, $employeenumber, $self->{name}, $self->{tel},
  638. $self->{role}, $self->{email})
  639. }
  640. $sth = $dbh->prepare($query);
  641. $sth->execute(@values);
  642. $dbh->commit;
  643. $dbh->disconnect;
  644. }
  645. }
  646. sub delete_login {
  647. my ($self, $form) = @_;
  648. my $dbh = DBI->connect(
  649. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd},
  650. {AutoCommit => 0})
  651. or $form->dberror(__FILE__.':'.__LINE__);
  652. my $login = $form->{login};
  653. $login =~ s/@.*//;
  654. my $query = qq|SELECT id FROM employee WHERE login = ?|;
  655. my $sth = $dbh->prepare($query);
  656. $sth->execute($login) || $form->dberror(__FILE__.':'.__LINE__.': '.$query);
  657. my ($id) = $sth->fetchrow_array;
  658. $sth->finish;
  659. my $query = qq|
  660. UPDATE employee
  661. SET login = NULL,
  662. enddate = current_date
  663. WHERE login = ?|;
  664. $sth = $dbh->prepare($query);
  665. $sth->execute($login);
  666. $dbh->commit;
  667. $dbh->disconnect;
  668. }
  669. sub config_vars {
  670. my @conf =
  671. qw(acs address businessnumber company countrycode
  672. currency dateformat dbconnect dbdriver dbhost dbname dboptions
  673. dbpasswd dbport dbuser email fax menuwidth name numberformat
  674. password printer role sid signature stylesheet tel templates
  675. timeout vclimit);
  676. @conf;
  677. }
  678. sub error {
  679. my ($self, $msg) = @_;
  680. if ($ENV{HTTP_USER_AGENT}) {
  681. print qq|Content-Type: text/html\n\n|.
  682. qq|<body bgcolor=ffffff>\n\n|.
  683. qq|<h2><font color=red>Error!</font></h2>\n|.
  684. qq|<p><b>$msg</b>|;
  685. }
  686. die "Error: $msg\n";
  687. }
  688. 1;