summaryrefslogtreecommitdiff
path: root/LedgerSMB.pm
blob: ce279466bc5260d35801b1b4f7d8b66a1da9712b (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. use Math::BigFloat;
  88. use LedgerSMB::Sysconfig;
  89. use Data::Dumper;
  90. use LedgerSMB::Auth;
  91. use LedgerSMB::Template;
  92. use LedgerSMB::Locale;
  93. use LedgerSMB::User;
  94. use strict;
  95. $CGI::Simple::POST_MAX = -1;
  96. package LedgerSMB;
  97. our $VERSION = '1.2.99';
  98. sub new {
  99. my $type = shift @_;
  100. my $argstr = shift @_;
  101. my %cookie;
  102. my $self = {};
  103. $self->{version} = $VERSION;
  104. $self->{dbversion} = "1.2.0";
  105. bless $self, $type;
  106. my $query = ($argstr) ? new CGI::Simple($argstr) : new CGI::Simple;
  107. my $params = $query->Vars;
  108. $self->{VERSION} = $VERSION;
  109. $self->merge($params);
  110. $self->{have_latex} = $LedgerSMB::Sysconfig::latex;
  111. # Adding this so that empty values are stored in the db as NULL's. If
  112. # stored procedures want to handle them differently, they must opt to do so.
  113. # -- CT
  114. for (keys %$self){
  115. if ($self->{$_} eq ''){
  116. $self->{$_} = undef;
  117. }
  118. }
  119. if ($self->is_run_mode('cgi', 'mod_perl')) {
  120. $ENV{HTTP_COOKIE} =~ s/;\s*/;/g;
  121. my @cookies = split /;/, $ENV{HTTP_COOKIE};
  122. foreach (@cookies) {
  123. my ( $name, $value ) = split /=/, $_, 2;
  124. $cookie{$name} = $value;
  125. }
  126. }
  127. $self->{action} =~ s/\W/_/g;
  128. $self->{action} = lc $self->{action};
  129. if ( $self->{path} eq "bin/lynx" ) {
  130. $self->{menubar} = 1;
  131. # Applying the path is deprecated. Use menubar instead. CT.
  132. $self->{lynx} = 1;
  133. $self->{path} = "bin/lynx";
  134. }
  135. else {
  136. $self->{path} = "bin/mozilla";
  137. }
  138. if ( ( $self->{script} =~ m#(\.\.|\\|/)# ) ) {
  139. $self->error("Access Denied");
  140. }
  141. if (!$self->{script}) {
  142. $self->{script} = 'login.pl';
  143. }
  144. # if ($self->{action} eq 'migrate_user'){
  145. # return $self;
  146. # }
  147. if ($self->{script} eq 'login.pl' &&
  148. ($self->{action} eq 'authenticate' || $self->{action} eq '__default'
  149. || !$self->{action})){
  150. return $self;
  151. }
  152. if (!$self->{company} && $self->is_run_mode('cgi', 'mod_perl')){
  153. my $ccookie = $cookie{LedgerSMB};
  154. $ccookie =~ s/.*:([^:]*)$/$1/;
  155. $self->{company} = $ccookie;
  156. }
  157. $self->_db_init;
  158. if ($self->is_run_mode('cgi', 'mod_perl')) {
  159. #check for valid session unless this is an inital authentication
  160. #request -- CT
  161. if (!LedgerSMB::Auth::session_check( $cookie{"LedgerSMB"}, $self) ) {
  162. print STDERR "Session did not check";
  163. $self->_get_password("Session Expired");
  164. exit;
  165. }
  166. $self->{_user} = LedgerSMB::User->fetch_config($self);
  167. }
  168. my %date_setting = (
  169. 'mm/dd/yy' => "SQL, US",
  170. 'mm-dd-yy' => "POSTGRES, US",
  171. 'dd/mm/yy' => "SQL, EUROPEAN",
  172. 'dd-mm-yy' => "POSTGRES, EUROPEAN",
  173. 'dd.mm.yy' => "GERMAN",
  174. );
  175. $self->{dbh}->do("set DateStyle to '"
  176. .$date_setting{$self->{_user}->{dateformat}}."'");
  177. #my $locale = LedgerSMB::Locale->get_handle($self->{_user}->{countrycode})
  178. $self->{_locale} = LedgerSMB::Locale->get_handle('en') # temporary
  179. or $self->error(__FILE__.':'.__LINE__.": Locale not loaded: $!\n");
  180. $self->{stylesheet} = $self->{_user}->{stylesheet};
  181. return $self;
  182. }
  183. #This function needs to be moved into the session handler.
  184. sub _get_password {
  185. my ($self) = shift @_;
  186. $self->{sessionexpired} = shift @_;
  187. LedgerSMB::Auth::credential_prompt();
  188. exit;
  189. }
  190. sub debug {
  191. my $self = shift @_;
  192. my $args = shift @_;
  193. my $file;
  194. if (scalar keys %$args){
  195. $file = $args->{'file'};
  196. }
  197. my $d = Data::Dumper->new( [$self] );
  198. $d->Sortkeys(1);
  199. if ($file) {
  200. open( FH, '>', "$file" ) or die $!;
  201. print FH $d->Dump();
  202. close(FH);
  203. }
  204. else {
  205. print "\n";
  206. print $d->Dump();
  207. }
  208. }
  209. sub escape {
  210. my $self = shift;
  211. my %args = @_;
  212. my $str = $args{string};
  213. my $regex = qr/([^a-zA-Z0-9_.-])/;
  214. $str =~ s/$regex/sprintf("%%%02x", ord($1))/ge;
  215. return $str;
  216. }
  217. sub is_blank {
  218. my $self = shift @_;
  219. my %args = @_;
  220. my $name = $args{name};
  221. if (not defined $name){
  222. # TODO: Raise error
  223. }
  224. my $rc;
  225. if ( $self->{$name} =~ /^\s*$/ ) {
  226. $rc = 1;
  227. }
  228. else {
  229. $rc = 0;
  230. }
  231. $rc;
  232. }
  233. sub is_run_mode {
  234. my $self = shift @_;
  235. my $mode = lc shift @_;
  236. my $rc = 0;
  237. if ( $mode eq 'cgi' && $ENV{GATEWAY_INTERFACE} ) {
  238. $rc = 1;
  239. }
  240. elsif ( $mode eq 'cli' && !( $ENV{GATEWAY_INTERFACE} || $ENV{MOD_PERL} ) ) {
  241. $rc = 1;
  242. }
  243. elsif ( $mode eq 'mod_perl' && $ENV{MOD_PERL} ) {
  244. $rc = 1;
  245. }
  246. $rc;
  247. }
  248. sub num_text_rows {
  249. my $self = shift @_;
  250. my %args = @_;
  251. my $string = $args{string};
  252. my $cols = $args{cols};
  253. my $maxrows = $args{max};
  254. my $rows = 0;
  255. for ( split /\n/, $string ) {
  256. my $line = $_;
  257. while ( length($line) > $cols ) {
  258. my $fragment = substr( $line, 0, $cols + 1 );
  259. $fragment =~ s/^(.*)\W.*$/$1/;
  260. $line =~ s/$fragment//;
  261. if ( $line eq $fragment ) { # No word breaks!
  262. $line = "";
  263. }
  264. ++$rows;
  265. }
  266. ++$rows;
  267. }
  268. if ( !defined $maxrows ) {
  269. $maxrows = $rows;
  270. }
  271. return ( $rows > $maxrows ) ? $maxrows : $rows;
  272. }
  273. sub redirect {
  274. my $self = shift @_;
  275. my %args = @_;
  276. my $msg = $args{msg};
  277. if ( $self->{callback} || !$msg ) {
  278. main::redirect();
  279. exit;
  280. }
  281. else {
  282. $self->info($msg);
  283. }
  284. }
  285. # TODO: Either we should have an amount class with formats and such attached
  286. # Or maybe we should move this into the user class...
  287. sub format_amount {
  288. # Based on SQL-Ledger's Form::format_amount
  289. my $self = shift @_;
  290. my %args = @_;
  291. my $myconfig = $args{user} || $self->{_user};
  292. my $amount = $args{amount};
  293. my $places = $args{precision};
  294. my $dash = $args{neg_format};
  295. my $negative;
  296. if ($amount) {
  297. $amount = $self->parse_amount( 'user' => $myconfig, 'amount' => $amount );
  298. $negative = ( $amount < 0 );
  299. $amount =~ s/-//;
  300. }
  301. if ( $places =~ /\d+/ ) {
  302. #$places = 4 if $places == 2;
  303. $amount = $self->round_amount( $amount, $places );
  304. }
  305. # is the amount negative
  306. # Parse $myconfig->{numberformat}
  307. my ( $ts, $ds ) = ( $1, $2 );
  308. if ($amount) {
  309. if ( $myconfig->{numberformat} ) {
  310. my ( $whole, $dec ) = split /\./, "$amount";
  311. $amount = join '', reverse split //, $whole;
  312. if ($places) {
  313. $dec .= "0" x $places;
  314. $dec = substr( $dec, 0, $places );
  315. }
  316. if ( $myconfig->{numberformat} eq '1,000.00' ) {
  317. $amount =~ s/\d{3,}?/$&,/g;
  318. $amount =~ s/,$//;
  319. $amount = join '', reverse split //, $amount;
  320. $amount .= "\.$dec" if ( $dec ne "" );
  321. }
  322. elsif ( $myconfig->{numberformat} eq '1 000.00' ) {
  323. $amount =~ s/\d{3,}?/$& /g;
  324. $amount =~ s/\s$//;
  325. $amount = join '', reverse split //, $amount;
  326. $amount .= "\.$dec" if ( $dec ne "" );
  327. }
  328. elsif ( $myconfig->{numberformat} eq "1'000.00" ) {
  329. $amount =~ s/\d{3,}?/$&'/g;
  330. $amount =~ s/'$//;
  331. $amount = join '', reverse split //, $amount;
  332. $amount .= "\.$dec" if ( $dec ne "" );
  333. }
  334. elsif ( $myconfig->{numberformat} eq '1.000,00' ) {
  335. $amount =~ s/\d{3,}?/$&./g;
  336. $amount =~ s/\.$//;
  337. $amount = join '', reverse split //, $amount;
  338. $amount .= ",$dec" if ( $dec ne "" );
  339. }
  340. elsif ( $myconfig->{numberformat} eq '1000,00' ) {
  341. $amount = "$whole";
  342. $amount .= ",$dec" if ( $dec ne "" );
  343. }
  344. elsif ( $myconfig->{numberformat} eq '1000.00' ) {
  345. $amount = "$whole";
  346. $amount .= ".$dec" if ( $dec ne "" );
  347. }
  348. if ( $dash =~ /-/ ) {
  349. $amount = ($negative) ? "($amount)" : "$amount";
  350. }
  351. elsif ( $dash =~ /DRCR/ ) {
  352. $amount = ($negative) ? "$amount DR" : "$amount CR";
  353. }
  354. else {
  355. $amount = ($negative) ? "-$amount" : "$amount";
  356. }
  357. }
  358. }
  359. else {
  360. if ( $dash eq "0" && $places ) {
  361. if ( $myconfig->{numberformat} =~ /0,00$/ ) {
  362. $amount = "0" . "," . "0" x $places;
  363. }
  364. else {
  365. $amount = "0" . "." . "0" x $places;
  366. }
  367. }
  368. else {
  369. $amount = ( $dash ne "" ) ? "$dash" : "";
  370. }
  371. }
  372. $amount;
  373. }
  374. # This should probably go to the User object too.
  375. sub parse_amount {
  376. my $self = shift @_;
  377. my %args = @_;
  378. my $myconfig = $args{user} || $self->{_user};
  379. my $amount = $args{amount};
  380. if ( $amount eq '' or ! defined $amount) {
  381. return 0;
  382. }
  383. if ( UNIVERSAL::isa( $amount, 'Math::BigFloat' ) )
  384. { # Amount may not be an object
  385. return $amount;
  386. }
  387. my $numberformat = $myconfig->{numberformat};
  388. if ( ( $numberformat eq '1.000,00' )
  389. || ( $numberformat eq '1000,00' ) )
  390. {
  391. $amount =~ s/\.//g;
  392. $amount =~ s/,/./;
  393. }
  394. elsif ( $numberformat eq '1 000.00' ) {
  395. $amount =~ s/\s//g;
  396. }
  397. elsif ( $numberformat eq "1'000.00" ) {
  398. $amount =~ s/'//g;
  399. }
  400. $amount =~ s/,//g;
  401. if ( $amount =~ s/\((\d*\.?\d*)\)/$1/ ) {
  402. $amount = $1 * -1;
  403. }
  404. elsif ( $amount =~ s/(\d*\.?\d*)\s?DR/$1/ ) {
  405. $amount = $1 * -1;
  406. }
  407. $amount =~ s/\s?CR//;
  408. $amount = new Math::BigFloat($amount);
  409. if ($amount->is_nan){
  410. $self->error("Invalid number detected during parsing");
  411. }
  412. return ( $amount * 1 );
  413. }
  414. sub round_amount {
  415. my ( $self, $amount, $places ) = @_;
  416. # These rounding rules follow from the previous implementation.
  417. # They should be changed to allow different rules for different accounts.
  418. if ($amount >= 0) {
  419. Math::BigFloat->round_mode('+inf');
  420. }
  421. else {
  422. Math::BigFloat->round_mode('-inf');
  423. }
  424. if ($places >= 0) {
  425. $amount = Math::BigFloat->new($amount)->ffround( -$places );
  426. }
  427. else {
  428. $amount = Math::BigFloat->new($amount)->ffround( -( $places - 1 ) );
  429. }
  430. $amount->precision(undef);
  431. return $amount;
  432. }
  433. sub call_procedure {
  434. my $self = shift @_;
  435. my %args = @_;
  436. my $procname = $args{procname};
  437. my @call_args;
  438. @call_args = @{ $args{args} } if defined $args{args};
  439. my $order_by = $args{order_by};
  440. my $argstr = "";
  441. my @results;
  442. $procname = $self->{dbh}->quote_identifier($procname);
  443. for ( 1 .. scalar @call_args ) {
  444. $argstr .= "?, ";
  445. }
  446. $argstr =~ s/\, $//;
  447. my $query = "SELECT * FROM $procname()";
  448. if ($order_by){
  449. $query .= " ORDER BY $order_by";
  450. }
  451. $query =~ s/\(\)/($argstr)/;
  452. my $sth = $self->{dbh}->prepare($query);
  453. if (scalar @call_args){
  454. $sth->execute(@call_args) || $self->error($self->{dbh}->errstr);
  455. } else {
  456. $sth->execute() || $self->error($self->{dbh}->errstr);
  457. }
  458. my @types = @{$sth->{TYPE}};
  459. my @names = @{$sth->{NAME_lc}};
  460. while ( my $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  461. for (0 .. $#names){
  462. # numeric float4/real
  463. if ($types[$_] == 3 or $types[$_] == 2) {
  464. $ref->{$names[$_]} = Math::BigFloat->new($ref->{$names[$_]});
  465. }
  466. }
  467. push @results, $ref;
  468. }
  469. @results;
  470. }
  471. # Keeping this here due to common requirements
  472. sub is_allowed_role {
  473. my $self = shift @_;
  474. my %args = @_;
  475. my @roles = @{$args{allowed_roles}};
  476. for my $role (@roles){
  477. if (scalar(grep /^$role$/, $self->{_roles})){
  478. return 1;
  479. }
  480. }
  481. return 1; # TODO change to 0 when the role system is implmented
  482. }
  483. # This should probably be moved to User too...
  484. sub date_to_number {
  485. #based on SQL-Ledger's Form::datetonum
  486. my $self = shift @_;
  487. my %args = @_;
  488. my $myconfig = $args{user};
  489. my $date = $args{date};
  490. my ( $yy, $mm, $dd );
  491. if ( $date && $date =~ /\D/ ) {
  492. if ( $date =~ /^\d{4}-\d\d-\d\d$/ ) {
  493. ( $yy, $mm, $dd ) = split /\D/, $date;
  494. } elsif ( $myconfig->{dateformat} =~ /^yy/ ) {
  495. ( $yy, $mm, $dd ) = split /\D/, $date;
  496. } elsif ( $myconfig->{dateformat} =~ /^mm/ ) {
  497. ( $mm, $dd, $yy ) = split /\D/, $date;
  498. } elsif ( $myconfig->{dateformat} =~ /^dd/ ) {
  499. ( $dd, $mm, $yy ) = split /\D/, $date;
  500. }
  501. $dd *= 1;
  502. $mm *= 1;
  503. $yy += 2000 if length $yy == 2;
  504. $dd = substr( "0$dd", -2 );
  505. $mm = substr( "0$mm", -2 );
  506. $date = "$yy$mm$dd";
  507. }
  508. $date;
  509. }
  510. # To be replaced with a generic interface to an Error class
  511. sub error {
  512. my ( $self, $msg ) = @_;
  513. if ( $ENV{GATEWAY_INTERFACE} ) {
  514. $self->{msg} = $msg;
  515. $self->{format} = "html";
  516. delete $self->{pre};
  517. print qq|Content-Type: text/html; charset=utf-8\n\n|;
  518. print "<head></head>";
  519. print
  520. qq|<body><h2 class="error">Error!</h2> <p><b>$self->{msg}</b></body>|;
  521. exit;
  522. }
  523. else {
  524. if ( $ENV{error_function} ) {
  525. &{ $ENV{error_function} }($msg);
  526. }
  527. die "Error: $msg\n";
  528. }
  529. }
  530. # Database routines used throughout
  531. sub _db_init {
  532. my $self = shift @_;
  533. my %args = @_;
  534. my $creds = LedgerSMB::Auth::get_credentials();
  535. $self->{login} = $creds->{login};
  536. if (!$self->{company}){
  537. $self->{company} = $LedgerSMB::Sysconfig::default_db;
  538. }
  539. my $dbname = $self->{company};
  540. # Note that we have to request the login/password again if the db
  541. # connection fails since this probably means bad credentials are entered.
  542. # Just in case, however, I think it is a good idea to include the DBI
  543. # error string. CT
  544. $self->{dbh} = DBI->connect(
  545. "dbi:Pg:dbname=$dbname", "$creds->{login}", "$creds->{password}", { AutoCommit => 0 }
  546. );
  547. my $dbh = $self->{dbh};
  548. if (($self->{script} eq 'login.pl') && ($self->{action} eq
  549. 'authenticate')){
  550. return;
  551. }
  552. elsif (!$dbh){
  553. $self->_get_password;
  554. }
  555. $dbh->{pg_server_prepare} = 0;
  556. $dbh->{pg_enable_utf8} = 1;
  557. # This is the general version check
  558. my $sth = $dbh->prepare("
  559. SELECT value FROM defaults
  560. WHERE setting_key = 'version'");
  561. $sth->execute;
  562. my ($dbversion) = $sth->fetchrow_array;
  563. if ($dbversion ne $self->{dbversion}){
  564. $self->error("Database is not the expected version. Was $dbversion, expected $self->{dbversion}");
  565. }
  566. my $query = "SELECT t.extends,
  567. coalesce (t.table_name, 'custom_' || extends)
  568. || ':' || f.field_name as field_def
  569. FROM custom_table_catalog t
  570. JOIN custom_field_catalog f USING (table_id)";
  571. my $sth = $self->{dbh}->prepare($query);
  572. $sth->execute;
  573. my $ref;
  574. while ( $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  575. push @{ $self->{custom_db_fields}{ $ref->{extends} } },
  576. $ref->{field_def};
  577. }
  578. }
  579. # Deprecated, only here for old code
  580. sub dberror{
  581. my $self = shift @_;
  582. $self->error(@_);
  583. }
  584. sub redo_rows {
  585. my $self = shift @_;
  586. my %args = @_;
  587. my @flds = @{ $args{fields} };
  588. my $count = $args{count};
  589. my $index = ( $args{index} ) ? $args{index} : 'runningnumber';
  590. my @rows;
  591. my $i; # incriment counter use only
  592. for $i ( 1 .. $count ) {
  593. my $temphash = { _inc => $i };
  594. for my $fld (@flds) {
  595. $temphash->{$fld} = $self->{ "$fld" . "_$i" };
  596. }
  597. push @rows, $temphash;
  598. }
  599. $i = 1;
  600. for my $row ( sort { $a->{index} <=> $b->{index} } @rows ) {
  601. for my $fld (@flds) {
  602. $self->{ "$fld" . "_$i" } = $row->{$fld};
  603. }
  604. ++$i;
  605. }
  606. }
  607. sub merge {
  608. my ( $self, $src ) = @_;
  609. for my $arg ( $self, $src ) {
  610. shift;
  611. }
  612. my %args = @_;
  613. my @keys;
  614. if (defined $args{keys}){
  615. @keys = @{ $args{keys} };
  616. }
  617. my $index = $args{index};
  618. if ( !scalar @keys ) {
  619. @keys = keys %{$src};
  620. }
  621. for my $arg ( @keys ) {
  622. my $dst_arg;
  623. if ($index) {
  624. $dst_arg = $arg . "_$index";
  625. }
  626. else {
  627. $dst_arg = $arg;
  628. }
  629. $self->{$dst_arg} = $src->{$arg};
  630. }
  631. }
  632. sub type {
  633. my $self = shift @_;
  634. if (!$ENV{REQUEST_METHOD} or
  635. ( !grep {$ENV{REQUEST_METHOD} eq $_} ("HEAD", "GET", "POST") ) ) {
  636. $self->error("Request method unset or set to unknown value");
  637. }
  638. return $ENV{REQUEST_METHOD};
  639. }
  640. sub DESTROY {}
  641. sub set {
  642. my $self = shift @_;
  643. my %args = @_;
  644. for my $arg (keys(%args)) {
  645. $self->{$arg} = $args{$arg};
  646. }
  647. return 1;
  648. }
  649. sub remove_cgi_globals {
  650. my ($self) = @_;
  651. for my $key (keys %$self){
  652. if ($key =~ /^\./){
  653. delete $self->{key}
  654. }
  655. }
  656. }
  657. sub take_top_level {
  658. my ($self) = @_;
  659. my $return_hash = {};
  660. for my $key (keys %$self){
  661. if (!ref($self->{$key}) && $key !~ /^\./){
  662. $return_hash->{$key} = $self->{$key}
  663. }
  664. }
  665. return $return_hash;
  666. }
  667. 1;