Further work on Moodle 1.9 integration.
[moodle/mihaisucan.git] / lib / editor / tinymce / jscripts / tiny_mce / plugins / spellchecker / rpc.php
blobcc8d0f2d61a5bb2a23d1fd8471ef49ef6f4b38b3
1 <?php
2 /**
3 * $Id$
5 * @author Moxiecode
6 * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
7 */
9 require_once("./includes/general.php");
11 // Set RPC response headers
12 header('Content-Type: text/plain');
13 header('Content-Encoding: UTF-8');
14 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
15 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
16 header("Cache-Control: no-store, no-cache, must-revalidate");
17 header("Cache-Control: post-check=0, pre-check=0", false);
18 header("Pragma: no-cache");
20 $raw = "";
22 // Try param
23 if (isset($_POST["json_data"]))
24 $raw = getRequestParam("json_data");
26 // Try globals array
27 if (!$raw && isset($_GLOBALS) && isset($_GLOBALS["HTTP_RAW_POST_DATA"]))
28 $raw = $_GLOBALS["HTTP_RAW_POST_DATA"];
30 // Try globals variable
31 if (!$raw && isset($HTTP_RAW_POST_DATA))
32 $raw = $HTTP_RAW_POST_DATA;
34 // Try stream
35 if (!$raw) {
36 if (!function_exists('file_get_contents')) {
37 $fp = fopen("php://input", "r");
38 if ($fp) {
39 $raw = "";
41 while (!feof($fp))
42 $raw = fread($fp, 1024);
44 fclose($fp);
46 } else
47 $raw = "" . file_get_contents("php://input");
50 // No input data
51 if (!$raw)
52 die('{"result":null,"id":null,"error":{"errstr":"Could not get raw post data.","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}');
54 // Passthrough request to remote server
55 if (isset($config['general.remote_rpc_url'])) {
56 $url = parse_url($config['general.remote_rpc_url']);
58 // Setup request
59 $req = "POST " . $url["path"] . " HTTP/1.0\r\n";
60 $req .= "Connection: close\r\n";
61 $req .= "Host: " . $url['host'] . "\r\n";
62 $req .= "Content-Length: " . strlen($raw) . "\r\n";
63 $req .= "\r\n" . $raw;
65 if (!isset($url['port']) || !$url['port'])
66 $url['port'] = 80;
68 $errno = $errstr = "";
70 $socket = fsockopen($url['host'], intval($url['port']), $errno, $errstr, 30);
71 if ($socket) {
72 // Send request headers
73 fputs($socket, $req);
75 // Read response headers and data
76 $resp = "";
77 while (!feof($socket))
78 $resp .= fgets($socket, 4096);
80 fclose($socket);
82 // Split response header/data
83 $resp = explode("\r\n\r\n", $resp);
84 echo $resp[1]; // Output body
87 die();
90 // Get JSON data
91 $json = new Moxiecode_JSON();
92 $input = $json->decode($raw);
94 // Execute RPC
95 if (isset($config['general.engine'])) {
96 $spellchecker = new $config['general.engine']($config);
97 $result = call_user_func_array(array($spellchecker, $input['method']), $input['params']);
98 } else
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"}}');
101 // Request and response id should always be the same
102 $output = array(
103 "id" => $input->id,
104 "result" => $result,
105 "error" => null
108 // Return JSON encoded string
109 echo $json->encode($output);