1/**
2 * JsHttpRequest: JavaScript "AJAX" data loader
3 * Minimized version: see debug directory for the complete one.
4 *
5 * @license LGPL
6 * @author Dmitry Koterov, http://en.dklab.ru/lib/JsHttpRequest/
7 * @version 5.x $Id: script.js 91 2007-04-09 12:29:16Z wingedfox $
8 */
9function JsHttpRequest(){var i=this;i.onreadystatechange=null;i.readyState=0;i.responseText=null;i.responseXML=null;i.status=200;i.statusText="OK";i.responseJS=null;i.caching=false;i.loader=null;i.session_name="PHPSESSID";i._ldObj=null;i._reqHeaders=[];i._openArgs=null;i._errors={inv_form_el:"Invalid FORM element detected: name=%, tag=%",must_be_single_el:"If used, <form> must be a single HTML element in the list.",js_invalid:"JavaScript code generated by backend is invalid!\n%",url_too_long:"Cannot use so long query with GET request (URL is larger than % bytes)",unk_loader:"Unknown loader: %",no_loaders:"No loaders registered at all, please check JsHttpRequest.LOADERS array",no_loader_matched:"Cannot find a loader which may process the request. Notices are:\n%",no_headers:"Method setRequestHeader() cannot work together with the % loader."};i.abort=function(){with(this){if(_ldObj&&_ldObj.abort){_ldObj.abort();}_cleanup();if(readyState==0){return}if(readyState==1&&!_ldObj){readyState=0;return}_changeReadyState(4,true);}};i.open=function(I,l,o,O,Q){with(this){try{if(document.location.search.match(new RegExp("[&?]"+session_name+"=([^&?]*)"))||document.cookie.match(new RegExp("(?:;|^)\\s*"+session_name+"=([^;]*)"))){l+=(l.indexOf("?")>=0?"&":"?")+session_name+"="+this.escape(RegExp.$1);}}catch(e){};_openArgs={method:(I||"").toUpperCase(),url:l,asyncFlag:o,username:O!=null?O:"",password:Q!=null?Q:""};_ldObj=null;_changeReadyState(1,true);return true}};i.send=function(I){if(!this.readyState){return}this._changeReadyState(1,true);this._ldObj=null;var l=[];var o=[];if(!this._hash2query(I,null,l,o)){return}var O=null;if(this.caching&&!o.length){O=this._openArgs.username+":"+this._openArgs.password+"@"+this._openArgs.url+"|"+l+"#"+this._openArgs.method;var Q=JsHttpRequest.CACHE[O];if(Q){this._dataReady(Q[0],Q[1]);return false}}var _=(this.loader||"").toLowerCase();if(_&&!JsHttpRequest.LOADERS[_]){return this._error("unk_loader",_);}var c=[];var C=JsHttpRequest.LOADERS;for(var e in C){var v=C[e].loader;if(!v){continue}if(_&&e!=_){continue}var V=new v(this);JsHttpRequest.extend(V,this._openArgs);JsHttpRequest.extend(V,{queryText:l.join("&"),queryElem:o,id:(new Date().getTime())+""+JsHttpRequest.COUNT++,hash:O,span:null});var x=V.load();if(!x){this._ldObj=V;JsHttpRequest.PENDING[V.id]=this;return true}if(!_){c[c.length]="- "+e.toUpperCase()+": "+this._l(x);}else{return this._error(x);}}return e?this._error("no_loader_matched",c.join("\n")):this._error("no_loaders");};i.getAllResponseHeaders=function(){with(this){return _ldObj&&_ldObj.getAllResponseHeaders?_ldObj.getAllResponseHeaders():[]}};i.getResponseHeader=function(I){with(this){return _ldObj&&_ldObj.getResponseHeader?_ldObj.getResponseHeader():[]}};i.setRequestHeader=function(I,l){with(this){_reqHeaders[_reqHeaders.length]=[I,l]}};i._dataReady=function(I,l){with(this){if(caching&&_ldObj){JsHttpRequest.CACHE[_ldObj.hash]=[I,l]}if(I!==null||l!==null){status=4;responseText=responseXML=I;responseJS=l}else{status=500;responseText=responseXML=responseJS=null}_changeReadyState(2);_changeReadyState(3);_changeReadyState(4);_cleanup();}};i._l=function(I){var l=0,o=0,O=this._errors[I[0]];while((o=O.indexOf("%",o))>=0){var Q=I[++l]+"";O=O.substring(0,o)+Q+O.substring(o+1,O.length);o+=1+Q.length}return O};i._error=function(I){I=this._l(typeof(I)=="string"?arguments:I);I="JsHttpRequest: "+I;if(!window.Error){throw I}else{if((new Error(1,"test")).description=="test"){throw new Error(1,I);}else{throw new Error(I);}}};i._hash2query=function(I,l,o,O){if(l==null){l=""}if((""+typeof(I)).toLowerCase()=="object"){var Q=false;if(I&&I.parentNode&&I.parentNode.appendChild&&I.tagName&&I.tagName.toUpperCase()=="FORM"){I={form:I}}for(var _ in I){var c=I[_];if(c instanceof Function){continue}var C=l?l+"["+this.escape(_)+"]":this.escape(_);var e=c&&c.parentNode&&c.parentNode.appendChild&&c.tagName;if(e){var v=c.tagName.toUpperCase();if(v=="FORM"){Q=true}else{if(v=="INPUT"||v=="TEXTAREA"||v=="SELECT"){}else{return this._error("inv_form_el",(c.name||""),c.tagName);}}O[O.length]={name:C,e:c}}else{if(c instanceof Object){this._hash2query(c,C,o,O);}else{if(c===null){continue}o[o.length]=C+"="+this.escape(""+c);}}if(Q&&O.length>1){return this._error("must_be_single_el");}}}else{o[o.length]=I}return true};i._cleanup=function(){var I=this._ldObj;if(!I){return}JsHttpRequest.PENDING[I.id]=false;var l=I.span;if(!l){return}I.span=null;var o=function(){l.parentNode.removeChild(l);};JsHttpRequest.setTimeout(o,50);};i._changeReadyState=function(I,l){with(this){if(l){status=statusText=responseJS=null;responseText=""}readyState=I;if(onreadystatechange){onreadystatechange();}}};i.escape=function(I){return escape(I).replace(new RegExp("\\+","g"),"%2B");}}JsHttpRequest.COUNT=0;JsHttpRequest.MAX_URL_LEN=2000;JsHttpRequest.CACHE={};JsHttpRequest.PENDING={};JsHttpRequest.LOADERS={};JsHttpRequest._dummy=function(){};JsHttpRequest.TIMEOUTS={s:window.setTimeout,c:window.clearTimeout};JsHttpRequest.setTimeout=function(i,I){window.JsHttpRequest_tmp=JsHttpRequest.TIMEOUTS.s;if(typeof(i)=="string"){l=window.JsHttpRequest_tmp(i,I);}else{var l=null;var o=function(){i();delete JsHttpRequest.TIMEOUTS[l]};l=window.JsHttpRequest_tmp(o,I);JsHttpRequest.TIMEOUTS[l]=o}window.JsHttpRequest_tmp=null;return l};JsHttpRequest.clearTimeout=function(i){window.JsHttpRequest_tmp=JsHttpRequest.TIMEOUTS.c;delete JsHttpRequest.TIMEOUTS[i];var I=window.JsHttpRequest_tmp(i);window.JsHttpRequest_tmp=null;return I};JsHttpRequest.query=function(i,I,l,o){var O=new this();O.caching=!o;O.onreadystatechange=function(){if(O.readyState==4){l(O.responseJS,O.responseText);}};var Q=null;if(i.match(/^((\w+)\.)?(GET|POST)\s+(.*)/i)){O.loader=RegExp.$2?RegExp.$2:null;Q=RegExp.$3;i=RegExp.$4}O.open(Q,i,true);O.send(I);};JsHttpRequest.dataReady=function(i){var I=this.PENDING[i.id];delete this.PENDING[i.id];if(I){I._dataReady(i.text,i.js);}else{if(I!==false){throw"dataReady(): unknown pending id: "+i.id}}};JsHttpRequest.extend=function(i,I){for(var l in I){i[l]=I[l]}};JsHttpRequest.LOADERS.xml={loader:function(i){JsHttpRequest.extend(i._errors,{xml_no:"Cannot use XMLHttpRequest or ActiveX loader: not supported",xml_no_diffdom:"Cannot use XMLHttpRequest to load data from different domain %",xml_no_headers:"Cannot use XMLHttpRequest loader or ActiveX loader, POST method: headers setting is not supported, needed to work with encodings correctly",xml_no_form_upl:"Cannot use XMLHttpRequest loader: direct form elements using and uploading are not implemented"});this.load=function(){if(this.queryElem.length){return["xml_no_form_upl"]}if(this.url.match(new RegExp("^([a-z]+)://([^\\/]+)(.*)","i"))){if(RegExp.$2.toLowerCase()==document.location.hostname.toLowerCase()){this.url=RegExp.$3}else{return["xml_no_diffdom",RegExp.$2]}}var I=null;if(window.XMLHttpRequest){try{I=new XMLHttpRequest();}catch(e){}}else{if(window.ActiveXObject){try{I=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){};if(!I){try{I=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}}}}if(!I){return["xml_no"]}var l=window.ActiveXObject||I.setRequestHeader;if(!this.method){this.method=l?"POST":"GET"}if(this.method=="GET"){if(this.queryText){this.url+=(this.url.indexOf("?")>=0?"&":"?")+this.queryText}this.queryText="";if(this.url.length>JsHttpRequest.MAX_URL_LEN){return["url_too_long",JsHttpRequest.MAX_URL_LEN]}}else{if(this.method=="POST"&&!l){return["xml_no_headers"]}}this.url+=(this.url.indexOf("?")>=0?"&":"?")+"JsHttpRequest="+(i.caching?"0":this.id)+"-xml";var o=this.id;I.onreadystatechange=function(){if(I.readyState!=4){return}I.onreadystatechange=JsHttpRequest._dummy;i.status=null;try{i.status=I.status;i.responseText=I.responseText}catch(e){};if(!i.status){return}try{eval("JsHttpRequest._tmp = function(id) { var d = "+i.responseText+"; d.id = id; JsHttpRequest.dataReady(d); }");}catch(e){return i._error("js_invalid",i.responseText);}JsHttpRequest._tmp(o);JsHttpRequest._tmp=null};I.open(this.method,this.url,true,this.username,this.password);if(l){for(var O=0;O<i._reqHeaders.length;O++){I.setRequestHeader(i._reqHeaders[O][0],i._reqHeaders[O][1]);}I.setRequestHeader("Content-Type","application/octet-stream");}I.send(this.queryText);this.span=null;this.xr=I;return null};this.getAllResponseHeaders=function(){return this.xr.getAllResponseHeaders();};this.getResponseHeader=function(I){return this.xr.getResponseHeader(I);};this.abort=function(){this.xr.abort();this.xr=null}}};JsHttpRequest.LOADERS.script={loader:function(i){JsHttpRequest.extend(i._errors,{script_only_get:"Cannot use SCRIPT loader: it supports only GET method",script_no_form:"Cannot use SCRIPT loader: direct form elements using and uploading are not implemented"});this.load=function(){if(this.queryText){this.url+=(this.url.indexOf("?")>=0?"&":"?")+this.queryText}this.url+=(this.url.indexOf("?")>=0?"&":"?")+"JsHttpRequest="+this.id+"-"+"script";this.queryText="";if(!this.method){this.method="GET"}if(this.method!=="GET"){return["script_only_get"]}if(this.queryElem.length){return["script_no_form"]}if(this.url.length>JsHttpRequest.MAX_URL_LEN){return["url_too_long",JsHttpRequest.MAX_URL_LEN]}if(i._reqHeaders.length){return["no_headers","SCRIPT"]}var I=this,l=document,o=null,O=l.body;if(!window.opera){this.span=o=l.createElement("SCRIPT");var Q=function(){o.language="JavaScript";if(o.setAttribute){o.setAttribute("src",I.url);}else{o.src=I.url}O.insertBefore(o,O.lastChild);}}else{this.span=o=l.createElement("SPAN");o.style.display="none";O.insertBefore(o,O.lastChild);o.innerHTML="Workaround for IE.<s"+"cript></"+"script>";var Q=function(){o=o.getElementsByTagName("SCRIPT")[0];o.language="JavaScript";if(o.setAttribute){o.setAttribute("src",I.url);}else{o.src=I.url}}}JsHttpRequest.setTimeout(Q,10);return null}}};JsHttpRequest.LOADERS.form={loader:function(i){JsHttpRequest.extend(i._errors,{form_el_not_belong:"Element \"%\" does not belong to any form!",form_el_belong_diff:"Element \"%\" belongs to a different form. All elements must belong to the same form!",form_el_inv_enctype:"Attribute \"enctype\" of the form must be \"%\" (for IE), \"%\" given."});this.load=function(){var I=this;if(!I.method){I.method="POST"}I.url+=(I.url.indexOf("?")>=0?"&":"?")+"JsHttpRequest="+I.id+"-"+"form";if(i._reqHeaders.length){return["no_headers","FORM"]}if(I.method=="GET"){if(I.queryText){I.url+=(I.url.indexOf("?")>=0?"&":"?")+I.queryText}if(I.url.length>JsHttpRequest.MAX_URL_LEN){return["url_too_long",JsHttpRequest.MAX_URL_LEN]}var l=I.url.split("?",2);I.url=l[0];I.queryText=l[1]||""}var o=null;var O=false;if(I.queryElem.length){if(I.queryElem[0].e.tagName.toUpperCase()=="FORM"){o=I.queryElem[0].e;O=true;I.queryElem=[]}else{o=I.queryElem[0].e.form;for(var Q=0;Q<I.queryElem.length;Q++){var _=I.queryElem[Q].e;if(!_.form){return["form_el_not_belong",_.name]}if(_.form!=o){return["form_el_belong_diff",_.name]}}}if(I.method=="POST"){var c="multipart/form-data";var C=(o.attributes.encType&&o.attributes.encType.nodeValue)||(o.attributes.enctype&&o.attributes.enctype.value)||o.enctype;if(C!=c){return["form_el_inv_enctype",c,C]}}}var e=o&&(o.ownerDocument||o.document)||document;var v="jshr_i_"+I.id;var V=I.span=e.createElement("DIV");V.style.position="absolute";V.style.display="none";V.style.visibility="hidden";V.innerHTML=(o?"":"<form"+(I.method=="POST"?" enctype=\"multipart/form-data\" method=\"post\"":"")+"></form>")+"<iframe name=\""+v+"\" id=\""+v+"\" style=\"width:0px; height:0px; overflow:hidden; border:none\"></iframe>";if(!o){o=I.span.firstChild}e.body.insertBefore(V,e.body.lastChild);var x=function(_,z){var Z=[];var w=_;if(_.mergeAttributes){var w=e.createElement("form");w.mergeAttributes(_,false);}for(var Q=0;Q<z.length;Q++){var W=z[Q][0],s=z[Q][1];Z[Z.length]=[W,w.getAttribute(W)];w.setAttribute(W,s);}if(_.mergeAttributes){_.mergeAttributes(w,false);}return Z};var X=function(){top.JsHttpRequestGlobal=JsHttpRequest;var z=[];if(!O){for(var Q=0,Z=o.elements.length;Q<Z;Q++){z[Q]=o.elements[Q].name;o.elements[Q].name=""}}var w=I.queryText.split("&");for(var Q=w.length-1;Q>=0;Q--){var W=w[Q].split("=",2);var _=e.createElement("INPUT");_.type="hidden";_.name=unescape(W[0]);_.value=W[1]!=null?unescape(W[1]):"";o.appendChild(_);}for(var Q=0;Q<I.queryElem.length;Q++){I.queryElem[Q].e.name=I.queryElem[Q].name}var s=x(o,[["action",I.url],["method",I.method],["onsubmit",null],["target",v]]);o.submit();x(o,s);for(var Q=0;Q<w.length;Q++){o.lastChild.parentNode.removeChild(o.lastChild);}if(!O){for(var Q=0,Z=o.elements.length;Q<Z;Q++){o.elements[Q].name=z[Q]}}};JsHttpRequest.setTimeout(X,100);return null}}};
10
11/**
12 *  JsHttpRequest controller wrapper
13 *
14 *  @author Ilya Lebedev
15 */
16function RemoteScript () {
17  var _JHRopen = this.open;
18  var _JHRsend = this.send;
19  var _callback = null;
20  /**
21   *  JsHttpRequest 'open' method wrapper
22   *
23   *  @param {String} method name GET, POST, FORM
24   *  @param {String, Array} callback type string means both plugin filename and method name,
25   *                                       array - 0 is plugin name, 1 is method name
26   *  @access public
27   */
28  this.open = function (method, callback) {
29    _JHRopen.call (this,method, DOKU_BASE+'lib/plugins/remotescript/rs.php');
30    _callback = callback;
31  };
32  /**
33   *  JsHttpRequest 'send' method wrapper
34   *
35   *  @param {String, Array, Object} method name GET, POST, FORM
36   *  @access public
37   */
38  this.send = function (content) {
39    /*
40    *  create proper params list
41    */
42    var c = {'args' : content};
43    if (_callback instanceof Array) {
44      c.callback = _callback[0];
45      c.method = _callback[1]
46    } else {
47      c.callback = _callback;
48    }
49    _JHRsend.call (this,c);
50  }
51};
52RemoteScript.prototype = new JsHttpRequest;
53/**
54 *  Wrapper to JsHttpRequest.query method with appropriate corrections
55 *
56 *  @param {String, Array} cbk callback either as function call (string) or class method call (array)
57 *  @param {Object, String, HTMLFormElement} content data to be sent to the server
58 *  @param {Function} onready callback function, should receive 2 params: JS and Text output
59 *  @param {Boolean} nocache disables query caching, when set to true
60 *  @scope public
61 */
62RemoteScript.query = function(url, content, onready, nocache) {
63    var req = new RemoteScript();
64    req.caching = !nocache;
65    req.onreadystatechange = function() {
66        if (req.readyState == 4) {
67            onready(_from_utf8(req.responseJS), _from_utf8(req.responseText));
68        }
69    };
70//    req.loader = 'script';
71    req.method = 'GET';
72    req.open(null, url, true);
73    req.send(content);
74};
75
76
77function _from_utf8(s) {
78  var c, d = "", flag = 0, tmp;
79  for (var i = 0; i < s.length; i++) {
80    c = s.charCodeAt(i);
81    if (flag == 0) {
82      if ((c & 0xe0) == 0xe0) {
83        flag = 2;
84        tmp = (c & 0x0f) << 12;
85      } else if ((c & 0xc0) == 0xc0) {
86        flag = 1;
87        tmp = (c & 0x1f) << 6;
88      } else if ((c & 0x80) == 0) {
89        d += s.charAt(i);
90      } else {
91        flag = 0;
92      }
93    } else if (flag == 1) {
94      flag = 0;
95      d += String.fromCharCode(tmp | (c & 0x3f));
96    } else if (flag == 2) {
97      flag = 3;
98      tmp |= (c & 0x3f) << 6;
99    } else if (flag == 3) {
100      flag = 0;
101      d += String.fromCharCode(tmp | (c & 0x3f));
102    } else {
103      flag = 0;
104    }
105  };
106  return d;
107};
108
109function _to_utf8(s) {
110  var c, d = "";
111  for (var i = 0; i < s.length; i++) {
112    c = s.charCodeAt(i);
113    if (c <= 0x7f) {
114      d += s.charAt(i);
115    } else if (c >= 0x80 && c <= 0x7ff) {
116      d += String.fromCharCode(((c >> 6) & 0x1f) | 0xc0);
117      d += String.fromCharCode((c & 0x3f) | 0x80);
118    } else {
119      d += String.fromCharCode((c >> 12) | 0xe0);
120      d += String.fromCharCode(((c >> 6) & 0x3f) | 0x80);
121      d += String.fromCharCode((c & 0x3f) | 0x80);
122    }
123  };
124  return d;
125};