| blib/lib/Ananke/Utils.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 3 | 165 | 1.8 |
| branch | 0 | 10 | 0.0 |
| condition | n/a | ||
| subroutine | 1 | 9 | 11.1 |
| pod | 8 | 8 | 100.0 |
| total | 12 | 192 | 6.2 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | #!/usr/bin/perl | ||||||
| 2 | # # # # # # | ||||||
| 3 | # Diversar funcoes para algum tipo | ||||||
| 4 | # de comunicacao com o browser do | ||||||
| 5 | # usuario. | ||||||
| 6 | # Tue Mar 20 12:34:00 BRT 2001 | ||||||
| 7 | # # # # # # | ||||||
| 8 | |||||||
| 9 | package Ananke::Utils; | ||||||
| 10 | 1 | 1 | 563 | use strict; | |||
| 1 | 2 | ||||||
| 1 | 2369 | ||||||
| 11 | |||||||
| 12 | our $VERSION = '1.0.2'; | ||||||
| 13 | $SIG{__WARN__} = sub { }; | ||||||
| 14 | |||||||
| 15 | sub getCookies { | ||||||
| 16 | # cookies are seperated by a semicolon and a space, this will split | ||||||
| 17 | # them and return a hash of cookies | ||||||
| 18 | 0 | 0 | 1 | my(@rawCookies) = split (/; /,$ENV{'HTTP_COOKIE'}); | |||
| 19 | 0 | my(%cookies); | |||||
| 20 | |||||||
| 21 | 0 | my($key,$val); | |||||
| 22 | 0 | foreach(@rawCookies){ | |||||
| 23 | 0 | ($key, $val) = split (/=/,$_); | |||||
| 24 | 0 | $cookies{$key} = $val; | |||||
| 25 | } | ||||||
| 26 | |||||||
| 27 | 0 | return %cookies; | |||||
| 28 | } | ||||||
| 29 | |||||||
| 30 | # Trata os dados do form | ||||||
| 31 | # Formata timestamp. | ||||||
| 32 | # Sat Mar 17 22:31:06 BRT 2001 | ||||||
| 33 | sub getTime { | ||||||
| 34 | 0 | 0 | 1 | my($time) = @_; | |||
| 35 | 0 | my ($seg,$min,$hora) = localtime($time); | |||||
| 36 | 0 | return sprintf("(%02d:%02d:%02d)",$hora,$min,$seg); | |||||
| 37 | } | ||||||
| 38 | |||||||
| 39 | # Substitui chars proibidos | ||||||
| 40 | sub replace_chars { | ||||||
| 41 | 0 | 0 | 1 | my($msg) = @_; | |||
| 42 | 0 | $_ = $msg; | |||||
| 43 | |||||||
| 44 | 0 | s/©/©/g; s/õ/õ/g; s/®/®/g; s/ö/ö/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 45 | 0 | s/ø/ø/g; s/"/"/g; s/ù/ù/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 46 | 0 | s/ú/ú/g; s/</g; s/û/û/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 47 | 0 | s/>/>/g; s/ý/ý/g; s/À/À/g; s/þ/þ/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 48 | 0 | s/Á/Á/g; s/ÿ/ÿ/g; s/Â/Â/g; s/:/:/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 49 | 0 | s/Ã/Ã/g; s/Ä/Ä/g; s/Å/Å/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 50 | 0 | s/Æ/Æ/g; s/Ç/Ç/g; s/È/È/g; s/É/É/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 51 | 0 | s/Ê/Ê/g; s/Ë/Ë/g; s/Ì/Ì/g; s/Í/Í/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 52 | 0 | s/Î/Î/g; s/Ï/Ï/g; s/Ð/Ð/g; s/Ñ/Ñ/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 53 | 0 | s/Õ/Õ/g; s/Ö/Ö/g; s/Ø/Ø/g; s/Ù/Ù/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 54 | 0 | s/Ú/Ú/g; s/Û/Û/g; s/Ü/Ü/g; s/Ý/Ý/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 55 | 0 | s/Þ/Þ/g; s/ß/ß/g; s/à/à/g; s/á/á/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 56 | 0 | s/å/å/g; s/æ/æ/g; s/ç/ç/g; s/è/è/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 57 | 0 | s/é/é/g; s/ê/ê/g; s/ë/ë/g; s/ì/ì/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 58 | 0 | s/í/í/g; s/î/î/g; s/ï/ï/g; s/ð/ð/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 59 | 0 | s/ñ/ñ/g; s/ò/ò/g; s/ó/ó/g; s/ô/ô/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 60 | 0 | s/ã/ã/g; | |||||
| 61 | 0 | s/£/£/g; s/§/§/g; s/«/«/g; s/¥/¥/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 62 | 0 | s/¯/¯/g; s/»/»/g; s/×/×/g; s/ð/ð/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 63 | 0 | s/¢/¢/g; s/¤/¤/g; s/¦/¦/g; s/¬/¬/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 64 | 0 | s/º/º/g; s/½/½/g; s/¼/¼/g; s/¾/¾/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 65 | 0 | s/ª/ª/g; s/´/´/g; s/¶/¶/g; s/·/·/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 66 | 0 | s/¸/¸/g; s/¹/¹/g; s/÷/÷/g; s/³/³/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 67 | 0 | s/¿/¿/g; s/Ð/Ð/g; s/¨/¨/g; s/¡/¡/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 68 | 0 | eval { s/±/±/g; }; | |||||
| 0 | |||||||
| 69 | |||||||
| 70 | 0 | s/!/!/g; s/@/@/g; s/\$/$/g; s/%/%/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 71 | 0 | s/\*/*/g; s/\(/(/g; s/\)/)/g; | |||||
| 0 | |||||||
| 0 | |||||||
| 72 | 0 | s/\////g; s/\\/\/g; | |||||
| 0 | |||||||
| 73 | |||||||
| 74 | 0 | return $_; | |||||
| 75 | |||||||
| 76 | } | ||||||
| 77 | |||||||
| 78 | # Recupega Post com multiples values | ||||||
| 79 | sub getForm { | ||||||
| 80 | 0 | 0 | 1 | my($r,$rr) = @_; | |||
| 81 | 0 | my($i,@j,$k,%r); | |||||
| 82 | |||||||
| 83 | # Printa conteudo | ||||||
| 84 | 0 | 0 | if ($r) { $r = $r; } | ||||
| 0 | 0 | ||||||
| 85 | 0 | elsif ($rr) { $r = $rr; } | |||||
| 86 | 0 | else { return; } | |||||
| 87 | |||||||
| 88 | 0 | $i = $r; | |||||
| 89 | |||||||
| 90 | 0 | while ($i =~ s/^([a-zA-Z0-9-_\%\.\,\+]+)=([a-zA-Z0-9-_\*\@\%\.\,\+]+)?&?//sx) { | |||||
| 91 | 0 | $j[0] = $1; | |||||
| 92 | 0 | $j[1] = $2; | |||||
| 93 | |||||||
| 94 | # Trasnforma os chars especiais em normais | ||||||
| 95 | 0 | $j[0] =~ tr/+/ /; | |||||
| 96 | 0 | $j[0] =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; | |||||
| 0 | |||||||
| 97 | 0 | $j[1] =~ tr/+/ /; | |||||
| 98 | 0 | $j[1] =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; | |||||
| 0 | |||||||
| 99 | |||||||
| 100 | # Verifica quantas vezes se repete | ||||||
| 101 | 0 | $k = $r =~ s/(^|&)($j[0]=)/$1$2/gi; | |||||
| 102 | |||||||
| 103 | # Verifica se joga em array ou hash | ||||||
| 104 | 0 | 0 | if ($k > 1) { push (@{$r{$j[0]}},$j[1]); } | ||||
| 0 | |||||||
| 0 | |||||||
| 105 | 0 | else { $r{$j[0]} = $j[1] } | |||||
| 106 | |||||||
| 107 | 0 | $k = 0; | |||||
| 108 | } | ||||||
| 109 | |||||||
| 110 | 0 | 0 | return %r if %r; | ||||
| 111 | 0 | return 0; | |||||
| 112 | } | ||||||
| 113 | |||||||
| 114 | # see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3) | ||||||
| 115 | sub salt { | ||||||
| 116 | 0 | 0 | 1 | my($salt); # initialization | |||
| 117 | 0 | my($i, $rand); | |||||
| 118 | 0 | my(@itoa64) = ( '0' .. '9', 'a' .. 'z', 'A' .. 'Z' ); # 0 .. 63 | |||||
| 119 | |||||||
| 120 | # to64 | ||||||
| 121 | 0 | for ($i = 0; $i < 27; $i++) { | |||||
| 122 | 0 | srand(time + $rand + $$); | |||||
| 123 | 0 | $rand = rand(25*29*17 + $rand); | |||||
| 124 | 0 | $salt .= $itoa64[$rand & $#itoa64]; | |||||
| 125 | } | ||||||
| 126 | |||||||
| 127 | 0 | return $salt; | |||||
| 128 | } | ||||||
| 129 | |||||||
| 130 | # Esconda a string | ||||||
| 131 | sub escape { | ||||||
| 132 | 0 | 0 | 1 | my ($str,$pat) = @_; | |||
| 133 | 0 | 0 | $pat = '^A-Za-z0-9 ' if ( $pat eq '' ); | ||||
| 134 | 0 | $str =~ s/([$pat])/sprintf("%%%02lx", unpack("c",$1))/ge; | |||||
| 0 | |||||||
| 135 | 0 | $str =~ s/ /\+/g; | |||||
| 136 | 0 | return $str; | |||||
| 137 | } | ||||||
| 138 | |||||||
| 139 | # Decoda a string | ||||||
| 140 | sub unescape { | ||||||
| 141 | 0 | 0 | 1 | my ($str) = @_; | |||
| 142 | 0 | $str =~ s/\+/ /g; | |||||
| 143 | 0 | $str =~ s/%(..)/pack("c", hex($1))/ge; | |||||
| 0 | |||||||
| 144 | 0 | return $str; | |||||
| 145 | } | ||||||
| 146 | |||||||
| 147 | # substitui enters por html | ||||||
| 148 | sub clean { | ||||||
| 149 | 0 | 0 | 1 | my($str) = @_; | |||
| 150 | |||||||
| 151 | 0 | $str =~ s/\\r//g; | |||||
| 152 | 0 | $str =~ s/\\n\\n/ /g; |
|||||
| 153 | 0 | $str =~ s/\\n/ /g; |
|||||
| 154 | |||||||
| 155 | 0 | return $str; | |||||
| 156 | } | ||||||
| 157 | |||||||
| 158 | 1; | ||||||
| 159 | |||||||
| 160 | =head1 NAME | ||||||
| 161 | |||||||
| 162 | Ananke::Utils - Utility functions | ||||||
| 163 | |||||||
| 164 | =head1 DESCRIPTION | ||||||
| 165 | |||||||
| 166 | Utility functions used to facility your life | ||||||
| 167 | |||||||
| 168 | =head1 SYNOPSIS | ||||||
| 169 | |||||||
| 170 | See all functions | ||||||
| 171 | |||||||
| 172 | =head1 METHODS | ||||||
| 173 | |||||||
| 174 | =head2 getCookies() | ||||||
| 175 | |||||||
| 176 | Retrieves any cookie information from the browser | ||||||
| 177 | |||||||
| 178 | %cookies = Ananke::Utils::getCookies; | ||||||
| 179 | |||||||
| 180 | =head2 getTime(timestamp) | ||||||
| 181 | |||||||
| 182 | Return time in hh:mm:ss | ||||||
| 183 | |||||||
| 184 | $var = &Ananke::Utils::getTime(time()); | ||||||
| 185 | |||||||
| 186 | =head2 replace_chars(string) | ||||||
| 187 | |||||||
| 188 | Replace all bad chars to html format | ||||||
| 189 | |||||||
| 190 | $var = &Ananke::Utils::escape("«¼TesTЪ"); | ||||||
| 191 | |||||||
| 192 | =head2 getForm(x,x) | ||||||
| 193 | |||||||
| 194 | If you use modperl, this functions is very good | ||||||
| 195 | |||||||
| 196 | my $r = shift; | ||||||
| 197 | my (%form,$i,$j); | ||||||
| 198 | $i=$r->content; $j=$r->args; | ||||||
| 199 | %form = &Ananke::Utils::getForm($i,$j); | ||||||
| 200 | |||||||
| 201 | this function understand array input, id[1], id[2],id[3]... | ||||||
| 202 | |||||||
| 203 | =head2 salt() | ||||||
| 204 | |||||||
| 205 | Return randomic string, used for generate password | ||||||
| 206 | |||||||
| 207 | =head2 escape(string) | ||||||
| 208 | |||||||
| 209 | URL encode | ||||||
| 210 | |||||||
| 211 | http://web/this has spaces' -> 'http://web/this%20has%20spaces' | ||||||
| 212 | |||||||
| 213 | $var = &Ananke::Utils::escape($ENV{'REQUEST_URI'}); | ||||||
| 214 | |||||||
| 215 | =head2 unescape(string) | ||||||
| 216 | |||||||
| 217 | URL decode | ||||||
| 218 | |||||||
| 219 | http://web/this%20has%20spaces -> http://web/this has spaces' | ||||||
| 220 | |||||||
| 221 | $var = &Ananke::Utils::unescape("http://web/this%20has%20spaces"); | ||||||
| 222 | |||||||
| 223 | =head2 clean(string) | ||||||
| 224 | |||||||
| 225 | Convert enter to and 2 enters to
|
||||||
| 226 | |||||||
| 227 | $var = clean($textarea); | ||||||
| 228 | |||||||
| 229 | =head1 AUTHOR | ||||||
| 230 | |||||||
| 231 | Udlei D. R. Nattis | ||||||
| 232 | nattis@anankeit.com.br | ||||||
| 233 | http://www.nobol.com.br | ||||||
| 234 | http://www.anankeit.com.br | ||||||
| 235 | |||||||
| 236 | =cut |