1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
# --
# HTML::Safe.pm - remove activ html stuff from html strings
# Copyright (C) 2001-2004 Martin Edenhofer <martin+code@otrs.org>
# --
# $Id: Safe.pm,v 1.1 2004/12/04 11:06:11 martin Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --
package HTML::Safe;
use strict;
use vars qw($VERSION);
$VERSION = "1.0";
=head1 NAME
HTML::Safe - remove activ html stuff from html strings
=head1 SYNOPSIS
A module to remove/strip active html tags/addons (javascript, applets, embeds and objects) from html strings.
=head1 PUBLIC INTERFACE
=over 4
=cut
=item new()
create a object
use HTML::Safe;
my $HTMLSafe = HTML::Safe->new();
Ot if you want do define own filter params
my $HTMLSafe = HTML::Safe->new(
NoApplet => 1,
NoObject => 1,
NoEmbed => 1,
NoIntSrcLoad => 0,
NoExtSrcLoad => 1,
NoJavaScript => 1,
);
=cut
sub new {
my $Type = shift;
my %Param = @_;
# allocate new hash for object
my $Self = {};
bless ($Self, $Type);
$Self->{Debug} = $Param{Debug} || 0;
foreach (qw(NoApplet NoObject NoEmbed NoExtSrcLoad NoIntSrcLoad NoJavaScript)) {
$Self->{$_} = defined($Param{$_}) ? $Param{$_} : 1;
}
return $Self;
}
=item Filter()
To filter html strings.
# get html
my $Data = 'Some HTML with active alements!';
# filter active elements
$HTMLSafe->Filter(Data => \$Data);
# print clean html
print $Data;
=cut
sub Filter {
my $Self = shift;
my %Param = @_;
# check needed stuff
foreach (qw(Data)) {
if (!$Param{$_}) {
print STDERR "Need $_!";
return;
}
}
# remove script tags
if ($Self->{NoJavaScript}) {
${$Param{Data}} =~ s{
<scrip.+?>(.+?)</script>
}
{
# print STDERR "$1 found!";
if ($Self->{Debug} > 0) {
print STDERR "Found <script> tags!\n";
}
if ($Self->{Debug} > 1) {
"### removed script tags ###";
}
else {
"";
}
}segxim;
}
# remove <applet> tags
if ($Self->{NoApplet}) {
${$Param{Data}} =~ s{
<apple.+?>(.+?)</applet>
}
{
# print STDERR "$1 found!";
if ($Self->{Debug} > 0) {
print STDERR "Found <applet> tags!\n";
}
if ($Self->{Debug} > 1) {
"### removed applet tags ###";
}
else {
"";
}
}segxim;
}
# remove <Object> tags
if ($Self->{NoObject}) {
${$Param{Data}} =~ s{
<objec.+?>(.+?)</object>
}
{
# print STDERR "$1 found!";
if ($Self->{Debug} > 0) {
print STDERR "Found <object> tags!\n";
}
if ($Self->{Debug} > 1) {
"### removed object tags ###";
}
else {
"";
}
}segxim;
}
# remove style/javascript parts
if ($Self->{NoJavaScript}) {
${$Param{Data}} =~ s{
<style.+?javascript(.+?|)>(.*)</style>
}
{
# print STDERR "$1 found!";
if ($Self->{Debug} > 0) {
print STDERR "Found <style script> tags!\n";
}
if ($Self->{Debug} > 1) {
"### removed javascript style tag ###";
}
else {
"";
}
}segxim;
}
# check each html tag
${$Param{Data}} =~ s{
(<.+?>)
}
{
my $Tag = $1;
if ($Self->{NoJavaScript}) {
# remove on action sub tags
$Tag =~ s{
\s(on.+?=(".+?"|'.+?'|.+?))
}
{
print STDERR "Found <on action> tags ($1)!\n" if ($Self->{Debug});
"";
}segxim;
# remove entities sub tags
$Tag =~ s{
(&\{.+?\})
}
{
print STDERR "Found js entities tags($1)!\n" if ($Self->{Debug});
"";
}segxim;
# remove javascript in a href links or src links
$Tag =~ s{
(<(a\shref|src)=)("javascript.+?"|'javascript.+?'|javascript.+?)(\s>|>|.+?>)
}
{
if ($Self->{Debug} > 0) {
print STDERR "Found <a href|src/javascript> tags ($3)!\n";
}
if ($Self->{Debug} > 1) {
"$1'-no js-'$4";
}
else {
"$1$4";
}
}segxim;
# remove link javascript tags
$Tag =~ s{
(<link.+?javascript(.+?|)>)
}
{
print STDERR "Found <link/javascript> ($1) tags!\n" if ($Self->{Debug});
"### removed javascript link tag ###";
}segxim;
}
# remove <embed> tags
if ($Self->{NoEmbed}) {
$Tag =~ s{
(<embed\s(.+?)>)
}
{
if ($Self->{Debug} > 0) {
print STDERR "Found <embed> ($1) tags!\n";
}
if ($Self->{Debug} > 1) {
"### removed embed tag ($1) ###";
}
else {
"";
}
}segxim;
}
# remove load tags
if ($Self->{NoIntSrcLoad} || $Self->{NoExtSrcLoad}) {
$Tag =~ s{
(<(.+?)\s(src=.+?)(\s.+?|)>)
}
{
if ($Self->{NoIntSrcLoad} || ($Self->{NoExtSrcLoad} && $3 =~ /(http|ftp|https)::/i)) {
if ($Self->{Debug} > 0) {
print STDERR "Found load tags ($1)!\n";
}
if ($Self->{Debug} > 1) {
"### can't load '$3' ###";
}
else {
"";
}
}
else {
$1;
}
}segxim;
}
# replace original tag with clean tag
$Tag;
}segxim;
}
1;
=head1 TERMS AND CONDITIONS
This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (GPL). If you
did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
=head1 VERSION
$Revision: 1.1 $ $Date: 2004/12/04 11:06:11 $
=cut
|