| 1 | package Template::EmbeddedPerl::Utils; | ||||||||||
| 2 | |||||||||||
| 3 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 7 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 7 | ▶ | use URI::Escape (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 7 | ▶ | use JSON::MaybeXS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 7 | our @EXPORT_OK = qw( | |||||||||
| 10 | normalize_linefeeds | ||||||||||
| 11 | uri_escape | ||||||||||
| 12 | escape_javascript | ||||||||||
| 13 | generate_error_message | ||||||||||
| 14 | ); | ||||||||||
| 15 | |||||||||||
| 16 | # uri_escape is a function from URI::Escape | ||||||||||
| 17 | # it is used to escape the uri string. | ||||||||||
| 18 | # uri_escape('http://www.google.com') => 'http%3A%2F%2Fwww.google.com' | ||||||||||
| 19 | |||||||||||
| 20 | sub uri_escape { | ||||||||||
| 21 | 0 | ▶ | my ($string) = @_; | ||||||||
|
Subroutine
uri_escape: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 22 | 0 | return URI::Escape::uri_escape($string); | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | # normalized the line endings to \n from mac and windows format. | ||||||||||
| 26 | |||||||||||
| 27 | sub normalize_linefeeds { | ||||||||||
| 28 | 63 | ▶ | my ($template) = @_; | ||||||||
|
Subroutine
normalize_linefeeds: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 29 | 63 | $template =~ s/\r\n/\n/g; | |||||||||
| 30 | 63 | $template =~ s/\r/\n/g; | |||||||||
| 31 | 63 | return $template; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | # Create a JSON encoder | ||||||||||
| 35 | 7 | my $json = JSON::MaybeXS->new(utf8 => 0, ascii => 1, allow_nonref => 1); | |||||||||
| 36 | |||||||||||
| 37 | # Define the escape_javascript function | ||||||||||
| 38 | sub escape_javascript { | ||||||||||
| 39 | 6 | ▶ | my ($javascript) = @_; | ||||||||
|
Subroutine
escape_javascript: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 40 | 6 | ▶ | return '' unless defined $javascript; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | # Encode the string as a JSON string | ||||||||||
| 43 | 6 | my $escaped = $json->encode($javascript); | |||||||||
| 44 | |||||||||||
| 45 | # Remove the surrounding quotes added by JSON encoding | ||||||||||
| 46 | 6 | $escaped =~ s/^"(.*)"$/$1/; | |||||||||
| 47 | |||||||||||
| 48 | # Escape additional characters not handled by JSON encoding | ||||||||||
| 49 | 6 | $escaped =~ s/`/\\`/g; # Escape backticks | |||||||||
| 50 | 6 | $escaped =~ s/\$/\\\$/g; # Escape dollar signs | |||||||||
| 51 | 6 | $escaped =~ s/'/\\'/g; # Escape single quotes | |||||||||
| 52 | |||||||||||
| 53 | 6 | return $escaped; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | sub generate_error_message { | ||||||||||
| 57 | 2 | ▶ | my ($msg, $template, $source) = @_; | ||||||||
|
Subroutine
generate_error_message: called CC=9 SCAR=22.5
Pod
documented
| |||||||||||
| 58 | |||||||||||
| 59 | 2 | ▶ | warn "RAW MESSAGE: [$msg]" if $ENV{DEBUG_TEMPLATE_EMBEDDED_PERL}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | |||||||||||
| 61 | 2 | ▶ | $source = $source ? "$source" : 'unknown'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 2 | my @files; | |||||||||
| 64 | 2 | ▶ | push @files, [$1, $2, $3, $msg] while $msg =~ /^(.+?) at\s+(.+?)\s+line\s+(\d+)/gm; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | 2 | my $text = ''; | |||||||||
| 67 | 2 | foreach my $file (@files) { | |||||||||
| 68 | 3 | my ($msg, $file, $line, $extra) = @$file; | |||||||||
| 69 | 3 | ▶ | if($file !~ m/eval/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | $text .= $extra; | |||||||||
| 71 | 0 | next; | |||||||||
| 72 | } | ||||||||||
| 73 | 3 | $text .= "$msg at $source line $line\n\n"; | |||||||||
| 74 | |||||||||||
| 75 | 3 | $line--; | |||||||||
| 76 | 3 | ▶ | my $start = $line -1 >= 0 ? $line -1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 3 | ▶ | my $end = $line + 1 < scalar(@$template) ? $line + 1 : scalar(@$template) - 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 3 | for my $i ($start..$end) { | |||||||||
| 79 | 8 | $text .= "@{[ $i+1 ]}: $template->[$i]\n"; | |||||||||
| 80 | } | ||||||||||
| 81 | 3 | $text .= "\n"; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 2 | return "$text\n"; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 7 | 1; | |||||||||
| 88 | |||||||||||
| 89 | |||||||||||
| 90 | =head1 NAME | ||||||||||
| 91 | |||||||||||
| 92 | Template::EmbeddedPerl::Utils - Utility functions for Template::EmbeddedPerl | ||||||||||
| 93 | |||||||||||
| 94 | =head1 DESCRIPTION | ||||||||||
| 95 | |||||||||||
| 96 | This module provides utility functions for L<Template::EmbeddedPerl>. It is not intended to be used directly. | ||||||||||
| 97 | |||||||||||
| 98 | =head1 EXPORTS | ||||||||||
| 99 | |||||||||||
| 100 | =head2 normalize_linefeeds | ||||||||||
| 101 | |||||||||||
| 102 | my $normalized = normalize_linefeeds($template); | ||||||||||
| 103 | |||||||||||
| 104 | Normalize the line endings to \n from mac and windows format. | ||||||||||
| 105 | |||||||||||
| 106 | =head2 uri_escape | ||||||||||
| 107 | |||||||||||
| 108 | my $escaped = uri_escape($string); | ||||||||||
| 109 | |||||||||||
| 110 | Escape the uri string. | ||||||||||
| 111 | |||||||||||
| 112 | =head2 escape_javascript | ||||||||||
| 113 | |||||||||||
| 114 | my $escaped = escape_javascript($javascript); | ||||||||||
| 115 | |||||||||||
| 116 | Escape the javascript string. This basically takes a string and escapes it so that it can be | ||||||||||
| 117 | embedded in a JavaScript string. So it escapes single quotes, backticks, and dollar signs and | ||||||||||
| 118 | that sort of this. It is not guaranteed to protect against all forms of XSS attacks. If you | ||||||||||
| 119 | are embedding user input in a JavaScript string, you should be sure to have cleaned that first | ||||||||||
| 120 | probably using HTML or URI escaping, or running the string through a JavaScript sanitizer to | ||||||||||
| 121 | remove any potentially harmful code. | ||||||||||
| 122 | |||||||||||
| 123 | =head2 generate_error_message | ||||||||||
| 124 | |||||||||||
| 125 | my $error_message = generate_error_message($msg, $template, $source); | ||||||||||
| 126 | |||||||||||
| 127 | Generate an error message. | ||||||||||
| 128 | |||||||||||
| 129 | =head1 SEE ALSO | ||||||||||
| 130 | |||||||||||
| 131 | L<Template::EmbeddedPerl> | ||||||||||
| 132 | |||||||||||
| 133 | =head1 AUTHOR | ||||||||||
| 134 | |||||||||||
| 135 | See L<Template::EmbeddedPerl> | ||||||||||
| 136 | |||||||||||
| 137 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 138 | |||||||||||
| 139 | See L<Template::EmbeddedPerl> | ||||||||||
| 140 | |||||||||||
| 141 | =cut | ||||||||||