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