includes/clientside/static/ajax.js
changeset 326 ab66d6d1f1f4
parent 304 e2cb5f1432c8
parent 320 112debff64bd
child 329 0437a7cf1acc
equal deleted inserted replaced
314:474f8be55943 326:ab66d6d1f1f4
    38   {
    38   {
    39     ajax.setRequestHeader("Content-length", parms.length);
    39     ajax.setRequestHeader("Content-length", parms.length);
    40   }
    40   }
    41   ajax.setRequestHeader("Connection", "close");
    41   ajax.setRequestHeader("Connection", "close");
    42   ajax.send(parms);
    42   ajax.send(parms);
       
    43 }
       
    44 
       
    45 /**
       
    46  * Show a friendly error message depicting an AJAX response that is not valid JSON
       
    47  * @param string Response text
       
    48  * @param string Custom error message. If omitted, the default will be shown.
       
    49  */
       
    50 
       
    51 function handle_invalid_json(response, customerror)
       
    52 {
       
    53   var mainwin = $('ajaxEditContainer').object;
       
    54   mainwin.innerHTML = '';
       
    55   
       
    56   // Title
       
    57   var h3 = document.createElement('h3');
       
    58   h3.appendChild(document.createTextNode('The site encountered an error while processing your request.'));
       
    59   mainwin.appendChild(h3);
       
    60   
       
    61   if ( typeof(customerror) == 'string' )
       
    62   {
       
    63     var el = document.createElement('p');
       
    64     el.appendChild(document.createTextNode(customerror));
       
    65     mainwin.appendChild(el);
       
    66   }
       
    67   else
       
    68   {
       
    69     customerror  = 'We unexpectedly received the following response from the server. The response should have been in the JSON ';
       
    70     customerror += 'serialization format, but the response wasn\'t composed only of the JSON response. There are three possible triggers';
       
    71     customerror += 'for this problem:';
       
    72     var el = document.createElement('p');
       
    73     el.appendChild(document.createTextNode(customerror));
       
    74     mainwin.appendChild(el);
       
    75     var ul = document.createElement('ul');
       
    76     var li1 = document.createElement('li');
       
    77     var li2 = document.createElement('li');
       
    78     var li3 = document.createElement('li');
       
    79     li1.appendChild(document.createTextNode('The server sent back a bad HTTP response code and thus sent an error page instead of running Enano. This indicates a possible problem with your server, and is not likely to be a bug with Enano.'));
       
    80     var osc_exception = ( window.location.hostname == 'demo.opensourcecms.com' ) ? ' This is KNOWN to be the case with the OpenSourceCMS.com demo version of Enano.' : '';
       
    81     li2.appendChild(document.createTextNode('The server sent back the expected JSON response, but also injected some code into the response that should not be there. Typically this consists of advertisement code. In this case, the administrator of this site will have to contact their web host to have advertisements disabled.' + osc_exception));
       
    82     li3.appendChild(document.createTextNode('It\'s possible that Enano triggered a PHP error or warning. In this case, you may be looking at a bug in Enano.'));
       
    83       
       
    84     ul.appendChild(li1);
       
    85     ul.appendChild(li2);
       
    86     ul.appendChild(li3);
       
    87     mainwin.appendChild(ul);
       
    88   }
       
    89   
       
    90   var p2 = document.createElement('p');
       
    91   p2.appendChild(document.createTextNode('The response received from the server is as follows:'));
       
    92   mainwin.appendChild(p2);
       
    93   
       
    94   var pre = document.createElement('pre');
       
    95   pre.appendChild(document.createTextNode(response));
       
    96   mainwin.appendChild(pre);
       
    97   
       
    98   var p3 = document.createElement('p');
       
    99   p3.appendChild(document.createTextNode('You may also choose to view the response as HTML. '));
       
   100   var a = document.createElement('a');
       
   101   a.appendChild(document.createTextNode('View as HTML...'));
       
   102   a._resp = response;
       
   103   a.id = 'invalidjson_link';
       
   104   a.onclick = function()
       
   105   {
       
   106     var mb = new messagebox(MB_YESNO | MB_ICONEXCLAMATION, 'Do you really want to view this response as HTML?', 'If the response was changed during transmission to include malicious code, you may be allowing that malicious code to run by viewing the response as HTML. Only do this if you have reviewed the response text and have found no suspicious code in it.');
       
   107     mb.onclick['Yes'] = function()
       
   108     {
       
   109       var html = $('invalidjson_link').object._resp;
       
   110       var win = window.open('about:blank', 'invalidjson_htmlwin', 'width=550,height=400,status=no,toolbars=no,toolbar=no,address=no,scroll=yes');
       
   111       win.document.write(html);
       
   112     }
       
   113     return false;
       
   114   }
       
   115   a.href = '#';
       
   116   p3.appendChild(a);
       
   117   mainwin.appendChild(p3);
    43 }
   118 }
    44 
   119 
    45 function ajaxEscape(text)
   120 function ajaxEscape(text)
    46 {
   121 {
    47   /*
   122   /*
   923         unsetAjaxLoading();
   998         unsetAjaxLoading();
   924         var resptext = String(ajax.responseText + ' ');
   999         var resptext = String(ajax.responseText + ' ');
   925         resptext = resptext.substr(0, resptext.length-1);
  1000         resptext = resptext.substr(0, resptext.length-1);
   926         if ( resptext.substr(0, 1) != '{' )
  1001         if ( resptext.substr(0, 1) != '{' )
   927         {
  1002         {
   928           alert('Invalid JSON response from server:\n' + resptext);
  1003           handle_invalid_json(resptext);
   929           return false;
  1004           return false;
   930         }
  1005         }
   931         var json = parseJSON(resptext);
  1006         var json = parseJSON(resptext);
   932         var catbox = document.getElementById('mdgCatBox');
  1007         var catbox = document.getElementById('mdgCatBox');
   933         if ( !catbox )
  1008         if ( !catbox )
  1023         addtag_nukeme = false;
  1098         addtag_nukeme = false;
  1024         var resptext = String(ajax.responseText + ' ');
  1099         var resptext = String(ajax.responseText + ' ');
  1025         resptext = resptext.substr(0, resptext.length-1);
  1100         resptext = resptext.substr(0, resptext.length-1);
  1026         if ( resptext.substr(0, 1) != '{' )
  1101         if ( resptext.substr(0, 1) != '{' )
  1027         {
  1102         {
  1028           alert('Invalid JSON response from server:\n' + resptext);
  1103           handle_invalid_json(resptext);
  1029           return false;
  1104           return false;
  1030         }
  1105         }
  1031         var json = parseJSON(resptext);
  1106         var json = parseJSON(resptext);
  1032         var parent = nukeme.parentNode;
  1107         var parent = nukeme.parentNode;
  1033         parent.removeChild(nukeme);
  1108         parent.removeChild(nukeme);