|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00010 require_once("./includes/general.php"); 00011 00012 // Set RPC response headers 00013 header('Content-Type: text/plain'); 00014 header('Content-Encoding: UTF-8'); 00015 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 00016 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 00017 header("Cache-Control: no-store, no-cache, must-revalidate"); 00018 header("Cache-Control: post-check=0, pre-check=0", false); 00019 header("Pragma: no-cache"); 00020 00021 $raw = ""; 00022 00023 // Try param 00024 if (isset($_POST["json_data"])) 00025 $raw = getRequestParam("json_data"); 00026 00027 // Try globals array 00028 if (!$raw && isset($_GLOBALS) && isset($_GLOBALS["HTTP_RAW_POST_DATA"])) 00029 $raw = $_GLOBALS["HTTP_RAW_POST_DATA"]; 00030 00031 // Try globals variable 00032 if (!$raw && isset($HTTP_RAW_POST_DATA)) 00033 $raw = $HTTP_RAW_POST_DATA; 00034 00035 // Try stream 00036 if (!$raw) { 00037 if (!function_exists('file_get_contents')) { 00038 $fp = fopen("php://input", "r"); 00039 if ($fp) { 00040 $raw = ""; 00041 00042 while (!feof($fp)) 00043 $raw = fread($fp, 1024); 00044 00045 fclose($fp); 00046 } 00047 } else 00048 $raw = "" . file_get_contents("php://input"); 00049 } 00050 00051 // No input data 00052 if (!$raw) 00053 die('{"result":null,"id":null,"error":{"errstr":"Could not get raw post data.","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}'); 00054 00055 // Passthrough request to remote server 00056 if (isset($config['general.remote_rpc_url'])) { 00057 $url = parse_url($config['general.remote_rpc_url']); 00058 00059 // Setup request 00060 $req = "POST " . $url["path"] . " HTTP/1.0\r\n"; 00061 $req .= "Connection: close\r\n"; 00062 $req .= "Host: " . $url['host'] . "\r\n"; 00063 $req .= "Content-Length: " . strlen($raw) . "\r\n"; 00064 $req .= "\r\n" . $raw; 00065 00066 if (!isset($url['port']) || !$url['port']) 00067 $url['port'] = 80; 00068 00069 $errno = $errstr = ""; 00070 00071 $socket = fsockopen($url['host'], intval($url['port']), $errno, $errstr, 30); 00072 if ($socket) { 00073 // Send request headers 00074 fputs($socket, $req); 00075 00076 // Read response headers and data 00077 $resp = ""; 00078 while (!feof($socket)) 00079 $resp .= fgets($socket, 4096); 00080 00081 fclose($socket); 00082 00083 // Split response header/data 00084 $resp = explode("\r\n\r\n", $resp); 00085 echo $resp[1]; // Output body 00086 } 00087 00088 die(); 00089 } 00090 00091 // Get JSON data 00092 $json = new Moxiecode_JSON(); 00093 $input = $json->decode($raw); 00094 00095 // Execute RPC 00096 if (isset($config['general.engine'])) { 00097 $spellchecker = new $config['general.engine']($config); 00098 $result = call_user_func_array(array($spellchecker, $input['method']), $input['params']); 00099 } else 00100 die('{"result":null,"id":null,"error":{"errstr":"You must choose an spellchecker engine in the config.php file.","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}'); 00101 00102 // Request and response id should always be the same 00103 $output = array( 00104 "id" => $input->id, 00105 "result" => $result, 00106 "error" => null 00107 ); 00108 00109 // Return JSON encoded string 00110 echo $json->encode($output); 00111 00112 ?>