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