Login   Register  
PHP Classes
elePHPant
Icontem

File: Xyndravandria/Averazain/XyndravandriaAverazain.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Mauro Di Girolamo  >  Xyndravandria Averazain  >  Xyndravandria/Averazain/XyndravandriaAverazain.js  >  Download  
File: Xyndravandria/Averazain/XyndravandriaAverazain.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Xyndravandria Averazain
Handle AJAX requests with PHP classes
Author: By
Last change: Alpha 0.0.0
Date: 1 year ago
Size: 10,700 bytes
 

Contents

Class file image Download
/*
=============================================================================================================================================
|   This file is part of a project released under the terms of the Xyndravandria PHP License (XyndravandriaPHPLicense.txt).                 |
|                                                                                                                                           |
|   You should be given a copy of the Xyndravandria PHP License (XyndravandriaPHPLicense.txt) within the same directory as the README.md;   |
|   if not, you can get a copy at http://Xyndravandria.ohost.de/XyndravandriaPHPLicense.txt .                                               |
|                                                                                                                                           |
|   The copyright (c) of this project is owned by Mauro Di Girolamo <maurodigirolamo@.web.de>.                                              |
============================================================================================================================================|



Xyndravandria Averazain
-----------------------
Alpha 0.0.0

Xyndravandria is the name of a collection of projects designed and developed by Mauro Di Girolamo (maurodigirolamo@web.de); he is therefore the copyright (c) owner of Xyndravandria itself and all of its projects.

Xyndravandria Averazain is released under the terms of the Xyndravandria PHP License (XyndravandriaPHPLicense.txt). You should be given a copy of the Xyndravandria PHP License (XyndravandriaPHPLicense.txt) within the same directory as the README.md; if not, you can get a copy at http://Xyndravandria.ohost.de/XyndravandriaPHPLicense.txt . There might be a release under a freer license for a later, more stable version.

The documentation is either included in ./admin_media/Documentation/ or can be read at http://Xyndravandria.ohost.de/Averazain/Documentation/.

All projects:

   Xyndravandria Averazain
   http://github.com/MauroDiGirolamo/Xyndravandria_Averazain
   PHP
   Averazain is an Ajax framework supporting also JavaScript disabled clients perfectly - including search engines like Google.
   
   Xyndravandria Dyverath
   http://github.com/MauroDiGirolamo/Xyndravandria_Dyverath
   PHP
   Dyverath is a database access wrapper.
   
   Xyndravandria Erozaver
   http://github.com/MauroDiGirolamo/Xyndravandria_Erozaver
   PHP
   Erozaver is a class extending the type hinting given by the PHP engine (additional support for basic type hinting and size constraints).
   
   Xyndravandria Mondraviel
   http://github.com/MauroDiGirolamo/Xyndravandria_Mondraviel
   PHP
   Mondraviel is a class used to separate HTML from PHP code by firstly register models - files containing place holders embedded in HTML code - and then later fill them dynamically with content by passing values for the place holders.
*/

// TODO: Analyse Xyndravandria_With_Request_Spamm.
// TODO: Revise visibility of the methods.
function XyndravandriaAverazain( ) {
   XyndravandriaAverazain.Sending = false;
}

XyndravandriaAverazain.OnInit = function( ) {
   XyndravandriaAverazain.RetrieveAnchor( );
   return;
};

XyndravandriaAverazain.RetrieveAnchor = function( ) {
   if( location.hash !== '' )
      XyndravandriaAverazain.Queue( 'GET', location.hash.substr( 1, location.hash.length - 1 ) + '&Request' ); // TODO: Debug XyndravandriaAverazain.Queue( 'GET', null, location.hash.substr( 1, location.hash.length - 1 ) + '&Request' );
   return;
};

