<?
/* Version: 0.0.1 */
/* This code is under the open source BSD license. See included license.txt */
/* WAX.php contains all the important waxy functions.
   */

/* Headers that will keep even the nastiest of proxies from caching the requests */
function wax_headers(){
        
header ("Expires: Tue, 16 Aug 1988 08:00:00 GMT");    // This one expired a little while ago
        
header ("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
        
header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
        
header ("Pragma: no-cache");                          // HTTP/1.0
}

/* Checks to see if a wax call is comming in, then handles it. */
function handle_wax_call($wax_class){
    if (@isset(
$_GET["wax_call"])){
        
wax_headers(); 
        
handle_call($wax_class); // Handle the function call
        
exit;
    }
}

/* The function that generates the javascript to call each function.
   */
function wax_script($wax_class){
    if (!
class_exists($wax_class))
        die(
"Class " $wax_class " does not seem to exist...");
    
$functions "";
    foreach (
get_class_methods($wax_class) as $method) {
        
$functions .= "function " $method "(){\n";
        
$functions .= "    var get_vars = '';\n";
        
$functions .= "    if (" $method ".arguments.length){\n";
        
$functions .= "        for (count = 0; count < " $method ".arguments.length; ++count){\n";
        
$functions .= "            get_vars = get_vars +  '&arg' + count.toString() + '=' + escape(" $method ".arguments[count]);\n";
        
$functions .= "        }\n";
        
$functions .= "    }\n";
        
$functions .= "    get_simple('?wax_call=" $method "'+get_vars,'eval(response)');\n";
        
$functions .= "    return false;\n";
        
$functions .= "}\n";
    }
    return 
$functions;
}

/* The function that handles the wax function calls.
   */
function handle_call($wax_class){
    
$wax_function $_GET["wax_call"];
    if (!
method_exists($wax_class$wax_function))
        die(
"Function " $wax_function " not found!");
    
$arguments = array();
    foreach (
$_GET as $key=>$val){
        if (
$key != "wax_call"){ //Ok, its an argument to the function
            
array_push($arguments,$val);
        }
    }
    echo 
call_user_func_array(array($wax_class$wax_function), $arguments);
}

/* Strips out \n and ' to make a string response safe.
   */
function make_response_safe($string){
    return 
str_replace("\n","",str_replace("'","&#39",$string));
}

if (
strpos($_SERVER["SCRIPT_FILENAME"],"wax.php") !== false){
        
header("Content-type: text/plain");
        echo 
"mmm waxy.";
}
?>