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