XyndravandriaAverazain.Request = function( SendMethod, Class, Method, Argument ) {
   var ArgumentParsed = 'Xyndravandria->Averazain->Request->Class=' + XyndravandriaAverazain.Mask( Class ) + '&Xyndravandria->Averazain->Request->Method=' + XyndravandriaAverazain.Mask( Method );
   var Form = null;
   if( typeof( Argument ) !== 'undefined' ) {
      ArgumentParsed += '&Xyndravandria->Averazain->Request->Argument=';
      for( var a = 0; a < Argument.length; ++a )
         ArgumentParsed += XyndravandriaAverazain.Mask( Argument[ a ] ) + ( a === Argument.length - 1 ? '' : ',' ); // TODO: Escaping?
   }
   if( XyndravandriaAverazain.AttachedForm !== null )
      ArgumentParsed += XyndravandriaAverazain.ReadOutAttachedForm( );
   XyndravandriaAverazain.Queue( SendMethod, ArgumentParsed );
   return;
};

XyndravandriaAverazain.Queue = function( SendMethod, Argument ) {
   if( XyndravandriaAverazain.Sending )
      window.setTimeout( 'XyndravandriaAverazain.Queue( \'' + SendMethod + '\', "' + Argument + '" );', 1 );
   else
      XyndravandriaAverazain.Send( SendMethod, Argument );
   return;
};

XyndravandriaAverazain.Send = function( SendMethod, Argument ) {
   XyndravandriaAverazain.Sending = true;
   var XHR = XyndravandriaAverazain.CreateXHR( );
   if( XHR === null ) {
      alert( 'Your browser does not support XMLHttpRequests!' );
      XyndravandriaAverazain.Sending = false;
   } else {
       XHR.onreadystatechange = function( ) {
         XyndravandriaAverazain.OnRequestStateChange( XHR, Argument );
         return;
      }
      if( SendMethod === 'POST' ) {
         XHR.open( 'POST', document.URL, true );
         XHR.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
         XHR.setRequestHeader( 'Content-length', Argument.length );
         XHR.setRequestHeader( 'Connection', 'close' );
         XHR.send( Argument );
      } else if( SendMethod === 'GET' ) {
         var URLBlank = document.URL.substr( 0, document.URL.length - location.hash.length );
         XHR.open( 'GET', URLBlank + '?' + Argument, true );
         XHR.send( null );
      } else {
         alert( 'Unknown send method: \'' + SendMethod + '\'!' );
         XyndravandriaAverazain.Sending = false;
      }
   }
   return;
};

XyndravandriaAverazain.CreateXHR = function( Argument ) {
   var XHR = null;
   try { 
      XHR = new XMLHttpRequest( ); // IE7+, Firefox, Chrome, Opera, Safari, ...
   } catch ( Exception1 ) { 
      try { 
         XHR = new ActiveXObject( 'Microsoft.XMLHTTP' ); // IE6, IE5, ...
      } catch ( Exception2 ) { 
         try { 
            XHR = new ActiveXObject( 'Msxml2.XMLHTTP' ); // Even older...
         } catch ( Exception3 ) {
            return XHR;
         }
      }
   }
   return XHR;
};

XyndravandriaAverazain.ExecuteJavascript = function( Text ) { // TODO: Implement option to disable executing of JavaScript within responses (which would lead to more security when thinking about XSS) in general?
   // -- Algorithm a) --
   //var Pattern = /<script type = "text\/javascript">([\s\S]+?)<\/script>/;
   //var Result;
   //while( ( Result = Pattern.exec( Text ) ) !== null ) {
   //   eval( Result[ 1 ] );
   //   Text = Text.replace( '<script type = "text\/javascript">' + Result[ 1 ] + '<\/script>', '' );
   //} 

   // -- Algorithm b) --
   while( Text.indexOf( '<script type = "text/javascript">' ) > -1 ) {
      var ScriptTagStart = Text.indexOf( '<script type = "text/javascript">' );
      var ScriptStart = ScriptTagStart + '<script type = "text/javascript">'.length;
      var ScriptLength = Text.indexOf( '<\/script>', ScriptStart ) - ScriptStart;
      var ScriptEnd = ScriptStart + ScriptLength;
      var ScriptTagEnd = ScriptEnd + '<\/script>'.length;
      eval( Text.substr( ScriptStart, ScriptLength ) );
      Text = Text.substr( 0, ScriptTagStart ) + Text.substr( ScriptTagEnd, Text.length - ScriptTagEnd );
   }
   
   // When it comes to performance, according to my measuring, b) is twice as fast as a).
   return Text;
};

