~eugeniabahit/europiocode/trunk

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
/*
* EuropioCode JS
*
* Codificador y decodificador de código.
*
* Codifica cadenas de texto convirtiendo caracteres no alfanuméricos en pseudo
* codigo, sanitizando así, cualquier campo de formulario previo a su
* envío. Luego, decodifica el pseudocódigo convirtiéndolo en entidades 
* hexadecimales de HTML.
* Utilizado de forma conjunta con ModSecurity y las reglas de OWASP,
* lograrán formularios invulnerables con aplicaciones 100% funcionales, gracias
* a su deodificador que interpretará el código de forma tal, que sean evitados
* los falsos positivos de ModSecurity. 
*
* EuropioCode is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* EuropioCode is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
*
*
* @package    EuropioCode
* @license    http://www.gnu.org/licenses/gpl.txt  GNU GPL 3.0
* @author     Eugenia Bahit <ebahit@member.fsf.org>
* @link       http://www.europio.org
* @version    beta 1
*/


function EuropioCode() {

    this.tabla = {
      '!': 33, '"': 34, '#': 35, '$': 36, '%': 37, '&': 38, "'": 39,
      '(': 40, ')': 41, '*': 42, '+': 43, ',': 44, '.': 46, '/': 47,
      ':': 58, '<': 60, '=': 61, '>': 62, '?': 63, '@': 64, '[': 91,
      '\\': 92, ']': 93, '^': 94, '_': 95, '`': 96, '{': 123, '|': 124,
      '}': 125, '~': 126, '€': 128, ' ': 160, '¡': 161, '£': 163, '«': 171,
      '´': 180, '·': 183, '»': 187, '¿': 191, 'Ç': 199, 'ç': 231,
      'Á': 193, 'É': 201, 'Í': 205, 'Ó': 211, 'Ú': 218, 'Ü': 220, 'Ñ': 209,
      'á': 225, 'é': 233, 'í': 237, 'ó': 243, 'ú': 250, 'ü': 252, 'ñ': 241,
      '\t': '09'
    };

    this.preformat_prefix = "pFt";

    this.preformat_table = new Array(
            "<b>", "<strong>", "<i>", "<em>", "<u>", 
            "<strike>", "<sub>", "<sup>",
            "<p>", "<blockquote>", "<hr>",
            "<ul>", "<ol>", "<li>",
            "<h1>", "<h2>", "<h3>", "<h4>", "<h5>", "<h6>",
            "<code>", "<pre>", "<br>"
    );

    /**
    * Codificar el valor de un campo de formulario en formato EuropioCode
    *
    * @param  (string) campo -- id del campo de formulario a ser codificado
    * @return void
    */
    this.encode = function(campo) {
        cadena_original = document.getElementById(campo).value;
        longitud_cadena = cadena_original.length;

        resultado = cadena_original.replace(/-/g, 'ECODG45ECODC');
        resultado = resultado.replace(/;/g, 'ECODG59ECODC');

        for(i=0; i<longitud_cadena; i++) {
            buscar = cadena_original[i];
            codigo_reemplazo = "ECODG" + this.tabla[buscar] + "ECODC";
            reemplazo = this.tabla[buscar] ? codigo_reemplazo : buscar;
            resultado = resultado.replace(buscar, reemplazo);
        }

        resultado = resultado.replace(/\n/g, 'ECODS');

        document.getElementById(campo).value = resultado;
        document.getElementById(campo).readOnly = 'ReadOnly';
    };


    /**
    * Decodifica una cadena en formato EuropioCode a sus entidades HTML
    *
    * @param  (string) cadena -- la cadena a ser decodificada
    * @param  (string) tipo_salto -- (opcional) '\n' o '<br>'
    * @return (string) retorno -- la cadena convertida a entidades HTML
    */
    this.decode = function(cadena, tipo_salto) {
        break_line = (tipo_salto) ? tipo_salto : '\n';
        retorno = cadena.replace(/ECODS/g, break_line);
        retorno = retorno.replace(/ECODG/g, '&#');
        retorno = retorno.replace(/ECODC/g, ';');
        return retorno;
    };


    /**
    * Codificar el valor de un campo de formulario de texto con preformato,
    * respetando los tags HTML permitidos, especificados en el array 
    * EuropioCode.preformat_table
    *
    * @param  (string) campo -- id del campo de formulario a ser codificado
    * @return void
    */
    this.encode_preformat = function(campo) {
        texto_preformateado = document.getElementById(campo).value;
        cadena = texto_preformateado;
        longitud_tabla = this.preformat_table.length;
        for(i=0; i<longitud_tabla; i++) {
            numero = (i < 10) ? '0' + i : i;console.log(numero);

            tag_apertura = new RegExp(this.preformat_table[i], 'g');
            pFt_apertura = this.preformat_prefix + numero;
            cadena = cadena.replace(tag_apertura, pFt_apertura);
            
            tag_cierre_str = this.preformat_table[i].replace("<", "</");
            tag_cierre = new RegExp(tag_cierre_str, 'g');
            pFt_cierre = this.preformat_prefix + 'e' + numero;
            cadena = cadena.replace(tag_cierre, pFt_cierre);
        } 
        
        document.getElementById(campo).value = cadena;
        document.getElementById(campo).readOnly = 'ReadOnly';
        this.encode(campo)
    };

}