summaryrefslogtreecommitdiff
path: root/LedgerSMB/User.pm
blob: e28af0722490faaf5bb7e2a02c7835de3254a68f (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}" WITH ENCODING = 'UNICODE'| );
  277. $form->{sid} = $form->{dbdefault};
  278. &dbconnect_vars($form, $form->{dbdefault});
  279. # The below line connects to Template1 or another template file in order
  280. # to create the db. One must disconnect and reconnect later.
  281. if ($form->{dbsuperuser}){
  282. my $superdbh = DBI->connect(
  283. $form->{dbconnect},
  284. $form->{dbsuperuser},
  285. $form->{dbsuperpasswd})
  286. or $form->dberror(__FILE__.':'.__LINE__);
  287. my $query = qq|$dbcreate{$form->{dbdriver}}|;
  288. $superdbh->do($query) || $form->dberror(__FILE__.':'.__LINE__.$query);
  289. $superdbh->disconnect;
  290. }
  291. #Reassign for the work below
  292. &dbconnect_vars($form, $form->{db});
  293. my $dbh = DBI->connect(
  294. $form->{dbconnect},
  295. $form->{dbuser},
  296. $form->{dbpasswd})
  297. or $form->dberror(__FILE__.':'.__LINE__);
  298. if ($form->{dbsuperuser}){
  299. my $superdbh = DBI->connect(
  300. $form->{dbconnect},
  301. $form->{dbsuperuser},
  302. $form->{dbsuperpasswd})
  303. or $form->dberror(__FILE__.':'.__LINE__);
  304. # JD: We need to check for plpgsql,
  305. # if it isn't there create it, if we can't error
  306. # Good chance I will have to do this twice as I get
  307. # used to the way the code is structured
  308. my %langcreate = ( 'Pg' => qq|CREATE LANGUAGE plpgsql|);
  309. my $query = qq|$langcreate{$form->{dbdriver}}|;
  310. $superdbh->do($query);
  311. $superdbh->disconnect;
  312. }
  313. # create the tables
  314. my $dbdriver =
  315. ($form->{dbdriver} =~ /Pg/)
  316. ? 'Pg'
  317. : $form->{dbdriver};
  318. my $filename = qq|sql/Pg-database.sql|;
  319. $self->process_query($form, $dbh, $filename);
  320. # load gifi
  321. ($filename) = split /_/, $form->{chart};
  322. $filename =~ s/_//;
  323. $self->process_query($form, $dbh, "sql/${filename}-gifi.sql");
  324. # load chart of accounts
  325. $filename = qq|sql/$form->{chart}-chart.sql|;
  326. $self->process_query($form, $dbh, $filename);
  327. # create custom tables and functions
  328. my $item;
  329. foreach $item (qw(tables functions)) {
  330. $filename = "sql/${dbdriver}-custom_${item}.sql";
  331. if (-f "$filename") {
  332. $self->process_query($form, $dbh, $filename);
  333. }
  334. }
  335. $dbh->disconnect;
  336. }
  337. sub process_query {
  338. my ($self, $form, $dbh, $filename) = @_;
  339. return unless (-f $filename);
  340. $ENV{PGPASSWORD} = $form->{dbpasswd};
  341. $ENV{PGUSER} = $form->{dbuser};
  342. $ENV{PGDATABASE} = $form->{db};
  343. $results = `psql -f $filename 2>&1`;
  344. if ($?){
  345. $form->error($!);
  346. }
  347. elsif ($results =~ /error/i){
  348. $form->error($results);
  349. }
  350. }
  351. sub dbdelete {
  352. my ($self, $form) = @_;
  353. $form->{sid} = $form->{dbdefault};
  354. &dbconnect_vars($form, $form->{dbdefault});
  355. my $dbh = DBI->connect(
  356. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
  357. or $form->dberror(__FILE__.':'.__LINE__);
  358. my $query = qq|DROP DATABASE "$form->{db}"|;
  359. $dbh->do($query) || $form->dberror(__FILE__.':'.__LINE__.$query);
  360. $dbh->disconnect;
  361. }
  362. sub dbsources_unused {
  363. my ($self, $form, $memfile) = @_;
  364. my @dbexcl = ();
  365. my @dbsources = ();
  366. $form->error(__FILE__.':'.__LINE__.": $memfile locked!") if (-f "${memfile}.LCK");
  367. # open members file
  368. open(FH, "$memfile") or $form->error(__FILE__.':'.__LINE__.": $memfile : $!");
  369. while (<FH>) {
  370. if (/^dbname=/) {
  371. my ($null,$item) = split /=/;
  372. push @dbexcl, $item;
  373. }
  374. }
  375. close FH;
  376. $form->{only_acc_db} = 1;
  377. my @db = &dbsources("", $form);
  378. push @dbexcl, $form->{dbdefault};
  379. foreach $item (@db) {
  380. unless (grep /$item$/, @dbexcl) {
  381. push @dbsources, $item;
  382. }
  383. }
  384. return @dbsources;
  385. }
  386. sub dbneedsupdate {
  387. my ($self, $form) = @_;
  388. my %dbsources = ();
  389. my $query;
  390. $form->{sid} = $form->{dbdefault};
  391. &dbconnect_vars($form, $form->{dbdefault});
  392. my $dbh = DBI->connect(
  393. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
  394. or $form->dberror(__FILE__.':'.__LINE__);
  395. if ($form->{dbdriver} =~ /Pg/) {
  396. $query = qq|
  397. SELECT d.datname
  398. FROM pg_database d, pg_user u
  399. WHERE d.datdba = u.usesysid
  400. AND u.usename = ?|;
  401. my $sth = $dbh->prepare($query);
  402. $sth->execute($form->{dbuser}) || $form->dberror(__FILE__.':'.__LINE__.$query);
  403. while (my ($db) = $sth->fetchrow_array) {
  404. next if ($db =~ /^template/);
  405. &dbconnect_vars($form, $db);
  406. my $dbh = DBI->connect(
  407. $form->{dbconnect}, $form->{dbuser},
  408. $form->{dbpasswd})
  409. or $form->dberror(__FILE__.':'.__LINE__);
  410. $query = qq|
  411. SELECT tablename
  412. FROM pg_tables
  413. WHERE tablename = 'defaults'|;
  414. my $sth = $dbh->prepare($query);
  415. $sth->execute || $form->dberror(__FILE__.':'.__LINE__.$query);
  416. if ($sth->fetchrow_array) {
  417. $query = qq|
  418. SELECT value FROM defaults
  419. WHERE setting_key = 'version'|;
  420. my $sth = $dbh->prepare($query);
  421. $sth->execute;
  422. if (my ($version) = $sth->fetchrow_array) {
  423. $dbsources{$db} = $version;
  424. }
  425. $sth->finish;
  426. }
  427. $sth->finish;
  428. $dbh->disconnect;
  429. }
  430. $sth->finish;
  431. }
  432. $dbh->disconnect;
  433. %dbsources;
  434. }
  435. sub dbupdate {
  436. my ($self, $form) = @_;
  437. $form->{sid} = $form->{dbdefault};
  438. my @upgradescripts = ();
  439. my $query;
  440. my $rc = -2;
  441. if ($form->{dbupdate}) {
  442. # read update scripts into memory
  443. opendir SQLDIR, "sql/." or $form->error(__FILE__.':'.__LINE__.': '.$!);
  444. @upgradescripts =
  445. sort script_version
  446. grep /$form->{dbdriver}-upgrade-.*?\.sql$/,
  447. readdir SQLDIR;
  448. closedir SQLDIR;
  449. }
  450. foreach my $db (split / /, $form->{dbupdate}) {
  451. next unless $form->{$db};
  452. # strip db from dataset
  453. $db =~ s/^db//;
  454. &dbconnect_vars($form, $db);
  455. my $dbh = DBI->connect(
  456. $form->{dbconnect}, $form->{dbuser},
  457. $form->{dbpasswd}, {AutoCommit => 0})
  458. or $form->dberror(__FILE__.':'.__LINE__);
  459. # check version
  460. $query = qq|
  461. SELECT value FROM defaults
  462. WHERE setting_key = 'version'|;
  463. my $sth = $dbh->prepare($query);
  464. # no error check, let it fall through
  465. $sth->execute;
  466. my $version = $sth->fetchrow_array;
  467. $sth->finish;
  468. next unless $version;
  469. $version = calc_version($version);
  470. my $dbversion = calc_version($form->{dbversion});
  471. foreach my $upgradescript (@upgradescripts) {
  472. my $a = $upgradescript;
  473. $a =~ s/(^$form->{dbdriver}-upgrade-|\.sql$)//g;
  474. my ($mindb, $maxdb) = split /-/, $a;
  475. $mindb = calc_version($mindb);
  476. $maxdb = calc_version($maxdb);
  477. next if ($version >= $maxdb);
  478. # exit if there is no upgrade script or version == mindb
  479. last if ($version < $mindb || $version >= $dbversion);
  480. # apply upgrade
  481. $self->process_query($form, $dbh, "sql/$upgradescript");
  482. $dbh->commit;
  483. $version = $maxdb;
  484. }
  485. $rc = 0;
  486. $dbh->disconnect;
  487. }
  488. $rc;
  489. }
  490. sub calc_version {
  491. my @v = split /\./, $_[0];
  492. my $version = 0;
  493. my $i;
  494. for ($i = 0; $i <= $#v; $i++) {
  495. $version *= 1000;
  496. $version += $v[$i];
  497. }
  498. return $version;
  499. }
  500. sub script_version {
  501. my ($my_a, $my_b) = ($a, $b);
  502. my ($a_from, $a_to, $b_from, $b_to);
  503. my ($res_a, $res_b, $i);
  504. $my_a =~ s/.*-upgrade-//;
  505. $my_a =~ s/.sql$//;
  506. $my_b =~ s/.*-upgrade-//;
  507. $my_b =~ s/.sql$//;
  508. ($a_from, $a_to) = split(/-/, $my_a);
  509. ($b_from, $b_to) = split(/-/, $my_b);
  510. $res_a = calc_version($a_from);
  511. $res_b = calc_version($b_from);
  512. if ($res_a == $res_b) {
  513. $res_a = calc_version($a_to);
  514. $res_b = calc_version($b_to);
  515. }
  516. return $res_a <=> $res_b;
  517. }
  518. sub save_member {
  519. my ($self) = @_;
  520. # replace \r\n with \n
  521. for (qw(address signature)) { $self->{$_} =~ s/\r?\n/\\n/g }
  522. # use central db
  523. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  524. #check to see if the user exists already
  525. my $userCheck = $dbh->prepare("SELECT id FROM users WHERE username = ?");
  526. $userCheck->execute($self->{login});
  527. my ($userID) = $userCheck->fetchrow_array;
  528. my $userConfExists = 0;
  529. if($userID){
  530. #got an id, check to see if it's in the users_conf table
  531. my $userConfCheck = $dbh->prepare("SELECT password, 1 FROM users_conf WHERE id = ?");
  532. $userConfCheck->execute($userID);
  533. ($oldPassword, $userConfExists) = $userConfCheck->fetchrow_array;
  534. }
  535. else{
  536. my $userConfAdd = $dbh->prepare("SELECT create_user(?);");
  537. $userConfAdd->execute($self->{login});
  538. ($userID) = $userConfAdd->fetchrow_array;
  539. }
  540. if($userConfExists){
  541. # for now, this is updating the table directly... ugly
  542. my $userConfUpdate = $dbh->prepare("UPDATE users_conf
  543. SET acs = ?, address = ?, businessnumber = ?,
  544. company = ?, countrycode = ?, currency = ?,
  545. dateformat = ?, dbdriver = ?,
  546. dbhost = ?, dbname = ?, dboptions = ?,
  547. dbpasswd = ?, dbport = ?, dbuser = ?,
  548. email = ?, fax = ?, menuwidth = ?,
  549. name = ?, numberformat = ?,
  550. print = ?, printer = ?, role = ?,
  551. sid = ?, signature = ?, stylesheet = ?,
  552. tel = ?, templates = ?, timeout = ?,
  553. vclimit = ?
  554. WHERE id = ?;");
  555. $userConfUpdate->execute($self->{acs}, $self->{address}, $self->{businessnumber},
  556. $self->{company}, $self->{countrycode}, $self->{currency},
  557. $self->{dateformat}, $self->{dbdriver},
  558. $self->{dbhost}, $self->{dbname}, $self->{dboptions},
  559. $self->{dbpasswd}, $self->{dbport}, $self->{dbuser},
  560. $self->{email}, $self->{fax}, $self->{menuwidth},
  561. $self->{name}, $self->{numberformat},
  562. $self->{print}, $self->{printer}, $self->{role},
  563. $self->{sid}, $self->{signature}, $self->{stylesheet},
  564. $self->{tel}, $self->{templates}, $self->{timeout},
  565. $self->{vclimit}, $userID);
  566. if($oldPassword ne $self->{password}){
  567. # if they're supplying a 32 char password that matches their old password
  568. # assume they don't want to change passwords
  569. $userConfUpdate = $dbh->prepare("UPDATE users_conf
  570. SET password = md5(?)
  571. WHERE id = ?");
  572. $userConfUpdate->execute($self->{password}, $userID);
  573. }
  574. }
  575. else{
  576. my $userConfInsert = $dbh->prepare("INSERT INTO users_conf(acs, address, businessnumber,
  577. company, countrycode, currency,
  578. dateformat, dbdriver,
  579. dbhost, dbname, dboptions, dbpasswd,
  580. dbport, dbuser, email, fax, menuwidth,
  581. name, numberformat, print, printer, role,
  582. sid, signature, stylesheet, tel, templates,
  583. timeout, vclimit, id, password)
  584. VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  585. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  586. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, md5(?));");
  587. $userConfInsert->execute($self->{acs}, $self->{address}, $self->{businessnumber},
  588. $self->{company}, $self->{countrycode}, $self->{currency},
  589. $self->{dateformat}, $self->{dbdriver},
  590. $self->{dbhost}, $self->{dbname}, $self->{dboptions},
  591. $self->{dbpasswd}, $self->{dbport}, $self->{dbuser},
  592. $self->{email}, $self->{fax}, $self->{menuwidth},
  593. $self->{name}, $self->{numberformat},
  594. $self->{print}, $self->{printer}, $self->{role},
  595. $self->{sid}, $self->{signature}, $self->{stylesheet},
  596. $self->{tel}, $self->{templates}, $self->{timeout},
  597. $self->{vclimit}, $userID, $self->{password});
  598. }
  599. if (! $self->{'admin'}) {
  600. $self->{dbpasswd} =~ s/\\'/'/g;
  601. $self->{dbpasswd} =~ s/\\\\/\\/g;
  602. # format dbconnect and dboptions string
  603. &dbconnect_vars($self, $self->{dbname});
  604. # check if login is in database
  605. my $dbh = DBI->connect(
  606. $self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd},
  607. {AutoCommit => 0})
  608. or $self->error($DBI::errstr);
  609. # add login to employee table if it does not exist
  610. my $login = $self->{login};
  611. $login =~ s/@.*//;
  612. my $sth = $dbh->prepare("SELECT id FROM employee WHERE login = ?;");
  613. $sth->execute($login);
  614. my ($id) = $sth->fetchrow_array;
  615. $sth->finish;
  616. my $employeenumber;
  617. my @values;
  618. if ($id) {
  619. $query = qq|UPDATE employee SET
  620. role = ?,
  621. email = ?,
  622. name = ?
  623. WHERE login = ?|;
  624. @values = ($self->{role}, $self->{email}, $self->{name}, $login);
  625. } else {
  626. my ($employeenumber) = Form::update_defaults(
  627. "", \%$self, "employeenumber", $dbh);
  628. $query = qq|
  629. INSERT INTO employee
  630. (login, employeenumber, name,
  631. workphone, role, email, sales)
  632. VALUES (?, ?, ?, ?, ?, ?, '1')|;
  633. @values = ($login, $employeenumber, $self->{name}, $self->{tel},
  634. $self->{role}, $self->{email})
  635. }
  636. $sth = $dbh->prepare($query);
  637. $sth->execute(@values);
  638. $dbh->commit;
  639. $dbh->disconnect;
  640. }
  641. }
  642. sub delete_login {
  643. my ($self, $form) = @_;
  644. my $dbh = DBI->connect(
  645. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd},
  646. {AutoCommit => 0})
  647. or $form->dberror(__FILE__.':'.__LINE__);
  648. my $login = $form->{login};
  649. $login =~ s/@.*//;
  650. my $query = qq|SELECT id FROM employee WHERE login = ?|;
  651. my $sth = $dbh->prepare($query);
  652. $sth->execute($login) || $form->dberror(__FILE__.':'.__LINE__.': '.$query);
  653. my ($id) = $sth->fetchrow_array;
  654. $sth->finish;
  655. my $query = qq|
  656. UPDATE employee
  657. SET login = NULL,
  658. enddate = current_date
  659. WHERE login = ?|;
  660. $sth = $dbh->prepare($query);
  661. $sth->execute($login);
  662. $dbh->commit;
  663. $dbh->disconnect;
  664. }
  665. sub config_vars {
  666. my @conf =
  667. qw(acs address businessnumber company countrycode
  668. currency dateformat dbconnect dbdriver dbhost dbname dboptions
  669. dbpasswd dbport dbuser email fax menuwidth name numberformat
  670. password printer role sid signature stylesheet tel templates
  671. timeout vclimit);
  672. @conf;
  673. }
  674. sub error {
  675. my ($self, $msg) = @_;
  676. if ($ENV{HTTP_USER_AGENT}) {
  677. print qq|Content-Type: text/html\n\n|.
  678. qq|<body bgcolor=ffffff>\n\n|.
  679. qq|<h2><font color=red>Error!</font></h2>\n|.
  680. qq|<p><b>$msg</b>|;
  681. }
  682. die "Error: $msg\n";
  683. }
  684. 1;