XyndravandriaAverazain.OnRequestStateChange = function( XHR, Argument ) {
    /*
    XHR.readyState
    0 - Uninitialized.
    1 - Loading.
    2 - Loaded.
    3 - Interactice.
    4 - Complete.
   */
   if( XHR.readyState === 4 && XHR.status === 200 ) {
      var ResponseText = XyndravandriaAverazain.ExecuteJavascript( XHR.responseText );

      // Read out attached target within ResponseText and cut it off.
      var a = ResponseText.length;
      while( --a > 0 && ResponseText[ a ] != '$' );
      var Target = ResponseText.substr( a + 1, ResponseText.length - a );
      ResponseText = ResponseText.substr( 0, a );

      if( Target != null && document.getElementById( Target ) != null )
         document.getElementById( Target ).innerHTML = ResponseText;
      XyndravandriaAverazain.Sending = false;
   }
   return;
};

XyndravandriaAverazain.AttachForm = function( Form ) {
   XyndravandriaAverazain.AttachedForm = Form;
   return;
};

XyndravandriaAverazain.ReadOutAttachedForm = function( ) {
   var Form = XyndravandriaAverazain.AttachedForm;
   var Argument = '';
   var Element;
   if( typeof( Form ) !== 'undefined' ) // TODO: Retest attaching of form data.
      for( var a = 0; a < Form.elements.length; a++ ) {
         Element = Form.elements[ a ];
         switch( Element.type ) {
            case 'text':
            case 'password':
            case 'hidden':
            case 'textarea':
               Argument += '&' + XyndravandriaAverazain.Mask( Element.name ) + '=' + XyndravandriaAverazain.Mask( Element.value );
               break;
            case 'checkbox':
               if( Element.checked )
                  Argument += '&' + XyndravandriaAverazain.Mask( Element.name ) + '=1';
               else
                  Argument += '&' + XyndravandriaAverazain.Mask( Element.name ) + '=0';
               break;
            case 'radio':
               if( Element.checked )
                  Argument += '&' + XyndravandriaAverazain.Mask( Element.name ) + '=' + XyndravandriaAverazain.Mask( Element.value );
               else
                  Argument += '&' + XyndravandriaAverazain.Mask( Element.name ) + '=';
               break;
            case 'select-one':
               Argument += '&' + XyndravandriaAverazain.Mask( Element.name ) + '=' + XyndravandriaAverazain.Mask( Element.options[ Element.selectedIndex ].value );
               break;
            default:
               break;
         }
      }
   return Argument;
};

XyndravandriaAverazain.Mask = function( Value ) {
   Value = new String( Value );
   return Value.split( "\n" ).join( '[a]' ).split( '&' ).join( '[b]' ).split( ',' ).join( '[c]' ).split( ':' ).join( '[d]' ).split( '#' ).join( '[e]' ).split( '=' ).join( '[f]' );
};

XyndravandriaAverazain.SetHash = function( Hash, Argument ) {
   Hash = XyndravandriaAverazain.Mask( Hash );
   if( Argument.length > 0 ) {
      Hash += ':' + XyndravandriaAverazain.Mask( Argument[ 0 ] );
      for( var a = 1; a < Argument.length; ++a )
         Hash += ',' + XyndravandriaAverazain.Mask( Argument[ a ] );
   }
   location.hash = Hash;
   return;
};