summaryrefslogtreecommitdiff
path: root/LedgerSMB.pm
blob: 63f4ec27848fb7c1a3aceca2a9edd1010bafa45d (plain)
  1. =head1 NAME
  2. LedgerSMB The Base class for many LedgerSMB objects, including DBObject.
  3. =head1 SYNOPSIS
  4. This module creates a basic request handler with utility functions available
  5. in database objects (LedgerSMB::DBObject)
  6. =head1 METHODS
  7. =over
  8. =item new ()
  9. This method creates a new base request instance. It also validates the
  10. session/user credentials, as appropriate for the run mode. Finally, it sets up
  11. the database connections for the user.
  12. =item date_to_number (user => $LedgerSMB::User, date => $string);
  13. This function takes the date in the format provided and returns a numeric
  14. string in YYMMDD format. This may be moved to User in the future.
  15. =item debug (file => $path);
  16. This dumps the current object to the file if that is defined and otherwise to
  17. standard output.
  18. =item escape (string => $string);
  19. This function returns the current string escaped using %hexhex notation.
  20. =item unescape (string => $string);
  21. This function returns the $string encoded using %hexhex using ordinary notation.
  22. =item format_amount (user => $LedgerSMB::User::hash, amount => $string, precision => $integer, neg_format => (-|DRCR));
  23. The function takes a monetary amount and formats it according to the user
  24. preferences, the negative format (- or DR/CR). Note that it may move to
  25. LedgerSMB::User at some point in the future.
  26. =item parse_amount (user => $LedgerSMB::User::hash, amount => $variable);
  27. If $amount is a Bigfloat, it is returned as is. If it is a string, it is
  28. parsed according to the user preferences stored in the LedgerSMB::User object.
  29. =item is_blank (name => $string)
  30. This function returns true if $self->{$string} only consists of whitespace
  31. characters or is an empty string.
  32. =item is_run_mode ('(cli|cgi|mod_perl)')
  33. This function returns 1 if the run mode is what is specified. Otherwise
  34. returns 0.
  35. =item is_allowed_role(allowed_roles => @role_names)
  36. This function returns 1 if the user's roles include any of the roles in
  37. @role_names. Currently it returns 1 when this is not found as well but when
  38. role permissions are introduced, this will change to 0.
  39. =item num_text_rows (string => $string, cols => $number, max => $number);
  40. This function determines the likely number of rows needed to hold text in a
  41. textbox. It returns either that number or max, which ever is lower.
  42. =item merge ($hashref, keys => @list, index => $number);
  43. This command merges the $hashref into the current object. If keys are
  44. specified, only those keys are used. Otherwise all keys are merged.
  45. If an index is specified, the merged keys are given a form of
  46. "$key" . "_$index", otherwise the key is used on both sides.
  47. =item redirect (msg => $string)
  48. This function redirects to the script and argument set determined by
  49. $self->{callback}, and if this is not set, goes to an info screen and prints
  50. $msg.
  51. =item redo_rows (fields => \@list, count => $integer, [index => $string);
  52. This function is undergoing serious redesign at the moment. If index is
  53. defined, that field is used for ordering the rows. If not, runningnumber is
  54. used. Behavior is not defined when index points to a field containing
  55. non-numbers.
  56. =item set (@attrs)
  57. Copies the given key=>vars to $self. Allows for finer control of
  58. merging hashes into self.
  59. =item remove_cgi_globals()
  60. Removes all elements starting with a . because these elements conflict with the
  61. ability to hide the entire structure for things like CSV lookups.
  62. =back
  63. =head1 Copyright (C) 2006, The LedgerSMB core team.
  64. # This work contains copyrighted information from a number of sources
  65. # all used with permission.
  66. #
  67. # This file contains source code included with or based on SQL-Ledger
  68. # which is Copyright Dieter Simader and DWS Systems Inc. 2000-2005
  69. # and licensed under the GNU General Public License version 2 or, at
  70. # your option, any later version. For a full list including contact
  71. # information of contributors, maintainers, and copyright holders,
  72. # see the CONTRIBUTORS file.
  73. #
  74. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  75. # Copyright (C) 2000
  76. #
  77. # Author: DWS Systems Inc.
  78. # Web: http://www.sql-ledger.org
  79. #
  80. # Contributors: Thomas Bayen <bayen@gmx.de>
  81. # Antti Kaihola <akaihola@siba.fi>
  82. # Moritz Bunkus (tex)
  83. # Jim Rawlings <jim@your-dba.com> (DB2)
  84. #====================================================================
  85. =cut
  86. use CGI::Simple;
  87. $CGI::Simple::DISABLE_UPLOADS = 0;
  88. use Math::BigFloat;
  89. use LedgerSMB::Sysconfig;
  90. use Data::Dumper;
  91. use LedgerSMB::Auth;
  92. use LedgerSMB::Template;
  93. use LedgerSMB::Locale;
  94. use LedgerSMB::User;
  95. use strict;
  96. $CGI::Simple::POST_MAX = -1;
  97. package LedgerSMB;
  98. our $VERSION = '1.2.99';
  99. sub new {
  100. my $type = shift @_;
  101. my $argstr = shift @_;
  102. my %cookie;
  103. my $self = {};
  104. $self->{version} = $VERSION;
  105. $self->{dbversion} = "1.2.0";
  106. bless $self, $type;
  107. my $query = ($argstr) ? new CGI::Simple($argstr) : new CGI::Simple;
  108. my $params = $query->Vars;
  109. $self->{VERSION} = $VERSION;
  110. $self->merge($params);
  111. $self->{have_latex} = $LedgerSMB::Sysconfig::latex;
  112. # Adding this so that empty values are stored in the db as NULL's. If
  113. # stored procedures want to handle them differently, they must opt to do so.
  114. # -- CT
  115. for (keys %$self){
  116. if ($self->{$_} eq ''){
  117. $self->{$_} = undef;
  118. }
  119. }
  120. if ($self->is_run_mode('cgi', 'mod_perl')) {
  121. $ENV{HTTP_COOKIE} =~ s/;\s*/;/g;
  122. my @cookies = split /;/, $ENV{HTTP_COOKIE};
  123. foreach (@cookies) {
  124. my ( $name, $value ) = split /=/, $_, 2;
  125. $cookie{$name} = $value;
  126. }
  127. }
  128. $self->{action} =~ s/\W/_/g;
  129. $self->{action} = lc $self->{action};
  130. if ( $self->{path} eq "bin/lynx" ) {
  131. $self->{menubar} = 1;
  132. # Applying the path is deprecated. Use menubar instead. CT.
  133. $self->{lynx} = 1;
  134. $self->{path} = "bin/lynx";
  135. }
  136. else {
  137. $self->{path} = "bin/mozilla";
  138. }
  139. if ( ( $self->{script} =~ m#(\.\.|\\|/)# ) ) {
  140. $self->error("Access Denied");
  141. }
  142. if (!$self->{script}) {
  143. $self->{script} = 'login.pl';
  144. }
  145. # if ($self->{action} eq 'migrate_user'){
  146. # return $self;
  147. # }
  148. if ($self->{script} eq 'login.pl' &&
  149. ($self->{action} eq 'authenticate' || $self->{action} eq '__default'
  150. || !$self->{action})){
  151. return $self;
  152. }
  153. if (!$self->{company} && $self->is_run_mode('cgi', 'mod_perl')){
  154. my $ccookie = $cookie{LedgerSMB};
  155. $ccookie =~ s/.*:([^:]*)$/$1/;
  156. $self->{company} = $ccookie;
  157. }
  158. $self->_db_init;
  159. if ($self->is_run_mode('cgi', 'mod_perl')) {
  160. #check for valid session unless this is an inital authentication
  161. #request -- CT
  162. if (!LedgerSMB::Auth::session_check( $cookie{"LedgerSMB"}, $self) ) {
  163. print STDERR "Session did not check";
  164. $self->_get_password("Session Expired");
  165. exit;
  166. }
  167. $self->{_user} = LedgerSMB::User->fetch_config($self);
  168. }
  169. my %date_setting = (
  170. 'mm/dd/yy' => "SQL, US",
  171. 'mm-dd-yy' => "POSTGRES, US",
  172. 'dd/mm/yy' => "SQL, EUROPEAN",
  173. 'dd-mm-yy' => "POSTGRES, EUROPEAN",
  174. 'dd.mm.yy' => "GERMAN",
  175. );
  176. $self->{dbh}->do("set DateStyle to '"
  177. .$date_setting{$self->{_user}->{dateformat}}."'");
  178. #my $locale = LedgerSMB::Locale->get_handle($self->{_user}->{countrycode})
  179. $self->{_locale} = LedgerSMB::Locale->get_handle('en') # temporary
  180. or $self->error(__FILE__.':'.__LINE__.": Locale not loaded: $!\n");
  181. $self->{stylesheet} = $self->{_user}->{stylesheet};
  182. return $self;
  183. }
  184. #This function needs to be moved into the session handler.
  185. sub _get_password {
  186. my ($self) = shift @_;
  187. $self->{sessionexpired} = shift @_;
  188. LedgerSMB::Auth::credential_prompt();
  189. exit;
  190. }
  191. sub debug {
  192. my $self = shift @_;
  193. my $args = shift @_;
  194. my $file;
  195. if (scalar keys %$args){
  196. $file = $args->{'file'};
  197. }
  198. my $d = Data::Dumper->new( [$self] );
  199. $d->Sortkeys(1);
  200. if ($file) {
  201. open( FH, '>', "$file" ) or die $!;
  202. print FH $d->Dump();
  203. close(FH);
  204. }
  205. else {
  206. print "\n";
  207. print $d->Dump();
  208. }
  209. }
  210. sub escape {
  211. my $self = shift;
  212. my %args = @_;
  213. my $str = $args{string};
  214. my $regex = qr/([^a-zA-Z0-9_.-])/;
  215. $str =~ s/$regex/sprintf("%%%02x", ord($1))/ge;
  216. return $str;
  217. }
  218. sub is_blank {
  219. my $self = shift @_;
  220. my %args = @_;
  221. my $name = $args{name};
  222. if (not defined $name){
  223. # TODO: Raise error
  224. }
  225. my $rc;
  226. if ( $self->{$name} =~ /^\s*$/ ) {
  227. $rc = 1;
  228. }
  229. else {
  230. $rc = 0;
  231. }
  232. $rc;
  233. }
  234. sub is_run_mode {
  235. my $self = shift @_;
  236. my $mode = lc shift @_;
  237. my $rc = 0;
  238. if ( $mode eq 'cgi' && $ENV{GATEWAY_INTERFACE} ) {
  239. $rc = 1;
  240. }
  241. elsif ( $mode eq 'cli' && !( $ENV{GATEWAY_INTERFACE} || $ENV{MOD_PERL} ) ) {
  242. $rc = 1;
  243. }
  244. elsif ( $mode eq 'mod_perl' && $ENV{MOD_PERL} ) {
  245. $rc = 1;
  246. }
  247. $rc;
  248. }
  249. sub num_text_rows {
  250. my $self = shift @_;
  251. my %args = @_;
  252. my $string = $args{string};
  253. my $cols = $args{cols};
  254. my $maxrows = $args{max};
  255. my $rows = 0;
  256. for ( split /\n/, $string ) {
  257. my $line = $_;
  258. while ( length($line) > $cols ) {
  259. my $fragment = substr( $line, 0, $cols + 1 );
  260. $fragment =~ s/^(.*)\W.*$/$1/;
  261. $line =~ s/$fragment//;
  262. if ( $line eq $fragment ) { # No word breaks!
  263. $line = "";
  264. }
  265. ++$rows;
  266. }
  267. ++$rows;
  268. }
  269. if ( !defined $maxrows ) {
  270. $maxrows = $rows;
  271. }
  272. return ( $rows > $maxrows ) ? $maxrows : $rows;
  273. }
  274. sub redirect {
  275. my $self = shift @_;
  276. my %args = @_;
  277. my $msg = $args{msg};
  278. if ( $self->{callback} || !$msg ) {
  279. main::redirect();
  280. exit;
  281. }
  282. else {
  283. $self->info($msg);
  284. }
  285. }
  286. # TODO: Either we should have an amount class with formats and such attached
  287. # Or maybe we should move this into the user class...
  288. sub format_amount {
  289. # Based on SQL-Ledger's Form::format_amount
  290. my $self = shift @_;
  291. my %args = @_;
  292. my $myconfig = $args{user} || $self->{_user};
  293. my $amount = $args{amount};
  294. my $places = $args{precision};
  295. my $dash = $args{neg_format};
  296. my $negative;
  297. if ($amount) {
  298. $amount = $self->parse_amount( 'user' => $myconfig, 'amount' => $amount );
  299. $negative = ( $amount < 0 );
  300. $amount =~ s/-//;
  301. }
  302. if ( $places =~ /\d+/ ) {
  303. #$places = 4 if $places == 2;
  304. $amount = $self->round_amount( $amount, $places );
  305. }
  306. # is the amount negative
  307. # Parse $myconfig->{numberformat}
  308. my ( $ts, $ds ) = ( $1, $2 );
  309. if ($amount) {
  310. if ( $myconfig->{numberformat} ) {
  311. my ( $whole, $dec ) = split /\./, "$amount";
  312. $amount = join '', reverse split //, $whole;
  313. if ($places) {
  314. $dec .= "0" x $places;
  315. $dec = substr( $dec, 0, $places );
  316. }
  317. if ( $myconfig->{numberformat} eq '1,000.00' ) {
  318. $amount =~ s/\d{3,}?/$&,/g;
  319. $amount =~ s/,$//;
  320. $amount = join '', reverse split //, $amount;
  321. $amount .= "\.$dec" if ( $dec ne "" );
  322. }
  323. elsif ( $myconfig->{numberformat} eq '1 000.00' ) {
  324. $amount =~ s/\d{3,}?/$& /g;
  325. $amount =~ s/\s$//;
  326. $amount = join '', reverse split //, $amount;
  327. $amount .= "\.$dec" if ( $dec ne "" );
  328. }
  329. elsif ( $myconfig->{numberformat} eq "1'000.00" ) {
  330. $amount =~ s/\d{3,}?/$&'/g;
  331. $amount =~ s/'$//;
  332. $amount = join '', reverse split //, $amount;
  333. $amount .= "\.$dec" if ( $dec ne "" );
  334. }
  335. elsif ( $myconfig->{numberformat} eq '1.000,00' ) {
  336. $amount =~ s/\d{3,}?/$&./g;
  337. $amount =~ s/\.$//;
  338. $amount = join '', reverse split //, $amount;
  339. $amount .= ",$dec" if ( $dec ne "" );
  340. }
  341. elsif ( $myconfig->{numberformat} eq '1000,00' ) {
  342. $amount = "$whole";
  343. $amount .= ",$dec" if ( $dec ne "" );
  344. }
  345. elsif ( $myconfig->{numberformat} eq '1000.00' ) {
  346. $amount = "$whole";
  347. $amount .= ".$dec" if ( $dec ne "" );
  348. }
  349. if ( $dash =~ /-/ ) {
  350. $amount = ($negative) ? "($amount)" : "$amount";
  351. }
  352. elsif ( $dash =~ /DRCR/ ) {
  353. $amount = ($negative) ? "$amount DR" : "$amount CR";
  354. }
  355. else {
  356. $amount = ($negative) ? "-$amount" : "$amount";
  357. }
  358. }
  359. }
  360. else {
  361. if ( $dash eq "0" && $places ) {
  362. if ( $myconfig->{numberformat} =~ /0,00$/ ) {
  363. $amount = "0" . "," . "0" x $places;
  364. }
  365. else {
  366. $amount = "0" . "." . "0" x $places;
  367. }
  368. }
  369. else {
  370. $amount = ( $dash ne "" ) ? "$dash" : "";
  371. }
  372. }
  373. $amount;
  374. }
  375. # This should probably go to the User object too.
  376. sub parse_amount {
  377. my $self = shift @_;
  378. my %args = @_;
  379. my $myconfig = $args{user} || $self->{_user};
  380. my $amount = $args{amount};
  381. if ( $amount eq '' or ! defined $amount) {
  382. return 0;
  383. }
  384. if ( UNIVERSAL::isa( $amount, 'Math::BigFloat' ) )
  385. { # Amount may not be an object
  386. return $amount;
  387. }
  388. my $numberformat = $myconfig->{numberformat};
  389. if ( ( $numberformat eq '1.000,00' )
  390. || ( $numberformat eq '1000,00' ) )
  391. {
  392. $amount =~ s/\.//g;
  393. $amount =~ s/,/./;
  394. }
  395. elsif ( $numberformat eq '1 000.00' ) {
  396. $amount =~ s/\s//g;
  397. }
  398. elsif ( $numberformat eq "1'000.00" ) {
  399. $amount =~ s/'//g;
  400. }
  401. $amount =~ s/,//g;
  402. if ( $amount =~ s/\((\d*\.?\d*)\)/$1/ ) {
  403. $amount = $1 * -1;
  404. }
  405. elsif ( $amount =~ s/(\d*\.?\d*)\s?DR/$1/ ) {
  406. $amount = $1 * -1;
  407. }
  408. $amount =~ s/\s?CR//;
  409. $amount = new Math::BigFloat($amount);
  410. if ($amount->is_nan){
  411. $self->error("Invalid number detected during parsing");
  412. }
  413. return ( $amount * 1 );
  414. }
  415. sub round_amount {
  416. my ( $self, $amount, $places ) = @_;
  417. # These rounding rules follow from the previous implementation.
  418. # They should be changed to allow different rules for different accounts.
  419. if ($amount >= 0) {
  420. Math::BigFloat->round_mode('+inf');
  421. }
  422. else {
  423. Math::BigFloat->round_mode('-inf');
  424. }
  425. if ($places >= 0) {
  426. $amount = Math::BigFloat->new($amount)->ffround( -$places );
  427. }
  428. else {
  429. $amount = Math::BigFloat->new($amount)->ffround( -( $places - 1 ) );
  430. }
  431. $amount->precision(undef);
  432. return $amount;
  433. }
  434. sub call_procedure {
  435. my $self = shift @_;
  436. my %args = @_;
  437. my $procname = $args{procname};
  438. my @call_args;
  439. @call_args = @{ $args{args} } if defined $args{args};
  440. my $order_by = $args{order_by};
  441. my $argstr = "";
  442. my @results;
  443. $procname = $self->{dbh}->quote_identifier($procname);
  444. for ( 1 .. scalar @call_args ) {
  445. $argstr .= "?, ";
  446. }
  447. $argstr =~ s/\, $//;
  448. my $query = "SELECT * FROM $procname()";
  449. if ($order_by){
  450. $query .= " ORDER BY $order_by";
  451. }
  452. $query =~ s/\(\)/($argstr)/;
  453. my $sth = $self->{dbh}->prepare($query);
  454. if (scalar @call_args){
  455. $sth->execute(@call_args) || $self->error($self->{dbh}->errstr);
  456. } else {
  457. $sth->execute() || $self->error($self->{dbh}->errstr);
  458. }
  459. my @types = @{$sth->{TYPE}};
  460. my @names = @{$sth->{NAME_lc}};
  461. while ( my $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  462. for (0 .. $#names){
  463. # numeric float4/real
  464. if ($types[$_] == 3 or $types[$_] == 2) {
  465. $ref->{$names[$_]} = Math::BigFloat->new($ref->{$names[$_]});
  466. }
  467. }
  468. push @results, $ref;
  469. }
  470. @results;
  471. }
  472. # Keeping this here due to common requirements
  473. sub is_allowed_role {
  474. my $self = shift @_;
  475. my %args = @_;
  476. my @roles = @{$args{allowed_roles}};
  477. for my $role (@roles){
  478. if (scalar(grep /^$role$/, $self->{_roles})){
  479. return 1;
  480. }
  481. }
  482. return 1; # TODO change to 0 when the role system is implmented
  483. }
  484. # This should probably be moved to User too...
  485. sub date_to_number {
  486. #based on SQL-Ledger's Form::datetonum
  487. my $self = shift @_;
  488. my %args = @_;
  489. my $myconfig = $args{user};
  490. my $date = $args{date};
  491. my ( $yy, $mm, $dd );
  492. if ( $date && $date =~ /\D/ ) {
  493. if ( $date =~ /^\d{4}-\d\d-\d\d$/ ) {
  494. ( $yy, $mm, $dd ) = split /\D/, $date;
  495. } elsif ( $myconfig->{dateformat} =~ /^yy/ ) {
  496. ( $yy, $mm, $dd ) = split /\D/, $date;
  497. } elsif ( $myconfig->{dateformat} =~ /^mm/ ) {
  498. ( $mm, $dd, $yy ) = split /\D/, $date;
  499. } elsif ( $myconfig->{dateformat} =~ /^dd/ ) {
  500. ( $dd, $mm, $yy ) = split /\D/, $date;
  501. }
  502. $dd *= 1;
  503. $mm *= 1;
  504. $yy += 2000 if length $yy == 2;
  505. $dd = substr( "0$dd", -2 );
  506. $mm = substr( "0$mm", -2 );
  507. $date = "$yy$mm$dd";
  508. }
  509. $date;
  510. }
  511. # To be replaced with a generic interface to an Error class
  512. sub error {
  513. my ( $self, $msg ) = @_;
  514. if ( $ENV{GATEWAY_INTERFACE} ) {
  515. $self->{msg} = $msg;
  516. $self->{format} = "html";
  517. delete $self->{pre};
  518. print qq|Content-Type: text/html; charset=utf-8\n\n|;
  519. print "<head></head>";
  520. print
  521. qq|<body><h2 class="error">Error!</h2> <p><b>$self->{msg}</b></body>|;
  522. exit;
  523. }
  524. else {
  525. if ( $ENV{error_function} ) {
  526. &{ $ENV{error_function} }($msg);
  527. }
  528. die "Error: $msg\n";
  529. }
  530. }
  531. # Database routines used throughout
  532. sub _db_init {
  533. my $self = shift @_;
  534. my %args = @_;
  535. my $creds = LedgerSMB::Auth::get_credentials();
  536. $self->{login} = $creds->{login};
  537. if (!$self->{company}){
  538. $self->{company} = $LedgerSMB::Sysconfig::default_db;
  539. }
  540. my $dbname = $self->{company};
  541. # Note that we have to request the login/password again if the db
  542. # connection fails since this probably means bad credentials are entered.
  543. # Just in case, however, I think it is a good idea to include the DBI
  544. # error string. CT
  545. $self->{dbh} = DBI->connect(
  546. "dbi:Pg:dbname=$dbname", "$creds->{login}", "$creds->{password}", { AutoCommit => 0 }
  547. );
  548. my $dbh = $self->{dbh};
  549. if (($self->{script} eq 'login.pl') && ($self->{action} eq
  550. 'authenticate')){
  551. return;
  552. }
  553. elsif (!$dbh){
  554. $self->_get_password;
  555. }
  556. $dbh->{pg_server_prepare} = 0;
  557. $dbh->{pg_enable_utf8} = 1;
  558. # This is the general version check
  559. my $sth = $dbh->prepare("
  560. SELECT value FROM defaults
  561. WHERE setting_key = 'version'");
  562. $sth->execute;
  563. my ($dbversion) = $sth->fetchrow_array;
  564. if ($dbversion ne $self->{dbversion}){
  565. $self->error("Database is not the expected version. Was $dbversion, expected $self->{dbversion}");
  566. }
  567. my $query = "SELECT t.extends,
  568. coalesce (t.table_name, 'custom_' || extends)
  569. || ':' || f.field_name as field_def
  570. FROM custom_table_catalog t
  571. JOIN custom_field_catalog f USING (table_id)";
  572. my $sth = $self->{dbh}->prepare($query);
  573. $sth->execute;
  574. my $ref;
  575. while ( $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  576. push @{ $self->{custom_db_fields}{ $ref->{extends} } },
  577. $ref->{field_def};
  578. }
  579. }
  580. # Deprecated, only here for old code
  581. sub dberror{
  582. my $self = shift @_;
  583. $self->error(@_);
  584. }
  585. sub redo_rows {
  586. my $self = shift @_;
  587. my %args = @_;
  588. my @flds = @{ $args{fields} };
  589. my $count = $args{count};
  590. my $index = ( $args{index} ) ? $args{index} : 'runningnumber';
  591. my @rows;
  592. my $i; # incriment counter use only
  593. for $i ( 1 .. $count ) {
  594. my $temphash = { _inc => $i };
  595. for my $fld (@flds) {
  596. $temphash->{$fld} = $self->{ "$fld" . "_$i" };
  597. }
  598. push @rows, $temphash;
  599. }
  600. $i = 1;
  601. for my $row ( sort { $a->{index} <=> $b->{index} } @rows ) {
  602. for my $fld (@flds) {
  603. $self->{ "$fld" . "_$i" } = $row->{$fld};
  604. }
  605. ++$i;
  606. }
  607. }
  608. sub merge {
  609. my ( $self, $src ) = @_;
  610. for my $arg ( $self, $src ) {
  611. shift;
  612. }
  613. my %args = @_;
  614. my @keys;
  615. if (defined $args{keys}){
  616. @keys = @{ $args{keys} };
  617. }
  618. my $index = $args{index};
  619. if ( !scalar @keys ) {
  620. @keys = keys %{$src};
  621. }
  622. for my $arg ( @keys ) {
  623. my $dst_arg;
  624. if ($index) {
  625. $dst_arg = $arg . "_$index";
  626. }
  627. else {
  628. $dst_arg = $arg;
  629. }
  630. $self->{$dst_arg} = $src->{$arg};
  631. }
  632. }
  633. sub type {
  634. my $self = shift @_;
  635. if (!$ENV{REQUEST_METHOD} or
  636. ( !grep {$ENV{REQUEST_METHOD} eq $_} ("HEAD", "GET", "POST") ) ) {
  637. $self->error("Request method unset or set to unknown value");
  638. }
  639. return $ENV{REQUEST_METHOD};
  640. }
  641. sub DESTROY {}
  642. sub set {
  643. my $self = shift @_;
  644. my %args = @_;
  645. for my $arg (keys(%args)) {
  646. $self->{$arg} = $args{$arg};
  647. }
  648. return 1;
  649. }
  650. sub remove_cgi_globals {
  651. my ($self) = @_;
  652. for my $key (keys %$self){
  653. if ($key =~ /^\./){
  654. delete $self->{key}
  655. }
  656. }
  657. }
  658. sub take_top_level {
  659. my ($self) = @_;
  660. my $return_hash = {};
  661. for my $key (keys %$self){
  662. if (!ref($self->{$key}) && $key !~ /^\./){
  663. $return_hash->{$key} = $self->{$key}
  664. }
  665. }
  666. return $return_hash;
  667. }
  668. 1;