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
CommitLineData
09ae4cad
PS
1<?php
2/**
3 * $Id$
4 *
5 * @author Moxiecode
6 * @copyright Copyright � 2004-2007, Moxiecode Systems AB, All rights reserved.
7 */
8
9// require moodle configuration data
6aa9d952 10require_once(dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))))).'/config.php');
09ae4cad
PS
11require_once("./includes/general.php");
12
13// Set RPC response headers
14header('Content-Type: text/plain');
15header('Content-Encoding: UTF-8');
16header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
17header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
18header("Cache-Control: no-store, no-cache, must-revalidate");
19header("Cache-Control: post-check=0, pre-check=0", false);
20header("Pragma: no-cache");
21
22$raw = "";
23
24// Try param
25if (isset($_POST["json_data"]))
26 $raw = getRequestParam("json_data");
27
28// Try globals array
29if (!$raw && isset($_GLOBALS) && isset($_GLOBALS["HTTP_RAW_POST_DATA"]))
30 $raw = $_GLOBALS["HTTP_RAW_POST_DATA"];
31
32// Try globals variable
33if (!$raw && isset($HTTP_RAW_POST_DATA))
34 $raw = $HTTP_RAW_POST_DATA;
35
36// Try stream
37if (!$raw) {
38 if (!function_exists('file_get_contents')) {
39 $fp = fopen("php://input", "r");
40 if ($fp) {
41 $raw = "";
42
43 while (!feof($fp))
44 $raw = fread($fp, 1024);
45
46 fclose($fp);
47 }
48 } else
49 $raw = "" . file_get_contents("php://input");
50}
51
52// No input data
53if (!$raw)
54 die('{"result":null,"id":null,"error":{"errstr":"Could not get raw post data.","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}');
55
56// Passthrough request to remote server
57if (isset($config['general.remote_rpc_url'])) {
58 $url = parse_url($config['general.remote_rpc_url']);
59
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;
66
67 if (!isset($url['port']) || !$url['port'])
68 $url['port'] = 80;
69
70 $errno = $errstr = "";
71
72 $socket = fsockopen($url['host'], intval($url['port']), $errno, $errstr, 30);
73 if ($socket) {
74 // Send request headers
75 fputs($socket, $req);
76
77 // Read response headers and data
78 $resp = "";
79 while (!feof($socket))
80 $resp .= fgets($socket, 4096);
81
82 fclose($socket);
83
84 // Split response header/data
85 $resp = explode("\r\n\r\n", $resp);
86 echo $resp[1]; // Output body
87 }
88
89 die();
90}
91
92// Get JSON data
93$json = new Moxiecode_JSON();
94$input = $json->decode($raw);
95
96// Execute RPC
97if (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"}}');
102
103// Request and response id should always be the same
104$output = array(
105 "id" => $input->id,
106 "result" => $result,
107 "error" => null
108);
109
110// Return JSON encoded string
111echo $json->encode($output);
112
113?>