Opps, wrong license copied. Should be GNU GPL v2
[phproxy.git] / javascript.js
1 var proxy_url_form_name       = 'poxy_url_form';  
2 var proxy_settings_form_name  = 'poxy_settings_form';
3 var flags_var_name            = 'hl';
4
5 /* the variables above should match the $config variables in index.php */
6
7 var alpha1 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
8 var alpha2 = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
9 var alnum  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._';
10
11 function str_rot13(str)
12 {
13     var newStr = '';
14     var curLet, curLetLoc;
15
16     for (var i = 0; i < str.length; i++)
17     {
18         curLet    = str.charAt(i);
19         curLetLoc = alpha1.indexOf(curLet);
20
21         if (curLet == '#')
22         {
23            window.document.getElementById('proxy_form').action += str.substring(i, str.length)
24         }
25
26         newStr += (curLetLoc < 0) ? curLet : alpha2.charAt(curLetLoc);
27      }
28
29     return newStr;
30 }
31
32
33
34 /* base64 encode code below is not my own, although I did modify it. */
35
36 function base64_encode(str)
37 {
38     var out = '';
39     var t, x, y ,z;
40
41     for (var i = 0; i < str.length; i += 3)
42     {
43         t = Math.min(3, str.length - i);
44         if (t == 1)
45         {
46             x = str.charCodeAt(i);
47             out += alnum.charAt((x >> 2));
48             out += alnum.charAt(((x & 0X00000003) << 4));
49             out += '--';
50         } 
51         else if (t == 2)
52         {
53             x = str.charCodeAt(i);
54             y = str.charCodeAt(i+1);
55             out += alnum.charAt((x >> 2));
56             out += alnum.charAt((((x & 0X00000003) << 4) | (y >> 4)));
57             out += alnum.charAt(((y & 0X0000000f) << 2));
58             out += '-';
59         }
60         else
61         {
62             x = str.charCodeAt(i);
63             y = str.charCodeAt(i+1);
64             z = str.charCodeAt(i+2);
65             out += alnum.charAt((x >> 2));
66             out += alnum.charAt((((x & 0x00000003) << 4) | (y >> 4)));
67             out += alnum.charAt((((y & 0X0000000f) << 2) | (z >> 6)));
68             out += alnum.charAt((z & 0X0000003f));
69         }
70     }
71
72     return out;
73 }
74
75 function submit_form()
76 {
77     var url           = document.forms[proxy_settings_form_name].url.value;
78     var flags         = '';
79     var rotate13      = document.forms[proxy_settings_form_name].elements['ops[]'][5].checked
80     var base64        = document.forms[proxy_settings_form_name].elements['ops[]'][6].checked;
81
82     for (i = 0; i < document.forms[proxy_settings_form_name].elements['ops[]'].length; i++)
83     {
84         flags += (document.forms[proxy_settings_form_name].elements['ops[]'][i].checked == true) ? '1' : '0';
85     }
86
87     document.forms[proxy_url_form_name].elements[flags_var_name].value = flags;
88     document.forms[proxy_url_form_name].target = (document.forms[proxy_settings_form_name].new_window.checked == true) ? '_blank' : '_top';
89
90     if (rotate13)
91     {
92         url = str_rot13(url);
93     }
94     else if (base64)
95     {
96         url = base64_encode(url);
97     }
98
99     document.forms[proxy_url_form_name].url_input.value = url;
100     document.forms[proxy_url_form_name].submit();
101     return false;
102 }