<?php

//
// jsrsServer.php - javascript remote scripting server include
//
// Orginal Author:  Brent Ashley [jsrs@megahuge.com]
// PHP version   :  Sébastien Cramatte [sebastien@webeclaireur.com] 
//		    Pierre Cailleux [cailleux@noos.fr] 
// Date		 :  May 2001 
// 
// see jsrsClient.js for version info
//
//  see license.txt for copyright and license info
//
// 20 Jan 2002: Cleaned up to work with phpLens and with register_globals disabled;
//			Not sure how it works with magic_quotes_gpc() but should not
//			be a problem with phpLens as we rawurlencode all text - John Lim
//			Changed ereg_replace() to str_replace() for speed.

function jsrsDispatch($validFuncs ){
  $func = jsrsBuildFunc($validFuncs);
  
  if ($func != ""){
    $retval;
    
    eval("\$retval =  " . $func . ";");
    
    if (strlen($retval)>0){
      jsrsReturn($retval."");
    } else {
      jsrsReturn("");
    } 
  } else {
    jsrsReturnError("function builds as empty string");
  }
}

function jsrsReturn($payload) {
global $HTTP_POST_VARS,$HTTP_GET_VARS;
 $C = $HTTP_POST_VARS['C'];
 if (empty($C)) $C = $HTTP_GET_VARS['C'];
  
  Print (
      "<html><head></head><body onload=\"p=document.layers?parentLayer:window.parent;p.jsrsLoaded('" 
    . $C . "');\">jsrsPayload:<br>" 
    . "<form name=\"jsrs_Form\"><textarea name=\"jsrs_Payload\">"
    . jsrsEscape($payload) . "</textarea></form></body></html>");
    exit();
}

function jsrsEscape($str){
  // escape ampersands so special chars aren't interpreted
  $tmp = str_replace( "&", "&amp;", $str );
  // escape slashes  with whacks so end tags don't interfere with return html
  return str_replace( "\/" , "\\/",$tmp); 
}

/////////////////////////////
//
// user functions


function jsrsReturnError($str){
global $HTTP_POST_VARS,$HTTP_GET_VARS;
 $C = $HTTP_POST_VARS['C'];
 if (empty($C)) $C = $HTTP_GET_VARS['C'];
 
  // escape quotes
  $cleanStr = str_replace("\'","\\'",$str);
  
  // !!!! --- Warning -- !!!
  $cleanStr = "jsrsError: " . str_replace("\"", "\\\"", $cleanStr); 
  print ("<html><head></head><body " 
         . "onload=\"p=document.layers?parentLayer:window.parent;p.jsrsError('" . $C . "','" . urlencode($str) . "');\">"
         . $cleanStr . "</body></html>" );
  exit();
}

function jsrsArrayToString( $a, $delim ){
  // user function to flatten 1-dim array to string for return to client
  $d = "~";
  if (!isset($delim)) $d = $delim;
  return implode($a,$d); 
}


function jsrsBuildFunc($validFuncs) {
global $HTTP_POST_VARS,$HTTP_GET_VARS;
 $isGET = false;
 $F = @$HTTP_POST_VARS['F'];
 if (empty($F)) {
 	$F = $HTTP_GET_VARS['F'];
	$isGET = true;
}
 
 $func = ""; 
 
 if ($F != "") {
  $func = $F;
  
      
  // make sure it's in the dispatch list
  if (strpos(strtoupper($validFuncs),strtoupper($func))===false)
     jsrsReturnError($func . " is not a valid function" );
   
   $func .= "(";
   $i = 0;
    
   //--- To optimize ! --- 
   if ($isGET) eval("\$P$i = \$HTTP_GET_VARS['P$i'];");
   else eval("\$P$i = \$HTTP_POST_VARS['P$i'];");
   $Ptmp = "P". $i;
     
   while ($$Ptmp!="") {
    $parm = $$Ptmp;
    $parm = substr($parm,1,strlen($parm)-2);
    $func .= "\"" . $parm . "\",";
    $i++;
     if ($isGET) eval("\$P$i = \$HTTP_GET_VARS['P$i'];");
     else eval("\$P$i = \$HTTP_POST_VARS['P$i'];");
    $Ptmp = "P". $i;
   }
   
   if (substr($func,strlen($func)-1,1)==",")  
    $func = substr($func,0,strlen($func)-1);

    $func .= ")";
  } 
 
 return $func;
}

function jsrsEvalEscape($thing) {
 $tmp = str_replace($thing,"\r\n","\n");
 return $tmp;
}

function jsrsVBArrayToString($a,$delim) {
 // --- not use in PHP see jsrsArrayToString method
 return jsrsArrayToString($a,$delim);
}


?>



