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