mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * $Id: rpc.php 822 2008-04-28 13:45:03Z spocke $
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package MCManager.includes
mp-wp_genesis 6 * @author Moxiecode
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 require_once("./includes/general.php");
mp-wp_genesis 10
mp-wp_genesis 11 // Set RPC response headers
mp-wp_genesis 12 header('Content-Type: text/plain');
mp-wp_genesis 13 header('Content-Encoding: UTF-8');
mp-wp_genesis 14 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
mp-wp_genesis 15 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
mp-wp_genesis 16 header("Cache-Control: no-store, no-cache, must-revalidate");
mp-wp_genesis 17 header("Cache-Control: post-check=0, pre-check=0", false);
mp-wp_genesis 18 header("Pragma: no-cache");
mp-wp_genesis 19
mp-wp_genesis 20 $raw = "";
mp-wp_genesis 21
mp-wp_genesis 22 // Try param
mp-wp_genesis 23 if (isset($_POST["json_data"]))
mp-wp_genesis 24 $raw = getRequestParam("json_data");
mp-wp_genesis 25
mp-wp_genesis 26 // Try globals array
mp-wp_genesis 27 if (!$raw && isset($_GLOBALS) && isset($_GLOBALS["HTTP_RAW_POST_DATA"]))
mp-wp_genesis 28 $raw = $_GLOBALS["HTTP_RAW_POST_DATA"];
mp-wp_genesis 29
mp-wp_genesis 30 // Try globals variable
mp-wp_genesis 31 if (!$raw && isset($HTTP_RAW_POST_DATA))
mp-wp_genesis 32 $raw = $HTTP_RAW_POST_DATA;
mp-wp_genesis 33
mp-wp_genesis 34 // Try stream
mp-wp_genesis 35 if (!$raw) {
mp-wp_genesis 36 if (!function_exists('file_get_contents')) {
mp-wp_genesis 37 $fp = fopen("php://input", "r");
mp-wp_genesis 38 if ($fp) {
mp-wp_genesis 39 $raw = "";
mp-wp_genesis 40
mp-wp_genesis 41 while (!feof($fp))
mp-wp_genesis 42 $raw = fread($fp, 1024);
mp-wp_genesis 43
mp-wp_genesis 44 fclose($fp);
mp-wp_genesis 45 }
mp-wp_genesis 46 } else
mp-wp_genesis 47 $raw = "" . file_get_contents("php://input");
mp-wp_genesis 48 }
mp-wp_genesis 49
mp-wp_genesis 50 // No input data
mp-wp_genesis 51 if (!$raw)
mp-wp_genesis 52 die('{"result":null,"id":null,"error":{"errstr":"Could not get raw post data.","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}');
mp-wp_genesis 53
mp-wp_genesis 54 // Passthrough request to remote server
mp-wp_genesis 55 if (isset($config['general.remote_rpc_url'])) {
mp-wp_genesis 56 $url = parse_url($config['general.remote_rpc_url']);
mp-wp_genesis 57
mp-wp_genesis 58 // Setup request
mp-wp_genesis 59 $req = "POST " . $url["path"] . " HTTP/1.0\r\n";
mp-wp_genesis 60 $req .= "Connection: close\r\n";
mp-wp_genesis 61 $req .= "Host: " . $url['host'] . "\r\n";
mp-wp_genesis 62 $req .= "Content-Length: " . strlen($raw) . "\r\n";
mp-wp_genesis 63 $req .= "\r\n" . $raw;
mp-wp_genesis 64
mp-wp_genesis 65 if (!isset($url['port']) || !$url['port'])
mp-wp_genesis 66 $url['port'] = 80;
mp-wp_genesis 67
mp-wp_genesis 68 $errno = $errstr = "";
mp-wp_genesis 69
mp-wp_genesis 70 $socket = fsockopen($url['host'], intval($url['port']), $errno, $errstr, 30);
mp-wp_genesis 71 if ($socket) {
mp-wp_genesis 72 // Send request headers
mp-wp_genesis 73 fputs($socket, $req);
mp-wp_genesis 74
mp-wp_genesis 75 // Read response headers and data
mp-wp_genesis 76 $resp = "";
mp-wp_genesis 77 while (!feof($socket))
mp-wp_genesis 78 $resp .= fgets($socket, 4096);
mp-wp_genesis 79
mp-wp_genesis 80 fclose($socket);
mp-wp_genesis 81
mp-wp_genesis 82 // Split response header/data
mp-wp_genesis 83 $resp = explode("\r\n\r\n", $resp);
mp-wp_genesis 84 echo $resp[1]; // Output body
mp-wp_genesis 85 }
mp-wp_genesis 86
mp-wp_genesis 87 die();
mp-wp_genesis 88 }
mp-wp_genesis 89
mp-wp_genesis 90 // Get JSON data
mp-wp_genesis 91 $json = new Moxiecode_JSON();
mp-wp_genesis 92 $input = $json->decode($raw);
mp-wp_genesis 93
mp-wp_genesis 94 // Execute RPC
mp-wp_genesis 95 if (isset($config['general.engine'])) {
mp-wp_genesis 96 $spellchecker = new $config['general.engine']($config);
mp-wp_genesis 97 $result = call_user_func_array(array($spellchecker, $input['method']), $input['params']);
mp-wp_genesis 98 } else
mp-wp_genesis 99 die('{"result":null,"id":null,"error":{"errstr":"You must choose an spellchecker engine in the config.php file.","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}');
mp-wp_genesis 100
mp-wp_genesis 101 // Request and response id should always be the same
mp-wp_genesis 102 $output = array(
mp-wp_genesis 103 "id" => $input->id,
mp-wp_genesis 104 "result" => $result,
mp-wp_genesis 105 "error" => null
mp-wp_genesis 106 );
mp-wp_genesis 107
mp-wp_genesis 108 // Return JSON encoded string
mp-wp_genesis 109 echo $json->encode($output);
mp-wp_genesis 110
mp-wp_genesis 111 ?>