uHTML/uScramble.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 7 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 166 CC 15 SCAR 28.6
CC
15
Cov
0%
_Scramble
42.0
ScJavaScriptData
30.0
ScrambleText
12.0
CRAP 17.5
1  
2  
3 #
4 # Author: Roland Mosler - info@Jorga-Interactive.de
5 #
6 # Das ist eine uHTML-Bibliothek vom Jorga Interactive
7 # Es ist erlaubt, diese Datei als Teil eines Software-Pakets
8 # von Jorga Interactive zu nutzen.
9 # Es gelten die Bedingungen des Hauptpakets
10 # Jegliche Nutzung außerhalb dieses Pakets ist untersagt.
11 #
12 # Fehler bitte an info@Jorga-Interactive.de
13 #
14 # This is an uHTML library from Jorga Interactive
15 # It is allowed to use this file as a part of
16 # a software package from Jorga Interactive
17 # The terms and conditions of the main package apply to this file
18 # Any use without the main package is prohibited.
19 #
20 # Please report errors to info@Jorga-Interactive.de
21 #
22 # © Roland Mosler, Jorga interactive
23 #
24  
25  
26  
27 use strict ;
28  
29 package uHTML::uScramble ;
30  
31 use version ; our $VERSION = "0.95" ;
32  
33 use uHTML ;
34  
35 sub _GetGlobVal
36 {
37 my $env = shift ;
38 $env->{'uScramble.GlobVal'} = int rand 2147483647 unless $env->{'uScramble.GlobVal'} ;
39 return $env->{'uScramble.GlobVal'} ;
40 }
41  
42 sub fak
43 {
44 my $n = @_ ;
45 my $r = $n-- ;
46 $r *= $n-- while $n > 1 ;
47 return $r
48 }
49  
50 sub permutate
51 {
52 my( $N,@S ) = @_ ;
53 my( $l,$i,@P ) ;
54  
55 for( $i=$#S; $i>=0; $i-- )
56 {
57 $l = $N % ($i+1) ;
58 $N = int($N/($i+1)) ;
59 $P[$i] = $S[$l] ;
60 $S[$l] = $S[$i] ;
61 }
62 return @P ;
63 }
64  
65 sub upermutate
66 {
67 my( $N,@S ) = @_ ;
68 my( @P ) ;
69 my @Per = permutate( $N,( 0 .. $#S ) ) ;
70  
71 $P[$Per[$_]] = $S[$_] foreach( 0 .. $#S ) ;
72 return @P ;
73 }
74  
75  
76 sub GlobCode
77 {
78 return _GetGlobVal( $_[0]->{'ENV'} ) ;
79 }
80  
81 ### scrambling of short strings not good yet
82  
83 sub _Scramble
84 {
85 my $GlobVal = shift ;
86 my( $R,$C,$c,$B,$b,$L,$S ) ;
87  
88 $S = $_[0] =~ s/(\W)/sprintf( ':%02X',ord $1 )/egsr ;
89  
90 if( ($L = length $S) > 14 )
91 {
92 for( $b=14; $b>4; $b-- )
93 {
94 if( $c = $L % $b ) { $C = $c, $B = $b if $c > $C }
95 else { $C = 0, $B = $b ; last }
96 }
97 $C = int( $L/$B ) + ($C ? 1 : 0) ;
98 }
99 else
100 {
101 $B = $L ; $C = 1 ;
102 }
103  
104 my $code = 12323 | int rand( $B > 12 ? 2147483647 : fak( $B )-1 ) ;
105 for( $c=0; $c<$C; $c++ )
106 {
107 $R .= join( '',permutate( $code,split( //,substr( $S,$c*$B,$B ) ) ) ) ;
108 }
109 return join( '*',$code^$GlobVal,$B,$C,$R ) ;
110 }
111  
112 sub _setID
113 {
114 my $Node = shift ;
115 my $env = $Node->{'ENV'} ;
116 $Node->setAttr( 'id','scramble' . ++$env->{'uScramble.Count'} ) unless $Node->testAttr( 'id' ) ;
117 }
118  
119 sub ScrambleText
120 {
121 my $Node = shift ;
122 my $env = $Node->{'ENV'} ;
123 return unless $Node->end() ;
124 $Node->name( $Node->testAttr( 'tag' ) ? $Node->attr( 'tag' ) : 'span' ) ;
125 $Node->deleteAttr( 'tag' ) ;
126 _setID( $Node ) ;
127 push @{$env->{'uScramble.Coded'}->{ $Node->attr( 'id' ) }},'#' ;
128 my $text = _Scramble( _GetGlobVal( $env ),join( '',@{$Node->map( '','' )} ) ) ;
129 $Node->insert( $text ) ;
130 }
131  
132 sub ScrambleAttr
133 {
134 my( $Node,$Par,$Var,$Text ) = @_ ;
135 my $env = $Node->{'ENV'} ;
136 my $Attr = $Node->rawAttr( $Par ) ;
137 my $H ;
138  
139 _setID( $Node ) ;
140 $H++ while $Attr =~ m/\[\}$H\{\]/ ;
141 $H = "[}$H\{]" ;
142 push @{$env->{'uScramble.Coded'}->{ $Node->attr( 'id' ) }},"$H$Par" ;
143 return( $H . _Scramble( _GetGlobVal( $env ),$Text ) . $H ) ;
144 }
145  
146 sub ScJavaScriptData
147 {
148 my $Node = shift ;
149 my $env = $Node->{'ENV'} ;
150  
151 return unless $env->{'uScramble.Count'} > 0 ;
152  
153 my ( $Text,$id,$par,$GV,$T,$A ) ;
154  
155 $GV = _GetGlobVal( $env ) ;
156 foreach $id( keys %{$env->{'uScramble.Coded'}} )
157 {
158 foreach $par( @{$env->{'uScramble.Coded'}->{$id}} )
159 {
160 $Text .= ($par eq '#') ?
161 ++$T && "deScrambleText( document.getElementById(\"$id\") ) ;\n" :
162 ++$A && "deScrambleAttr( document.getElementById(\"$id\"),\"$par\" ) ;\n" ;
163 }
164 }
165  
166 $A = ! $A ? "" : "function deScrambleAttr( elem,param )
167 {
168 var D = param.match( /^\\[\\}\\d*\\{\\]/ ) ;
169 var P = param.replace( /^\\[\\}\\d*\{\\]/,\"\" ) ;
170 var A = elem.getAttribute( P ) ;
171  
172 if( A )
173 {
174 var V = elem.getAttribute( P ).split( D[0] ) ;
175 V[1] = deScrambleString( V[1] ) ;
176 elem.setAttribute( P,V.join(\"\") ) ;
177 }
178 }" ;
179  
180 $T = !$T ? "" : " function deScrambleText( elem )
181 {
182 elem.innerHTML = deScrambleString( elem.innerHTML ) ;
183 }" ;
184  
185 $Node->map( "
186 <script>
187  
188 var GlobCodeVal = $GV ;
189  
190 function permutate( N,S )
191 {
192 var l,i,P = new Array ;
193  
194 for( i=S.length-1; i>=0; i-- )
195 {
196 l = N % (i+1) ;
197 N = Math.floor(N/(i+1)) ;
198 P[i] = S[l] ;
199 S[l] = S[i] ;
200 }
201 return P ;
202 }
203  
204 function upermutate( N,S )
205 {
206 var P = new Array ;
207 var Per = new Array ;
208 var Num = new Array ;
209 var i ;
210  
211 for( i=S.length-1; i>=0; i-- ) Num[i] = i ;
212 Per = permutate( N,Num ) ;
213  
214 for( i=S.length-1; i>=0; i-- ) P[Per[i]] = S[i] ;
215 return P ;
216 }
217  
218 function deScrambleString( S )
219 {
220 var i,R,B,c ;
221 var D = S.split(\"*\") ;
222 if( D[0].match( /\\D/ ) ) return S ;
223 c = parseInt(D[0])^GlobCodeVal ;
224 R = \"\" ;
225 for( i=0; i<D[2]; i++ )
226 {
227 B = D[3].substr( i*D[1],D[1] ) ;
228 R += upermutate( c,B.split('') ).join('') ;
229 }
230 return decodeURIComponent( R.replace( /\\:(..)/g,\"%\$1\") ) ;
231 }
232  
233 $T
234 $A
235 $Text
236  
237 </script>
238 ",'' ) ;
239 }
240  
241 sub GetScBody
242 {
243 my $Body = shift ;
244 my $env = $Body->{'ENV'} ;
245  
246 return if exists $env->{'uScramble.Code'} and not $Body->testAttr( 'scramble' ) ;
247 $env->{'uScramble.Code'} = uHTMLnode->new( 'ScrambleJSData','',undef,$env ) ;
248 $Body->appendChild( $env->{'uScramble.Code'} ) ;
249 $Body->deleteAttr( 'scramble' ) ;
250 }
251  
252 uHTML::registerTag( 'Scramble',\&ScrambleText ) ;
253 uHTML::registerVar( 'Scramble',\&ScrambleAttr ) ;
254 uHTML::register( 'ScrambleCode',\&GlobCode ) ;
255 uHTML::registerTagCode( 'body',\&GetScBody ) ;
256 uHTML::registerTag( 'ScrambleJSData',\&ScJavaScriptData ) ;
257  
258  
259 ######################################
260 ######################################
261  
262 =pod
263  
264 =encoding UTF-8
265  
266 =head1 NAME
267  
268 =over 2
269  
270 =item B<uHTML::uScramble> - Scrambling B<HTML> code
271  
272 =back
273  
274 =head1 VERSION
275  
276 Version 0.95
277  
278 =head1 DESCRIPTION
279  
280 The library B<uHTML::uScramble> provides tags and functions to scramble the HTML output.
281 The scrambled HTML code get descrambled in the browser by a JavaScript function after loading.
282 As robots, scanners, crawler, etc. (usually) do not execute JavaScript within the loaded B<HTML>
283 pages, it is a simple method to conceal sensitive data, e.g. email addresses, from them.
284  
285  
286 Requirements
287  
288 The B<uHTML::uScramble> library requires only the main B<uHTML> library.
289  
290  
291 B< >
292  
293 =head1 B<uHTML> tags provided by the uHTML::uScramble library
294  
295 B< >
296  
297  
298  
299 =head2 Scramble
300  
301 =head3 Overview
302  
303  
304 The Scramble tag scrambles its content before sending it to the browser. When selecting content for scrambling please take in account, that this content gets hidden from crawlers and can't get found by e.g. google.
305  
306 Attributes
307  
308 =head4 tag="name"
309  
310 The attribute tag determines the name of the tag with which will replace Scramble. If missing it defaults to span.
311  
312 =head3 Example
313  
314 <Scramble tag="div">John@mail.com</Scramble>
315  
316 =head2 ScrambleCode
317  
318 =head3 Overview
319  
320  
321 The ScrambleCode tag inserts the actually used scramble code number. Is seldom used beyond debug purposes.
322  
323  
324 =head3 Example
325  
326 <ScrambleCode>
327  
328 B< >
329  
330 =head1 Attribute variables and functions provided by the uHTML::Scramble library
331  
332 B< >
333  
334 =head2 $Scramble(text)
335  
336 =head3 Overview
337  
338  
339 The Scramble function returns scrambles the content of an attribute.
340  
341 =head3 Parameters
342  
343 =head4 text
344  
345 The parameter text defines the text to get obscured.
346  
347 =head3 Example
348  
349 <div title="$Scramble(John@mail.com)"> ... </div>
350  
351 =head2 $ScrambleCode(B<>)
352  
353 =head3 Overview
354  
355  
356 The ScrambleCode function returns the actually used scramble code number. Is seldom used beyond debug purposes.
357  
358  
359 =head3 Example
360  
361 <Scramble title="$ScrambleCode"> ... </Scramble>
362  
363  
364