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