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