Subversion Repositories cryptochat

Rev

Blame | Last modification | View Log | RSS feed

  1. <?php
  2. if (!isset($SAJAX_INCLUDED)) {
  3.        
  4.         /*
  5.          * GLOBALS AND DEFAULTS
  6.          *
  7.          */
  8.         $GLOBALS['sajax_version'] = "0.13";
  9.         $GLOBALS['sajax_debug_mode'] = false;
  10.         $GLOBALS['sajax_export_array'] = array();
  11.         $GLOBALS['sajax_export_list'] = array();
  12.         $GLOBALS['sajax_remote_uri'] = "";
  13.         $GLOBALS['sajax_failure_redirect'] = "";
  14.         $GLOBALS['sajax_request_type'] = "GET";
  15.        
  16.         /*
  17.          * CODE
  18.          *
  19.          */
  20.        
  21.         function sajax_handle_client_request() {
  22.                 if (empty($_GET["rs"]) && empty($_POST["rs"]))
  23.                         return;
  24.                
  25.                 ob_start();
  26.                
  27.                 if (!empty($_GET["rs"])) {
  28.                         // Always call server
  29.                         header ("Cache-Control: max-age=0, must-revalidate");   // HTTP/1.1
  30.                         header ("Pragma: no-cache");                                                    // HTTP/1.0
  31.                         $func_name = $_GET["rs"];
  32.                         if (! empty($_GET["rsargs"]))
  33.                                 $args = $_GET["rsargs"];
  34.                 } else {
  35.                         $func_name = $_POST["rs"];
  36.                         if (! empty($_POST["rsargs"]))
  37.                                 $args = $_POST["rsargs"];
  38.                 }
  39.                
  40.                 if(! empty($args)) {
  41.                         if(get_magic_quotes_gpc())
  42.                                 $args = stripslashes($args);
  43.                        
  44.                         $args = json_decode($args, true);
  45.                        
  46.                         if(get_magic_quotes_gpc()) {
  47.                                 function array_addslashes($value) {
  48.                                         if(is_array($value))
  49.                                                 return array_map("array_addslashes", $value);
  50.                                         else
  51.                                                 return addslashes($value);
  52.                                 }
  53.                                
  54.                                 $args = array_map("array_addslashes", $args);
  55.                         }
  56.                 } else {
  57.                         $args = array();
  58.                 }
  59.                
  60.                 global $sajax_export_list;
  61.                
  62.                 if (! in_array($func_name, $sajax_export_list)) {
  63.                         $error = $func_name." not callable";
  64.                 } else {
  65.                         $result = call_user_func_array($func_name, $args);
  66.                        
  67.                         $error = ob_get_contents();
  68.                         ob_end_clean();
  69.                 }
  70.                
  71.                 header('Content-Type: text/plain; charset=UTF-8');
  72.                 if(!empty($error)) {
  73.                         echo '-:'.$error;
  74.                 } else {
  75.                         echo "+:".json_encode($result);
  76.                 }
  77.                 exit;
  78.         }
  79.        
  80.         $sajax_js_has_been_shown = false;
  81.         function sajax_show_javascript()
  82.         {
  83.                 global $sajax_js_has_been_shown;
  84.                 global $sajax_debug_mode;
  85.                 global $sajax_failure_redirect;
  86.                
  87.                 if (! $sajax_js_has_been_shown) {
  88.                        
  89. ?>
  90.         sajax_debug_mode = <?php echo($sajax_debug_mode ? "true" : "false"); ?>;
  91.         sajax_failure_redirect = "<?php echo($sajax_failure_redirect); ?>";
  92. <?php
  93.                         global $sajax_export_array;
  94.                         foreach($sajax_export_array as $function) {
  95. ?>
  96.         function x_<?php echo($function["name"]); ?>() {
  97.                 return sajax_do_call("<?php echo($function["name"]); ?>", arguments, "<?php echo($function["method"]); ?>", <?php echo($function["asynchronous"] ? 'true' : 'false'); ?>, "<?php echo($function["uri"]); ?>");
  98.         }
  99. <?php
  100.                         }
  101.                         $sajax_js_has_been_shown = true;
  102.                 }
  103.         }
  104.        
  105.         function sajax_export() {
  106.                 global $sajax_export_array;
  107.                 global $sajax_export_list;
  108.                 global $sajax_request_type;
  109.                 global $sajax_remote_uri;
  110.                
  111.                 $num = func_num_args();
  112.                 for ($i=0; $i<$num; $i++) {
  113.                         $function = func_get_arg($i);
  114.                        
  115.                         if(!is_array($function))
  116.                                 $function = array("name" => $function);
  117.                        
  118.                         if(!isset($function["method"]))
  119.                                 $function["method"] = $sajax_request_type;
  120.                        
  121.                         if(!isset($function["asynchronous"]))
  122.                                 $function["asynchronous"] = true;
  123.                        
  124.                         if(!isset($function["uri"]))
  125.                                 $function["uri"] = $sajax_remote_uri;
  126.                        
  127.                         $key = array_search($function["name"], $sajax_export_list);
  128.                         if ($key === false) {
  129.                                 $sajax_export_array[] = $function;
  130.                                 $sajax_export_list[] = $function["name"];
  131.                         } else {
  132.                                 //Overwrite old function
  133.                                 $sajax_export_array[$key] = $function;
  134.                                 $sajax_export_list[$key] = $function["name"];
  135.                         }
  136.                 }
  137.         }
  138.        
  139.         $SAJAX_INCLUDED = 1;
  140. }
  141. ?>