| 1 | |||||||||||||||||||||||||||||||||
| 2 | package TUWF::Misc; | ||||||||||||||||||||||||||||||||
| 3 | # Yeah, just put all miscellaneous functions in one module! | ||||||||||||||||||||||||||||||||
| 4 | # Geez, talk about being sloppy... | ||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Scalar::Util 'looks_like_number'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use TUWF::Validate; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 1 | our $VERSION = '1.6'; | |||||||||||||||||||||||||||||||
| 15 | 1 | our @EXPORT_OK = ('uri_escape', 'kv_validate'); | |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub uri_escape { | ||||||||||||||||||||||||||||||
|
Subroutine
uri_escape: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 19 | 0 | utf8::encode(local $_ = shift); | |||||||||||||||||||||||||||||||
| 20 | 0 | s/([^A-Za-z0-9._~-])/sprintf '%%%02X', ord $1/eg; | |||||||||||||||||||||||||||||||
| 21 | 0 | return $_; | |||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 13 | ▶ | sub _template_validate_num { | ||||||||||||||||||||||||||||||
|
Subroutine
_template_validate_num: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 28 | 13 | $_[0] *= 1; # Normalize to perl number | |||||||||||||||||||||||||||||||
| 29 | 13 | ▶ | return 0 if defined($_[1]{min}) && $_[0] < $_[1]{min}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_[1]{"min"} and $_[0] < $_[1]{"min"}
MC/DC100%2/2
defined $_[1]{"min"} and $_[0] < $_[1]{"min"}
defined $_[1]{"min"}
$_[0] < $_[1]{"min"}
Truth table100%3/3
A and B
A= defined $_[1]{"min"}B= $_[0] < $_[1]{"min"}
| |||||||||||||||||||||||||||||||||
| 30 | 9 | ▶ | return 0 if defined($_[1]{max}) && $_[0] > $_[1]{max}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_[1]{"max"} and $_[0] > $_[1]{"max"}
MC/DC100%2/2
defined $_[1]{"max"} and $_[0] > $_[1]{"max"}
defined $_[1]{"max"}
$_[0] > $_[1]{"max"}
Truth table100%3/3
A and B
A= defined $_[1]{"max"}B= $_[0] > $_[1]{"max"}
| |||||||||||||||||||||||||||||||||
| 31 | 6 | return 1; | |||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 1 | my %default_templates = ( | |||||||||||||||||||||||||||||||
| 36 | # JSON number format, regex from http://stackoverflow.com/questions/13340717/json-numbers-regular-expression | ||||||||||||||||||||||||||||||||
| 37 | num => { func => \&_template_validate_num, regex => qr/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/, inherit => ['min','max'] }, | ||||||||||||||||||||||||||||||||
| 38 | int => { func => \&_template_validate_num, regex => qr/^-?(?:0|[1-9]\d*)$/, inherit => ['min','max'] }, | ||||||||||||||||||||||||||||||||
| 39 | uint => { func => \&_template_validate_num, regex => qr/^(?:0|[1-9]\d*)$/, inherit => ['min','max'] }, | ||||||||||||||||||||||||||||||||
| 40 | ascii => { regex => qr/^[\x20-\x7E]*$/ }, | ||||||||||||||||||||||||||||||||
| 41 | email => { regex => $TUWF::Validate::re_email, maxlength => 254 }, | ||||||||||||||||||||||||||||||||
| 42 | weburl => { regex => $TUWF::Validate::re_weburl, maxlength => 65536 }, # the maxlength is a bit arbitrary, but better than unlimited | ||||||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 92 | ▶ | sub kv_validate { | ||||||||||||||||||||||||||||||
|
Subroutine
kv_validate: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 47 | 92 | my($sources, $templates, $params) = @_; | |||||||||||||||||||||||||||||||
| 48 | 92 | $templates = { %default_templates, %$templates }; | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 92 | my @err; | |||||||||||||||||||||||||||||||
| 51 | my %ret; | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 92 | for my $f (@$params) { | |||||||||||||||||||||||||||||||
| 54 | # Inherit some options from templates. | ||||||||||||||||||||||||||||||||
| 55 | !exists($f->{$_}) && _val_from_tpl($f, $_, $templates, $f) | ||||||||||||||||||||||||||||||||
| 56 | 92 | ▶ | for(qw|required default rmwhitespace multi mincount maxcount|); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 92 | my $src = (grep $f->{$_}, keys %$sources)[0]; | |||||||||||||||||||||||||||||||
| 59 | 92 | my @values = $sources->{$src}->($f->{$src}); | |||||||||||||||||||||||||||||||
| 60 | 92 | ▶ | @values = ($values[0]) if !$f->{multi}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | # check each value and add it to %ret | ||||||||||||||||||||||||||||||||
| 63 | 92 | for (@values) { | |||||||||||||||||||||||||||||||
| 64 | 97 | ▶ | my $errfield = _validate_early($_, $f) || _validate($_, $templates, $f); | ||||||||||||||||||||||||||||||
|
Condition100%3/3
_validate_early($_, $f) || _validate($_, $templates, $f)
MC/DC100%2/2
_validate_early($_, $f) || _validate($_, $templates, $f)
_validate_early($_, $f)
_validate($_, $templates, $f)
Truth table100%3/3
A || B
A= _validate_early($_, $f)B= _validate($_, $templates, $f)
| |||||||||||||||||||||||||||||||||
| 65 | 97 | ▶ | next if !$errfield || $errfield eq 'default'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $errfield or $errfield eq "default"
MC/DC100%2/2
not $errfield or $errfield eq "default"
not $errfield
$errfield eq "default"
Truth table100%3/3
not(A) or B
A= not $errfieldB= $errfield eq "default"
| |||||||||||||||||||||||||||||||||
| 66 | 47 | push @err, [ $f->{$src}, $errfield, $f->{$errfield} ]; | |||||||||||||||||||||||||||||||
| 67 | 47 | last; | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | 92 | ▶ | $ret{$f->{$src}} = $f->{multi} ? \@values : $values[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | # check mincount/maxcount | ||||||||||||||||||||||||||||||||
| 72 | 92 | ▶ | push @err, [ $f->{$src}, 'mincount', $f->{mincount} ] if $f->{mincount} && @values < $f->{mincount}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$f->{"mincount"} and @values < $f->{"mincount"}
MC/DC100%2/2
$f->{"mincount"} and @values < $f->{"mincount"}
$f->{"mincount"}
@values < $f->{"mincount"}
Truth table100%3/3
A and B
A= $f->{"mincount"}B= @values < $f->{"mincount"}
| |||||||||||||||||||||||||||||||||
| 73 | 92 | ▶ | push @err, [ $f->{$src}, 'maxcount', $f->{maxcount} ] if $f->{maxcount} && @values > $f->{maxcount}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$f->{"maxcount"} and @values > $f->{"maxcount"}
MC/DC100%2/2
$f->{"maxcount"} and @values > $f->{"maxcount"}
$f->{"maxcount"}
@values > $f->{"maxcount"}
Truth table100%3/3
A and B
A= $f->{"maxcount"}B= @values > $f->{"maxcount"}
| |||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 92 | ▶ | $ret{_err} = \@err if @err; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | 92 | return \%ret; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 896 | ▶ | sub _val_from_tpl { | ||||||||||||||||||||||||||||||
|
Subroutine
_val_from_tpl: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 82 | 896 | my($top_rules, $field, $tpls, $rules) = @_; | |||||||||||||||||||||||||||||||
| 83 | 896 | ▶ | return if !$rules->{template}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 379 | my $tpl = $tpls->{$rules->{template}}; | |||||||||||||||||||||||||||||||
| 85 | 379 | ▶ | if(exists $tpl->{$field}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 17 | $top_rules->{$field} = $tpl->{$field}; | |||||||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||||||
| 88 | 362 | _val_from_tpl($top_rules, $field, $tpls, $tpl); | |||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | # Initial validation of a value. Same as _validate() below, but this one | ||||||||||||||||||||||||||||||||
| 94 | # validates options that need to be checked only once. (The checks in | ||||||||||||||||||||||||||||||||
| 95 | # _validate() may run several times when templates are used). | ||||||||||||||||||||||||||||||||
| 96 | 97 | ▶ | sub _validate_early { # value, \%rules | ||||||||||||||||||||||||||||||
|
Subroutine
_validate_early: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||
| 97 | 97 | my($v, $r) = @_; | |||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 97 | ▶ | $r->{required}++ if not exists $r->{required}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 100 | 97 | ▶ | $r->{rmwhitespace}++ if not exists $r->{rmwhitespace}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | # remove whitespace | ||||||||||||||||||||||||||||||||
| 103 | 97 | ▶ | if($v && $r->{rmwhitespace}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$v and $r->{"rmwhitespace"}
MC/DC100%2/2
$v and $r->{"rmwhitespace"}
$v
$r->{"rmwhitespace"}
Truth table100%3/3
A and B
A= $vB= $r->{"rmwhitespace"}
| |||||||||||||||||||||||||||||||||
| 104 | 75 | $_[0] =~ s/\r//g; | |||||||||||||||||||||||||||||||
| 105 | 75 | $_[0] =~ s/^[\s\n]+//; | |||||||||||||||||||||||||||||||
| 106 | 75 | $_[0] =~ s/[\s\n]+$//; | |||||||||||||||||||||||||||||||
| 107 | 75 | $v = $_[0] | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | # empty | ||||||||||||||||||||||||||||||||
| 111 | 97 | ▶ | if(!defined($v) || length($v) < 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $v or length $v < 1
MC/DC100%2/2
not defined $v or length $v < 1
not defined $v
length $v < 1
Truth table100%3/3
not(A) or B
A= not defined $vB= length $v < 1
| |||||||||||||||||||||||||||||||||
| 112 | 9 | ▶ | return 'required' if $r->{required}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 113 | 3 | ▶ | $_[0] = $r->{default} if exists $r->{default}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | 3 | return 'default'; | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | 88 | return undef; | |||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | # Internal function used by kv_validate, checks one value on the validation | ||||||||||||||||||||||||||||||||
| 121 | # rules, the name of the failed rule on error, undef otherwise | ||||||||||||||||||||||||||||||||
| 122 | 155 | ▶ | sub _validate { # value, \%templates, \%rules | ||||||||||||||||||||||||||||||
|
Subroutine
_validate: called CC=19 SCAR=29.5
| |||||||||||||||||||||||||||||||||
| 123 | 155 | my($v, $t, $r) = @_; | |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 155 | ▶ | croak "Template $r->{template} not defined." if $r->{template} && !$t->{$r->{template}}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$r->{"template"} and not $$t{$r->{"template"}}
MC/DC0%0/2
$r->{"template"} and not $$t{$r->{"template"}}
$r->{"template"}
not $$t{$r->{"template"}}
Truth table66%2/3
A and not(B)
A= $r->{"template"}B= not $$t{$r->{"template"}}
| |||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | # length | ||||||||||||||||||||||||||||||||
| 128 | 155 | ▶ | return 'minlength' if $r->{minlength} && length $v < $r->{minlength}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$r->{"minlength"} and length $v < $r->{"minlength"}
MC/DC100%2/2
$r->{"minlength"} and length $v < $r->{"minlength"}
$r->{"minlength"}
length $v < $r->{"minlength"}
Truth table100%3/3
A and B
A= $r->{"minlength"}B= length $v < $r->{"minlength"}
| |||||||||||||||||||||||||||||||||
| 129 | 153 | ▶ | return 'maxlength' if $r->{maxlength} && length $v > $r->{maxlength}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$r->{"maxlength"} and length $v > $r->{"maxlength"}
MC/DC100%2/2
$r->{"maxlength"} and length $v > $r->{"maxlength"}
$r->{"maxlength"}
length $v > $r->{"maxlength"}
Truth table100%3/3
A and B
A= $r->{"maxlength"}B= length $v > $r->{"maxlength"}
| |||||||||||||||||||||||||||||||||
| 130 | # enum | ||||||||||||||||||||||||||||||||
| 131 | 150 | ▶ | return 'enum' if $r->{enum} && !grep $_ eq $v, @{$r->{enum}}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$r->{"enum"} and not grep(($_ eq $v), @{$$r{"enum"};})
MC/DC100%2/2
$r->{"enum"} and not grep(($_ eq $v), @{$$r{"enum"};})
$r->{"enum"}
not grep(($_ eq $v), @{$$r{"enum"};})
Truth table100%3/3
A and not(B)
A= $r->{"enum"}B= not grep(($_ eq $v), @{$$r{"enum"};})
| |||||||||||||||||||||||||||||||||
| 132 | # regex | ||||||||||||||||||||||||||||||||
| 133 | 149 | ▶ | return 'regex' if $r->{regex} && (ref($r->{regex}) eq 'ARRAY' ? ($v !~ m/$r->{regex}[0]/) : ($v !~ m/$r->{regex}/)); | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$r->{"regex"} and ref $r->{"regex"} eq "ARRAY" ? !($v =~ /$r->{"regex"}[0]/i) : !($v =~ /$r->{"regex"}/)
MC/DC100%2/2
$r->{"regex"} and ref $r->{"regex"} eq "ARRAY" ? !($v =~ /$r->{"regex"}[0]/i) : !($v =~ /$r->{"regex"}/)
$r->{"regex"}
ref $r->{"regex"} eq "ARRAY" ? !($v =~ /$r->{"regex"}[0]/i) : !($v =~ /$r->{"regex"}/)
Truth table100%3/3
A and B
A= $r->{"regex"}B= ref $r->{"regex"} eq "ARRAY" ? !($v =~ /$r->{"regex"}[0]/i) : !($v =~ /$r->{"regex"}/)
| |||||||||||||||||||||||||||||||||
| 134 | # template | ||||||||||||||||||||||||||||||||
| 135 | 124 | ▶ | if($r->{template}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | 67 | my $in = $t->{$r->{template}}{inherit}; | |||||||||||||||||||||||||||||||
| 137 | 67 | ▶ | my %r = (($in ? (map exists($r->{$_}) ? ($_,$r->{$_}) : (), @$in) : ()), %{$t->{$r->{template}}}); | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 138 | 67 | ▶ | return 'template' if _validate($_[0], $t, \%r); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | # function | ||||||||||||||||||||||||||||||||
| 141 | 92 | ▶ | return 'func' if $r->{func} && (ref($r->{func}) eq 'ARRAY' ? !$r->{func}[0]->($_[0], $r) : !$r->{func}->($_[0], $r)); | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$r->{"func"} and ref $r->{"func"} eq "ARRAY" ? !$r->{"func"}[0]->($_[0], $r) : !$r->{"func"}->($_[0], $r)
MC/DC100%2/2
$r->{"func"} and ref $r->{"func"} eq "ARRAY" ? !$r->{"func"}[0]->($_[0], $r) : !$r->{"func"}->($_[0], $r)
$r->{"func"}
ref $r->{"func"} eq "ARRAY" ? !$r->{"func"}[0]->($_[0], $r) : !$r->{"func"}->($_[0], $r)
Truth table100%3/3
A and B
A= $r->{"func"}B= ref $r->{"func"} eq "ARRAY" ? !$r->{"func"}[0]->($_[0], $r) : !$r->{"func"}->($_[0], $r)
| |||||||||||||||||||||||||||||||||
| 142 | # passed validation | ||||||||||||||||||||||||||||||||
| 143 | 82 | return undef; | |||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | sub TUWF::Object::formValidate { | ||||||||||||||||||||||||||||||
|
Subroutine
formValidate: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 150 | 0 | my($self, @fields) = @_; | |||||||||||||||||||||||||||||||
| 151 | return kv_validate( | ||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | { post => sub { $self->reqPosts(shift) }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | get => sub { $self->reqGets(shift) }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | param => sub { $self->reqParams(shift) }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | cookie => sub { $self->reqCookie(shift) }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 156 | }, $self->{_TUWF}{validate_templates} || {}, | ||||||||||||||||||||||||||||||||
| 157 | \@fields | ||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | ); | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'_TUWF'}{'validate_templates'} || {}
MC/DC0%0/1
$self->{'_TUWF'}{'validate_templates'} || {}
$self->{'_TUWF'}{'validate_templates'}
Truth table0%0/2
A
A= $self->{'_TUWF'}{'validate_templates'}
| |||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | # A simple mail function, body and headers as arguments. Usage: | ||||||||||||||||||||||||||||||||
| 164 | # $self->mail('body', header1 => 'value of header 1', ..); | ||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub TUWF::Object::mail { | ||||||||||||||||||||||||||||||
|
Subroutine
mail: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||
| 166 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 167 | 0 | my $body = shift; | |||||||||||||||||||||||||||||||
| 168 | 0 | my %hs = @_; | |||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | croak "No To: specified!\n" if !$hs{To}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | croak "No Subject: specified!\n" if !$hs{Subject}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | $hs{'Content-Type'} ||= 'text/plain; charset=\'UTF-8\''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$hs{"Content-Type"} ||= "text/plain; charset='UTF-8'"
MC/DC0%0/1
$hs{"Content-Type"} ||= "text/plain; charset='UTF-8'"
$hs{"Content-Type"}
Truth table0%0/2
A
A= $hs{"Content-Type"}
| |||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | $hs{From} ||= $self->{_TUWF}{mail_from}; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$hs{"From"} ||= $self->{"_TUWF"}{"mail_from"}
MC/DC0%0/2
$hs{"From"} ||= $self->{"_TUWF"}{"mail_from"}
$hs{"From"}
$self->{"_TUWF"}{"mail_from"}
Truth table0%0/3
A ||= B
A= $hs{"From"}B= $self->{"_TUWF"}{"mail_from"}
| |||||||||||||||||||||||||||||||||
| 174 | 0 | $body =~ s/\r?\n/\n/g; | |||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 0 | my $mail = ''; | |||||||||||||||||||||||||||||||
| 177 | 0 | foreach (keys %hs) { | |||||||||||||||||||||||||||||||
| 178 | 0 | $hs{$_} =~ s/[\r\n]//g; | |||||||||||||||||||||||||||||||
| 179 | 0 | $mail .= sprintf "%s: %s\n", $_, $hs{$_}; | |||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||
| 181 | 0 | $mail .= sprintf "\n%s", $body; | |||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | if($self->{_TUWF}{mail_sendmail} eq 'log') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 184 | 0 | $self->log("tuwf->mail(): The following mail would have been sent:\n$mail"); | |||||||||||||||||||||||||||||||
| 185 | } elsif(open(my $mailer, '|-:utf8', "$self->{_TUWF}{mail_sendmail} -t -f '$hs{From}'")) { | ||||||||||||||||||||||||||||||||
| 186 | 0 | print $mailer $mail; | |||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | croak "Error running sendmail ($!)" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 188 | if !close($mailer); | ||||||||||||||||||||||||||||||||
| 189 | } else { | ||||||||||||||||||||||||||||||||
| 190 | 0 | croak "Error opening sendail ($!)"; | |||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | sub TUWF::Object::compile { | ||||||||||||||||||||||||||||||
|
Subroutine
compile: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 196 | 0 | TUWF::Validate::compile($_[0]{_TUWF}{custom_validations}, $_[1]); | |||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | sub _compile { | ||||||||||||||||||||||||||||||
|
Subroutine
_compile: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | ref $_[0] eq 'TUWF::Validate' ? $_[0] : $TUWF::OBJ->compile($_[0]); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | sub TUWF::Object::validate { | ||||||||||||||||||||||||||||||
|
Subroutine
validate: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||||||
| 206 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 207 | 0 | my $what = shift; | |||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | return _compile($_[0])->validate($self->reqJSON) if $what eq 'json'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | # 'param' is special, and not really encouraged. Create a new hash based on | ||||||||||||||||||||||||||||||||
| 212 | # reqParam() and cache the result. | ||||||||||||||||||||||||||||||||
| 213 | $self->{_TUWF}{Req}{PARAM} ||= { | ||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | map { my @v = $self->reqParams($_); +($_, @v > 1 ? \@v : $v[0]) } $self->reqParams() | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$self->{"_TUWF"}{"Req"}{"PARAM"} ||= {map({my(@v) = $self->reqParams($_);
$_, @v > 1 ? \@v : $v[0];} $self->reqParams)}
MC/DC0%0/1
$self->{"_TUWF"}{"Req"}{"PARAM"} ||= {map({my(@v) = $self->reqParams($_);
$_, @v > 1 ? \@v : $v[0];} $self->reqParams)}
$self->{"_TUWF"}{"Req"}{"PARAM"}
Truth table0%0/2
A
A= $self->{"_TUWF"}{"Req"}{"PARAM"}
| |||||||||||||||||||||||||||||||||
| 215 | } if $what eq 'param'; | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | my $source = | ||||||||||||||||||||||||||||||||
| 218 | $what eq 'get' ? $self->{_TUWF}{Req}{GET} : | ||||||||||||||||||||||||||||||||
| 219 | $what eq 'post' ? $self->{_TUWF}{Req}{POST} : | ||||||||||||||||||||||||||||||||
| 220 | $what eq 'param' ? $self->{_TUWF}{Req}{PARAM} | ||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | : croak "Invalid source type '$what'"; | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | # Multi-value, schema hash or object | ||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | return _compile($_[0])->validate($source) if @_ == 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | # Single value | ||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | return _compile($_[1])->validate($source->{$_[0]}) if @_ == 2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | # Multi-value, separate params | ||||||||||||||||||||||||||||||||
| 230 | 0 | _compile({ type => 'hash', keys => { @_ } })->validate($source); | |||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | # Internal function used by other TUWF modules to find an appropriate JSON | ||||||||||||||||||||||||||||||||
| 235 | # module. Kinda like JSON::MaybeXS, but without an extra dependency. | ||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | sub _JSON { | ||||||||||||||||||||||||||||||
|
Subroutine
_JSON: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | return 'JSON::XS' if $INC{'JSON/XS.pm'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | return 'Cpanel::JSON::XS' if $INC{'Cpanel/JSON/XS.pm'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | return 'JSON::PP' if $INC{'JSON/PP.pm'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | return 'JSON::XS' if eval { require JSON::XS; 1 }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | return 'Cpanel::JSON::XS' if eval { require Cpanel::JSON::XS; 1 }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | die "Unable to load a suitable JSON module: $@" if !eval { require JSON::PP; 1 }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 243 | 0 | 'JSON::PP' | |||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | 1 | 1; | |||||||||||||||||||||||||||||||