source: trunk/phpgwapi/js/expressoAjax/expressoAjax.js @ 2847

Revision 2847, 12.9 KB checked in by amuller, 14 years ago (diff)

Ticket #1086 - Implementando a classe rsa com passagem entre php e js

Line 
1var debug_controller =false;
2var files = new Array();
3var progressBar;
4var handlerExecuteForm = null;
5
6  if (document.all)
7        {
8                navigator.userAgent.toLowerCase().indexOf('msie 5') != -1 ? is_ie5 = true : is_ie5 = false;
9                is_ie = true;
10                is_moz1_6 = false;
11                is_mozilla = false;
12                is_ns4 = false;
13        }
14        else if (document.getElementById)
15        {
16                navigator.userAgent.toLowerCase().match('mozilla.*rv[:]1\.6.*gecko') ? is_moz1_6 = true : is_moz1_6 = false;
17                is_ie = false;
18                is_ie5 = false;
19                is_mozilla = true;
20                is_ns4 = false;
21        }
22        else if (document.layers)
23        {
24                is_ie = false;
25                is_ie5 = false;
26                is_moz1_6 = false;
27                is_mozilla = false;
28                is_ns4 = true;
29        }
30
31/****************************************** Connector Class *************************************************/
32        // Constructor
33        function cConnector()
34        {
35                this.requests = new Array();
36                this.hold_session = false;
37                this.oxmlhttp = null;
38                this.isVisibleBar = false;
39                this.tid = 0;
40                this.progressBar = null;
41                this.oldX = 0;
42                this.oldY = 0;
43                this.updateVersion = "";
44                this.cacheRequest = false;
45
46        };
47        cConnector.prototype.verify_session = function(data) {
48                if(data && data.nosession){
49                        // If hold session is setted, dont reload the page ...
50                        if(expresso.connector.hold_session) {
51                                if(typeof(write_msg) == "function" && typeof(get_lang) == "function")
52                                        write_msg(get_lang("your session could not be verified."));
53                                else
54                                        alert("your session could not be verified.");
55                        }
56                        else
57                                window.location.reload();
58                        return false;
59                }
60                else
61                        return true;
62        };
63        cConnector.prototype.buildBar = function()
64                {
65                        var div = document.getElementById('divProgressBar');
66
67                        if(! div) {
68                                div = document.createElement("DIV");
69                                div.style.visibility    = "hidden";
70                                div.style.width = "103px";
71                                div.id = 'divProgressBar';
72                                div.align = "center";
73                                var loadingTitle = document.getElementById('txt_loading');
74                                div.innerHTML = '&nbsp;&nbsp;<font face="Verdana" size="2" color="WHITE">'+loadingTitle.value+'...</font>&nbsp;';
75                                div.style.background = "#cc4444";
76                                div.style.position = 'fixed';
77                                div.style.top = '0px';
78                                div.style.right = '0px';
79                                document.body.appendChild(div);
80
81                                if(is_ie) {
82                                        var elem = document.all[div.id];
83                                        elem.style.position="absolute";
84                                        var root = document.body;
85                                        var posX = elem.offsetLeft-root.scrollLeft;
86                                        var posY = elem.offsetTop-root.scrollTop;
87                                        root.onscroll = function() {
88                                                elem.style.right = '0px';
89                                                elem.style.top = (posY + root.scrollTop) + "px";
90                                        };
91                                }
92
93                                if(debug_controller) {
94                                        div = document.createElement("DIV");
95                                        div.style.width = "800px";
96                                        div.style.height= "400px";
97                                        div.id = "debug_controller";
98                                        div.align='right';
99                                        document.body.appendChild(div);
100                                }
101                        }
102        };
103//------------------------------------ BEGIN: Functions for Connector HTTPRequest  -------------------------------------------------//
104        // Serialize Data Method
105        cConnector.prototype.serialize = function(data)
106        {       var _thisObject = this;
107                var f = function(data)
108                {
109                        var str_data;
110
111                        if (data == null ||
112                                (typeof(data) == 'string' && data == ''))
113                        {
114                                str_data = 'N;';
115                        }
116
117                        else switch(typeof(data))
118                        {
119                                case 'object':
120                                        var arrayCount = 0;
121
122                                        str_data = '';
123
124                                        for (i in data)
125                                        {
126                                                if (i == 'length')
127                                                {
128                                                        continue;
129                                                }
130
131                                                arrayCount++;
132                                                switch (typeof(i))
133                                                {
134                                                        case 'number':
135                                                                str_data += 'i:' + i + ';' + f(data[i]);
136                                                                break;
137
138                                                        case 'string':
139                                                                str_data += 's:' + i.length + ':"' + i + '";' + f(data[i]);
140                                                                break;
141
142                                                        default:
143                                                                showMessage(Element('cc_msg_err_serialize_data_unknown').value);
144                                                                break;
145                                                };
146                                        }
147
148                                        if (!arrayCount)
149                                        {
150                                                str_data = 'N;';
151                                        }
152                                        else
153                                        {
154                                                str_data = 'a:' + arrayCount + ':{' + str_data + '}';
155                                        }
156
157                                        break;
158
159                                case 'string':
160                                        str_data = 's:' + data.length + ':"' + data + '";';
161                                        break;
162
163                                case 'number':
164                                        str_data = 'i:' + data + ';';
165                                        break;
166
167                                case 'boolean':
168                                        str_data = 'b:' + (data ? '1' : '0') + ';';
169                                        break;
170
171//                              default:
172//                                      //showMessage(Element('cc_msg_err_serialize_data_unknown').value);
173//                                      //return null;
174                                       
175                        };
176
177                        return str_data;
178                };
179
180                return f(data);
181        };
182
183
184        //Unserialize Data Method
185        cConnector.prototype.unserialize = function(str)
186        {
187
188                _thisObject = this;
189                var matchB = function (str, iniPos)
190                {
191                        var nOpen, nClose = iniPos;
192                        do
193                        {
194                                nOpen = str.indexOf('{', nClose+1);
195                                nClose = str.indexOf('}', nClose+1);
196
197                                if (nOpen == -1)
198                                {
199                                        return nClose;
200                                }
201                                if (nOpen < nClose )
202                                {
203                                        nClose = matchB(str, nOpen);
204                                }
205                        } while (nOpen < nClose);
206
207                        return nClose;
208                };
209
210                var f = function (str)
211                {
212                        switch (str.toString().charAt(0))
213                        {
214                                case 'a':
215
216                                        var data = new Array();
217                                        var n = parseInt( str.substring( str.indexOf(':')+1, str.indexOf(':',2) ) );
218                                        var arrayContent = str.substring(str.indexOf('{')+1, str.lastIndexOf('}'));
219
220                                        for (var i = 0; i < n; i++)
221                                        {
222                                                var pos = 0;
223
224                                                /* Process Index */
225                                                var indexStr = arrayContent.substr(pos, arrayContent.indexOf(';')+1);
226                                                var index = f(indexStr);
227                                                pos = arrayContent.indexOf(';', pos)+1;
228
229                                                /* Process Content */
230                                                var part = null;
231                                                switch (arrayContent.charAt(pos))
232                                                {
233                                                        case 'a':
234                                                                var pos_ = matchB(arrayContent, arrayContent.indexOf('{', pos))+1;
235                                                                part = arrayContent.substring(pos, pos_);
236                                                                pos = pos_;
237                                                                data[index] = f(part);
238                                                                break;
239
240                                                        case 's':
241                                                                var pval = arrayContent.indexOf(':', pos+2);
242                                                                var val  = parseInt(arrayContent.substring(pos+2, pval));
243                                                                pos = pval + val + 4;
244                                                                data[index] = arrayContent.substr(pval+2, val);
245                                                                break;
246
247                                                        default:
248                                                                part = arrayContent.substring(pos, arrayContent.indexOf(';', pos)+1);
249                                                                pos = arrayContent.indexOf(';', pos)+1;
250                                                                data[index] = f(part);
251                                                                break;
252                                                }
253                                                arrayContent = arrayContent.substr(pos);
254                                        }
255                                        break;
256
257                                case 's':
258                                        var pos = str.indexOf(':', 2);
259                                        var val = parseInt(str.substring(2,pos));
260                                        var data = str.substr(pos+2, val);
261                                        str = str.substr(pos + 4 + val);
262                                        break;
263
264                                case 'i':
265                                case 'd':
266                                        var pos = str.indexOf(';');
267                                        var data = parseInt(str.substring(2,pos));
268                                        str = str.substr(pos + 1);
269                                        break;
270
271                                case 'N':
272                                        var data = null;
273                                        str = str.substr(str.indexOf(';') + 1);
274                                        break;
275
276                                case 'b':
277                                        var data = str.charAt(2) == '1' ? true : false;
278                                        break;
279                        };
280                        return data;
281                };
282
283                return f(str);
284        };
285
286        // Request Constructor Connector
287        cConnector.prototype.newRequest = function (id, target, method, handler, data)
288        {
289
290                _connector.go( {
291                        "access" : target,
292                        "handler" : function( data )
293                        {
294                                handler( expresso.connector.unserialize( data ) );
295                        },
296                        "cache": expresso.connector.cacheRequest
297                } );
298
299
300                this.cacheRequest=false;
301        };
302        // Cancel Request Connector
303        cConnector.prototype.cancelRequest = function (){
304                return false;
305        };
306//------------------------------------  END: Functions for Connector HTTPRequest  -------------------------------------------------//
307
308//      ----------------------------------- BEGIN: Functions for build Bar Progress ---------------------------------------------------------//
309        cConnector.prototype.hideProgressBar = function ()
310        {
311                var div = document.getElementById('divProgressBar');
312                div.style.visibility = 'hidden';
313                this.isVisibleBar = false;
314        };
315
316        cConnector.prototype.showProgressBar = function(){
317                var div = document.getElementById('divProgressBar');
318                div.style.visibility = 'visible';
319
320                this.isVisibleBar = true;
321        };
322
323        cConnector.prototype.loadAllScripts = function(scripts) {
324                for(var i = 0; i < scripts.length; i++){
325                        this.loadScript(scripts[i]);
326                }
327        };
328
329        cConnector.prototype.loadScript = function(scriptPath)  {
330        if (document.getElementById('uploadscript_'+scriptPath)) {
331                return;
332        }
333                var head = document.getElementsByTagName("head")[0];
334                var script = document.createElement("SCRIPT");
335                script.id = 'uploadscript_'+scriptPath;
336                script.type = 'text/javascript';
337
338                if(is_ie) {
339
340                        this.oxmlhttp.open("GET", URL_SERVER + "expressoMail1_2/js/"+scriptPath+".js?"+this.updateVersion, false);
341                        this.oxmlhttp.setRequestHeader('Content-Type','text/plain');
342                        this.oxmlhttp.send(null);
343                        if(this.oxmlhttp.status != 0 && this.oxmlhttp.status != 200 ||  this.oxmlhttp.status == 0 && this.oxmlhttp.responseText.length == 0)
344                                throw new Error("Error " + this.oxmlhttp.status + "("+this.oxmlhttp.statusText+") when loading script file '"+scriptPath+"'");
345                        script.text = this.oxmlhttp.responseText;
346                }
347                else {
348                        script.src =  URL_SERVER + "expressoMail1_2/js/"+scriptPath+".js?"+this.updateVersion;
349                }
350
351                head.appendChild(script);
352                return;
353        };
354//------------------------------------  END: Functions for Progress Bar  -------------------------------------------------//
355        // Default Controller File
356        var URL_DEFAULT = URL_SERVER + "controller.php?menuaction=";
357        // connector object
358        var _onmouseup = document.onmouseup;
359        var isExecuteForm = false;
360        var id = null;
361
362        cConnector.prototype.cacheNextRequest = function(){
363                this.cacheRequest=true;
364        };
365
366        //      Function executes AJAX
367        //      cExecute (url, handler, params)
368        //      url: 'module.class.method'
369        //  handle: function handle() receive response.
370        //  params: parameters for POST method
371        //      form: form element (for upload files)
372        function cExecute(url, handler, params, form) {
373                if(isExecuteForm){
374                        isExecuteForm = false;
375                        document.onmouseup = _onmouseup;
376                }
377                if(form) {
378                        cExecuteForm(url, form);
379                        return;
380                }
381                //url = URL_DEFAULT + url;
382                if(params)
383                        method = "POST";
384                else
385                        method = "GET";
386
387                id = url;
388                expresso.connector.newRequest(id, url, method, handler, params);
389        };
390
391// This function executes submit values to Controller (POST)
392        // The return is void.
393        //      cExecuteForm (url, form)
394        //      url: 'module.class.method'
395        //      form: form element (for upload files)
396        function cExecuteForm(url, form, handler,id){
397                expresso.connector.buildBar();
398                isExecuteForm = true;
399
400                document.onmouseup = alertBut;
401
402                expresso.connector.showProgressBar();
403                if(! (divUpload = document.getElementById('divUpload'))) {
404                        divUpload               = document.createElement('DIV');
405                        divUpload.id    = 'divUpload';
406                        document.body.appendChild(divUpload);
407                }
408
409                if(! (el = document.getElementById('_action'))) {
410                        el                      = document.createElement('input');
411                        el.type = 'hidden';
412                        el.id           = '_action';
413                        el.name = '_action';
414                        form.appendChild(el);
415                }
416
417                var divFiles = document.getElementById("divFiles_"+id);
418                if (divFiles && divFiles.firstChild) {
419                        el                      = document.createElement('input');
420                        el.type = 'hidden';
421                        el.name = 'countFiles';
422                        var countDivFiles = 0;
423                        try{
424                                countDivFiles = parseInt(divFiles.lastChild.firstChild.id.split('_')[2]) + 1; // The id of last file
425                                }
426                        catch (e) { var countDivFiles = 0; };
427                        el.value        = countDivFiles ;
428                        form.appendChild(el);
429                }
430
431                form._action.value = url;
432                // Connector Bug fixing: Encapsulating returned handler function
433                handlerExecuteForm = handler;
434                var form_handler = function (data){
435                        handlerExecuteForm(data);
436                        handlerExecuteForm = null;
437                };
438                divUpload.innerHTML= "<iframe onload=\"cExecute('expressoMail1_2.functions.getReturnExecuteForm',"+form_handler+");\"  style='display:"+(debug_controller ? "" : "none")+";width:"+(debug_controller ? 400 : 0)+";height:"+(debug_controller ? 400 : 0)+";' name='uploadFile'></iframe>";
439                form.action = URL_DEFAULT + url;
440                form.target ="uploadFile";
441                form.submit();
442                // Connector Bug fixing: Exclude '_action' element after cExecuteForm execution
443                if(el && el.id == '_action'){
444                        el.parentNode.removeChild(el);
445                }
446        };
447
448
449        function alertBut(e) {
450                if(!e)
451                        e = window.event;
452
453            if(_onmouseup)
454                        _onmouseup(e);
455
456                if(isExecuteForm) {
457                if(confirm(get_lang("There's an action processing. Do you want abort it?"))) {
458                        expresso.connector.hideProgressBar();
459                        isExecuteForm = false;
460                        delete expresso.connector.requests[id];
461                                expresso.connector.requests[id] = null;
462                        stop();
463                        return;
464                }
465                else
466                        return false;
467            }
468        };
469
470function expressoAjax(){
471        this.connector = new cConnector();
472
473};
474expressoAjax.prototype.require = function (module){
475        expresso.connector.loadScript(module);
476};
477
478(function( )
479{
480        var sec_key = null;
481        var mod = null;
482        function encode( data )
483        {
484                var result = "";
485                var val = "";
486                var packsize = 3;
487                for (i=0; i < data.length; i+=packsize){
488                        for (j=i; j < i+packsize; j++)
489                                if (data.charCodeAt(j) > 1)
490                                        val += ""+data.charCodeAt(j);
491                                else
492                                        val += "000";
493                        bigVal = int2bigInt(val,50,10);
494                        val = "";
495                        bigKey = int2bigInt(sec_key,50,10);
496                        bigN = int2bigInt(mod,50,10);
497                        bigResult = powMod(bigVal,bigKey,bigN);
498                        result += bigInt2str(bigResult,10)+" ";
499                }
500                return result;
501        }
502
503        function crypt( input, modulus )
504        {
505                sec_key = input;
506                mod = modulus;
507
508        }
509
510        crypt.prototype.encode = encode;
511        window.crypt = crypt;
512})( );
513
514expressoAjax.prototype.crypt = new crypt(keys[0],keys[1]);
515delete(keys);
516
517var expresso = new expressoAjax();
Note: See TracBrowser for help on using the repository browser.